Actual error message (3.4.17/4.5.5+): DNN/ONNX(Shape): dynamic 'zero' shapes are not supported
- OpenCV => 4.5.1
- Operating System / Platform => Windows 64 Bit
- Compiler => MinGW 7.3
Can opencv load dynamic input models?
An error is reported when using opencv to load the onnx model of dynamic input type.
Detailed description
The following is the compressed package of the input type and model file.

super_resolution.zip
After I converted a pytorch model to onnx, the input size was 1x3x640x640. This model can run at onnxronnuntime. Then I tried to use opencv's readNet(path) function. The model can also work normally.
When I convert this model to dynamic input, this model can also run in onnxronnuntime, but when I use opencv's readNet to load this dynamic input model, ,the program starts to report an error, the error is as follows
OpenCV: terminate handler is called! The last OpenCV error is: OpenCV(4.5.1) Error: Assertion failed (total(os[i]) > 0) in getLayerShapesRecursively, file F:\OpenCV\opencv451\sources\modules\dnn\src\dnn.cpp, line 3520
If opencv can load dynamic input models,do I need to set the input and output size after the model is loaded by opencv?
If yes, is there any information on this part of the content?
Steps to reproduce
I first use python to convert the model to onnx.
The conversion code of pytorch to onnx I used is as follows
input_names = ["input0"]
output_names = ["output0","output1","output2"]
x = torch.randn(1, 3, 640, 640, requires_grad=True)
dynamic_axes = {'input0': {2: "height", 3: 'width'}, # variable lenght axes
'output0': {1: '_size'},
'output1': {1: '_size'},
'output2': {1: '_size'}}
# Export the model
torch.onnx.export(net, # model being run
x, # model input (or a tuple for multiple inputs)
"super_resolution.onnx", # where to save the model (can be a file or file-like object)
export_params=True, # store the trained parameter weights inside the model file
opset_version=11, # the ONNX version to export the model to
do_constant_folding=True, # whether to execute constant folding for optimization
input_names=input_names, # the model's input names
output_names=output_names, # the model's output names
dynamic_axes=dynamic_axes)
Then use opencv to load, the key code is as follows
net = readNet(std::string(pth_file.toLocal8Bit()));
net.setPreferableBackend(DNN_BACKEND_OPENCV);
net.setPreferableTarget(DNN_TARGET_CPU);
cv::Mat blob;
blob = blobFromImage(img,1.0, cv::Size(inpHeight,inpWidth), cv::Scalar(104, 117, 123), true, false, CV_32F);
net.setInput(blob,"input0");
std::vector<cv::Mat> outs;
net.forward(outs, getOutputsNames(net));
std::vector<bbox_l> bbox;
bbox = post_processing(img, outs);
Issue submission checklist
Actual error message (3.4.17/4.5.5+):
DNN/ONNX(Shape): dynamic 'zero' shapes are not supportedCan opencv load dynamic input models?
An error is reported when using opencv to load the onnx model of dynamic input type.
Detailed description
The following is the compressed package of the input type and model file.
super_resolution.zip
After I converted a pytorch model to onnx, the input size was 1x3x640x640. This model can run at onnxronnuntime. Then I tried to use opencv's readNet(path) function. The model can also work normally.
When I convert this model to dynamic input, this model can also run in onnxronnuntime, but when I use opencv's readNet to load this dynamic input model, ,the program starts to report an error, the error is as follows
OpenCV: terminate handler is called! The last OpenCV error is: OpenCV(4.5.1) Error: Assertion failed (total(os[i]) > 0) in getLayerShapesRecursively, file F:\OpenCV\opencv451\sources\modules\dnn\src\dnn.cpp, line 3520If opencv can load dynamic input models,do I need to set the input and output size after the model is loaded by opencv?
If yes, is there any information on this part of the content?
Steps to reproduce
I first use python to convert the model to onnx.
The conversion code of pytorch to onnx I used is as follows
Then use opencv to load, the key code is as follows
Issue submission checklist
answers.opencv.org, Stack Overflow, etc and have not found solution