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") +)); + + + }}