Skip to content

Commit d6c8f05

Browse files
committed
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]>
1 parent a5388a4 commit d6c8f05

File tree

8 files changed

+181
-186
lines changed

8 files changed

+181
-186
lines changed

PyTorch/Classification/ConvNets/configs.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ anchors:
105105
mixup: 0.2
106106
lr_schedule: cosine
107107
momentum: 0.9
108-
warmup: 16
108+
warmup: 16
109109
epochs: 400
110-
data_backend: pytorch
110+
data_backend: dali-gpu
111111
augmentation: autoaugment
112112
num_classes: 1000
113113
interpolation: bicubic

PyTorch/Classification/ConvNets/efficientnet/README.md

+16-15
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ This model uses SGD optimizer for B0 models and RMSPROP optimizer alpha=0.853 e
9292
* 0.0125 for 128 batch size for B0 models
9393
* 4.09e-06 for 32 batch size for B4 models
9494
scale the learning rate.
95-
* Learning rate schedule:
95+
* Learning rate schedule:
9696
* cosine LR schedule for B0 models
9797
* linear LR schedule for B4 models
9898
* Weight decay (WD):
@@ -128,7 +128,7 @@ The following features are supported by this model:
128128

129129
| Feature | EfficientNet
130130
|-----------------------|--------------------------
131-
|[DALI](https://docs.nvidia.com/deeplearning/dali/release-notes/index.html) | Yes (without autoaugmentation)
131+
|[DALI](https://docs.nvidia.com/deeplearning/dali/release-notes/index.html) | Yes
132132
|[APEX AMP](https://nvidia.github.io/apex/amp.html) | Yes
133133
|[QAT](https://github.com/NVIDIA/TensorRT/tree/master/tools/pytorch-quantization) | Yes
134134

@@ -143,10 +143,11 @@ We use [NVIDIA DALI](https://github.com/NVIDIA/DALI),
143143
which speeds up data loading when CPU becomes a bottleneck.
144144
DALI can use CPU or GPU, and outperforms the PyTorch native dataloader.
145145

146-
Run training with `--data-backends dali-gpu` or `--data-backends dali-cpu` to enable DALI.
147-
For DGXA100 and DGX1 we recommend `--data-backends dali-cpu`.
146+
Run training with `--data-backend dali-gpu` or `--data-backend dali-cpu` to enable DALI.
147+
For DGXA100 and DGX1 we recommend `--data-backend dali-gpu`.
148148

149-
DALI currently does not support Autoaugmentation, so for best accuracy it has to be disabled.
149+
DALI supports [AutoAugment](https://docs.nvidia.com/deeplearning/dali/user-guide/docs/auto_aug/auto_augment.html),
150+
as well as other [Automatic Augmentation schemes](https://docs.nvidia.com/deeplearning/dali/user-guide/docs/auto_aug/auto_aug.html).
150151

151152

152153
**[APEX](https://github.com/NVIDIA/apex)**
@@ -199,7 +200,7 @@ To enable mixed precision, you can:
199200

200201
#### Enabling TF32
201202

202-
TensorFloat-32 (TF32) is the new math mode in [NVIDIA A100](https://www.nvidia.com/en-us/data-center/a100/) GPUs for handling the matrix math also called tensor operations. TF32 running on Tensor Cores in A100 GPUs can provide up to 10x speedups compared to single-precision floating-point math (FP32) on Volta GPUs.
203+
TensorFloat-32 (TF32) is the new math mode in [NVIDIA A100](https://www.nvidia.com/en-us/data-center/a100/) GPUs for handling the matrix math also called tensor operations. TF32 running on Tensor Cores in A100 GPUs can provide up to 10x speedups compared to single-precision floating-point math (FP32) on Volta GPUs.
203204

204205
TF32 Tensor Cores can speed up networks using FP32, typically with no loss of accuracy. It is more robust than FP16 for models which require high dynamic range for weights or activations.
205206

@@ -319,9 +320,9 @@ For example:
319320
You can download pre-trained weights from NGC:
320321

321322
```bash
322-
wget --content-disposition -O
323+
wget --content-disposition -O
323324

324-
unzip
325+
unzip
325326
```
326327

327328
To run inference on ImageNet, run:
@@ -441,12 +442,12 @@ URL for each model can be found in the following table:
441442

442443
| **Model** | **NGC weights URL** |
443444
|:---------:|:-------------------:|
444-
| efficientnet-b0 | https://api.ngc.nvidia.com/v2/models/nvidia/efficientnet_b0_pyt_amp/versions/20.12.0/files/nvidia_efficientnet-b0_210412.pth |
445-
| efficientnet-b4 | https://api.ngc.nvidia.com/v2/models/nvidia/efficientnet_b4_pyt_amp/versions/20.12.0/files/nvidia_efficientnet-b4_210412.pth |
446-
| efficientnet-widese-b0 | https://api.ngc.nvidia.com/v2/models/nvidia/efficientnet_widese_b0_pyt_amp/versions/20.12.0/files/nvidia_efficientnet-widese-b0_210412.pth |
447-
| efficientnet-widese-b4 | https://api.ngc.nvidia.com/v2/models/nvidia/efficientnet_widese_b4_pyt_amp/versions/20.12.0/files/nvidia_efficientnet-widese-b4_210412.pth |
448-
| efficientnet-quant-b0 | https://api.ngc.nvidia.com/v2/models/nvidia/efficientnet_b0_pyt_qat_ckpt_fp32/versions/21.03.0/files/nvidia-efficientnet-quant-b0-130421.pth |
449-
| efficientnet-quant-b4 | https://api.ngc.nvidia.com/v2/models/nvidia/efficientnet_b4_pyt_qat_ckpt_fp32/versions/21.03.0/files/nvidia-efficientnet-quant-b4-130421.pth |
445+
| efficientnet-b0 | https://api.ngc.nvidia.com/v2/models/nvidia/efficientnet_b0_pyt_amp/versions/20.12.0/files/nvidia_efficientnet-b0_210412.pth |
446+
| efficientnet-b4 | https://api.ngc.nvidia.com/v2/models/nvidia/efficientnet_b4_pyt_amp/versions/20.12.0/files/nvidia_efficientnet-b4_210412.pth |
447+
| efficientnet-widese-b0 | https://api.ngc.nvidia.com/v2/models/nvidia/efficientnet_widese_b0_pyt_amp/versions/20.12.0/files/nvidia_efficientnet-widese-b0_210412.pth |
448+
| efficientnet-widese-b4 | https://api.ngc.nvidia.com/v2/models/nvidia/efficientnet_widese_b4_pyt_amp/versions/20.12.0/files/nvidia_efficientnet-widese-b4_210412.pth |
449+
| efficientnet-quant-b0 | https://api.ngc.nvidia.com/v2/models/nvidia/efficientnet_b0_pyt_qat_ckpt_fp32/versions/21.03.0/files/nvidia-efficientnet-quant-b0-130421.pth |
450+
| efficientnet-quant-b4 | https://api.ngc.nvidia.com/v2/models/nvidia/efficientnet_b4_pyt_qat_ckpt_fp32/versions/21.03.0/files/nvidia-efficientnet-quant-b4-130421.pth |
450451

451452
To run inference on ImageNet, run:
452453

@@ -469,7 +470,7 @@ During the QAT process, evaluation is done in the same way as during standard tr
469470

470471
or to evaluate a created checkpoint with the flag `--evaluate`:
471472

472-
`python ./quant_main.py --arch efficientnet-quant-<version> --evaluate --epochs 1 --resume <path to checkpoint> -b <batch size> <path to imagenet>`
473+
`python ./quant_main.py --arch efficientnet-quant-<version> --evaluate --epochs 1 --resume <path to checkpoint> -b <batch size> <path to imagenet>`
473474

474475
It also can run on multi-GPU in an identical way as the standard `main.py` script:
475476

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from nvidia.dali import fn
16+
from nvidia.dali import types
17+
18+
from nvidia.dali.pipeline.experimental import pipeline_def
19+
20+
from nvidia.dali.auto_aug import auto_augment, trivial_augment
21+
22+
23+
@pipeline_def(enable_conditionals=True)
24+
def training_pipe(data_dir, interpolation, image_size, output_layout, automatic_augmentation,
25+
dali_device="gpu", rank=0, world_size=1):
26+
rng = fn.random.coin_flip(probability=0.5)
27+
28+
jpegs, labels = fn.readers.file(name="Reader", file_root=data_dir, shard_id=rank,
29+
num_shards=world_size, random_shuffle=True, pad_last_batch=True)
30+
31+
if dali_device == "gpu":
32+
decoder_device = "mixed"
33+
resize_device = "gpu"
34+
else:
35+
decoder_device = "cpu"
36+
resize_device = "cpu"
37+
38+
# This padding sets the size of the internal nvJPEG buffers to be able to handle all images
39+
# from full-sized ImageNet without additional reallocations
40+
images = fn.decoders.image_random_crop(jpegs, device=decoder_device, output_type=types.RGB,
41+
device_memory_padding=211025920,
42+
host_memory_padding=140544512,
43+
random_aspect_ratio=[0.75, 4.0 / 3.0],
44+
random_area=[0.08, 1.0])
45+
46+
images = fn.resize(images, device=resize_device, size=[image_size, image_size],
47+
interp_type=interpolation, antialias=False)
48+
49+
# Make sure that from this point we are processing on GPU regardless of dali_device parameter
50+
images = images.gpu()
51+
52+
images = fn.flip(images, horizontal=rng)
53+
54+
# Based on the specification, apply the automatic augmentation policy. Note, that from the point
55+
# of Pipeline definition, this `if` statement relies on static scalar parameter, so it is
56+
# evaluated exactly once during build - we either include automatic augmentations or not.
57+
# We pass the shape of the image after the resize so the translate operations are done
58+
# relative to the image size.
59+
if automatic_augmentation is None:
60+
output = images
61+
elif automatic_augmentation == "autoaugment":
62+
output = auto_augment.auto_augment_image_net(images, shape=[image_size, image_size])
63+
else:
64+
raise ValueError(f"Automatic augmentation: '{automatic_augmentation}'"
65+
f" is not supported for DALI")
66+
67+
68+
output = fn.crop_mirror_normalize(output, dtype=types.FLOAT, output_layout=output_layout,
69+
crop=(image_size, image_size),
70+
mean=[0.485 * 255, 0.456 * 255, 0.406 * 255],
71+
std=[0.229 * 255, 0.224 * 255, 0.225 * 255])
72+
73+
return output, labels
74+
75+
76+
@pipeline_def
77+
def validation_pipe(data_dir, interpolation, image_size, image_crop, output_layout, rank=0,
78+
world_size=1):
79+
jpegs, label = fn.readers.file(name="Reader", file_root=data_dir, shard_id=rank,
80+
num_shards=world_size, random_shuffle=False, pad_last_batch=True)
81+
82+
images = fn.decoders.image(jpegs, device="mixed", output_type=types.RGB)
83+
84+
images = fn.resize(images, resize_shorter=image_size, interp_type=interpolation,
85+
antialias=False)
86+
87+
output = fn.crop_mirror_normalize(images, dtype=types.FLOAT, output_layout=output_layout,
88+
crop=(image_crop, image_crop),
89+
mean=[0.485 * 255, 0.456 * 255, 0.406 * 255],
90+
std=[0.229 * 255, 0.224 * 255, 0.225 * 255])
91+
return output, label

0 commit comments

Comments
 (0)