o
    )iSn                     @   s   d dl Z d dlmZ d dlmZmZmZmZ d dlZd dlm	Z	 ddl
mZmZ g dZde	d	ed
ededeee  f
ddZG dd deZG dd dejjZG dd dejjZG dd dejjZG dd dejjZdS )    N)Enum)DictListOptionalTuple)Tensor   )
functionalInterpolationMode)AutoAugmentPolicyAutoAugmentRandAugmentTrivialAugmentWideAugMiximgop_name	magnitudeinterpolationfillc                 C   s  |dkrt j| dddgdtt|dg||ddgd} | S |dkr>t j| dddgddtt|g||ddgd} | S |dkrVt j| dt|dgd|ddg|d} | S |d	krnt j| ddt|gd|ddg|d} | S |d
kr}t j| |||d} | S |dkrt | d| } | S |dkrt | d| } | S |dkrt 	| d| } | S |dkrt 
| d| } | S |dkrt | t|} | S |dkrt | |} | S |dkrt | } | S |dkrt | } | S |dkrt | } | S |dkr	 | S td| d)NShearX        r         ?)angle	translatescaleshearr   r   centerShearY
TranslateX)r   r   r   r   r   r   
TranslateYRotater   r   
BrightnessColorContrast	Sharpness	PosterizeSolarizeAutoContrastEqualizeInvertIdentityzThe provided operator  is not recognized.)FZaffinemathdegreesatanintrotateZadjust_brightnessZadjust_saturationZadjust_contrastZadjust_sharpnessZ	posterizeZsolarizeZautocontrastZequalizeinvert
ValueError)r   r   r   r   r    r5   q/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/torchvision/transforms/autoaugment.py	_apply_op   s   C6
+
!
	

r7   c                   @   s   e Zd ZdZdZdZdZdS )r   zoAutoAugment policies learned on different datasets.
    Available policies are IMAGENET, CIFAR10 and SVHN.
    ZimagenetZcifar10ZsvhnN)__name__
__module____qualname____doc__IMAGENETCIFAR10SVHNr5   r5   r5   r6   r   ]   s
    r   c                       s   e Zd ZdZejejdfdededee	e
  ddf fddZdede	eeee
ee f eee
ee f f  fd	d
Zdedeeef deeeeef f fddZededeeeef fddZdedefddZdefddZ  ZS )r   a?  AutoAugment data augmentation method based on
    `"AutoAugment: Learning Augmentation Strategies from Data" <https://arxiv.org/pdf/1805.09501.pdf>`_.
    If the image is torch Tensor, it should be of type torch.uint8, and it is expected
    to have [..., 1 or 3, H, W] shape, where ... means an arbitrary number of leading dimensions.
    If img is PIL Image, it is expected to be in mode "L" or "RGB".

    Args:
        policy (AutoAugmentPolicy): Desired policy enum defined by
            :class:`torchvision.transforms.autoaugment.AutoAugmentPolicy`. Default is ``AutoAugmentPolicy.IMAGENET``.
        interpolation (InterpolationMode): Desired interpolation enum defined by
            :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.NEAREST``.
            If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported.
        fill (sequence or number, optional): Pixel fill value for the area outside the transformed
            image. If given a number, the value is used for all bands respectively.
    Npolicyr   r   returnc                    s,   t    || _|| _|| _| || _d S N)super__init__r?   r   r   _get_policiespolicies)selfr?   r   r   	__class__r5   r6   rC   y   s
   
zAutoAugment.__init__c                 C   sF   |t jkr	g dS |t jkrg dS |t jkrg dS td| d)N)))r&   皙?   )r    333333?	   )r'   rK      r(   rK   Nr)   皙?Nr)   rK   N))r&   rK      )r&   rK      r)   rI   N)r'   皙?   )rW   r    rR   rJ   ))r'   rK      rS   ))r&   rR   rN   r)   r   N))r    rX   r[   )r'   rK   rJ   )rS   )r&   rI   rU   )rZ   r#   rI   r   ))r    rI   rL   rS   ))r)   r   NrQ   r*   rK   Nr\   )r#   rK   rY   )r$   r   rJ   )rZ   )r#   r      ))r#   rR   rJ   )r'   rR   rT   ))r%   rI   rT   r_   ))r   rK   rN   r\   )r]   rS   rV   rM   r^   r`   rP   ))r*   皙?N)r$   rX   rU   ))r    ffffff?ra   )r   333333?rL   ))r%   rR   r   )r%   ?r[   ))r         ?rJ   r   rd   rL   ))r(   rg   Nr)   rf   N))r   rX   rT   )r&   re   rT   ))r#   rI   r[   )r"   rK   rT   ))r%   re   rL   )r"   rd   rL   )rS   )r)   rg   N))r$   rK   rT   )r%   rK   rN   ))r#   rd   rT   )r   rg   rJ   ))r)   re   N)r(   rI   N))r   rI   r[   )r%   rX   rU   ))r"   rf   rU   )r#   rX   rJ   ))r'   rg   ra   )r*   r   N)r)   rX   NrO   )rj   rS   ))r#   rf   rL   rS   )r(   rR   N)r'   rX   rJ   ))r"   rc   r[   )r#   rd   r   ))r'   rI   rN   r(   rf   N))r   rf   rL   rh   )rl   )r'   rR   r[   )rQ   rb   )rh   rl   ))r   rf   rY   )r*   rX   N)r   rf   rJ   r*   rd   N)rS   )r'   rK   rU   r*   rf   NrS   rS   )r    rf   r[   )rm   rk   )rn   )r*   rI   N))r   rf   rN   )r'   rX   rU   )rq   rk   rr   )rm   )r'   re   r[   ))r   rR   rJ   ro   )ri   )r   rK   rU   rp   ))r$   re   r[   r    rR   rY   )r*   rR   N)r   r   ra   ))r   rd   rU   )r'   rI   rJ   )r_   rs   ))r   re   rT   )r   rf   r[   ))r   rc   rU   r_   ))r'   rd   ra   )r   rK   rT   ))r   rR   rY   rt   ))r   rd   rL   )r   rR   r[   ))r   rR   rN   )r(   rd   N))r   rd   ra   rb   zThe provided policy r,   )r   r<   r=   r>   r4   )rF   r?   r5   r5   r6   rD      s   


zAutoAugment._get_policiesnum_bins
image_sizec                 C   s   t dd|dft dd|dft dd|d  |dft dd|d  |dft dd|dft dd|dft dd|dft dd|dft dd|dfd	t ||d d
     dft dd|dft ddft ddft ddfdS )Nr   re   Tt ?r   r         >@rf   rJ   rY   F     o@)r   r   r   r   r    r"   r#   r$   r%   r&   r'   r(   r)   r*   )torchlinspacearangeroundr1   tensorrF   ru   rv   r5   r5   r6   _augmentation_space   s   $zAutoAugment._augmentation_spacetransform_numc                 C   s4   t t| d }td}tdd}|||fS )zGet parameters for autoaugment transformation

        Returns:
            params required by the autoaugment transformation
        r   )ra   ra   )r1   rz   randintitemZrand)r   Z	policy_idprobssignsr5   r5   r6   
get_params   s   

zAutoAugment.get_paramsr   c                 C   s   | j }t|\}}}t|tr*t|ttfrt|g| }n|dur*dd |D }| t| j	\}}}| 
d||f}	t| j	| D ]7\}
\}}}||
 |kr{|	| \}}|durct||  nd}|rq||
 dkrq|d9 }t|||| j|d}qD|S )	z
            img (PIL Image or Tensor): Image to be transformed.

        Returns:
            PIL Image or Tensor: AutoAugmented image.
        Nc                 S      g | ]}t |qS r5   float.0fr5   r5   r6   
<listcomp>      z'AutoAugment.forward.<locals>.<listcomp>
   r   r         r!   )r   r-   get_dimensions
isinstancer   r1   r   r   lenrE   r   	enumerater   r7   r   )rF   r   r   channelsheightwidthZtransform_idr   r   op_metair   pZmagnitude_id
magnitudessignedr   r5   r5   r6   forward   s$   
zAutoAugment.forwardc                 C   s   | j j d| j d| j dS )Nz(policy=, fill=))rH   r8   r?   r   )rF   r5   r5   r6   __repr__  s   zAutoAugment.__repr__)r8   r9   r:   r;   r   r<   r
   NEARESTr   r   r   rC   r   strr1   rD   r   r   boolr   staticmethodr   r   r   __classcell__r5   r5   rG   r6   r   h   s0    
*
.Zr   c                       s   e Zd ZdZdddejdfdededed	ed
eee	  ddf fddZ
dedeeef deeeeef f fddZdedefddZdefddZ  ZS )r   a~  RandAugment data augmentation method based on
    `"RandAugment: Practical automated data augmentation with a reduced search space"
    <https://arxiv.org/abs/1909.13719>`_.
    If the image is torch Tensor, it should be of type torch.uint8, and it is expected
    to have [..., 1 or 3, H, W] shape, where ... means an arbitrary number of leading dimensions.
    If img is PIL Image, it is expected to be in mode "L" or "RGB".

    Args:
        num_ops (int): Number of augmentation transformations to apply sequentially.
        magnitude (int): Magnitude for all the transformations.
        num_magnitude_bins (int): The number of different magnitude values.
        interpolation (InterpolationMode): Desired interpolation enum defined by
            :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.NEAREST``.
            If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported.
        fill (sequence or number, optional): Pixel fill value for the area outside the transformed
            image. If given a number, the value is used for all bands respectively.
    ra   rL      Nnum_opsr   num_magnitude_binsr   r   r@   c                    s,   t    || _|| _|| _|| _|| _d S rA   )rB   rC   r   r   r   r   r   )rF   r   r   r   r   r   rG   r5   r6   rC   2  s   

zRandAugment.__init__ru   rv   c                 C   s   t ddft dd|dft dd|dft dd|d  |dft dd|d  |dft dd|dft dd	|dft dd	|dft dd	|dft dd	|dfd
t ||d d     dft dd|dft ddft ddfdS )Nr   Fre   Trw   r   r   rx   rf   rJ   rY   ry   r+   r   r   r   r   r    r"   r#   r$   r%   r&   r'   r(   r)   rz   r~   r{   r|   r}   r1   r   r5   r5   r6   r   A  s   $zRandAugment._augmentation_spacer   c                 C   s   | j }t|\}}}t|tr*t|ttfrt|g| }n|dur*dd |D }| | j||f}t	| j
D ]B}ttt|d }t| | }	||	 \}
}|
jdkrbt|
| j  nd}|rptddrp|d9 }t||	|| j|d	}q8|S )

            img (PIL Image or Tensor): Image to be transformed.

        Returns:
            PIL Image or Tensor: Transformed image.
        Nc                 S   r   r5   r   r   r5   r5   r6   r   a  r   z'RandAugment.forward.<locals>.<listcomp>r   r   r   ra   r   r!   )r   r-   r   r   r   r1   r   r   r   ranger   rz   r   r   r   listkeysndimr   r7   r   )rF   r   r   r   r   r   r   _op_indexr   r   r   r   r5   r5   r6   r   T  s"   
 zRandAugment.forwardc                 C   s:   | j j d| j d| j d| j d| j d| j d}|S )Nz	(num_ops=z, magnitude=z, num_magnitude_bins=, interpolation=r   r   )rH   r8   r   r   r   r   r   rF   sr5   r5   r6   r   o  s   
	zRandAugment.__repr__)r8   r9   r:   r;   r
   r   r1   r   r   r   rC   r   r   r   r   r   r   r   r   r   r5   r5   rG   r6   r     s.    
.r   c                	       s   e Zd ZdZdejdfdededeee	  ddf fdd	Z
d
edeeeeef f fddZdedefddZdefddZ  ZS )r   a  Dataset-independent data-augmentation with TrivialAugment Wide, as described in
    `"TrivialAugment: Tuning-free Yet State-of-the-Art Data Augmentation" <https://arxiv.org/abs/2103.10158>`_.
    If the image is torch Tensor, it should be of type torch.uint8, and it is expected
    to have [..., 1 or 3, H, W] shape, where ... means an arbitrary number of leading dimensions.
    If img is PIL Image, it is expected to be in mode "L" or "RGB".

    Args:
        num_magnitude_bins (int): The number of different magnitude values.
        interpolation (InterpolationMode): Desired interpolation enum defined by
            :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.NEAREST``.
            If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported.
        fill (sequence or number, optional): Pixel fill value for the area outside the transformed
            image. If given a number, the value is used for all bands respectively.
    r   Nr   r   r   r@   c                    s    t    || _|| _|| _d S rA   )rB   rC   r   r   r   )rF   r   r   r   rG   r5   r6   rC     s   

zTrivialAugmentWide.__init__ru   c                 C   s   t ddft dd|dft dd|dft dd|dft dd|dft dd|dft dd|dft dd|dft dd|dft dd|dfdt ||d d	     dft d
d|dft ddft ddfdS )Nr   FgGz?Tg      @@g     `@rJ   r   rU   ry   r   r   )rF   ru   r5   r5   r6   r     s   $z&TrivialAugmentWide._augmentation_spacer   c                 C   s   | j }t|\}}}t|tr*t|ttfrt|g| }n|dur*dd |D }| | j}tt	
t|d }t| | }|| \}	}
|	jdkr`t|	t	j
t|	dt	jd  nd}|
rnt	
ddrn|d	9 }t|||| j|d
S )r   Nc                 S   r   r5   r   r   r5   r5   r6   r     r   z.TrivialAugmentWide.forward.<locals>.<listcomp>r   r   dtyper   ra   r   r!   )r   r-   r   r   r   r1   r   r   r   rz   r   r   r   r   r   r   longr7   r   )rF   r   r   r   r   r   r   r   r   r   r   r   r5   r5   r6   r     s$   

"zTrivialAugmentWide.forwardc                 C   s*   | j j d| j d| j d| j d}|S )Nz(num_magnitude_bins=r   r   r   )rH   r8   r   r   r   r   r5   r5   r6   r     s   
zTrivialAugmentWide.__repr__)r8   r9   r:   r;   r
   r   r1   r   r   r   rC   r   r   r   r   r   r   r   r   r   r5   r5   rG   r6   r   |  s"    
"r   c                       s   e Zd ZdZdddddejdfdeded	ed
ededede	e
e  ddf fddZdedeeef deeeeef f fddZejjdefddZejjdefddZdedefddZdedefddZdefd d!Z  ZS )"r   a  AugMix data augmentation method based on
    `"AugMix: A Simple Data Processing Method to Improve Robustness and Uncertainty" <https://arxiv.org/abs/1912.02781>`_.
    If the image is torch Tensor, it should be of type torch.uint8, and it is expected
    to have [..., 1 or 3, H, W] shape, where ... means an arbitrary number of leading dimensions.
    If img is PIL Image, it is expected to be in mode "L" or "RGB".

    Args:
        severity (int): The severity of base augmentation operators. Default is ``3``.
        mixture_width (int): The number of augmentation chains. Default is ``3``.
        chain_depth (int): The depth of augmentation chains. A negative value denotes stochastic depth sampled from the interval [1, 3].
            Default is ``-1``.
        alpha (float): The hyperparameter for the probability distributions. Default is ``1.0``.
        all_ops (bool): Use all operations (including brightness, contrast, color and sharpness). Default is ``True``.
        interpolation (InterpolationMode): Desired interpolation enum defined by
            :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.NEAREST``.
            If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported.
        fill (sequence or number, optional): Pixel fill value for the area outside the transformed
            image. If given a number, the value is used for all bands respectively.
    r[   r   TNseveritymixture_widthchain_depthalphaall_opsr   r   r@   c                    sn   t    d| _d|  kr| jks n td| j d| d|| _|| _|| _|| _|| _|| _	|| _
d S )Nr   r   z!The severity must be between [1, z]. Got z	 instead.)rB   rC   _PARAMETER_MAXr4   r   r   r   r   r   r   r   )rF   r   r   r   r   r   r   r   rG   r5   r6   rC     s   


zAugMix.__init__ru   rv   c                 C   s  t dd|dft dd|dft d|d d |dft d|d d |dft dd|dfdt ||d d     d	ft d
d|d	ft dd	ft dd	fd	}| jr|t dd|dft dd|dft dd|dft dd|dfd |S )Nr   re   Tr   g      @r   rx   rY   Fry   )	r   r   r   r   r    r&   r'   r(   r)   rf   )r"   r#   r$   r%   )rz   r{   r|   r}   r1   r~   r   update)rF   ru   rv   r   r5   r5   r6   r     s&   $zAugMix._augmentation_spacec                 C   
   t |S rA   )r-   Zpil_to_tensorrF   r   r5   r5   r6   _pil_to_tensor     
zAugMix._pil_to_tensorr   c                 C   r   rA   )r-   Zto_pil_imager   r5   r5   r6   _tensor_to_pil  r   zAugMix._tensor_to_pilparamsc                 C   r   rA   )rz   _sample_dirichlet)rF   r   r5   r5   r6   r     r   zAugMix._sample_dirichletorig_imgc              	   C   st  | j }t|\}}}t|tr-|}t|ttfr!t|g| }n|dur,dd |D }n| |}| | j	||f}t
|j}|dgtd|j d | }	|	dgdg|	jd   }
| tj| j| jg|	jd|
d d}| tj| jg| j |	jd|
d d|dddf |
d dg }|dddf |
|	 }t| jD ]x}|	}| jdkr| jnttjddd	d
 }t|D ]K}ttt|d	 }t
| | }|| \}}|jdkrt|tj| jd	tjd  nd}|rtdd	r|d9 }t|||| j |d}q|!|dd|f |
|  q||j"|j#d}t|ts8| $|S |S )r   Nc                 S   r   r5   r   r   r5   r5   r6   r   /  r   z"AugMix.forward.<locals>.<listcomp>r   rY   r   )devicer   r   )lowhighsizer   r   ra   r   r!   )%r   r-   r   r   r   r1   r   r   r   r   r   shapeviewmaxr   r   r   rz   r~   r   r   expandr   r   r   r   r   r   r   r   r   r7   r   Zadd_tor   r   )rF   r   r   r   r   r   r   r   Z	orig_dimsbatchZ
batch_dimsmZcombined_weightsZmixr   augdepthr   r   r   r   r   r   r5   r5   r6   r   !  sT   


 "$(
 "
zAugMix.forwardc                 C   sJ   | j j d| j d| j d| j d| j d| j d| j d| j d}|S )	Nz
(severity=z, mixture_width=z, chain_depth=z, alpha=z
, all_ops=r   r   r   )	rH   r8   r   r   r   r   r   r   r   r   r5   r5   r6   r   [  s"   
zAugMix.__repr__)r8   r9   r:   r;   r
   ZBILINEARr1   r   r   r   r   rC   r   r   r   r   r   rz   ZjitZunusedr   r   r   r   r   r   r5   r5   rG   r6   r     sD    
	.:r   )r.   enumr   typingr   r   r   r   rz   r    r	   r-   r
   __all__r   r   r7   r   nnModuler   r   r   r   r5   r5   r5   r6   <module>   s0    

P 8]V