hukaixuan19970627 / yolov5_obb

yolov5 + csl_label.(Oriented Object Detection)(Rotation Detection)(Rotated BBox)基于yolov5的旋转目标检测
GNU General Public License v3.0
1.81k stars 425 forks source link

detection error on onnx file #534

Closed blackHorz closed 1 year ago

blackHorz commented 1 year ago

Hi, I am successful converting the model from pt to onnx format. However running this command is giving me this error

(Py39_Torch1.10_cu11.3) D:\yolov5_obb-master\yolov5_obb-master>python detect.py --weights D://yolov5_obb-master//datasets//teklek//best.onnx --source test_teklek.jpg --img 640 --device 0 --dnn detect: weights=['D://yolov5_obb-master//datasets//teklek//best.onnx'], source=test_teklek.jpg, imgsz=[640, 640], conf_thres=0.25, iou_thres=0.2, max_det=1000, device=0, 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=2, hide_labels=False, hide_conf=False, half=False, dnn=True YOLOv5 2022-8-12 torch 1.10.1+cu113 CUDA:0 (Quadro RTX 3000, 6144MiB)

Loading D:\yolov5_obb-master\datasets\teklek\best.onnx for ONNX OpenCV DNN inference... [ERROR:0@4.051] global net_impl.cpp:1164 cv::dnn::dnn4_v20221220::Net::Impl::getLayerShapesRecursively OPENCV/DNN: [Reshape]:(onnx_node!Reshape_345): getMemoryShapes() throws exception. inputs=1 outputs=1/1 blobs=0 [ERROR:0@4.057] global net_impl.cpp:1167 cv::dnn::dnn4_v20221220::Net::Impl::getLayerShapesRecursively input[0] = [ 1 561 80 80 ] [ERROR:0@4.060] global net_impl.cpp:1171 cv::dnn::dnn4_v20221220::Net::Impl::getLayerShapesRecursively output[0] = [ ] [ERROR:0@4.063] global net_impl.cpp:1177 cv::dnn::dnn4_v20221220::Net::Impl::getLayerShapesRecursively Exception message: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\layers\reshape_layer.cpp:109: error: (-215:Assertion failed) total(srcShape, srcRange.start, srcRange.end) == maskTotal in function 'cv::dnn::computeShapeByReshapeMask'

Traceback (most recent call last): File "D:\yolov5_obb-master\yolov5_obb-master\detect.py", line 252, in main(opt) File "D:\yolov5_obb-master\yolov5_obb-master\detect.py", line 247, in main run(*vars(opt)) File "D:\Anaconda\envs\Py39_Torch1.10_cu11.3\lib\site-packages\torch\autograd\grad_mode.py", line 28, in decorate_context return func(args, *kwargs) File "D:\yolov5_obb-master\yolov5_obb-master\detect.py", line 101, in run model.warmup(imgsz=(1, 3, imgsz), half=half) # warmup File "D:\yolov5_obb-master\yolov5_obb-master\models\common.py", line 436, in warmup self.forward(im) # warmup File "D:\yolov5_obb-master\yolov5_obb-master\models\common.py", line 398, in forward y = self.net.forward() cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\layers\reshape_layer.cpp:109: error: (-215:Assertion failed) total(srcShape, srcRange.start, srcRange.end) == maskTotal in function 'cv::dnn::computeShapeByReshapeMask'

blackHorz commented 1 year ago

So looks like i need to do it with onnx-simplify. Now everything works however my predictions are 0

Here is how I have to do it.

Export to onnx format : ` (Py39_Torch1.10_cu11.3) D:\yolov5_obb-master\yolov5_obb-master>python export.py --weights D://yolov5_obb-master//datasets//mix//best.pt --imgsz 640 --include onnx --simplify
export: data=data\dotav15_poly.yaml, weights=['D://yolov5_obb-master//datasets//mix//best.pt'], imgsz=[640], batch_size=1, device=cpu, half=False, inplace=False, train=False, optimize=False, int8=False, dynamic=False, simplify=True, opset=12, verbose=False, workspace=4, nms=False, agnostic_nms=False, topk_per_class=100, topk_all=100, iou_thres=0.45, conf_thres=0.25, include=['onnx'] YOLOv5 2022-8-12 torch 1.10.1+cu113 CPU

Fusing layers... Model Summary: 290 layers, 21592437 parameters, 0 gradients, 50.2 GFLOPs

PyTorch: starting from D:\yolov5_obb-master\datasets\mix\best.pt (43.7 MB)

ONNX: starting export with onnx 1.13.1... D:\yolov5_obb-master\yolov5_obb-master\models\yolo.py:68: 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 self.onnx_dynamic or self.grid[i].shape[2:4] != x[i].shape[2:4]: ONNX: simplifying with onnx-simplifier 0.4.17... ONNX: export success, saved as D:\yolov5_obb-master\datasets\mix\best.onnx (86.8 MB) ONNX: run --dynamic ONNX model inference with: 'python detect.py --weights D:\yolov5_obb-master\datasets\mix\best.onnx'

Export complete (7.66s) Results saved to D:\yolov5_obb-master\datasets\mix Detection using opencv-dnn (Py39_Torch1.10_cu11.3) D:\yolov5_obb-master\yolov5_obb-master>python detect.py --weights D://yolov5_obb-master//datasets//mix//best.onnx --source test.jpg --img 640 --device 0 --conf-thres 0.15 --dnn detect: weights=['D://yolov5_obb-master//datasets//mix//best.onnx'], source=test.jpg, imgsz=[640, 640], conf_thres=0.15, iou_thres=0.2, max_det=1000, device=0, 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=2, hide_labels=False, hide_conf=False, half=False, dnn=True YOLOv5 2022-8-12 torch 1.10.1+cu113 CUDA:0 (Quadro RTX 3000, 6144MiB)

Loading D:\yolov5_obb-master\datasets\mix\best.onnx for ONNX OpenCV DNN inference... PREDICTION : [tensor([], size=(0, 7))] image 1/1 D:\yolov5_obb-master\yolov5_obb-master\test.jpg: 640x640 Done. (0.832s) Speed: 1283.9ms pre-process, 831.8ms inference, 10.5ms NMS per image at shape (1, 3, 640, 640) Results saved to runs\detect\exp3 ` You can see the PREDICTION is empty. I do not know what is wrong here.

Alex-Schaefer commented 1 year ago

I see you have closed this issue, would you mind telling your solution? I have a similar problem, detect.py with the .pt model works well, but detect.py with the exported .onnx model not.