image_to_pixle_params_yoloSAM/ultralytics-main/examples/YOLOv8-ONNXRuntime
Yao Yi Zhe 8d61f7f78c first commit 2025-07-14 17:36:53 +08:00
..
README.md first commit 2025-07-14 17:36:53 +08:00
main.py first commit 2025-07-14 17:36:53 +08:00

README.md

YOLOv8 - ONNX Runtime

This repository provides an example implementation for running Ultralytics YOLOv8 models using the ONNX Runtime. This allows for efficient inference across various hardware platforms supporting the ONNX format.

⚙️ Installation

To get started, you'll need Python installed. Then, install the necessary dependencies.

Installing Required Dependencies

Clone the repository and install the packages listed in the requirements.txt file using pip:

git clone https://github.com/ultralytics/ultralytics.git
cd ultralytics/examples/YOLOv8-ONNXRuntime
pip install -r requirements.txt

Installing ONNX Runtime Backend

You need to choose the appropriate ONNX Runtime package based on your hardware.

GPU Acceleration (NVIDIA)

If you have an NVIDIA GPU and want to leverage CUDA for faster inference, install the onnxruntime-gpu package. Ensure you have the correct NVIDIA drivers and CUDA toolkit installed. Refer to the official ONNX Runtime GPU documentation for compatibility details.

pip install onnxruntime-gpu

CPU Only

If you don't have a compatible NVIDIA GPU or prefer CPU-based inference, install the standard onnxruntime package. Check the ONNX Runtime installation guide for more options.

pip install onnxruntime

🚀 Usage

Once the dependencies and the appropriate ONNX Runtime backend are installed, you can perform inference using the provided Python script.

Exporting Your Model

Before running inference, you need a YOLOv8 model in the ONNX format (.onnx). You can export your trained Ultralytics YOLOv8 models using the Ultralytics CLI or Python SDK. See the Ultralytics export documentation for detailed instructions.

Example export command:

yolo export model=yolov8n.pt format=onnx # Export yolov8n model to ONNX

Running Inference

Execute the main.py script with the path to your ONNX model and input image. You can also adjust the confidence and Intersection over Union (IoU) thresholds for object detection.

python main.py --model yolov8n.onnx --img image.jpg --conf-thres 0.5 --iou-thres 0.5
  • --model: Path to the YOLOv8 ONNX model file (e.g., yolov8n.onnx).
  • --img: Path to the input image (e.g., image.jpg).
  • --conf-thres: Confidence threshold for filtering detections. Only detections with a score higher than this value will be kept. Learn more about thresholds in the performance metrics guide.
  • --iou-thres: IoU threshold for Non-Maximum Suppression (NMS). Boxes with IoU greater than this threshold will be suppressed. See the NMS glossary entry for details.

The script will process the image, perform object detection, draw bounding boxes on the detected objects, and save the output image as output.jpg.

Contributing

Contributions to enhance this example or add new features are welcome! Please refer to the main Ultralytics repository for contribution guidelines. If you encounter issues or have suggestions, feel free to open an issue on the ONNX Runtime GitHub or the Ultralytics repository.