Skip to content

tue-robotics/yolo_onnx_ros

Repository files navigation

YOLOv8 OnnxRuntime C++

C++ Onnx-runtime

This algorithm is inspired by Ultralitics implementation to perform inference using YOLOv8 (we also supports v11) in C++ with ONNX Runtime and OpenCV's API.

Benefits ✨

  • Friendly for deployment in the industrial sector.
  • Faster than OpenCV's DNN inference on both CPU and GPU.
  • Supports FP32 and FP16 CUDA acceleration.

Note ☕

  1. Benefit for Ultralytics' latest release, a Transpose op is added to the YOLOv8 model, while make v8 and v5 has the same output shape. Therefore, you can run inference with YOLOv5/v7/v8 via this project.

Exporting YOLOv8 Models 📦

To export YOLOv8 models, use the following Python script:

from ultralytics import YOLO

# Load a YOLOv8 model
model = YOLO("yolov8n.pt")

# Export the model
model.export(format="onnx", opset=12, simplify=True, dynamic=False, imgsz=640)

Alternatively, you can use the following command for exporting the model in the terminal

yolo export model=yolov8n.pt opset=12 simplify=True dynamic=False format=onnx imgsz=640,640

Exporting YOLOv8 FP16 Models 📦

import onnx
from onnxconverter_common import float16

model = onnx.load(R"YOUR_ONNX_PATH")
model_fp16 = float16.convert_float_to_float16(model)
onnx.save(model_fp16, R"YOUR_FP16_ONNX_PATH")

Download COCO.yaml file 📂

In order to run example, you also need to download coco.yaml. You can download the file manually from here

Dependencies ⚙️

Dependency Version
Onnxruntime(linux,windows,macos) >=1.14.1
OpenCV >=4.0.0
C++ Standard >=17
Cmake >=3.5
Cuda (Optional) =12.8
cuDNN (Cuda required) =9

Note: The dependency on C++17 is due to the usage of the C++17 filesystem feature.

Note (2): Due to ONNX Runtime, we need to use CUDA 12.8 and cuDNN 9. Keep in mind that this requirement might change in the future.

Build 🛠️

  1. Clone the repository to your local machine.

  2. Navigate to the root directory of the repository.

  3. Create a build directory and navigate to it:

    mkdir build && cd build
  4. Run CMake to generate the build files:

    cmake ..

    Notice:

    If you encounter an error indicating that the ONNXRUNTIME_ROOT variable is not set correctly, you can resolve this by building the project using the appropriate command tailored to your system.

    # compiled in a linux system
    cmake -D LINUX=TRUE ..
  5. Build the project:

    make
  6. The built executable should now be located in the build directory.

Usage 🚀

To run from main just run the executable. To run the detector on you C++ application:

//To run the detector add on you C++ application:
std::vector<DL_RESULT> results;
std::unique_ptr<YOLO_V8> yoloDetector = Initialize();
results = DetectObjects(yoloDetector, img);

//You can change your param as you like (inside the Initialize() function)
//Pay attention to your device and the onnx model type(fp32 or fp16)
DL_INIT_PARAM params;
params.rectConfidenceThreshold = 0.1;
params.iouThreshold = 0.5;
params.modelPath = "yolov8n.onnx";
params.imgSize = { 640, 640 };
params.cudaEnable = true;
params.modelType = YOLO_DETECT_V8;
yoloDetector->CreateSession(params);
Detector(yoloDetector);

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •