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 occurred with python detect.py --weights yolov5s.onnx --dnn whose yolov5s.onnx was exported with export.py #10575

Closed
1 of 2 tasks
ChrisVicky opened this issue Dec 23, 2022 · 7 comments
Labels
bug Something isn't working

Comments

@ChrisVicky
Copy link

Search before asking

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

YOLOv5 Component

Export

Bug

When I run detect.py with yolov5s.onnx, Error occurred.

python detect.py --weights yolov5s.onnx --dnn
detect: weights=['yolov5s.onnx'], source=data/images, data=data/coco128.yaml, imgsz=[640, 640], conf_thres=0.25, iou_thres=0.45, max_det=1000, device=, view_img=False, save_txt=False, save_conf=False, save_crop=False, nosave=False, classes=None, agnostic_nms=False, augment=False, visualize=False, update=False, project=runs/detect, name=exp, exist_ok=False, line_thickness=3, hide_labels=False, hide_conf=False, half=False, dnn=True, vid_stride=1
YOLOv5 🚀 v7.0-47-g2370a55 Python-3.10.8 torch-1.13.1+cu117 CPU

Loading yolov5s.onnx for ONNX OpenCV DNN inference...
[ERROR:0@1.404] global /io/opencv/modules/dnn/src/onnx/onnx_importer.cpp (1018) 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 "/home/christopher/Coding/YOLO/NEW/yolov5-tmp/detect.py", line 261, in <module>
    main(opt)
  File "/home/christopher/Coding/YOLO/NEW/yolov5-tmp/detect.py", line 256, in main
    run(**vars(opt))
  File "/home/christopher/.anaconda3/envs/yolov5-backup/lib/python3.10/site-packages/torch/autograd/grad_mode.py", line 27, in decorate_context
    return func(*args, **kwargs)
  File "/home/christopher/Coding/YOLO/NEW/yolov5-tmp/detect.py", line 98, in run
    model = DetectMultiBackend(weights, device=device, dnn=dnn, data=data, fp16=half)
  File "/home/christopher/Coding/YOLO/NEW/yolov5-tmp/models/common.py", line 363, in __init__
    net = cv2.dnn.readNetFromONNX(w)
cv2.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'
>

PS: The yolov5s.onnx was exported with python export.py --weights yolov5s.pn --include onnx

Environment

- YOLO: YOLO v7.0-47-g2370a55 Python-3.10.8 torch-1.13.1+cu117 CPU [Cloned at (2022-12-23)]
- OS: Arch Linux x86_64 - 6.0.12-arch1-1
- Python: Python 3.10.8
- Anaconda: conda 22.11.1

Minimal Reproducible Example

git clone git@github.com:ultralytics/yolov5.git yolov5-tmp
cd yolov5-tmp
conda create --name yolov5-tmp
conda activate yolov5-tmp
conda install pip
pip install -r requirements.txt
python export.py --weights yolov5s.pt --include onnx
python detect.py --weights yolov5s.onnx --dnn

Additional

Success with another .onnx model.

I did some search and tried detect.py with another .onnx model downloaded from This Repo which is recommended in Issue 251

python detect.py --weights yolov5s-cpp.onnx --dnn

The output seems ok.

detect: weights=['yolov5s-cpp.onnx'], source=data/images, data=data/coco128.yaml, imgsz=[640, 640], conf_thres=0.25, iou_thres=0.45, max_det=1000, device=, view_img=False, save_txt=False, save_conf=False, save_crop=False, nosave=False, classes=None, agnostic_nms=False, augment=False, visualize=False, update=False, project=runs/detect, name=exp, exist_ok=False, line_thickness=3, hide_labels=False, hide_conf=False, half=False, dnn=True, vid_stride=1
YOLOv5 🚀 v7.0-47-g2370a55 Python-3.10.8 torch-1.13.1+cu117 CPU

Loading yolov5s-cpp.onnx for ONNX OpenCV DNN inference...
image 1/2 /home/christopher/Coding/YOLO/NEW/yolov5-tmp/data/images/bus.jpg: 640x640 4 persons, 1 bus, 415.7ms
image 2/2 /home/christopher/Coding/YOLO/NEW/yolov5-tmp/data/images/zidane.jpg: 640x640 2 persons, 2 ties, 403.0ms
Speed: 1.5ms pre-process, 409.4ms inference, 0.8ms NMS per image at shape (1, 3, 640, 640)
Results saved to runs/detect/exp2

Success without --dnn option

Addtionally, I tried to run detect.py on the exported .onnx model without --dnn option and it works fine.

python detect.py --weights yolov5s.onnx

Results:

detect: weights=['yolov5s.onnx'], source=data/images, data=data/coco128.yaml, imgsz=[640, 640], conf_thres=0.25, iou_thres=0.45, max_det=1000, device=, view_img=False, save_txt=False, save_conf=False, save_crop=False, nosave=False, classes=None, agnostic_nms=False, augment=False, visualize=False, update=False, project=runs/detect, name=exp, exist_ok=False, line_thickness=3, hide_labels=False, hide_conf=False, half=False, dnn=False, vid_stride=1
YOLOv5 🚀 v7.0-47-g2370a55 Python-3.10.8 torch-1.13.1+cu117 CPU

Loading yolov5s.onnx for ONNX Runtime inference...
image 1/2 /home/christopher/Coding/YOLO/NEW/yolov5-tmp/data/images/bus.jpg: 640x640 4 persons, 1 bus, 114.7ms
image 2/2 /home/christopher/Coding/YOLO/NEW/yolov5-tmp/data/images/zidane.jpg: 640x640 2 persons, 2 ties, 106.9ms
Speed: 1.2ms pre-process, 110.8ms inference, 1.1ms NMS per image at shape (1, 3, 640, 640)
Results saved to runs/detect/exp3
  • Therefore, I assume the Problem lies in the export.py file. However, I'm not capable to debug the file and figure it out.

Are you willing to submit a PR?

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

github-actions bot commented Dec 23, 2022

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

@AyushExel
Copy link
Contributor

@glenn-jocher do you know about this issue?

@ChrisVicky
Copy link
Author

Thanks to Issue #8439 , I managed to export .onnx models that is compatible with --dnn option with the following environments.

  • python version:
Python 3.8.15
  • pip list:
Package                 Version
----------------------- --------------------
absl-py                 1.3.0
asttokens               2.2.1
backcall                0.2.0
cachetools              5.2.0
certifi                 2022.12.7
charset-normalizer      2.1.1
contourpy               1.0.6
cycler                  0.11.0
decorator               5.1.1
executing               1.2.0
fonttools               4.38.0
gitdb                   4.0.10
GitPython               3.1.29
google-auth             2.15.0
google-auth-oauthlib    0.4.6
grpcio                  1.51.1
idna                    3.4
importlib-metadata      5.2.0
ipython                 8.7.0
jedi                    0.18.2
kiwisolver              1.4.4
Markdown                3.4.1
MarkupSafe              2.1.1
matplotlib              3.6.2
matplotlib-inline       0.1.6
numpy                   1.24.0
oauthlib                3.2.2
onnx                    1.12.0
opencv-python           4.6.0.66
packaging               22.0
pandas                  1.5.2
parso                   0.8.3
pexpect                 4.8.0
pickleshare             0.7.5
Pillow                  9.3.0
pip                     22.3.1
prompt-toolkit          3.0.36
protobuf                3.20.1
psutil                  5.9.4
ptyprocess              0.7.0
pure-eval               0.2.2
pyasn1                  0.4.8
pyasn1-modules          0.2.8
Pygments                2.13.0
pyparsing               3.0.9
python-dateutil         2.8.2
pytz                    2022.7
PyYAML                  6.0
requests                2.28.1
requests-oauthlib       1.3.1
rsa                     4.9
scipy                   1.9.3
seaborn                 0.12.1
setuptools              65.5.0
six                     1.16.0
smmap                   5.0.0
stack-data              0.6.2
tensorboard             2.11.0
tensorboard-data-server 0.6.1
tensorboard-plugin-wit  1.8.1
thop                    0.1.1.post2209072238
torch                   1.11.0
torchvision             0.12.0
tqdm                    4.64.1
traitlets               5.8.0
typing_extensions       4.4.0
urllib3                 1.26.13
wcwidth                 0.2.5
Werkzeug                2.2.2
wheel                   0.37.1
zipp                    3.11.0
  • change in export.py
diff --git a/export.py b/export.py
index 7910178..33b6d4b 100644
--- a/export.py
+++ b/export.py
@@ -624,7 +624,7 @@ def parse_opt():
     parser.add_argument('--int8', action='store_true', help='CoreML/TF INT8 quantization')
     parser.add_argument('--dynamic', action='store_true', help='ONNX/TF/TensorRT: dynamic axes')
     parser.add_argument('--simplify', action='store_true', help='ONNX: simplify model')
-    parser.add_argument('--opset', type=int, default=17, help='ONNX: opset version')
+    parser.add_argument('--opset', type=int, default=12, help='ONNX: opset version')
     parser.add_argument('--verbose', action='store_true', help='TensorRT: verbose log')
     parser.add_argument('--workspace', type=int, default=4, help='TensorRT: workspace size (GB)')
     parser.add_argument('--nms', action='store_true', help='TF: add NMS to model')

But still, I would like to know whether it is possible to export .onnx with the latest version of torch.

  • Successfully export the .onnx model and run with --dnn.

2022-12-25_19-12.png

@glenn-jocher
Copy link
Member

@ChrisVicky @AyushExel ONNX works correctly under any combination of torch and cv2, but DNN inference depends on a the exact cv2 and torch versions used, so you may need to experiment some.

In any case if you have an ONNX model you'll get much better performance using ONNX inference than DNN inference on both CPU and GPU.

@glenn-jocher
Copy link
Member

@ChrisVicky also I'd raise this issue directly with cv2 as errors generated there are outside of our control.

@ChrisVicky
Copy link
Author

Thanks a lot!

@glenn-jocher
Copy link
Member

@ChrisVicky you're welcome! If you have any more questions or run into issues, feel free to ask. We're here to help!

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

3 participants