-
Notifications
You must be signed in to change notification settings - Fork 629
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add more AutoAugment policies #4753
Conversation
@params(*tuple(itertools.product((True, False), (0, 1), ('height', 'width', 'both')))) | ||
def test_translation(use_shape, offset_fraction, extent): | ||
# make sure the translation helper processes the args properly | ||
# note, it only uses translate_y (as it is in imagenet policy) | ||
shape = [300, 400] | ||
fill_value = 217 | ||
params = {} | ||
if use_shape: | ||
param = offset_fraction | ||
param_name = "max_translate_rel" | ||
else: | ||
param_name = "max_translate_abs" | ||
if extent == 'both': | ||
param = shape[0] * offset_fraction | ||
elif extent == 'height': | ||
param = [shape[0] * offset_fraction, 0] | ||
elif extent == 'width': | ||
param = [0, shape[1] * offset_fraction] | ||
else: | ||
assert False, f"Unrecognized extent={extent}" | ||
params[param_name] = param | ||
translate_y = auto_augment._get_translate_y(use_shape=use_shape, **params) | ||
policy = Policy(f"Policy_{use_shape}_{offset_fraction}", num_magnitude_bins=21, | ||
sub_policies=[[(translate_y, 1, 20)]]) | ||
|
||
@experimental.pipeline_def(enable_conditionals=True, batch_size=3, num_threads=4, device_id=0, | ||
seed=43) | ||
def pipeline(): | ||
encoded_image, _ = fn.readers.file(name="Reader", file_root=images_dir) | ||
image = fn.decoders.image(encoded_image, device="mixed") | ||
image = fn.resize(image, size=shape) | ||
if use_shape: | ||
return auto_augment.apply_auto_augment(policy, image, fill_value=fill_value, | ||
shape=shape) | ||
else: | ||
return auto_augment.apply_auto_augment(policy, image, fill_value=fill_value) | ||
|
||
p = pipeline() | ||
p.build() | ||
output, = p.run() | ||
output = [np.array(sample) for sample in output.as_cpu()] | ||
for i, sample in enumerate(output): | ||
sample = np.array(sample) | ||
if offset_fraction == 1 and extent != "width": | ||
assert np.all(sample == fill_value), f"sample_idx: {i}" | ||
else: | ||
background_count = np.sum(sample == fill_value) | ||
assert background_count / sample.size < 0.1, \ | ||
f"sample_idx: {i}, {background_count / sample.size}" | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's now common utility for AA/TA/RA modules. Tested in test_augmentations
.
@params(*tuple(itertools.product((True, False), (0, 1), ('height', 'width', 'both')))) | ||
def test_translation(use_shape, offset_fraction, extent): | ||
# make sure the translation helper processes the args properly | ||
# note, it only uses translate_y (as it is in imagenet policy) | ||
shape = [300, 400] | ||
fill_value = 105 | ||
params = {} | ||
if use_shape: | ||
param = offset_fraction | ||
param_name = "max_translate_rel" | ||
else: | ||
param_name = "max_translate_abs" | ||
assert extent in ('height', 'width', 'both'), f"{extent}" | ||
if extent == 'both': | ||
param = [shape[0] * offset_fraction, shape[1] * offset_fraction] | ||
elif extent == 'height': | ||
param = [shape[0] * offset_fraction, 0] | ||
elif extent == 'width': | ||
param = [0, shape[1] * offset_fraction] | ||
params[param_name] = param | ||
translate_x, translate_y = rand_augment._get_translations(use_shape=use_shape, **params) | ||
if extent == 'both': | ||
augments = [translate_x, translate_y] | ||
elif extent == 'height': | ||
augments = [translate_y] | ||
elif extent == 'width': | ||
augments = [translate_x] | ||
|
||
@experimental.pipeline_def(enable_conditionals=True, batch_size=3, num_threads=4, device_id=0, | ||
seed=43) | ||
def pipeline(): | ||
encoded_image, _ = fn.readers.file(name="Reader", file_root=images_dir) | ||
image = fn.decoders.image(encoded_image, device="mixed") | ||
image = fn.resize(image, size=shape) | ||
if use_shape: | ||
return rand_augment.apply_rand_augment(augments, image, n=1, m=30, | ||
fill_value=fill_value, shape=shape) | ||
else: | ||
return rand_augment.apply_rand_augment(augments, image, n=1, m=30, | ||
fill_value=fill_value) | ||
|
||
p = pipeline() | ||
p.build() | ||
output, = p.run() | ||
output = [np.array(sample) for sample in output.as_cpu()] | ||
for i, sample in enumerate(output): | ||
sample = np.array(sample) | ||
if offset_fraction == 1: | ||
assert np.all(sample == fill_value), f"sample_idx: {i}" | ||
else: | ||
background_count = np.sum(sample == fill_value) | ||
assert background_count / sample.size < 0.1, \ | ||
f"sample_idx: {i}, {background_count / sample.size}" | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's now common utility for AA/TA/RA modules. Tested in test_augmentations
.
@params(*tuple(itertools.product((True, False), (0, 1), ('x', 'y')))) | ||
def test_translation(use_shape, offset_fraction, extent): | ||
# make sure the translation helper processes the args properly | ||
# note, it only uses translate_y (as it is in imagenet policy) | ||
fill_value = 0 | ||
params = {} | ||
if use_shape: | ||
param = offset_fraction | ||
param_name = "max_translate_rel" | ||
else: | ||
param = 1000 * offset_fraction | ||
param_name = "max_translate_abs" | ||
params[param_name] = param | ||
translation_x, translation_y = trivial_augment._get_translations(use_shape=use_shape, **params) | ||
augment = [translation_x] if extent == 'x' else [translation_y] | ||
|
||
@experimental.pipeline_def(enable_conditionals=True, batch_size=9, num_threads=4, device_id=0, | ||
seed=43) | ||
def pipeline(): | ||
encoded_image, _ = fn.readers.file(name="Reader", file_root=images_dir) | ||
image = fn.decoders.image(encoded_image, device="mixed") | ||
if use_shape: | ||
shape = fn.peek_image_shape(encoded_image) | ||
return trivial_augment.apply_trivial_augment(augment, image, num_magnitude_bins=3, | ||
fill_value=fill_value, shape=shape) | ||
else: | ||
return trivial_augment.apply_trivial_augment(augment, image, num_magnitude_bins=3, | ||
fill_value=fill_value) | ||
|
||
p = pipeline() | ||
p.build() | ||
output, = p.run() | ||
output = [np.array(sample) for sample in output.as_cpu()] | ||
if offset_fraction == 1: | ||
# magnitudes are random here, but some should randomly be maximal | ||
all_black = 0 | ||
for i, sample in enumerate(output): | ||
sample = np.array(sample) | ||
all_black += np.all(sample == fill_value) | ||
assert all_black | ||
else: | ||
for i, sample in enumerate(output): | ||
sample = np.array(sample) | ||
background_count = np.sum(sample == fill_value) | ||
assert background_count / sample.size < 0.1, \ | ||
f"sample_idx: {i}, {background_count / sample.size}" | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's now common utility for AA/TA/RA modules. Tested in test_augmentations
.
def _get_translations(use_shape: bool = False, max_translate_abs: Optional[int] = None, | ||
max_translate_rel: Optional[float] = None) -> List[_Augmentation]: | ||
max_translate_height, max_translate_width = _parse_validate_offset( | ||
use_shape, max_translate_abs=max_translate_abs, max_translate_rel=max_translate_rel, | ||
default_translate_abs=100, default_translate_rel=100 / 224) | ||
if use_shape: | ||
return [ | ||
a.translate_x.augmentation((0, max_translate_width), True), | ||
a.translate_y.augmentation((0, max_translate_height), True), | ||
] | ||
else: | ||
return [ | ||
a.translate_x_no_shape.augmentation((0, max_translate_width), True), | ||
a.translate_y_no_shape.augmentation((0, max_translate_height), True), | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's now common utility for AA/TA/RA modules. Defined in core._utils
.
|
||
|
||
def _get_translations(use_shape: bool = False, max_translate_abs: Optional[int] = None, | ||
max_translate_rel: Optional[float] = None) -> List[_Augmentation]: | ||
max_translate_height, max_translate_width = _parse_validate_offset( | ||
use_shape, max_translate_abs=max_translate_abs, max_translate_rel=max_translate_rel, | ||
default_translate_abs=32, default_translate_rel=1.) | ||
if use_shape: | ||
return [ | ||
a.translate_x.augmentation((0, max_translate_width), True), | ||
a.translate_y.augmentation((0, max_translate_height), True), | ||
] | ||
else: | ||
return [ | ||
a.translate_x_no_shape.augmentation((0, max_translate_width), True), | ||
a.translate_y_no_shape.augmentation((0, max_translate_height), True), | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's now common utility for AA/TA/RA modules. Defined in core._utils
.
!build |
CI MESSAGE: [7763263]: BUILD STARTED |
param_shape = shape | ||
param_name = "max_translate_abs" | ||
if extent == 'both': | ||
param = [param_shape[0] * offset_fraction, param_shape[1] * offset_fraction] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick:
param = [param_shape[0] * offset_fraction, param_shape[1] * offset_fraction] | |
param = offset_fraction * param_shape[:2] |
or
param = [param_shape[0] * offset_fraction, param_shape[1] * offset_fraction] | |
param = offset_fraction * param_shape |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure if that helps - the *
works as composition of concatentation not by broadcasting, so I would either get []
or the same list.
CI MESSAGE: [7763263]: BUILD PASSED |
11e0d27
to
13f8614
Compare
Rebased onto #4747 |
!build |
CI MESSAGE: [7802387]: BUILD STARTED |
CI MESSAGE: [7802387]: BUILD FAILED |
CI MESSAGE: [7802387]: BUILD PASSED |
Signed-off-by: Kamil Tokarski <[email protected]>
Signed-off-by: Kamil Tokarski <[email protected]>
Signed-off-by: Kamil Tokarski <[email protected]>
Signed-off-by: Kamil Tokarski <[email protected]>
Signed-off-by: Kamil Tokarski <[email protected]>
…always skipped augmentations Signed-off-by: Kamil Tokarski <[email protected]>
Signed-off-by: Kamil Tokarski <[email protected]>
Signed-off-by: Kamil Tokarski <[email protected]>
Signed-off-by: Kamil Tokarski <[email protected]>
Signed-off-by: Kamil Tokarski <[email protected]>
13f8614
to
5fd45e0
Compare
Rebased onto #4751 |
!build |
CI MESSAGE: [7815611]: BUILD STARTED |
augmentations. If tuple is specified, the first component limits height, the second the | ||
width. | ||
augmentations. If a tuple is specified, the first component limits height, the second the | ||
width. Defaults to 250. | ||
max_translate_rel: float or (float, float), optional |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tuple is not specified in the type annotation, but I am not sure if those are not an overkill. The docs rendering of such long annotations is a bit problematic.
CI MESSAGE: [7815611]: BUILD FAILED |
!build |
CI MESSAGE: [7836392]: BUILD STARTED |
CI MESSAGE: [7836392]: BUILD PASSED |
Category:
New feature (non-breaking change which adds functionality)
Description:
This PR adds 3 more (apart from the "v0" image-net policy) policies to auto_augment module (reduced image net, reduced cifar-10, svhn). A new function, simply called
auto_augment
, is added to theauto_augment
module, as a convienience wrapper for applying AA with one of the predefined policies.The predifined policies as introduced in the AA paper are a bit over-specified: 1. they specify meanigless magnitude bins for augmentations that do not accept magnitudes, 2. they specify some augmentations to be run with 0 probability, 3. they specify some augmentations to be run with magnitude such that the operation is in fact an identity. This PR adds warnings and adjusts the definition of policies to address the points 1. and 2.
Now, all AA/TA and RA modules use both translation_x and translation_y augmentations, so I removed the _get_translation_y helper from auto_aug and moved the _get_translations from RA to common util used across the three modules.
Additonally, the PR fills some gaps in the documentation (i.e. max_translation_abs/rel) and the docs in utilities.
Additional information:
Affected modules and functionalities:
Key points relevant for the review:
Tests:
Checklist
Documentation
DALI team only
Requirements
REQ IDs: N/A
JIRA TASK: DALI-3299