a
    þdd  ã                   @   s>   d Z ddlZddlZddlZejdfdd„ZG dd„ dƒZdS )zÊ Random Erasing (Cutout)

Originally inspired by impl at https://github.com/zhunzhong07/Random-Erasing, Apache 2.0
Copyright Zhun Zhong & Liang Zheng

Hacked together by / Copyright 2019, Ross Wightman
é    NÚcudac                 C   sX   | rt j|||d ¡ S |r:t j|d ddf||d ¡ S t j|d ddf||dS d S )N©ÚdtypeÚdevicer   é   )ÚtorchÚemptyZnormal_Úzeros)Ú	per_pixelÚ
rand_colorZ
patch_sizer   r   © r   úa/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/timm/data/random_erasing.pyÚ_get_pixels   s
    r   c                
   @   s2   e Zd ZdZddd„Zdd„ Zdd„ Zdd„ ZdS )ÚRandomErasingaY   Randomly selects a rectangle region in an image and erases its pixels.
        'Random Erasing Data Augmentation' by Zhong et al.
        See https://arxiv.org/pdf/1708.04896.pdf

        This variant of RandomErasing is intended to be applied to either a batch
        or single image tensor after it has been normalized by dataset mean and std.
    Args:
         probability: Probability that the Random Erasing operation will be performed.
         min_area: Minimum percentage of erased area wrt input image area.
         max_area: Maximum percentage of erased area wrt input image area.
         min_aspect: Minimum aspect ratio of erased area.
         mode: pixel color mode, one of 'const', 'rand', or 'pixel'
            'const' - erase block is constant color of 0 for all channels
            'rand'  - erase block is same per-channel random (normal) color
            'pixel' - erase block is per-pixel random (normal) color
        max_count: maximum number of erasing blocks per image, area per box is scaled by count.
            per-image count is randomly chosen between 1 and this value.
    ç      à?ç{®Gáz”?çUUUUUUÕ?ç333333Ó?NÚconstr   r   r   c                 C   s¢   || _ || _|| _|pd| }t |¡t |¡f| _|| _|p@|| _|	| _| 	¡ | _
d| _d| _| j
dkrrd| _n&| j
dkr„d| _n| j
r˜| j
dks˜J ‚|
| _d S )Nr   FZrandTZpixelr   )ÚprobabilityÚmin_areaÚmax_areaÚmathÚlogÚlog_aspect_ratioÚ	min_countÚ	max_countÚ
num_splitsÚlowerÚmoder   r
   r   )Úselfr   r   r   Z
min_aspectZ
max_aspectr   r   r   r   r   r   r   r   Ú__init__.   s"    



zRandomErasing.__init__c              	   C   s"  t   ¡ | jkrd S || }| j| jkr,| jnt  | j| j¡}t|ƒD ]Ø}tdƒD ]Ê}	t  | j| j¡| | }
t	 
t j| jŽ ¡}ttt	 |
| ¡ƒƒ}ttt	 |
| ¡ƒƒ}||k rP||k rPt  d|| ¡}t  d|| ¡}t| j| j|||f|| jd|d d …||| …||| …f<  qDqPqDd S )Né
   r   r   )Úrandomr   r   r   ÚrandintÚrangeÚuniformr   r   r   Úexpr   ÚintÚroundÚsqrtr   r
   r   r   )r    ÚimgÚchanÚimg_hÚimg_wr   ÚareaÚcountÚ_ÚattemptZtarget_areaZaspect_ratioÚhÚwÚtopÚleftr   r   r   Ú_eraseN   s.    ÿû$zRandomErasing._erasec                 C   s‚   t | ¡ ƒdkr.| j|g| ¡ ¢|j‘R Ž  nP| ¡ \}}}}| jdkrR|| j nd}t||ƒD ]}|  || ||||j¡ q`|S )Né   r   r   )ÚlenÚsizer7   r   r   r%   )r    ÚinputZ
batch_sizer,   r-   r.   Zbatch_startÚir   r   r   Ú__call__f   s    zRandomErasing.__call__c                 C   s:   | j jd| j› d| j›  }|d| j› d| j› d7 }|S )Nz(p=z, mode=z	, count=(z, z)))Ú	__class__Ú__name__r   r   r   r   )r    Úfsr   r   r   Ú__repr__q   s    zRandomErasing.__repr__)
r   r   r   r   Nr   r   Nr   r   )r?   Ú
__module__Ú__qualname__Ú__doc__r!   r7   r=   rA   r   r   r   r   r      s             õ
 r   )rD   r#   r   r   Úfloat32r   r   r   r   r   r   Ú<module>   s
   