o
    ,h@n                     @   s   d dl Z d dlmZ d dl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)Optional)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.)Faffinemathdegreesatanintrotateadjust_brightnessadjust_saturationadjust_contrastadjust_sharpness	posterizesolarizeautocontrastequalizeinvert
ValueError)r   r   r   r   r    r;   ]/var/www/html/scripts/venv/lib/python3.10/site-packages/torchvision/transforms/autoaugment.py	_apply_op   s   C6
+
!
	

r=   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.
    imagenetcifar10svhnN)__name__
__module____qualname____doc__IMAGENETCIFAR10SVHNr;   r;   r;   r<   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__rH   r   r   _get_policiespolicies)selfrH   r   r   	__class__r;   r<   rL   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$   rT      r%   rT   Nr&   皙?Nr&   rT   N))r#   rT      )r#   rT      r&   rR   N)r$   皙?   )r`   r   r[   rS   ))r$   rT      r\   ))r#   r[   rW   r&   r   N))r   ra   rd   )r$   rT   rS   )r\   )r#   rR   r^   )rc   r    rR   r   ))r   rR   rU   r\   ))r&   r   NrZ   r'   rT   Nre   )r    rT   rb   )r!   r   rS   )rc   )r    r      ))r    r[   rS   )r$   r[   r]   ))r"   rR   r]   rh   ))r   rT   rW   re   )rf   r\   r_   rV   rg   ri   rY   ))r'   皙?N)r!   ra   r^   ))r   ffffff?rj   )r   333333?rU   ))r"   r[   r   )r"   ?rd   ))r         ?rS   r   rm   rU   ))r%   rp   Nr&   ro   N))r   ra   r]   )r#   rn   r]   ))r    rR   rd   )r   rT   r]   ))r"   rn   rU   )r   rm   rU   )r\   )r&   rp   N))r!   rT   r]   )r"   rT   rW   ))r    rm   r]   )r   rp   rS   ))r&   rn   N)r%   rR   N))r   rR   rd   )r"   ra   r^   ))r   ro   r^   )r    ra   rS   ))r$   rp   rj   )r'   r   N)r&   ra   NrX   )rs   r\   ))r    ro   rU   r\   )r%   r[   N)r$   ra   rS   ))r   rl   rd   )r    rm   r   ))r$   rR   rW   r%   ro   N))r   ro   rU   rq   )ru   )r$   r[   rd   )rZ   rk   )rq   ru   ))r   ro   rb   )r'   ra   N)r   ro   rS   r'   rm   N)r\   )r$   rT   r^   r'   ro   Nr\   r\   )r   ro   rd   )rv   rt   )rw   )r'   rR   N))r   ro   rW   )r$   ra   r^   )rz   rt   r{   )rv   )r$   rn   rd   ))r   r[   rS   rx   )rr   )r   rT   r^   ry   ))r!   rn   rd   r   r[   rb   )r'   r[   N)r   r   rj   ))r   rm   r^   )r$   rR   rS   )rh   r|   ))r   rn   r]   )r   ro   rd   ))r   rl   r^   rh   ))r$   rm   rj   )r   rT   r]   ))r   r[   rb   r}   ))r   rm   rU   )r   r[   rd   ))r   r[   rW   )r%   rm   N))r   rm   rj   rk   zThe provided policy r)   )r   rE   rF   rG   r:   )rO   rH   r;   r;   r<   rM      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   rn   Tt ?r   r         >@ro   rS   rb   F     o@)r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   )torchlinspacearangeroundr/   tensorrO   r~   r   r;   r;   r<   _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   )rj   rj   )r/   r   randintitemrand)r   	policy_idprobssignsr;   r;   r<   
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 r;   float.0fr;   r;   r<   
<listcomp>      z'AutoAugment.forward.<locals>.<listcomp>
   r   r         r   )r   r*   get_dimensions
isinstancer   r/   r   r   lenrN   r   	enumerater   r=   r   )rO   r   r   channelsheightwidthtransform_idr   r   op_metair   pmagnitude_id
magnitudessignedr   r;   r;   r<   forward   s$   
zAutoAugment.forwardc                 C   s   | j j d| j d| j dS )Nz(policy=, fill=))rQ   rA   rH   r   )rO   r;   r;   r<   __repr__  s   zAutoAugment.__repr__)rA   rB   rC   rD   r   rE   r   NEARESTr   listr   rL   tuplestrr/   rM   dictr   boolr   staticmethodr   r   r   __classcell__r;   r;   rP   r<   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.
    rj   rU      Nnum_opsr   num_magnitude_binsr   r   rI   c                    s,   t    || _|| _|| _|| _|| _d S rJ   )rK   rL   r   r   r   r   r   )rO   r   r   r   r   r   rP   r;   r<   rL   2  s   

zRandAugment.__init__r~   r   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   Frn   Tr   r   r   r   ro   rS   rb   r   r(   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r   r   r   r   r   r/   r   r;   r;   r<   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   r;   r   r   r;   r;   r<   r   a  r   z'RandAugment.forward.<locals>.<listcomp>r   r   r   rj   r   r   )r   r*   r   r   r   r/   r   r   r   ranger   r   r   r   r   r   keysndimr   r=   r   )rO   r   r   r   r   r   r   _op_indexr   r   r   r   r;   r;   r<   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   )rQ   rA   r   r   r   r   r   rO   sr;   r;   r<   r   o  s   
	zRandAugment.__repr__)rA   rB   rC   rD   r   r   r/   r   r   r   rL   r   r   r   r   r   r   r   r   r   r;   r;   rP   r<   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   rI   c                    s    t    || _|| _|| _d S rJ   )rK   rL   r   r   r   )rO   r   r   r   rP   r;   r<   rL     s   

zTrivialAugmentWide.__init__r~   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     `@rS   r   r^   r   r   r   )rO   r~   r;   r;   r<   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   r;   r   r   r;   r;   r<   r     r   z.TrivialAugmentWide.forward.<locals>.<listcomp>r   r   dtyper   rj   r   r   )r   r*   r   r   r   r/   r   r   r   r   r   r   r   r   r   r   longr=   r   )rO   r   r   r   r   r   r   r   r   r   r   r   r;   r;   r<   r     s$   

"zTrivialAugmentWide.forwardc                 C   s*   | j j d| j d| j d| j d}|S )Nz(num_magnitude_bins=r   r   r   )rQ   rA   r   r   r   r   r;   r;   r<   r     s   
zTrivialAugmentWide.__repr__)rA   rB   rC   rD   r   r   r/   r   r   r   rL   r   r   r   r   r   r   r   r   r   r;   r;   rP   r<   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.
    rd   r   TNseveritymixture_widthchain_depthalphaall_opsr   r   rI   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.)rK   rL   _PARAMETER_MAXr:   r   r   r   r   r   r   r   )rO   r   r   r   r   r   r   r   rP   r;   r<   rL     s   


zAugMix.__init__r~   r   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   rn   Tr   g      @r   r   rb   Fr   )	r   r   r   r   r   r#   r$   r%   r&   ro   )r   r    r!   r"   )r   r   r   r   r/   r   r   update)rO   r~   r   r   r;   r;   r<   r     s&   $zAugMix._augmentation_spacec                 C   
   t |S rJ   )r*   pil_to_tensorrO   r   r;   r;   r<   _pil_to_tensor     
zAugMix._pil_to_tensorr   c                 C   r   rJ   )r*   to_pil_imager   r;   r;   r<   _tensor_to_pil  r   zAugMix._tensor_to_pilparamsc                 C   r   rJ   )r   _sample_dirichlet)rO   r   r;   r;   r<   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   r;   r   r   r;   r;   r<   r   /  r   z"AugMix.forward.<locals>.<listcomp>r   rb   r   )devicer   r   )lowhighsizer   r   rj   r   r   )%r   r*   r   r   r   r/   r   r   r   r   r   shapeviewmaxr   r   r   r   r   r   r   expandr   r   r   r   r   r   r   r   r   r=   r   add_tor   r   )rO   r   r   r   r   r   r   r   	orig_dimsbatch
batch_dimsmcombined_weightsmixr   augdepthr   r   r   r   r   r   r;   r;   r<   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   )	rQ   rA   r   r   r   r   r   r   r   r   r;   r;   r<   r   [  s"   
zAugMix.__repr__)rA   rB   rC   rD   r   BILINEARr/   r   r   r   r   rL   r   r   r   r   r   r   jitunusedr   r   r   r   r   r   r;   r;   rP   r<   r     sD    
	.:r   )r,   enumr   typingr   r   r    r   r*   r   __all__r   r   r   r=   r   nnModuler	   r
   r   r   r;   r;   r;   r<   <module>   s0    

P 8]V