# Error using PyTorch Yolo5 model after conversion to ONNX

**URL:** https://forum.opencv.org/t/error-using-pytorch-yolo5-model-after-conversion-to-onnx/24022
**Category:** Python
**Tags:** dnn, onnx
**Created:** [September 14, 2025, 9:53pm UTC](https://forum.opencv.org/t/error-using-pytorch-yolo5-model-after-conversion-to-onnx/24022 "2025-09-14T21:53:43Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![bluedalmatian](https://avatars.discourse-cdn.com/v4/letter/b/c2a13f/32.png) [@bluedalmatian](https://forum.opencv.org/u/bluedalmatian)
#### Post date: [September 14, 2025, 9:53pm UTC](https://forum.opencv.org/t/error-using-pytorch-yolo5-model-after-conversion-to-onnx/24022/1 "2025-09-14T21:53:43Z")

</div>

Im trying to use a custom trained Yolo5 model which Ive then converted to ONNX to use with OpenCV.

The error Im getting is outputs = net.forward(net.getUnconnectedOutLayersNames())  
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
cv2.error: OpenCV(4.6.0) ./modules/dnn/include/opencv2/dnn/shape\_utils.hpp:170: error: (-215:Assertion failed) start \<= (int)shape.size() && end \<= (int)shape.size() && start \<= end in function ‘total’

After googling I can find several people with similar issue which suggests its caused by the conversion but none of those posts have a solution.

I did the conversion like this: python.exe .\export.py --weights c:\Users\Andrew\best.pt --include onnx

And the output from that indicated all went OK:

(.venv) PS C:\Users\Andrew\PycharmProjects\Yolo5Trainer\yolov5\> python.exe .\export.py --weights c:\Users\Andrew\best.pt --include onnx --simplify  
export: data=C:\Users\Andrew\PycharmProjects\Yolo5Trainer\yolov5\data\coco128.yaml, weights=[‘c:\Users\Andrew\best.pt’], imgsz=[640, 640], batch\_size=1, device=cpu, half=False, inplace=False, keras=False, optimize=False, int8  
=False, per\_tensor=False, dynamic=False, cache=, simplify=True, mlmodel=False, opset=17, verbose=False, workspace=4, nms=False, agnostic\_nms=False, topk\_per\_class=100, topk\_all=100, iou\_thres=0.45, conf\_thres=0.25, include=[‘onnx’]  
YOLOv5 v7.0-430-g459d8bf0 Python-3.12.4 torch-2.8.0+cpu CPU

Fusing layers…  
Model summary: 157 layers, 1768636 parameters, 0 gradients, 4.2 GFLOPs

PyTorch: starting from c:\Users\Andrew\best.pt with output shape (1, 25200, 12) (3.7 MB)#  
ONNX: starting export with onnx 1.19.0…

ONNX: slimming with onnxslim 0.1.68…  
ONNX: export success 15.0s, saved as c:\Users\Andrew\best.onnx (7.2 MB)

Export complete (15.4s)  
Results saved to C:\Users\Andrew  
Detect: python detect.py --weights c:\Users\Andrew\best.onnx  
Validate: python val.py --weights c:\Users\Andrew\best.onnx  
PyTorch Hub: model = torch.hub.load(‘ultralytics/yolov5’, ‘custom’, ‘c:\Users\Andrew\best.onnx’)  
Visualize: [https://netron.app](https://netron.app)

Im using OpenCV 4.6 on Raspberry Pi OS 12

Is anyone able to give me pointers as to how I can debug this further please?
