@@ -33,17 +33,20 @@ class RandomErasing:
3333 'const' - erase block is constant color of 0 for all channels
3434 'rand' - erase block is same per-cannel random (normal) color
3535 'pixel' - erase block is per-pixel random (normal) color
36+ max_count: maximum number of erasing blocks per image, area per box is scaled by count.
37+ per-image count is randomly chosen between 1 and this value.
3638 """
3739
3840 def __init__ (
3941 self ,
4042 probability = 0.5 , sl = 0.02 , sh = 1 / 3 , min_aspect = 0.3 ,
41- mode = 'const' , device = 'cuda' ):
43+ mode = 'const' , max_count = 1 , device = 'cuda' ):
4244 self .probability = probability
4345 self .sl = sl
4446 self .sh = sh
4547 self .min_aspect = min_aspect
46- self .max_count = 8
48+ self .min_count = 1
49+ self .max_count = max_count
4750 mode = mode .lower ()
4851 self .rand_color = False
4952 self .per_pixel = False
@@ -59,11 +62,13 @@ def _erase(self, img, chan, img_h, img_w, dtype):
5962 if random .random () > self .probability :
6063 return
6164 area = img_h * img_w
62- count = random .randint (1 , self .max_count )
65+ count = self .min_count if self .min_count == self .max_count else \
66+ random .randint (self .min_count , self .max_count )
6367 for _ in range (count ):
6468 for attempt in range (10 ):
65- target_area = random .uniform (self .sl / count , self .sh / count ) * area
66- aspect_ratio = random .uniform (self .min_aspect , 1 / self .min_aspect )
69+ target_area = random .uniform (self .sl , self .sh ) * area / count
70+ log_ratio = (math .log (self .min_aspect ), math .log (1 / self .min_aspect ))
71+ aspect_ratio = math .exp (random .uniform (* log_ratio ))
6772 h = int (round (math .sqrt (target_area * aspect_ratio )))
6873 w = int (round (math .sqrt (target_area / aspect_ratio )))
6974 if w < img_w and h < img_h :
0 commit comments