# Error when reading Yolo5 as onnx using cv2

**URL:** <https://forum.opencv.org/t/error-when-reading-yolo5-as-onnx-using-cv2/11507>\
**Category:** Python\
**Tags:** dnn, onnx, yolov5\
**Created:** [January 5, 2023, 8:14pm UTC](https://forum.opencv.org/t/error-when-reading-yolo5-as-onnx-using-cv2/11507 "2023-01-05T20:14:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![diasm](https://avatars.discourse-cdn.com/v4/letter/d/d26b3c/32.png) [@diasm](https://forum.opencv.org/u/diasm)\
**Post date:** [January 5, 2023, 8:14pm UTC](https://forum.opencv.org/t/error-when-reading-yolo5-as-onnx-using-cv2/11507/1 "2023-01-05T20:14:02Z")

</div>

Hello everyone!

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

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

```

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

```auto
[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?

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [January 5, 2023, 8:50pm UTC](https://forum.opencv.org/t/error-when-reading-yolo5-as-onnx-using-cv2/11507/2 "2023-01-05T20:50:27Z")

</div>

> [@diasm](#):
>
> the yolo5 model as onnx

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

---

<div class="post-metadata">

**Author:** ![diasm](https://avatars.discourse-cdn.com/v4/letter/d/d26b3c/32.png) [@diasm](https://forum.opencv.org/u/diasm)\
**Post date:** [January 5, 2023, 9:11pm UTC](https://forum.opencv.org/t/error-when-reading-yolo5-as-onnx-using-cv2/11507/3 "2023-01-05T21:11:03Z")

</div>

I used Google Colab for everything

1. !git clone [GitHub - ultralytics/yolov5: YOLOv5 🚀 in PyTorch \> ONNX \> CoreML \> TFLite](https://github.com/ultralytics/yolov5.git) - 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!

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [January 6, 2023, 9:31am UTC](https://forum.opencv.org/t/error-when-reading-yolo5-as-onnx-using-cv2/11507/4 "2023-01-06T09:31:03Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![diasm](https://avatars.discourse-cdn.com/v4/letter/d/d26b3c/32.png) [@diasm](https://forum.opencv.org/u/diasm)\
**Post date:** [January 8, 2023, 11:49pm UTC](https://forum.opencv.org/t/error-when-reading-yolo5-as-onnx-using-cv2/11507/5 "2023-01-08T23:49:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [January 9, 2023, 8:10am UTC](https://forum.opencv.org/t/error-when-reading-yolo5-as-onnx-using-cv2/11507/6 "2023-01-09T08:10:05Z")

</div>

> [@diasm](#):
>
> What is opset?

the onnx api version

> [@diasm](#):
>
> Why did you think that was the solution?

found while scanning ultralytics github issues …

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

---

<div class="post-metadata">

**Author:** ![Satish\_Saini](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/satish_saini/32/7666_2.png) [@Satish\_Saini](https://forum.opencv.org/u/Satish_Saini)\
**Post date:** [April 12, 2023, 4:02pm UTC](https://forum.opencv.org/t/error-when-reading-yolo5-as-onnx-using-cv2/11507/7 "2023-04-12T16:02:43Z")

</div>

finally , I got the solution .  
Thanks Berak 🤩
