重现步骤
模型转换过程(yolo_v5) D:\bishe\yolov5-6.0\yolov5-6.0>python export.py --weight best.pt --imgsz 640 --batch 1 --include onnx --simplify --opset 12 --dynamic
export: data=data\mydata.yaml, weights=best.pt, imgsz=[640], batch_size=1, device=cpu, half=False, inplace=False, train=False, optimize=False, int8=False, dynamic=True, simplify=True, opset=12, topk_per_class=100, topk_all=100, iou_thres=0.45, conf_thres=0.25, include=['onnx']
YOLOv5 2025-3-29 torch 1.8.1+cu111 CPU
Fusing layers...
Model Summary: 213 layers, 1760518 parameters, 0 gradients, 4.1 GFLOPs
PyTorch: starting from best.pt (3.9 MB)
ONNX: starting export with onnx 1.9.0...
D:\bishe\yolov5-6.0\yolov5-6.0\models\yolo.py:60: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
if self.grid[i].shape[2:4] != x[i].shape[2:4] or self.onnx_dynamic:
ONNX: simplifying with onnx-simplifier 0.3.6...
ONNX: export success, saved as best.onnx (7.1 MB)
ONNX: run --dynamic ONNX model inference with: 'python detect.py --weights best.onnx'
Export complete (2.85s)
Results saved to D:\bishe\yolov5-6.0\yolov5-6.0
Visualize with https://netron.app
(yolo_v5) D:\bishe\yolov5-6.0\yolov5-6.0>cd /d D:\bishe\yolov5-6.0\yolov5-6.0\kmodel
(yolo_v5) D:\bishe\yolov5-6.0\yolov5-6.0\kmodel>python to_kmodel.py --model best.onnx --dataset calibration_images --input_width 640 --input_height 640 --target k230 --ptq_option 0
warn: Nncase.Hosting.PluginLoader[0]
NNCASE_PLUGIN_PATH is not set.
运行的代码
from libs.YOLO import YOLOv5
import os,sys,gc
import ulab.numpy as np
import image
# 从本地读入图片,并实现HWC转CHW
def read_img(img_path):
img_data = image.Image(img_path)
img_data_rgb888=img_data.to_rgb888()
img_hwc=img_data_rgb888.to_numpy_ref()
shape=img_hwc.shape
img_tmp = img_hwc.reshape((shape[0] * shape[1], shape[2]))
img_tmp_trans = img_tmp.transpose()
img_res=img_tmp_trans.copy()
img_return=img_res.reshape((shape[2],shape[0],shape[1]))
return img_return,img_data_rgb888
if __name__=="__main__":
img_path="/data/test.jpg"
kmodel_path="/data/best.kmodel"
labels = ["head"]
confidence_threshold = 0.3
nms_threshold=0.45
model_input_size=[640,640]
img,img_ori=read_img(img_path)
rgb888p_size=[img.shape[2],img.shape[1]]
# 初始化YOLOv5实例
yolo=YOLOv5(task_type="detect",mode="image",kmodel_path=kmodel_path,labels=labels,rgb888p_size=rgb888p_size,model_input_size=model_input_size,conf_thresh=confidence_threshold,nms_thresh=nms_threshold,max_boxes_num=50,debug_mode=0)
yolo.config_preprocess()
try:
res=yolo.run(img)
yolo.draw_result(res,img_ori)
gc.collect()
except Exception as e:
sys.print_exception(e)
finally:
yolo.deinit()
期待结果和实际结果
软硬件版本信息
错误日志
尝试解决过程
补充材料