Deploying to DeepStream for Multitask Classification

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 Deploy. The generated TensorRT engine file can also be ingested by DeepStream.

  • Option 3 (Deprecated for x86 devices): Generate a device-specific optimized TensorRT engine using TAO Converter.

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: TAO Deploy is used to convert the .etlt file to TensorRT; this file is then provided directly to DeepStream. The TAO Deploy workflow is similar to TAO Converter, which is deprecated for x86 devices from TAO version 4.0.x but is still required for deployment to Jetson devices.

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

There are two options to integrate TAO models with DeepStream:

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

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

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 .onnx 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 that is fully configurable. You can configure input sources, the 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:

arch_ref_appl.png

Typically, two or more configuration files 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 will set the 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 configuration files are used to specify the models, inference resolution, batch size, number of classes, and other customizations. The main configuration file will call all the supporting configuration files.

Here are some configuration files in samples/configs/deepstream-app for reference:

  • source4_1080p_dec_infer-resnet_tracker_sgie_tiled_display_int8.txt: The main configuration file

  • config_infer_primary.txt: The supporting configuration file for the primary detector in the pipeline above

  • config_infer_secondary_*.txt: The supporting configuration file for the 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 with little to no change. You will only need to modify or create config_infer_primary.txt and config_infer_secondary_*.txt.

Integrating a Multitask Image Classification Model

See Exporting The Model for more details on how to export a TAO model. fter the model has been generated, two extra files are required:

  1. A label file

  2. A DeepStream configuration file

Label File

The label file is a text file, containing the names of the classes that the TAO model is trained to classify against. The order in which the classes are listed must match the order in which the model predicts the output. This order may be deduced from the classmap.json file that is generated by TAO. This file is a simple dictionary containing the ‘class_name’ to ‘index map’. A sample classmap.json file generated for the model generated from the TAO Toolkit Tutorial notebook would be:

Copy
Copied!
            

{ "tasks": ["base_color", "category", "season"], "class_mapping": { "base_color": { "0": "Black", "1": "Blue", "2": "Brown", "3": "Green", "4": "Grey", "5": "Navy Blue", "6": "Pink", "7": "Purple", "8": "Red", "9": "Silver", "10": "White"}, "category": { "0": "Bags", "1": "Bottomwear", "2": "Eyewear", "3": "Fragrance", "4": "Innerwear", "5": "Jewellery", "6": "Sandal", "7": "Shoes", "8": "Topwear", "9": "Watches"}, "season": { "0": "Fall", "1": "Spring", "2": "Summer", "3": "Winter" } } }

Given this sample class_map.json` file, here is a sample multitask_classification_labels.txt file, arranged in order of index:

Copy
Copied!
            

Fall;Spring;Summer;Winter Bags;Bottomwear;Eyewear;Fragrance;Innerwear;Jewellery;Sandal;Shoes;Topwear;Watches Black;Blue;Brown;Green;Grey;Navy Blue;Pink;Purple;Red;Silver;White

DeepStream Configuration File

A typical use case for video analytic is first to do an object detection and then crop the detected object and send it further for classification or multi-task classification. This is supported by deepstream-app and the app architecture can be seen above. The pgie_multi_task_tao_config.txt is used to configure the classification model.

Option 1: Integrate the model (.onnx) directly in the DeepStream app. For this option, you will need to add the following parameters in the configuration file. The int8-calib-file is only required for INT8 precision.

Copy
Copied!
            

onnx-file=<TAO Toolkit exported .onnx> int8-calib-file=<Calibration cache file>

From TAO 5.0.0, .etlt is deprecated. To integrate .etlt directly in the DeepStream app, you need following parmaters in the configuration file.

Copy
Copied!
            

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

Option 2: Integrate the TensorRT engine file with the DeepStream app.

  1. Generate the device-specific TensorRT engine using TAO Deploy.

  2. After the engine file is generated, modify the following parameter 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. The net-scale-factor, offsets, and model-color-format parameters must be updated according to the preprocessing_mode in the training spec file:

  • preprocessing_mode: "caffe":

    Copy
    Copied!
                

    net-scale-factor=1.0 offsets=B;G;R model-color-format=1

    Where B;G;R shoud be replaced by the image_mean parameters. If image_mean is not set, the default ImageNet mean (103.939;116.779;123.68) will be used.

  • preprocessing_mode: "torch":

    Copy
    Copied!
                

    net-scale-factor=0.017507 offsets=123.675;116.280;103.53 model-color-format=0

  • preprocessing_mode: "tf":

    Copy
    Copied!
                

    net-scale-factor=0.0078 offsets=127.5;127.5;127.5 model-color-format=0

Add the label file generated above with the following:

Copy
Copied!
            

labelfile-path=<Multitask classification labels>

For all options, refer to the configuration file below. To learn more about all the parameters, 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=multi_task_labels.txt onnx-file=../../../models/multi_task/abc.onnx model-engine-file=../../../models/multi_task/abc.etlt_b1_gpu0_fp16.engine infer-dims=3;80;60 batch-size=1 ## 0=FP32, 1=INT8, 2=FP16 mode network-mode=2 interval=0 gie-unique-id=1 network-type=1 scaling-filter=1 scaling-compute-hw=1 output-blob-names=base_color/Softmax;category/Softmax;season/Softmax classifier-threshold=0.5

Previous Deploying to DeepStream for Classification TF1/TF2/PyTorch
Next Deploying to DeepStream for DetectNet_v2
© Copyright 2024, NVIDIA. Last updated on Mar 22, 2024.