From 22b1b1edacd0e7744ad3dc8884e03e14afd09358 Mon Sep 17 00:00:00 2001 From: Abduragim Shtanchaev <44877829+Abdurrahheem@users.noreply.github.com> Date: Fri, 5 Apr 2024 16:55:23 +0400 Subject: [PATCH] Merge pull request #25071 from Abdurrahheem:ash/1D-scatter 1D Scatter Layer Test #25071 This PR introduces parametrized test for `Scatter` layer to test its functionality for 1D arrays ### 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/test/test_layers_1d.cpp | 50 +++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/modules/dnn/test/test_layers_1d.cpp b/modules/dnn/test/test_layers_1d.cpp index 3a0f9540d3..a79614c3f6 100644 --- a/modules/dnn/test/test_layers_1d.cpp +++ b/modules/dnn/test/test_layers_1d.cpp @@ -430,4 +430,54 @@ INSTANTIATE_TEST_CASE_P(/*nothing*/, Layer_Softmax_Test, Combine( testing::Values(0, 1) )); +typedef testing::TestWithParam, std::string>> Layer_Scatter_Test; +TEST_P(Layer_Scatter_Test, Accuracy1D) { + + std::vector input_shape = get<0>(GetParam()); + std::string opr = get<1>(GetParam()); + + LayerParams lp; + lp.type = "Scatter"; + lp.name = "addLayer"; + lp.set("axis", 0); + lp.set("reduction", opr); + Ptr layer = ScatterLayer::create(lp); + + cv::Mat input = cv::Mat(input_shape.size(), input_shape.data(), CV_32F); + cv::randn(input, 0.0, 1.0); + + int indices[] = {3, 2, 1, 0}; + cv::Mat indices_mat(input_shape.size(), input_shape.data(), CV_32S, indices); + cv::Mat output(input_shape.size(), input_shape.data(), CV_32F, 0.0); + + // create reference output + cv::Mat output_ref(input_shape, CV_32F, 0.0); + for (int i = 0; i < input_shape[0]; i++){ + output_ref.at(indices[i]) = input.at(i); + } + + if (opr == "add"){ + output_ref += output; + } else if (opr == "mul"){ + output_ref = output.mul(output_ref); + } else if (opr == "max"){ + cv::max(output_ref, output, output_ref); + } else if (opr == "min"){ + cv::min(output_ref, output, output_ref); + } + + std::vector inputs{output, indices_mat, input}; + std::vector outputs; + runLayer(layer, inputs, outputs); + ASSERT_EQ(outputs.size(), 1); + ASSERT_EQ(shape(output_ref), shape(outputs[0])); +} +INSTANTIATE_TEST_CASE_P(/*nothing*/, Layer_Scatter_Test, Combine( +/*input blob shape*/ testing::Values(std::vector{4}, + std::vector{1, 4}), +/*reduce*/ Values("none", "add", "mul", "max", "min") +)); + + + }}