nareshmlx/opencv-pr huggingface dataset:
**File:** doc/tutorials/dnn/dnn_OCR/dnn_OCR.markdown
**Change Type:** added
**Context:** PR #17675: [GSoC] Add digit and text recognition samples.

**Code Changes:**
```diff
@@ -0,0 +1,46 @@
+# How to run custom OCR model {#tutorial_dnn_OCR}
+
+@prev_tutorial{tutorial_dnn_custom_layers}
+
+## Introduction
+
+In this tutorial, we first introduce how to obtain the custom OCR model, then how to transform your own OCR models so that they can be run correctly by the opencv_dnn module. and finally we will provide some pre-trained models.
+
+## Train your own OCR model
```

Review comment:
This is a conversion script I provided to facilitate the conversion of the PyTorch model into the correct onnx model to be called by OpenCV.
**File:** doc/tutorials/dnn/dnn_OCR/dnn_OCR.markdown
**Change Type:** added
**Context:** PR #17675: [GSoC] Add digit and text recognition samples.

**Code Changes:**
```diff
@@ -0,0 +1,46 @@
+# How to run custom OCR model {#tutorial_dnn_OCR}
+
+@prev_tutorial{tutorial_dnn_custom_layers}
+
+## Introduction
+
+In this tutorial, we first introduce how to obtain the custom OCR model, then how to transform your own OCR models so that they can be run correctly by the opencv_dnn module. and finally we will provide some pre-trained models.
+
+## Train your own OCR model
```

Review comment:
I don't know if this is allowed in the tutorial file.
**File:** doc/tutorials/dnn/dnn_OCR/dnn_OCR.markdown
**Change Type:** added
**Context:** PR #17675: [GSoC] Add digit and text recognition samples.

**Code Changes:**
```diff
@@ -0,0 +1,46 @@
+# How to run custom OCR model {#tutorial_dnn_OCR}
+
+@prev_tutorial{tutorial_dnn_custom_layers}
+
+## Introduction
+
+In this tutorial, we first introduce how to obtain the custom OCR model, then how to transform your own OCR models so that they can be run correctly by the opencv_dnn module. and finally we will provide some pre-trained models.
+
+## Train your own OCR model
```

Review comment:
But you have only one column. Which device and framework to used to measure efficiency? If not OpenCV - I'd like to recommend to drop it.
**File:** doc/tutorials/dnn/dnn_OCR/dnn_OCR.markdown
**Change Type:** added
**Context:** PR #17675: [GSoC] Add digit and text recognition samples.

**Code Changes:**
```diff
@@ -0,0 +1,46 @@
+# How to run custom OCR model {#tutorial_dnn_OCR}
+
+@prev_tutorial{tutorial_dnn_custom_layers}
+
+## Introduction
+
+In this tutorial, we first introduce how to obtain the custom OCR model, then how to transform your own OCR models so that they can be run correctly by the opencv_dnn module. and finally we will provide some pre-trained models.
+
+## Train your own OCR model
```

Review comment:
Yes, It is in OpenCV.
**File:** doc/tutorials/dnn/dnn_OCR/dnn_OCR.markdown
**Change Type:** added
**Context:** PR #17675: [GSoC] Add digit and text recognition samples.

**Code Changes:**
```diff
@@ -0,0 +1,46 @@
+# How to run custom OCR model {#tutorial_dnn_OCR}
+
+@prev_tutorial{tutorial_dnn_custom_layers}
+
+## Introduction
+
+In this tutorial, we first introduce how to obtain the custom OCR model, then how to transform your own OCR models so that they can be run correctly by the opencv_dnn module. and finally we will provide some pre-trained models.
+
+## Train your own OCR model
```

Review comment:
I mean it's more readable to have just > After completing the model training, use [transform_to_onnx.py](https://github.com/zihaomu/deep-text-recognition-benchmark/blob/master/transform_to_onnx.py) to convert the model into onnx format.
**File:** doc/tutorials/dnn/dnn_OCR/dnn_OCR.markdown
**Change Type:** added
**Context:** PR #17675: [GSoC] Add digit and text recognition samples.

**Code Changes:**
```diff
@@ -0,0 +1,46 @@
+# How to run custom OCR model {#tutorial_dnn_OCR}
+
+@prev_tutorial{tutorial_dnn_custom_layers}
+
+## Introduction
+
+In this tutorial, we first introduce how to obtain the custom OCR model, then how to transform your own OCR models so that they can be run correctly by the opencv_dnn module. and finally we will provide some pre-trained models.
+
+## Train your own OCR model
```

Review comment:
on which device?
**File:** doc/tutorials/dnn/dnn_OCR/dnn_OCR.markdown
**Change Type:** added
**Context:** PR #17675: [GSoC] Add digit and text recognition samples.

**Code Changes:**
```diff
@@ -0,0 +1,46 @@
+# How to run custom OCR model {#tutorial_dnn_OCR}
+
+@prev_tutorial{tutorial_dnn_custom_layers}
+
+## Introduction
+
+In this tutorial, we first introduce how to obtain the custom OCR model, then how to transform your own OCR models so that they can be run correctly by the opencv_dnn module. and finally we will provide some pre-trained models.
+
+## Train your own OCR model
```

Review comment:
Every performance benchmark should be followed by drivers info, detailed HW configuration. I'd like to suggest to remove this column from the docs.
**File:** doc/tutorials/dnn/dnn_OCR/dnn_OCR.markdown
**Change Type:** added
**Context:** PR #17675: [GSoC] Add digit and text recognition samples.

**Code Changes:**
```diff
@@ -0,0 +1,46 @@
+# How to run custom OCR model {#tutorial_dnn_OCR}
+
+@prev_tutorial{tutorial_dnn_custom_layers}
+
+## Introduction
+
+In this tutorial, we first introduce how to obtain the custom OCR model, then how to transform your own OCR models so that they can be run correctly by the opencv_dnn module. and finally we will provide some pre-trained models.
+
+## Train your own OCR model
```

Review comment:
Ok, I will remove the FPS, which is HW related.
**File:** modules/ml/test/test_knearest.cpp
**Change Type:** modified
**Context:** PR #18061: Fix KD Tree kNN Implementation

**Code Changes:**
```diff
@@ -37,18 +37,31 @@ TEST(ML_KNearest, accuracy)
         EXPECT_LE(err, 0.01f);
     }
     {
-        // TODO: broken
-#if 0
         SCOPED_TRACE("KDTree");
-        Mat bestLabels;
+        Mat neighborIndexes;
         float err = 1000;
```

Review comment:
Please remove tabs. Use 4 spaces for indentation
**File:** modules/ml/src/knearest.cpp
**Change Type:** modified
**Context:** PR #18061: Fix KD Tree kNN Implementation
**Review Line:** 384

**Code Changes:**
```diff
@@ -381,36 +381,23 @@ class KDTreeImpl CV_FINAL : public Impl
         Mat res, nr, d;
         if( _results.needed() )
         {
-            _results.create(testcount, 1, CV_32F);
             res = _results.getMat();
         }
         if( _neighborResponses.needed() )
         {
-            _neighborResponses.create(testcount, k, CV_32F);
             nr = _neighborResponses.getMat();
```

Review comment:
This doesn't look valid. OpenCV API can reuse output buffers (if they are not zero). However it is not tested widely. ``` res.push_back(_res.t()); ``` will keep garbage in the beginning from the passed buffer for output.
**File:** modules/ml/src/knearest.cpp
**Change Type:** modified
**Context:** PR #18061: Fix KD Tree kNN Implementation
**Review Line:** 384

**Code Changes:**
```diff
@@ -381,36 +381,23 @@ class KDTreeImpl CV_FINAL : public Impl
         Mat res, nr, d;
         if( _results.needed() )
         {
-            _results.create(testcount, 1, CV_32F);
             res = _results.getMat();
         }
         if( _neighborResponses.needed() )
         {
-            _neighborResponses.create(testcount, k, CV_32F);
             nr = _neighborResponses.getMat();
```

Review comment:
I believe I saw some similar behavior with what you're describing when I left these lines in here - in the example of the test case with two samples, `res` would contain four values - the first two of which were garbage, and the latter two contained valid/reasonable values.
**File:** modules/ml/test/test_knearest.cpp
**Change Type:** modified
**Context:** PR #18061: Fix KD Tree kNN Implementation

**Code Changes:**
```diff
@@ -37,18 +37,31 @@ TEST(ML_KNearest, accuracy)
         EXPECT_LE(err, 0.01f);
     }
     {
-        // TODO: broken
-#if 0
         SCOPED_TRACE("KDTree");
-        Mat bestLabels;
+        Mat neighborIndexes;
         float err = 1000;
```

Review comment:
Please swap arguments for `EXPECT_EQ()` macro: - `EXPECT_EQ(expected, actual)`
**File:** modules/videoio/src/cap_ffmpeg.cpp
**Change Type:** modified
**Context:** PR #17489: FFmpeg cap consider rotation metadata 3.4

**Code Changes:**
```diff
@@ -235,7 +235,11 @@ class CvCapture_FFMPEG_proxy CV_FINAL : public cv::IVideoCapture
         if (!ffmpegCapture ||
            !icvRetrieveFrame_FFMPEG_p(ffmpegCapture, &data, &step, &width, &height, &cn))
             return false;
-        cv::Mat(height, width, CV_MAKETYPE(CV_8U, cn), data, step).copyTo(frame);
+
+        cv::Mat tmp(height, width, CV_MAKETYPE(CV_8U, cn), data, step);
+        this->rotateFrame(tmp);
+        tmp.copyTo(frame);
+
```

Review comment:
OpenCV build for Windows used FFmpeg through dedicated wrapper DLL and internal fields are not visible. You have to use `getProperty` method to get values.
**File:** modules/videoio/src/cap_ffmpeg.cpp
**Change Type:** modified
**Context:** PR #17489: FFmpeg cap consider rotation metadata 3.4

**Code Changes:**
```diff
@@ -235,7 +235,11 @@ class CvCapture_FFMPEG_proxy CV_FINAL : public cv::IVideoCapture
         if (!ffmpegCapture ||
            !icvRetrieveFrame_FFMPEG_p(ffmpegCapture, &data, &step, &width, &height, &cn))
             return false;
-        cv::Mat(height, width, CV_MAKETYPE(CV_8U, cn), data, step).copyTo(frame);
+
+        cv::Mat tmp(height, width, CV_MAKETYPE(CV_8U, cn), data, step);
+        this->rotateFrame(tmp);
+        tmp.copyTo(frame);
+
```

Review comment:
It's bad idea. The FFmpeg wrapper has pure C interface. It's done to make the wrapper agnostic to complex object size, memory layout, etc. Also it helps to control memory allocation/deletion pairs and do it with expected runtime and in expected moment of time. Most probably it's the reason of CI failures on Windows.
**File:** modules/videoio/test/test_ffmpeg.cpp
**Change Type:** modified
**Context:** PR #17489: FFmpeg cap consider rotation metadata 3.4
**Review Line:** 652

**Code Changes:**
```diff
+
+    cap.set(CAP_PROP_ORIENTATION_AUTO, true);
+    if (cap.get(CAP_PROP_ORIENTATION_AUTO) == 0)
+        throw SkipTestException("FFmpeg frame rotation metadata is not supported");
+
+    Size actual;
+    EXPECT_NO_THROW(actual = Size((int)cap.get(CAP_PROP_FRAME_WIDTH),
+                                    (int)cap.get(CAP_PROP_FRAME_HEIGHT)));
+    EXPECT_EQ(384, actual.width);
+    EXPECT_EQ(672, actual.height);
+
```

Review comment:
I suppose to check `ASSERT_TRUE(cap.get(CAP_PROP_ORIENTATION_META) == 90);` before doing next steps
**File:** modules/videoio/src/cap_ffmpeg_impl.hpp
**Change Type:** modified
**Context:** PR #17489: FFmpeg cap consider rotation metadata 3.4

**Code Changes:**
```diff
@@ -482,6 +482,7 @@ struct CvCapture_FFMPEG
     bool setProperty(int, double);
     bool grabFrame();
     bool retrieveFrame(int, unsigned char** data, int* step, int* width, int* height, int* cn);
+    void rotateFrame(cv::Mat &mat) const;
 
     void init();
 
@@ -497,6 +498,7 @@ struct CvCapture_FFMPEG
     double  r2d(AVRational r) const;
```

Review comment:
copy-paste mistake ? Is it should be ```(LIBAVUTIL_BUILD >= CALC_FFMPEG_VERSION(52, 94, 100)))``` ?
**File:** modules/videoio/src/cap_ffmpeg.cpp
**Change Type:** modified
**Context:** PR #17489: FFmpeg cap consider rotation metadata 3.4
**Review Line:** 291

**Code Changes:**
```diff
+            flag = cv::ROTATE_180;
+        } else { // Unsupported rotation
+            return;
+        }
+
+        cv::rotate(mat, mat, flag);
+    }
 };
 
 } // namespace
```

Review comment:
cv::Mat / cv::rotate is not available for FFmpeg wrapper on 3.4 branch. Build of Windows wrapper is broken
**File:** modules/dnn/test/test_onnx_importer.cpp
**Change Type:** modified
**Context:** PR #17978: Fix ONNX loading in issues opencv#17516, opencv#17531 - **Merge with extra:** opencv/opencv_extra#793 resolves #17516 resolves #17531 Also ...
**Review Line:** 627

**Code Changes:**
```diff
+        applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
+#if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2020040000)
+    applyTestTag(CV_TEST_TAG_DNN_SKIP_IE);
+#endif
+    testONNXModels("matmul_with_two_inputs");
+}
+
 INSTANTIATE_TEST_CASE_P(/*nothing*/, Test_ONNX_layers, dnnBackendsAndTargets());
 
 class Test_ONNX_nets : public Test_ONNX_layers
```

Review comment:
Please skip the tests for all the versions lower 2020.4: 2020.3 - failed https://pullrequest.opencv.org/buildbot/builders/precommit_custom_mac/builds/1053 2020.4 - passed https://pullrequest.opencv.org/buildbot/builders/precommit_custom_linux/builds/4279
**File:** modules/dnn/test/test_onnx_importer.cpp
**Change Type:** modified
**Context:** PR #17978: Fix ONNX loading in issues opencv#17516, opencv#17531 - **Merge with extra:** opencv/opencv_extra#793 resolves #17516 resolves #17531 Also ...
**Review Line:** 627

**Code Changes:**
```diff
+        applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
+#if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2020040000)
+    applyTestTag(CV_TEST_TAG_DNN_SKIP_IE);
+#endif
+    testONNXModels("matmul_with_two_inputs");
+}
+
 INSTANTIATE_TEST_CASE_P(/*nothing*/, Test_ONNX_layers, dnnBackendsAndTargets());
 
 class Test_ONNX_nets : public Test_ONNX_layers
```

Review comment:
It's OK just add skip exception (see `applyTestTag`)
**File:** modules/dnn/test/test_onnx_importer.cpp
**Change Type:** modified
**Context:** PR #17978: Fix ONNX loading in issues opencv#17516, opencv#17531 - **Merge with extra:** opencv/opencv_extra#793 resolves #17516 resolves #17531 Also ...

**Code Changes:**
```diff
@@ -606,6 +606,26 @@ TEST_P(Test_ONNX_layers, Pad2d_Unfused)
     testONNXModels("ZeroPad2d");
 }
 
+TEST_P(Test_ONNX_layers, LinearWithConstant)
+{
+    if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
+        applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
+#if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2020040000)
+    applyTestTag(CV_TEST_TAG_DNN_SKIP_IE);
```

Review comment:
Any skip tags must be applied under a condition only (`#if` or `if`). They don't work unconditionally as expected (because all tests are skipped "unconditionally"). ``` [ RUN ] Test_ONNX_layers.LinearWithConstant/0, where GetParam() = OCV/OCL [ SKIP ] Test with tag 'dnn_skip_ocl_fp16' is skipped ('dnn_skip_ocl_fp16' is in skip list) [ OK ] Test_ONNX_layers.LinearWithConstant/0 (0 ms) [ RUN ] Test_ONNX_layers.LinearWithConstant/1, where GetParam() = OCV/OCL_FP16 [ SKIP ] Test with tag 'dnn_skip_ocl_fp16' is skipped ('dnn_skip_ocl_fp16' is in skip list) [ OK ] Test_ONNX_layers.LinearWithConstant/1 (0 ms) [ RUN ] Test_ONNX_layers.LinearWithConstant/2, where GetParam() = OCV/CPU [ SKIP ] Test with tag 'dnn_skip_ocl_fp16' is skipped ('dnn_skip_ocl_fp16' is in skip list) [ OK ] Test_ONNX_layers.LinearWithConstant/2 (0 ms) [ RUN ] Test_ONNX_layers.MatmulWithTwoInputs/0, where GetParam() = OCV/OCL [ SKIP ] Test with tag 'dnn_skip_ocl_fp16' is skipped ('dnn_skip_ocl_fp16' is in skip list) [ OK ] Test_ONNX_layers.MatmulWithTwoInputs/0 (0 ms) ```
**File:** modules/dnn/src/layers/scale_layer.cpp
**Change Type:** modified
**Context:** PR #17919: Flownet2 with anysize

**Code Changes:**
```diff
@@ -15,6 +15,7 @@ Implementation of Scale layer.
 #include "../op_inf_engine.hpp"
 #include "../ie_ngraph.hpp"
 
+#include <opencv2/imgproc.hpp>
 #include <opencv2/dnn/shape_utils.hpp>
 
 namespace cv
@@ -324,7 +325,7 @@ class DataAugmentationLayerImpl CV_FINAL : public DataAugmentationLayer
                          std::vector<MatShape> &internals) const CV_OVERRIDE
```

Review comment:
Avoid adding empty lines
**File:** samples/dnn/optical_flow.py
**Change Type:** modified
**Context:** PR #17919: Flownet2 with anysize

**Code Changes:**
```diff
@@ -5,7 +5,7 @@
 Original repo:  https://github.com/lmb-freiburg/flownet2.
 
 Download the converted .caffemodel model from https://drive.google.com/open?id=16qvE9VNmU39NttpZwZs81Ga8VYQJDaWZ
-and .prototxt from https://drive.google.com/open?id=19bo6SWU2p8ZKvjXqMKiCPdK8mghwDy9b.
+and .prototxt from https://drive.google.com/file/d/1RyNIUsan1ZOh2hpYIH36A-jofAvJlT6a/view?usp=sharing.
 Otherwise download original model from https://lmb.informatik.uni-freiburg.de/resources/binaries/flownet2/flownet2-models.tar.gz,
 convert .h5 model to .caffemodel and modify original .prototxt using .prototxt from link above.
 '''
@@ -18,7 +18,7 @@
```

Review comment:
Can we use `np.ceil` instead of `math.ceil`?
**File:** samples/dnn/optical_flow.py
**Change Type:** modified
**Context:** PR #17919: Flownet2 with anysize
**Review Line:** 67

**Code Changes:**
```diff
-    parser.add_argument('--height', default=320, help='Input height')
-    parser.add_argument('--width',  default=448, help='Input width')
-    parser.add_argument('--proto', '-p', default='FlowNet2_deploy.prototxt', help='Path to prototxt.')
+    parser.add_argument('--height', default=320, type=int, help='Input height')
+    parser.add_argument('--width', default=448, type=int, help='Input width')
+    parser.add_argument('--proto', '-p', default='FlowNet2_deploy_anysize.prototxt', help='Path to prototxt.')
     parser.add_argument('--model', '-m', default='FlowNet2_weights.caffemodel', help='Path to caffemodel.')
     args, _ = parser.parse_known_args()
 
@@ -75,7 +75,25 @@ def motion_to_color(self, flow):
     cv.namedWindow(winName, cv.WINDOW_NORMAL)
```

Review comment:
Why do we need to change the default proto name?
**File:** modules/dnn/src/layers/scale_layer.cpp
**Change Type:** modified
**Context:** PR #17919: Flownet2 with anysize
**Review Line:** 355

**Code Changes:**
```diff
         Mat data_mean_cpu = blobs[1].clone();
+        Mat mean_resize = Mat(inputs[0].size[3], inputs[0].size[2], CV_32FC3);
+        Mat mean_3d = Mat(data_mean_cpu.size[3], data_mean_cpu.size[2], CV_32FC3, data_mean_cpu.ptr<float>(0));
+        resize(mean_3d, mean_resize, Size(inputs[0].size[3], inputs[0].size[2]));
+        int new_size[] = {1, mean_resize.channels(), mean_resize.cols, mean_resize.rows};
+        Mat data_mean_cpu_resize = mean_resize.reshape(1, *new_size);
         Mat data_mean_per_channel_cpu = blobs[2].clone();
 
-        const int numWeights = data_mean_cpu.total();
+        const int numWeights = data_mean_cpu_resize.total();
         CV_Assert(numWeights != 0);
```

Review comment:
Could you provide a link to this code from original implementation?
**File:** modules/dnn/src/layers/scale_layer.cpp
**Change Type:** modified
**Context:** PR #17919: Flownet2 with anysize
**Review Line:** 355

**Code Changes:**
```diff
         Mat data_mean_cpu = blobs[1].clone();
+        Mat mean_resize = Mat(inputs[0].size[3], inputs[0].size[2], CV_32FC3);
+        Mat mean_3d = Mat(data_mean_cpu.size[3], data_mean_cpu.size[2], CV_32FC3, data_mean_cpu.ptr<float>(0));
+        resize(mean_3d, mean_resize, Size(inputs[0].size[3], inputs[0].size[2]));
+        int new_size[] = {1, mean_resize.channels(), mean_resize.cols, mean_resize.rows};
+        Mat data_mean_cpu_resize = mean_resize.reshape(1, *new_size);
         Mat data_mean_per_channel_cpu = blobs[2].clone();
 
-        const int numWeights = data_mean_cpu.total();
+        const int numWeights = data_mean_cpu_resize.total();
         CV_Assert(numWeights != 0);
```

Review comment:
Actucally I think the original implementation ([code from caffe](https://github.com/lmb-freiburg/flownet2/blob/9eed763789c06fa5f44e7af79078c0446a82bfc6/src/caffe/layers/data_augmentation_layer.cu#L604)) does not consider the case and is potentially a bug. In the original implementation, `count` is calculated by `count = cropped_width_*cropped_height_*bottomchannels` which is basically the `input[0]`'s shape in our case. However, `data_mean_gpu` could have different sizes against `top_data`/`bottom_data`. In the original implementation, the authors skipped this and no error occurred because of `data_mean_gpu` is `float*` in the cuda code. However, in our case, we have to explicitly resize the `data_mean_cpu` to match the shape of the `input[0]`.
**File:** samples/dnn/optical_flow.py
**Change Type:** modified
**Context:** PR #17919: Flownet2 with anysize
**Review Line:** 67

**Code Changes:**
```diff
-    parser.add_argument('--height', default=320, help='Input height')
-    parser.add_argument('--width',  default=448, help='Input width')
-    parser.add_argument('--proto', '-p', default='FlowNet2_deploy.prototxt', help='Path to prototxt.')
+    parser.add_argument('--height', default=320, type=int, help='Input height')
+    parser.add_argument('--width', default=448, type=int, help='Input width')
+    parser.add_argument('--proto', '-p', default='FlowNet2_deploy_anysize.prototxt', help='Path to prototxt.')
     parser.add_argument('--model', '-m', default='FlowNet2_weights.caffemodel', help='Path to caffemodel.')
     args, _ = parser.parse_known_args()
 
@@ -75,7 +75,25 @@ def motion_to_color(self, flow):
     cv.namedWindow(winName, cv.WINDOW_NORMAL)
```

Review comment:
Sure we can keep the default proto name. This name (with `_anysize`) is the one I attached in the google drive.
**File:** modules/dnn/src/layers/scale_layer.cpp
**Change Type:** modified
**Context:** PR #17919: Flownet2 with anysize
**Review Line:** 355

**Code Changes:**
```diff
         Mat data_mean_cpu = blobs[1].clone();
+        Mat mean_resize = Mat(inputs[0].size[3], inputs[0].size[2], CV_32FC3);
+        Mat mean_3d = Mat(data_mean_cpu.size[3], data_mean_cpu.size[2], CV_32FC3, data_mean_cpu.ptr<float>(0));
+        resize(mean_3d, mean_resize, Size(inputs[0].size[3], inputs[0].size[2]));
+        int new_size[] = {1, mean_resize.channels(), mean_resize.cols, mean_resize.rows};
+        Mat data_mean_cpu_resize = mean_resize.reshape(1, *new_size);
         Mat data_mean_per_channel_cpu = blobs[2].clone();
 
-        const int numWeights = data_mean_cpu.total();
+        const int numWeights = data_mean_cpu_resize.total();
         CV_Assert(numWeights != 0);
```

Review comment:
To be sure of this approach, we need to add a test. Create a simple proto with the appropriate params and compare the result of Caffe and OpenCV.
**File:** samples/dnn/optical_flow.py
**Change Type:** modified
**Context:** PR #17919: Flownet2 with anysize
**Review Line:** 67

**Code Changes:**
```diff
-    parser.add_argument('--height', default=320, help='Input height')
-    parser.add_argument('--width',  default=448, help='Input width')
-    parser.add_argument('--proto', '-p', default='FlowNet2_deploy.prototxt', help='Path to prototxt.')
+    parser.add_argument('--height', default=320, type=int, help='Input height')
+    parser.add_argument('--width', default=448, type=int, help='Input width')
+    parser.add_argument('--proto', '-p', default='FlowNet2_deploy_anysize.prototxt', help='Path to prototxt.')
     parser.add_argument('--model', '-m', default='FlowNet2_weights.caffemodel', help='Path to caffemodel.')
     args, _ = parser.parse_known_args()
 
@@ -75,7 +75,25 @@ def motion_to_color(self, flow):
     cv.namedWindow(winName, cv.WINDOW_NORMAL)
```

Review comment:
In `flownet2-models.tar.gz` from https://github.com/lmb-freiburg/flownet2/blob/master/models/download-models.sh default name is `FlowNet2_deploy.prototxt.template`