-
-
Notifications
You must be signed in to change notification settings - Fork 16.2k
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
How to use the onnx (converted from .pt) in opencv C++ #5378
Comments
👋 Hello @NacerFaraj, 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. |
@NacerFaraj we don't have C/C++ examples of cv2 DNN ONNX inference, but you can use detect.py as a reference example of python cv2 DNN ONNX inference: python export.py --weights yolov5s.pt --include onnx
python detect.py --weights yolov5s.onnx --dnn # DNN inference
python detect.py --weights yolov5s.onnx # ONNX Runtime inference |
Then, would you plz explain the the structure of the
|
@NacerFaraj for COCO with 80 classes outputs will be shape(n,85) with 85 dimension = (x,y,w,h,object_conf, class0_conf, class1_conf, ...) |
have you solved your problem?I just stuck in this problem like you.can you share the code here or for me? |
not yet :-( |
@NacerFaraj we don't provide assistance debugging custom code due to limited resources, however for custom workflows be aware that YOLOv5 models expect inputs in RGB order. |
I don't think yolov5 can be used in this opencv4.5.4. |
You can use Yolo the next way. In C++ auto net = cv::dnn::readNetFromONNX("yolov5.onnx"); // Creates 4-dimensional blob from image.
There must be changed one or other things, but I think it will work. |
@hilevelhdd @futureflsl @NacerFaraj please see the last comment and tell us your result. Good luck |
I have installed OpenCV 4.5.4 and correctly loaded the model with cv::dnn::readNetFromONNX("yolov5.onnx") after python3 export.py --weights best.pt --include onnx --simplify. However I was unable to make detections. The output is a downscaled image without any prediction. |
@PauloMendes33 DNN inference is simple:
|
I have the same issue...
|
Were you able to figure this out @rtrahms ? |
have a look at my repo ONNX-yolov5 |
@Hexmagic awesome, nice work! We don't have any good C++ inference example ourselves unfortunately. |
Thanks @Hexmagic! Are you getting the same results for predictions as you are using the detect.py that @glenn-jocher recommends? I'm getting wildly different results on OpenCV (even on python) than using detect.py, with the same model and inputs. |
Where in detect.py is this? I see that the default --imgsz parameter is 640x640 on line 54 |
I was wrong,detect.py not use original shape or 640x640,108 line dataset = LoadImages(source, img_size=imgsz, stride=stride, auto=pt) augmentation.py line 105, dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding (640-480)=160, (640-640)=0
if auto: # minimum rectangle
dw, dh = np.mod(dw, stride), np.mod(dh, stride) # wh padding 160%320=0, 0%32=0 |
Thanks - any idea why OpenCV net is much lower confidence than detect.py even when same resolution? Or is it the same for you? |
general.py line 701, function # Compute conf
x[:, 5:] *= x[:, 4:5] # conf = obj_conf * cls_conf and this is my cpp file,whice get same result compare to detect.py |
Thank you very much @Hexmagic 😄 |
👋 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 ⭐! |
@Hexmagic great job! I noticed some blogs but all of them accuracy goes down compared official version, your works really help me a lot! ^_^ |
I encounter the following error using
|
@fisakhan since this error was generated by cv2 on a working ONNX model you should raise this directly with OpenCV. |
@fisakhan python export.py --weights yolov5s.pt --include onnx --simplify |
I am using OpenCV 4.5.4, which supports the use of converted yolov5 from pythorch (*.pt) to onnx.
I got a pre-trained yolov5 model. After converting it to onnx, I can load the model successfully using:
cv::dnn::readNetFromONNX("best.onnx")
. BUT, I do not know how to use it to find the bounding boxes around the objects given a test image!There are plenty of samples showing how to do so with the darknet. But, I couldn't find any sample for using yolov5.onnx in opencv & C++.
any help?
The text was updated successfully, but these errors were encountered: