This repository provides a script and recipe to train the Mask R-CNN model for Tensorflow to achieve state-of-the-art accuracy, and is tested and maintained by NVIDIA.
- Model overview
- Setup
- Quick Start Guide
- Advanced
- Performance
- Release notes
Mask R-CNN is a convolution-based neural network for the task of object instance segmentation. The paper describing the model can be found here. NVIDIA’s Mask R-CNN 20.06 is an optimized version of Google's TPU implementation, leveraging mixed precision arithmetic using Tensor Cores on NVIDIA Volta, Turing, and Ampere GPUs while maintaining target accuracy.
This model is trained with mixed precision using Tensor Cores on Volta, Turing, and the NVIDIA Ampere GPU architectures. Therefore, researchers can get results 2.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.
This repository also contains scripts to interactively launch training, benchmarking and inference routines in a Docker container.
The major differences between the official implementation of the paper and our version of Mask R-CNN are as follows:
- Mixed precision support with TensorFlow AMP.
- Gradient accumulation to simulate larger batches.
- Custom fused CUDA kernels for faster computations.
There are other publicly NVIDIA available implementations of Mask R-CNN:
Mask R-CNN builds on top of Faster R-CNN adding an additional mask head for the task of image segmentation.
The architecture consists of the following:
- ResNet-50 backbone with Feature Pyramid Network (FPN)
- Region proposal network (RPN) head
- RoI Align
- Bounding and classification box head
- Mask head
The Mask R-CNN configuration and the hyper-parameters for training and testing purposes are in separate files.
The default configuration of this model can be found at mask-rcnn/hyperparameters/mask_rcnn_params.py
.
The default configuration is as follows:
-
Feature extractor:
- Images resized with aspect ratio maintained and smaller side length between [832,1344]
- Ground Truth mask size 112
- Backbone network weights are frozen after second epoch
-
RPN:
- Anchor stride set to 16
- Anchor sizes set to (32, 64, 128, 256, 512)
- Foreground IOU Threshold set to 0.7, Background IOU Threshold set to 0.3
- RPN target fraction of positive proposals set to 0.5
- Train Pre-NMS Top proposals set to 2000 per FPN layer
- Train Post-NMS Top proposals set to 1000
- Test Pre-NMS Top proposals set to 1000 per FPN layer
- Test Post-NMS Top proposals set to 1000
- RPN NMS Threshold set to 0.7
-
RoI heads:
- Foreground threshold set to 0.5
- Batch size per image set to 512
- Positive fraction of batch set to 0.25
The default hyper-parameters can be found at mask-rcnn/hyperparameters/cmdline_utils.py
.
These hyperparameters can be overridden through the command-line options, in the launch scripts.
The following features are supported by this model:
Feature | Mask R-CNN |
---|---|
Automatic mixed precision (AMP) | Yes |
Horovod Multi-GPU (NCCL) | Yes |
Accelerated Linear Algebra (XLA) | Yes |
Automatic Mixed Precision (AMP)
This implementation of Mask-RCNN uses AMP to implement mixed precision training. It allows us to use FP16 training with FP32 master weights by modifying just a few lines of code.
Horovod
Horovod is a distributed training framework for TensorFlow, Keras, PyTorch, and MXNet. The goal of Horovod is to make distributed deep learning fast and easy to use. For more information about how to get started with Horovod, see the Horovod: Official repository.
Multi-GPU training with Horovod
Our model uses Horovod to implement efficient multi-GPU training with NCCL. For details, see example sources in this repository or see the TensorFlow tutorial.
XLA support (experimental)
XLA is a domain-specific compiler for linear algebra that can accelerate TensorFlow models with potentially no source code changes. The results are improvements in speed and memory usage: most internal benchmarks run ~1.1-1.5x faster after XLA is enabled.
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 the Volta and Turing architecture, 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 previously required two steps:
- Porting the model to use the FP16 data type where appropriate.
- Adding loss scaling to preserve small gradient values.
This can now be achieved using Automatic Mixed Precision (AMP) for TensorFlow to enable the full mixed precision methodology in your existing TensorFlow model code. AMP enables mixed precision training on Volta and Turing GPUs automatically. The TensorFlow framework code makes all necessary model changes internally.
In TF-AMP, the computational graph is optimized to use as few casts as necessary and maximize the use of FP16, and the loss scaling is automatically applied inside of supported optimizers. AMP can be configured to work with the existing tf.contrib loss scaling manager by disabling the AMP scaling with a single environment variable to perform only the automatic mixed-precision optimization. It accomplishes this by automatically rewriting all computation graphs with the necessary operations to enable mixed precision training and automatic loss scaling.
- How to train using mixed precision, see the Mixed Precision Training paper and Training With Mixed Precision documentation.
- Techniques used for mixed precision training, see the Mixed-Precision Training of Deep Neural Networks blog.
- How to access and enable AMP for TensorFlow, see Using TF-AMP from the TensorFlow User Guide.
Mixed precision is enabled in TensorFlow by using the Automatic Mixed Precision (TF-AMP) extension which casts variables to half-precision upon retrieval, 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 TensorFlow, loss scaling can be applied statically by using simple multiplication of loss by a constant value or automatically, by TF-AMP. Automatic mixed precision makes all the adjustments internally in TensorFlow, providing two benefits over manual operations. First, programmers need not modify network model code, reducing development and maintenance effort. Second, using AMP maintains forward and backward compatibility with all the APIs for defining and running TensorFlow models.
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 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.
The following section lists the requirements that you need to meet in order to start training the Mask R-CNN model.
This repository contains Dockerfile which extends the TensorFlow NGC container and encapsulates some dependencies. Aside from these dependencies, ensure you have the following components:
- NVIDIA Docker
- TensorFlow 20.06-tf1-py3 NGC container
- GPU-based architecture:
For more information about how to get started with NGC containers, see the following sections from the NVIDIA GPU Cloud Documentation and the Deep Learning Documentation:
- Getting Started Using NVIDIA GPU Cloud
- Accessing And Pulling From The NGC Container Registry
- Running TensorFlow
For those unable to use the TensorFlow NGC container, to set up the required environment or create your own container, see the versioned NVIDIA Container Support Matrix.
To train your model using mixed precision with Tensor Cores or using 32-bit, perform the following steps using the default parameters of the Mask R-CNN model on the COCO 2014 dataset.
-
Clone the repository.
git clone https://github.com/NVIDIA/DeepLearningExamples.git cd DeepLearningExamples/TensorFlow/Segmentation/MaskRCNN
-
Build the Mask R-CNN TensorFlow NGC container.
For TensorFlow 1.1x:
bash ./scripts/docker/build_tf1.sh
For TensorFlow 2.x:
bash ./scripts/docker/build_tf2.sh
-
Start an interactive session in the NGC container to run training/inference.
Run the following command to launch the Docker container, the only argument is the absolute path to the
data directory
which holds or will hold thetfrecords
data. If data has not already been downloaded in thedata directory
then download it in step 4, else step 4 can be skipped.For TensorFlow 1.1x:
bash ./scripts/docker/launch_tf1.sh [data directory]
For TensorFlow 2.x:
bash ./scripts/docker/launch_tf2.sh [data directory]
-
Download and preprocess the dataset.
This repository provides scripts to download and extract the COCO 2017 dataset.
If you already have the data then you do not need to run the following script, proceed to downloading the pre-trained weights. Data will be downloaded to thedata directory
provided in step 3.cd dataset bash download_and_preprocess_coco.sh /data
By default, the data is organized into the following structure:
<data/dir> annotations/ instances_train2017.json instances_val2017.json train2017/ COCO_train2017_*.jpg val2017/ COCO_val2017_*.jpg
This repository also provides scripts to download the pre-trained weights of ResNet-50 backbone. The script will make a new directory with the name
weights
in the current directory and download the pre-trained weights in it../download_and_process_pretrained_weights.sh
Ensure that the
weights
folder created has aresnet
folder in it. Inside theresnet
folder there should be 3 folders for checkpoints and weights:extracted_from_maskrcnn
,resnet-nhwc-2018-02-07
andresnet-nhwc-2018-10-14
. Before moving to the next step, ensure the above folders are not empty. -
Start training.
To run training for a default configuration (on 1/4/8 GPUs, AMP/32-bit), run one of the scripts in the
./scripts
directory called./scripts/train{_AMP}_{1,4,8}GPU.sh
. For example:bash ./scripts/train_AMP_8GPU.sh
The above script trains a model and performs an evaluation on the COCO 2017 dataset. By default, this training script:
- Uses 8 GPUs.
- Saves a checkpoint every 3696 iterations and at the end of training. All checkpoints, evaluation results and training logs are saved to the
/results
directory (in the container which can be mounted to a local directory). - Mixed precision training with Tensor Cores.
-
Start validation/evaluation.
- For evaluation with AMP precision:
bash ./scripts/evaluation_AMP.sh
- For evaluation with 32-bit precision:
bash ./scripts/evaluation.sh
- For evaluation with AMP precision:
The following sections provide greater details of the dataset, running training and inference, and the training results.
Descriptions of the key scripts and folders are provided below.
mask_rcnn
- Contains codes to build individual components of the model such as backbone, FPN, RPN, mask and bbox heads etc.download_and_process_pretrained_weights.sh
- Can be used to download backbone pre-trained weights.scripts/
- A folder that contains shell scripts to train the model and perform inferences.train{_AMP}_{1,4,8}GPU.sh
- Training script on 1, 4, 8 GPUs with AMP or 32-bit precision.evaluation_{AMP}.sh
- Evaluations script on either AMP precision or 32-bit precision.benchmark_training.py
- Script for running train performance benchmarks.benchmark_inference.py
- Script for running inference performance benchmarks.
dataset/
- A folder that contains shell scripts and Python files to download the dataset.mask_rcnn_main.py
- Is the main function that is the starting point for the training and evaluation process.docker/
- A folder that contains scripts to build a Docker image and start an interactive session.
You can modify the training behavior through the various flags in both the train_net.py
script and through overriding specific parameters in the config files. Flags in the mask_rcnn_main.py
script are as follows:
--mode
- Specifies the action to take liketrain
,train_and_eval
oreval
.--checkpoint
- The checkpoint of the backbone.--eval_samples
- Number of samples to evaluate.--init_learning_rate
- Initial learning rate.--learning_rate_steps
- Specifies at which steps to reduce the learning rate.--num_steps_per_eval
- Specifies after how many steps of training evaluation should be performed.--total_steps
- Specifies the total number of steps for which training should be run.--train_batch_size
- Training batch size per GPU.--eval_batch_size
- Evaluation batch size per GPU.--amp
- Specifies to use AMP precision or 32-bit.--xla
- Specifies to use XLA (Accelerated Linear Algebra) of TensorFlow or not.
To see the full list of available options and their descriptions, use the -h
or --help
command-line option, for example:
python mask_rcnn_main.py --helpfull
The Mask R-CNN model was trained on the COCO 2017 dataset. This dataset comes with a training and validation set.
This repository contains the ./dataset/download_and_preprocess_coco.sh
script which automatically downloads and preprocesses the training and validation sets. The helper scripts are also present in the dataset/
folder.
The data should be organized into the following structure:
<data/dir>
annotations/
instances_train2017.json
instances_val2017.json
train2017/
COCO_train2017_*.jpg
val2017/
COCO_val2017_*.jpg
Training is performed using the mask_rcnn_main.py
script along with parameters defined in the config files.
The default config files can be found in the
mask_rcnn_tf/mask_rcnn/mask_rcnn_params.py, mask_rcnn_tf/mask_rcnn/cmd_utils.py
files. To specify which GPUs to train on, CUDA_VISIBLE_DEVICES
variable can be changed in the training scripts
provided in the scripts
folder.
This script outputs results to the /results
directory by default. The training log will contain information about:
- Loss, time per iteration, learning rate and memory metrics
- Performance values such as throughput per step
- Test accuracy and test performance values after evaluation
To run inference run mask_rcnn_main.py
with commandline parameter
mode=eval
. To run inference with a checkpoint, set the commandline
parameter --model_dir
to [absolute path of checkpoint folder]
.
The inference log will contain information about:
- Inference time per step
- Inference throughput per step
- Evaluation accuracy and performance values
The following section shows how to run benchmarks measuring the model performance in training and inference modes.
To run training benchmarking on a selected number of GPUs with either AMP or 32-bit precision, run the following script:
python scripts/benchmark_training.py --gpus {1,4,8} --batch_size {2,4} [--amp]
To run inference benchmarking on a single GPU with either AMP or 32-bit precision, run the following script:
python scripts/benchmark_inference.py --batch_size {2,4,8} [--amp]
The following sections provide details on how we achieved our performance and accuracy in training and inference.
Our results were obtained by building and launching the docker containers for TensorFlow 1.1x ./scripts/docker/build_tf1.sh
, bash ./scripts/docker/launch_tf1.sh [data directory]
respectively and running the scripts/train{_AMP}_{1,4,8}GPU.sh
training script on NVIDIA DGX A100 (8x A100 40GB) GPUs.
GPUs | Batch size / GPU | Precision | Final AP BBox | Final AP Segm | Time to train | Time to train speedup |
---|---|---|---|---|---|---|
8 | 4 | TF32 | 0.3777 | 0.3435 | 5 h | - |
8 | 4 | AMP | 0.3782 | 0.3432 | 4 h | 1.25 |
Our results were obtained by building and launching the docker containers for TensorFlow 1.1x ./scripts/docker/build_tf1.sh
, bash ./scripts/docker/launch_tf1.sh [data directory]
respectively and running the scripts/train{_AMP}_{1,4,8}GPU.sh
training script on NVIDIA DGX-1 with 8x V100 16GB GPUs.
GPUs | Batch size / GPU | Precision | Final AP BBox | Final AP Segm | Time to train | Time to train speedup |
---|---|---|---|---|---|---|
8 | 4 | FP32 | 0.3767 | 0.3420 | 14 h | - |
8 | 4 | AMP | 0.3770 | 0.3423 | 9 h | 1.50 |
Learning curves
The following image shows the training loss as a function of iteration for training using DGX A100 (TF32 and TF-AMP) and DGX-1 V100 (FP32 and TF-AMP).
Our results were obtained by running python scripts/benchmark_training.py --gpus {1,4,8} --batch_size {2,4} [--amp]
benchmark script in the TensorFlow 1.1x 20.06-py3
NGC container on NVIDIA DGX A100 (8x A100 40GB) GPUs. Performance numbers (in images per second) were averaged over 200 steps omitting the first 100 warm-up steps.
GPUs | Batch size / GPU | Throughput - TF32 [img/s] | Throughput - mixed precision [img/s] | Throughput speedup (TF32 - mixed precision) | Weak scaling - TF32 | Weak scaling - mixed precision |
---|---|---|---|---|---|---|
1 | 2 | 11.38 | 18.51 | 1.63 | - | - |
1 | 4 | 12.49 | 21.20 | 1.70 | - | - |
4 | 2 | 43.95 | 65.74 | 1.50 | 3.86 | 3.55 |
4 | 4 | 48.26 | 72.96 | 1.51 | 3.86 | 3.44 |
8 | 2 | 81.69 | 114.59 | 1.40 | 7.18 | 6.19 |
8 | 4 | 89.02 | 132.31 | 1.49 | 7.13 | 6.24 |
Our results were obtained by running python scripts/benchmark_training.py --gpus {1,4,8} --batch_size {2,4} [--amp]
benchmark script in the TensorFlow 1.1x 20.06-py3
NGC container on NVIDIA DGX-1 V100 (8x V100 16GB) GPUs. Performance numbers (in images per second) were averaged over 200 steps omitting the first 100 warm-up steps.
GPUs | Batch size / GPU | Throughput - FP32 [img/s] | Throughput - mixed precision [img/s] | Throughput speedup (FP32 - mixed precision) | Weak scaling - TF32 | Weak scaling - mixed precision |
---|---|---|---|---|---|---|
1 | 2 | 6.37 | 12.19 | 1.91 | - | - |
1 | 4 | 6.79 | 12.79 | 1.88 | - | - |
4 | 2 | 23.32 | 30.82 | 1.32 | 3.66 | 2.53 |
4 | 4 | 22.96 | 36.45 | 1.59 | 3.38 | 2.85 |
8 | 2 | 40.18 | 58.41 | 1.45 | 6.31 | 4.79 |
8 | 4 | 42.65 | 62.80 | 1.47 | 6.28 | 4.91 |
To achieve these same results, follow the steps in the Quick Start Guide.
Our results were obtained by running the scripts/train{_AMP}_{1,4,8}GPU.sh
training script in the
TensorFlow 20.06-py3 NGC container on NVIDIA DGX A100 (8x A100 40GB) GPUs.
GPUs | Batch size / GPU | Precision | Final AP BBox | Final AP Segm | Time to train | Time to train speedup |
---|---|---|---|---|---|---|
8 | 4 | TF32 | 0.3783 | 0.3400 | 5 h | - |
8 | 4 | AMP | 0.3796 | 0.3415 | 4 h | 1.25 |
Our results were obtained by running the scripts/train{_AMP}_{1,4,8}GPU.sh
training script in the
TensorFlow 20.06-py3 NGC container on NVIDIA DGX-1 V100 (8x V100 16GB) GPUs.
GPUs | Batch size / GPU | Precision | Final AP BBox | Final AP Segm | Time to train | Time to train speedup |
---|---|---|---|---|---|---|
8 | 4 | FP32 | 0.3784 | 0.3400 | 14 h | - |
8 | 4 | AMP | 0.3786 | 0.3410 | 9 h | 1.50 |
Learning curves
The following image shows the training loss as a function of iteration for training using DGX A100 (TF32 and TF-AMP) and DGX-1 V100 (FP32 and TF-AMP).
Our results were obtained by running python scripts/benchmark_training.py --gpus {1,4,8} --batch_size {2,4} [--amp]
benchmark script in the TensorFlow 2.x 20.06-py3
NGC container on NVIDIA DGX A100 (8x A100 40GB) GPUs. Performance numbers (in images per second) were averaged over 200 steps omitting the first 100 warm-up steps.
GPUs | Batch size / GPU | Throughput - TF32 [img/s] | Throughput - mixed precision [img/s] | Throughput speedup (TF32 - mixed precision) | Weak scaling - TF32 | Weak scaling - mixed precision |
---|---|---|---|---|---|---|
1 | 2 | 11.83822087 | 18.5130037 | 1.56 | - | - |
1 | 4 | 12.67925418 | 19.93965192 | 1.57 | - | - |
4 | 2 | 44.50704695 | 58.11168627 | 1.31 | 3.76 | 3.14 |
4 | 4 | 47.38663139 | 64.66523539 | 1.36 | 3.74 | 3.24 |
8 | 2 | 80.21134592 | 110.9716499 | 1.38 | 6.78 | 5.99 |
8 | 4 | 89.93247608 | 150.0217503 | 1.67 | 7.09 | 7.52 |
Our results were obtained by running python scripts/benchmark_training.py --gpus {1,4,8} --batch_size {2,4} [--amp]
benchmark script in the TensorFlow 2.x 20.06-py3
NGC container on NVIDIA DGX-1 V100 (8x V100 16GB) GPUs. Performance numbers (in images per second) were averaged over 200 steps omitting the first 100 warm-up steps.
GPUs | Batch size / GPU | Throughput - FP32 [img/s] | Throughput - mixed precision [img/s] | Throughput speedup (FP32 - mixed precision) | Weak scaling - TF32 | Weak scaling - mixed precision |
---|---|---|---|---|---|---|
1 | 2 | 5.70 | 11.63 | 2.04 | - | - |
1 | 4 | 6.20 | 12.63 | 2.04 | - | - |
4 | 2 | 21.22 | 25.18 | 1.19 | 3.72 | 2.16 |
4 | 4 | 21.79 | 30.63 | 1.41 | 3.51 | 2.42 |
8 | 2 | 38.64 | 52.13 | 1.35 | 6.78 | 4.48 |
8 | 4 | 40.76 | 59.62 | 1.46 | 6.57 | 4.72 |
To achieve these same results, follow the steps in the Quick Start Guide.
Our results were obtained by running python scripts/benchmark_inference.py --batch_size {2,4,8} [--amp]
benchmark script in the TensorFlow 1.1x 20.06-py3
NGC container on NVIDIA DGX A100 (1x A100 40GB) GPU.
FP16
Batch size | Throughput Avg [img/s] |
---|---|
2 | 28.37 |
4 | 31.35 |
8 | 33.79 |
TF32
Batch size | Throughput Avg [img/s] |
---|---|
2 | 21.81 |
4 | 23.77 |
8 | 24.59 |
To achieve these same results, follow the steps in the Quick Start Guide.
Our results were obtained by running python scripts/benchmark_inference.py --batch_size {2,4,8} [--amp]
benchmark script in the TensorFlow 1.1x 20.06-py3
NGC container on NVIDIA DGX-1 V100 (1x V100 16GB) GPU.
FP16
Batch size | Throughput Avg [img/s] |
---|---|
2 | 23.52 |
4 | 24.64 |
8 | 26.83 |
FP32
Batch size | Throughput Avg [img/s] |
---|---|
2 | 14.85 |
4 | 15.45 |
8 | 16.00 |
To achieve these same results, follow the steps in the Quick Start Guide.
Our results were obtained by running python scripts/benchmark_inference.py --batch_size {2,4,8} [--amp]
benchmark script in the TensorFlow 2.x 20.06-py3
NGC container on NVIDIA DGX A100 (1x A100 40GB) GPU.
FP16
Batch size | Throughput Avg [img/s] |
---|---|
2 | 26.28 |
4 | 36.23 |
8 | 40.84 |
TF32
Batch size | Throughput Avg [img/s] |
---|---|
2 | 20.20 |
4 | 24.94 |
8 | 31.38 |
To achieve these same results, follow the steps in the Quick Start Guide.
Our results were obtained by running python scripts/benchmark_inference.py --batch_size {2,4,8} [--amp]
benchmark script in the TensorFlow 2.x 20.06-py3
NGC container on NVIDIA DGX-1 V100 (1x V100 16GB) GPU.
FP16
Batch size | Throughput Avg [img/s] |
---|---|
2 | 23.63 |
4 | 27.64 |
8 | 33.60 |
FP32
Batch size | Throughput Avg [img/s] |
---|---|
2 | 15.45 |
4 | 16.71 |
8 | 18.78 |
To achieve these same results, follow the steps in the Quick Start Guide.
June 2020
-
Updated accuracy tables with A100 results
-
Updated training and inference performance tables with A100 results
March 2020
- Initial release
There are no known issues with this model.