
    khd                     T    d Z ddlZddlZddlZej                  dfdZ G d d      Zy)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                  |||      j                         S |r,t        j                  |d   ddf||      j                         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/teggl/fontify/venv/lib/python3.12/site-packages/timm/data/random_erasing.py_get_pixelsr      sk     {{:U6BJJLL	{{JqM1a0fMUUWW{{JqM1a0fMM    c                   >    e Zd ZdZ	 	 	 	 	 	 	 	 	 	 ddZd Zd Zd Zy)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.
    Nc                    || _         || _        || _        |xs d|z  }t        j                  |      t        j                  |      f| _        || _        |xs || _        |	| _        |j                         | _
        d| _        d| _        | j                  dk(  rd| _        |
| _        y | j                  dk(  rd| _        |
| _        y | j                  r| j                  dk(  sJ |
| _        y )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__zRandomErasing.__init__.   s     '  11z>
!%*!5txx
7K L""/i$JJL	99"DO
 	 YY'!!DN  yyDII$888r   c           
         t        j                          | j                  kD  ry ||z  }| j                  | j                  k(  r| j                  n)t        j                  | j                  | j                        }t        |      D ]J  }t        d      D ]8  }	t        j                  | j                  | j                        |z  |z  }
t        j                  t        j                  | j                         }t        t        t        j                  |
|z                    }t        t        t        j                  |
|z                    }||k  s||k  st        j                  d||z
        }t        j                  d||z
        }t        | j                   | j"                  |||f|| j$                        |d d |||z   |||z   f<    J M y )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   st   ==?T---u}"&..DNN"BNN4>>4>>: 	u 	A 9 $nnT]]DMMJTQTYY#xx8M8M(NOdiil(BCDEdiil(BCDEu9U ..EAI6C!>>!UQY7D9Dq!##{{:C3sQw;TAX56 	r   c                 l   t        |j                               dk(  r/ | j                  |g|j                         |j                    |S |j                         \  }}}}| j                  dkD  r|| j                  z  nd}t        ||      D ]$  }| j                  ||   ||||j                         & |S )N   r   r   )lensizer@   r   r!   r,   )r$   input
batch_sizer3   r4   r5   batch_startis           r   __call__zRandomErasing.__call__f   s    uzz|!DKK:

:ekk:  .3ZZ\*JeU;???Q;N*7TUK;
3 GE!HdE5%++FGr   c                     | 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   sV    ^^$$T-=-=,>gdii['QQ
	$..)DNN+;2>>	r   )
g      ?g{Gz?gUUUUUU?g333333?Nr   r   Nr   r   )rL   
__module____qualname____doc__r'   r@   rI   rN    r   r   r   r      s<    * @0	r   r   )rQ   r*   r   r	   float32r   r   rR   r   r   <module>rT      s2       :?v 	N[ [r   