-
Notifications
You must be signed in to change notification settings - Fork 4.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 export to onnx be used by opencv? #49
Comments
When I export the official model yolov7 to onnx with export.py from u5 git brach, then use opencvdnn to do inference job. It works well. But when I train the yolov7 (using cfg/train/yolov7.yaml, IDetection function) with the custom dataset, then export it into onnx format with same way. Then opencv report the following error when it load the onnx model. create yolov7 object detection ....
[ERROR:0@0.270] global /io/opencv/modules/dnn/src/onnx/onnx_importer.cpp (906) handleNode DNN/ONNX: ERROR during processing node with 2 inputs and 1 outputs: [Mul]:(461) from domain='ai.onnx'
Traceback (most recent call last):
File "/home/jingfeng/project/yolov7/weights/yolov7_dnn.py", line 188, in <module>
cocoDetect = yolov7()
File "/home/jingfeng/project/yolov7/weights/yolov7_dnn.py", line 73, in __init__
self.net = cv2.dnn.readNetFromONNX('yolov7_gun.onnx')
cv2.error: OpenCV(4.5.5) /io/opencv/modules/dnn/src/onnx/onnx_importer.cpp:928: error: (-2:Unspecified error) in function 'handleNode'
> Node [Mul@ai.onnx]:(461) parse error: OpenCV(4.5.5) /io/opencv/modules/dnn/src/onnx/onnx_importer.cpp:1899: error: (-213:The function/feature is not implemented) Different shapes case is not supported with constant inputs: Mul in function 'parseMul' Although I simplify the onnx model and try different opset, and I failed to fix that issue still. Something different between the official yolov7 model and the trained model, but I have not find it yet. If someone has any idea, I appreciate. |
We use equation in section 4.4 of YOLOR paper to re-parameterize IDetect into Detect by merging implicit knowledge into convolutional layers. So training-time cfg are as in cfg/training, and inference-time cfg are in cfg/deploy. |
@YaoQ can you upload and send me the ONNX converted file that works with OpenCV DNN, please? I've converted the base model (YOLOv7) and the YOLOv7x to ONNX but none of that works. |
Your opencv is 4.6.0? That also no support yolo5s.onnx,But opencv 4.5.5 have worked normal! |
I use OpenCV v4.5.5. YOLOv4 darknet and YOLOv5 exported from ONNX is working fine on my system, only the v7 is not working at the moment. |
Just try opencv 4.6.0 please. |
The same with you. I find some differents:#99 |
Please check this issue, and someone export the onnx working with opencvDNN and onnxruntime in C++ or python. |
opencv dnn success run custom yolov7 1 reparam trained best.pt
2 use branch https://github.com/WongKinYiu/yolov7/tree/u5 to export onnx model |
Yes, it works! |
Please try to use opencv 4.6.0! |
|
When I have used so C++ code to detect, that no work. The code: #include <opencv2/opencv.hpp> std::vectorstd::string load_class_list() void load_net(cv::dnn::Net &net, bool is_cuda) const std::vectorcv::Scalar colors = {cv::Scalar(255, 255, 0), cv::Scalar(0, 255, 0), cv::Scalar(0, 255, 255), cv::Scalar(255, 0, 0)}; const float INPUT_WIDTH = 640.0; struct Detection cv::Mat format_yolov5(const cv::Mat &source) { void detect(cv::Mat &image, cv::dnn::Net &net, std::vector &output, const std::vectorstd::string &className) { auto input_image = format_yolov5(image); cv::dnn::blobFromImage(input_image, blob, 1./255., cv::Size(INPUT_WIDTH, INPUT_HEIGHT), cv::Scalar(), true, false); float x_factor = input_image.cols / INPUT_WIDTH; float *data = (float *)outputs[0].data; const int dimensions = 85; std::vector class_ids; for (int i = 0; i < rows; ++i) {
} std::vector nms_result; int main(int argc, char **argv) std::vectorstd::string class_list = load_class_list(); cv::Mat frame; bool is_cuda = argc > 1 && strcmp(argv[1], "cuda") == 0; cv::dnn::Net net; auto start = std::chrono::high_resolution_clock::now(); while (true)
} std::cout << "Total frames: " << total_frames << "\n"; return 0; |
|
I used trasfer learning on yolov7 to train my custom dataset with 3 classes. Transfer learning: (Changed batch-size to 8) Inference: the result test_image is ok. Then i convert the best.pt to onnx: Then, I got best.onnx but when i use OpenCV 4.6.0 readNet to load this best.onnx, i got exception. But if i download the pre-trained yolov7.pt and convert to onnx using same command as below: the output yolov7.onnx can load in OpenCV 4.6.0 readNet (Same source) without exception. So the testing OpenCV code should work, but the converted custom yolov7 onnx not works on it. Does anyone know why? Thanks My custom yaml, pt, onnx files, can be downloaded from: |
Thanks for sharing the files. |
An image with custom object: https://drive.google.com/file/d/1pw72rCi8U3TNRkAVRD4G5RzbiujUz635/view?usp=sharing Tried to create a py file with the following code to do reparam ` importfrom copy import deepcopy nc = 3 #change with your nc model trained by cfg/training/*.yamlckpt = torch.load('cfg/training/yolov7-custom.pt', map_location=device) reparameterized model in cfg/deploy/*.yamlmodel = Model('cfg/training/yolov7-custom.yaml', ch=3, nc=nc).to(device) with open('cfg/training/yolov7-custom.yaml') as f: copy intersect weightsstate_dict = ckpt['model'].float().state_dict() reparametrized YOLORfor i in range((nc + 5) * 3): model to be savedckpt = {'model': deepcopy(model.module if is_parallel(model) else model).half(), save reparameterized modeltorch.save(ckpt, 'cfg/deploy/yolov7-custom-reparam.pt') After run, got cfg/deploy/yolov7-custom-reparam.pt, convert to onnx Got yolov7-custom-reparam.onnx And try to load the yolov7-custom-reparam.onnx on OpenCV 4.6.0 with cuda, dnn: [ERROR:0@9.701] global C:\opencv\opencv-4.6.0\modules\dnn\src\onnx\onnx_importer |
In section - reparameterized model in cfg/deploy/*.yaml Shouldnt you take a config file from the deploy folder there? Also why not using this script https://github.com/WongKinYiu/yolov7/blob/main/tools/reparameterization.ipynb I am still new and will try out things in the next day - just stepped over this thread as i need to run on opecv too. |
Can someone briefly, explain the difference between Detect and IDetect functions? I used the u5 branch to export the onnx model and was able to infer with the OpenCV without reparameterization. but I used the yolov7.pt trained on coco dataset. does this reparameterization affect the inference speed? |
I am getting |
really appreciate, it works, my env -opencv4.5.5 export.py(torch>1.7.0 && torch < 1.12.0) |
Hi, I use official yolov7.pt and export the model by export.py from u5 branch, and it show the following error
Should I reparam the model? but when I reparam the offical model, it shows the following error:
|
This was due to the wrong |
@YaoQ Can you please upload that working ONNX format file into google drive and post here. I'm not able to convert. I need YOLOv7.onnx andYOLOv7-tiny.onnx |
@knoppmyth Do you have .onnx file of yolov7 and yolov7-tiny ? IF you have please upload. |
@linuxfedora2020 If you have that yolov7.onnx file can you upload that ? |
Thank you so much! This actually works very well, I follow these steps & now I can use YOLOv7 onnx file in OpenCV. If anyone wants to use ONNX file in OpenCV, these steps are safe to go. |
opencv 4.6.0
The text was updated successfully, but these errors were encountered: