极智AI | deepstream6.0部署yolov3和yolov4教程

本文涉及的产品
视觉智能开放平台,视频通用资源包5000点
视觉智能开放平台,分割抠图1万点
视觉智能开放平台,图像通用资源包5000点
简介: 大家好,我是极智视界,本文介绍了使用 deepstream6.0 部署 yolov3 和 yolov4 的方法。

大家好,我是极智视界,本文介绍了使用 deepstream6.0 部署 yolov3 和 yolov4 的方法。

Yolo 系列是工程中应用十分广泛的目标检测算法,特别是从 yolov3 开始,逐步的进化,到 yolov4、yolov5 等,工程的接受度越来越高。而 deepstream 是英伟达提出的一套加速深度学习落地的 pipeline 应用,那么当 deepstream 遇到 yolo,会擦出什么样的火花呢,让我们来看。

关于 deepstream 的安装教程,可以查阅我之前写的几篇:《【经验分享】ubuntu 安装 deepstream6.0》、《【经验分享】ubuntu 安装 deepstream5.1》。

先来看下 deepstream6.0 source 的目录结构:

  • apps
  • apps-common
  • audio_apps
  • sample_apps:例程,如 deepstream-app、deepstream-test1...
  • gst-plugins:gstreamer 插件
  • include:头
  • libs:库
  • objectDetector_FasterRCNN:FasterRCNN 示例
  • objectDetector_SSD:SSD 示例
  • objectDetector_Yolo:YOLO 示例
  • tools: 日志相关


1、deepstream6.0 部署 yolov3

通过上述的 objectDetector_Yolo 工程来跑 yolov3,在 objectDetector_Yolo 工程里主要关注以下几个模块:

  • nvdsinfer_custom_impl_Yolo:yolov3 工程实现代码;
  • nvdsinfer_yolo_engine.cpp:解析模型、生成引擎
  • nvdsparsebbox_Yolo.cpp:输出层的解析函数,解析目标检测框
  • trt_utils.cpp 和 trt_utils.h:构造 TensorRT网络的工具类的接口和实现
  • yolo.cpp 和 yolo.h:生成 yolo 引擎的接口和实现
  • yoloPlugins.cpp 和 yoloPlugins.h:YoloLayerV3 and YoloLayerV3PluginCreator 的接口和实现
  • kernels.cu:cuda核底层实现
  • config_infer_xxx_.txt:模型的配置;
  • deepstream_app_config_xxx.txt:Gstreamer nvinfer 插件的配置文件;
  • xxx.cfg、xxx.weights:模型文件;

有以上这些就够了,下面开始。

1.1 下载模型文件

deepstream6.0 SDK 中是没有 yolov3 的模型文件的,需要自行下载,给出传送。

yolov3.cfg:https://github.com/pjreddie/darknet/blob/master/cfg/yolov3.cfg

yolov3.weights:https://link.zhihu.com/?target=https%3A//pjreddie.com/media/files/yolov3.weights

这里多说一句,如果你有 TensorRT 的 yolov3.engine 的话,就不需要原始模型文件了,如果没有 .engine 的话,其实会根据原始文件先生成 .engine。

1.2 配置 config_infer_primary_yolov3.txt

[property]
gpu-id=0
net-scale-factor=0.0039215697906911373
#0=RGB, 1=BGR
model-color-format=0
custom-network-config=yolov3.cfg
model-file=yolov3.weights
labelfile-path=labels.txt
int8-calib-file=yolov3-calibration.table.trt7.0
## 0=FP32, 1=INT8, 2=FP16 mode
network-mode=1
num-detected-classes=80
gie-unique-id=1
network-type=0
is-classifier=0
cluster-mode=2
maintain-aspect-ratio=1
parse-bbox-func-name=NvDsInferParseCustomYoloV3
custom-lib-path=nvdsinfer_custom_impl_Yolo/libnvdsinfer_custom_impl_Yolo.so
engine-create-func-name=NvDsInferYoloCudaEngineGet
[class-attrs-all]
nms-iou-threshold=0.3
threshold=0.7

1.3 配置 deepstream_app_config_yolov3.txt

[application]
enable-perf-measurement=1
perf-measurement-interval-sec=5
[tiled-display]
enable=1
rows=1
columns=1
width=1280
height=720
gpu-id=0
nvbuf-memory-type=0
[source0]
enable=1
type=3
uri=file://../../samples/streams/sample_1080p_h264.mp4
num-sources=1
gpu-id=0
cudadec-memtype=0
[sink0]
enable=1
#Type - 1=FakeSink 2=EglSink 3=File
type=2
sync=0
source-id=0
gpu-id=0
nvbuf-memory-type=0
[osd]
enable=1
gpu-id=0
border-width=1
text-size=15
text-color=1;1;1;1;
text-bg-color=0.3;0.3;0.3;1
font=Serif
show-clock=0
clock-x-offset=800
clock-y-offset=820
clock-text-size=12
clock-color=1;0;0;0
nvbuf-memory-type=0
[streammux]
gpu-id=0
live-source=0
batch-size=1
batched-push-timeout=40000
width=1920
height=1080
enable-padding=0
nvbuf-memory-type=0
[primary-gie]
enable=1
gpu-id=0
#model-engine-file=model_b1_gpu0_int8.engine
labelfile-path=labels.txt
batch-size=1
bbox-border-color0=1;0;0;1
bbox-border-color1=0;1;1;1
bbox-border-color2=0;0;1;1
bbox-border-color3=0;1;0;1
interval=2
gie-unique-id=1
nvbuf-memory-type=0
config-file=config_infer_primary_yoloV3.txt
[tracker]
enable=1
tracker-width=640
tracker-height=384
ll-lib-file=/opt/nvidia/deepstream/deepstream-6.0/lib/libnvds_nvmultiobjecttracker.so
ll-config-file=../../samples/configs/deepstream-app/config_tracker_NvDCF_perf.yml
gpu-id=0
enable-batch-process=1
enable-past-frame=1
display-tracking-id=1
[tests]
file-loop=0

1.4 工程编译

进入到 /opt/nvidia/deepstream/deepstream-6.0/sources/objectDetector_Yolo

cd /opt/nvidia/deepstream/deepstream-6.0/sources/objectDetector_Yolo

依次执行下面两条命令,编译生成 .so 文件:

export CUDA_VER=11.4    # 设置与设备相同的CUDA版本

或者在 /opt/nvidia/deepstream/deepstream-6.0/sources/objectDetector_Yolo/nvdsinfer_custom_impl_Yolo/Makefile 中修改:

然后执行编译

make -C nvdsinfer_custom_impl_Yolo

编译后会生产动态库文件,生成了 libnvdsinfer_custom_impl_Yolo.so 动态库文件。

1.5 执行

deepstream-app -c deepstream_app_config_yoloV3.txt

这里完成了 deepstream6.0 Yolov3 的部署。


2、deepstream6.0 部署 yolov4

这里以不同的方式来部署一下 yolov4,即直接调用 TensorRT Engine,而不是从原始模型导入。

2.1 使用 darknet2onnx2TRT 生成 yolov4.engine

下载 yolov4 darknet 原始权重,给出百度网盘传送:

https://pan.baidu.com/s/1dAGEW8cm-dqK14TbhhVetA     Extraction code:dm5b

clone 模型转换工程:

git clone https://github.com/Tianxiaomo/pytorch-YOLOv4.git Yolov42TRT

开始模型转换:

cd Yolov42TRT
# darknet2onnx
python demo_darknet2onnx.py ./cfg/yolov4.cfg ./cfg/yolov4.weights ./data/dog.jpg 1
# onnx2trt
trtexec --onnx=./yolov4_1_3_608_608_static.onnx --fp16 --saveEngine=./yolov4.engine --device=0

这样就会生成 yolov4.engine。

2.2 deepstream yolov4 推理工程配置

clone deepstream yolov4 推理工程:

git clone https://github.com/NVIDIA-AI-IOT/yolov4_deepstream.git
cd yolov4_deepstream/deepstream_yolov4

配置 config_infer_primary_yoloV4.txt:

[property]
gpu-id=0
net-scale-factor=0.0039215697906911373
#0=RGB, 1=BGR
model-color-format=0
model-engine-file=yolov4.engine
labelfile-path=labels.txt
batch-size=1
## 0=FP32, 1=INT8, 2=FP16 mode
network-mode=2
num-detected-classes=80
gie-unique-id=1
network-type=0
is-classifier=0
## 0=Group Rectangles, 1=DBSCAN, 2=NMS, 3= DBSCAN+NMS Hybrid, 4 = None(No clustering)
cluster-mode=2
maintain-aspect-ratio=1
parse-bbox-func-name=NvDsInferParseCustomYoloV4
custom-lib-path=nvdsinfer_custom_impl_Yolo/libnvdsinfer_custom_impl_Yolo.so
[class-attrs-all]
nms-iou-threshold=0.6
pre-cluster-threshold=0.4

配置 deepstream_app_config_yoloV4.txt:

[application]
enable-perf-measurement=1
perf-measurement-interval-sec=5
[tiled-display]
enable=0
rows=1
columns=1
width=1280
height=720
gpu-id=0
nvbuf-memory-type=0
[source0]
enable=1
type=3
uri=file:/opt/nvidia/deepstream/deepstream-6.0/samples/streams/sample_1080p_h264.mp4
num-sources=1
gpu-id=0
cudadec-memtype=0
[sink0]
enable=1
#Type - 1=FakeSink 2=EglSink 3=File
type=3
sync=0
source-id=0
gpu-id=0
nvbuf-memory-type=0
container=1
codec=1
output-file=yolov4.mp4
[osd]
enable=1
gpu-id=0
border-width=1
text-size=12
text-color=1;1;1;1;
text-bg-color=0.3;0.3;0.3;1
font=Serif
show-clock=0
clock-x-offset=800
clock-y-offset=820
clock-text-size=12
clock-color=1;0;0;0
nvbuf-memory-type=0
[streammux]
gpu-id=0
live-source=0
batch-size=1
batched-push-timeout=40000
width=1280
height=720
enable-padding=0
nvbuf-memory-type=0
[primary-gie]
enable=1
gpu-id=0
model-engine-file=yolov4.engine
labelfile-path=labels.txt
batch-size=1
bbox-border-color0=1;0;0;1
bbox-border-color1=0;1;1;1
bbox-border-color2=0;0;1;1
bbox-border-color3=0;1;0;1
interval=0
gie-unique-id=1
nvbuf-memory-type=0
config-file=config_infer_primary_yoloV4.txt
[tracker]
enable=0
tracker-width=512
tracker-height=320
ll-lib-file=/opt/nvidia/deepstream/deepstream-5.0/lib/libnvds_mot_klt.so
[tests]
file-loop=0

把 2.1 转换生成的 yolov4.engine 拷贝到 /opt/nvidia/deepstream/deepstream-6.0/sources/yolov4_deepstream

2.3 工程编译

进入到 /opt/nvidia/deepstream/deepstream-6.0/sources/yolov4_deepstream

cd /opt/nvidia/deepstream/deepstream-6.0/sources/yolov4_deepstream

依次执行下面两条命令,编译生成 .so 文件:

export CUDA_VER=11.4    # 设置与设备相同的CUDA版本

或者在 /opt/nvidia/deepstream/deepstream-6.0/sources/yolov4_deepstream/nvdsinfer_custom_impl_Yolo/Makefile 中修改:

然后执行编译

make -C nvdsinfer_custom_impl_Yolo

编译后会生产动态库文件,生成了 libnvdsinfer_custom_impl_Yolo.so 动态库文件。

2.4 执行

deepstream-app -c deepstream_app_config_yoloV4.txt

这里完成了 deepstream6.0 Yolov4 的部署。


以上分享了 deepstream6.0 部署 yolov3 和 yolov4 的方法,希望我的分享会对你的学习有一点帮助。


logo_show.gif


相关实践学习
部署Stable Diffusion玩转AI绘画(GPU云服务器)
本实验通过在ECS上从零开始部署Stable Diffusion来进行AI绘画创作,开启AIGC盲盒。
目录
打赏
0
0
0
0
7
分享
相关文章
Jupyter MCP服务器部署实战:AI模型与Python环境无缝集成教程
Jupyter MCP服务器基于模型上下文协议(MCP),实现大型语言模型与Jupyter环境的无缝集成。它通过标准化接口,让AI模型安全访问和操作Jupyter核心组件,如内核、文件系统和终端。本文深入解析其技术架构、功能特性及部署方法。MCP服务器解决了传统AI模型缺乏实时上下文感知的问题,支持代码执行、变量状态获取、文件管理等功能,提升编程效率。同时,严格的权限控制确保了安全性。作为智能化交互工具,Jupyter MCP为动态计算环境与AI模型之间搭建了高效桥梁。
174 2
Jupyter MCP服务器部署实战:AI模型与Python环境无缝集成教程
短短时间,疯狂斩获1.9k star,开源AI神器AingDesk:一键部署上百模型,本地运行还能联网搜索!
AingDesk 是一款开源的本地 AI 模型管理工具,已获 1.9k Star。它支持一键部署上百款大模型(如 DeepSeek、Llama),适配 CPU/GPU,可本地运行并联网搜索。五大核心功能包括零门槛模型部署、实时联网搜证、私人知识库搭建、跨平台共享和智能体工厂,满足学术、办公及团队协作需求。相比 Ollama 和 Cherry Studio,AingDesk 更简单易用,适合技术小白、团队管理者和隐私敏感者。项目地址:https://github.com/aingdesk/AingDesk。
270 3
阿里云 AI 搜索开放平台新功能发布:新增GTE自部署模型
阿里云 AI搜索开放平台正式推出 GTE 多语言通用文本向量模型(iic/gte_sentence-embedding_multilingual-base)
165 4
当无人机遇上Agentic AI:新的应用场景及挑战
本文简介了Agentic AI与AI Agents的不同、Agentic无人机的概念、应用场景、以及所面临的挑战
109 5
当无人机遇上Agentic AI:新的应用场景及挑战
破茧成蝶:阿里云应用服务器让传统 J2EE 应用无缝升级 AI 原生时代
本文详细介绍了阿里云应用服务器如何助力传统J2EE应用实现智能化升级。文章分为三部分:第一部分阐述了传统J2EE应用在智能化转型中的痛点,如协议鸿沟、资源冲突和观测失明;第二部分展示了阿里云应用服务器的解决方案,包括兼容传统EJB容器与微服务架构、支持大模型即插即用及全景可观测性;第三部分则通过具体步骤说明如何基于EDAS开启J2EE应用的智能化进程,确保十年代码无需重写,轻松实现智能化跃迁。
295 40
🔔阿里云百炼智能体和工作流可以发布为组件了,AI应用变成“搭积木”
本文介绍了如何通过智能体组件化设计快速生成PPT。首先,创建一个“PPT大纲生成”智能体并发布为组件,该组件可根据用户输入生成结构清晰的大纲。接着,在新的智能体应用中调用此组件与MCP服务(如ChatPPT),实现从大纲到完整PPT的自动化生成。整个流程模块化、复用性强,显著降低AI开发门槛,提升效率。非技术人员也可轻松上手,满足多样化场景需求。
🔔阿里云百炼智能体和工作流可以发布为组件了,AI应用变成“搭积木”
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等