3、YOLOv8模型部署
Windows10环境安装YOLOv8的python环境安装,参考我的另一篇《Win10环境下yolov8快速配置与测试》:https://blog.csdn.net/m0_72734364/article/details/128815530
3.1 获取YOLOv8 onnx文件
直接在网盘下载(https://share.weiyun.com/3T3mZKBm),或者 google driver(https://drive.google.com/drive/folders/1-8phZHkx_Z274UVqgw6Ma-6u5AKmqCOv) 或者使用如下命令导出onnx:
# 🔥 yolov8 官方仓库: https://github.com/ultralytics/ultralytics # 🔥 yolov8 官方教程: https://docs.ultralytics.com/quickstart/ # 🚀TensorRT-Alpha will be updated synchronously as soon as possible! # 安装 yolov8 conda create -n yolov8 python==3.8 -y conda activate yolov8 pip install ultralytics==8.0.5 pip install onnx==1.12.0 # 下载官方权重(".pt" file) https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n.pt https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8s.pt https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8m.pt https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8l.pt https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8x.pt https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8x6.pt
导出 onnx:
# 640 yolo mode=export model=yolov8n.pt format=onnx dynamic=True #simplify=True yolo mode=export model=yolov8s.pt format=onnx dynamic=True #simplify=True yolo mode=export model=yolov8m.pt format=onnx dynamic=True #simplify=True yolo mode=export model=yolov8l.pt format=onnx dynamic=True #simplify=True yolo mode=export model=yolov8x.pt format=onnx dynamic=True #simplify=True # 1280 yolo mode=export model=yolov8x6.pt format=onnx dynamic=True #simplify=True
3.2 编译 onnx
# trtexec.exe在路径:F:\ThirdParty\TensorRT-8.4.2.4\bin # trtexec.exe, onnx, trt路径改成自己的 # 640 ../../../../TensorRT-8.4.2.4/bin/trtexec.exe --onnx=yolov8n.onnx --saveEngine=yolov8n.trt --buildOnly --minShapes=images:1x3x640x640 --optShapes=images:4x3x640x640 --maxShapes=images:8x3x640x640 ../../../../TensorRT-8.4.2.4/bin/trtexec.exe --onnx=yolov8s.onnx --saveEngine=yolov8s.trt --buildOnly --minShapes=images:1x3x640x640 --optShapes=images:4x3x640x640 --maxShapes=images:8x3x640x640 ../../../../TensorRT-8.4.2.4/bin/trtexec.exe --onnx=yolov8m.onnx --saveEngine=yolov8m.trt --buildOnly --minShapes=images:1x3x640x640 --optShapes=images:4x3x640x640 --maxShapes=images:8x3x640x640 ../../../../TensorRT-8.4.2.4/bin/trtexec.exe --onnx=yolov8l.onnx --saveEngine=yolov8l.trt --buildOnly --minShapes=images:1x3x640x640 --optShapes=images:4x3x640x640 --maxShapes=images:8x3x640x640 ../../../../TensorRT-8.4.2.4/bin/trtexec.exe --onnx=yolov8x.onnx --saveEngine=yolov8x.trt --buildOnly --minShapes=images:1x3x640x640 --optShapes=images:4x3x640x640 --maxShapes=images:8x3x640x640 # 1280 ../../../../TensorRT-8.4.2.4/bin/trtexec.exe --onnx=yolov8x6.onnx --saveEngine=yolov8x6.trt --buildOnly --minShapes=images:1x3x1280x1280 --optShapes=images:4x3x1280x1280 --maxShapes=images:8x3x1280x1280
你将会的到例如:yolov8n.trt、yolov8s.trt、yolov8m.trt等文件。
3.3 编译运行
3.3.1 新建、设置项目
下载TensorRT-Alpha仓库:
git clone https://github.com/FeiYull/tensorrt-alpha
- 新建yolov8 C++项目:参考B站视频【提示:从0分34秒开始,演示如何设置NVCC编译,如何避免tensorrt在win环境的坑】:「yolov8 tensorrt 实战之先导」(https://www.bilibili.com/video/BV1xT411f72f/?spm_id_from=333.999.0.0&vd_source=a96c9c3f099f4167807291a34fd50fd5)
- 小结:后续创建TensorRT-Alpha中YOLOv7、 YOLOv6等工程之后,只需要将上文中的属性表添加到工程,然后按照《yolov8 tensorrt 实战之先导》提到的设置工程就OK。
3.3.2 编译运行
上面视频在vs2019中设置命令行参数,您也可以在CMD命令行上运行程序,如下:
# 下面参数解释 # --show 表示可视化结果 # --savePath 表示保存,默认保存在build目录 # --savePath=../ 保存在上一级目录 ## 640 # 推理图片 ./app_yolov8.exe --model=../../data/yolov8/yolov8n.trt --size=640 --batch_size=1 --img=../../data/6406407.jpg --show --savePath ./app_yolov8.exe --model=../../data/yolov8/yolov8n.trt --size=640 --batch_size=8 --video=../../data/people.mp4 --show --savePath # 推理视频 ./app_yolov8.exe --model=../../data/yolov8/yolov8n.trt --size=640 --batch_size=8 --video=../../data/people.mp4 --show --savePath=../ # 在线推理相机视频 ./app_yolov8.exe --model=../../data/yolov8/yolov8n.trt --size=640 --batch_size=2 --cam_id=0 --show ## 1280 # infer camera ./app_yolov8.exe --model=../../data/yolov8/yolov8x6.trt --size=1280 --batch_size=2 --cam_id=0 --show
我的B站所有视频都在WIN10环境运行,https://space.bilibili.com/2028719613
- yolov8 tensorrt cuda模型推理加速部署TensorRT-Alpha《ski facility》: https://player.bilibili.com/player.html?aid=778153500