在tengine平台使用TensorRT运行目标检测算法 您所在的位置:网站首页 tengine部署 在tengine平台使用TensorRT运行目标检测算法

在tengine平台使用TensorRT运行目标检测算法

2023-07-25 11:03| 来源: 网络整理| 查看: 265

在tengine平台使用TensorRT运行目标检测算法 1. 安装相关库2. 修改部署代码3. 编译4. 执行结果调试过程出现问题记录

来自实际项目,项目简述:自动驾驶测试阶段,需要在车端测试算法运行在一个带有2080Ti工控机,这里使用tengine来部署模型。由于项目保密原因,本文不使用实际本团队开发的算法,就仅仅使用tengine自带的demo,完全够用。

需求:使用开源的tengine在GPU上使用TensorRT运行目标检测模型 系统配置相关:Ubuntu18.4+CUDA10.2+CUDNN8.0.1+TensorRT7.1.3.4 系统配置方法:Ubuntu18配置CUDA10.2 cudnn8.0.1 TensorRT7.1.3.4 本文使用的demo包含算法:ScrFD人脸检测总结,2021轻量级的人脸检测

1. 安装相关库

(1)安装Opencv: (2)安装Protobuf(因为我的系统里面已经有了,所以这块没有啥经验)

sudo apt-get install gcc-7-aarch64-linux-gnu

(3)安装OpenCV Video关联库

$ sudo apt-get install v4l-utils 2. 修改部署代码

在编译代码之前,需要先写好部署代码,主要是下面三个部分:

(1)模型格式

TensorRT 支持加载 Float32 tmfile,如果工作在 Float16 推理精度模式下,Tengine 框架将在加载 Float32 tmfile 后自动在线转换为 Float16 数据进行推理。

推理精度设置:TensorRT 支持 Float32 、 Float16 、 Int8 三种精度模型进行网络模型推理,需要在执行 prerun_graph_multithread(graph_t graph, struct options opt) 之前通过 struct options opt 显式设置推理精度。

a. 开启GPU FP32模式

/* set runtime options */ struct options opt; opt.num_thread = num_thread; opt.cluster = TENGINE_CLUSTER_ALL; opt.precision = TENGINE_MODE_FP32; opt.affinity = 0;

b. 开启GPU FP16模式

/* set runtime options */ struct options opt; opt.num_thread = num_thread; opt.cluster = TENGINE_CLUSTER_ALL; opt.precision = TENGINE_MODE_FP16; opt.affinity = 0;

c. 开启GPU Int8模式

/* set runtime options */ struct options opt; opt.num_thread = num_thread; opt.cluster = TENGINE_CLUSTER_ALL; opt.precision = TENGINE_MODE_INT8; opt.affinity = 0;

(2)后端硬件绑定

在加载模型前,需要显式指定 TensorRT 硬件后端 context,并在调用 graph_t create_graph(context_t context, const char* model_format, const char* fname, …) 时传入该参数。

/* create NVIDIA TensorRT backend */ context_t trt_context = create_context("trt", 1); add_context_device(trt_context, "TRT"); /* create graph, load tengine model xxx.tmfile */ create_graph(trt_context, "tengine", model_file); 3. 编译 $ cd #进入源码所在根路径 $ mkdir -p build-linux-trt && cd build-linux-trt # 创建并进入编译文件夹 $ cmake -DTENGINE_ENABLE_TENSORRT=ON \ -DTENSORRT_INCLUDE_DIR=/usr/include/aarch64-linux-gnu \ -DTENSORRT_LIBRARY_DIR=/usr/lib/aarch64-linux-gnu .. $ make -j4 $ make install 4. 执行结果

进入到编译后的文件夹里面: 在这里插入图片描述运行如下命令:

./tm_scrfd_trt_fp16 -m /home/mabt/tengine/Tengine-tengine-lite/benchmark/models/scrfd_500m_kps.tmfile -i 1.jpg -r 10 -t 1

参数解释: tm_scrfd_trt_fp16是编译后的部署代码 -m 是指定模型转换为tmfile后的文件路径 -i 是输入图片 -r 是重复次数 -t 是线程数

运行结果如下: 在这里插入图片描述同时也输出了一张图片: 在这里插入图片描述

在这里插入图片描述

调试过程出现问题记录 在编译部署工程时,提示cmake版本低,故需要升级cmake,升级方法详见我的另一篇博客:Ubuntu18升级 cmake报错缺少头文件:fatal error: NvInfer.h: No such file or directory | #include ,解决方法参加另一篇博客:使用tengine部署模型时,编译报错:fatal error: NvInfer.h: No such file or directory | #include <NvInfer.h>error while loading shared libraries: libtengine-lite.so: cannot open shared object file: No such file or directory,找不到 libtengine-lite.so,解决:在~/.bashrc中添加libtengine-lite.so路径


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有