a
    ¨þ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 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)Ú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   úk/var/www/html/stable-diffusion-webui/venv/lib/python3.9/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   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__r@   r   r   Ú_get_policiesÚpolicies)Úselfr@   r   r   ©Ú	__class__r6   r7   rD   y   s
    
zAutoAugment.__init__)r@   rA   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(   rL   é   ©r)   rL   N©©r*   çš™™™™™é?N©r*   rL   N))r'   rL   é   )r'   rL   é   ©©r*   rJ   N)r(   çš™™™™™É?é   )rX   ©r!   rS   rK   ))r(   rL   é   rT   ))r'   rS   rO   ©r*   r   N))r!   rY   r\   )r(   rL   rK   )rT   )r'   rJ   rV   )r[   ©r$   rJ   r   ))r!   rJ   rM   rT   ))r*   r   NrR   ©©r+   rL   Nr]   ©)r$   rL   rZ   )r%   r   rK   )r[   )r$   r   é   ))r$   rS   rK   )r(   rS   rU   ))r&   rJ   rU   r`   ))r   rL   rO   r]   )r^   rT   rW   rN   r_   ra   rQ   ))©r+   çš™™™™™¹?N)r%   rY   rV   ))r!   çffffffæ?rb   )r   ç333333Ó?rM   ))r&   rS   r   )r&   çÍÌÌÌÌÌì?r\   ))r   ç      à?rK   ©r    re   rM   ))r)   rh   N©r*   rg   N))r   rY   rU   )r'   rf   rU   ))r$   rJ   r\   )r#   rL   rU   ))r&   rf   rM   )r#   re   rM   )rT   )r*   rh   N))r%   rL   rU   )r&   rL   rO   ))r$   re   rU   )r   rh   rK   ))r*   rf   N)r)   rJ   N))r    rJ   r\   )r&   rY   rV   ))r#   rg   rV   )r$   rY   rK   ))r(   rh   rb   )r+   r   N)©r*   rY   NrP   )rk   rT   ))r$   rg   rM   rT   )©r)   rS   N)r(   rY   rK   ))r#   rd   r\   )r$   re   r   ))r(   rJ   rO   ©r)   rg   N))r    rg   rM   ri   )rm   )r(   rS   r\   )rR   rc   )ri   rm   ))©r   rg   rZ   )r+   rY   N)©r   rg   rK   ©r+   re   N)rT   )r(   rL   rV   ©©r+   rg   NrT   ©rT   )r!   rg   r\   )rn   rl   )ro   )r+   rJ   N))r   rg   rO   )r(   rY   rV   )rr   rl   rs   )rn   )r(   rf   r\   ))r   rS   rK   rp   )rj   )r    rL   rV   rq   ))r%   rf   r\   ©r!   rS   rZ   )©r+   rS   N)r    r   rb   ))r   re   rV   )r(   rJ   rK   )r`   rt   ))r   rf   rU   )r   rg   r\   ))r   rd   rV   r`   ))r(   re   rb   )r    rL   rU   ))r   rS   rZ   ru   ))r   re   rM   )r    rS   r\   ))r   rS   rO   )r)   re   N))r   re   rb   rc   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   rf   TçtþÅ Ý?r   r   ç      >@rg   rK   rZ   Fç     ào@)r   r   r   r    r!   r#   r$   r%   r&   r'   r(   r)   r*   r+   )ÚtorchÚlinspaceÚarangeÚroundr2   Útensor©rG   rw   rx   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   )rb   rb   )r2   r|   Ú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urTdd„ |D ƒ}|  t| j	ƒ¡\}}}|  
d||f¡}	t| j	| ƒD ]n\}
\}}}||
 |krˆ|	| \}}|du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>  ó    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*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.
    rb   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__rv   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   Frf   Try   r   r   rz   rg   rK   rZ   r{   ©r,   r   r   r   r    r!   r#   r$   r%   r&   r'   r(   r)   r*   ©r|   r€   r}   r~   r   r2   r   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u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  r‘   z'RandAugment.forward.<locals>.<listcomp>r„   r   r   rb   r“   r"   )r   r.   r”   r•   r   r2   rŒ   r‚   r¬   Úranger«   r|   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    
ÿþýüûÿ	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"   ú
ù*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   rA   c                    s    t ƒ  ¡  || _|| _|| _d S rB   )rC   rD   r¬   r   r   )rG   r¬   r   r   rH   r6   r7   rD   Œ  s    
zTrivialAugmentWide.__init__)rw   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   rV   r{   r­   r®   )rG   rw   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u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   ·  r‘   z.TrivialAugmentWide.forward.<locals>.<listcomp>r„   r   ©Údtyper   rb   r“   r"   )r   r.   r”   r•   r   r2   rŒ   r‚   r¬   r|   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   ü
û 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.
    r\   éÿÿÿÿ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__rv   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   rf   Tr   g      @r   rz   rZ   Fr{   )	r   r   r   r    r!   r'   r(   r)   r*   rg   )r#   r$   r%   r&   )r|   r}   r~   r   r2   r€   rÁ   Úupdate)rG   rw   rx   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   )r|   Ú_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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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   /  r‘   z"AugMix.forward.<locals>.<listcomp>r   rZ   r   )Údevicer¼   r„   )ÚlowÚhighÚsizer¹   r   rb   r“   r"   )%r   r.   r”   r•   r   r2   rŒ   rÅ   r‚   rÂ   r±   ÚshapeÚviewÚmaxr³   rÍ   rÈ   r|   r€   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"    
ÿþýüûúùÿzAugMix.__repr__)r9   r:   r;   r<   r
   ZBILINEARr2   rŒ   r§   r   r   rD   r   r   r¦   r   r‚   r|   ZjitZunusedrÅ   rÆ   rÈ   r    r¤   r©   r6   r6   rH   r7   r   Ò  s4   ø
÷*:r   )r/   Úenumr   Útypingr   r   r   r   r|   r   Ú r	   r.   r
   Ú__all__r¦   rŒ   r8   r   ÚnnÚModuler   r   r   r   r6   r6   r6   r7   Ú<module>   s   ÿP 8]V