|
|
@ -33,16 +33,20 @@ class RandomErasing:
|
|
|
|
'const' - erase block is constant color of 0 for all channels
|
|
|
|
'const' - erase block is constant color of 0 for all channels
|
|
|
|
'rand' - erase block is same per-cannel random (normal) color
|
|
|
|
'rand' - erase block is same per-cannel random (normal) color
|
|
|
|
'pixel' - erase block is per-pixel 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.
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
self,
|
|
|
|
probability=0.5, sl=0.02, sh=1/3, min_aspect=0.3,
|
|
|
|
probability=0.5, sl=0.02, sh=1/3, min_aspect=0.3,
|
|
|
|
mode='const', device='cuda'):
|
|
|
|
mode='const', max_count=1, device='cuda'):
|
|
|
|
self.probability = probability
|
|
|
|
self.probability = probability
|
|
|
|
self.sl = sl
|
|
|
|
self.sl = sl
|
|
|
|
self.sh = sh
|
|
|
|
self.sh = sh
|
|
|
|
self.min_aspect = min_aspect
|
|
|
|
self.min_aspect = min_aspect
|
|
|
|
|
|
|
|
self.min_count = 1
|
|
|
|
|
|
|
|
self.max_count = max_count
|
|
|
|
mode = mode.lower()
|
|
|
|
mode = mode.lower()
|
|
|
|
self.rand_color = False
|
|
|
|
self.rand_color = False
|
|
|
|
self.per_pixel = False
|
|
|
|
self.per_pixel = False
|
|
|
@ -58,18 +62,22 @@ class RandomErasing:
|
|
|
|
if random.random() > self.probability:
|
|
|
|
if random.random() > self.probability:
|
|
|
|
return
|
|
|
|
return
|
|
|
|
area = img_h * img_w
|
|
|
|
area = img_h * img_w
|
|
|
|
for attempt in range(100):
|
|
|
|
count = self.min_count if self.min_count == self.max_count else \
|
|
|
|
target_area = random.uniform(self.sl, self.sh) * area
|
|
|
|
random.randint(self.min_count, self.max_count)
|
|
|
|
aspect_ratio = random.uniform(self.min_aspect, 1 / self.min_aspect)
|
|
|
|
for _ in range(count):
|
|
|
|
h = int(round(math.sqrt(target_area * aspect_ratio)))
|
|
|
|
for attempt in range(10):
|
|
|
|
w = int(round(math.sqrt(target_area / aspect_ratio)))
|
|
|
|
target_area = random.uniform(self.sl, self.sh) * area / count
|
|
|
|
if w < img_w and h < img_h:
|
|
|
|
log_ratio = (math.log(self.min_aspect), math.log(1 / self.min_aspect))
|
|
|
|
top = random.randint(0, img_h - h)
|
|
|
|
aspect_ratio = math.exp(random.uniform(*log_ratio))
|
|
|
|
left = random.randint(0, img_w - w)
|
|
|
|
h = int(round(math.sqrt(target_area * aspect_ratio)))
|
|
|
|
img[:, top:top + h, left:left + w] = _get_pixels(
|
|
|
|
w = int(round(math.sqrt(target_area / aspect_ratio)))
|
|
|
|
self.per_pixel, self.rand_color, (chan, h, w),
|
|
|
|
if w < img_w and h < img_h:
|
|
|
|
dtype=dtype, device=self.device)
|
|
|
|
top = random.randint(0, img_h - h)
|
|
|
|
break
|
|
|
|
left = random.randint(0, img_w - w)
|
|
|
|
|
|
|
|
img[:, top:top + h, left:left + w] = _get_pixels(
|
|
|
|
|
|
|
|
self.per_pixel, self.rand_color, (chan, h, w),
|
|
|
|
|
|
|
|
dtype=dtype, device=self.device)
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
def __call__(self, input):
|
|
|
|
def __call__(self, input):
|
|
|
|
if len(input.size()) == 3:
|
|
|
|
if len(input.size()) == 3:
|
|
|
|