YOLOv8-TensorRT-CPP

cyrusbehrMIT更新放缓

用 C++ 把 YOLOv8 模型部署到 TensorRT 上做高性能推理,适合需要低延迟目标检测的工程落地场景。

YOLOv8 TensorRT C++ Implementation

暂无版本735star84fork同步于 2026-11-07 07:30 UTC

怎么上手

由 README 提炼,执行前请确认
git clone https://github.com/cyrusbehr/YOLOv8-TensorRT-CPP --recursive
mkdir build && cd build && cmake .. && make -j
硬件要求按 README 说明

需要 NVIDIA GPU 与 CUDA(建议 >= 12.0)、cuDNN(建议 >= 8)、TensorRT(要求 >= 10.0),并需带 CUDA 支持的 OpenCV(建议 >= 4.8);README 未给出具体显存要求,仅在 INT8 校准报显存不足时建议调小 calibrationBatchSize。

需要 CUDA
下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

用 TensorRT C++ API 在 GPU 上跑 YOLOv8 推理,支持检测、分割和姿态估计

用 C++ 把 YOLOv8 模型部署到 TensorRT 上做高性能推理,适合需要低延迟目标检测的工程落地场景。

核心能力

  • 用 TensorRT C++ API 执行 GPU 推理
  • 支持目标检测、语义分割与人体姿态估计
  • 提供 PyTorch 转 ONNX 的转换脚本
  • 支持 FP32/FP16/INT8 精度,INT8 需校准数据
  • 内置 benchmark 可分别测预处理、推理、后处理耗时

容易踩的坑

clone 必须加 --recursive,否则子模块缺失;需手动把 CMakeLists.txt 里的 TODO 替换为 TensorRT 安装路径;首次运行要花 5 分钟以上从 ONNX 生成 TensorRT engine,且目前不支持 Windows。

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

0 个版本

暂无 release 数据(可能没有发版,或尚未同步)。

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    Stargazers Issues LinkedIn All Contributors

    Looking for Maintainers 🚀

    This project is actively seeking maintainers to help guide its growth and improvement. If you're passionate about this project and interested in contributing, I’d love to hear from you!

    Please feel free to reach out via LinkedIn to discuss how you can get involved.

    Getting Started

    This project demonstrates how to use the TensorRT C++ API to run GPU inference for YoloV8. It makes use of my other project tensorrt-cpp-api to run inference behind the scene, so make sure you are familiar with that project.

    Prerequisites

    • Tested and working on Ubuntu 20.04 & 22.04 (Windows is not supported at this time)
    • Install CUDA, instructions here.
      • Recommended >= 12.0
    • Install cudnn, instructions here.
      • Recommended >= 8
    • sudo apt install build-essential
    • sudo apt install python3-pip
    • pip3 install cmake
    • Install OpenCV with cuda support. To compile OpenCV from source, run the build_opencv.sh script provided here.
      • Recommended >= 4.8
    • Download TensorRT 10 from here.
      • Required >= 10.0
    • Extract, and then navigate to the CMakeLists.txt file and replace the TODO with the path to your TensorRT installation.

    Installation

    • git clone https://github.com/cyrusbehr/YOLOv8-TensorRT-CPP --recursive
    • Note: Be sure to use the --recursive flag as this repo makes use of git submodules.

    Converting Model from PyTorch to ONNX

    • Navigate to the official YoloV8 repository and download your desired version of the model (ex. YOLOv8x).
      • The code also supports semantic segmentation models out of the box (ex. YOLOv8x-seg) and pose estimation models (ex. yolov8x-pose.onnx).
    • pip3 install ultralytics
    • Navigate to the scripts/ directory and run the following:
    • python3 pytorch2onnx.py --pt_path <path to your pt file>
    • After running this command, you should successfully have converted from PyTorch to ONNX.
    • Note: If converting the model using a different script, be sure that end2end is disabled. This flag will add bbox decoding and nms directly to the model, whereas my implementation does these steps external to the model using good old C++.

    Building the Project

    • mkdir build
    • cd build
    • cmake ..
    • make -j

    Running the Executables

    • Note: the first time you run any of the scripts, it may take quite a long time (5 mins+) as TensorRT must generate an optimized TensorRT engine file from the onnx model. This is then saved to disk and loaded on subsequent runs.
    • Note: The executables all work out of the box with Ultralytic's pretrained object detection, segmentation, and pose estimation models.
    • To run the benchmarking script, run: ./benchmark --model /path/to/your/onnx/model.onnx --input /path/to/your/benchmark/image.png
    • To run inference on an image and save the annotated image to disk run: ./detect_object_image --model /path/to/your/onnx/model.onnx --input /path/to/your/image.jpg
      • You can use the images in the images/ directory for testing
    • To run inference using your webcam and display the results in real time, run: ./detect_object_video --model /path/to/your/onnx/model.onnx --input 0
    • Note: You can also specify a TensorRT engine file by using the --trt_model option if you have a pre-built TensorRT engine available.
    • For a full list of arguments, run any of the executables without providing any arguments.

    INT8 Inference

    Enabling INT8 precision can further speed up inference at the cost of accuracy reduction due to reduced dynamic range. For INT8 precision, calibration data must be supplied which is representative of real data the model will see. It is advised to use 1K+ calibration images. To enable INT8 inference with the YoloV8 sanity check model, the following steps must be taken:

    • Download and extract the COCO validation dataset, or procure data representative of your inference data: wget http://images.cocodataset.org/zips/val2017.zip
    • Provide the additional command line arguments when running the executables: --precision INT8 --calibration-data /path/to/your/calibration/data
    • If you get an "out of memory in function allocate" error, then you must reduce Options.calibrationBatchSize so that the entire batch can fit in your GPU memory.

    Benchmarking

    • Before running benchmarks, ensure your GPU is unloaded.
    • Run the executable benchmark using the /images/640_640.jpg image.
    • If you'd like to benchmark each component (preprocess, inference, postprocess), recompile setting the ENABLE_BENCHMARKS flag to ON: cmake -DENABLE_BENCHMARKS=ON ...
      • You can then rerun the executable

    Benchmarks run on NVIDIA GeForce RTX 3080 Laptop GPU, Intel(R) Core(TM) i7-10870H CPU @ 2.20GHz using 640x640 BGR image in GPU memory and FP16 precision.

    ModelTotal TimePreprocess TimeInference TimePostprocess Time
    yolov8n3.613 ms0.081 ms1.703 ms1.829 ms
    yolov8n-pose2.107 ms0.091 ms1.609 ms0.407 ms
    yolov8n-seg15.194 ms0.109 ms2.732 ms12.353 ms
    ModelPrecisionTotal TimePreprocess TimeInference TimePostprocess Time
    yolov8xFP3225.819 ms0.103 ms23.763 ms1.953 ms
    yolov8xFP1610.147 ms0.083 ms7.677 ms2.387 ms
    yolov8xINT87.32 ms0.103 ms4.698 ms2.519 ms

    TODO: Need to improve postprocessing time using CUDA kernel.

    How to debug

    • If you have issues creating the TensorRT engine file from the onnx model, navigate to libs/tensorrt-cpp-api/src/engine.cpp and change the log level by changing the severity level to kVERBOSE and rebuild and rerun. This should give you more information on where exactly the build process is failing.

    Show your appreciation

    If this project was helpful to you, I would appreicate if you could give it a star. That will encourage me to ensure it's up to date and solve issues quickly.

    Contributors