
    RЦid                     V    S r SSKrSSKrSSKr\R                  S4S jr " S S5      rg)zRandom 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                     U (       a#  [         R                  " X#US9R                  5       $ U(       a)  [         R                  " US   SS4X4S9R                  5       $ [         R                  " US   SS4X4S9$ )Ndtypedevicer      )torchemptynormal_zeros)	per_pixel
rand_color
patch_sizer   r   s        W/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/timm/data/random_erasing.py_get_pixelsr      se     {{:6BJJLL	{{JqM1a0MUUWW{{JqM1a0MM    c                   H    \ rS rSrSr          S	S jrS rS rS rSr	g)
RandomErasing   a  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.
Nc                    Xl         X l        X0l        U=(       d    SU-  n[        R                  " U5      [        R                  " U5      4U l        Xpl        U=(       d    UU l        Xl        UR                  5       U l
        SU l        SU l        U R                  S:X  a  SU l        O;U R                  S:X  a  SU l        O#U R                  (       a  U R                  S:X  d   eXl        g )Nr   FrandTpixelconst)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__RandomErasing.__init__.   s     '  11z>
!%*!5txx
7K L""/i$JJL	99"DOYY'!!DNyyDII$888r   c           
         [         R                   " 5       U R                  :  a  g X4-  nU R                  U R                  :X  a  U R                  O*[         R                  " U R                  U R                  5      n[        U5       GHL  n[        S5       GH8  n	[         R                  " U R                  U R                  5      U-  U-  n
[        R                  " [         R                  " U R                  6 5      n[        [        [        R                  " X-  5      5      5      n[        [        [        R                  " X-  5      5      5      nX:  d  M  X:  d  M  [         R                  " SX<-
  5      n[         R                  " SXM-
  5      n[        U R                   U R"                  X,U4UU R$                  S9US S 2XU-   2XU-   24'     GMJ     GMO     g )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   _eraseRandomErasing._eraseN   sV   ==?T---}"&..DNN"BNN4>>4>>: 	uA 9$nnT]]DMMJTQTYY#xx8M8M(NOdii(BCDEdii(BCDE9 ..EI6C!>>!UY7D9D!##{{:C3Qw;AX56  % r   c                 f   [        UR                  5       5      S:X  a/  U R                  " U/UR                  5       QUR                  P76   U$ UR                  5       u  p#pEU R                  S:  a  X R                  -  OSn[        Xb5       H"  nU R                  X   X4XQR                  5        M$     U$ )N   r   r   )lensizerB   r   r"   r.   )r%   input
batch_sizer5   r6   r7   batch_startis           r   __call__RandomErasing.__call__f   s    uzz|!KK:

:ekk:  .3ZZ\*Je;???Q;N*7TUK;3EHd5++F 4r   c                     U R                   R                  SU R                   SU R                   3-   nUSU R                   SU R
                   S3-  nU$ )Nz(p=z, mode=z	, count=(z, z)))	__class____name__r   r$   r    r!   )r%   fss     r   __repr__RandomErasing.__repr__q   sV    ^^$$T-=-=,>gdii['QQ
	$..)DNN+;2>>	r   )r   r   r   r!   r   r    r$   r"   r   r   r   )
g      ?g{Gz?gUUUUUU?g333333?Nr   r   Nr   r   )
rP   
__module____qualname____firstlineno____doc__r(   rB   rL   rR   __static_attributes__ r   r   r   r      s<    * @0	r   r   )rW   r,   r   r	   float32r   r   rY   r   r   <module>r[      s2       :?v 	N[ [r   