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

ONNX conversion error for opencv dnn #10665

Closed
2 tasks done
RizwanRumi opened this issue Jan 4, 2023 · 16 comments
Closed
2 tasks done

ONNX conversion error for opencv dnn #10665

RizwanRumi opened this issue Jan 4, 2023 · 16 comments
Labels
bug Something isn't working

Comments

@RizwanRumi
Copy link

RizwanRumi commented Jan 4, 2023

Search before asking

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

YOLOv5 Component

No response

Bug

Hi,

After converting the yolov5s.onnx (v6.1, v6.2) file, I am getting an error in opencv dnn. Why this is happening? Would you please tell me how to solve it? I am following this link

[ERROR:0@0.355] global onnx_importer.cpp:1054 cv::dnn::dnn4_v20221220::ONNXImporter::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 "E:...\Inspection\main.py", line 125, in
net = cv2.dnn.readNet(modelWeights)
cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\onnx\onnx_importer.cpp:1073: error: (-2:Unspecified error) in function 'cv::dnn::dnn4_v20221220::ONNXImporter::handleNode'

Node [Split@ai.onnx]:(onnx_node!/model.24/Split) parse error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\layers\slice_layer.cpp:274: error: (-215:Assertion failed) splits > 0 && inpShape[axis_rw] % splits == 0 in function 'cv::dnn::SliceLayerImpl::getMemoryShapes'

Environment

  • YOLO : YOLOV5
  • OS : Windows 10
  • Python: 3.9.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!
@RizwanRumi RizwanRumi added the bug Something isn't working label Jan 4, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Jan 4, 2023

👋 Hello @RizwanRumi, 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

YOLOv5 CI

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

@UNeedCryDear
Copy link

do_constant_folding=True, # WARNING: DNN inference with torch>=1.12 may require do_constant_folding=False

set this flag False and try again.

@RizwanRumi
Copy link
Author

Hello @UNeedCryDear,

Thank you for your reply. I have changed the flag value but it shows the same error.

@RizwanRumi
Copy link
Author

RizwanRumi commented Jan 5, 2023

Thanks to issues#8439 and #8744. My problem has been solved. I have installed the below versions in google colab and also edited export.py file : line 627 -> parser.add_argument('--opset', type=int, default=12, help='ONNX: opset version')
solution

@ZaWarudo111
Copy link

@RizwanRumi Thank you! Installing those versions of the packages worked for me! Then i just exported using python export.py --weights mymodel.pt --include onnx --opset 12

Thanks to issues#8439 and #8744. My problem has been solved. I have installed the below versions in google colab and also edited export.py file : line 627 -> parser.add_argument('--opset', type=int, default=12, help='ONNX: opset version') solution

@dbickson
Copy link

HI @glenn-jocher I am encountering same bug again, using google colab, torch=1.11.0, torchvision=0.12.0 onnx=1.12.0 and running export with --opset 12 still I am getting:

error: OpenCV(4.6.0) /io/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) /io/opencv/modules/dnn/src/layers/slice_layer.cpp:235: error: (-215:Assertion failed) splits > 0 && inpShape[axis_rw] % splits == 0 in function 'getMemoryShapes'

Same error on ubuntu 18 and also on MAC M1 12.2.1

@RizwanRumi
Copy link
Author

Hi @dbickson,

Try to downgrade the Opencv version and you can check issue #8439

@dbickson
Copy link

hi @RizwanRumi upgrading opencv python to 4.7 did not help either:

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.24/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'
> 

@UNeedCryDear
Copy link

Opencv does not support dynamic models. You need to export onnx without flag --dynamic
Use https://netron.app/ to open your onnx-model. What's the difference between yours and mine:
image

@oddmanru
Copy link

oddmanru commented Feb 6, 2023

@RizwanRumi Thank you for the finding. I've encourned the same issue and have confirmed that this workaround worked for me.
image
I have added the --opset 12 flag when running the export.py script. It is pretty straightforward and it works. Moreover, the current version of OpenCV is 4.6.0 with Python v3.8.10 on Colab. Apperently, they have made some upgrades on the Colab. Therefore, some people still need to recompile the dev version so that it could support CUDA environment on DNN.

@fenghuohuo2001
Copy link

I have solve the problem by "python export.py --weights yolov5s.pt --include onnx --opset 12"
my environment is "Python-3.8.0 torch-1.13.0+cu117"

@glenn-jocher
Copy link
Member

@fenghuohuo2001 thank you for letting us know that your issue has been resolved by using the command "python export.py --weights yolov5s.pt --include onnx --opset 12" and your environment is "Python 3.8.0 torch-1.13.0+cu117". We're glad that the solution worked for you. Please don't hesitate to reach out to us if you have any further questions or concerns.

@yunus-temurlenk
Copy link

Thanks to issues#8439 and #8744. My problem has been solved. I have installed the below versions in google colab and also edited export.py file : line 627 -> parser.add_argument('--opset', type=int, default=12, help='ONNX: opset version') solution

After long search this is fixed my issue too. you dont need to edit the code. just specify in the command opset value as 12:

python3 export.py --weights yolov5s.pt --img 640 --include onnx

Here the issue is package version. Hope opencv 5.0 will fix this dynamic issue

@glenn-jocher
Copy link
Member

@yunus-temurlenk i'm glad to hear that specifying the opset value as 12 in the export command resolved your issue without needing to edit the code. It's always great when a solution simplifies the process. We're looking forward to potential fixes in future OpenCV releases as well. If you have any more questions or run into other issues, feel free to ask. Happy coding! 😊👍

@shubhamg2205
Copy link

I am using custom yolov8 for object detection. If the pre-train the yolov8n model it's working fine with opencv dnn framework using Yolov8CppInference. But in case of yolov8s it shows the following error.

#5 0x0000000000493716 in cv::error(int, std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > const&, char const*, char const*, int) ()
#6 0x000000000061511d in cv::dnn::dnn4_v20221220::ONNXImporter::ONNXImporter(cv::dnn::dnn4_v20221220::Net&, char const*) ()
#7 0x000000000061525c in cv::dnn::dnn4_v20221220::Net cv::dnn::dnn4_v20221220::detail::readNet<cv::dnn::dnn4_v20221220::ONNXImporter, char const*>(char const*&&) ()
#8 0x00000000006152ba in cv::dnn::dnn4_v20221220::readNetFromONNX(std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > const&) ()
#9 0x00000000005ac4a4 in cv::dnn::dnn4_v20221220::readNet(std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > const&, std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > const&, std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > const&) ()

@glenn-jocher
Copy link
Member

@shubhamg2205 it seems like you're encountering an issue with OpenCV's DNN module when trying to use a custom YOLOv8 model. The error message you've provided indicates a problem during the import of the ONNX model into OpenCV, but it doesn't give specific details about the cause of the error.

Here are a few general suggestions that might help you troubleshoot the issue:

  1. Check ONNX Model Compatibility: Ensure that the ONNX model is compatible with the version of OpenCV you are using. Some newer features in ONNX may not be supported by older versions of OpenCV.

  2. Validate ONNX Model: Use ONNX tools (like onnx.checker.check_model) to validate the ONNX model and ensure there are no internal inconsistencies or errors.

  3. Use Supported Opset: When exporting the model to ONNX, make sure to use an opset version that is supported by OpenCV. You can specify the opset version during the export process.

  4. Update OpenCV: If you're not using the latest version of OpenCV, consider updating to the latest version, as it may have better support for ONNX models.

  5. Inspect the Model: Use tools like Netron (https://netron.app) to visually inspect the ONNX model and identify any unusual layers or structures that might be causing the import to fail.

  6. Check OpenCV Build: If you're building OpenCV from source, ensure that it's built with ONNX support enabled.

  7. Review Error Logs: If there are more detailed error logs or a stack trace, review them to pinpoint where the error is occurring.

  8. Test with a Known Good Model: Try using a known good YOLOv8 model that works with OpenCV to determine if the issue is with the model or the setup.

If you continue to face issues, consider reaching out to the OpenCV community or the maintainers of the YOLOv8CppInference project for more targeted support.

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

No branches or pull requests

9 participants