a
    PSicSn                     @   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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.)Faffinemathdegreesatanintrotateadjust_brightnessadjust_saturationadjust_contrastadjust_sharpness	posterizesolarizeautocontrastequalizeinvert
ValueErrorr    r?   ^/var/www/html/django/DPS/env/lib/python3.9/site-packages/torchvision/transforms/autoaugment.py	_apply_op   s    

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



	

	









rA   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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__rL   r   r   _get_policiespolicies)selfrL   r   r   	__class__r?   r@   rP   y   s
    
zAutoAugment.__init__)rL   rM   c                 C   sJ   |t jkrg dS |t jkr$g dS |t jkr6g dS td| dd S )N)))r'   皙?   )r!   333333?	   )r(   rX      r)   rX   Nr*   皙?Nr*   rX   N))r'   rX      )r'   rX      r*   rV   N)r(   皙?   )rd   r!   r_   rW   ))r(   rX      r`   ))r'   r_   r[   r*   r   N))r!   re   rh   )r(   rX   rW   )r`   )r'   rV   rb   )rg   r$   rV   r   ))r!   rV   rY   r`   ))r*   r   Nr^   r+   rX   Nri   )r$   rX   rf   )r%   r   rW   )rg   )r$   r      ))r$   r_   rW   )r(   r_   ra   ))r&   rV   ra   rl   ))r   rX   r[   ri   )rj   r`   rc   rZ   rk   rm   r]   ))r+   皙?N)r%   re   rb   ))r!   ffffff?rn   )r   333333?rY   ))r&   r_   r   )r&   ?rh   ))r         ?rW   r    rq   rY   ))r)   rt   Nr*   rs   N))r   re   ra   )r'   rr   ra   ))r$   rV   rh   )r#   rX   ra   ))r&   rr   rY   )r#   rq   rY   )r`   )r*   rt   N))r%   rX   ra   )r&   rX   r[   ))r$   rq   ra   )r   rt   rW   ))r*   rr   N)r)   rV   N))r    rV   rh   )r&   re   rb   ))r#   rs   rb   )r$   re   rW   ))r(   rt   rn   )r+   r   N)r*   re   Nr\   )rw   r`   ))r$   rs   rY   r`   )r)   r_   N)r(   re   rW   ))r#   rp   rh   )r$   rq   r   ))r(   rV   r[   r)   rs   N))r    rs   rY   ru   )ry   )r(   r_   rh   )r^   ro   )ru   ry   ))r   rs   rf   )r+   re   N)r   rs   rW   r+   rq   N)r`   )r(   rX   rb   r+   rs   Nr`   r`   )r!   rs   rh   )rz   rx   )r{   )r+   rV   N))r   rs   r[   )r(   re   rb   )r~   rx   r   )rz   )r(   rr   rh   ))r   r_   rW   r|   )rv   )r    rX   rb   r}   ))r%   rr   rh   r!   r_   rf   )r+   r_   N)r    r   rn   ))r   rq   rb   )r(   rV   rW   )rl   r   ))r   rr   ra   )r   rs   rh   ))r   rp   rb   rl   ))r(   rq   rn   )r    rX   ra   ))r   r_   rf   r   ))r   rq   rY   )r    r_   rh   ))r   r_   r[   )r)   rq   N))r   rq   rn   ro   zThe provided policy r-   )r   rI   rJ   rK   r>   )rS   rL   r?   r?   r@   rQ      s    


zAutoAugment._get_policiesnum_bins
image_sizerM   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   rr   Tt ?r   r         >@rs   rW   rf   F     o@)r   r   r   r    r!   r#   r$   r%   r&   r'   r(   r)   r*   r+   )torchlinspacearangeroundr3   tensorrS   r   r   r?   r?   r@   _augmentation_space   s    $zAutoAugment._augmentation_space)transform_numrM   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   )rn   rn   )r3   r   randintitemrand)r   Z	policy_idprobssignsr?   r?   r@   
get_params   s    
zAutoAugment.get_paramsr   rM   c                 C   s   | j }t|\}}}t|trTt|ttfr>t|g| }n|durTdd |D }| t| j	\}}}| 
d||f}	t| j	| D ]n\}
\}}}||
 |kr|	| \}}|du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 r?   float.0fr?   r?   r@   
<listcomp>      z'AutoAugment.forward.<locals>.<listcomp>
   r   r         r"   )r   r.   get_dimensions
isinstancer   r3   r   r   lenrR   r   	enumerater   rA   r   )rS   r   r   channelsheightwidthZtransform_idr   r   op_metair   pZmagnitude_id
magnitudessignedr   r?   r?   r@   forward   s"    
zAutoAugment.forwardrM   c                 C   s   | j j d| j d| j dS )Nz(policy=, fill=))rU   rE   rL   r   )rS   r?   r?   r@   __repr__  s    zAutoAugment.__repr__)rE   rF   rG   rH   r   rI   r
   NEARESTr   r   r   rP   r   strr3   rQ   r   r   boolr   staticmethodr   r   r   __classcell__r?   r?   rT   r@   r   h   s$   
*Z*r   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.
    rn   rY      N)num_opsr   num_magnitude_binsr   r   rM   c                    s,   t    || _|| _|| _|| _|| _d S rN   )rO   rP   r   r   r   r   r   )rS   r   r   r   r   r   rT   r?   r@   rP   2  s    
zRandAugment.__init__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   Frr   Tr   r   r   r   rs   rW   rf   r   r,   r   r   r   r    r!   r#   r$   r%   r&   r'   r(   r)   r*   r   r   r   r   r   r3   r   r?   r?   r@   r   A  s    $zRandAugment._augmentation_spacer   c                 C   s   | j }t|\}}}t|trTt|ttfr>t|g| }n|du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 r?   r   r   r?   r?   r@   r   a  r   z'RandAugment.forward.<locals>.<listcomp>r   r   r   rn   r   r"   )r   r.   r   r   r   r3   r   r   r   ranger   r   r   r   r   listkeysndimr   rA   r   )rS   r   r   r   r   r   r   _op_indexr   r   r   r   r?   r?   r@   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   )rU   rE   r   r   r   r   r   rS   sr?   r?   r@   r   o  s    
	zRandAugment.__repr__)rE   rF   rG   rH   r
   r   r3   r   r   r   rP   r   r   r   r   r   r   r   r   r   r?   r?   rT   r@   r     s"   
*r   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   rM   c                    s    t    || _|| _|| _d S rN   )rO   rP   r   r   r   )rS   r   r   r   rT   r?   r@   rP     s    
zTrivialAugmentWide.__init__)r   rM   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     `@rW   r   rb   r   r   r   )rS   r   r?   r?   r@   r     s    $z&TrivialAugmentWide._augmentation_spacer   c                 C   s   | j }t|\}}}t|trTt|ttfr>t|g| }n|du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 r?   r   r   r?   r?   r@   r     r   z.TrivialAugmentWide.forward.<locals>.<listcomp>r   r   dtyper   rn   r   r"   )r   r.   r   r   r   r3   r   r   r   r   r   r   r   r   r   r   longrA   r   )rS   r   r   r   r   r   r   r   r   r   r   r   r?   r?   r@   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   )rU   rE   r   r   r   r   r?   r?   r@   r     s    
zTrivialAugmentWide.__repr__)rE   rF   rG   rH   r
   r   r3   r   r   r   rP   r   r   r   r   r   r   r   r   r   r?   r?   rT   r@   r   |  s   
 r   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.
    rh   r   TN)severitymixture_widthchain_depthalphaall_opsr   r   rM   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.)rO   rP   _PARAMETER_MAXr>   r   r   r   r   r   r   r   )rS   r   r   r   r   r   r   r   rT   r?   r@   rP     s    

zAugMix.__init__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   rr   Tr   g      @r   r   rf   Fr   )	r   r   r   r    r!   r'   r(   r)   r*   rs   )r#   r$   r%   r&   )r   r   r   r   r3   r   r   update)rS   r   r   r   r?   r?   r@   r     s&    $zAugMix._augmentation_spacer   c                 C   s
   t |S rN   )r.   pil_to_tensorrS   r   r?   r?   r@   _pil_to_tensor  s    zAugMix._pil_to_tensor)r   c                 C   s
   t |S rN   )r.   to_pil_imager   r?   r?   r@   _tensor_to_pil  s    zAugMix._tensor_to_pil)paramsrM   c                 C   s
   t |S rN   )r   _sample_dirichlet)rS   r   r?   r?   r@   r     s    zAugMix._sample_dirichlet)orig_imgrM   c              	   C   s|  | j }t|\}}}t|trZ|}t|ttfrBt|g| }qd|du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 r?   r   r   r?   r?   r@   r   /  r   z"AugMix.forward.<locals>.<listcomp>r   rf   r   )devicer   r   )lowhighsizer   r   rn   r   r"   )%r   r.   r   r   r   r3   r   r   r   r   r   shapeviewmaxr   r   r   r   r   r   r   expandr   r   r   r   r   r   r   r   r   rA   r   add_tor   r   )rS   r   r   r   r   r   r   r   Z	orig_dimsbatch
batch_dimsmZcombined_weightsmixr   augdepthr   r   r   r   r   r   r?   r?   r@   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   )	rU   rE   r   r   r   r   r   r   r   r   r?   r?   r@   r   [  s"    
zAugMix.__repr__)rE   rF   rG   rH   r
   BILINEARr3   r   r   r   r   rP   r   r   r   r   r   r   jitunusedr   r   r   r   r   r   r?   r?   rT   r@   r     s4   
*:r   )r0   enumr   typingr   r   r   r   r   r    r	   r.   r
   __all__r   r   rA   r   nnModuler   r   r   r   r?   r?   r?   r@   <module>   s   P 8]V