
    khF                     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)z9 Multi-Scale RandomErasing

Copyright 2020 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        V/var/www/teggl/fontify/venv/lib/python3.12/site-packages/effdet/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y)RandomErasinga   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 tweaked for multi-scale obj detection training.
    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| _        |dk(  rd| _
        |
| _        y |dk(  rd| _        |
| _        y |r|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lowerr   r   r   )selfr   r   r   
min_aspect
max_aspectmoder   r    r!   r   s              r   __init__zRandomErasing.__init__)   s     '  11z>
!%*!5txx
7K L""/i$zz|6>"DO
 	 W_!DN  tw..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._erase@   sq   ==?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$1#DKK:9C3sQw;TAX56 	r   c           	         |j                   \  }}}}|d   }|d   |j                  d      z  j                         }|d d df   j                  |      |d d df<   |d d df   j                  |      |d d df<   | j                  dkD  r|| j                  z  nd}	t        |	|      D ].  }
| j                  ||
   |||
df   ||
df   |j                         0 |S )N	img_scaleimg_sizer   r   )max)shape	unsqueezer/   clampr!   r,   r@   r   )r#   inputtarget
batch_sizer3   input_hinput_w
img_scalesrC   batch_startis              r   __call__zRandomErasing.__call__T   s    -2[[*
D'7K(
:&)=)=a)@@EEG!!Q$--'-:A!!Q$--'-:A7;7JjDOO3PQ{J/ 	UAKKa$AAT	Ur   )
g      ?g{Gz?g      ?g333333?Nr   r   Nr   r   )__name__
__module____qualname____doc__r'   r@   rP    r   r   r   r      s     ( VZLR.(
r   r   )rT   r*   r   r	   float32r   r   rU   r   r   <module>rW      s2       :?v 	NH Hr   