问题描述:
当我们训练好yolov8模型时,如何将pt文件转化为ONNX格式
代码示例:
ultralytics官方提供了pt转onnx的接口
'''将pt转化为onnx
onnx = 1.16.0
opset = 12
'''
from ultralytics import YOLO
class Yolov8Pt2Onnx:
def __init__(self, pt_path) -> None:
self.convert(pt_path)
def convert(self, pt_path):
model = YOLO(pt_path)
model.export(format = "onnx", opset=12)
if __name__ == "__main__":
pt_path = r"yolov8n.pt"
Yolov8Pt2Onnx(pt_path)
- 转换完成的onnx文件默认保存在pt文件目录下
- export参数详解:https://blog.csdn.net/qq_42761751/article/details/140825169?spm=1001.2014.3001.5501