From 5260b486953935081bd8cc6f3c64caa607358312 Mon Sep 17 00:00:00 2001 From: Abduragim Shtanchaev <44877829+Abdurrahheem@users.noreply.github.com> Date: Wed, 15 May 2024 11:26:26 +0400 Subject: [PATCH] Merge pull request #25390 from Abdurrahheem:ash/0d-padding-layer 1/0D test padding layer #25390 This PR introduces 0/1D test for `padding` layer. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake --- modules/dnn/src/layers/padding_layer.cpp | 6 +++ modules/dnn/test/test_layers_1d.cpp | 61 ++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/modules/dnn/src/layers/padding_layer.cpp b/modules/dnn/src/layers/padding_layer.cpp index 4e864c1f6694..ecc5b3519aa1 100644 --- a/modules/dnn/src/layers/padding_layer.cpp +++ b/modules/dnn/src/layers/padding_layer.cpp @@ -58,7 +58,13 @@ class PaddingLayerImpl CV_FINAL : public PaddingLayer { CV_Assert(inputs.size() == 1); const MatShape& inpShape = inputs[0]; + if (inpShape.empty()){ + CV_Assert(paddings.size() == 1); + outputs.resize(1, MatShape(1, paddings[0].first + paddings[0].second + 1)); + return false; + } CV_Assert(inpShape.size() >= paddings.size()); + CV_Assert(inputDims == -1 || inpShape.size() == inputDims || inpShape.size() > paddings.size()); outputs.resize(1, inpShape); diff --git a/modules/dnn/test/test_layers_1d.cpp b/modules/dnn/test/test_layers_1d.cpp index 6e826ba1ce46..1a46221e3cde 100644 --- a/modules/dnn/test/test_layers_1d.cpp +++ b/modules/dnn/test/test_layers_1d.cpp @@ -567,6 +567,67 @@ INSTANTIATE_TEST_CASE_P(/*nothing*/, Layer_Slice_Test, std::vector({1, 4}) )); +typedef testing::TestWithParam>> Layer_Padding_Test; +TEST_P(Layer_Padding_Test, Accuracy_01D){ + + std::vector input_shape = get<0>(GetParam()); + float pad_value = 10; + + LayerParams lp; + lp.type = "Padding"; + lp.name = "PaddingLayer"; + std::vector paddings = {5, 3}; // Pad before and pad after for one dimension + lp.set("paddings", DictValue::arrayInt(paddings.data(), paddings.size())); + lp.set("value", pad_value); + lp.set("input_dims", (input_shape.size() == 1) ? -1 : 0); + Ptr layer = PaddingLayer::create(lp); + + cv::Mat input(input_shape.size(), input_shape.data(), CV_32F); + cv::randn(input, 0.0, 1.0); + + + // Fill in the padding values manually + // Create output ref shape depending on the input shape and input_dims + std::vector output_shape; + if (input_shape.size() == 0){ + output_shape = {1 + paddings[0] + paddings[1]}; + } else if (input_shape.size() == 1){ + output_shape = {input_shape[0] + paddings[0] + paddings[1]}; + } else { + output_shape = {input_shape[0], input_shape[1] + paddings[0] + paddings[1]}; + } + + cv::Mat output_ref(output_shape.size(), output_shape.data(), CV_32F, pad_value); + + if (input_shape.size() == 0){ + output_ref.at(paddings[0]) = input.at(0); + } else if (input_shape.size() == 1){ + for (int i = 0; i < input_shape[0]; ++i){ + output_ref.at(i + paddings[0]) = input.at(i); + } + } else { + for (int i = 0; i < input_shape[0]; ++i){ + for (int j = 0; j < input_shape[1]; ++j){ + output_ref.at(i, j + paddings[0]) = input.at(i, j); + } + } + } + + std::vector inputs{input}; + std::vector outputs; + runLayer(layer, inputs, outputs); + ASSERT_EQ(1, outputs.size()); + ASSERT_EQ(shape(output_ref), shape(outputs[0])); + normAssert(output_ref, outputs[0]); +} +INSTANTIATE_TEST_CASE_P(/*nothing*/, Layer_Padding_Test, +/*input blob shape*/ testing::Values( + std::vector{}, + std::vector{1}, + std::vector{1, 4}, + std::vector{4, 1} +)); + typedef testing::TestWithParam>> Layer_FullyConnected_Test; TEST_P(Layer_FullyConnected_Test, Accuracy_01D) {