(-215:Assertion failed) splits > 0 && inpShape[axis_rw] % splits == 0 in function ‘getMemoryShapes‘

使用ONNX运行YOLOv5时, 遇到报错如下:

./build/main yolov5s.onnx data/images/zidane.jpg 
[ERROR:[email protected]] global /home/changym/sources/opencv-4.6.0/modules/dnn/src/onnx/onnx_importer.cpp (1018) handleNode DNN/ONNX: ERROR during processing node with 2 inputs and 3 outputs: [Split]:(onnx_node!/model.24/Split) from domain='ai.onnx'
terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(4.6.0) /home/changym/sources/opencv-4.6.0/modules/dnn/src/onnx/onnx_importer.cpp:1040: error: (-2:Unspecified error) in function 'handleNode'
> Node [[email protected]]:(onnx_node!/model.24/Split) parse error: OpenCV(4.6.0) /home/changym/sources/opencv-4.6.0/modules/dnn/src/layers/slice_layer.cpp:235: error: (-215:Assertion failed) splits > 0 && inpShape[axis_rw] % splits == 0 in function 'getMemoryShapes'

原先使用的是最新版本的pytorch,根据issue8744,将pytorch降级到1.11。

重新运行,生成ONNX时报错:

python export.py --data data/coco128.yaml --weights yolov5s.pt --include onnx
export: data=data/coco128.yaml, weights=['yolov5s.pt'], imgsz=[640, 640], batch_size=1, device=cpu, half=False, inplace=False, keras=False, optimize=False, int8=False, dynamic=False, simplify=False, opset=17, 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  v7.0-116-g5c91dae Python-3.7.16 torch-1.11.0+cu102 CPU

Fusing layers... 
YOLOv5s summary: 213 layers, 7225885 parameters, 0 gradients

PyTorch: starting from yolov5s.pt with output shape (1, 25200, 85) (14.1 MB)

ONNX: starting export with onnx 1.12.0...
ONNX: export failure ❌ 0.0s: Unsupported ONNX opset version: 17

这里其实只要运行时设置opset参数就好了,即--opset=11,如下:

python export.py --data data/coco128.yaml --weights yolov5s.pt --include onnx --opset=11
export: data=data/coco128.yaml, weights=['yolov5s.pt'], imgsz=[640, 640], batch_size=1, device=cpu, half=False, inplace=False, keras=False, optimize=False, int8=False, dynamic=False, simplify=False, opset=11, 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  v7.0-116-g5c91dae Python-3.7.16 torch-1.11.0+cu102 CPU

Fusing layers... 
YOLOv5s summary: 213 layers, 7225885 parameters, 0 gradients

PyTorch: starting from yolov5s.pt with output shape (1, 25200, 85) (14.1 MB)

ONNX: starting export with onnx 1.12.0...
ONNX: export success ✅ 1.7s, saved as yolov5s.onnx (28.0 MB)

Export complete (2.1s)
Results saved to /home/changym/yolov5
Detect:          python detect.py --weights yolov5s.onnx 
Validate:        python val.py --weights yolov5s.onnx 
PyTorch Hub:     model = torch.hub.load('ultralytics/yolov5', 'custom', 'yolov5s.onnx')  
Visualize:       https://netron.app

重新运行yolo track,成功

./build/main yolov5s.onnx data/images/zidane.jpg 
date       time         ( uptime  ) [ thread name/id ]                   file:line     v| 
2023-03-01 09:07:28.048 (   0.000s) [main thread     ]             loguru.cpp:644   INFO| arguments: ./build/main yolov5s.onnx data/images/zidane.jpg
2023-03-01 09:07:28.048 (   0.000s) [main thread     ]             loguru.cpp:647   INFO| Current dir: /home/changym/yolov5/ONNX-yolov5
2023-03-01 09:07:28.048 (   0.000s) [main thread     ]             loguru.cpp:649   INFO| stderr verbosity: 0
2023-03-01 09:07:28.048 (   0.000s) [main thread     ]             loguru.cpp:650   INFO| -----------------------------------
2023-03-01 09:07:28.048 (   0.000s) [main thread     ]               main.cpp:22    INFO| Start main process
2023-03-01 09:07:28.092 (   0.043s) [main thread     ]               main.cpp:24    INFO| Load model done ..
2023-03-01 09:07:28.097 (   0.049s) [main thread     ]               main.cpp:26    INFO| Read image from data/images/zidane.jpg
2023-03-01 09:07:28.281 (   0.232s) [main thread     ]               main.cpp:28    INFO| Detect process finished
2023-03-01 09:07:28.282 (   0.233s) [main thread     ]           detector.cpp:66    INFO| Extract output mat from detection
2023-03-01 09:07:28.295 (   0.246s) [main thread     ]           detector.cpp:90    INFO| Do NMS in 25200 boxes
2023-03-01 09:07:28.296 (   0.247s) [main thread     ]           detector.cpp:92    INFO| After NMS  4 boxes keeped
2023-03-01 09:07:28.296 (   0.247s) [main thread     ]           detector.cpp:98    INFO| Draw boxes and labels in orign image
2023-03-01 09:07:31.023 (   2.975s) [main thread     ]               main.cpp:31    INFO| Post process done save image to assets/output.jpg
detect Image And Save to assets/output.jpg
2023-03-01 09:07:31.032 (   2.983s) [main thread     ]             loguru.cpp:523   INFO| atexit

你可能感兴趣的:(计算机视觉,深度学习,人工智能)