Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Add standard ResNet data augmentation for ImageRecordIter #11027

Merged
merged 40 commits into from
Jun 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
987a112
add resnet augmentation
hetong007 May 23, 2018
626aa7d
add test
hetong007 May 23, 2018
252188e
fix scope
hetong007 May 23, 2018
b699b75
fix warning
hetong007 May 23, 2018
883c58e
fix lint
hetong007 May 23, 2018
3580a0d
fix lint
hetong007 May 23, 2018
6cc27cc
add color jitter and pca noise
hetong007 May 23, 2018
8906933
fix center crop
hetong007 May 23, 2018
a693eac
merge
hetong007 May 23, 2018
662bbf7
fix lint
hetong007 May 23, 2018
f6a7177
merge
hetong007 May 23, 2018
a777cb9
Trigger CI
hetong007 May 24, 2018
0c1c89b
fix
hetong007 May 24, 2018
74ee0c4
Merge branch 'resnet_aug' of https://github.com/hetong007/incubator-m…
hetong007 May 24, 2018
5e99e7c
fix augmentation implementation
hetong007 May 25, 2018
e171803
Merge remote-tracking branch 'upstream/master' into resnet_aug
hetong007 Jun 5, 2018
6b388ac
add checks for parameters
hetong007 Jun 5, 2018
9615bf6
Merge branch 'resnet_aug' of github.com:hetong007/incubator-mxnet int…
hetong007 Jun 5, 2018
9e372b0
modify training script
hetong007 Jun 6, 2018
8edf384
merge with upstream
Jun 6, 2018
bae03f4
fix compile error
hetong007 Jun 6, 2018
03b8f9f
Trigger CI
hetong007 Jun 6, 2018
54c077e
Trigger CI
hetong007 Jun 6, 2018
37f65fe
modify error message
hetong007 Jun 6, 2018
b6bc840
Merge branch 'resnet_aug' of https://github.com/hetong007/incubator-m…
hetong007 Jun 6, 2018
9bf2494
Trigger CI
hetong007 Jun 6, 2018
d266cb3
Trigger CI
hetong007 Jun 6, 2018
12e0009
Trigger CI
hetong007 Jun 7, 2018
4c79a06
improve script in example
hetong007 Jun 8, 2018
27b99e3
fix script
hetong007 Jun 12, 2018
c8a3bfa
clear code
hetong007 Jun 12, 2018
966360c
Trigger CI
hetong007 Jun 12, 2018
ecdb44a
set min_aspect_ratio to optional, move rotation and pad before random…
hetong007 Jun 14, 2018
6a848bb
fix
hetong007 Jun 15, 2018
f4a0195
Trigger CI
hetong007 Jun 15, 2018
3161574
Trigger CI
hetong007 Jun 15, 2018
0fd347a
Trigger CI
hetong007 Jun 18, 2018
22719b1
Merge remote-tracking branch 'upstream/master' into resnet_aug
hetong007 Jun 18, 2018
43c71e8
fix default values
hetong007 Jun 19, 2018
a0e3015
Trigger CI
hetong007 Jun 19, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 37 additions & 11 deletions example/image-classification/common/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,23 @@ def add_data_args(parser):
def add_data_aug_args(parser):
aug = parser.add_argument_group(
'Image augmentations', 'implemented in src/io/image_aug_default.cc')
aug.add_argument('--random-crop', type=int, default=1,
aug.add_argument('--random-crop', type=int, default=0,
help='if or not randomly crop the image')
aug.add_argument('--random-mirror', type=int, default=1,
aug.add_argument('--random-mirror', type=int, default=0,
help='if or not randomly flip horizontally')
aug.add_argument('--max-random-h', type=int, default=0,
help='max change of hue, whose range is [0, 180]')
aug.add_argument('--max-random-s', type=int, default=0,
help='max change of saturation, whose range is [0, 255]')
aug.add_argument('--max-random-l', type=int, default=0,
help='max change of intensity, whose range is [0, 255]')
aug.add_argument('--min-random-aspect-ratio', type=float, default=None,
help='min value of aspect ratio, whose value is either None or a positive value.')
aug.add_argument('--max-random-aspect-ratio', type=float, default=0,
help='max change of aspect ratio, whose range is [0, 1]')
help='max value of aspect ratio. If min_random_aspect_ratio is None, '
'the aspect ratio range is [1-max_random_aspect_ratio, '
'1+max_random_aspect_ratio], otherwise it is '
'[min_random_aspect_ratio, max_random_aspect_ratio].')
aug.add_argument('--max-random-rotate-angle', type=int, default=0,
help='max angle to rotate, whose range is [0, 360]')
aug.add_argument('--max-random-shear-ratio', type=float, default=0,
Expand All @@ -63,16 +68,28 @@ def add_data_aug_args(parser):
help='max ratio to scale')
aug.add_argument('--min-random-scale', type=float, default=1,
help='min ratio to scale, should >= img_size/input_shape. otherwise use --pad-size')
aug.add_argument('--max-random-area', type=float, default=1,
help='max area to crop in random resized crop, whose range is [0, 1]')
aug.add_argument('--min-random-area', type=float, default=1,
help='min area to crop in random resized crop, whose range is [0, 1]')
aug.add_argument('--brightness', type=float, default=0,
Copy link
Member

@rahul003 rahul003 Jun 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to set defaults to the values we know work well, so that this script is runnable easily. Especially since we no longer set the aug level in train_imagenet.py

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My opinion is that by setting default values to perform no augmentation, users know exactly what kind of augmentation has been applied to the pipeline. It is transparent and will cause less confusion.

On the other hand, not every model uses the same augmentation as ResNet to train on ImageNet. If later another work introduces a better augmentation pipeline with different parameters, then we are unable to justify our default parameters.

Copy link
Member

@rahul003 rahul003 Jun 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about a function like the set_aug_level with the defaults set for resnet, but that function is not automatically called?
My only concern was that we don't have a good source (for users not using GluonCv, on a good set of starting params). Your point also makes sense, so am not too strongly opinionated about this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just reviewed the code again, I think I'll turn off --random-crop and --random-mirror as well.

I can replace set_aug_level with set_resnet_aug. The current one has settings look kind of arbitrary to me.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR removed set_data_aug_level() in data.py, but the function in still used in example/image-classification/fine-tune.py and example/image-classification/train_cifar10.py. Also, shouldn't set_resnet_aug() turn on mirroring, given the default has been changed to 0? Currently:

def set_resnet_aug(aug):
    # standard data augmentation setting for resnet training
    aug.set_defaults(random_crop=0, random_resized_crop=1)
    aug.set_defaults(min_random_area=0.08)
    aug.set_defaults(max_random_aspect_ratio=4./3., min_random_aspect_ratio=3./4.)
    aug.set_defaults(brightness=0.4, contrast=0.4, saturation=0.4, pca_noise=0.1)

@hetong007 @ptrendx

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DickJC123 Hi Dick, I'm fixing these issues in #11533

help='brightness jittering, whose range is [0, 1]')
aug.add_argument('--contrast', type=float, default=0,
help='contrast jittering, whose range is [0, 1]')
aug.add_argument('--saturation', type=float, default=0,
help='saturation jittering, whose range is [0, 1]')
aug.add_argument('--pca-noise', type=float, default=0,
help='pca noise, whose range is [0, 1]')
aug.add_argument('--random-resized-crop', type=int, default=0,
help='whether to use random resized crop')
return aug

def set_data_aug_level(aug, level):
if level >= 1:
aug.set_defaults(random_crop=1, random_mirror=1)
if level >= 2:
aug.set_defaults(max_random_h=36, max_random_s=50, max_random_l=50)
if level >= 3:
aug.set_defaults(max_random_rotate_angle=10, max_random_shear_ratio=0.1, max_random_aspect_ratio=0.25)

def set_resnet_aug(aug):
# standard data augmentation setting for resnet training
aug.set_defaults(random_crop=1, random_resized_crop=1)
aug.set_defaults(min_random_area=0.08)
aug.set_defaults(max_random_aspect_ratio=4./3., min_random_aspect_ratio=3./4.)
aug.set_defaults(brightness=0.4, contrast=0.4, saturation=0.4, pca_noise=0.1)

class SyntheticDataIter(DataIter):
def __init__(self, num_classes, data_shape, max_iter, dtype):
Expand Down Expand Up @@ -135,8 +152,16 @@ def get_rec_iter(args, kv=None):
max_random_scale = args.max_random_scale,
pad = args.pad_size,
fill_value = 127,
random_resized_crop = args.random_resized_crop,
min_random_scale = args.min_random_scale,
max_aspect_ratio = args.max_random_aspect_ratio,
min_aspect_ratio = args.min_random_aspect_ratio,
max_random_area = args.max_random_area,
min_random_area = args.min_random_area,
brightness = args.brightness,
contrast = args.contrast,
saturation = args.saturation,
pca_noise = args.pca_noise,
random_h = args.max_random_h,
random_s = args.max_random_s,
random_l = args.max_random_l,
Expand All @@ -156,6 +181,7 @@ def get_rec_iter(args, kv=None):
mean_r = rgb_mean[0],
mean_g = rgb_mean[1],
mean_b = rgb_mean[2],
resize = 256,
data_name = 'data',
label_name = 'softmax_label',
batch_size = args.batch_size,
Expand Down
4 changes: 2 additions & 2 deletions example/image-classification/train_imagenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
fit.add_fit_args(parser)
data.add_data_args(parser)
data.add_data_aug_args(parser)
# use a large aug level
data.set_data_aug_level(parser, 3)
# uncomment to set standard augmentation for resnet training
# data.set_resnet_aug(parser)
parser.set_defaults(
# network
network = 'resnet',
Expand Down
Loading