当前位置:   article > 正文

cv2.dnn.readNetFromONNX读取yolov8的onnx报错解决过程_readnetfromonnx 报错

readnetfromonnx 报错
  1. Loading /home/inference/Amplitudemode_AI/all_model_and_pred/xxx/segment/train3/weights/last.onnx for ONNX OpenCV DNN inference...
  2. [ERROR:0@3.062] global onnx_importer.cpp:1051 handleNode DNN/ONNX: ERROR during processing node with 2 inputs and 2 outputs: [Split]:(onnx_node!/model.22/Split) from domain='ai.onnx'
  3. Traceback (most recent call last):
  4. File "/home/inference/Amplitudemode_AI/all_model_and_pred/AI_Ribfrac_ths/onnx_test_seg/infer-seg.py", line 167, in <module>
  5. model = AutoBackend(weights="/home/inference/Amplitudemode_AI/all_model_and_pred/xxx/segment/train3/weights/last.onnx", dnn=True)
  6. File "/home/inference/miniconda3/envs/yolov8/lib/python3.10/site-packages/ultralytics/nn/autobackend.py", line 124, in __init__
  7. net = cv2.dnn.readNetFromONNX(w)
  8. cv2.error: OpenCV(4.7.0) /io/opencv/modules/dnn/src/onnx/onnx_importer.cpp:1073: error: (-2:Unspecified error) in function 'handleNode'
  9. > Node [Split@ai.onnx]:(onnx_node!/model.22/Split) parse error: OpenCV(4.7.0) /io/opencv/modules/dnn/src/layers/slice_layer.cpp:274: error: (-215:Assertion failed) splits > 0 && inpShape[axis_rw] % splits == 0 in function 'getMemoryShapes'
  10. >

上述是尝试用opencv读取模型时的报错信息。

接着去github上的yolov8官方项目的问题区搜索,经过尝试最终搜索关键字如下:

ONNX DNN  splits > 0 && inpShape[axis_rw] % splits == 0 in function 'getMemoryShapes 

找到对应问题如下:Exported ONNX cannot be opened in OpenCV · Issue #226 · ultralytics/ultralytics · GitHubSearch before asking I have searched the YOLOv8 issues and found no similar bug report. YOLOv8 Component Export Bug I created a custom object detector and export to ONNX. Attempting to load it in OpenCV results in the following error: `[...icon-default.png?t=N7T8https://github.com/ultralytics/ultralytics/issues/226

 找到解决方法如下转换时要设置(关键是添加opset=11)

yolo mode=export model=runs/detect/train/weights/best.pt imgsz=[640,640] format=onnx opset=11

实际转化代码如下:

  1. from ultralytics import YOLO
  2. model = YOLO(
  3. "/home/inference/Amplitudemode_AI/all_model_and_pred/xxx/segment/train3/weights/last.pt")
  4. success = model.export(format="onnx", opset=11, simplify=True) # export the model to onnx format
  5. assert success

用转换好的onnx调用官方api推理如下:

  1. from ultralytics import YOLO
  2. model = YOLO("/home/inference/Amplitudemode_AI/all_model_and_pred/xxx/segment/train3/weights/last.onnx") # 模型加载
  3. results = model.predict(
  4. source='/home/inference/tt',imgsz=640, save=True, boxes=False) # save plotted images 保存绘制图片

正常推理成功。

ps 2024年2月22日

用官方api推理少了关键dnn=True的配置,添加时会报错。

故而我用官方的模型去测试先转onnx如下代码:

  1. # -*-coding:utf-8-*-
  2. from ultralytics import YOLO
  3. # Load a model
  4. model = YOLO('yolov8n-seg.pt') # load an official model
  5. # Export the model
  6. model.export(format='onnx', opset=12)

这里用opset=12,是发现官方实例里升级到12了https://github.com/ultralytics/ultralytics/tree/main/examples/YOLOv8-OpenCV-ONNX-Python

命令行测试推理如下报错:

  1. yolo predict task=segment model=yolov8n-seg.onnx imgsz=640 dnn
  2. WARNING ⚠️ 'source' is missing. Using default 'source=/home/inference/miniconda3/envs/yolov8v2/lib/python3.9/site-packages/ultralytics/assets'.
  3. Ultralytics YOLOv8.1.17
    声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/599369
    推荐阅读
    相关标签