
    NgF                     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 )z9 Multi-Scale RandomErasing

Copyright 2020 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        V/var/www/html/ai-engine/env/lib/python3.11/site-packages/effdet/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S )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.
          ?{Gz?      ?333333?Nconstr   r   r   c                 d   || _         || _        || _        |pd|z  }t          j        |          t          j        |          f| _        || _        |p|| _        |	| _        |	                                }d| _
        d| _        |dk    rd| _
        n|dk    rd| _        n
|r|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lowerr   r   r   )selfr   r   r   
min_aspect
max_aspectmoder#   r$   r%   r   s              r   __init__zRandomErasing.__init__)   s     '  11z>
!%*!5!5tx
7K7K L""/i$zz||6>>"DOOW__!DNN.tw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._erase@   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$1#DK:9 :9 :9C3sQw;TAX56 E	 	r   c           	         |j         \  }}}}|d         }|d         |                    d          z                                  }|d d df                             |          |d d df<   |d d df                             |          |d d df<   | j        dk    r
|| j        z  nd}	t          |	|          D ]6}
|                     ||
         |||
df         ||
df         |j                   7|S )N	img_scaleimg_sizer   r   )max)shape	unsqueezer3   clampr%   r0   rD   r   )r'   inputtarget
batch_sizer7   input_hinput_w
img_scalesrG   batch_startis              r   __call__zRandomErasing.__call__T   s   -2[*
D'7K(
:&)=)=a)@)@@EEGG!!!!Q$--'-::A!!!!Q$--'-::A7;7J7JjDO33PQ{J// 	U 	UAKKa$AATTTTr   )
r   r   r   r   Nr   r   Nr   r   )__name__
__module____qualname____doc__r+   rD   rT    r   r   r   r      sZ         ( VZLR   .  (
 
 
 
 
r   r   )rX   r.   r    r	   float32r   r   rY   r   r   <module>r[      s        :?v 	N 	N 	N 	NH H H H H H H H H Hr   