-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable AutoAugment and modernize DALI pipeline for ConvNets
Update DALI implementation to use modern "fn" API instead of old class approach. Add a codepath using AutoAugment in DALI training pipeline. It can be easily extended to use other Automatic Augmentations. The integration of DALI Pipeline with PyTorch additionally skips the transposition when exposing NHWC data. Extract the DALI implementation to separate file. Update the readme and some configuration files for EfficientNet: * dali-gpu is the default one, instead of PyTorch * DALI supports AutoAugment (+ a mention of other Automatic Augmentations) Fix a typo in the readme files: --data-backends -> --data-backend This PR is a backport of the changes made to this example, when it was introduced into DALI codebase: https://github.com/NVIDIA/DALI/tree/main/docs/examples/use_cases/pytorch/efficientnet The changes were tested with the smallest EfficientNet only. The usage od DALI GPU pipeline in the training can remove the CPU bottlneck on both DGX-1V and DGX-A100 when running using AMP which was covered in the blogpost: https://developer.nvidia.com/blog/why-automatic-augmentation-matters/ Signed-off-by: Krzysztof Lecki <[email protected]>
- Loading branch information
Showing
8 changed files
with
184 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
PyTorch/Classification/ConvNets/image_classification/dali.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from nvidia.dali import fn | ||
from nvidia.dali import types | ||
|
||
from nvidia.dali.pipeline.experimental import pipeline_def | ||
|
||
from nvidia.dali.auto_aug import auto_augment, trivial_augment | ||
|
||
|
||
@pipeline_def(enable_conditionals=True) | ||
def training_pipe(data_dir, interpolation, image_size, output_layout, automatic_augmentation, | ||
dali_device="gpu", rank=0, world_size=1): | ||
rng = fn.random.coin_flip(probability=0.5) | ||
|
||
jpegs, labels = fn.readers.file(name="Reader", file_root=data_dir, shard_id=rank, | ||
num_shards=world_size, random_shuffle=True, pad_last_batch=True) | ||
|
||
if dali_device == "gpu": | ||
decoder_device = "mixed" | ||
resize_device = "gpu" | ||
else: | ||
decoder_device = "cpu" | ||
resize_device = "cpu" | ||
|
||
# This padding sets the size of the internal nvJPEG buffers to be able to handle all images | ||
# from full-sized ImageNet without additional reallocations | ||
images = fn.decoders.image_random_crop(jpegs, device=decoder_device, output_type=types.RGB, | ||
device_memory_padding=211025920, | ||
host_memory_padding=140544512, | ||
random_aspect_ratio=[0.75, 4.0 / 3.0], | ||
random_area=[0.08, 1.0]) | ||
|
||
images = fn.resize(images, device=resize_device, size=[image_size, image_size], | ||
interp_type=interpolation, antialias=False) | ||
|
||
# Make sure that from this point we are processing on GPU regardless of dali_device parameter | ||
images = images.gpu() | ||
|
||
images = fn.flip(images, horizontal=rng) | ||
|
||
# Based on the specification, apply the automatic augmentation policy. Note, that from the point | ||
# of Pipeline definition, this `if` statement relies on static scalar parameter, so it is | ||
# evaluated exactly once during build - we either include automatic augmentations or not. | ||
# We pass the shape of the image after the resize so the translate operations are done | ||
# relative to the image size. | ||
if automatic_augmentation is None: | ||
output = images | ||
elif automatic_augmentation == "autoaugment": | ||
output = auto_augment.auto_augment_image_net(images, shape=[image_size, image_size]) | ||
else: | ||
raise ValueError(f"Automatic augmentation: '{automatic_augmentation}'" | ||
f" is not supported for DALI") | ||
|
||
|
||
output = fn.crop_mirror_normalize(output, dtype=types.FLOAT, output_layout=output_layout, | ||
crop=(image_size, image_size), | ||
mean=[0.485 * 255, 0.456 * 255, 0.406 * 255], | ||
std=[0.229 * 255, 0.224 * 255, 0.225 * 255]) | ||
|
||
return output, labels | ||
|
||
|
||
@pipeline_def | ||
def validation_pipe(data_dir, interpolation, image_size, image_crop, output_layout, rank=0, | ||
world_size=1): | ||
jpegs, label = fn.readers.file(name="Reader", file_root=data_dir, shard_id=rank, | ||
num_shards=world_size, random_shuffle=False, pad_last_batch=True) | ||
|
||
images = fn.decoders.image(jpegs, device="mixed", output_type=types.RGB) | ||
|
||
images = fn.resize(images, resize_shorter=image_size, interp_type=interpolation, | ||
antialias=False) | ||
|
||
output = fn.crop_mirror_normalize(images, dtype=types.FLOAT, output_layout=output_layout, | ||
crop=(image_crop, image_crop), | ||
mean=[0.485 * 255, 0.456 * 255, 0.406 * 255], | ||
std=[0.229 * 255, 0.224 * 255, 0.225 * 255]) | ||
return output, label |
Oops, something went wrong.