How to load model YOLOv8 ONNX CV2 DNN

Ali Mustofa
6 min readFeb 1, 2023

YOLOv8 is the latest version (v8) of the YOLO (You Only Look Once) object detection system. YOLO is a real-time, one-shot object detection system that aims to perform object detection in a single forward pass of the network, making it fast and efficient. YOLOv8 is an improved version of the previous YOLO models with improved accuracy and faster inference speed.

ONNX (Open Neural Network Exchange) is an open format to represent deep learning models. To convert a YOLOv8 model to ONNX format, you need to use a tool such as ONNX Runtime, which provides an API to convert models from different frameworks to ONNX format. The exact steps would depend on the programming framework and tools you are using to develop and run your YOLOv8 model.

  1. How to convert?
model = YOLO("yolov8s.pt")  
results = model.predict(source="https://ultralytics.com/images/bus.jpg")[0]

model.export(format="onnx",opset=12) # export the model to ONNX format

2. How to load ONNX using CV2 DNN?

OpenCV’s “dnn” module stands for “Deep Neural Network” and provides functionality for running inference on deep learning models. The module supports multiple backends including ONNX, TensorFlow, and Caffe. With the “dnn” module, you can import pre-trained deep learning models, run forward-pass inference, and obtain the…

--

--