EfficientDet (TF2)

NVIDIA TAO Release tlt.40

With EfficientDet, 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 convention from command line:

Copy
Copied!
            

tao efficientdet_tf2 <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.

EfficientDet expects directories of images for training or validation and annotation JSON files in COCO format. See the Data Annotation Format page for more information about the data format for EfficientDet.

The raw image data and the corresponding annotation file need to be converted to TFRecords before training and evaluation. The dataset_convert tool helps to achieve seamless conversion while providing insight on potential issues in an annotation file. The following sections detail how to use dataset_convert.

Sample Usage of the Dataset Converter Tool

The dataset_convert tool is described below:

Copy
Copied!
            

tao efficientdet_tf2 dataset-convert [-h] -e <conversion spec file>

Below is a sample for the data conversion spec file. The format of the spec file is YAML with configuration parameters under the scopre of dataset_convert.

Copy
Copied!
            

dataset_convert: image_dir: '/workspace/tao-experiments/data/raw-data/train2017/' annotations_file: '/workspace/tao-experiments/data/raw-data/annotations/instances_train2017.json' output_dir: '/workspace/tao-experiments/data' tag: 'train' num_shards: 256 include_masks: True

The details of each parameter are summarized in the table below:

Field

Description

Data Type and Constraints

Recommended/Typical Value

image_dir

The path to the directory where raw images are stored

String

annotations_file

The path to the annotation JSON file

String

output_dir

The output directory where TFRecords are saved

String

tag

The number of shards for the converted TFRecords

Integer

256

num_shards

The path to a TAO pruned model for re-training, if any

String

include_mask

Whether to include segmentation groundtruth during conversion

Boolean

False

Note

A log file named <tag>_warnings.json will be generated in the output_dir if the bounding box of an object is out of bounds with respect to the image frame or if an object mask is out of bounds with respect to its bounding box. The log file records the image_id that has problematic object IDs. For example, {"200365": {"box": [918], "mask": []} means the bounding box of object 918 is out of bounds in image 200365.


The following example shows how to use the command:

Copy
Copied!
            

tao efficientdet_tf2 dataset_convert -i /path/to/convert.yaml


Below is a sample for the EfficientDet spec file. It has 8 major components: data, model, train, evaluate, inference, augment, prune and export config as well as mandatory parameters for the encryption key (key) and results directory (results_dir).

Copy
Copied!
            

data: loader: prefetch_size: 4 shuffle_file: False shuffle_buffer: 10000 cycle_length: 32 block_length: 16 max_instances_per_image: 100 skip_crowd_during_training: True image_size: '512x512' num_classes: 91 train_tfrecords: - '/datasets/coco/train-*' val_tfrecords: - '/datasets/coco/val-*' val_json_file: '/datasets/coco/annotations/instances_val2017.json' train: optimizer: name: 'sgd' momentum: 0.9 lr_schedule: name: 'cosine' warmup_epoch: 5 warmup_init: 0.0001 learning_rate: 0.2 amp: True checkpoint: "/weights/efficientnet-b0_500.eff" num_examples_per_epoch: 100 moving_average_decay: 0.999 batch_size: 20 checkpoint_interval: 5 l2_weight_decay: 0.00004 l1_weight_decay: 0.0 clip_gradients_norm: 10.0 image_preview: True qat: False random_seed: 42 pruned_model_path: '' num_epochs: 200 model: name: 'efficientdet-d0' aspect_ratios: '[(1.0, 1.0), (1.4, 0.7), (0.7, 1.4)]' anchor_scale: 4 min_level: 3 max_level: 7 num_scales: 3 freeze_bn: False freeze_blocks: [] augment: rand_hflip: True random_crop_min_scale: 0.1 random_crop_max_scale: 2 auto_color_distortion: False auto_translate_xy: False evaluate: batch_size: 8 num_samples: 5000 max_detections_per_image: 100 model_path: '' export: max_batch_size: 8 dynamic_batch_size: True min_score_thresh: 0.4 model_path: "" output_path: "" inference: model_path: "" image_dir: "" output_dir: "" dump_label: False batch_size: 1 prune: model_path: "" normalizer: 'max' output_path: "" equalization_criterion: 'union' granularity: 8 threshold: 0.5 min_num_filters: 16 excluded_layers: [] key: 'nvidia_tlt' results_dir: '/workspace/results_dir'

The format of the spec file is YAML. The top level structure of the spec file is summarized in the table below:

Field

Description

data

Configuration related to data sources and dataloader

model

Configuration related to model construction

augment

Configuration related to data augmentation during training

train

Configuration related to the training process

evaluate

Configuration related to the standalone evaluation process

prune

Configuration for pruning a trained model

inference

Configuration for running model inference

export

Configuration for exporting a trained model

key

Global encryption key

results_dir

Directory where experiment results and status logging are saved

Training Config

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

Field

Description

Data Type and Constraints

Recommended/Typical Value

batch_size

The batch size for each GPU, so the effective batch size is batch_size_per_gpu * num_gpus.

Unsigned int, positive

16

num_epochs

The number of epochs to train the network

Unsigned int, positive

300

num_examples_per _epoch

Total number of images in the training set

Unsigned int, positive

checkpoint

The path to the pretrained model, if any

String

pruned_model_path

The path to a TAO pruned model for re-training, if any

String

checkpoint_interval

The number of training epochs that should run per model checkpoint/validation

Unsigned int, positive

10

amp

Whether to use mixed precision training

Boolean

moving_average_decay

Moving average decay

Float

0.9999

l2_weight_decay

L2 weight decay

Float

l1_weight_decay

L1 weight decay

Float

random_seed

Random seed

Unsigned int, positive

42

clip_gradients_norm

Clip gradients by the norm value

Float

5

qat

Enabled quantization aware training

Boolean

False

optimizer

Optimizer configuration

lr_schedule

Learning rate scheduler configuration

The optimizer configuration(train.optimizer) specifies the type and parameters of an optimizer. +———————+——————————————————————————————————-+——————————-+————————————————————————————–+ | Field | Description | Data Type and Constraints | Recommended/Typical Value | +———————+——————————————————————————————————-+——————————-+————————————————————————————–+ | name | Optimizer name (only sgd is supported) | String | ‘sgd’ | +———————+——————————————————————————————————-+——————————-+————————————————————————————–+ | momentum | Momentum | float | 0.9 | +———————+——————————————————————————————————-+——————————-+————————————————————————————–+

The learning rate scheduler configuration(train.lr_schedule) specifies the type and parameters of a learning rate scheduler. +———————+——————————————————————————————————-+——————————-+————————————————————————————–+ | Field | Description | Data Type and Constraints | Recommended/Typical Value | +———————+——————————————————————————————————-+——————————-+————————————————————————————–+ | name | The name of the learning rate scheduler. Available options are cosine and soft_anneal | String | ‘cosine’ | +———————+——————————————————————————————————-+——————————-+————————————————————————————–+ | warmup_epoch | The number of warmup epochs in the learning rate schedule | Unsigned int, positive | – | +———————+——————————————————————————————————-+——————————-+————————————————————————————–+ | warmup_init | The initial learning rate in the warmup period | Float | – | +———————+——————————————————————————————————-+——————————-+————————————————————————————–+ | learning_rate | The maximum learning rate | Float | – | +———————+——————————————————————————————————-+——————————-+————————————————————————————–+

Evaluation Config

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

Field

Description

Data Type and Constraints

Recommended/Typical Value

model_path

The path to the model to be evaluated

String

max_detections_per_image

The maximum number of detections to visualize

Unsigned int, positive

100

batch_size

The batch size for each GPU, so the effective batch size is batch_size_per_gpu * num_gpus

Unsigned int, positive

16

num_samples

The number of samples for evaluation

Unsigned int

label_map

YAML file that stores index to label name mapping. (Optional) If set, per class AP metric will be calculated

String

start_eval_epoch

Evaluation will not start until this epoch (Default: 1)

Unsigned int

Inference Config

The inference configuration (inference) defines the parameters needed for the standalone inference with the trained .tlt model. Details are summarized in the table below.

Field

Description

Data Type and Constraints

Recommended/Typical Value

model_path

The path to the model to run inference with

String

image_dir

The path to the image directory

String

output_dir

The path to the output directory where annotated images will be saved

String

dump_label

Whether to dump label files in KITTI format

Boolean

batch_size

Batch size to run inference with

Unsigned int

min_score_thresh

Minimum confidence threshold to render the predicted bounding boxes

String

label_map

YAML file that stores index to label name mapping (Optional) If set, annotated images will have class labels associated with bounding boxes

String

max_boxes_to_draw

The maximum number of bounding boxes that will be rendered in the annotated images

String

Dataset Config

The data configuration (data) specifies the input data source and format. This is used for training, evaluation. A detailed description is summarized in the table below.

Field

Description

Data Type and Constraints

Recommended/Typical Value

image_size

The image dimension in “WxH” format, where W and H indicates the dimension of the resized and padded input.

String

“512x512”

train_tfrecords

The TFRecord path for training

String

val_tfrecords

The TFRecord path for validation

String

val_json_file

The annotation file path for validation

String

num_classes

The number of classes. If there are N categories in the annotation, num_classes should be N+1 (background class)

Unsigned int

max_instances_per_image

The maximum number of object instances to parse (default: 100)

Unsigned int

100

skip_crowd_during_training

Specifies whether to skip crowd during training

Boolean

True

loader

Data loader configuration

The dataloader configuration (data.loader) specifies how batches of data are fed into the model.

Field

Description

Data Type and Constraints

Recommended/Typical Value

prefetch_size

The image dimension in “WxH” format, where W and H indicates the dimension of the resized and padded input.

String

“512x512”

shuffle_file

The TFRecord path for training

String

shuffle_buffer

The image dimension in “WxH” format, where W and H indicates the dimension of the resized and padded input.

String

“512x512”

cycle_length

The TFRecord path for training

String

block_length

The TFRecord path for training

String

Model Config

The model configuration (model) specifies the model structure. A detailed description is summarized in the table below.

Field

Description

Data Type and Constraints

Recommended/Typical Value

model_name

EfficientDet model name

string

“efficientdet_d0”

min_level

The minimum level of the output feature pyramid

Unsigned int

3 (only 3 is supported)

max_level

The maximum level of the output feature pyramid

Unsigned int

7 (only 7 is supported)

num_scales

The number of anchor octave scales on each pyramid level (e.g. if set to 3, the anchor scales are [2^0, 2^(1/3), 2^(2/3)])

Unsigned int

3

max_instances_per_image

The maximum number of object instances to parse (default: 100)

Unsigned int

100

aspect_ratios

A list of tuples representing the aspect ratios of anchors on each pyramid level

string

“[(1.0, 1.0), (1.4, 0.7), (0.7, 1.4)]”

anchor_scale

Scale of the base-anchor size to the feature-pyramid stride

Unsigned int

4

Augmentation Config

The augment configuration specifies the image augmentation methods used after preprocessing.

Field

Description

Data Type and Constraints

Recommended/Typical Value

rand_hflip

Whether to perform random horizontal flip

Boolean

random_crop_min_scale

The minimum scale of RandomCrop augmentation. Default: 0.1

Float

0.1

random_crop_max_scale

The maximum scale of RandomCrop augmentation. Default: 2.0

Float

2.0

auto_color_distortion

Whether to enable automatic color augmentation

Boolean

False

auto_translate_xy

Whether to enable automatic image translation in X/Y axis

Boolean

False

Pruning Config

The prune configuration defines the pruning process for a trained model. A detailed description is summarized in the table below.

Field

Description

Data Type and Constraints

Recommended/Typical Value

normalizer

Normalization method. Specify max to normalize by dividing each norm by the maximum norm within a layer or L2 to normalize by dividing by the L2 norm of the vector comprising all kernel norms

String

max

equalization_criterion

The criteria to equalize the stats of inputs to an element-wise op layer or depth-wise conv layer. Options are arithmetic_mean geometric_mean,``union``, and intersection.

String

union

granularity

The number of filters to remove at a time

Integer

8

threshold

Pruning threshold

Float

min_num_filters

The minimum number of filters to keep per layer. Default: 16

Integer

16

excluded_layers

A list of layers to be excluded from pruning

List

model_path

The path to the .tlt model file to be pruned

String

Export Config

The export configuration contains the parameters of exporting a .tlt model to .etlt model, which can be used for deployment.

Field

Description

Data Type and Constraints

Recommended/Typical Value

max_batch_size

The maximum batch size of the .etlt model if dynamic_batch_size is set to False

Boolean

dynamic_batch_size

Whether to use dynamic batch size in the exported .etlt model

Boolean

True

model_path

The path to the .tlt model file to be exported

String

output_path

The path to save the exported .etlt model

String

False

Train the EfficientDet model using this command:

Copy
Copied!
            

tao efficientdet_tf2 train [-h] -e <experiment_spec> [--gpus <num_gpus>] [--gpu_index <gpu_index>] [--log_file <log_file_path>]

Required Arguments

  • -e, --experiment_spec: The experiment specification file to set up the training experiment.

Optional Arguments

  • --gpus: The number of GPUs to be used for training in a multi-GPU scenario. The default value is 1.

  • --gpu_index: The indices of the GPUs to use for training. This argument can be used when the machine has multiple GPUs installed.

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

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

Input Requirement

  • Input size: C * W * H (where C = 1 or 3, W >= 128, H >= 128; W, H are multiples of 32)

  • Image format: JPG

  • Label format: COCO detection

Sample Usage

Here’s an example of the train command:

Copy
Copied!
            

tao efficientdet_tf2 train --gpus 2 -e /path/to/spec.yaml


To run evaluation with an EfficientDet model, use this command:

Copy
Copied!
            

tao efficientdet_tf2 evaluate [-h] -e <experiment_spec> [--gpu_index <gpu_index>] [--log_file <log_file_path>]

Required Arguments

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

Optional Arguments

  • --gpu_index: The index of the GPU to use for evaluation. This argument can be used 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 value is stdout.

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

Sample Usage

Here’s an example of using the evaluate command:

Copy
Copied!
            

tao efficientdet_tf2 evaluate -e /path/to/spec.yaml


The inference tool for EfficientDet models can be used to visualize bboxes and generate frame-by- frame KITTI format labels on a directory of images.

Copy
Copied!
            

tao efficientdet_tf2 inference [-h] -e <experiment spec file> [--gpu_index <gpu_index>] [--log_file <log_file_path>]

Required Arguments

  • -e, --experiment_spec: The path to an experiment spec file

Optional Arguments

  • --gpu_index: The index of the GPU to run inference on. This argument can be used when the machine has multiple GPUs installed. Note that inference can only run on a single GPU.

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

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

Sample Usage

Here’s an example of using the inference command:

Copy
Copied!
            

tao efficientdet_tf2 inference -e /path/to/spec.yaml


Pruning removes parameters from the model to reduce the model size without compromising the integrity of the model itself using the tao efficientdet_tf2 prune command.

The tao efficientdet_tf2 prune command includes these parameters:

Copy
Copied!
            

tao efficientdet_tf2 prune [-h] -e <experiment spec file> [--gpu_index <gpu_index>] [--log_file <log_file_path>]

Required Arguments

  • -e, --experiment_spec: The path to an experiment spec file

Optional Arguments

  • --gpu_index: The index of the GPU to run pruning on. This argument can be used when the machine has multiple GPUs installed. Note that inference can only run on a single GPU.

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

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

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

Note

Due to the complexity of larger EfficientDet models, the pruning process will take significantly longer to finish. For example, pruning the EfficientDet-D5 model may take at least 25 minutes on a V100 server.


Using the Prune Command

Here’s an example of using the tao efficientdet_tf2 prune command:

Copy
Copied!
            

tao efficientdet_tf2 prune -e /path/to/spec.yaml


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

We recommend turning off the regularizer or reducing the weight decay in the training_config for EfficientDet to recover the accuracy when retraining a pruned model. To do this, set the regularizer type to NO_REG as mentioned in the Training config section. All the other parameters may be retained in the spec file from the previous training.

Exporting the model decouples the training process from deployment 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. The exported model may be used universally across training and deployment hardware. The exported model format is referred to as .etlt. The .etlt model format is also an encrypted model format, and it uses the same key as the .tlt model that it is exported from. This key is required when deploying a model.

Exporting the EfficientDet Model

Here’s an example of the command line arguments of the tao efficientdet_tf2 export command:

Copy
Copied!
            

tao efficientdet_tf2 export [-h] -e <path to experiment spec> [--gpu_index <gpu_index>] [--log_file <log_file_path>]

Required Arguments

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

Optional Arguments

  • --gpu_index: The index of (discrete) GPUs used for exporting the model. You can specify the index of the GPU to run export 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 value is stdout.

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

Sample usage

Here’s a sample command to export an EfficientDet model in INT8 mode.

Copy
Copied!
            

tao efficientdet_tf2 export -e /path/to/spec.yaml


For TensorRT engine generation, validation, and int8 calibration, refer to the TAO Deploy documentation.

Refer to the Integrating an EfficientDet (TF1/TF2) Model page to learn more about deploying an EfficientDet TF2 model to Deepsteram.

© Copyright 2022, NVIDIA.. Last updated on Mar 23, 2023.