Error when reading Yolo5 as onnx using cv2

Hello everyone!

I try to load the yolo5 model as onnx into the cv2 using the following line:

yolo = cv2.dnn.readNetFromONNX('Model/weights/best.onnx')

However, when I run the code, I receive this big error chunk:

[ERROR:0@0.673] global /Users/runner/work/opencv-python/opencv-python/opencv/modules/dnn/src/onnx/onnx_importer.cpp (1021) handleNode DNN/ONNX: ERROR during processing node with 2 inputs and 3 outputs: [Split]:(onnx_node!/model.24/Split) from domain='ai.onnx'
Traceback (most recent call last):
  File "/Users/diasmashikov/Desktop/object-detection-yolo/yolo_predictions.py", line 17, in <module>
    yolo = cv2.dnn.readNetFromONNX('Model/weights/best.onnx')
cv2.error: OpenCV(4.6.0) /Users/runner/work/opencv-python/opencv-python/opencv/modules/dnn/src/onnx/onnx_importer.cpp:1040: error: (-2:Unspecified error) in function 'handleNode'
> Node [Split@ai.onnx]:(onnx_node!/model.24/Split) parse error: OpenCV(4.6.0) /Users/runner/work/opencv-python/opencv-python/opencv/modules/dnn/src/layers/slice_layer.cpp:235: error: (-215:Assertion failed) splits > 0 && inpShape[axis_rw] % splits == 0 in function 'getMemoryShapes'

My Python version is 3.9.13
I tried OpenCV(installed using pip install opencv-python) versions: 4.40.44, 4.6.0.66, 4.7.0.x

Still, none of them work. Does anyone know to solve the problem?

hi there, please explain, where & how you got that model

I used Google Colab for everything

  1. !git clone GitHub - ultralytics/yolov5: YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite - Cloned the GitHub repo
  2. !python train.py --data data.yaml --cfg yolov5s.yaml --batch-size 8 --name Model --epochs 50 - Initiated the training process for Yolo5
  3. !python export.py --weights runs/train/Model/weights/best.pt --include onnx --simplify - Converted the PyTorch model(yolo) to onnx

If you need more details on each thing I am glad to provide any information!

1 Like

can you try to add

--opset 12

to the export params ?

(iirc, you have to downgrade the opset from 13 to 12, to make the onnx work with cv2.dnn)

3 Likes

Oh my god man! It worked! What is opset? Why did you think that was the solution?

the onnx api version

found while scanning ultralytics github issues …

see, they added a special hack to make it work with dnn, but v13 broke it

finally , I got the solution .
Thanks Berak :star_struck: