
    Ngd                     R    d Z ddlZddlZddlZej        dfdZ G 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                     | r)t          j        |||                                          S |r2t          j        |d         ddf||                                          S t          j        |d         ddf||          S )Ndtypedevicer      )torchemptynormal_zeros)	per_pixel
rand_color
patch_sizer   r   s        T/var/www/html/ai-engine/env/lib/python3.11/site-packages/timm/data/random_erasing.py_get_pixelsr      s      N{:U6BBBJJLLL	 N{JqM1a0fMMMUUWWW{JqM1a0fMMMM    c                   @    e Zd ZdZ	 	 	 	 	 	 	 	 	 	 ddZd Zd Z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                    || _         || _        || _        |pd|z  }t          j        |          t          j        |          f| _        || _        |p|| _        |	| _        |	                                | _
        d| _        d| _        | j
        dk    rd| _        n'| j
        dk    rd| _        n| j
        r| j
        dk    sJ |
| _        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   s              r   __init__zRandomErasing.__init__.   s     '  11z>
!%*!5!5tx
7K7K L""/i$JJLL	9"DOOY'!!!DNNy8DI$8$8$8$8r   c           
      L   t          j                     | j        k    rd S ||z  }| j        | j        k    r| j        nt          j        | j        | j                  }t          |          D ]9}t          d          D ]%}	t          j        | j        | j                  |z  |z  }
t          j
        t          j        | j                   }t          t          t          j        |
|z                                }t          t          t          j        |
|z                                }||k     rp||k     rjt          j        d||z
            }t          j        d||z
            }t          | j        | j        |||f|| j                  |d d |||z   |||z   f<    n';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lefts                   r   _erasezRandomErasing._eraseN   s   =??T---Fu}"&.DN"B"BN4>4>:: 	u 	 	A 99  $nT]DMJJTQTYY#x8M(NOOdil(BCCDDEEdil(BCCDDEEu99U .EAI66C!>!UQY77D9Dq!##{: : :C3sQw;TAX56 E!	 	r   c                 n   t          |                                          dk    r( | j        |g|                                |j        R   ng|                                \  }}}}| j        dk    r
|| j        z  nd}t          ||          D ]&}|                     ||         ||||j                   '|S )N   r   r   )lensizerD   r   r%   r0   )r(   input
batch_sizer7   r8   r9   batch_startis           r   __call__zRandomErasing.__call__f   s    uzz||!!DK:

:ek:::::-2ZZ\\*JeU;??Q;N;N*77TUK;
33 G GE!HdE5%+FFFFr   c                 p    | j         j        d| j         d| j         z   }|d| j         d| j         dz  }|S )Nz(p=z, mode=z	, count=(z, z)))	__class____name__r   r'   r#   r$   )r(   fss     r   __repr__zRandomErasing.__repr__q   sN    ^$'QT-='Q'Qdi'Q'QQ
>$.>>DN>>>>	r   )
r   r   r   r   Nr   r   Nr   r   )rP   
__module____qualname____doc__r+   rD   rM   rR    r   r   r   r      s         *    @  0	 	 	    r   r   )rU   r.   r    r	   float32r   r   rV   r   r   <module>rX      s        :?v 	N 	N 	N 	N[ [ [ [ [ [ [ [ [ [r   