
    kh}?                     &   d dl Z d dlZd dlZd dlmZmZmZmZmZm	Z	m
Z
 d dlZd dlZd dlmZ d dlmZmZ d dlmZmZ d dlmZ ddlmZmZ dd	lmZmZm Z m!Z!m"Z"m#Z#  G d
 de      Z$ G d de      Z% G d de%      Z& G d de%      Z' G d de      Z(y)    N)AnyCallableDictListOptionalSequenceUnion)one_hot)tree_flattentree_unflatten)
transforms
tv_tensors)
functional   )_RandomApplyTransform	Transform)_check_sequence_input_parse_labels_getterhas_anyis_pure_tensor	query_chw
query_sizec                        e Zd ZdZej
                  Zdeee	f   f fdZ
	 	 	 	 	 ddedee   dee   dedef
 fd	Zd
ede	de	de	de	f
 fdZdee	   deee	f   fdZde	deee	f   de	fdZ xZS )RandomErasingaN  Randomly select a rectangle region in the input image or video and erase its pixels.

    This transform does not support PIL Image.
    'Random Erasing Data Augmentation' by Zhong et al. See https://arxiv.org/abs/1708.04896

    Args:
        p (float, optional): probability that the random erasing operation will be performed.
        scale (tuple of float, optional): range of proportion of erased area against input image.
        ratio (tuple of float, optional): range of aspect ratio of erased area.
        value (number or tuple of numbers): erasing value. Default is 0. If a single int, it is used to
            erase all pixels. If a tuple of length 3, it is used to erase
            R, G, B channels respectively.
            If a str of 'random', erasing each pixel with random values.
        inplace (bool, optional): boolean to make this transform inplace. Default set to False.

    Returns:
        Erased input.

    Example:
        >>> from torchvision.transforms import v2 as transforms
        >>>
        >>> transform = transforms.Compose([
        >>>   transforms.RandomHorizontalFlip(),
        >>>   transforms.PILToTensor(),
        >>>   transforms.ConvertImageDtype(torch.float),
        >>>   transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
        >>>   transforms.RandomErasing(),
        >>> ])
    returnc                 p    t        t        | 	         | j                  d      S | j                        S )Nrandom)value)dictsuper _extract_params_for_v1_transformr   )self	__class__s    ^/var/www/teggl/fontify/venv/lib/python3.12/site-packages/torchvision/transforms/v2/_augment.pyr!   z.RandomErasing._extract_params_for_v1_transform2   s8    G46"jj0(
 	
6:jj
 	
    pscaleratior   inplacec                 b   t         |   |       t        |t        j                  t
        t        t        f      st        d      t        |t
              r|dk7  rt        d      t        |t              st        d      t        |t              st        d      |d   |d   kD  s|d   |d   kD  rt        j                  d	       |d   dk  s|d   dkD  rt        d
      || _        || _        t        |t        t         f      rt!        |      g| _        nTt        |t
              rd | _        n<t        |t        t        f      r|D cg c]  }t!        |       c}| _        n|| _        || _        t'        j(                  t'        j*                  | j                              | _        y c c}w )N)r&   z=Argument value should be either a number or str or a sequencer   z&If value is str, it should be 'random'zScale should be a sequencezRatio should be a sequencer   r   z,Scale and ratio should be of kind (min, max)zScale should be between 0 and 1)r    __init__
isinstancenumbersNumberstrtuplelist	TypeError
ValueErrorr   warningswarnr'   r(   intfloatr   r)   torchlogtensor
_log_ratio)r"   r&   r'   r(   r   r)   vr#   s          r$   r+   zRandomErasing.__init__8   s\    	1%'..#ud!CD[\\eS!ex&7EFF%*899%*899!HuQxU1Xa%8MMHI8a<58a<>??

ec5\*,DJs#DJe}-,12q%(2DJDJ))ELL$<= 3s   F,r   inptargskwargsc                    t        |t        j                  t        j                  f      rAt	        j
                  t        |       j                   dt        |      j                   d       t        | $  ||g|i |S )Nz:() is currently passing through inputs of type tv_tensors.z(. This will likely change in the future.)
r,   r   BoundingBoxesMaskr4   r5   type__name__r    _call_kernel)r"   r   r=   r>   r?   r#   s        r$   rE   zRandomErasing._call_kernel[   st    dZ55zGHMM:&&' ("4j1122Z\ w#JFtFvFFr%   flat_inputsc           	         t        |      \  }}}| j                  (t        | j                        d|fvrt        d| d      ||z  }| j                  }t        d      D ]  }|t        j                  d      j                  | j                  d   | j                  d         j                         z  }t        j                  t        j                  d      j                  |d   |d               j                         }	t        t        t        j                  ||	z                    }
t        t        t        j                  ||	z                    }|
|k  r||k  s| j                  7t        j                  ||
|gt        j                         j#                         }n't        j$                  | j                        d d d d f   }t        j&                  d||
z
  dz   d      j                         }t        j&                  d||z
  dz   d      j                         } n dd||d f\  }}}
}}t)        |||
||	      S )
Nr   z@If value is a sequence, it should have either a single value or z (number of inpt channels)
   r   )dtyper   size)ijhwr<   )r   r   lenr3   r;   ranger8   emptyuniform_r'   itemexpr6   roundmathsqrtfloat32normal_r:   randintr   )r"   rF   img_cimg_himg_warea	log_ratio_
erase_areaaspect_ratiorO   rP   r<   rM   rN   s                  r$   make_paramszRandomErasing.make_paramsc   s   '4ue::!3tzz?q%j+HRSXRYYst  u}OO	r 	5AA 7 7

1tzzRS} U Z Z \\J 99A''aLaL
 df  E$))J$=>?@AE$))J$=>?@AI!e)zz!KK1U]]CKKMLL,Qd];aQT:??AAaQT:??AA+	5. q%4MAq!Qa1Q!,,r%   paramsc                 p    |d   0 | j                   t        j                  |fi |d| j                  i}|S )Nr<   r)   )rE   Feraser)   r"   r=   rf   s      r$   	transformzRandomErasing.transform   s7    #;"$4$$QWWdSfSdllSDr%   )      ?)g{Gz?gQ?)g333333?gffffff
@g        F)rD   
__module____qualname____doc___transformsr   _v1_transform_clsr   r/   r   r!   r7   r   boolr+   r   rE   r   re   rk   __classcell__r#   s   @r$   r   r      s    < $11
$sCx. 
 !-!+!>!> !> 	!>
 !> !>FGx Gs G3 GRU GZ] G$-tCy $-T#s(^ $-Lc 4S> c r%   r   c                        e Zd Zdddddedee   ddf fdZd	 Zd
ej                  defdZ
dej                  dedej                  fdZ xZS )_BaseMixUpCutMix      ?Ndefault)alphanum_classeslabels_getterry   rz   r   c                
   t         |           t        |      | _        t        j
                  j                  t	        j                  |g      t	        j                  |g            | _        || _	        t        |      | _        y N)r    r+   r7   ry   r8   distributionsBetar:   _distrz   r   _labels_getter)r"   ry   rz   r{   r#   s       r$   r+   z_BaseMixUpCutMix.__init__   s`    5\
((--ellE7.CU\\SXRYEZ[
&2=Ar%   c           	      l  	 t        |      dkD  r|n|d   }t        |      \  }}| j                  |      }t        |t        j
                  j
                  t        j                  t        j                        r!t        t        |       j                   d      | j                  |      	t        	t        j                        st        dt        	       d      	j                   dvrt        d	j"                   d      	j                   dk(  rQ| j$                  E	j"                  d	   | j$                  k7  r)t        d
	j"                  d	    d| j$                   d      	j                   dk(  r| j$                  t        d      		j"                  d   d| j'                  t)        ||      D cg c]
  \  }}|s	| c}}      }d|t+        	fdt-        |      D              <   t)        ||      D cg c]  \  }}|r| j/                  ||      n| }}}t1        ||      S c c}}w c c}}w )Nr   r   z9() does not support PIL images, bounding boxes and masks.z%The labels must be a tensor, but got z	 instead.)r      zlabels should be index based with shape (batch_size,) or probability based with shape (batch_size, num_classes), but got a tensor of shape r   zYWhen passing 2D labels, the number of elements in last dimension must match num_classes:  != z$. You can Leave num_classes to None.z=num_classes must be passed if the labels are index-based (1D))labels
batch_sizeTc              3   2   K   | ]  \  }}|u s|  y wr}    ).0idxr=   r   s      r$   	<genexpr>z+_BaseMixUpCutMix.forward.<locals>.<genexpr>   s     !`)#tQUY_Q_#!`s   )rQ   r   _needs_transform_listr   PILImager   rA   rB   r3   rC   rD   r   r,   r8   Tensorndimshaperz   re   zipnext	enumeraterk   r   )
r"   inputsrF   specneeds_transform_listr=   needs_transformrf   flat_outputsr   s
            @r$   forwardz_BaseMixUpCutMix.forward   s+   v;?q	(0T#99+F;		1I1I:??[T
 3 344mnoo$$V,&%,,/DT&\NR[\]];;f$--3\\N)E 
 ;;! 0 0 <bAQUYUeUeAeT<<#$D)9)9(: ;56  ;;! 0 0 8\]]  ,,q/
 58FZ5[o14_no
 eiT!`y7M!``a ,/{<P+Q
' -<DNN4(E
 

 lD11 p
s   4
H*?H*; H0r=   r   c                    t        |t        j                        rdnd}|j                  |k7  rt	        d| d|j                   d      |j
                  d   |k7  rt	        d|j
                  d    d| d	      y )
N      zExpected a batched input with z dims, but got z dimensions instead.r   zRThe batch size of the image or video does not match the batch size of the labels: r   .)r,   r   Videor   r3   r   )r"   r=   r   expected_num_dimss       r$   _check_image_or_videoz&_BaseMixUpCutMix._check_image_or_video   s    !+D*2B2B!CA99))01B0C?SWS\S\R]]qr  ::a=J&d::a=/j\4  'r%   labellamc                   |j                   dk(  rt        || j                        }|j                  j                  s|j                         }|j                  dd      j                  d|z
        j                  |j                  |            S )Nr   )rz   r   rw   )
r   r
   rz   rI   is_floating_pointr7   rollmul_add_mul)r"   r   r   s      r$   _mixup_labelz_BaseMixUpCutMix._mixup_label   sj    ::?Et/?/?@E{{,,KKMEzz!Q$$S3Y/44UYYs^DDr%   )rD   rm   rn   r7   r   r6   r+   r   r8   r   r   r   rs   rt   s   @r$   rv   rv      sq    ),4_h B B8C= Bmq B+2Z
%,, 
s 
E%,, E E%,, Er%   rv   c                   N    e Zd ZdZdee   deeef   fdZdedeeef   defdZ	y)	MixUpa  Apply MixUp to the provided batch of images and labels.

    Paper: `mixup: Beyond Empirical Risk Minimization <https://arxiv.org/abs/1710.09412>`_.

    .. note::
        This transform is meant to be used on **batches** of samples, not
        individual images. See
        :ref:`sphx_glr_auto_examples_transforms_plot_cutmix_mixup.py` for detailed usage
        examples.
        The sample pairing is deterministic and done by matching consecutive
        samples in the batch, so the batch needs to be shuffled (this is an
        implementation detail, not a guaranteed convention.)

    In the input, the labels are expected to be a tensor of shape ``(batch_size,)``. They will be transformed
    into a tensor of shape ``(batch_size, num_classes)``.

    Args:
        alpha (float, optional): hyperparameter of the Beta distribution used for mixup. Default is 1.
        num_classes (int, optional): number of classes in the batch. Used for one-hot-encoding.
            Can be None only if the labels are already one-hot-encoded.
        labels_getter (callable or "default", optional): indicates how to identify the labels in the input.
            By default, this will pick the second parameter as the labels if it's a tensor. This covers the most
            common scenario where this transform is called as ``MixUp()(imgs_batch, labels_batch)``.
            It can also be a callable that takes the same input as the transform, and returns the labels.
    rF   r   c                 ^    t        t        | j                  j                  d                  S )Nr   r   )r   r7   r   sample)r"   rF   s     r$   re   zMixUp.make_params   s!    djj//3455r%   r=   rf   c                    |d   }||d   u r| j                  ||      S t        |t        j                  t        j                  f      st        |      r| j                  ||d          |j                  dd      j                  d|z
        j                  |j                  |            }t        |t        j                  t        j                  f      rt        j                  ||	      }|S |S )
Nr   r   r   r   r   r   r   rw   like)r   r,   r   r   r   r   r   r   r   r   r   wrap)r"   r=   rf   r   outputs        r$   rk   zMixUp.transform   s    Um6(##$$Ts$33z//1A1ABC~VZG[&&t|8L&MYYq!_))#)499$((3-HF$!1!1:3C3C DE#d;MKr%   N
rD   rm   rn   ro   r   r   r   r/   re   rk   r   r%   r$   r   r      sH    46tCy 6T#s(^ 6c 4S> c r%   r   c                   N    e Zd ZdZdee   deeef   fdZdedeeef   defdZ	y)	CutMixa  Apply CutMix to the provided batch of images and labels.

    Paper: `CutMix: Regularization Strategy to Train Strong Classifiers with Localizable Features
    <https://arxiv.org/abs/1905.04899>`_.

    .. note::
        This transform is meant to be used on **batches** of samples, not
        individual images. See
        :ref:`sphx_glr_auto_examples_transforms_plot_cutmix_mixup.py` for detailed usage
        examples.
        The sample pairing is deterministic and done by matching consecutive
        samples in the batch, so the batch needs to be shuffled (this is an
        implementation detail, not a guaranteed convention.)

    In the input, the labels are expected to be a tensor of shape ``(batch_size,)``. They will be transformed
    into a tensor of shape ``(batch_size, num_classes)``.

    Args:
        alpha (float, optional): hyperparameter of the Beta distribution used for mixup. Default is 1.
        num_classes (int, optional): number of classes in the batch. Used for one-hot-encoding.
            Can be None only if the labels are already one-hot-encoded.
        labels_getter (callable or "default", optional): indicates how to identify the labels in the input.
            By default, this will pick the second parameter as the labels if it's a tensor. This covers the most
            common scenario where this transform is called as ``CutMix()(imgs_batch, labels_batch)``.
            It can also be a callable that takes the same input as the transform, and returns the labels.
    rF   r   c                    t        | j                  j                  d            }t        |      \  }}t	        j
                  |d      }t	        j
                  |d      }dt        j                  d|z
        z  }t        ||z        }t        ||z        }	t        t	        j                  ||z
  d            }
t        t	        j                  ||	z
  d            }t        t	        j                  ||z   |            }t        t	        j                  ||	z   |            }|
|||f}t        d||
z
  ||z
  z  ||z  z  z
        }t        ||	      S )
Nr   rJ   rK   rl   rw   r   )min)max)boxlam_adjusted)r7   r   r   r   r8   r\   rX   rY   r6   clampr   )r"   rF   r   HWr_xr_yrr_w_halfr_h_halfx1y1x2y2r   r   s                   r$   re   zCutMix.make_params'  s   DJJ%%b)*+&1mmAD)mmAD)$))C#I&&q1u:q1u:S8^34S8^34S8^34S8^342r2SBGR#8AE#BBC,77r%   r=   rf   c                    ||d   u r| j                  ||d         S t        |t        j                  t        j                  f      st        |      r| j                  ||d          |d   \  }}}}|j                  dd      }|j                         }|d	||||f   |d	||||f<   t        |t        j                  t        j                  f      rt        j                  ||
      }|S |S )Nr   r   r   r   r   r   r   r   .r   )
r   r,   r   r   r   r   r   r   cloner   )	r"   r=   rf   r   r   r   r   rolledr   s	            r$   rk   zCutMix.transform=  s    6(##$$Tvn/E$FFz//1A1ABC~VZG[&&t|8L&M#E]NBBYYq!_FZZ\F(.sBrE2b5/@(AF32r"u$%$!1!1:3C3C DE#d;MKr%   Nr   r   r%   r$   r   r     sH    68tCy 8T#s(^ 8,c 4S> c r%   r   c                   v     e Zd ZdZdeeee   f   f fdZdee	   de
ee	f   fdZde	de
ee	f   de	fd	Z xZS )
JPEGa>  Apply JPEG compression and decompression to the given images.

    If the input is a :class:`torch.Tensor`, it is expected
    to be of dtype uint8, on CPU, and have [..., 3 or 1, H, W] shape,
    where ... means an arbitrary number of leading dimensions.

    Args:
        quality (sequence or number): JPEG quality, from 1 to 100. Lower means more compression.
            If quality is a sequence like (min, max), it specifies the range of JPEG quality to
            randomly select from (inclusive of both ends).

    Returns:
        image with JPEG compression.
    qualityc                 H   t         |           t        |t              r t        |t              rt        d      ||g}nt        |dd       d|d   cxk  r|d   cxk  rdk  r)n n&t        |d   t              rt        |d   t              st        d|      || _        y )	Nzquality can't be boolr   )r   )	req_sizesr   r   d   z7quality must be an integer from 1 to 100, got quality =)	r    r+   r,   r6   rr   r2   r   r3   r   )r"   r   r#   s     r$   r+   zJPEG.__init__`  s    gs#'4( 788(G!'9EWQZ471:44GAJPS9TYcdklmdnpsYtWg[YZZr%   rF   r   c                     t        j                  | j                  d   | j                  d   dz   d      j                         }t	        |      S )Nr   r   r   r   )r8   r\   r   rU   r   )r"   rF   r   s      r$   re   zJPEG.make_paramsn  s=    --Qa11DbINNPG$$r%   r=   rf   c                 L    | j                  t        j                  ||d         S )Nr   r   )rE   rh   jpegrj   s      r$   rk   zJPEG.transformr  s#      vi7H IIr%   )rD   rm   rn   ro   r	   r6   r   r+   r   r   r   r/   re   rk   rs   rt   s   @r$   r   r   P  sg    c8C=&8 9 %tCy %T#s(^ %Jc J4S> Jc Jr%   r   ))rX   r-   r4   typingr   r   r   r   r   r   r	   	PIL.Imager   r8   torch.nn.functionalr
   torch.utils._pytreer   r   torchvisionr   rp   r   torchvision.transforms.v2r   rh   
_transformr   r   _utilsr   r   r   r   r   r   r   rv   r   r   r   r   r%   r$   <module>r      s       G G G   ' < = 5 8 o o|) |~HEy HEV- -`B BJ#J9 #Jr%   