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.)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    r2   h/home/www/facesmatcher.com/frenv_anti/lib/python3.10/site-packages/torchvision/transforms/autoaugment.py	_apply_op   s   C6
+
!
	

r4   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SVHNr2   r2   r2   r3   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__r2   r3   r@   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$   rH      r%   rH   Nr&   皙?Nr&   rH   N))r#   rH      )r#   rH      r&   rF   N)r$   皙?   )rT   r   rO   rG   ))r$   rH      rP   ))r#   rO   rK   r&   r   N))r   rU   rX   )r$   rH   rG   )rP   )r#   rF   rR   )rW   r    rF   r   ))r   rF   rI   rP   ))r&   r   NrN   r'   rH   NrY   )r    rH   rV   )r!   r   rG   )rW   )r    r      ))r    rO   rG   )r$   rO   rQ   ))r"   rF   rQ   r\   ))r   rH   rK   rY   )rZ   rP   rS   rJ   r[   r]   rM   ))r'   皙?N)r!   rU   rR   ))r   ffffff?r^   )r   333333?rI   ))r"   rO   r   )r"   ?rX   ))r         ?rG   r   ra   rI   ))r%   rd   Nr&   rc   N))r   rU   rQ   )r#   rb   rQ   ))r    rF   rX   )r   rH   rQ   ))r"   rb   rI   )r   ra   rI   )rP   )r&   rd   N))r!   rH   rQ   )r"   rH   rK   ))r    ra   rQ   )r   rd   rG   ))r&   rb   N)r%   rF   N))r   rF   rX   )r"   rU   rR   ))r   rc   rR   )r    rU   rG   ))r$   rd   r^   )r'   r   N)r&   rU   NrL   )rg   rP   ))r    rc   rI   rP   )r%   rO   N)r$   rU   rG   ))r   r`   rX   )r    ra   r   ))r$   rF   rK   r%   rc   N))r   rc   rI   re   )ri   )r$   rO   rX   )rN   r_   )re   ri   ))r   rc   rV   )r'   rU   N)r   rc   rG   r'   ra   N)rP   )r$   rH   rR   r'   rc   NrP   rP   )r   rc   rX   )rj   rh   )rk   )r'   rF   N))r   rc   rK   )r$   rU   rR   )rn   rh   ro   )rj   )r$   rb   rX   ))r   rO   rG   rl   )rf   )r   rH   rR   rm   ))r!   rb   rX   r   rO   rV   )r'   rO   N)r   r   r^   ))r   ra   rR   )r$   rF   rG   )r\   rp   ))r   rb   rQ   )r   rc   rX   ))r   r`   rR   r\   ))r$   ra   r^   )r   rH   rQ   ))r   rO   rV   rq   ))r   ra   rI   )r   rO   rX   ))r   rO   rK   )r%   ra   N))r   ra   r^   r_   zThe provided policy r)   )r   r9   r:   r;   r1   )rC   r<   r2   r2   r3   rA      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   rb   Tt ?r   r         >@rc   rG   rV   F     o@)r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   )torchlinspacearangeroundr.   tensorrC   rr   rs   r2   r2   r3   _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   )r^   r^   )r.   rw   randintitemZrand)r~   Z	policy_idprobssignsr2   r2   r3   
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 r2   float.0fr2   r2   r3   
<listcomp>      z'AutoAugment.forward.<locals>.<listcomp>
   r   r         r   )r   r*   get_dimensions
isinstancer   r.   r   r   lenrB   r}   	enumerater   r4   r   )rC   r   r   channelsheightwidthZtransform_idr   r   op_metair   pZmagnitude_id
magnitudessignedr   r2   r2   r3   forward   s$   
zAutoAugment.forwardc                 C   s   | j j d| j d| j dS )Nz(policy=, fill=))rE   r5   r<   r   )rC   r2   r2   r3   __repr__  s   zAutoAugment.__repr__)r5   r6   r7   r8   r   r9   r   NEARESTr   listr   r@   tuplestrr.   rA   dictr   boolr}   staticmethodr   r   r   __classcell__r2   r2   rD   r3   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.
    r^   rI      Nnum_opsr   num_magnitude_binsr   r   r=   c                    s,   t    || _|| _|| _|| _|| _d S r>   )r?   r@   r   r   r   r   r   )rC   r   r   r   r   r   rD   r2   r3   r@   2  s   

zRandAugment.__init__rr   rs   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   Frb   Trt   r   r   ru   rc   rG   rV   rv   r(   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   rw   r{   rx   ry   rz   r.   r|   r2   r2   r3   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   r2   r   r   r2   r2   r3   r   a  r   z'RandAugment.forward.<locals>.<listcomp>r   r   r   r^   r   r   )r   r*   r   r   r   r.   r   r}   r   ranger   rw   r   r   r   r   keysndimr   r4   r   )rC   r   r   r   r   r   r   _op_indexr   r   r   r   r2   r2   r3   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   )rE   r5   r   r   r   r   r   rC   sr2   r2   r3   r   o  s   
	zRandAugment.__repr__)r5   r6   r7   r8   r   r   r.   r   r   r   r@   r   r   r   r   r   r}   r   r   r   r2   r2   rD   r3   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 r>   )r?   r@   r   r   r   )rC   r   r   r   rD   r2   r3   r@     s   

zTrivialAugmentWide.__init__rr   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     `@rG   r   rR   rv   r   r   )rC   rr   r2   r2   r3   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   r2   r   r   r2   r2   r3   r     r   z.TrivialAugmentWide.forward.<locals>.<listcomp>r   r   dtyper   r^   r   r   )r   r*   r   r   r   r.   r   r}   r   rw   r   r   r   r   r   r   longr4   r   )rC   r   r   r   r   r   r   r   r   r   r   r   r2   r2   r3   r     s$   
$zTrivialAugmentWide.forwardc                 C   s*   | j j d| j d| j d| j d}|S )Nz(num_magnitude_bins=r   r   r   )rE   r5   r   r   r   r   r2   r2   r3   r     s   
zTrivialAugmentWide.__repr__)r5   r6   r7   r8   r   r   r.   r   r   r   r@   r   r   r   r   r   r}   r   r   r   r2   r2   rD   r3   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.
    rX   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.)r?   r@   _PARAMETER_MAXr1   r   r   r   r   r   r   r   )rC   r   r   r   r   r   r   r   rD   r2   r3   r@     s   


zAugMix.__init__rr   rs   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   rb   Tr   g      @r   ru   rV   Frv   )	r   r   r   r   r   r#   r$   r%   r&   rc   )r   r    r!   r"   )rw   rx   ry   rz   r.   r{   r   update)rC   rr   rs   r   r2   r2   r3   r}     s&   $zAugMix._augmentation_spacec                 C   
   t |S r>   )r*   Zpil_to_tensorrC   r   r2   r2   r3   _pil_to_tensor     
zAugMix._pil_to_tensorr   c                 C   r   r>   )r*   Zto_pil_imager   r2   r2   r3   _tensor_to_pil  r   zAugMix._tensor_to_pilparamsc                 C   r   r>   )rw   _sample_dirichlet)rC   r   r2   r2   r3   r     r   zAugMix._sample_dirichletorig_imgc              	   C   st  | j }t|\}}}t|tr-|}t|ttfr!t|g| }q2|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   r2   r   r   r2   r2   r3   r   /  r   z"AugMix.forward.<locals>.<listcomp>r   rV   r   )devicer   r   )lowhighsizer   r   r^   r   r   )%r   r*   r   r   r   r.   r   r   r}   r   r   shapeviewmaxr   r   r   rw   r{   r   r   expandr   r   r   r   r   r   r   r   r   r4   r   Zadd_tor   r   )rC   r   r   r   r   r   r   r   Z	orig_dimsbatchZ
batch_dimsmZcombined_weightsZmixr   augdepthr   r   r   r   r   r   r2   r2   r3   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   )	rE   r5   r   r   r   r   r   r   r   r   r2   r2   r3   r   [  s"   
zAugMix.__repr__)r5   r6   r7   r8   r   ZBILINEARr.   r   r   r   r   r@   r   r   r   r   r}   rw   ZjitZunusedr   r   r   r   r   r   r2   r2   rD   r3   r     sD    
	.:r   )r+   enumr   typingr   rw   r    r   r*   r   __all__r   r   r   r4   r   nnModuler	   r
   r   r   r2   r2   r2   r3   <module>   s0    

P 8]V