Repository: opencv
Issue: -215:Assertion failed !empty() in function 'forward'
Labels: category: dnn

##### System information (version)
<!-- Example
- OpenCV => 4.2
- Operating System / Platform => Windows 64 Bit
- Compiler => Visual Studio 2017
-->

- OpenCV => 4.5.0
- Operating System / Platform => iOS 14.1
- Compiler => :Xcode 12.1 (12A7403)

##### Detailed description

I'm getting this error:

libc++abi.dylib: terminating with uncaught exception of type cv::Exception: OpenCV(4.5.0) /Volumes/build-storage/build/master_iOS-mac/opencv/modules/dnn/src/dnn.cpp:3977: error: (-215:Assertion failed) !empty() in function 'forward'

This is triggered by net.forward() - see below please.

##### Steps to reproduce

```
using namespace cv;
using namespace std;

class CropLayer : public cv::dnn::Layer
{
public:
    CropLayer(const cv::dnn::LayerParams &params) : Layer(params) {}
    
    static cv::Ptr<cv::dnn::Layer> create(cv::dnn::LayerParams& params) {
        return cv::Ptr<cv::dnn::Layer>(new CropLayer(params));
    }
    
    virtual bool getMemoryShapes(const std::vector<std::vector<int> > &inputs,
                                 const int requiredOutputs,
                                 std::vector<std::vector<int> > &outputs,
                                 std::vector<std::vector<int> > &internals) const CV_OVERRIDE {
        CV_UNUSED(requiredOutputs); CV_UNUSED(internals);
        std::vector<int> outShape(4);
        outShape[0] = inputs[0][0];  // batch size
        outShape[1] = inputs[0][1];  // number of channels
        outShape[2] = inputs[1][2];
        outShape[3] = inputs[1][3];
        outputs.assign(1, outShape);
        return false;
    }
    
    virtual void forward(cv::InputArrayOfArrays inputs_arr,
                         cv::OutputArrayOfArrays outputs_arr,
                         cv::OutputArrayOfArrays internals_arr) CV_OVERRIDE {
        std::vector<cv::Mat> inputs, outputs;
        inputs_arr.getMatVector(inputs);
        outputs_arr.getMatVector(outputs);
        cv::Mat& inp = inputs[0];
        cv::Mat& out = outputs[0];
        int ystart = (inp.size[2] - out.size[2]) / 2;
        int xstart = (inp.size[3] - out.size[3]) / 2;
        int yend = ystart + out.size[2];
        int xend = xstart + out.size[3];
        const int batchSize = inp.size[0];
        const int numChannels = inp.size[1];
        const int height = out.size[2];
        const int width = out.size[3];
        int sz[] = { (int)batchSize, numChannels, height, width };
        out.create(4, sz, CV_32F);
        for(int i=0; i<batchSize; i++) {
            for(int j=0; j<numChannels; j++) {
                cv::Mat plane(inp.size[2], inp.size[3], CV_32F, inp.ptr<float>(i,j));
                cv::Mat crop = plane(cv::Range(ystart,yend), cv::Range(xstart,xend));
                cv::Mat targ(height, width, CV_32F, out.ptr<float>(i,j));
                crop.copyTo(targ);
            }
        }
    }    
};
```
    ```
    CV_DNN_REGISTER_LAYER_CLASS(Crop, CropLayer);
    cv::dnn::Net net;
    net = cv::dnn::readNet("hed_pretrained_bsds.caffemodel" ,"deploy.prototxt");
    void process(cv::Mat& img) {
      cv::Size reso(500,500);
      cv::Mat blob = cv::dnn::blobFromImage(img, 1.0, reso, cv::Scalar(104.00698793, 116.66876762, 122.67891434), false, false);
      net.setInput(blob);
      cv::Mat out = net.forward(); //Runtime ERROR here 
      cv::resize(out.reshape(1, reso.height), out, img.size());
      img = out;
    }
    ```
The blob's rows and cols is equal to -1, is that expected?

model: https://firebasestorage.googleapis.com/v0/b/tssst-da0a1.appspot.com/o/hed_pretrained_bsds.caffemodel?alt=media
deploy.prototxt: https://firebasestorage.googleapis.com/v0/b/tssst-da0a1.appspot.com/o/deploy.prototxt?alt=media

Thank you

##### 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/
   -->