DeepStream 中的 Gst-nvdspostprocess#

Gst-nvdspostprocess 插件在 DeepStream 6.1 中发布。该插件支持解析 DeepStream SDK 中的各种推理模型。该插件可以对 Gst-nvinfer 和 Gst-nvinferserver 提供的输出层的张量执行解析。本文档旨在提供关于如何使用 Gst-nvdspostprocess 插件处理各种推理模型的指南。

本文档提供有关以下内容的详细信息:本文档分为四个部分。

检测器模型#

要使用 Yolo V3 检测器,请按照 /opt/nvidia/deepstream/deepstream/sources/objectDetector_Yolo/README 中提到的先决条件步骤进行操作。

  1. 通过在以下文件夹 /opt/nvidia/deepstream/deepstream/sources/objectDetector_Yolo/ 中运行以下测试管道,检查设置是否配置正确。

#For dGPU
gst-launch-1.0 filesrc location=/opt/nvidia/deepstream/deepstream/samples/streams/sample_1080p_h264.mp4 ! decodebin !  \
m.sink_0 nvstreammux name=m batch-size=1 width=1920  height=1080 ! nvinfer config-file-path=config_infer_primary_yoloV3.txt ! \
nvvideoconvert ! nvdsosd ! nveglglessink sync=0

#For Jetson
gst-launch-1.0 filesrc location=/opt/nvidia/deepstream/deepstream/samples/streams/sample_1080p_h264.mp4 ! decodebin !  \
m.sink_0 nvstreammux name=m batch-size=1 width=1920  height=1080 ! nvinfer config-file-path=config_infer_primary_yoloV3.txt ! \
nvvideoconvert ! nvdsosd ! nv3dsink sync=0
  1. 要更新上述管道以使用后处理插件进行解析,必须修改 /opt/nvidia/deepstream/deepstream/sources/objectDetector_Yolo/config_infer_primary_yoloV3.txt 文件,方法是

  1. network-type=0 更改为 network-type=100。这样做会在 nvinfer 插件中禁用输出后处理。

  2. 设置 output-tensor-meta=1,然后 nvinfer 插件会将张量元数据附加到输入缓冲区。

  1. 将修改后的文件另存为 config_infer_primary_yoloV3_modified.txt。必须按如下方式创建 YAML 格式的后处理插件配置文件。

property:
 gpu-id: 0 #Set the GPU id
 process-mode: 1 # Set the mode as primary inference
 num-detected-classes: 80 # Change according the models output
 gie-unique-id: 1  # This should match the one set in inference config
 ## 1=DBSCAN, 2=NMS, 3= DBSCAN+NMS Hybrid, 4 = None(No clustering)
 cluster-mode: 2  # Set  appropriate clustering algorithm
 network-type: 0  # Set the network type as detector
 labelfile-path: labels.txt # Set the path of labels wrt to this config file
 parse-bbox-func-name: NvDsPostProcessParseCustomYoloV3 # Set custom parsing function

class-attrs-all: # Set as done in the original infer configuration
 nms-iou-threshold: 0.5
 pre-cluster-threshold: 0.7
  1. 将上述配置另存为 config_detector.yml。可以按如下方式执行以下管道。

#For dGPU
gst-launch-1.0 filesrc location=/opt/nvidia/deepstream/deepstream/samples/streams/sample_1080p_h264.mp4 ! decodebin ! \
sink_0 nvstreammux name=m batch-size=1 width=1920  height=1080 ! nvinfer config-file-path=config_infer_primary_yoloV3_modified.txt ! \
nvdspostprocess postprocesslib-config-file=config_detector.yml \
postprocesslib-name=/opt/nvidia/deepstream/deepstream/lib/libpostprocess_impl.so ! nvvideoconvert ! nvdsosd ! nveglglessink sync=0

#For Jetson
gst-launch-1.0 filesrc location=/opt/nvidia/deepstream/deepstream/samples/streams/sample_1080p_h264.mp4 ! decodebin ! \
sink_0 nvstreammux name=m batch-size=1 width=1920  height=1080 ! nvinfer config-file-path=config_infer_primary_yoloV3_modified.txt ! \
nvdspostprocess postprocesslib-config-file=config_detector.yml \
postprocesslib-name=/opt/nvidia/deepstream/deepstream/lib/libpostprocess_impl.so ! nvvideoconvert ! nvdsosd ! \
nv3dsink sync=0

注意

NvDsPostProcessParseCustomYoloV3 函数在 /opt/nvidia/deepstream/deepstream/sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/post_processor_custom_impl.cpp 中定义

可以按照与上述类似的步骤来演示 Faster RCNN 网络 (/opt/nvidia/deepstream/deepstream/sources/objectDetector_FasterRCNN/README) 与 nvdspostprocess 插件的用法,并使用以下 config_detector.yml

property:
  gpu-id: 0 #Set the GPU id
  process-mode: 1 # Set the mode as primary inference
  num-detected-classes: 21 # Change according the models output
  gie-unique-id: 1  # This should match the one set in inference config
  ## 1=DBSCAN, 2=NMS, 3= DBSCAN+NMS Hybrid, 4 = None(No clustering)
  cluster-mode: 2  # Set  appropriate clustering algorithm
  network-type: 0  # Set the network type as detector
  labelfile-path: labels.txt # Set the path of labels wrt to this config file
  parse-bbox-func-name: NvDsPostProcessParseCustomFasterRCNN # Set custom parsing function FRCNN

class-attrs-all: # Set as done in the original infer configuration
  topk: 20
  nms-iou-threshold: 0.4
  pre-cluster-threshold: 0.5

class-attrs-0:
  pre-cluster-threshold: 1.1

下面给出了使用修改后的 nvinfer 配置和后处理插件运行 Faster RCNN 网络的管道。

#For dGPU
gst-launch-1.0 filesrc location=/opt/nvidia/deepstream/deepstream/samples/streams/sample_1080p_h264.mp4 ! decodebin !  \
m.sink_0 nvstreammux name=m batch-size=1 width=1920  height=1080 ! nvinfer config-file-path=config_infer_primary_fasterRCNN_modified.txt ! \
nvdspostprocess postprocesslib-config-file=config_detector.yml postprocesslib-name=/opt/nvidia/deepstream/deepstream/lib/libpostprocess_impl.so ! \
nvvideoconvert ! nvdsosd ! nveglglessink sync=0

#For Jetson
gst-launch-1.0 filesrc location=/opt/nvidia/deepstream/deepstream/samples/streams/sample_1080p_h264.mp4 ! decodebin !  \
m.sink_0 nvstreammux name=m batch-size=1 width=1920  height=1080 ! nvinfer config-file-path=config_infer_primary_fasterRCNN_modified.txt ! \
nvdspostprocess postprocesslib-config-file=config_detector.yml postprocesslib-name=/opt/nvidia/deepstream/deepstream/lib/libpostprocess_impl.so ! \
nvvideoconvert ! nvdsosd ! nv3dsink sync=0

主要分类模型#

主要分类模型的演示使用了 dGPU 上的 DeepStream Triton Docker 容器。Docker 运行后,应创建模型仓库和分类视频。

注意

  • 下面提到的脚本 prepare_classification_test_video.sh 需要安装 ffmpeg。一些底层编解码器库需要与 ffmpeg 一起重新安装。

  • 使用以下命令安装/重新安装 ffmpeg:apt-get install --reinstall libflac8 libmp3lame0 libxvidcore4 ffmpeg

  1. 执行以下命令以下载模型仓库并创建示例分类视频。

cd /opt/nvidia/deepstream/deepstream/samples
./prepare_ds_triton_model_repo.sh
apt-get install --reinstall libflac8 libmp3lame0 libxvidcore4 ffmpeg
./prepare_classification_test_video.sh
cd /opt/nvidia/deepstream/deepstream/samples/configs/deepstream-app-triton
  1. 通过运行以下示例分类管道进行检查

gst-launch-1.0 filesrc location=/opt/nvidia/deepstream/deepstream/samples/streams/classification_test_video.mp4  ! decodebin ! \
m.sink_0 nvstreammux name=m batch-size=1 width=1920  height=1080 ! \
nvinferserver config-file-path=config_infer_primary_classifier_densenet_onnx.txt  \
! nvvideoconvert ! nvdsosd ! nveglglessink sync=1

注意

要在 Docker 内部使用 nveglglessink,请确保从主机完成 xhost +,并在 Docker 内部设置适当的 DISPLAY 环境变量。

  1. 现在,更新 config_infer_primary_classifier_densenet_onnx.txt 以禁用后处理并附加 nvinferserver 中的张量输出元数据。可以通过使用以下参数更新配置文件来完成此操作:infer_config { postprocess { other {} } }output_control { output_tensor_meta : true }

infer_config {
 unique_id: 5
 gpu_ids: [0]
 max_batch_size: 1
 backend {
   triton {
     model_name: "densenet_onnx"
     version: -1
     model_repo {
       root: "../../triton_model_repo"
       strict_model_config: true
       tf_gpu_memory_fraction: 0.0
       tf_disable_soft_placement: 0
     }
   }
 }
 preprocess {
   network_format: IMAGE_FORMAT_RGB
   tensor_order: TENSOR_ORDER_LINEAR
   maintain_aspect_ratio: 0
   frame_scaling_hw: FRAME_SCALING_HW_DEFAULT
   frame_scaling_filter: 1
   normalize {
   scale_factor: 0.0078125
   channel_offsets: [128, 128, 128]
   }
 }
 #Disable post processing in nvinferserver
 postprocess {
   other {
   }
 }
 extra {
   copy_input_to_host_buffers: false
   output_buffer_pool_size: 2
 }
}
input_control {
 process_mode: PROCESS_MODE_FULL_FRAME
 interval: 0
}
#Enable attaching output tensor meta in nvinferserver
output_control {
 output_tensor_meta: true
}
  1. 将上述配置另存为 config_infer_primary_classifier_densenet_onnx_modified.txt。创建一个 config_classifier.yml,如下所示。

property:
 gpu-id: 0
 network-type: 1 # Type of network i.e. classifier
 process-mode: 1 # Operate in primary mode i.e. operate on full frame
 classifier-threshold: 0.2 #Set classifier threshold
 gie-unique-id: 5 # Set the unique_id matching one in the inference
 classifier-type: ObjectClassifier # type of classifier
 labelfile-path: /opt/nvidia/deepstream/deepstream/samples/triton_model_repo/densenet_onnx/densenet_labels.txt #Path of the labels fine
  1. 现在可以执行以下带有 nvdspostprocess 插件的管道来查看分类结果

gst-launch-1.0 filesrc location=/opt/nvidia/deepstream/deepstream/samples/streams/classification_test_video.mp4  ! decodebin ! \
m.sink_0 nvstreammux name=m batch-size=1 width=1920  height=1080 !   nvinferserver \
config-file-path=config_infer_primary_classifier_densenet_onnx_modified.txt ! \
nvdspostprocess postprocesslib-config-file= config_classifier.yml postprocesslib-name= \
/opt/nvidia/deepstream/deepstream/lib/libpostprocess_impl.so ! nvvideoconvert ! nvdsosd ! nveglglessink sync=1

Mask RCNN 模型#

要使用实例分割模型,请按照软件包 /opt/nvidia/deepstream/deepstream/samples/configs/tao_pretrained_models/README.md 中的 README 获取 TAO 工具包配置文件和 PeopleSegNet 模型。

  1. 完成设置后,执行以下管道以验证模型。

cd /opt/nvidia/deepstream/deepstream/samples/configs/tao_pretrained_models
gst-launch-1.0 filesrc location=/opt/nvidia/deepstream/deepstream/samples/streams/sample_1080p_h264.mp4 ! decodebin ! \
m.sink_0 nvstreammux name=m batch-size=1 width=1920 height=1080 ! nvinfer config-file-path= config_infer_primary_peopleSegNet.txt  ! \
nvvideoconvert ! nvdsosd display-mask=1 process-mode=0 ! nveglglessink sync=0

注意

为了正确操作,请确保按照 TAO README 中所述编译并替换 Tensor-RT OSS 插件。

  1. 如前几节所述,更新 nvinfer 配置文件以禁用后处理并启用附加张量输出元数据。这可以通过更改 network-type=100output-tensor-meta=1 来完成。

  2. 将文件另存为名称 config_infer_primary_peopleSegNet_modified.txtconfig_mrcnn.yml 可以按如下方式创建。

property:
 gpu-id: 0
 process-mode: 1 # Process on full frame
 num-detected-classes: 2 #Total Detected classes
 gie-unique-id: 1  #Match with gie-unique-id of inference config
 ## 1=DBSCAN, 2=NMS, 3= DBSCAN+NMS Hybrid, 4 = None(No clustering)
 cluster-mode: 4 # Disable clustering
 network-type: 3 # Network is instance segmentation
 labelfile-path: peopleSegNet_labels.txt
 parse-bbox-instance-mask-func-name: NvDsInferParseCustomMrcnnTLTV2

class-attrs-all:
 pre-cluster-threshold: 0.8
  1. 以下管道可用于测试带有 MRCNN 网络的 nvdspostprocess 插件,使用上述配置文件。

gst-launch-1.0 filesrc location=/opt/nvidia/deepstream/deepstream/samples/streams/sample_1080p_h264.mp4 ! decodebin !   \
m.sink_0 nvstreammux name=m batch-size=1 width=1920 height=1080 ! \
nvinfer config-file-path= config_infer_primary_peopleSegNet.txt ! \
nvdspostprocess postprocesslib-name= /opt/nvidia/deepstream/deepstream/lib/libpostprocess_impl.so \
postprocesslib-config-file= config_mrcnn.yml  !   nvvideoconvert ! nvdsosd display-mask=1 process-mode=0 ! nveglglessink sync=0

自定义解析函数#

本节介绍后处理库中用于可用网络架构的解析函数。

支持的自定义解析函数#

自定义解析函数

描述

NvDsPostProcessParseCustomResnet

解析 DeepStream 中打包的 Resnet 10 模型

NvDsPostProcessParseCustomTfSSD

Tensorflow/Onnx SSD 检测器

NvDsPostProcessParseCustomNMSTLT

解析 TAO 工具包开放架构模型 SSD、FRCNN、DSSD、RetinaNet

NvDsPostProcessParseCustomBatchedNMSTLT

解析 TAO 工具包开放架构模型 Yolo V3、Yolo V4

NvDsPostProcessParseCustomMrcnnTLTV2

解析 TAO 工具包开放架构模型 MaskRCNN

NvDsPostProcessParseCustomFasterRCNN

解析 Faster R-CNN 网络

NvDsPostProcessClassiferParseCustomSoftmax

解析 DeepStream 中打包的 Resnet 18 车辆类型分类器模型

NvDsPostProcessParseCustomSSD

解析 SSD 网络

NvDsPostProcessParseCustomYoloV3

解析 Yolo V3 网络

NvDsPostProcessParseCustomYoloV3Tiny

解析 Yolo V3 Tiny 网络

NvDsPostProcessParseCustomYoloV2

解析 Yolo V2 网络

NvDsPostProcessParseCustomYoloV2Tiny

解析 Yolo V2 Tiny 网络