-
-
Notifications
You must be signed in to change notification settings - Fork 16.3k
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
Using cv2.dnn to use our trained model #4558
Comments
👋 Hello @prp-e, 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 Glenn Jocher at glenn.jocher@ultralytics.com. RequirementsPython>=3.6.0 with all requirements.txt installed including PyTorch>=1.7. To get started: $ git clone https://github.com/ultralytics/yolov5
$ cd yolov5
$ pip install -r requirements.txt EnvironmentsYOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):
StatusIf 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. |
Apparently it is possible to read from ONNX, So I used |
I made a simple onnx export using:
and this is what I got:
and I wrote this to test it out: import cv2
import numpy as np
import onnx
net = cv2.dnn.readNetFromONNX("yolov5/runs/train/exp3/weights/last.onnx")
image = cv2.imread("data/images/hotdogs-25.jpg")
image = cv2.resize(image, (320, 320))
blob = cv2.dnn.blobFromImage(image, 1.0 , (320, 320))
net.setInput(blob)
results = net.forward()
biggest_pred_index = np.array(results)[0].argmax()
print(biggest_pred_index)
cv2.imshow("image", image)
cv2.waitKey(0) but when I run test code, I get:
|
@prp-e your code is out of date. You should update and then run ONNX inference with detect.py:
For cv2 errors, I would raise those directly on the cv2 repository as they are out of our control. |
Thanks. I've updated my base, I'll check it out. |
@prp-e good news 😃! Your original issue may now be fixed ✅ in PR #4833 by @SamFC10. This PR implements architecture updates to allow for ONNX-exported YOLOv5 models to be used with OpenCV DNN. To receive this update:
Thank you for spotting this issue and informing us of the problem. Please let us know if this update resolves the issue for you, and feel free to inform us of any other issues you discover or feature requests that come to mind. Happy trainings with YOLOv5 🚀! |
@glenn-jocher @SamFC10 I have the same issue. I am getting this error:
I have tried all export commands I have seen on Github without success. Is there anything special I need to do in order to export a model without a |
From what I understand we need to export the models without the |
@willyd Yes, onnx export with --dynamic option produces a Code for testing import numpy as np
import cv2
inp = np.random.rand(1, 3, 640, 640).astype(np.float32)
net = cv2.dnn.readNetFromONNX('yolov5s.onnx')
net.setInput(inp)
out = net.forward()
print(out.shape) onnx model without --dynamic onnx model with --dynamic
There is already a feature request for this layer opencv/opencv#20324. Meanwhile you can use onnx models without --dynamic option. |
Thanks @SamFC10 for the clarification. Somehow I thought all examples add the |
My export code is:
and when I try to open it with
what's the problem now? |
@prp-e as explained in this issue above --dynamic does not support DNN inference. Workaround is to export with default settings. |
@prp-e @glenn-jocher is right. Remove the dynamic option and it should work. If you want to use dynamic image sizes you can export with --dynamic and use onnxruntime instead of OpenCV for inference. |
Thanks, my problem with loading ONNX solved. Now, I wrote this piece of code: inp = cv2.imread('pedestrians.jpg')
net = cv2.dnn.readNetFromONNX('pedestrians_new.onnx')
inp = cv2.resize(inp, (640, 640))
net.setInput(inp)
out = net.forward() and I get this result:
|
@prp-e Input shape is incorrect. Model expects inputs of size (1, 3, 640, 640), you are passing an input of size (640, 640, 3). Make sure your input pre-processing steps are correct (use detect.py as reference) |
Thanks @SamFC10. I couldn't find the reshaping process from |
@prp-e Check if this works import numpy as np
import cv2
inp = cv2.imread('pedestrians.jpg')
blob = cv2.dnn.blobFromImage(inp, 1.0/255, (640, 640), swapRB=True)
net = cv2.dnn.readNetFromONNX('pedestrians_new.onnx')
net.setInput(blob)
out = net.forward() |
Thanks @SamFC10, it worked perfectly. I also have a question about bounding boxes, etc. How can I add them to my |
@prp-e How to solve the " loading ONNX"?
|
How can I use
cv2.dnn
to use model I trained using YOLOv5?I have a super old code (which is for YOLOv3 I believe) which looks like this:
And using YOLOv5 I did this:
Is it possible to feed this
weights/last.pt
to DNN? I run my code on CPU and using pytroch for detection, makes my code slow as hell. I'd be thankful if you help me get it to work withcv2.dnn
method.The text was updated successfully, but these errors were encountered: