From 65074651a43ae798d522bab681d82a7d25740ea3 Mon Sep 17 00:00:00 2001 From: Abduragim Shtanchaev <44877829+Abdurrahheem@users.noreply.github.com> Date: Thu, 4 Apr 2024 11:36:00 +0400 Subject: [PATCH] Merge pull request #25224 from Abdurrahheem:ash/0D-concat-test Concat Layer 0/1D test #25224 This PR introduces parametrized `0/1D` input support test for `Concat` 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/concat_layer.cpp | 5 +++- modules/dnn/test/test_layers_1d.cpp | 32 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/modules/dnn/src/layers/concat_layer.cpp b/modules/dnn/src/layers/concat_layer.cpp index ecd6ff29a8..a0453842e4 100644 --- a/modules/dnn/src/layers/concat_layer.cpp +++ b/modules/dnn/src/layers/concat_layer.cpp @@ -108,7 +108,10 @@ public: } } - axisSum += curShape[cAxis]; + axisSum += (!curShape.empty()) ? curShape[cAxis] : 1; + } + if (inputs[0].empty()){ + outputs[0] = MatShape(1); } outputs[0][cAxis] = axisSum; return false; diff --git a/modules/dnn/test/test_layers_1d.cpp b/modules/dnn/test/test_layers_1d.cpp index 097bce1ecd..16cb2836fe 100644 --- a/modules/dnn/test/test_layers_1d.cpp +++ b/modules/dnn/test/test_layers_1d.cpp @@ -349,4 +349,36 @@ INSTANTIATE_TEST_CASE_P(/*nothing*/, Layer_Expand_Test, Combine( ) )); +typedef testing::TestWithParam>> Layer_Concat_Test; +TEST_P(Layer_Concat_Test, Accuracy_01D) +{ + LayerParams lp; + lp.type = "Concat"; + lp.name = "ConcatLayer"; + lp.set("axis", 0); + + Ptr layer = ConcatLayer::create(lp); + + std::vector input_shape = get<0>(GetParam()); + std::vector output_shape = {3}; + + Mat input1(input_shape.size(), input_shape.data(), CV_32F, 1.0); + Mat input2(input_shape.size(), input_shape.data(), CV_32F, 2.0); + Mat input3(input_shape.size(), input_shape.data(), CV_32F, 3.0); + + float data[] = {1.0, 2.0, 3.0}; + Mat output_ref(output_shape, CV_32F, data); + + std::vector inputs{input1, input2, input3}; + std::vector outputs; + + runLayer(layer, inputs, outputs); + ASSERT_EQ(shape(output_ref), shape(outputs[0])); + normAssert(output_ref, outputs[0]); +} +INSTANTIATE_TEST_CASE_P(/*nothing*/, Layer_Concat_Test, +/*input blob shape*/ testing::Values( + std::vector({}), + std::vector({1}) +)); }}