U
    (dSn                     @   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 ddd	d
dgZe	eeeeee  d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)ListTupleOptionalDict)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} n|dkr|t j| dddgddtt|g||ddgd} n|dkrt j| dt|dgd|ddg|d} nP|d	krt j| ddt|gd|ddg|d} n |d
krt j| |||d} n|dkrt | d| } n|dkr2t | d| } n|dkrNt 	| d| } n|dkrjt 
| d| } n|dkrt | t|} nv|dkrt | |} n^|dkrt | } nH|dkrt | } n2|dkrt | } n|dkrntd| d| S )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
ValueErrorr    r6   F/tmp/pip-unpacked-wheel-vx7f76es/torchvision/transforms/autoaugment.py	_apply_op   s    





	

	









r8   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SVHNr6   r6   r6   r7   r   ]   s   c                	       s   e Zd ZdZejejdfeeee	e
  dd fddZee	eeee
ee f eee
ee f f  dddZeeeef eeeeef f d	d
dZeeeeeef dddZeedddZed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__r6   r7   rD   y   s
    
zAutoAugment.__init__)r@   rA   c                 C   s   |t jkr@ddddddddd	d
dddddddddddddddgS |t jkrdddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-gS |t jkrd.d/d0d1d2d3d4d5d6d2d7d8d9d1d:d;d<d=d>d?d@dAdBdCdDgS tdE| dFd S )GN))r'   皙?   )r!   333333?	   ))r(   rL      r)   rL   N)r*   皙?Nr*   rL   N))r'   rL      )r'   rL      )r*   rJ   N)r(   皙?   )rU   r!   rQ   rK   ))r(   rL      rR   ))r'   rQ   rN   r*   r   N))r!   rV   rY   )r(   rL   rK   )rR   )r'   rJ   rT   )rX   r$   rJ   r   ))r!   rJ   rM   rR   ))r*   r   NrP   )r+   rL   NrZ   ))r$   rL   rW   )r%   r   rK   )rX   )r$   r      ))r$   rQ   rK   )r(   rQ   rS   ))r&   rJ   rS   r\   ))r   rL   rN   rZ   )r[   rR   )r+   皙?N)r%   rV   rT   ))r!   ffffff?r]   )r   333333?rM   ))r&   rQ   r   )r&   ?rY   ))r         ?rK   r    r`   rM   ))r)   rc   Nr*   rb   N))r   rV   rS   )r'   ra   rS   ))r$   rJ   rY   )r#   rL   rS   ))r&   ra   rM   )r#   r`   rM   )rR   )r*   rc   N))r%   rL   rS   )r&   rL   rN   ))r$   r`   rS   )r   rc   rK   ))r*   ra   N)r)   rJ   N))r    rJ   rY   )r&   rV   rT   ))r#   rb   rT   )r$   rV   rK   ))r(   rc   r]   )r+   r   N)r*   rV   NrO   )rf   rR   ))r$   rb   rM   rR   )r)   rQ   N)r(   rV   rK   ))r#   r_   rY   )r$   r`   r   ))r(   rJ   rN   r)   rb   N))r    rb   rM   rd   )rh   )r(   rQ   rY   )rP   r^   )rd   rh   )r   rb   rW   )r+   rV   N)r   rb   rK   r+   r`   N)rR   )r(   rL   rT   )r+   rb   NrR   )rR   )r!   rb   rY   )ri   rg   )rj   )r+   rJ   N))r   rb   rN   )r(   rV   rT   )rl   rg   )ri   )r(   ra   rY   ))r   rQ   rK   rk   )re   )r    rL   rT   ))r%   ra   rY   r!   rQ   rW   )r+   rQ   N)r    r   r]   ))r   r`   rT   )r(   rJ   rK   )r\   rm   ))r   ra   rS   )r   rb   rY   ))r   r_   rT   r\   ))r(   r`   r]   )r    rL   rS   ))r   rQ   rW   rn   ))r   r`   rM   )r    rQ   rY   ))r   rQ   rN   )r)   r`   N))r   r`   r]   r^   zThe provided policy r-   )r   r=   r>   r?   r5   )rG   r@   r6   r6   r7   rE      s    


zAutoAugment._get_policiesnum_bins
image_sizerA   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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   ra   Tt ?r   r         >@rb   rK   rW   F     o@)r   r   r   r    r!   r#   r$   r%   r&   r'   r(   r)   r*   r+   )torchlinspacearangeroundr2   tensorrG   rp   rq   r6   r6   r7   _augmentation_space   s    $zAutoAugment._augmentation_space)transform_numrA   c                 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   )r]   r]   )r2   ru   randintitemZrand)r|   Z	policy_idprobssignsr6   r6   r7   
get_params   s    
zAutoAugment.get_paramsr   rA   c                 C   s   | j }t|\}}}t|trTt|ttfr>t|g| }n|dk	rTdd |D }| t| j	\}}}| 
d||f}	t| j	| D ]n\}
\}}}||
 |kr|	| \}}|dk	rt||  nd}|r||
 dkr|d9 }t|||| j|d}q|S )	z
            img (PIL Image or Tensor): Image to be transformed.

        Returns:
            PIL Image or Tensor: AutoAugmented image.
        Nc                 S   s   g | ]}t |qS r6   float.0fr6   r6   r7   
<listcomp>  s     z'AutoAugment.forward.<locals>.<listcomp>
   r   r         r"   )r   r.   get_dimensions
isinstancer   r2   r   r   lenrF   r{   	enumerater   r8   r   )rG   r   r   channelsheightwidthZtransform_idr   r   op_metair   pZmagnitude_id
magnitudessignedr   r6   r6   r7   forward   s"    
zAutoAugment.forwardrA   c                 C   s   | j j d| j d| j dS )Nz(policy=, fill=))rI   r9   r@   r   )rG   r6   r6   r7   __repr__  s    zAutoAugment.__repr__)r9   r:   r;   r<   r   r=   r
   NEARESTr   r   r   rD   r   strr2   rE   r   r   boolr{   staticmethodr   r   r   __classcell__r6   r6   rH   r7   r   h   s$   
*Z*c                       s   e Zd ZdZdddejdfeeeeeee	  dd fddZ
eeeef eeeeef f d	d
dZeedddZed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.
    r]   rM      N)num_opsr   num_magnitude_binsr   r   rA   c                    s,   t    || _|| _|| _|| _|| _d S rB   )rC   rD   r   r   r   r   r   )rG   r   r   r   r   r   rH   r6   r7   rD   2  s    
zRandAugment.__init__ro   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   Fra   Trr   r   r   rs   rb   rK   rW   rt   r,   r   r   r   r    r!   r#   r$   r%   r&   r'   r(   r)   r*   ru   ry   rv   rw   rx   r2   rz   r6   r6   r7   r{   A  s    $zRandAugment._augmentation_spacer   c                 C   s   | j }t|\}}}t|trTt|ttfr>t|g| }n|dk	rTdd |D }| | j||f}t	| j
D ]}ttt|d }t| | }	||	 \}
}|
jdkrt|
| j  nd}|rtddr|d9 }t||	|| j|d	}qp|S )

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

        Returns:
            PIL Image or Tensor: Transformed image.
        Nc                 S   s   g | ]}t |qS r6   r   r   r6   r6   r7   r   a  s     z'RandAugment.forward.<locals>.<listcomp>r}   r   r   r]   r   r"   )r   r.   r   r   r   r2   r   r{   r   ranger   ru   r~   r   r   listkeysndimr   r8   r   )rG   r   r   r   r   r   r   _op_indexr   r   r   r   r6   r6   r7   r   T  s"    
 zRandAugment.forwardr   c                 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   )rI   r9   r   r   r   r   r   rG   sr6   r6   r7   r   o  s    4	zRandAugment.__repr__)r9   r:   r;   r<   r
   r   r2   r   r   r   rD   r   r   r   r   r   r{   r   r   r   r6   r6   rH   r7   r     s"   
*c                       s|   e Zd ZdZdejdfeeeee	  dd fddZ
eeeeeef f ddd	Zeed
ddZed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   N)r   r   r   rA   c                    s    t    || _|| _|| _d S rB   )rC   rD   r   r   r   )rG   r   r   r   rH   r6   r7   rD     s    
zTrivialAugmentWide.__init__)rp   rA   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     `@rK   r   rT   rt   r   r   )rG   rp   r6   r6   r7   r{     s    $z&TrivialAugmentWide._augmentation_spacer   c                 C   s   | j }t|\}}}t|trTt|ttfr>t|g| }n|dk	rTdd |D }| | j}tt	
t|d }t| | }|| \}	}
|	jdkrt|	t	j
t|	dt	jd  nd}|
rt	
ddr|d	9 }t|||| j|d
S )r   Nc                 S   s   g | ]}t |qS r6   r   r   r6   r6   r7   r     s     z.TrivialAugmentWide.forward.<locals>.<listcomp>r}   r   dtyper   r]   r   r"   )r   r.   r   r   r   r2   r   r{   r   ru   r~   r   r   r   r   r   longr8   r   )rG   r   r   r   r   r   r   r   r   r   r   r   r6   r6   r7   r     s$    
$zTrivialAugmentWide.forwardr   c                 C   s*   | j j d| j d| j d| j d}|S )Nz(num_magnitude_bins=r   r   r   )rI   r9   r   r   r   r   r6   r6   r7   r     s    $zTrivialAugmentWide.__repr__)r9   r:   r;   r<   r
   r   r2   r   r   r   rD   r   r   r   r   r   r{   r   r   r   r6   r6   rH   r7   r   |  s   
 c                
       s   e Zd ZdZdddddejdfeeeeeee	e
e  dd fdd	Zeeeef eeeeef f d
ddZejjedddZejjedddZeedddZeedddZed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.
    rY   r   TN)severitymixture_widthchain_depthalphaall_opsr   r   rA   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.)rC   rD   _PARAMETER_MAXr5   r   r   r   r   r   r   r   )rG   r   r   r   r   r   r   r   rH   r6   r7   rD     s    

zAugMix.__init__ro   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   ra   Tr   g      @r   rs   rW   Frt   )	r   r   r   r    r!   r'   r(   r)   r*   rb   )r#   r$   r%   r&   )ru   rv   rw   rx   r2   ry   r   update)rG   rp   rq   r   r6   r6   r7   r{     s&    $zAugMix._augmentation_spacer   c                 C   s
   t |S rB   )r.   Zpil_to_tensorrG   r   r6   r6   r7   _pil_to_tensor  s    zAugMix._pil_to_tensor)r   c                 C   s
   t |S rB   )r.   Zto_pil_imager   r6   r6   r7   _tensor_to_pil  s    zAugMix._tensor_to_pil)paramsrA   c                 C   s
   t |S rB   )ru   _sample_dirichlet)rG   r   r6   r6   r7   r     s    zAugMix._sample_dirichlet)orig_imgrA   c              	   C   s|  | j }t|\}}}t|trZ|}t|ttfrBt|g| }qd|dk	rdd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 ]}|	}| jdkrn| jnttjddd	d
 }t|D ]}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 |
|  qT||j"|j#d}t|tsx| $|S |S )r   Nc                 S   s   g | ]}t |qS r6   r   r   r6   r6   r7   r   /  s     z"AugMix.forward.<locals>.<listcomp>r   rW   r   )devicer   r}   )lowhighsizer   r   r]   r   r"   )%r   r.   r   r   r   r2   r   r   r{   r   r   shapeviewmaxr   r   r   ru   ry   r   r   expandr   r   r   r~   r   r   r   r   r   r8   r   Zadd_tor   r   )rG   r   r   r   r   r   r   r   Z	orig_dimsbatchZ
batch_dimsmZcombined_weightsZmixr   augdepthr   r   r   r   r   r   r6   r6   r7   r   !  sR    


 "$*$$
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   )	rI   r9   r   r   r   r   r   r   r   r   r6   r6   r7   r   [  s    DzAugMix.__repr__)r9   r:   r;   r<   r
   ZBILINEARr2   r   r   r   r   rD   r   r   r   r   r{   ru   ZjitZunusedr   r   r   r   r   r   r6   r6   rH   r7   r     s4   
*:)r/   enumr   typingr   r   r   r   ru   r    r	   r.   r
   __all__r   r   r8   r   nnModuler   r   r   r   r6   r6   r6   r7   <module>   s$       
P 8]V