DeepStreamとTriton Inference Serverを用いて物体検出を行う

DeepStreamはどんな物体検出モデルでも動作できますか?

TensorRTに変換できるものでないと難しいぞ

じゃあ、ちょっと使いづらいですね

Triton Inference Serverと統合されたので、TensorRTに変換できないケースはこれを使えば動作できるぞ

DeepStreamは動画データに対して前処理からDeep Learningによる動画解析までを高速に行えるアプリケーションです。

DeepStreamで扱えるのはTensorRTエンジンに変換可能もしくは変換済みのモデルになります。変換できないケースは対応できません。

Triton inference Serverの機能が加わったのでTensorRTエンジンに変換できないモデルでも扱うことができます。

本記事では

本記事は下記記事を参考にして作成しています。

環境構築

下記リンクにあるDockerコンテナを使用します。

https://ngc.nvidia.com/catalog/containers/nvidia:deepstream

下記コマンドでコンテナを取得します。

docker pull nvcr.io/nvidia/deepstream:5.1-21.02-triton

Dockerを動作します。

xhost +
docker run --gpus all -it --rm -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY -w /opt/nvidia/deepstream/deepstream-5.1 nvcr.io/nvidia/deepstream:5.1-21.02-triton

Triton Inference Serverの設定

モデルの取得

下記コマンドでモデルを取得します。

wget http://download.tensorflow.org/models/object_detection/faster_rcnn_inception_v2_coco_2018_01_28.tar.gz
tar xvf faster_rcnn_inception_v2_coco_2018_01_28.tar.gz
cd samples/trtis_model_repo/
mkdir faster_rcnn_inception_v2 && cd faster_rcnn_inception_v2 && mkdir 1
cp ../../../faster_rcnn_inception_v2_coco_2018_01_28/frozen_inference_graph.pb 1/model.graphdef

ラベルファイルを取得します。このラベルファイルはCOCOデータ・セットのラベルファイルになります。

wget https://raw.githubusercontent.com/NVIDIA-AI-IOT/deepstream_triton_model_deploy/master/faster_rcnn_inception_v2/config/labels.txt

Triton Inference Serverのモデル設定ファイルを作成

Triton Inference Serverは様々なプラットフォームのモデルをサポートしています。

こちらのリンクにサポートしているプラットフォームが記述されていますが、一例としてPyTorch、ONNX、TensorFlowなど幅広くサポートしています。

設定にはInputのノード名とOutputのノード名と各入出力のサイズの情報が必要です。ONNXの場合はNETRONを使用すれば把握することができます。TensorFlowのモデルの場合はTensorBoardを使用すれば把握できます。

instance_groupは一つのGPUで同時に動作するモデル数を設定できます。今回は1GPUに対して1つのモデルを設定しています。

モデルの同時実行に関してはこちらをご覧ください。

Triton Inference Serverのモデル設定ファイルの詳細な説明はこちらになります。

name: "faster_rcnn_inception_v2"
platform: "tensorflow_graphdef"
max_batch_size: 1
input [
  {
    name: "image_tensor"
    data_type: TYPE_UINT8
    format: FORMAT_NHWC
    dims: [ 600, 1024, 3 ]
  }
]
output [
  {
    name: "detection_boxes"
    data_type: TYPE_FP32
    dims: [ 100, 4]
    reshape { shape: [100,4] }
  },
  {
    name: "detection_classes"
    data_type: TYPE_FP32
    dims: [ 100 ]
  },
  {
    name: "detection_scores"
    data_type: TYPE_FP32
    dims: [ 100 ]
  },
  {
    name: "num_detections"
    data_type: TYPE_FP32
    dims: [ 1 ]
    reshape { shape: [] }
  }
]
# Specify GPU instance.
instance_group {
  kind: KIND_GPU
  count: 1
  gpus: 0
}

下記のようなファイル構成になります。2021年9月1日時点でのDocker内のファイルパスになります。

Triton Inference Server

/opt/nvidia/deepstream/deepstream-5.1/samples/trtis_model_repo/faster_rcnn_inception_v2/
├── 1
│   └── model.graphdef
├── config.pbtxt
└── labels.txt

DeepStreamの設定

DeepStreamの設定ファイルを作成します。

下記ディレクトリに移動します。

cd /opt/nvidia/deepstream/deepstream-5.1//samples/configs/deepstream-app-trtis/

source1_primary_faster_rcnn_inception_v2.txtを作成します。

下記リンクのファイルの内容をコピーします。

https://github.com/NVIDIA-AI-IOT/deepstream_triton_model_deploy/blob/master/faster_rcnn_inception_v2/config/source1_primary_faster_rcnn_inception_v2.txt

このファイルはDeepStreamを動作するための設定をしているファイルです。

batch-sizeが4になっているのでbatch-sizeを1に設定します。Triton Inference Server側とサイズを合わせるための設定になります。

DeepStreamの詳細な設定はこちらをご覧ください。

DeepStreamの内部でTriton Inference Serverの設定ファイルを呼んでいます。

[streammux]
gpu-id=0
batch-size=1
:
:
[primary-gie]
enable=1
#(0): nvinfer; (1): nvinferserver
plugin-type=1
#infer-raw-output-dir=trtis-output
batch-size=1
interval=0
gie-unique-id=1
config-file=config_infer_primary_faster_rcnn_inception_v2.txt

先程はTriton Inference Serverで使用するモデルの設定ファイルを作成しました。今回はTriton Inference Serverの設定ファイルを作成します。

  • preprocess入力フォーマットと解像度をモデルが処理できる形に変更します。
  • infer-config—バッチサイズと各種ファイルのパスを設定します。
  • postprocess—推論結果の後処理でパースするための設定しています。バウンディングボックスのパースのため、NvDsInferParseCustomTfSSDを使用しています。パースするための共有ライブラリを設定します。この共有ライブラリは後ほどビルドします。
infer_config {
  unique_id: 1
  gpu_ids: [0]
  max_batch_size: 1
  backend {
    trt_is {
      model_name: "faster_rcnn_inception_v2"
      version: -1
      model_repo {
        root: "../../trtis_model_repo"
        log_level: 2
        tf_gpu_memory_fraction: 0
        tf_disable_soft_placement: 0
      }
    }
  }
 
  preprocess {
    network_format: IMAGE_FORMAT_RGB
    tensor_order: TENSOR_ORDER_NONE
    maintain_aspect_ratio: 0
    frame_scaling_hw: FRAME_SCALING_HW_DEFAULT
    frame_scaling_filter: 1
    normalize {
      scale_factor: 1.0
      channel_offsets: [0, 0, 0]
    }
  }
 
  postprocess {
    labelfile_path: "../../trtis_model_repo/faster_rcnn_inception_v2/labels.txt"
    detection {
      num_detected_classes: 91
      custom_parse_bbox_func: "NvDsInferParseCustomTfSSD"
      nms {
        confidence_threshold: 0.3
        iou_threshold: 0.6
        topk : 100
      }
    }
  }
 
  extra {
    copy_input_to_host_buffers: false
  }
 
  custom_lib {
    path: "/opt/nvidia/deepstream/deepstream-5.0/lib/libnvds_infercustomparser.so"
  }
}
input_control {
  process_mode: PROCESS_MODE_FULL_FRAME
  interval: 0
}
 
output_control {
  detect_control {
    default_filter { bbox_filter { min_width: 32, min_height: 32 } }
  }
}

先程作成されたconfigファイルを下記のように設定します。

/opt/nvidia/deepstream/deepstream-5.1/samples/configs/deepstream-app-trtis/
├── config_infer_primary_faster_rcnn_inception_v2.txt
├── source1_primary_faster_rcnn_inception_v2.txt

共有ライブラリのビルド

バウンディングボックスをパースするための共有ライブラリをビルドします。

cd /opt/nvidia/deepstream/deepstream-5.1/sources/libs/nvdsinfer_customparser
make all

下記のように共有ライブラリが作成されていることが確認できます。

/opt/nvidia/deepstream/deepstream-5.1/sources/libs/nvdsinfer_customparser/libnvds_infercustomparser.so

物体検出アプリケーションの動作

deepstream-appを下記で動作します。

cd /opt/nvidia/deepstream/deepstream-5.1/samples/configs/deepstream-app-trtis
deepstream-app -c source1_primary_faster_rcnn_inception_v2.txt

これでTriton Inference ServerとDeepStreamを組み合わせて使うことができるぞ

これでDeepStreamでいろんなモデルを動作できますね。



Close Bitnami banner
Bitnami