前言

您可以通过将YOLOv8 模型转换为ONNX 格式,扩大模型兼容性和部署灵活性。

安装ultralytics

要安装所需的软件包,请运行

# Install the required package for YOLOv8
pip install ultralytics

使用方法

  1. Python
from ultralytics import YOLO

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

# Export the model to ONNX format
model.export(format="onnx")  # creates 'yolov8n.onnx'

# Load the exported ONNX model
onnx_model = YOLO("yolov8n.onnx")

# Run inference
result = onnx_model.predict(source='<your img path>', save=True, imgsz=480)
print(result)
# 其中imgsz表示图片尺寸
  1. CLI
# Export a YOLOv8n PyTorch model to ONNX format
yolo export model=yolov8n.pt format=onnx  # creates 'yolov8n.onnx'

# Run inference with the exported model
yolo predict model=yolov8n.onnx source='https://ultralytics.com/images/bus.jpg'

点击了解更多 👈🏻