Skip to content

Commit 780c0a9

Browse files
committed
Change args for RandomErasing so only one required for pixel/color mode
1 parent 76539d9 commit 780c0a9

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

data/loader.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ class PrefetchLoader:
1818
def __init__(self,
1919
loader,
2020
rand_erase_prob=0.,
21-
rand_erase_pp=False,
21+
rand_erase_mode='const',
2222
mean=IMAGENET_DEFAULT_MEAN,
2323
std=IMAGENET_DEFAULT_STD):
2424
self.loader = loader
2525
self.mean = torch.tensor([x * 255 for x in mean]).cuda().view(1, 3, 1, 1)
2626
self.std = torch.tensor([x * 255 for x in std]).cuda().view(1, 3, 1, 1)
2727
if rand_erase_prob > 0.:
2828
self.random_erasing = RandomErasing(
29-
probability=rand_erase_prob, per_pixel=rand_erase_pp)
29+
probability=rand_erase_prob, mode=rand_erase_mode)
3030
else:
3131
self.random_erasing = None
3232

@@ -68,7 +68,7 @@ def create_loader(
6868
is_training=False,
6969
use_prefetcher=True,
7070
rand_erase_prob=0.,
71-
rand_erase_pp=False,
71+
rand_erase_mode='const',
7272
interpolation='bilinear',
7373
mean=IMAGENET_DEFAULT_MEAN,
7474
std=IMAGENET_DEFAULT_STD,
@@ -121,7 +121,7 @@ def create_loader(
121121
loader = PrefetchLoader(
122122
loader,
123123
rand_erase_prob=rand_erase_prob if is_training else 0.,
124-
rand_erase_pp=rand_erase_pp,
124+
rand_erase_mode=rand_erase_mode,
125125
mean=mean,
126126
std=std)
127127

data/random_erasing.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
import torch
66

77

8-
def _get_patch(per_pixel, rand_color, patch_size, dtype=torch.float32, device='cuda'):
8+
def _get_pixels(per_pixel, rand_color, patch_size, dtype=torch.float32, device='cuda'):
9+
# NOTE I've seen CUDA illegal memory access errors being caused by the normal_()
10+
# paths, flip the order so normal is run on CPU if this becomes a problem
11+
# ie torch.empty(patch_size, dtype=dtype).normal_().to(device=device)
912
if per_pixel:
1013
return torch.empty(
1114
patch_size, dtype=dtype, device=device).normal_()
@@ -27,20 +30,29 @@ class RandomErasing:
2730
sl: Minimum proportion of erased area against input image.
2831
sh: Maximum proportion of erased area against input image.
2932
min_aspect: Minimum aspect ratio of erased area.
30-
per_pixel: random value for each pixel in the erase region, precedence over rand_color
31-
rand_color: random color for whole erase region, 0 if neither this or per_pixel set
33+
mode: pixel color mode, one of 'const', 'rand', or 'pixel'
34+
'const' - erase block is constant color of 0 for all channels
35+
'rand' - erase block is same per-cannel random (normal) color
36+
'pixel' - erase block is per-pixel random (normal) color
3237
"""
3338

3439
def __init__(
3540
self,
3641
probability=0.5, sl=0.02, sh=1/3, min_aspect=0.3,
37-
per_pixel=False, rand_color=False, device='cuda'):
42+
mode='const', device='cuda'):
3843
self.probability = probability
3944
self.sl = sl
4045
self.sh = sh
4146
self.min_aspect = min_aspect
42-
self.per_pixel = per_pixel # per pixel random, bounded by [pl, ph]
43-
self.rand_color = rand_color # per block random, bounded by [pl, ph]
47+
mode = mode.lower()
48+
self.rand_color = False
49+
self.per_pixel = False
50+
if mode == 'rand':
51+
self.rand_color = True # per block random normal
52+
elif mode == 'pixel':
53+
self.per_pixel = True # per pixel random normal
54+
else:
55+
assert not mode or mode == 'const'
4456
self.device = device
4557

4658
def _erase(self, img, chan, img_h, img_w, dtype):
@@ -55,8 +67,9 @@ def _erase(self, img, chan, img_h, img_w, dtype):
5567
if w < img_w and h < img_h:
5668
top = random.randint(0, img_h - h)
5769
left = random.randint(0, img_w - w)
58-
img[:, top:top + h, left:left + w] = _get_patch(
59-
self.per_pixel, self.rand_color, (chan, h, w), dtype=dtype, device=self.device)
70+
img[:, top:top + h, left:left + w] = _get_pixels(
71+
self.per_pixel, self.rand_color, (chan, h, w),
72+
dtype=dtype, device=self.device)
6073
break
6174

6275
def __call__(self, input):

train.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@
6969
help='Dropout rate (default: 0.1)')
7070
parser.add_argument('--reprob', type=float, default=0.4, metavar='PCT',
7171
help='Random erase prob (default: 0.4)')
72-
parser.add_argument('--repp', action='store_true', default=False,
73-
help='Random erase per-pixel (default: False)')
72+
parser.add_argument('--remode', type=str, default='const',
73+
help='Random erase mode (default: "const")')
7474
parser.add_argument('--lr', type=float, default=0.01, metavar='LR',
7575
help='learning rate (default: 0.01)')
7676
parser.add_argument('--warmup-lr', type=float, default=0.0001, metavar='LR',
@@ -223,7 +223,7 @@ def main():
223223
is_training=True,
224224
use_prefetcher=True,
225225
rand_erase_prob=args.reprob,
226-
rand_erase_pp=args.repp,
226+
rand_erase_mode=args.remode,
227227
interpolation='random', # FIXME cleanly resolve this? data_config['interpolation'],
228228
mean=data_config['mean'],
229229
std=data_config['std'],

0 commit comments

Comments
 (0)