From 869016d8b159bef6bddbe778dafa1a6bb8c65d3b Mon Sep 17 00:00:00 2001 From: Abduragim Shtanchaev <44877829+Abdurrahheem@users.noreply.github.com> Date: Mon, 15 Apr 2024 09:15:36 +0300 Subject: [PATCH] Merge pull request #25208 from Abdurrahheem:ash/0D-fullyConnected-test Fully connected 0D test. #25208 This PR introduces parametrized `0/1D` input support test for `Fullyconnected` 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 --- .../dnn/src/layers/fully_connected_layer.cpp | 5 +-- modules/dnn/test/test_layers_1d.cpp | 36 +++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/modules/dnn/src/layers/fully_connected_layer.cpp b/modules/dnn/src/layers/fully_connected_layer.cpp index e3d4a111c8..ba741aa662 100644 --- a/modules/dnn/src/layers/fully_connected_layer.cpp +++ b/modules/dnn/src/layers/fully_connected_layer.cpp @@ -167,10 +167,11 @@ public: cAxis = normalize_axis(axis, inputsTmp[0]); } - MatShape outShape(cAxis + 1); + MatShape outShape((!inputs[0].empty()) ? cAxis + 1 : cAxis); for (int i = 0; i < cAxis; ++i) outShape[i] = inputsTmp[0][i]; - outShape.back() = numOutput; + if (!inputs[0].empty()) + outShape.back() = numOutput; outputs.resize(1, outShape); return false; diff --git a/modules/dnn/test/test_layers_1d.cpp b/modules/dnn/test/test_layers_1d.cpp index 4dbc8ba696..4e5e82e031 100644 --- a/modules/dnn/test/test_layers_1d.cpp +++ b/modules/dnn/test/test_layers_1d.cpp @@ -567,4 +567,40 @@ INSTANTIATE_TEST_CASE_P(/*nothing*/, Layer_Slice_Test, std::vector({1, 4}) )); +typedef testing::TestWithParam>> Layer_FullyConnected_Test; +TEST_P(Layer_FullyConnected_Test, Accuracy_01D) +{ + LayerParams lp; + lp.type = "InnerProduct"; + lp.name = "InnerProductLayer"; + lp.set("num_output", 1); + lp.set("bias_term", false); + lp.set("axis", 0); + + std::vector input_shape = get<0>(GetParam()); + + RNG& rng = TS::ptr()->get_rng(); + float inp_value = rng.uniform(0.0, 10.0); + Mat weights(std::vector{total(input_shape), 1}, CV_32F, inp_value); + lp.blobs.push_back(weights); + + Ptr layer = LayerFactory::createLayerInstance("InnerProduct", lp); + + Mat input(input_shape.size(), input_shape.data(), CV_32F); + randn(input, 0, 1); + Mat output_ref = input.reshape(1, 1) * weights; + output_ref.dims = 1; + + std::vector inputs{input}; + std::vector outputs; + runLayer(layer, inputs, outputs); + normAssert(output_ref, outputs[0]); +} +INSTANTIATE_TEST_CASE_P(/*nothting*/, Layer_FullyConnected_Test, + testing::Values( + std::vector({}), + std::vector({1}), + std::vector({4}) +)); + }}