YOLO11-obb使用C++及trt进行推理(详细版)

针对YOLO的使用.engine权重及C++代码进行推理


使用TensorRT-YOLO项目网站是:
https://github.com/laugh12321/TensorRT-YOLO

可以直接选择git或者下载下来

git clone https://github.com/laugh12321/TensorRT-YOLO
cd TensorRT-YOLO

1.编译主程序

教程网址是:

https://github.com/laugh12321/TensorRT-YOLO/blob/main/docs/cn/build_and_install.md

pip install "pybind11[global]" # 安装 pybind11,用于生成 Python 绑定
cmake -S . -B build -D TRT_PATH=/your/tensorrt/dir -D BUILD_PYTHON=ON -D CMAKE_INSTALL_PREFIX=/your/tensorrt-yolo/install/dir
cmake --build build -j$(nproc) --config Release --target install

//将TRT_PATH地址更换为tensorrt的安装路径

TRT_PATH=/your/tensorrt/dir

//输出路径,创建个文件夹然后选中就可以
CMAKE_INSTALL_PREFIX=/your/tensorrt-yolo/install/dir

  其中:efficientRotatedNMSInference.cuh文件会报错

        需要指定转换folat类型,我上传一下我修改后的文件,搜索到这个文件然后替换掉就可以了

https://pan.baidu.com/s/1n4_ncb9IFyxFRVs6d0w6Ig?pwd=4qan 提取码: 4qan

添加环境变量

export PATH=$PATH:/your/tensorrt-yolo/install/dir # linux

路径就是 :

CMAKE_INSTALL_PREFIX=/your/tensorrt-yolo/install/dir     #刚才选择输出的文件夹路径




2.第二步将训练好的.pt文件转换为.onnx文件再转换为.engine文件

需要使用本项目代码进行转换,官方代码转换得到的.engine实测报错不能运行 

(1)安装包

pip install -U tensorrt_yolo
pip install --upgrade build
python -m build --wheel
pip install dist/tensorrt_yolo-6.*-py3-none-any.whl[export]

(2)转换为.onnx文件

trtyolo export -w models/yolo11n-obb.pt -v yolo11 -o models -s

其中参数代表:

  • -v, --version: 模型版本。支持的选项包括 yolov3, yolov5, yolov8, yolov10, yolo11, yolo12, yolo-world, yoloe, pp-yoloe, ultralytics

  • -o, --output: 导出模型保存的目录路径。

  • -w, --weights: PyTorch YOLO 权重文件路径(非 PP-YOLOE 模型必需)。

  • --model_dir: 包含 PaddleDetection PP-YOLOE 模型的目录路径(PP-YOLOE 模型必需)。

  • --model_filename: PaddleDetection PP-YOLOE 模型文件名(PP-YOLOE 模型必需)。

  • --params_filename: PaddleDetection PP-YOLOE 参数文件名(PP-YOLOE 模型必需)。

  • -b, --batch: 模型的总批量大小。使用 -1 表示动态批量大小,默认为 1

  • --max_boxes: 每张图像的最大检测框数量,默认为 100

  • --iou_thres: NMS 的 IoU 阈值,默认为 0.45

  • --conf_thres: 目标检测的置信度阈值,默认为 0.25

  • --imgsz: 图像大小(单个值表示正方形,或 "height,width")。默认为 640(非 PP-YOLOE 模型)。

  • --names: 自定义类别名称(仅适用于 YOLO-World 和 YOLOE 模型,以逗号分隔,例如 "person,car,dog")。

  • --repo_dir: 包含本地仓库的目录路径(仅适用于 YOLOv3 和 YOLOv5 模型,使用 torch.hub.load 时适用)。

  • --opset: ONNX opset 版本,默认为 12

  • -s, --simplify: 是否简化导出的 ONNX 模型,默认为 False

(3)转换为.engine文件

trtexec --onnx=yolo11n-obb.onnx --saveEngine=yolo11n-obb.engine --fp16 --minShapes=images:1x3x640x640 --optShapes=images:4x3x640x640 --maxShapes=images:8x3x640x640 --staticPlugins=/your/tensorrt-yolo/install/dir/lib/custom_plugins.dll --setPluginsToSerialize=/your/tensorrt-yolo/install/dir/lib/custom_plugins.dll

记得把后面两个文件路径也改一下,搜索一下就能搜到,就是第一步中输出文件夹路径里的文件



3.编译所需要的模型

cd ./examples/obb
cmake -S . -B build
cmake --build build -j8 --config Release

 推理指令:

 cd bin
 ./obb -e ../models/yolo11n-obb.engine -i ../images -o ../output -l ../labels.txt

将 ../models/yolo11n-obb.engine更改为所用.engine的路径

将../images修改为要推理图片所在路径

将../labels.txt修改为标签txt文件所在路径,txt里面内容写好自己模型的标签

你可能感兴趣的:(c++,目标检测,计算机视觉,YOLO,图像处理)