How to Use Export YOLO ONNX model to use the ZED Custom Object Detection

Introduction #

This page will show you how to export a YOLO model into a ONNX file to use the ZED YOLO TensorRT inference example, or the CUSTOM_YOLOLIKE_BOX_OBJECTS mode in the native ZED SDK ObjectDetection module

This lets you train your custom model using Ultralytics YOLO (v5, v8, v10, v11), YOLOv6, YOLOv7

It can be used with the default model trained on COCO dataset (80 classes) provided by the framework maintainers. A custom detector can be trained with the same architecture. These tutorials walk you through the workflow of training a custom detector :

It allows using ZED 3D cameras with YOLO object detection, adding 3D localization and tracking to the most recent YOLO models, using your own classes.

Workflow #

The process is the following:

  1. Train your model or use an existing SOTA model
  2. Export into a ONNX file
  3. Load into the SDK or the sample the ONNX file to generate an optimized model. This process uses TensorRT framework to run the inference and requires a initial step of inference engine generation.

ZED SDK samples are available on Github:

YOLOv8 #

Installing yolov8 #

YOLOv8 can be installed directly from pip using the following command:

python -m pip -U install ultralytics

ONNX file export #

In this documentation, we’ll use the CLI for export https://docs.ultralytics.com/modes/export/

yolo export model=yolov8n.pt format=onnx simplify=True dynamic=False imgsz=608

For a custom model model the weight file can be changed:

yolo export model=yolov8l_custom_model.pt format=onnx simplify=True dynamic=False imgsz=512

Please refer to the corresponding documentation for more details https://github.com/ultralytics/ultralytics

YOLOv6 #

The sample was mainly tested with YOLOv6 v3.0 but should work with other version with minor to no modifications.

Installing yolov6 #

YOLOv6 can be installed directly from pip using the following command:

git clone https://github.com/meituan/YOLOv6
cd YOLOv6
pip install -r requirements.txt
pip install onnx>=1.10.0

ONNX file export #

wget https://github.com/meituan/YOLOv6/releases/download/0.3.0/yolov6s.pt
python ./deploy/ONNX/export_onnx.py \
    --weights yolov6s.pt \
    --img 640 \
    --batch 1 \
    --simplify

For a custom model model the weight file can be changed:

python ./deploy/ONNX/export_onnx.py \
    --weights yolov6l_custom_model.pt \
    --img 640 \
    --batch 1 \
    --simplify

Please refer to the corresponding documentation for more details https://github.com/meituan/YOLOv6/tree/main/deploy/ONNX

YOLOv7 #

Installing yolov7 #

YOLOv8 can be installed directly from pip using the following command:

git clone https://github.com/WongKinYiu/yolov7.git
cd yolov7
python -m pip install -r requirements.txt

ONNX file export #

In this documentation, we’ll use the export script export.py

python export.py --weights ./yolov7-tiny.pt --grid --simplify --topk-all 100 --iou-thres 0.65 --conf-thres 0.35 --img-size 640 640

📌 Note: The --end2end option must NOT be used to run the inference with the ZED SDK for compatibility reasons.

For a custom model model the weight file can be changed:

python export.py --weights ./yolov7_custom_model.pt --grid --simplify --topk-all 100 --iou-thres 0.65 --conf-thres 0.35 --img-size 512 512

Please refer to the corresponding documentation for more details https://github.com/WongKinYiu/yolov7/tree/main?tab=readme-ov-file#export

YOLOv5 #

Installing yolov5 #

YOLOv5 can be installed directly from pip using the following command:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

ONNX file export #

python export.py --weights yolov5s.pt --include onnx --imgsz 640

For a custom model model the weight file can be changed:

python export.py --weights yolov8l_custom_model.pt --include onnx

Please refer to the corresponding documentation for more details https://docs.ultralytics.com/yolov5/tutorials/model_export/