Repository: opencv
Issue: Opencv loads models of dynamic input types
Labels: bug,feature,category: dnn,confirmed

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

<!-- your description -->

The following is the compressed package of the input type and model file.

![image](https://user-images.githubusercontent.com/40584518/104946382-eb546100-59f4-11eb-86f5-988aec87323b.png)


[super_resolution.zip](https://github.com/opencv/opencv/files/5831810/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

<!-- to add code example fence it with triple backticks and optional file extension
    ```.cpp
    // C++ code example
    ```
 or attach as .txt or .zip file
-->
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

 - [x] I report the issue, it's not a question
   <!--
   OpenCV team works with answers.opencv.org, Stack Overflow and other communities
   to discuss problems. Tickets with question without real issue statement will be
   closed.
   -->
 - [x] I checked the problem with documentation, FAQ, open issues,
       answers.opencv.org, Stack Overflow, etc and have not found solution
   <!--
   Places to check:
   * OpenCV documentation: https://docs.opencv.org
   * FAQ page: https://github.com/opencv/opencv/wiki/FAQ
   * OpenCV forum: https://answers.opencv.org
   * OpenCV issue tracker: https://github.com/opencv/opencv/issues?q=is%3Aissue
   * Stack Overflow branch: https://stackoverflow.com/questions/tagged/opencv
   -->
 - [x] I updated to latest OpenCV version and the issue is still there
   <!--
   master branch for OpenCV 4.x and 3.4 branch for OpenCV 3.x releases.
   OpenCV team supports only latest release for each branch.
   The ticket is closed, if the problem is not reproduced with modern version.
   -->
 - [x] There is reproducer code and related data files: videos, images, onnx, etc
   <!--
   The best reproducer -- test case for OpenCV that we can add to the library.
   Recommendations for media files and binary files:
   * Try to reproduce the issue with images and videos in opencv_extra repository
     to reduce attachment size
   * Use PNG for images, if you report some CV related bug, but not image reader
     issue
   * Attach the image as archive to the ticket, if you report some reader issue.
     Image hosting services compress images and it breaks the repro code.
   * Provide ONNX file for some public model or ONNX file with with random weights,
     if you report ONNX parsing or handling issue. Architecture details diagram
     from netron tool can be very useful too. See https://lutzroeder.github.io/netron/
   -->