Repository: opencv
Issue: dnn: fusion does not respect output blob requirements
Labels: bug,category: dnn,category: dnn (onnx)

##### System information (version)

- OpenCV => 4.4.0, 3.4.11

##### Detailed description

Consider the following network:

```
Convolution
BatchNorm
ReLU
 ```

ReLU should not be fused with Convolution (after BatchNorm) if the output blob of BatchNorm layer is required. But it's being fused!

I think there should also be a `pinsToKeep.count(lpNext) == 0` check in the if below:

https://github.com/opencv/opencv/blob/b698d0a6ee12342a87b8ad739d908fd8d7ff1fca/modules/dnn/src/dnn.cpp#L2583

I am not 100% sure if that is a complete fix.

##### Steps to reproduce

Requires debug printfs to be enabled here:

https://github.com/opencv/opencv/blob/b698d0a6ee12342a87b8ad739d908fd8d7ff1fca/modules/dnn/src/dnn.cpp#L2528

```
#include <opencv2/dnn.hpp>

int main ()
{
    const int in_channels = 16;
    const int in_height = 16, in_width = 16;

    int inpSz[] = {1, in_channels, in_height, in_width};
    cv::Mat input(4, &inpSz[0], CV_32F);
    cv::randu(input, -1.0f, 1.0f);

    const int num_filters = in_channels;
    const int kernel_h = 3, kernel_w = 3;
    const int pad_h = kernel_h / 2, pad_w = kernel_w / 2;

    cv::dnn::LayerParams convParams;
    convParams.set("kernel_h", kernel_h);
    convParams.set("kernel_w", kernel_w);
    convParams.set("pad_h", pad_h);
    convParams.set("pad_w", pad_w);
    convParams.set("num_output", num_filters);
    convParams.set("bias_term", true);
    convParams.type = "Convolution";
    convParams.name = "conv";

    int sz[] = {num_filters, in_channels, kernel_h, kernel_w};
    cv::Mat weights(4, &sz[0], CV_32F);
    cv::randu(weights, -1.0f, 1.0f);
    convParams.blobs.push_back(weights);

    cv::Mat bias(1, num_filters, CV_32F);
    cv::randu(bias, -1.0f, 1.0f);
    convParams.blobs.push_back(bias);

    cv::dnn::LayerParams batchNormParams;
    batchNormParams.set("has_weight", true);
    batchNormParams.set("has_bias", true);
    batchNormParams.set("eps", 1e-7);
    batchNormParams.type = "BatchNorm";
    batchNormParams.name = "bn";
    batchNormParams.blobs.push_back(bias);
    batchNormParams.blobs.push_back(bias);
    batchNormParams.blobs.push_back(bias);
    batchNormParams.blobs.push_back(bias);

    cv::dnn::LayerParams activationParams;
    activationParams.type = "ReLU";
    activationParams.name = "relu";

    cv::dnn::Net net;
    int convId = net.addLayer(convParams.name, convParams.type, convParams);
    net.addLayerToPrev(batchNormParams.name, batchNormParams.type, batchNormParams);
    net.addLayerToPrev(activationParams.name, activationParams.type, activationParams);
    net.connect(0, 0, convId, 0);

    std::vector<cv::Mat> outputs;
    std::vector<std::string> outNames = {"bn", "relu"};
    net.setInput(input);
    net.forward(outputs, outNames);
    return 0;
}
```

gives

```
analyzing _input: 
analyzing conv: Convolution
        fused with bn
        fused with relu
skipped bn: BatchNorm
skipped relu: ReLU
```

ReLU should not be fused since batch norm outputs are required.

##### 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 archite 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/
   -->