Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error loading ONNX model in opencv #8439

Closed
1 of 2 tasks
Neskelogth opened this issue Jul 2, 2022 · 16 comments
Closed
1 of 2 tasks

Error loading ONNX model in opencv #8439

Neskelogth opened this issue Jul 2, 2022 · 16 comments
Labels
bug Something isn't working Stale

Comments

@Neskelogth
Copy link

Search before asking

  • I have searched the YOLOv5 issues and found no similar bug report.

YOLOv5 Component

No response

Bug

I tried to load the onnx model yolov5s.onnx on opencv after exporting it from the .pt file found in the release.
The command I used to export it is
python export.py --weights yolov5s.pt --include onnx
as seen in the instructions of the release. The problem is that if I try to load the model in opencv I get this error
[ERROR:0@14.876] global D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\onnx\onnx_importer.cpp (1021) cv::dnn::dnn4_v20220524::ONNXImporter::handleNode DNN/ONNX: ERROR during processing node with 1 inputs and 1 outputs: [Identity]:(onnx_node!Identity_0) from domain='ai.onnx' Traceback (most recent call last): File "D:\Unitn\Corsi\Computer vision\project\synopsis\main.py", line 24, in <module> bg_frames = find_bg_frames(video, duration, fps) File "D:\Unitn\Corsi\Computer vision\project\synopsis\utils.py", line 152, in find_bg_frames people_found = find_people_with_yolo(frame) File "D:\Unitn\Corsi\Computer vision\project\synopsis\utils.py", line 102, in find_people_with_yolo net = cv2.dnn.readNet(weights) cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\onnx\onnx_importer.cpp:1040: error: (-2:Unspecified error) in function 'cv::dnn::dnn4_v20220524::ONNXImporter::handleNode' Node [Identity@ai.onnx]:(onnx_node!Identity_0) parse error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\layer.cpp:246: error: (-215:Assertion failed) inputs.size() in function 'cv::dnn::dnn4_v20220524::Layer::getMemoryShapes'
While downloading a onnx file from this link the problem does not appear. I hope it's clear enough

Environment

-YOLO: YOLOv5s
-OS: Windows 10
-Python: 3.10.0
-OpenCV: 4.6.0

Minimal Reproducible Example

No response

Additional

No response

Are you willing to submit a PR?

  • Yes I'd like to help by submitting a PR!
@Neskelogth Neskelogth added the bug Something isn't working label Jul 2, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Jul 2, 2022

👋 Hello @Neskelogth, thank you for your interest in YOLOv5 🚀! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://ultralytics.com or email support@ultralytics.com.

Requirements

Python>=3.7.0 with all requirements.txt installed including PyTorch>=1.7. To get started:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), validation (val.py), inference (detect.py) and export (export.py) on macOS, Windows, and Ubuntu every 24 hours and on every commit.

@glenn-jocher
Copy link
Member

glenn-jocher commented Jul 2, 2022

@Neskelogth see Export tutorial for OpenCV DNN and C++ details.

OpenCV DNN inference

OpenCV inference with ONNX models:

python export.py --weights yolov5s.pt --include onnx

python detect.py --weights yolov5s.onnx --dnn  # detect
python val.py --weights yolov5s.onnx --dnn  # validate

C++ Inference

YOLOv5 OpenCV DNN C++ inference on exported ONNX model examples:

YOLOv5 OpenVINO C++ inference examples:

YOLOv5 Tutorials

Good luck 🍀 and let us know if you have any other questions!

@zouzhen
Copy link

zouzhen commented Jul 5, 2022

I had the same problem.
Environment
-YOLO: YOLOv5s
-OS: Windows 10
-Python: 3.10.0
-OpenCV: 4.5.5.64

@msly
Copy link

msly commented Jul 5, 2022

use pytorch 1.11 solved (before is 1.12 has same error)

@zouzhen
Copy link

zouzhen commented Jul 6, 2022

@msly Thanks, I'm ready to run like you said,

@lus-zzz
Copy link

lus-zzz commented Jul 11, 2022

solved my problem

@Damix48
Copy link

Damix48 commented Jul 20, 2022

use pytorch 1.11 solved (before is 1.12 has same error)

Before exporting or before training?

@niniack
Copy link

niniack commented Jul 22, 2022

before exporting.

I tried exporting with 1.12, found this issue, downgraded, and tried detecting/training with 1.11 but it failed.

Then did exporting and detecting both with 1.11 and it worked!

@github-actions
Copy link
Contributor

github-actions bot commented Aug 22, 2022

👋 Hello, this issue has been automatically marked as stale because it has not had recent activity. Please note it will be closed if no further activity occurs.

Access additional YOLOv5 🚀 resources:

Access additional Ultralytics ⚡ resources:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLOv5 🚀 and Vision AI ⭐!

@tsvetiko
Copy link

Using opencv-python==4.6.0.66 solved the issue for me!

@wvalcke
Copy link

wvalcke commented Nov 26, 2022

Which pytorch version ? 1.11 or 1.12, or ... 1.13 or ...?
What is now the recommended pytorch version for yolov5 which supports all features for export/train/detect....
thanks !

@wvalcke
Copy link

wvalcke commented Nov 26, 2022

I just tested with opencv-python 4.6.0.66: this does NOT work, the error is the same as with the C++ test i've done.
I did an export of a yolo5s model to onnx using pytorch 1.12.1.
It is said this works with pytorch 1.11, but than it has problems with training, (see above the discussion), is this correct?
Does anyone actually knows which versions work an all situations?

@0xBYTESHIFT
Copy link

I got "handleNode DNN/ONNX: ERROR during processing node with 1 inputs and 1 outputs" after exporting using a colab, provided in a readme. This is a mess...

@glenn-jocher
Copy link
Member

#10368

@vlarobbyk
Copy link

Hello. To fix the error you must to export the PyTorch model to ONNX adding the paramter --opset 12. The complete command will look like as following:

python export.py --weights models/yolov5n.pt --opset 12 --simplify --include onnx

@glenn-jocher
Copy link
Member

@vlarobbyk thanks for sharing this solution! We appreciate your contribution to the community. 🙌 If you encounter any additional issues or have further insights, feel free to share them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Stale
Projects
None yet
Development

No branches or pull requests