U
    ­Ê¹jSn  ã                   @   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)Ú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�} �n¾|dkr|t j| dddgddt t |¡¡g||ddgd�} �n€|dkr¬t j| dt|ƒdgd|ddg|d�} �nP|d	krÜt j| ddt|ƒgd|ddg|d�} �n |d
krút 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�r†t  | t|ƒ¡} nv|dk�ržt  | |¡} n^|dk�r´t  | ¡} nH|dk�rÊt  | ¡} n2|dk�ràt  | ¡} n|dk�rìn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   úa/var/www/html/bwave/python/venv/lib/python3.8/site-packages/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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rÀd.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 )z†Get 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	rÆt||  ¡ ƒ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rÄt|
| j  ¡ ƒnd}|ràt 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   Fg®Gáz®ï?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rÀt|	t	j
t|	ƒdt	jd�  ¡ ƒnd}|
rÜt	 
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�ròt|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