YOLOv11 C++ TensorRT

引用

YOLOv11 C++ TensorRT项目是一个用C++实现并使用NVIDIA TensorRT进行优化的高性能对象检测解决方案。该项目利用 YOLOv11 模型提供快速准确的对象检测,并利用 TensorRT 最大限度地提高推理效率和性能。

📢 更新

主要特点:

模型转换:将 ONNX 模型转换为 TensorRT 引擎文件以加速推理。

视频推理:有效地对视频文件进行对象检测。

图像推理:对单个图像执行对象检测。

高效率:针对使用 NVIDIA GPU 的实时物体检测进行了优化。

使用 CUDA 进行预处理:支持 CUDA 的预处理,可实现更快的输入处理。

YOLOv11 模型的基准推理时间

📂 项目结构

YOLOv11-TensorRT/

├── CMakeLists.txt # Build configuration for the project

├── include/ # Header files

├── src/

│ ├── main.cpp # Main entry point for the application

│ ├── yolov11.cpp # YOLOv11 implementation

│ └── preprocess.cu # CUDA preprocessing code

├── assets/ # Images and benchmarks for README

└── build/ # Compiled binaries

🛠️ 设置

先决条件

CMake(版本 3.18 或更高版本)

TensorRT(V8.6.1.6:用于使用 YOLOv11 进行优化推理。)

CUDA 工具包(V11.7:用于 GPU 加速)

OpenCV(V4.10.0:用于图像和视频处理)

NVIDIA GPU(计算能力 7.5 或更高)

安装

cd YOLOv11-TensorRT

更新 CMakeLists.txt 中的 TensorRT 和 OpenCV 路径:

set(TENSORRT_PATH "F:/Program Files/TensorRT-8.6.1.6") # Adjust this to your path

构建项目:

mkdir build

cd build

cmake ..

make -j$(nproc)

🚀 使用方法

将 Yolov11 转换为 ONNX 模型

from ultralytics import YOLO

Load the YOLO model

model = YOLO("yolo11s.pt")

#Export the model to ONNX format

export_path = model.export(format="onnx")

将 ONNX 模型转换为 TensorRT 引擎

要将 ONNX 模型转换为 TensorRT 引擎文件,请使用以下命令:

./YOLOv11TRT convert path_to_your_model.onnx path_to_your_engine.engine.

path_to_your_model.onnx:ONNX 模型文件的路径。

path_to_your_engine.engine:TensorRT 引擎文件的保存路径。

对视频进行推理

要对视频运行推理,请使用以下命令:

./YOLOv11TRT infer_video path_to_your_video.mp4 path_to_your_engine.engine

path_to_your_video.mp4:输入视频文件的路径。

path_to_your_engine.engine:TensorRT 引擎文件的路径。

对视频进行推理

对图像运行推理 要在图像上运行推理,请使用以下命令:

./YOLOv11TRT infer_image path_to_your_image.jpg path_to_your_engine.engine

path_to_your_image.jpg:输入图像文件的路径。

path_to_your_engine.engine:TensorRT 引擎文件的路径。

⚙️ 配置

CMake 配置

在 CMakeLists.txt 中,如果 TensorRT 和 OpenCV 安装在非默认位置,请更新它们的路径:

设置TensorRT安装路径

#Define the path to TensorRT installation

set(TENSORRT_PATH "F:/Program Files/TensorRT-8.6.1.6") # Update this to the actual path for TensorRT

确保该路径指向安装 TensorRT 的目录。

故障排除

找不到 nvinfer.lib:确保 TensorRT 已正确安装且 nvinfer.lib 位于指定路径中。更新 CMakeLists.txt 以包含 TensorRT 库的正确路径。

链接器错误:验证所有依赖项(OpenCV、CUDA、TensorRT)是否正确安装,以及它们的路径是否在 CMakeLists.txt 中正确设置。

运行时错误:确保您的系统具有正确的 CUDA 驱动程序,并且 TensorRT 运行时库可访问。将 TensorRT 的 bin 目录添加到您的系统 PATH。

相关推荐
博笙困了4 小时前
AcWing学习——双指针算法
c++·算法
感哥4 小时前
C++ 指针和引用
c++
光影少年4 小时前
webpack打包优化
webpack·掘金·金石计划·前端工程化
感哥15 小时前
C++ 多态
c++
沐怡旸21 小时前
【底层机制】std::string 解决的痛点?是什么?怎么实现的?怎么正确用?
c++·面试
River4161 天前
Javer 学 c++(十三):引用篇
c++·后端
感哥1 天前
C++ std::set
c++
侃侃_天下1 天前
最终的信号类
开发语言·c++·算法
博笙困了1 天前
AcWing学习——差分
c++·算法
青草地溪水旁1 天前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(2)
c++·设计模式·抽象工厂模式