UNeedCryDear / yolov7-opencv-dnn-cpp

使用opencv部署yolov7
Apache License 2.0
71 stars 16 forks source link

YOLOv7.pt to ONNX #10

Closed PauloMendes33 closed 11 months ago

PauloMendes33 commented 1 year ago

Hello

I need to know how do I export the YOLOv7.pt file to .ONNX using the official export.py from WongKinYiu github page. I tried as it is informed in the WongKinYiu's github page but the resulting YOLOv7.onnx file doesn't work with OpenCV_4.5.4 ( using Pytorch_1.13).

@UNeedCryDear Can you solve this problem? (As you solved many others so easily xD)

Best Wishes, PM

UNeedCryDear commented 1 year ago
python path/to/export.py --weights yolov7.pt --img [640,640] 

Without -- grid, it can work . If it still doesn't work under torch1.13.x, please let me know. I haven't tested this project for a long time, maybe I need to update it.

PauloMendes33 commented 1 year ago

I use NetBeans 8.1 IDE and OpenCV 4.5.4 with C++ to run the software.

I tested the next commands to export the file to onnx in my Ubuntu 18.04 with ONNX 1.13.0:

$ !python export.py --weights best.pt --grid --end2end --simplify --topk-all 100 --iou-thres 0.65 --conf-thres 0.35 --img-size 640 640 --max-wh 640

$ !python export.py --weights best.pt --end2end --simplify --topk-all 100 --iou-thres 0.65 --conf-thres 0.35 --img-size 640 640 --max-wh 640

$ !python export.py --weights /content/best.pt --img-size 640 640

Doesn't work any of the output .onnx files.

Also in Google CoLab with ONNX 1.14.0 and torch 2.0.1+cu118, I tried the following commands:

$ !python export.py --weights best.pt --grid --end2end --simplify --topk-all 100 --iou-thres 0.65 --conf-thres 0.35 --img-size 640 640 --max-wh 640

$ !python export.py --weights best.pt --end2end --simplify --topk-all 100 --iou-thres 0.65 --conf-thres 0.35 --img-size 640 640 --max-wh 640

$ !python export.py --weights /content/best.pt --img-size 640 640

And it doesn't work when I try read the best.onnx model with OpenCV 4.5.4.

I tried to print the error and What I have got is the following: "Caught exception "OpenCV(4.5.4) /home/user/opencv-4.5.4/modules/dnn/src/onnx/onnx_importer.cpp:198: error: (-5:Bad argument) Can't read ONNX file: ./models/best.onnx in function 'ONNXImporter' " read onnx model failed! "

I Don't know... what should it be? Can you solve it? Thank you for your fast reply @UNeedCryDear . ;)

PM

UNeedCryDear commented 1 year ago

remove params --grid and --end2end when exporting. If --grid is used, the detect layer will also be exported together,which will cause opencv read failure! And --end2end is used for onnxruntime. It means yolov7.onnx file has post processing operations, but opencv cannot support it.

PauloMendes33 commented 1 year ago

Hello

I am back to work with your YOLOv7 software. @UNeedCryDear I have tried the command to export (Ubuntu 18.04+Onnx 1.13) without the flags --grid and --end2end: $ python3 export.py --weights best_art_cie.pt --simplify --topk-all 100 --iou-thres 0.45 --conf-thres 0.4 Same result in Netbeans 8.1+OpenCV 4.5.4 : NetBeans Terminal: Caught exception "OpenCV(4.5.4) /home/user/opencv-4.5.4/modules/dnn/src/onnx/onnx_importer.cpp:198: error: (-5:Bad argument) Can't read ONNX file: ./models/best_art_cie.onnx in function 'ONNXImporter' " read onnx model failed!

What should I do? Thank you for your last fast fast reply (I was very very busy to test your solution).

Best Wishes, Paulo Mendes

UNeedCryDear commented 1 year ago

I am unable to reproduce your error. Could you provide your best_art_cie.pt for me to try it. I am not sure what code you have modified. For example, using the "ReOrg" in model.yaml. image

UNeedCryDear commented 1 year ago

I suggest that you export the official model to test, and then export your custom model. python3 export.py --weights yolov7.pt --img [640,640]

PauloMendes33 commented 1 year ago

Hello @UNeedCryDear

I have changed almost anything from your software, there is no need for it. ;) I am sending my best_art_cie.pt weight file for you to test it, in the link below: https://drive.google.com/drive/folders/1x5clNnxrodSORJmbapwnYZvnUoaL39rC?usp=sharing

In the other hand, I have downloaded the yolov7-tiny.pt file and exported it to onnx with the following commands: 1) python3 export.py --weights yolov7-tiny.pt --img 640 640

2) python3 export.py --weights yolov7-tiny.pt --grid --end2end --simplify --topk-all 100 --iou-thres 0.45 --conf-thres 0.4 --img-size 640 640 --max-wh 640

3) python3 export.py --weights yolov7-tiny.pt --simplify --topk-all 100 --iou-thres 0.45 --conf-thres 0.4 --img-size 640 640 --max-wh 640

It doesn't work, always returning the same error. I will copy all data to here, because it mentions other problems such as no onnx modules... next it is:

======================================================================================================================================================================================================================================================================================================

$ python3 export.py --weights yolov7-tiny.pt --simplify --topk-all 100 --iou-thres 0.45 --conf-thres 0.4 --img-size 640 640 --max-wh 640 Import onnx_graphsurgeon failure: No module named 'onnx_graphsurgeon' Namespace(batch_size=1, conf_thres=0.4, device='cpu', dynamic=False, dynamic_batch=False, end2end=False, fp16=False, grid=False, img_size=[640, 640], include_nms=False, int8=False, iou_thres=0.45, max_wh=640, simplify=True, topk_all=100, weights='yolov7-tiny.pt') YOLOR 🚀 v0.1-126-g84932d7 torch 1.13.1+cu117 CPU

Fusing layers... Model Summary: 200 layers, 6219709 parameters, 6219709 gradients

Starting TorchScript export with torch 1.13.1+cu117... TorchScript export success, saved as yolov7-tiny.torchscript.pt CoreML export failure: No module named 'coremltools'

Starting TorchScript-Lite export with torch 1.13.1+cu117... TorchScript-Lite export success, saved as yolov7-tiny.torchscript.ptl

Starting ONNX export with onnx 1.13.0... /home/user/YOLOv7_18_08_2023/yolov7/models/yolo.py:582: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs! if augment: /home/user/YOLOv7_18_08_2023/yolov7/models/yolo.py:614: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs! if profile: /home/user/YOLOv7_18_08_2023/yolov7/models/yolo.py:629: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs! if profile: Simplifier failure: No module named 'onnxsim' ONNX export success, saved as yolov7-tiny.onnx

Export complete (6.54s). Visualize with https://github.com/lutzroeder/netron.

Best Regards @UNeedCryDear , PM

UNeedCryDear commented 1 year ago

https://drive.google.com/file/d/1eFygeNjj8tW4cPh1KZwOQ7ssvnjcACqN/view?usp=sharing

export by torch1.8.2

PauloMendes33 commented 1 year ago

Hello

The result in Ubuntu 18.04+NetBeans8.1+OpenCV4.5.4

Caught exception "OpenCV(4.5.4) /home/pauloasmendes/opencv-4.5.4/modules/dnn/src/onnx/onnx_importer.cpp:198: error: (-5:Bad argument) Can't read ONNX file: ./models/best_art_cie.onnx in function 'ONNXImporter' " read onnx model failed!

I don't know @UNeedCryDear ... What should I do?

Best wishes, PM

UNeedCryDear commented 1 year ago

image

it worked on win10 with opencv4.5.0. I don't know what happend on linux, may be you shoule propose a new issue on the official of OpenCV

PauloMendes33 commented 1 year ago

Hello

I understand. I will solve the problem in some way.

You used your best_art_cie.onnx (that you exported from my best_art_cie.pt)? You are using the software named "yolov5-opencv-dnn-cpp", I am using your software named "yolov7-opencv-dnn-cpp" (https://github.com/UNeedCryDear/yolov7-opencv-dnn-cpp), is there diferences in the software?

I will test it after in another machine with another OS and OpenCV version.

Thank you very much! Best wishes, PM

UNeedCryDear commented 1 year ago

best_art_cie.onnx exported from your pt model. When it's just reading an onnx file, this is the same as "yolov7-opencv-dnn-cpp".Because their reading code is the same.

PauloMendes33 commented 1 year ago

Hello

I tested with my YOLOv5 software and the best_art_cie.onnx file is also well read. It returns, "read net ok!"

I will look for the YOLOv7 model laod software.

PM

PauloMendes33 commented 1 year ago

Hello @UNeedCryDear

Believe it or not, the problem was in the path directory of the .onnx file defined in the main software file, as it was: string model_path = "./models/best_art_cie.onnx"; As it works, after changing the files to the main folder: string model_path = "best_art_cie.onnx";

Well, I am very very sorry to occupy your time with this problem. :/

The correct way to export the .pt weight file to .onnx (to use with OpenCV) is as you mentioned at the beginning: $ python export.py --weights best.pt --simplify --topk-all 100 --iou-thres 0.45 --conf-thres 0.4 --img-size 640 640 --max-wh 640 (without --grid and -end2end)

Best wishes, PM