Skip to content

Commit 6946281

Browse files
committed
Experimenting with random erasing changes
1 parent aeaaad7 commit 6946281

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

timm/data/random_erasing.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def __init__(
4343
self.sl = sl
4444
self.sh = sh
4545
self.min_aspect = min_aspect
46+
self.max_count = 8
4647
mode = mode.lower()
4748
self.rand_color = False
4849
self.per_pixel = False
@@ -58,18 +59,20 @@ def _erase(self, img, chan, img_h, img_w, dtype):
5859
if random.random() > self.probability:
5960
return
6061
area = img_h * img_w
61-
for attempt in range(100):
62-
target_area = random.uniform(self.sl, self.sh) * area
63-
aspect_ratio = random.uniform(self.min_aspect, 1 / self.min_aspect)
64-
h = int(round(math.sqrt(target_area * aspect_ratio)))
65-
w = int(round(math.sqrt(target_area / aspect_ratio)))
66-
if w < img_w and h < img_h:
67-
top = random.randint(0, img_h - h)
68-
left = random.randint(0, img_w - w)
69-
img[:, top:top + h, left:left + w] = _get_pixels(
70-
self.per_pixel, self.rand_color, (chan, h, w),
71-
dtype=dtype, device=self.device)
72-
break
62+
count = random.randint(1, self.max_count)
63+
for _ in range(count):
64+
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)
67+
h = int(round(math.sqrt(target_area * aspect_ratio)))
68+
w = int(round(math.sqrt(target_area / aspect_ratio)))
69+
if w < img_w and h < img_h:
70+
top = random.randint(0, img_h - h)
71+
left = random.randint(0, img_w - w)
72+
img[:, top:top + h, left:left + w] = _get_pixels(
73+
self.per_pixel, self.rand_color, (chan, h, w),
74+
dtype=dtype, device=self.device)
75+
break
7376

7477
def __call__(self, input):
7578
if len(input.size()) == 3:

0 commit comments

Comments
 (0)