# SSD

With SSD, the following tasks are supported:

• dataset_convert

• train

• evaluate

• prune

• inference

• export

These tasks may be invoked from the TAO Toolkit Launcher by following the below mentioned convention from command line:

Copy
Copied!

tao ssd <sub_task> <args_per_subtask>


where, args_per_subtask are the command line arguments required for a given subtask. Each of these sub-tasks are explained in detail below.

## Data Input for Object Detection

The object detection apps in TAO Toolkit expect data in KITTI format for training and evaluation.

## Pre-processing the Dataset

The ssd dataloader supports the raw KITTI formatted data as well as TFrecords.

To use TFRecords for optimized iteration across the data batches, the KITTI formatted data need to be converted to TFRecords format first. This can be done using the dataset_convert subtask.

The dataset_convert tool requires a configuration file as input. Details of the configuration file and examples are included in the following sections.

### Configuration File for Dataset Converter

The dataset_convert tool provides several configurable parameters. The parameters are encapsulated in a spec file to convert data from the KITTI format to the TFRecords format which the SSD trainer can ingest. This is a prototxt format file with 3 global parameters:

• kitti_config: A nested prototxt configuration with multiple input parameters

• image_directory_path: The path to the dataset root. The image_dir_name is appended to this path to get the input images and must be the same path specified in the experiment spec file.

• target_class_mapping: The prototxt dictionary that maps the class names in the tfrecords to the target class to be trained in the network.

Here are descriptions of the configurable parameters for the kitti_config field:

Parameter

Datatype

Default

Description

Supported Values

root_directory_path

string

The path to the dataset root directory

image_dir_name

string

The relative path to the directory containing images from the path in root_directory_path.

label_dir_name

string

The relative path to the directory containing labels from the path in root_directory_path.

partition_mode

string

The method employed when partitioning the data to multiple folds. Two methods are supported:

• Random partitioning: The data is divided in to 2 folds, train and val. This mode requires that the val_split parameter be set.

• Sequence-wise partitioning: The data is divided into n partitions (defined by the num_partitions parameter) based on the number of sequences available.

• random

• sequence

num_partitions

int

2 (if partition_mode is random)

The number of partitions to use to split the data (N folds). This field is ignored when the partition model is set to random, as by default only two partitions are generated: val and train. In sequence mode, the data is split into n-folds. The number of partitions is ideally fewer than the total number of sequences in the kitti_sequence_to_frames file.

n=2 for random partition n< number of sequences in the kitti_sequence_to_frames_file

image_extension

str

.png

The extension of the images in the image_dir_name parameter.

.png .jpg .jpeg

val_split

float

20

The percentage of data to be separated for validation. This only works under “random” partition mode. This partition is available in fold 0 of the TFrecords generated.

0 <= x < 100

kitti_sequence_to_frames_file

str

The name of the KITTI sequence to frame mapping file. This file must be present within the dataset root as mentioned in the root_directory_path.

num_shards

int

10

The number of shards per fold.

1-20

The sample configuration file shown below converts 100% dataset to be training dataset.

Copy
Copied!

kitti_config {
root_directory_path: "/workspace/tao-experiments/data/training"
image_dir_name: "image_2"
label_dir_name: "label_2"
image_extension: ".png"
partition_mode: "random"
num_partitions: 2
val_split: 0
num_shards: 10
}
image_directory_path: "/workspace/tao-experiments/data/training"
target_class_mapping {
key: "car"
value: "car"
}
target_class_mapping {
key: "pedestrian"
value: "pedestrian"
}
target_class_mapping {
key: "cyclist"
value: "cyclist"
}
target_class_mapping {
key: "van"
value: "car"
}
target_class_mapping {
key: "person_sitting"
value: "pedestrian"
}


### Sample Usage of the Dataset Converter Tool

The dataset_convert tool is described below:

Copy
Copied!

tao ssd dataset-convert [-h] -d DATASET_EXPORT_SPEC
-o OUTPUT_FILENAME
[-v]


You can use the following arguments:

• -h, --help: Show this help message and exit

• -d, --dataset-export-spec: The path to the detection dataset spec containing the config for exporting .tfrecord files

• -o, --output_filename: The output filename

• -v: Enable verbose mode to show debug messages

The following example shows how to use the command with the dataset:

Copy
Copied!

tao ssd dataset_convert -d /path/to/spec.txt
-o /path/to/tfrecords/train


## Creating a Configuration File

Below is a sample of the SSD spec file. It has six major components: ssd_config, training_config, eval_config, nms_config, augmentation_config, and dataset_config. The format of the spec file is a protobuf text(prototxt) message and each of its fields can be either a basic data type or a nested message.

Copy
Copied!

random_seed: 42
ssd_config {
aspect_ratios: "[[1.0, 2.0, 0.5],  [1.0, 2.0, 0.5, 3.0, 1.0/3.0],  [1.0, 2.0, 0.5, 3.0, 1.0/3.0],  [1.0, 2.0, 0.5, 3.0, 1.0/3.0],  [1.0, 2.0, 0.5],  [1.0, 2.0, 0.5]]"
scales: "[0.07, 0.15, 0.33, 0.51, 0.69, 0.87, 1.05]"
two_boxes_for_ar1: true
clip_boxes: false
variances: "[0.1, 0.1, 0.2, 0.2]"
arch: "resnet"
nlayers: 18
freeze_bn: false
freeze_blocks: 0
}
training_config {
batch_size_per_gpu: 16
num_epochs: 80
enable_qat: false
learning_rate {
soft_start_annealing_schedule {
min_learning_rate: 5e-5
max_learning_rate: 2e-2
soft_start: 0.15
annealing: 0.8
}
}
regularizer {
type: L1
weight: 3e-5
}
}
eval_config {
validation_period_during_training: 10
average_precision_mode: SAMPLE
batch_size: 16
matching_iou_threshold: 0.5
}
nms_config {
confidence_threshold: 0.01
clustering_iou_threshold: 0.6
top_k: 200
}
augmentation_config {
output_width: 300
output_height: 300
output_channel: 3
image_mean {
key: 'b'
value: 103.9
}
image_mean {
key: 'g'
value: 116.8
}
image_mean {
key: 'r'
value: 123.7
}
}
dataset_config {
data_sources: {
# option 1
tfrecords_path: "/path/to/train/tfrecord"

# option 2
# label_directory_path: "/path/to/train/labels"
# image_directory_path: "/path/to/train/images"
}
include_difficult_in_training: true
target_class_mapping {
key: "car"
value: "car"
}
target_class_mapping {
key: "pedestrian"
value: "pedestrian"
}
target_class_mapping {
key: "cyclist"
value: "cyclist"
}
target_class_mapping {
key: "van"
value: "car"
}
target_class_mapping {
key: "person_sitting"
value: "pedestrian"
}
validation_data_sources: {
label_directory_path: "/path/to/val/labels"
image_directory_path: "/path/to/val/images"
}
}


The top level structure of the spec file is summarized in the sections below.

### Training Config

The training configuration (training_config) defines the parameters needed for the training, evaluation, and inference. Details are summarized in the table below.

 Field Description Data Type and Constraints Recommended/Typical Value batch_size_per_gpu The batch size for each GPU, so the effective batch size is batch_size_per_gpu * num_gpus Unsigned int, positive – num_epochs The number of epochs to train the network Unsigned int, positive – enable_qat Whether to use quantization-aware training Boolean Note: SSD does not support loading a pruned non-QAT model and retraining it with QAT enabled, or vice versa. For example, to get a pruned QAT model, perform the initial training with QAT enabled or enable_qat=True. learning_rate Only soft_start_annealing_schedule with these nested parameters is supported. min_learning_rate: The minimum learning during the entire experiment max_learning_rate: The maximum learning during the entire experiment soft_start: Time to lapse before warm up ( expressed in percentage of progress between 0 and 1) annealing: Time to start annealing the learning rate Message type – regularizer This parameter configures the regularizer to be used while training and contains the following nested parameters. type: The type or regularizer to use. NVIDIA supports NO_REG, L1, and L2 weight: The floating point value for the regularizer weight Message type L1 Note: NVIDIA suggests using the L1 regularizer when training a network before pruning as L1 regularization helps make the network weights more prunable. max_queue_size The number of prefetch batches in data loading Unsigned int, positive – n_workers The number of workers for data loading (set to less than 4 when using tfrecords) Unsigned int, positive – use_multiprocessing Whether to use multiprocessing mode of keras sequence data loader Boolean –
Note

The learning rate is automatically scaled with the number of GPUs used during training, or the effective learning rate is learning_rate * n_gpu.

### Evaluation Config

The evaluation configuration (eval_config) defines the parameters needed for the evaluation either during training or as a standalone procedure. Details are summarized in the table below.

 Field Description Data Type and Constraints Recommended/Typical Value validation_period_during_training The number of training epochs per validation Unsigned int, positive 10 average_precision_mode The Average Precision (AP) calculation mode can be either SAMPLE or INTEGRATE. SAMPLE is used as VOC metrics for VOC 2009 or before. INTEGRATE is used for VOC 2010 or after. ENUM type ( SAMPLE or INTEGRATE) SAMPLE matching_iou_threshold The lowest IoU of the predicted box and ground truth box that can be considered a match. Boolean 0.5

### NMS Config

The NMS configuration (nms_config) defines the parameters needed for NMS postprocessing. The NMS configuration applies to the NMS layer of the model in training, validation, evaluation, inference, and export. Details are summarized in the table below.

 Field Description Data Type and Constraints Recommended/Typical Value confidence_threshold Boxes with a confidence score less than confidence_threshold are discarded before applying NMS. float 0.01 cluster_iou_threshold The IoU threshold below which boxes will go through the NMS process. float 0.6 top_k top_k boxes will be output after the NMS keras layer. If the number of valid boxes is less than k, the returned array will be padded with boxes whose confidence score is 0. Unsigned int 200 infer_nms_score_bits The number of bits to represent the score values in NMS plugin in TensorRT OSS. The valid range is integers in [1, 10]. Setting it to any other values will make it fall back to ordinary NMS. Currently this optimized NMS plugin is only avaible in FP16 but it should also be selected by INT8 data type as there is no INT8 NMS in TensorRT OSS and hence this fastest implementation in FP16 will be selected. If falling back to ordinary NMS, the actual data type when building the engine will decide the exact precision(FP16 or FP32) to run at. int. In the interval [1, 10]. 0

### Augmentation Config

The augmentation_config parameter defines the image size after preprocessing. The augmentation methods in the SSD paper will be performed during training, including random flip, zoom-in, zoom-out and color jittering. And the augmented images will be resized to the output shape defined in augmentation_config. In evaluation process, only the resize will be performed.

Note

The details of augmentation methods can be found in setcion 2.2 and 3.6 of the paper.

 Field Description Data Type and Constraints Recommended/Typical Value output_channel Output image channel of augmentation pipeline. integer – output_width The width of preprocessed images and the network input. integer, multiple of 32 – output_height The height of preprocessed images and the network input. integer, multiple of 32 – random_crop_min_scale Minimum patch scale of RandomCrop augmentation. Default:0.3 float >= 1.0 – random_crop_max_scale Maximum patch scale of RandomCrop augmentation. Default:1.0 float >= 1.0 – random_crop_min_ar Minimum aspect ratio of RandomCrop augmentation. Default:0.5 float > 0 – random_crop_max_ar Maximum aspect ratio of RandomCrop augmentation. Default:2.0 float > 0 – zoom_out_min_scale Minimum scale of ZoomOut augmentation. Default:1.0 float >= 1.0 – zoom_out_max_scale Maximum scale of ZoomOut augmentation. Default:4.0 float >= 1.0 – brightness Brightness delta in color jittering augmentation. Default:32 integer >= 0 – contrast Contrast delta factor in color jitter augmentation. Default:0.5 float of [0, 1) – saturation Saturation delta factor in color jitter augmentation. Default:0.5 float of [0, 1) – hue Hue delta in color jittering augmentation. Default:18 integer >= 0 – random_flip Probablity of performing random horizontal flip. Default:0.5 float of [0, 1) – image_mean A key/value pair to specify image mean values. If omitted, ImageNet mean will be used for image preprocessing. If set, depending on output_channel, either ‘r/g/b’ or ‘l’ key/value pair must be configured. dict –
Note

If set random_crop_min_scale = random_crop_max_scale = 1.0, RandomCrop augmentation will be disabled. Similarly, set zoom_out_min_scale = zoom_out_max_scale = 1, ZoomOut augmentation will be disabled. And all color jitter delta values are set to 0, color jittering augmentation will be disabled.

### Dataset Config

The dataset_config parameter defines the path to the training dataset, validation dataset, and target_class_mapping.

 Field Description Data Type and Constraints Recommended/Typical Value data_sources The path to the training dataset. When using tfrecord as dataset ingestion, set: tfrecords_path: The path to tfrecords When using raw KITTI labels and images, set: label_directory_path: The path to the label directory image_directory_path: The path to the image directory Message type include_difficult_in_training Specifies whether to include difficult objects in the label (the Pascal VOC difficult label or KITTI occluded objects) bool true validation_data_sources The path to the training dataset images and labels Message type target_class_mapping A mapping of classes in labels to the target classes Message type
Note

data_sources and validation_data_sources are both repeated fields. Multiple datasets can be added to sources.

### SSD config

The SSD configuration (ssd_config) defines the parameters needed for building the SSD model. Details are summarized in the table below.

 Field Description Data Type and Constraints Recommended/Typical Value aspect_ratios_global The anchor boxes of aspect ratios defined in aspect_ratios_global will be generated for each feature layer used for prediction. Note that either the aspect_ratios_global or aspect_ratios parameter is required; you don’t need to specify both. string “[1.0, 2.0, 0.5, 3.0, 0.33]” aspect_ratios The aspect ratio of anchor boxes for different SSD feature layers Note: Either the aspect_ratios_global or aspect_ratios parameter is required; you don’t need to specify both. string “[[1.0,2.0,0.5], [1.0,2.0,0.5], [1.0,2.0,0.5], [1.0,2.0,0.5], [1.0,2.0,0.5], [1.0, 2.0, 0.5, 3.0, 0.33]]” two_boxes_for_ar1 If this parameter is True, two boxes will be generated with an aspect ratio of 1. One with a scale for this layer and the other with a scale that is the geometric mean of the scale for this layer and the scale for the next layer. Boolean True clip_boxes If true, all corner anchor boxes will be truncated so they are fully inside the feature images. Boolean False scales A list of positive floats containing scaling factors per convolutional predictor layer. This list must be one element longer than the number of predictor layers so that, if two_boxes_for_ar1 is true, the second aspect-ratio 1.0 box for the last layer can have a proper scale. Except for the last element in this list, each positive float is the scaling factor for boxes in that layer. For example, if for one layer the scale is 0.1, then the generated anchor box with aspect ratio 1 for that layer (the first aspect-ratio 1 box if two_boxes_for_ar1 is set to True) will have its height and width as 0.1*min(img_h, img_w). min_scale and max_scale are two positive floats. If both of them appear in the config, the program can automatically generate the scales by evenly splitting the space between min_scale and max_scale. string “[0.05, 0.1, 0.25, 0.4, 0.55, 0.7, 0.85]” min_scale/max_scale variances If both appear in the config, scales will be generated evenly by splitting the space between min_scale and max_scale. A list of 4 positive floats. The four floats, in order, represent variances for box center x, box center y, log box height, and log box width. The box offset for box center (cx, cy) and log box size (height/width) w.r.t. anchor will be divided by their respective variance value. Therefore, larger variances result in less significant differences between two different boxes on encoded offsets. float steps An optional list inside quotation marks with a length that is the number of feature layers for prediction. The elements should be floats or tuples/lists of two floats. The steps define how many pixels apart the anchor-box center points should be. If the element is a float, both vertical and horizontal margin is the same. Otherwise, the first value is step_vertical and the second value is step_horizontal. If steps are not provided, anchor boxes will be distributed uniformly inside the image. string offsets An optional list of floats inside quotation marks with length equal to the number of feature layers for prediction. The first anchor box will have a margin of offsets[i]*steps[i] pixels from the left and top borders. If offsets are not provided, 0.5 will be used as default value. string arch The backbone for feature extraction. Currently, “resnet”, “vgg”, “darknet”, “googlenet”, “mobilenet_v1”, “mobilenet_v2” and “squeezenet” are supported. string resnet nlayers The number of conv layers in a specific arch. For “resnet”, 10, 18, 34, 50 and 101 are supported. For “vgg”, 16 and 19 are supported. For “darknet”, 19 and 53 are supported. All other networks don’t have this configuration, and users should delete this parameter from the config file. Unsigned int freeze_bn Whether to freeze all batch normalization layers during training. boolean False freeze_blocks The list of block IDs to be frozen in the model during training. You can choose to freeze some of the CNN blocks in the model to make the training more stable and/or easier to converge. The definition of a block is heuristic for a specific architecture. For example, by stride or by logical blocks in the model, etc. However, the block ID numbers identify the blocks in the model in a sequential order so you don’t have to know the exact locations of the blocks when you do training. As a general principle, the smaller the block ID, the closer it is to the model input; the larger the block ID, the closer it is to the model output. You can divide the whole model into several blocks and optionally freeze a subset of it. Note that for FasterRCNN, you can only freeze the blocks that are before the ROI pooling layer. Any layer after the ROI pooling layer will not be frozen anyway. For different backbones, the number of blocks and the block ID for each block are different. It deserves some detailed explanations on how to specify the block IDs for each backbone. list(repeated integers) ResNet series. For the ResNet series, the block IDs valid for freezing is any subset of [0, 1, 2, 3] (inclusive) VGG series. For the VGG series, the block IDs valid for freezing is any subset of[1, 2, 3, 4, 5] (inclusive) GoogLeNet. For the GoogLeNet, the block IDs valid for freezing is any subset of[0, 1, 2, 3, 4, 5, 6, 7] (inclusive) MobileNet V1. For the MobileNet V1, the block IDs valid for freezing is any subset of [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] (inclusive) MobileNet V2. For the MobileNet V2, the block IDs valid for freezing is any subset of [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] (inclusive) DarkNet. For the DarkNet 19 and DarkNet 53, the block IDs valid for freezing is any subset of [0, 1, 2, 3, 4, 5] (inclusive)

## Training the Model

Train the SSD model using this command:

Copy
Copied!

tao ssd train [-h] -e <experiment_spec>
-r <output_dir>
-k <key>
[--gpus <num_gpus>]
[--gpu_index <gpu_index>]
[--use_amp]
[--log_file <log_file>]
[-m <resume_model_path>]
[--initial_epoch <initial_epoch>]


### Required Arguments

• -r, --results_dir: Path to the folder where the experiment output is written.

• -k, --key: Provide the encryption key to decrypt the model.

• -e, --experiment_spec_file: Experiment specification file to set up the evaluation experiment. This should be the same as the training specification file.

### Optional Arguments

• --gpus num_gpus: Number of GPUs to use and processes to launch for training. The default = 1.

• --gpu_index: The GPU indices used to run the training. We can specify the GPU indices used to run training when the machine has multiple GPUs installed.

• --use_amp: A flag to enable AMP training.

• --log_file: The path to the log file. Defaults to stdout.

• -m, --resume_model_weights: Path to a pre-trained model or model to continue training.

• --initial_epoch: Epoch number to resume from.

• --use_multiprocessing: Enable multiprocessing mode in data generator.

• -h, --help: Show this help message and exit.

### Input Requirement

• Input size: C * W * H (where C = 1 or 3, W >= 128, H >= 128)

• Image format: JPG, JPEG, PNG

• Label format: KITTI detection

### Sample Usage

Here’s an example of using the train command on an SSD model:

Copy
Copied!

tao ssd train --gpus 2 -e /path/to/spec.txt -r /path/to/result -k $KEY  ## Evaluating the Model Use the following command to run evaluation for an SSD model: Copy Copied!  tao ssd evaluate [-h] -m <model> -e <experiment_spec_file> [-k <key>] [--gpu_index <gpu_index>] [--log_file <log_file>]  ### Required Arguments • -m, --model: The .tlt model or TensorRT engine to be evaluated. • -e, --experiment_spec_file: The experiment spec file to set up the evaluation experiment. This should be the same as the training spec file. ### Optional Arguments • -h, --help: Show this help message and exit. • -k, --key：The encoding key for the .tlt model • --gpu_index: The index of the GPU to run evaluation (useful when the machine has multiple GPUs installed). Note that evaluation can only run on a single GPU. • --log_file: The path to the log file. The default path is stdout. Here is a sample command to evaluate a SSD model: Copy Copied!  tao ssd evaluate -m /path/to/trained_tlt_ssd_model -k <model_key> -e /path/to/ssd_spec.txt  ## Running Inference on the Model The inference command for SSD networks can be used to visualize bboxes or generate frame-by-frame KITTI format labels on a directory of images. Here’s an example of using this tool: Copy Copied!  tao ssd inference [-h] -i <input directory> -o <output annotated image directory> -e <experiment spec file> -m <model file> -k <key> [-l <output label directory>] [-t <bbox filter threshold>] [--gpu_index <gpu_index>] [--log_file <log_file>]  ### Required Arguments • -m, --model: The path to the pretrained model (TAO model). • -i, --in_image_dir: The directory of input images for inference. • -o, --out_image_dir: The directory path to output annotated images. • -k, --key: The key to the load model. • -e, --config_path: The path to an experiment spec file for training. ### Optional Arguments • -t, --threshold: The threshold for drawing a bbox and dumping a label file. (default: 0.3). • -h, --help: Show this help message and exit. • -l, --out_label_dir: The directory to output KITTI labels to. • --gpu_index: The index of the GPU to run inference (useful when the machine has multiple GPUs installed). Note that evaluation can only run on a single GPU. • --log_file: The path to the log file. The default path is stdout. Here is a sample of using inference with the SSD model: Copy Copied!  tao ssd inference -i /path/to/input/images_dir -o /path/to/output/dir -m /path/to/trained_tlt_ssd_model -k <model_key> -e /path/to/ssd_spec.txt  ## Pruning the Model Pruning removes parameters from the model to reduce the model size without compromising the integrity of the model itself. The prune command includes these parameters: Copy Copied!  tao ssd prune [-h] -m <pretrained_model> -o <output_file> -k <key> [-n <normalizer>] [-eq <equalization_criterion>] [-pg <pruning_granularity>] [-pth <pruning threshold>] [-nf <min_num_filters>] [-el [<excluded_list>] [--gpu_index <gpu_index>] [--log_file <log_file>]  ### Required Arguments • -m, --pretrained_model: The path to the pretrained model. • -o, --output_file: The path to output checkpoints to. • -k, --key: The key to load a .tlt model. ### Optional Arguments • -h, --help: Show this help message and exit. • -n, normalizer: max to normalize by dividing each norm by the maximum norm within a layer; L2 to normalize by dividing by the L2 norm of the vector comprising all kernel norms. (default: max) • -eq, --equalization_criterion: Criteria to equalize the stats of inputs to an element wise op layer, or depth-wise convolutional layer. This parameter is useful for resnets and mobilenets. Options are arithmetic_mean, geometric_mean, union, and intersection. (default: union) • -pg, -pruning_granularity: Number of filters to remove at a time. (default:8) • -pth: Threshold to compare normalized norm against. (default:0.1) • -nf, --min_num_filters: Minimum number of filters to keep per layer (default:16) • -el, --excluded_layers: List of excluded_layers. Examples: -i item1 item2 (default: []) • --gpu_index: The index of the GPU to run pruning (useful when the machine has multiple GPUs installed). Note that evaluation can only run on a single GPU. • --log_file: The path to the log file. Defaults to stdout. Here’s an example of using the prune command: Copy Copied!  tao ssd prune -m /workspace/output/weights/resnet_003.tlt \ -o /workspace/output/weights/resnet_003_pruned.tlt \ -eq union \ -pth 0.7 -k$KEY


After pruning, the model needs to be retrained. See Re-training the Pruned Model for more details.

## Re-training the Pruned Model

Once the model has been pruned, there might be a slight decrease in accuracy. This happens because some previously useful weights may have been removed. To regain accuracy, NVIDIA recommends that you retrain this pruned model over the same dataset. To do this, use the tao ssd train command with an updated spec file that points to the newly pruned model as the pretrained model file.

Users are advised to turn off the regularizer in the training_config for SSD to recover the accuracy when retraining a pruned model. You may do this by setting the regularizer type to NO_REG, as mentioned here. All the other parameters may be retained in the spec file from the previous training.

Note

SSD does not support loading a pruned non-QAT model and retraining it with QAT enabled, or vice versa. For example, to get a pruned QAT model, perform the initial training with QAT enabled or enable_qat=True.

## Exporting the Model

The TAO Toolkit includes the export command to export and prepare TAO models for Deploying to DeepStream. The export command optionally generates the calibration cache for TensorRT INT8 engine calibration.

Exporting the model decouples the training process from inference and allows conversion to TensorRT engines outside the TAO environment. TensorRT engines are specific to each hardware configuration and should be generated for each unique inference environment. This may be interchangeably referred to as the .trt or .engine file. The same exported TAO model may be used universally across training and deployment hardware. This is referred to as the .etlt file or encrypted TAO file. During model export, the TAO model is encrypted with a private key. This key is required when you deploy this model for inference.

### INT8 Mode Overview

TensorRT engines can be generated in INT8 mode to improve performance, but require a calibration cache at engine creation-time. The calibration cache is generated using a calibration tensor file, if export is run with the --data_type flag set to int8. Pre-generating the calibration information and caching it removes the need for calibrating the model on the inference machine. Moving the calibration cache is usually much more convenient than moving the calibration tensorfile, since it is a much smaller file and can be moved with the exported model. Using the calibration cache also speeds up engine creation as building the cache can take several minutes to generate depending on the size of the Tensorfile and the model itself.

The export tool can generate an INT8 calibration cache by ingesting training data using the following method:

• Pointing the tool to a directory of images that you want to use to calibrate the model. For this option, make sure to create a sub-sampled directory of random images that best represent your training dataset.

### FP16/FP32 Model

The calibration.bin is only required if you need to run inference at INT8 precision. For FP16/FP32-based inference, the export step is much simpler: all you need to do is provide a model from the train step to export to convert it into an encrypted TAO model.

### Exporting command

Use the following command to export an SSD model

Copy
Copied!

tao ssd export  [-h] -m <path to the .tlt model file generated by tao train>
-k <key>
-e <path to experiment spec file>
[-o <path to output file>]
[--cal_data_file <path to tensor file>]
[--cal_image_dir <path to the directory images to calibrate the model]
[--cal_cache_file <path to output calibration file>]
[--data_type <Data type for the TensorRT backend during export>]
[--batches <Number of batches to calibrate over>]
[--max_batch_size <maximum trt batch size>]
[--max_workspace_size <maximum workspace size]
[--batch_size <batch size to TensorRT engine>]
[--engine_file <path to the TensorRT engine file>]
[--strict_type_constraints]
[--force_ptq]
[--gen_ds_config]
[--gpu_index <gpu_index>]
[--log_file <log_file_path>]
[--verbose]


#### Required Arguments

• -m, --model: The path to the .tlt model file to be exported using export.

• -k, --key: The key used to save the .tlt model file.

• -e, --experiment_spec: The path to the spec file.

#### Optional Arguments

• -o, --output_file: The path to save the exported model to. The default path is ./<input_file>.etlt.

• --data_type: The desired engine data type. The options are “fp32”, “fp16”, “int8”. The default value is “fp32”. If using int8, the following INT8 arguments are required.

• -s, --strict_type_constraints: A Boolean flag to indicate whether or not to apply the TensorRT strict_type_constraints when building the TensorRT engine. Note this is only for applying the strict type of INT8 mode.

• --gen_ds_config: A Boolean flag indicating whether to generate the template DeepStream related configuration (“nvinfer_config.txt”) as well as a label file (“labels.txt”) in the same directory as the output_file. Note that the config file is NOT a complete configuration file and requires the user to update the sample config files in DeepStream with the parameters generated.

• --gpu_index: The index of (descrete) GPUs used for exporting the model. We can specify the GPU index to run export if the machine has multiple GPUs installed. Note that export can only run on a single GPU.

• --log_file: Path to the log file. Defaults to stdout.

### INT8 Export Mode Required Arguments

• --cal_data_file: The tensorfile generated from tlt-int8-tensorfile for calibrating the engine. This can also be an output file if used with --cal_image_dir.

• --cal_image_dir: The directory of images to use for calibration.

Note

The --cal_image_dir parameter applies the necessary preprocessing to generate a tensorfile at the path mentioned in the --cal_data_file parameter, which is in turn used for calibration. The number of generated batches in the tensorfile is obtained from the value set to the --batches parameter, and the batch_size is obtained from the value set to the --batch_size parameter. Ensure that the directory mentioned in --cal_image_dir has at least batch_size * batches number of images in it. The valid image extensions are .jpg, .jpeg, and .png. In this case, the input_dimensions of the calibration tensors are derived from the input layer of the .tlt model.

### INT8 Export Optional Arguments

• --cal_cache_file: The path to save the calibration cache file to. The default value is ./cal.bin.

• --batches: The number of batches to use for calibration and inference testing. The default value is 10.

• --batch_size: The batch size to use for calibration. The default value is 8.

• --max_batch_size: The maximum batch size of the TensorRT engine. The default value is 16.

• --max_workspace_size: The maximum workspace size of the TensorRT engine. The default value is 1073741824 = 1<<30.

• --engine_file: The path to the serialized TensorRT engine file. Note that this file is hardware specific and cannot be generalized across GPUs. Use this argument to quickly test your model accuracy using TensorRT on the host. As the TensorRT engine file is hardware specific, you cannot use this engine file for deployment unless the deployment GPU is identical to the training GPU.

• --force_ptq: A Boolean flag to force post-training quantization on the exported .etlt model. * --gpu_index: The index of the (descrete) GPU for exporting the model if the machine has multiple GPUs installed. Note that export can only run on a single GPU.

• --log_file: The path to the log file. The default path is stdout.

Note

When exporting a model that was trained with QAT enabled, the tensor scale factors to calibrate the activations are peeled out of the model and serialized to a TensorRT-readable cache file defined by the cal_cache_file argument. However, the current version of QAT doesn’t natively support DLA int8 deployment on Jetson. To deploy this model on Jetson with DLA int8, use the --force_ptq flag to use TensorRT post-training quantization to generate the calibration cache file.

### Exporting a Model

Here’s a sample command using the --cal_image_dir option.

Copy
Copied!

tao ssd export -m $USER_EXPERIMENT_DIR/data/ssd/ssd_kitti_retrain_epoch12.tlt \ -o$USER_EXPERIMENT_DIR/data/ssd/ssd_kitti_retrain.int8.etlt \
-e $SPECS_DIR/ssd_kitti_retrain_spec.txt \ --key$KEY \
--cal_image_dir  $USER_EXPERIMENT_DIR/data/KITTI/val/image_2 \ --data_type int8 \ --batch_size 8 \ --batches 10 \ --cal_data_file$USER_EXPERIMENT_DIR/data/ssd/cal.tensorfile \
--cal_cache_file $USER_EXPERIMENT_DIR/data/ssd/cal.bin \ --engine_file$USER_EXPERIMENT_DIR/data/ssd/detection.trt


## Deploying to DeepStream

The deep learning and computer vision models that you’ve trained can be deployed on edge devices, such as a Jetson Xavier or Jetson Nano, a discrete GPU, or in the cloud with NVIDIA GPUs. TAO Toolkit has been designed to integrate with DeepStream SDK, so models trained with TAO Toolkit will work out of the box with DeepStream SDK.

DeepStream SDK is a streaming analytic toolkit to accelerate building AI-based video analytic applications. This section will describe how to deploy your trained model to DeepStream SDK.

To deploy a model trained by TAO Toolkit to DeepStream we have two options:

• Option 1: Integrate the .etlt model directly in the DeepStream app. The model file is generated by export.

• Option 2: Generate a device specific optimized TensorRT engine using tao-converter. The generated TensorRT engine file can also be ingested by DeepStream.

Machine-specific optimizations are done as part of the engine creation process, so a distinct engine should be generated for each environment and hardware configuration. If the TensorRT or CUDA libraries of the inference environment are updated (including minor version updates), or if a new model is generated, new engines need to be generated. Running an engine that was generated with a different version of TensorRT and CUDA is not supported and will cause unknown behavior that affects inference speed, accuracy, and stability, or it may fail to run altogether.

Option 1 is very straightforward. The .etlt file and calibration cache are directly used by DeepStream. DeepStream will automatically generate the TensorRT engine file and then run inference. TensorRT engine generation can take some time depending on size of the model and type of hardware. Engine generation can be done ahead of time with Option 2. With option 2, the tao-converter is used to convert the .etlt file to TensorRT; this file is then provided directly to DeepStream.

See the Exporting the Model section for more details on how to export a TAO model.

### TensorRT Open Source Software (OSS)

TensorRT OSS build is required for SSD models. This is required because several TensorRT plugins that are required by these models are only available in TensorRT open source repo and not in the general TensorRT release. Specifically, for SSD, we need the batchTilePlugin and NMSPlugin.

If the deployment platform is x86 with NVIDIA GPU, follow instructions for x86; if your deployment is on NVIDIA Jetson platform, follow instructions for Jetson.

#### TensorRT OSS on x86

Building TensorRT OSS on x86:

1. Install Cmake (>=3.13).

Note

TensorRT OSS requires cmake >= v3.13, so install cmake 3.13 if your cmake version is lower than 3.13c

Copy
Copied!

sudo apt remove --purge --auto-remove cmake
tar xvf cmake-3.13.5.tar.gz
cd cmake-3.13.5/
./configure
make -j$(nproc) sudo make install sudo ln -s /usr/local/bin/cmake /usr/bin/cmake  2. Get GPU architecture. The GPU_ARCHS value can be retrieved by the deviceQuery CUDA sample: Copy Copied!  cd /usr/local/cuda/samples/1_Utilities/deviceQuery sudo make ./deviceQuery  If the /usr/local/cuda/samples doesn’t exist in your system, you could download deviceQuery.cpp from this GitHub repo. Compile and run deviceQuery. Copy Copied!  nvcc deviceQuery.cpp -o deviceQuery ./deviceQuery  This command will output something like this, which indicates the GPU_ARCHS is 75 based on CUDA Capability major/minor version. Copy Copied!  Detected 2 CUDA Capable device(s) Device 0: "Tesla T4" CUDA Driver Version / Runtime Version 10.2 / 10.2 CUDA Capability Major/Minor version number: 7.5  3. Build TensorRT OSS: Copy Copied!  git clone -b 21.03 https://github.com/nvidia/TensorRT cd TensorRT/ git submodule update --init --recursive export TRT_SOURCE=pwd cd$TRT_SOURCE
mkdir -p build && cd build


Note

Make sure your GPU_ARCHS from step 2 is in TensorRT OSS CMakeLists.txt. If GPU_ARCHS is not in TensorRT OSS CMakeLists.txt, add -DGPU_ARCHS=<VER> as below, where <VER> represents GPU_ARCHS from step 2.

Copy
Copied!

/usr/local/bin/cmake .. -DGPU_ARCHS=xy  -DTRT_LIB_DIR=/usr/lib/aarch64-linux-gnu/ -DCMAKE_C_COMPILER=/usr/bin/gcc -DTRT_BIN_DIR=pwd/out
make nvinfer_plugin -j$(nproc)  After building ends successfully, libnvinfer_plugin.so* will be generated under \pwd\/out/. 4. Replace the original libnvinfer_plugin.so*: Copy Copied!  sudo mv /usr/lib/x86_64-linux-gnu/libnvinfer_plugin.so.7.x.y${HOME}/libnvinfer_plugin.so.7.x.y.bak   // backup original libnvinfer_plugin.so.x.y
sudo cp $TRT_SOURCE/pwd/out/libnvinfer_plugin.so.7.m.n /usr/lib/x86_64-linux-gnu/libnvinfer_plugin.so.7.x.y sudo ldconfig  #### TensorRT OSS on Jetson (ARM64) 1. Install Cmake (>=3.13) Note TensorRT OSS requires cmake >= v3.13, while the default cmake on Jetson/Ubuntu 18.04 is cmake 3.10.2. Upgrade TensorRT OSS using: Copy Copied!  sudo apt remove --purge --auto-remove cmake wget https://github.com/Kitware/CMake/releases/download/v3.13.5/cmake-3.13.5.tar.gz tar xvf cmake-3.13.5.tar.gz cd cmake-3.13.5/ ./configure make -j$(nproc)
sudo make install
sudo ln -s /usr/local/bin/cmake /usr/bin/cmake


2. Get GPU architecture based on your platform. The GPU_ARCHS for different Jetson platform are given in the following table.

 Jetson Platform GPU_ARCHS Nano/Tx1 53 Tx2 62 AGX Xavier/Xavier NX 72
3. Build TensorRT OSS:

Copy
Copied!

git clone -b 21.03 https://github.com/nvidia/TensorRT
cd TensorRT/
git submodule update --init --recursive
export TRT_SOURCE=pwd
cd $TRT_SOURCE mkdir -p build && cd build  Note The -DGPU_ARCHS=72 below is for Xavier or NX, for other Jetson platform, change 72 referring to GPU_ARCHS from step 2. Copy Copied!  /usr/local/bin/cmake .. -DGPU_ARCHS=72 -DTRT_LIB_DIR=/usr/lib/aarch64-linux-gnu/ -DCMAKE_C_COMPILER=/usr/bin/gcc -DTRT_BIN_DIR=pwd/out make nvinfer_plugin -j$(nproc)


After building ends successfully, libnvinfer_plugin.so* will be generated under ‘pwd’/out/.

4. Replace "libnvinfer_plugin.so*" with the newly generated.

Copy
Copied!

$export TRT_INC_PATH=”/usr/include/x86_64-linux-gnu”  1. Run the tao-converter using the sample command below and generate the engine. 2. Instructions to build TensorRT OSS on Jetson can be found in the TensorRT OSS on x86 section above or in this GitHub repo. Note Make sure to follow the output node names as mentioned in Exporting the Model section of the respective model. #### Instructions for Jetson For the Jetson platform, the tao-converter is available to download in the NVIDIA developer zone. You may choose the version you wish to download as listed in the overview section. Once the tao-converter is downloaded, please follow the instructions below to generate a TensorRT engine. 1. Unzip the zip file on the target machine. 2. Install the OpenSSL package using the command: Copy Copied!  sudo apt-get install libssl-dev  3. Export the following environment variables: Copy Copied!  $ export TRT_LIB_PATH=”/usr/lib/aarch64-linux-gnu”
$export TRT_INC_PATH=”/usr/include/aarch64-linux-gnu”  1. For Jetson devices, TensorRT 7.1 comes pre-installed with Jetpack. If you are using older JetPack, upgrade to JetPack 4.4 or JetPack 4.5. 2. Instructions to build TensorRT OSS on Jetson can be found in the TensorRT OSS on Jetson (ARM64) section above or in this GitHub repo. 3. Run the tao-converter using the sample command below and generate the engine. Note Make sure to follow the output node names as mentioned in Exporting the Model section of the respective model. #### Using the tao-converter Copy Copied!  tao-converter [-h] -k <encryption_key> -d <input_dimensions> -o <comma separated output nodes> [-c <path to calibration cache file>] [-e <path to output engine>] [-b <calibration batch size>] [-m <maximum batch size of the TRT engine>] [-t <engine datatype>] [-w <maximum workspace size of the TRT Engine>] [-i <input dimension ordering>] [-p <optimization_profiles>] [-s] [-u <DLA_core>] input_file  ##### Required Arguments • input_file: Path to the .etlt model exported using tao ssd export. • -k: The key used to encode the .tlt model when doing the training. • -d: Comma-separated list of input dimensions that should match the dimensions used for tao ssd export. • -o: Comma-separated list of output blob names that should match the output configuration used for tao ssd export. For SSD, set this argument to NMS. ##### Optional Arguments • -e: Path to save the engine to. (default: ./saved.engine) • -t: Desired engine data type, generates calibration cache if in INT8 mode. The default value is fp32. The options are {fp32, fp16, int8}. • -w: Maximum workspace size for the TensorRT engine. The default value is 1073741824(1<<30). • -i: Input dimension ordering, all other TAO commands use NCHW. The default value is nchw. The options are {nchw, nhwc, nc}. For SSD, we can omit it(defaults to nchw). • -p: Optimization profiles for .etlt models with dynamic shape. Comma separated list of optimization profile shapes in the format <input_name>,<min_shape>,<opt_shape>,<max_shape>, where each shape has the format: <n>x<c>x<h>x<w>. Can be specified multiple times if there are multiple input tensors for the model. This is only useful for new models introduced in TAO Toolkit 3.21.08. This parameter is not required for models that are already existed in version 2.0. • -s: TensorRT strict type constraints. A Boolean to apply TensorRT strict type constraints when building the TensorRT engine. • -u: Use DLA core. Specifying DLA core index when building the TensorRT engine on Jetson devices. ##### INT8 Mode Arguments • -c: Path to calibration cache file, only used in INT8 mode. The default value is ./cal.bin. • -b: Batch size used during the export step for INT8 calibration cache generation. (default: 8). • -m: Maximum batch size for TensorRT engine.(default: 16). If meet with out-of-memory issue, decrease the batch size accordingly. This parameter is not required for .etlt models generated with dynamic shape. (This is only possible for new models introduced in TAO Toolkit 3.21.08.) ##### Sample Output Log Here is a sample log for exporting a SSD model. Copy Copied!  tao-converter -k$KEY  \
-d 3,384,1248 \
-o NMS \
-e /export/trt.fp16.engine \
-t fp16 \
-i nchw \
-m 1 \
/ws/ssd_resnet18_epoch_100.etlt
..
[INFO] Some tactics do not have sufficient workspace memory to run. Increasing workspace size may increase performance, please check verbose output.
[INFO] Detected 1 inputs and 2 output network tensors.


### Integrating the model to DeepStream

There are 2 options to integrate models from TAO with DeepStream:

• Option 1: Integrate the model (.etlt) with the encrypted key directly in the DeepStream app. The model file is generated by tao ssd export.

• Option 2: Generate a device specific optimized TensorRT engine, using tao-converter. The TensorRT engine file can also be ingested by DeepStream.

For SSD, we will need to build TensorRT Open source plugins and custom bounding box parser. The instructions are provided below in the TensorRT OSS section above and the required code can be found in this GitHub repo.

In order to integrate the models with DeepStream, you need the following:

1. Download and install DeepStream SDK. The installation instructions for DeepStream are provided in the DeepStream Development Guide.

2. An exported .etlt model file and optional calibration cache for INT8 precision.

3. A labels.txt file containing the labels for classes in the order in which the networks produces outputs.

4. A sample config_infer_*.txt file to configure the nvinfer element in DeepStream. The nvinfer element handles everything related to TensorRT optimization and engine creation in DeepStream.

DeepStream SDK ships with an end-to-end reference application which is fully configurable. Users can configure input sources, inference model and output sinks. The app requires a primary object detection model, followed by an optional secondary classification model. The reference application is installed as deepstream-app. The graphic below shows the architecture of the reference application.

There are typically 2 or more configuration files that are used with this app. In the install directory, the config files are located in samples/configs/deepstream-app or sample/configs/tlt_pretrained_models. The main config file configures all the high level parameters in the pipeline above. This would set input source and resolution, number of inferences, tracker and output sinks. The other supporting config files are for each individual inference engine. The inference specific config files are used to specify models, inference resolution, batch size, number of classes and other customization. The main config file will call all the supporting config files. Here are some config files in samples/configs/deepstream-app for your reference.

• source4_1080p_dec_infer-resnet_tracker_sgie_tiled_display_int8.txt: Main config file

• config_infer_primary.txt: Supporting config file for primary detector in the pipeline above

• config_infer_secondary_*.txt: Supporting config file for secondary classifier in the pipeline above

The deepstream-app will only work with the main config file. This file will most likely remain the same for all models and can be used directly from the DeepStream SDK will little to no change. User will only have to modify or create config_infer_primary.txt and config_infer_secondary_*.txt.

#### Integrating an SSD Model

To run a SSD model in DeepStream, you need a label file and a DeepStream configuration file. In addition, you need to compile the TensorRT 7+ Open source software and SSD bounding box parser for DeepStream.

A DeepStream sample with documentation on how to run inference using the trained SSD models from TAO Toolkit is provided on GitHub here.

##### Prerequisite for SSD Model
1. SSD requires batchTilePlugin and NMS_TRT. This plugin is available in the TensorRT open source repo, but not in TensorRT 7.0. Detailed instructions to build TensorRT OSS can be found in TensorRT Open Source Software (OSS).

2. SSD requires custom bounding box parsers that are not built-in inside the DeepStream SDK. The source code to build custom bounding box parsers for SSD is available here. The following instructions can be used to build bounding box parser:

Step1: Install git-lfs (git >= 1.8.2)

Copy
Copied!

curl -s https://packagecloud.io/install/repositories/github/git-lfs/
script.deb.sh | sudo bash
sudo apt-get install git-lfs
git lfs install


Copy
Copied!

git clone -b release/tlt3.0 https://github.com/NVIDIA-AI-IOT/deepstream_tlt_apps


Step 3: Build

Copy
Copied!

// or Path for DS installation
export CUDA_VER=10.2         // CUDA version, e.g. 10.2
make


This generates libnvds_infercustomparser_tlt.so in the directory post_processor.

### Label File

The label file is a text file containing the names of the classes that the SSD model is trained to detect. The order in which the classes are listed here must match the order in which the model predicts the output. During the training, TAO SSD will specify all class names in lower case and sort them in alphabetical order. For example, if the dataset_config is:

Copy
Copied!

dataset_config {
data_sources: {
label_directory_path: "/workspace/tao-experiments/data/training/label_2"
image_directory_path: "/workspace/tao-experiments/data/training/image_2"
}
target_class_mapping {
key: "car"
value: "car"
}
target_class_mapping {
key: "person"
value: "person"
}
target_class_mapping {
key: "bicycle"
value: "bicycle"
}
validation_data_sources: {
label_directory_path: "/workspace/tao-experiments/data/val/label"
image_directory_path: "/workspace/tao-experiments/data/val/image"
}
}


Then the corresponding ssd_labels.txt file would be:

Copy
Copied!

background
bicycle
car
person


### DeepStream Configuration File

The detection model is typically used as a primary inference engine. It can also be used as a secondary inference engine. To run this model in the sample deepstream-app, you must modify the existing config_infer_primary.txt file to point to this model.

Option 1: Integrate the model (.etlt) directly in the DeepStream app.

For this option, users will need to add the following parameters in the configuration file. The int8-calib-file is only required for INT8 precision.

Copy
Copied!

tlt-encoded-model=<TLT exported .etlt>
tlt-model-key=<Model export key>
int8-calib-file=<Calibration cache file>


The tlt-encoded-model parameter points to the exported model (.etlt) from TLT. The tlt-model-key is the encryption key used during model export.

Option 2: Integrate TensorRT engine file with DeepStream app.

Step 1: Generate TensorRT engine using tao-converter. Detailed instructions are provided in the Generating an engine using tao-converter section above.

Step 2: Once the engine file is generated successfully, modify the following parameters to use this engine with DeepStream.

Copy
Copied!

model-engine-file=<PATH to generated TensorRT engine>


All other parameters are common between the two approaches. To use the custom bounding box parser instead of the default parsers in DeepStream, modify the following parameters in [property] section of primary infer configuration file:

Copy
Copied!

parse-bbox-func-name=NvDsInferParseCustomNMSTLT
custom-lib-path=<PATH to libnvds_infercustomparser_tlt.so>


Add the label file generated above using:

Copy
Copied!

labelfile-path=<ssd labels>


For all the options, see the sample configuration file below. To learn about what all the parameters are used for, refer to the DeepStream Development Guide.

Copy
Copied!

[property]
gpu-id=0
net-scale-factor=1.0
offsets=103.939;116.779;123.68
model-color-format=1
labelfile-path=<Path to ssd_labels.txt>
tlt-encoded-model=<Path to SSD etlt model>
tlt-model-key=<Key to decrypt model>
infer-dims=3;384;1248
uff-input-order=0
maintain-aspect-ratio=1
uff-input-blob-name=Input
batch-size=1
## 0=FP32, 1=INT8, 2=FP16 mode
network-mode=0
num-detected-classes=4
interval=0
gie-unique-id=1
is-classifier=0
#network-type=0
output-blob-names=NMS
parse-bbox-func-name=NvDsInferParseCustomNMSTLT
custom-lib-path=<Path to libnvds_infercustomparser_tlt.so>

[class-attrs-all]
threshold=0.3
roi-top-offset=0
roi-bottom-offset=0
detected-min-w=0
detected-min-h=0
detected-max-w=0
detected-max-h=0