赞
踩
- Loading /home/inference/Amplitudemode_AI/all_model_and_pred/xxx/segment/train3/weights/last.onnx for ONNX OpenCV DNN inference...
- [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'
- Traceback (most recent call last):
- File "/home/inference/Amplitudemode_AI/all_model_and_pred/AI_Ribfrac_ths/onnx_test_seg/infer-seg.py", line 167, in <module>
- model = AutoBackend(weights="/home/inference/Amplitudemode_AI/all_model_and_pred/xxx/segment/train3/weights/last.onnx", dnn=True)
- File "/home/inference/miniconda3/envs/yolov8/lib/python3.10/site-packages/ultralytics/nn/autobackend.py", line 124, in __init__
- net = cv2.dnn.readNetFromONNX(w)
- cv2.error: OpenCV(4.7.0) /io/opencv/modules/dnn/src/onnx/onnx_importer.cpp:1073: error: (-2:Unspecified error) in function 'handleNode'
- > 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'
- >
上述是尝试用opencv读取模型时的报错信息。
接着去github上的yolov8官方项目的问题区搜索,经过尝试最终搜索关键字如下:
ONNX DNN splits > 0 && inpShape[axis_rw] % splits == 0 in function 'getMemoryShapes
找到解决方法如下转换时要设置(关键是添加opset=11)
yolo mode=export model=runs/detect/train/weights/best.pt imgsz=[640,640] format=onnx opset=11
实际转化代码如下:
- from ultralytics import YOLO
-
- model = YOLO(
- "/home/inference/Amplitudemode_AI/all_model_and_pred/xxx/segment/train3/weights/last.pt")
- success = model.export(format="onnx", opset=11, simplify=True) # export the model to onnx format
- assert success
用转换好的onnx调用官方api推理如下:
- from ultralytics import YOLO
- model = YOLO("/home/inference/Amplitudemode_AI/all_model_and_pred/xxx/segment/train3/weights/last.onnx") # 模型加载
- results = model.predict(
- source='/home/inference/tt',imgsz=640, save=True, boxes=False) # save plotted images 保存绘制图片
正常推理成功。
ps 2024年2月22日
用官方api推理少了关键dnn=True的配置,添加时会报错。
故而我用官方的模型去测试先转onnx如下代码:
- # -*-coding:utf-8-*-
- from ultralytics import YOLO
- # Load a model
- model = YOLO('yolov8n-seg.pt') # load an official model
- # Export the model
- model.export(format='onnx', opset=12)
这里用opset=12,是发现官方实例里升级到12了https://github.com/ultralytics/ultralytics/tree/main/examples/YOLOv8-OpenCV-ONNX-Python
命令行测试推理如下报错:
- yolo predict task=segment model=yolov8n-seg.onnx imgsz=640 dnn
- WARNING ⚠️ 'source' is missing. Using default 'source=/home/inference/miniconda3/envs/yolov8v2/lib/python3.9/site-packages/ultralytics/assets'.
- Ultralytics YOLOv8.1.17 声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/599369推荐阅读
相关标签
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。