o
    پid                     @   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   sT   | rt j|||d S |rt j|d ddf||d S t j|d ddf||dS )Ndtypedevicer      )torchemptynormal_zeros)	per_pixel
rand_color
patch_sizer   r    r   L/home/ubuntu/.local/lib/python3.10/site-packages/timm/data/random_erasing.py_get_pixels   s
   r   c                   @   sF   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.
          ?{Gz?UUUUUU?333333?Nconstr   r   r   c                 C   s   || _ || _|| _|pd| }t|t|f| _|| _|p || _|	| _|	 | _
d| _d| _| j
dkr9d| _n| j
dkrBd| _n
| j
rL| j
dksLJ |
| _d S )Nr   FrandTpixelr   )probabilitymin_areamax_areamathloglog_aspect_ratio	min_count	max_count
num_splitslowermoder   r   r   )selfr   r   r   
min_aspect
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 ]l}tdD ]e}	t | j| j| | }
t	
t j| j }ttt	|
| }ttt	|
| }||k r||k rt d|| }t d|| }t| j| j|||f|| jd|d d ||| ||| f<  nq(q"d 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_attempttarget_areaaspect_ratiohwtopleftr   r   r   _eraseN   s2   $zRandomErasing._erasec                 C   s   t | dkr| j|g| |jR   |S | \}}}}| jdkr*|| j nd}t||D ]}| || ||||j q1|S )N   r   r   )lensizer?   r   r!   r+   )r$   input
batch_sizer2   r3   r4   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   )rI   
__module____qualname____doc__r'   r?   rG   rK   r   r   r   r   r      s     
 r   )rN   r)   r   r   float32r   r   r   r   r   r   <module>   s    