or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Syncing
xxxxxxxxxx
Accelerate multi-streaming cameras with DeepStream and deploy custom (YOLO) models
使用DeepStream加速多串流攝影機並部署客製(YOLO)模型
tags:
Edge AI
DeepStream
Edge_AI
deployment
Nvidia
Jetson
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →NVIDIA Jetson 平台部署相關筆記
基本環境設定
OpenCV在Nvidia Jetson平台開啟cuda加速
模型部署與加速
將PyTorch模型轉換為TensorRT,實現3-8倍加速
使用DeepStream加速多串流攝影機並部署客製(YOLO)模型
使用Deepstream python API提取模型輸出張量並定製模型后處理(如:YOLO-Pose)
yolov7 with multiple cameras running on DeepStream
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →Github 開箱即用
簡介
白話來說,DeepStream是Nvidia是作為AI部屬平台的加速工具,主要在串接模型資料傳輸處理與協定上進行加速,特別是在多媒體串流處理上更有使用的必要性
DeepStream 應用架構解析
DeepStream Reference Application - deepstream-app
DeepStream組件
Nvidia DeepStream是一個人工智能框架,有助於利用Jetson和GPU設備中的Nvidia GPU的最終潛力來實現計算機視覺。它為Jetson Nano等邊緣設備和Jetson系列的其他設備提供動力,實時處理邊緣設備上的並行視頻流。
DeepStream使用Gstreamer流水線(用C語言編寫)在GPU中獲取輸入視頻,最終以更快的速度處理它,以便進一步處理。
DeepStream的組成部分
DeepStream有一個基於插件的架構。基於圖形的管道接口允許高層組件互連。它可以在GPU和CPU上使用多線程進行異質並行處理(heterogeneous parallel processing)。
下面是DeepStream的主要組件和它們的高級功能
Meta Dat
Decoder
Video Aggregator
(nvstreammux)
Inferencing
(nvinfer)
Format Conversion and Scaling
(nvvidconv)
Object Tracker
(nvtracker)
Screen Tiler
(nvstreamtiler)
On Screen Display
(nvdosd)
(nvmsgconv + nvmsgbroker)
(source : Nvidia DeepStream – A Simplistic Guide)
Decoder -> Muxer -> Inference -> Tracker (if any) -> Tiler -> Format Conversion -> On Screen Display -> Sink
DeepStream應用程序由兩部分組成,一部分是配置文件,另一部分是其驅動文件(可以是C語言或Python語言)。
在設定檔config.txt相關的控制項的說明在
Configuration Groups
文件內使用Gstreamer 命令列檢視組件功能
這邊需要先有Gstreamer的基本概念才看得懂
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →安裝完成gstreamer後,執行
gst-inspect-1.0
指令檢視模組功能,以nvinfer
模組為例nvinfer
插件的屬性定義與功能Pad Templates
andPads
(數據傳輸接口)src
輸出(數據生產)與sink
輸入(數據消費)的端點video/x-raw(memory:NVMM)
預設使用NVIDIA的GPU處理NV12
與RGBA
RGBA
Element Properties
(元素屬性)可以見到有許多參數的設定說明,這也是後面config file中設定的參數項目
.get_property()
.set_property()
Python API(binding)使用入門
以下主要參考官方提供的DeepStream Python AppsNVIDIA-AI-IOT/deepstream_python_apps的範例,內有詳細執行範例
環境安裝
按NVIDIA-AI-IOT/deepstream_python_apps/blob/master/bindings/README.md文件指引
補充 執行到 步驟1.3 deepstream_python_apps倉儲安裝
按以下指示安裝
當執行到 "1.3 Initialization of submodules"時
要將"deepstream_python_apps"的倉儲clone到DeepStream根目錄/source目錄下
<DeepStream 6.2 ROOT>/sources
:我這邊安裝的是deepstream 6.2版,可以用dpkg -L deepstream-6.2
指令查找安裝位置,按Linux系統慣例果然在/opt/之下,我查到的安裝位置是:/opt/nvidia/deepstream/deepstream-6.2/
補充 執行步驟2 Compiling the bindings時出現錯誤
按照官方指示執行到2.1 Quick build (x86-ubuntu-20.04 | python 3.8 | Deepstream 6.2)時
按照官方預設指令先執行
make ..
,再接在執行make
以後,會在/opt/nvidia/deepstream/deepstream/sources/deepstream_python_apps/bindings/build
下產出 'dist/pyds-1.1.6-py3-none-linux_x86_64.whl' 檔案,後面接著執行會出現錯誤訊息"ERROR: pyds-1.1.6-py3-none-linux_x86_64.whl is not a supported wheel on this platform."
cmake .. -DPIP_PLATFORM=linux_aarch64
範例
- 官方文件內有提供各種應用情境範例的.py檔及配置文件,包含串聯不同模型、multistream、結合Triton或直接使用本機TRT直接推論
模型轉換
模型轉換流程與格式
Nvidia官方指引Using the TensorRT Runtime API
以pytorch > onnx > TensorRT 為例
TensorRT格式有
.engine
與model.trt
兩種.engine
格式model.trt
格式tensorrt
、pycuda
等模組讀取二進位的.trt檔,對其序列化後使用在DeepStream文件設定中,指定以
model-engine-file
讀取==.engine==參數配置的必要性第一次啟用時,DeepStream app時會根據設定文件的配置,載入.onnx檔構建 TensorRT引擎後產出model.engine檔案,此後再指定“model-engine-file=model.engine”啟動就會快很多
在 DeepStream 中,如果已經指定了 "onnx-file",那麽指定 "model-engine-file" 的設置通常是可選的。
當指定 "model-engine-file" 參數時,它用於指定預先構建好的 TensorRT 引擎文件的路徑。預先構建引擎意味著將 ONNX 模型轉換為 TensorRT 引擎的過程已經提前執行,引擎文件已經生成並存儲在磁盤上。在應用程序運行時,DeepStream 將直接加載該引擎文件,而無需再次進行模型轉換和優化的過程。這可以節省啟動時間並加快執行速度。
如果指定了 "model-engine-file",DeepStream 將忽略 "onnx-file" 的設置,而直接加載和使用預先構建的引擎文件
YOLOv7模型格式轉換(Onnx → TensorRT Engine)
模型取得
NVIDIA-AI-IOT/yolo_deepstream/yolov7_qat
yolov7_qat_640.onnx
)下載後下一步要在自己的硬體平台先轉為TensorRT Engine格式
準備 TensorRT engines
convert onnx model(.onnx) to TensorRT-engine(.engine )
使用DeepStream部署YOLO系列模型
主要流程參照NVIDIA官方文件NVIDIA-AI-IOT/yolo_deepstream
另外這篇非官方的導覽也可以參考marcoslucianops/DeepStream-Yolo/customModels
手動修改DeepStream Python Binding範例文件,並指定使用客製模型(YOLOv7)
/opt/nvidia/deepstream/deepstream
目錄內以下分兩部分來說明:
1. DeepStream 模型檔案配置與客製模型編譯
這裡採取的作法是把客製模型放在模型專用目錄下,方便未來其他專案重複取用
samples/models/tao_pretrained_models/yolov7/
/opt/nvidia/deepstream/deepstream/samples/models/tao_pretrained_models/yolov7
指定為path_yolov7
變數,方便之後取用這時可以看到
nvdsinfer_custom_impl_Yolo/
內的資料結構包含3個檔案進入
nvdsinfer_custom_impl_Yolo
目錄內開始編譯編譯成功後,就會得到1個主要的.so檔及2個編譯好的物件檔案(.o)
2. DeepStream app(python API)與配置文件
python biding的範例檔位於
/opt/nvidia/deepstream/deepstream/sources/deepstream_python_apps/apps
2.1 這邊以
deepstream_test1_rtsp_in_rtsp_out/
為例deepstream_test1_rtsp_in_rtsp_out_getconf.py
dstest1_pgie_inferserver_config.txt
資料輸入
rtsp://
或file:/
)讀入多種格式的影片檔案(.h264、.mp4、.mov等)資料輸出
rtsp://localhost:8554/ds-test
rtsp://<your_server_ip>:8554/ds-test
修改
deepstream_test1_rtsp_in_rtsp_out.py
讀取設定檔的路徑2.2 模型配置文件修改
dstest1_pgie_yolov7_config.txt
pgie_config.txt 客製模型配置文件說明
以下說明改動部分,主要是跟模型存放路徑有關
為方便閱讀,這邊以your_model_path 代替完整路徑
/opt/nvidia/deepstream/deepstream/samples/models/tao_pretrained_models/yolov7/
onnx-file=your_model_path/yolov7.onnx
model-engine-file= your_model_path/yolov7.onnx_b16_gpu0_fp16.engine
labelfile-path=your_model_path/labels.txt
batch-size
至於在deepstream中該設多大呢? 參考前人經驗並非越大越好,還需要實際測試,但可以先抓跟你要輸入的camera數量接近parse-bbox-func-name=NvDsInferParseCustomYoloV7_cuda
# 使用cuda做後處裡custom-lib-path=your_model_path/nvdsinfer_custom_impl_Yolo/libnvdsinfer_custom_impl_Yolo.so
make
編譯好的.so檔dstest1_pgie_yolov7_config.txt 詳細配置說明
Case Study
deepstream sdk-api
範例 :讓螢幕畫面的類別標籤展示信任分數confidence scores
完整的程式碼放在YunghuiHsu/deepstream_python_apps/apps/deepstream-rtsp-in-rtsp-out
流程如示意圖,主要在Metadata進入


nvdosd
物件(負責螢幕顯示工作)前加入Probe,告知nvdosd
該如何顯示想要的資訊,螢幕追蹤資訊要如何呈現,則在def tiler_src_pad_buffer_probe()
中定義取出並顯示信賴分數(Confidence score)
在
def tiler_src_pad_buffer_probe()
中撈出meta資料obj_meta.text_params.display_text
: 顯示meta資料文字obj_meta.confidence
: meta物件資料中預定義提取信賴分數的關鍵字deepstream_test1_rtsp_in_rtsp_out_getconf.py
接下來還需要加入探針才能更新要顯示的資訊
在
nvdosd
物件接口前加入探針(probe)更新要顯示的資訊deepstream_test1_rtsp_in_rtsp_out_getconf.py
執行範例
Reference for display confidence scores
自定義傳遞訊息資料
使用NvDsEventMsgMeta物件
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →自定義修改及撈取MetaData
關於客製meta data存放、修改與撈出
使用自定義 NvDsUserMeta物件
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →NvDsInferTensorMeta
在
[Gst-nvinfer
]階段,可直接從TensorRT inference engine讀取原始(預測的)輸出張量,轉為meta格式- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →從Deepstream Buffer中取出影像與meta資料進行客製操作
從流程圖中可見,分別從FRAME BUFFER與<INFERENCE>模塊撈出影像與模型預測的張量
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →使用Deepstream python API提取模型輸出張量並定製模型后處理(如:YOLO-Pose)
NvBufSurface
NvBufSurface 是 NVIDIA DeepStream SDK 中的一個結構,用於表示經過解碼和處理的視訊幀的圖像資料
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →NvBufSurface 的功能如下:
code 範例(c++)
code 範例(c++)
DeepStream效能優化的基本原則
DeepStream best practices
以下羅列幾項基本設置,更多請參考官方文件對於效能提升的提示DeepStream best practices
Jetson optimization
確保Jetson時脈開至最大
參考資料
NVIDIA DeepStream官方文件
DeepStream SDK | NVIDIA Developer
Building a Real-time Redaction App Using NVIDIA DeepStream, Part 2: Deployment | NVIDIA Technical Blog
NVIDIA 官方範例程式
其他不錯的DeepStream概念介紹
Figure 2: A DeepStream Pipeline with two processing elements
NVIDIA 教學影片
2023/01。NVIDIA DeepStream Technical Deep Dive: DeepStream Inference Options with Triton & TensorRT
帶有DS-TensorRT(gst-nvinfer)插件的DeepStream-app設置
DeepStream-app with DS-Triton (gst-nvinferserver) Server CAPI
DeepStream-app with DS-Triton (gst-nvinferserver) gRPC Inference
Triton Server CAPI
Triton gRPC Remote
- 不需要通過網絡傳輸數據
- 可以使用Triton Server提供的所有特性
- 不支持在遠程服務器上運行模型
- 遠程服務器必須支持gRPC協議
在NVIDIA Triton Inference Server中,"CAPI"和"gRPC"都是指不同的推理模式。
CAPI
gRPC
推理前的DeepStream批(Batching)處理
nvstreammux
在推理前將所有的輸入數據流分批輸入 在推理前一起進行nvstreammux
的批處理策略適用於兩個推理插件解析推理數據的DeepStream示例應用程序
DeepStream Triton推理數據流
2022/06。NVIDIA DeepStream Technical Deep Dive : Multi-Object Tracker
2021/01。Implementing Real-time Vision AI Apps Using NVIDIA DeepStream SDK
C++ python bindings
python-bindings-overview
YOLO - DeepStream
以下幾個倉儲有提供C++的範例程式,不過如果要結合PYTHON BINDING的範例程式執行,要手動修改的部分還蠻複雜的
GITHUB
efficientNMS
外掛的支援Forum
python3 deepstream_test_1.py /opt/nvidia/deepstream/deepstream-6.2/samples/streams/sample_720p.h264
GStreamer
DeepStream的功能是建基於GStreamer上,所以在使用前最好對後者的原理有點概念會比較好修改
gstreamer/documentation
Deploy YOLOv8 on NVIDIA Jetson using TensorRT and DeepStream SDK