Skip to content
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 AutoAugment and ImageNet policy #4702

Merged
merged 7 commits into from
Mar 13, 2023

Conversation

stiepan
Copy link
Member

@stiepan stiepan commented Mar 9, 2023

Category:

New feature (non-breaking change which adds functionality)

Description:

Adds AutoAugment: augmentation scheme that applies augmentations randomly per sample based on a policy. The policy consits of sub-polcies, each sub-policy is a sequence of augmentations to apply. Sub-policies are chosen uniformy at random for each sample. Then, the augmentations from the sequence are applied one-by-one. Each augmentation is described with the probability of it being applied or skipped and a magnitude - inuitevely a strenght of the operation.

The ImageNet policy is a policy found by the authors of the paper that introduces AA shown to improve the accuracy (https://arxiv.org/abs/1805.09501).

Additional information:

This is a part of Automatic Augmentation module #4648

The implementation groups the same augmentations from different sub-policies in hope for bigger partial-batches. For example, the ImageNet policy has 25 sub-policies, but in fact only 8 augmentations at each stage of the sub-policy.

Take the policy:

num_magnitude_bins = 11
[
[(rotate, 0.5, 7), (solarize, 0.3, 2)],
[(shear_x, 0.2, 5), (posterize, 0.6, 8)],
[(rotate, 0.9, 6), (posterize, 0.1, 10)],
]

Here the samples processing is split in three paths. However images that fall into 1st and 3rd sub-policy can be run together in first stage. Similarily, in the second stage, the samples that fall into 2nd and 3rd policy can be run together.

DALI will execute such policy by first: splitting batch into one that is omitted this time based on the probabilities from the first stage, i.e. (0.5, 0.2 and 0.9) and the one to process in the given stage, then the latter batch is split between rotate and shear_x. The rotate is called with per-sample angle that corresponds to the magnitudes from sub_policy (7 and 6). The batch is then merged and similar split is done for the second stage.

Affected modules and functionalities:

Key points relevant for the review:

Tests:

  • Existing tests apply
  • New tests added
    • Python tests
    • GTests
    • Benchmark
    • Other
  • N/A

Checklist

Documentation

  • Existing documentation applies
  • Documentation updated
    • Docstring
    • Doxygen
    • RST
    • Jupyter
    • Other
  • N/A

DALI team only

Requirements

  • Implements new requirements
  • Affects existing requirements
  • N/A

REQ IDs: N/A

JIRA TASK: DALI-3193

@stiepan stiepan added conditional execution Questions related to conditional execution and using `if` statements in DALI automatic augmentations Automatic augmentations (AutoAugment, RandAugment, TrivialAugment and more) support in DALI. labels Mar 9, 2023
@stiepan stiepan assigned awolant and unassigned mzient Mar 9, 2023
@stiepan stiepan mentioned this pull request Mar 9, 2023
18 tasks
Signed-off-by: Kamil Tokarski <ktokarski@nvidia.com>
Signed-off-by: Kamil Tokarski <ktokarski@nvidia.com>
Signed-off-by: Kamil Tokarski <ktokarski@nvidia.com>
Signed-off-by: Kamil Tokarski <ktokarski@nvidia.com>
…kipping

Signed-off-by: Kamil Tokarski <ktokarski@nvidia.com>
@stiepan
Copy link
Member Author

stiepan commented Mar 10, 2023

!build

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [7543750]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [7543750]: BUILD FAILED

Signed-off-by: Kamil Tokarski <ktokarski@nvidia.com>
@stiepan
Copy link
Member Author

stiepan commented Mar 13, 2023

!build

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [7563307]: BUILD STARTED

param = [shape[0] * offset_fraction, 0]
elif extent == 'width':
param = [0, shape[1] * offset_fraction]
params[param_name] = param

Check failure

Code scanning / CodeQL

Potentially uninitialized local variable

Local variable 'param' may be used before it is initialized.
Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

else:
expected_counts[(mag, )] = prob / 2
expected_counts[(-mag, )] = prob / 2
only_right_p = (1 - left_p) * right_p / len(sub_policies)

Check warning

Code scanning / CodeQL

Variable defined multiple times

This assignment to 'only_right_p' is unnecessary as it is [redefined](1) before this value is used.
Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [7563307]: BUILD FAILED

Copy link
Contributor

@awolant awolant left a comment

Choose a reason for hiding this comment

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

Well documented, well tested (+1 for using nose2 utils), very good descriptions of errors. Nice

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [7563307]: BUILD PASSED

Signed-off-by: Kamil Tokarski <ktokarski@nvidia.com>
@stiepan
Copy link
Member Author

stiepan commented Mar 13, 2023

!build

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [7566484]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [7566484]: BUILD FAILED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [7566484]: BUILD PASSED

Comment on lines +15 to +20
try:
import numpy as np
except ImportError:
raise RuntimeError(
"Could not import numpy. DALI's automatic augmentation examples depend on numpy. "
"Please install numpy to use the examples.")
Copy link
Member

Choose a reason for hiding this comment

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

This appears in multiple places throughout our codebase. Wouldn't it be better to just add a dependency to the DALI wheel?

Copy link
Member Author

Choose a reason for hiding this comment

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

I kind of followed a pattern here used elsewhere in DALI, but that makes a lot of sense. If we decide to change it, we can remove all such import wrapper at once.

@stiepan stiepan merged commit 732f236 into NVIDIA:main Mar 13, 2023
@stiepan stiepan mentioned this pull request Mar 14, 2023
18 tasks
stiepan added a commit that referenced this pull request Mar 14, 2023
* Add Policy util and utils for better error reporting
* Add AutoAugment and ImageNet policy
---------

Signed-off-by: Kamil Tokarski <ktokarski@nvidia.com>
aderylo pushed a commit to zpp-dali-2022/DALI that referenced this pull request Mar 17, 2023
* Add Policy util and utils for better error reporting
* Add AutoAugment and ImageNet policy
---------

Signed-off-by: Kamil Tokarski <ktokarski@nvidia.com>
@JanuszL JanuszL mentioned this pull request Sep 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automatic augmentations Automatic augmentations (AutoAugment, RandAugment, TrivialAugment and more) support in DALI. conditional execution Questions related to conditional execution and using `if` statements in DALI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants