NGC | Catalog
CatalogResourcesResNet50 v1.5 for PaddlePaddle

ResNet50 v1.5 for PaddlePaddle

Logo for ResNet50 v1.5 for PaddlePaddle
Description
With modified architecture and initialization this ResNet50 version gives ~0.5% better accuracy than original.
Publisher
NVIDIA Deep Learning Examples
Latest Version
22.05.1
Modified
November 4, 2022
Compressed Size
49.78 KB

This resource is using open-source code maintained in github (see the quick-start-guide section) and available for download from NGC

The ResNet50 v1.5 model is a modified version of the original ResNet50 v1 model.

The difference between v1 and v1.5 is that in the bottleneck blocks which requires downsampling, v1 has stride = 2 in the first 1x1 convolution, whereas v1.5 has stride = 2 in the 3x3 convolution.

This difference makes ResNet50 v1.5 slightly more accurate (~0.5% top1) than v1 but comes with a small performance drawback (~5% imgs/sec).

The model is initialized as described in Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification

This model is trained with mixed precision using Tensor Cores on the NVIDIA Ampere GPU architectures. Therefore, researchers can get results over 2x faster than training without Tensor Cores while experiencing the benefits of mixed precision training. This model is tested against each NGC monthly container release to ensure consistent accuracy and performance over time.

Default configuration

The following sections highlight the default configurations for the ResNet50 model.

Optimizer

This model uses SGD with momentum optimizer with the following hyperparameters:

  • Momentum (0.875)
  • Learning rate (LR) = 0.256 for 256 global batch size, for other batch sizes we linearly scale the learning rate. For example, default LR = 2.048 for 2048 global batch size on 8xA100. (256 batch size per GPU)
  • Learning rate schedule - we use cosine LR schedule
  • Linear warmup of the learning rate during the first 5 epochs according to Training ImageNet in 1 hour.
  • Weight decay (WD)= 3.0517578125e-05 (1/32768).
  • We do not apply WD on Batch Norm trainable parameters (gamma/bias)
  • Label smoothing = 0.1
  • We train for:
    • 50 Epochs -> configuration that reaches 75.9% top1 accuracy
    • 90 Epochs -> configuration that reaches 76.9% top1 accuracy (90 epochs is a standard for ImageNet networks)

Data augmentation

This model uses the following data augmentation:

  • For training:
    • Normalization
    • Random resized crop to 224x224
      • Scale from 8% to 100%
      • Aspect ratio from 3/4 to 4/3
    • Random horizontal flip
  • For inference:
    • Normalization
    • Scale to 256x256
    • Center crop to 224x224

Other training recipes

This script does not target any specific benchmark. There are changes that others have made which can speed up convergence and/or increase accuracy.

One of the more popular training recipes is provided by fast.ai.

The fast.ai recipe introduces many changes to the training procedure, one of which is progressive resizing of the training images.

The first part of training uses 128px images, the middle part uses 224px images, and the last part uses 288px images. The final validation is performed on 288px images.

The training script in this repository performs validation on 224px images, just like the original paper described.

These two approaches can't be directly compared, since the fast.ai recipe requires validation on 288px images, and this recipe keeps the original assumption that validation is done on 224px images.

Using 288px images means that more FLOPs are needed during inference to reach the same accuracy.

Feature support matrix

This model supports the following features:

Feature ResNet50
DALI Yes
Paddle AMP Yes
Paddle ASP Yes
Paddle-TRT Yes

Features

  • NVIDIA DALI - DALI is a library accelerating the data preparation pipeline. To accelerate your input pipeline, you only need to define your data loader with the DALI library. For more information about DALI, refer to the DALI product documentation.

  • Paddle AMP is a PaddlePaddle built-in module that provides functions to construct AMP workflow. The details can be found in Automatic Mixed Precision (AMP), which requires minimal network code changes to leverage Tensor Cores performance. Refer to the Enabling mixed precision section for more details.

  • Paddle ASP is a PaddlePaddle built-in module that provides functions to enable automatic sparsity workflow with only a few code line insertions. The full APIs can be found in Paddle.static.sparsity. Paddle ASP support, currently, static graph mode only (Dynamic graph support is under development). Refer to the Enable Automatic SParsity section for more details.

  • Paddle-TRT is a PaddlePaddle inference integration with TensorRT. It selects subgraph to be accelerated by TensorRT, while leaving the rest of the operations to be executed natively by PaddlePaddle. Refer to the Inference with TensorRT section for more details.

DALI

We use NVIDIA DALI, which speeds up data loading when the CPU becomes a bottleneck. DALI can use CPU or GPU and outperforms the PaddlePaddle native data loader.

For data loader, we only support DALI as data loader for now.

Mixed precision training

Mixed precision is the combined use of different numerical precisions in a computational method. Mixed precision training offers significant computational speedup by performing operations in half-precision format while storing minimal information in single-precision to retain as much information as possible in critical parts of the network. Since the introduction of Tensor Cores in NVIDIA Volta, and following with both the NVIDIA Turing and NVIDIA Ampere architectures, significant training speedups are experienced by switching to mixed precision -- up to 3x overall speedup on the most arithmetically intense model architectures. Using mixed precision training requires two steps:

  1. Porting the model to use the FP16 data type where appropriate.
  2. Adding loss scaling to preserve small gradient values.

The ability to train deep learning networks with lower precision was introduced in the Pascal architecture and first supported in CUDA 8 in the NVIDIA Deep Learning SDK.

For information about:

Enabling mixed precision

Mixed precision is enabled in Paddle by using the Automatic Mixed Precision (AMP) while storing variables in single-precision format. Furthermore, to preserve small gradient magnitudes in backpropagation, a loss scaling step must be included when applying gradients. In PaddlePaddle, loss scaling can be easily applied by passing in arguments to GradScaler(). The scaling value to be used can be dynamic or fixed.

For an in-depth walk through on AMP, check out sample usage here. Paddle AMP is a PaddlePaddle built-in module that provides functions to construct AMP workflow. The details can be found in Automatic Mixed Precision (AMP), which requires minimal network code changes to leverage Tensor Cores performance.

Code example to enable mixed precision for static graph:

  • Use paddle.static.amp.decorate to wrap optimizer

    import paddle.static.amp as amp
    mp_optimizer = amp.decorate(optimizer=optimizer, init_loss_scaling=8.0)
    
  • Minimize loss , and get scaled_loss, which is useful when you need customized loss.

    ops, param_grads = mp_optimizer.minimize(loss)
    scaled_loss = mp_optimizer.get_scaled_loss()
    
  • For distributed training, it is recommended to use Fleet to enable amp, which is a unified API for distributed training of PaddlePaddle. For more information, refer to Fleet

    import paddle.distributed.fleet as fleet
    strategy = fleet.DistributedStrategy()
    strategy.amp = True # by default this is false
    optimizer = fleet.distributed_optimizer(optimizer, strategy=dist_strategy)
    

Enabling TF32

TensorFloat-32 (TF32) is the new math mode in NVIDIA 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.

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 a high dynamic range for weights or activations.

For more information, refer to the TensorFloat-32 in the A100 GPU Accelerates AI Training, HPC up to 20x blog post.

TF32 is supported in the NVIDIA Ampere GPU architecture and is enabled by default.

Automatic SParsity

Automatic SParsity (ASP) provides a workflow to transfer deep learning models from dense to 2:4 structured sparse, that allows that inference leverage NVIDIA's Sparse Tensor Core, introduced in Ampere architecture, to theoretically reach 2x speedup and save almost 50% memory usage. The workflow of ASP generally includes two steps:

  • Prune well-trained dense models to 2:4 sparse.
  • Retrain sparse model with same hyper-parameters to recover accuracy.

For more information, refer to

Enable Automatic SParsity

There is a built-in module in PaddlePaddle to enable ASP training, which only needs to insert a couple of lines in the original codebase optimizer decoration and model pruning.

optimizer = sparsity.decorate(optimizer)
...
sparsity.prune_model(main_program)

Moreover, ASP is also compatible with mixed precision training.

Note that currently ASP only supports static graphs (Dynamic graph support is under development).