Update RandomErasing with some improved arg names, tweak to aspect range

pull/74/head
Ross Wightman 5 years ago
parent 3cc0f91e23
commit 2e955cfd0c

@ -23,13 +23,13 @@ class RandomErasing:
This variant of RandomErasing is intended to be applied to either a batch This variant of RandomErasing is intended to be applied to either a batch
or single image tensor after it has been normalized by dataset mean and std. or single image tensor after it has been normalized by dataset mean and std.
Args: Args:
probability: The probability that the Random Erasing operation will be performed. probability: Probability that the Random Erasing operation will be performed.
sl: Minimum proportion of erased area against input image. min_area: Minimum percentage of erased area wrt input image area.
sh: Maximum proportion of erased area against input image. max_area: Maximum percentage of erased area wrt input image area.
min_aspect: Minimum aspect ratio of erased area. min_aspect: Minimum aspect ratio of erased area.
mode: pixel color mode, one of 'const', 'rand', or 'pixel' mode: pixel color mode, one of 'const', 'rand', or 'pixel'
'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-channel 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. 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. per-image count is randomly chosen between 1 and this value.
@ -37,14 +37,15 @@ class RandomErasing:
def __init__( def __init__(
self, self,
probability=0.5, sl=0.02, sh=1/3, min_aspect=0.3, probability=0.5, min_area=0.02, max_area=1/3, min_aspect=0.3, max_aspect=None,
mode='const', max_count=1, device='cuda'): mode='const', min_count=1, max_count=None, device='cuda'):
self.probability = probability self.probability = probability
self.sl = sl self.min_area = min_area
self.sh = sh self.max_area = max_area
self.min_aspect = min_aspect max_aspect = max_aspect or 1 / min_aspect
self.min_count = 1 self.log_aspect_ratio = (math.log(min_aspect), math.log(max_aspect))
self.max_count = max_count self.min_count = min_count
self.max_count = max_count or min_count
mode = mode.lower() mode = mode.lower()
self.rand_color = False self.rand_color = False
self.per_pixel = False self.per_pixel = False
@ -64,9 +65,8 @@ class RandomErasing:
random.randint(self.min_count, self.max_count) random.randint(self.min_count, self.max_count)
for _ in range(count): for _ in range(count):
for attempt in range(10): for attempt in range(10):
target_area = random.uniform(self.sl, self.sh) * area / count target_area = random.uniform(self.min_area, self.max_area) * area / count
log_ratio = (math.log(self.min_aspect), math.log(1 / self.min_aspect)) aspect_ratio = math.exp(random.uniform(*self.log_aspect_ratio))
aspect_ratio = math.exp(random.uniform(*log_ratio))
h = int(round(math.sqrt(target_area * aspect_ratio))) h = int(round(math.sqrt(target_area * aspect_ratio)))
w = int(round(math.sqrt(target_area / aspect_ratio))) w = int(round(math.sqrt(target_area / aspect_ratio)))
if w < img_w and h < img_h: if w < img_w and h < img_h:

Loading…
Cancel
Save