Deploying to DeepStream for YOLOv3
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 has been designed to integrate with DeepStream SDK, so models trained with TAO 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 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.
As of 5.0.0, tao-converter
is deprecated. This method may not be
available in the future releases. This section is only applicable
if you’re still using tao-converter
for legacy. For tao deploy
,
please jump to Integrating YOLOv3 Model.
For YOLOv3, we need the batchTilePlugin
and batchedNMSPlugin
plugins from the
TensorRT OSS build.
If the deployment platform is x86 with an NVIDIA GPU, follow the TensorRT OSS on x86 instructions. On the other hand, if your deployment is on the NVIDIA Jetson platform, follow the TensorRT OSS on Jetson (ARM64) instructions.
TensorRT OSS on x86
Building TensorRT OSS on x86:
Install Cmake (>=3.13).
NoteTensorRT OSS requires cmake >= v3.13, so install cmake 3.13 if your cmake version is lower than 3.13c
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
Get GPU architecture. The
GPU_ARCHS
value can be retrieved by thedeviceQuery
CUDA sample: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 downloaddeviceQuery.cpp
from this GitHub repo. Compile and rundeviceQuery
.nvcc deviceQuery.cpp -o deviceQuery ./deviceQuery
This command will output something like this, which indicates the
GPU_ARCHS
is75
based on CUDA Capability major/minor version.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
Build TensorRT OSS:
git clone -b 21.08 https://github.com/nvidia/TensorRT cd TensorRT/ git submodule update --init --recursive export TRT_SOURCE=`pwd` cd $TRT_SOURCE mkdir -p build && cd build
NoteMake sure your
GPU_ARCHS
from step 2 is in TensorRT OSSCMakeLists.txt
. If GPU_ARCHS is not in TensorRT OSSCMakeLists.txt
, add-DGPU_ARCHS=<VER>
as below, where<VER>
representsGPU_ARCHS
from step 2./usr/local/bin/cmake .. -DGPU_ARCHS=xy -DTRT_LIB_DIR=/usr/lib/x86_64-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/.
Replace the original
libnvinfer_plugin.so*
:sudo mv /usr/lib/x86_64-linux-gnu/libnvinfer_plugin.so.8.x.y ${HOME}/libnvinfer_plugin.so.8.x.y.bak // backup original libnvinfer_plugin.so.x.y sudo cp $TRT_SOURCE/`pwd`/out/libnvinfer_plugin.so.8.m.n /usr/lib/x86_64-linux-gnu/libnvinfer_plugin.so.8.x.y sudo ldconfig
TensorRT OSS on Jetson (ARM64)
Install Cmake (>=3.13)
NoteTensorRT OSS requires cmake >= v3.13, while the default cmake on Jetson/Ubuntu 18.04 is cmake 3.10.2.
Upgrade TensorRT OSS using:
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
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 Build TensorRT OSS:
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
NoteThe
-DGPU_ARCHS=72
below is for Xavier or NX, for other Jetson platform, change72
referring toGPU_ARCHS
from step 2./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/.
Replace
"libnvinfer_plugin.so*"
with the newly generated.sudo mv /usr/lib/aarch64-linux-gnu/libnvinfer_plugin.so.8.x.y ${HOME}/libnvinfer_plugin.so.8.x.y.bak // backup original libnvinfer_plugin.so.x.y sudo cp `pwd`/out/libnvinfer_plugin.so.8.m.n /usr/lib/aarch64-linux-gnu/libnvinfer_plugin.so.8.x.y sudo ldconfig
To integrate a model trained by TAO with DeepStream, you should generate a device-specific optimized TensorRT engine using TAO Deploy. The generated TensorRT engine file can then be ingested by DeepStream.
For YOLOv3, you will need to build the TensorRT open source plugins and custom bounding-box parser. The instructions to build TensorRT open source plugins are provided in the TensorRT OSS section above. The instructions to build custom bounding-box parser is provided below in prerequisite section and the required code can be found in this GitHub repo.
To integrate the models with DeepStream, you will need the following:
The DeepStream SDK (download from the DeepStream SDK Download Page). The installation instructions for DeepStream are provided in the DeepStream Development Guide.
An exported
.onnx
model file and optional calibration cache for INT8 precision.A
labels.txt
file containing the labels for classes in the order in which the networks produce outputs.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, 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:
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 configuration 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 have to modify or create config_infer_primary.txt
and
config_infer_secondary_*.txt
.
Integrating a YOLOv3 Model
To run a YOLOv3 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 YOLOv3 bounding box parser for DeepStream.
A DeepStream sample with documentation on how to run inference using the trained YOLOv3 models from TAO is provided on GitHub repo.
Prerequisite for YOLOv3 Model
(Only required if you are using TAO Deploy <tao_deploy_overview>.) YOLOv3 requires the batchTilePlugin, resizeNearestPlugin and batchedNMSPlugin. These plugins are available in the TensorRT open source repo, but not in TensorRT 7.0. Detailed instructions to build TensorRT OSS can be found in the TensorRT Open Source Software (OSS) section.
YOLOv3 requires custom bounding box parsers that are not built in to the DeepStream SDK. The source code to build custom bounding box parsers for YOLOv3 is available in GitHub repo. The following instructions can be used to build the bounding-box parser:
Install git-lfs (git >= 1.8.2)
curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash sudo apt-get install git-lfs git lfs install
Download the source code with SSH or HTTPS:
git clone -b release/tlt3.0 https://github.com/NVIDIA-AI-IOT/deepstream_tlt_apps
Build the parser:
// 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
.
The label file is a text file containing the names of the classes that the YOLOv3 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 YOLOv3 will specify all class names in lower case and sort them in alphabetical order. For example, if the dataset_config is:
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 yolov3_labels.txt file would be:
bicycle
car
person
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.
Integrate the TensorRT engine file with the DeepStream app
Generate the TensorRT engine using TAO Deploy.
Once the engine file is generated successfully, modify the following parameters to use this engine with DeepStream:
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 the [property]
section of the primary infer configuration file:
parse-bbox-func-name=NvDsInferParseCustomBatchedNMSTLT
custom-lib-path=<PATH to libnvds_infercustomparser_tlt.so>
Add the label file generated above using the following:
labelfile-path=<YOLOv3 labels>
For all the options, see the configuration file below. To learn more about all the parameters, refer to the DeepStream Development Guide.
Here’s a sample config file, pgie_yolov3_config.txt:
[property]
gpu-id=0
net-scale-factor=1.0
offsets=103.939;116.779;123.68
model-color-format=1
labelfile-path=<Path to yolov3_labels.txt>
model-engine-file=<PATH to generated TensorRT engine>
infer-dims=3;384;1248
maintain-aspect-ratio=1
uff-input-order=0
uff-input-blob-name=Input
batch-size=1
## 0=FP32, 1=INT8, 2=FP16 mode
network-mode=0
num-detected-classes=3
interval=0
gie-unique-id=1
is-classifier=0
#network-type=0
#no cluster
cluster-mode=3
output-blob-names=BatchedNMS
parse-bbox-func-name=NvDsInferParseCustomBatchedNMSTLT
custom-lib-path=<Path to libnvds_infercustomparser_tlt.so>
[class-attrs-all]
pre-cluster-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
Integrate the ONNX model with the DeepStream app
The ONNX model can also be integrated into DeepStream directly. Here’s a sample config file, pgie_yolov3_config.txt:
[property]
gpu-id=0
net-scale-factor=1.0
offsets=103.939;116.779;123.68
model-color-format=1
labelfile-path=<Path to yolov3_labels.txt>
onnx-file=<Path to ONNX model>
maintain-aspect-ratio=1
batch-size=1
## 0=FP32, 1=INT8, 2=FP16 mode
network-mode=0
num-detected-classes=3
interval=0
gie-unique-id=1
is-classifier=0
#network-type=0
#no cluster
cluster-mode=3
parse-bbox-func-name=NvDsInferParseCustomBatchedNMSTLT
custom-lib-path=<Path to libnvds_infercustomparser_tlt.so>
[class-attrs-all]
pre-cluster-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