YOLOv8-TensorRT C++ ubuntu部署

YOLOv8-TensorRT C++ ubuntu20.04部署

先要安装好显卡驱动、CUDA、CUDNN

以ubuntu20.04、显卡1650安装470版本的显卡驱动、11.3版本的CUDA及8.2版本的CUDNN为例

下载TensorRT

进入网站:

https://developer.nvidia.com/nvidia-tensorrt-8x-download

进行勾选下载:

TAR是免安装直接解压可用的

解压:

bash 复制代码
tar -zxvf TensorRT-8.4.2.4.Linux.x86_64-gnu.cuda-11.6.cudnn8.4.tar.gz

cd TensorRT-8.4.2.4/samples/sampleMNIST

make

cd ../../bin

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/YourPath/TensorRT-8.4.2.4/lib

./sample_mnist

终端打印出如下内容表明cuda+cudnn+tensorrt安装正常:

可以在.bashrc里面加入TensorRT的路径:

复制代码
# TensorRT
export TRT_PATH=/usr/local/TensorRT-8.4.2.4
export PATH=$PATH:$TRT_PATH/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$TRT_PATH/lib
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$TRT_PATH/targets/x86_64-linux-gnu/lib

使用YOLOv8-TensorRT

先下载

https://github.com/triple-Mu/YOLOv8-TensorRT.git

bash 复制代码
git clone https://github.com/triple-Mu/YOLOv8-TensorRT.git

cd YOLOv8-TensorRT

pip install -r requirements.txt

C++ build:

CMakeLists中需要将TensorRT路径改一下

复制代码
# TensorRT
set(TensorRT_INCLUDE_DIRS /usr/include/x86_64-linux-gnu)
set(TensorRT_LIBRARIES /usr/lib/x86_64-linux-gnu)

加下来就可以编译了

复制代码
export root=${PWD}
cd csrc/detect/normal
mkdir build
cmake ..
make
mv yolov8 ${root}
cd ${root}

官方给出的model是pt格式,我们需要TensorRT要用的engine格式,

PyTorch model -> ONNX -> TensorRT Engine

Then,transform!

python 复制代码
# PyTorch model -> ONNX

from ultralytics import YOLO

# Load a model
model = YOLO("yolov8s.pt")  # load a pretrained model (recommended for training)
success = model.export(format="onnx", opset=11, simplify=True)  # export the model to onnx format
assert success

ONNX -> TensorRT Engine需要用TensorRT

终端进入TensorRT目录

bash 复制代码
cd samples/trtexec
make

然后退到TensorRT进入bin文件夹可以看到trtexec可执行文件

复制代码
/bin/trtexec \
--onnx=yolov8s.onnx \
--saveEngine=yolov8s.engine \
--fp16

路径不要错就可以成功将onnx转为engine格式了,运行时间可能会有些长

如果在C++ build时遇到下面error可以按下面的解决方案:

复制代码
error: invalid initialization of reference of type 'std::vector<cv::String>&' from expression of type 'std::vector<std::__cxx11::basic_string<char> >'
   69 |         cv::glob(path + "/*.jpg", imagePathList);

将第46行:

c++ 复制代码
    std::vector<std::string> imagePathList;

改为:

c++ 复制代码
    std::vector<cv::string> imagePathList;

接下来就可以使用了

bash 复制代码
# infer image
./yolov8 yolov8s.engine data/bus.jpg
# infer images
./yolov8 yolov8s.engine data
# infer video
./yolov8 yolov8s.engine data/test.mp4 # the video path

如:

复制代码
./yolov8 pt/yolov8n.engine data/zidane.jpg
相关推荐
前端初见12 小时前
快速上手TypeScript,TS速通
javascript·ubuntu·typescript
HIT_Weston16 小时前
14、【Ubuntu】【VSCode】VSCode 断联问题分析:hostname(二)
linux·vscode·ubuntu
冲上云霄的Jayden16 小时前
bash执行脚本 CondaError: Run ‘conda init‘ before ‘conda activate‘
linux·ubuntu·conda·bash·init·activate
qiuiuiu41318 小时前
正点原子RK3568学习日志19- Linux错误处理 字符驱动框架完全体
linux·服务器·学习·ubuntu
如果是君18 小时前
【git使用】ubuntu下利用git工具提交一个工程
linux·git·ubuntu
MOYIXIAOWEIWEI18 小时前
rocky 9.5系统安装zabbix监控实现邮件告警
ubuntu·zabbix·rocky
Linux运维技术栈19 小时前
Ubuntu Certbot版本查询失败?Snap安装后报错终极修复指南(通用版)
ubuntu·ssl
HIT_Weston1 天前
15、【Ubuntu】【VSCode】VSCode 断联问题分析:UID 补充
linux·vscode·ubuntu
碰大点1 天前
Ubuntu 16.04交叉编译arm-linux-gnueabihf的QT5.6.2
linux·arm开发·qt·ubuntu·arm-linux
AlexMercer10121 天前
Ubuntu从零开始配置Git
c++·git·ubuntu·gitee