From 58dc39793095c82676d1a1ff0c6c1e6fb1146646 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Sat, 27 Nov 2021 02:51:57 +0000 Subject: [PATCH] dnn(test): add two_inputs test with FP32/U8 data types - remove similar test from IE scope under HAVE_INF_ENGINE --- modules/dnn/test/test_layers.cpp | 51 --------------------------- modules/dnn/test/test_misc.cpp | 60 ++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 51 deletions(-) diff --git a/modules/dnn/test/test_layers.cpp b/modules/dnn/test/test_layers.cpp index 836b0aab9a..e61fd733f9 100644 --- a/modules/dnn/test/test_layers.cpp +++ b/modules/dnn/test/test_layers.cpp @@ -1380,57 +1380,6 @@ INSTANTIATE_TEST_CASE_P(/*nothing*/, Test_DLDT_two_inputs_3dim, Combine( testing::ValuesIn(list_sizes) )); -typedef testing::TestWithParam > > Test_DLDT_two_inputs; -TEST_P(Test_DLDT_two_inputs, as_backend) -{ - static const float kScale = 0.5f; - static const float kScaleInv = 1.0f / kScale; - - Backend backendId = get<0>(get<2>(GetParam())); - Target targetId = get<1>(get<2>(GetParam())); - - Net net; - LayerParams lp; - lp.type = "Eltwise"; - lp.name = "testLayer"; - lp.set("operation", "sum"); - int eltwiseId = net.addLayerToPrev(lp.name, lp.type, lp); // connect to a first input - net.connect(0, 1, eltwiseId, 1); // connect to a second input - - int inpSize[] = {1, 2, 3, 4}; - Mat firstInp(4, &inpSize[0], get<0>(GetParam())); - Mat secondInp(4, &inpSize[0], get<1>(GetParam())); - randu(firstInp, 0, 255); - randu(secondInp, 0, 255); - - net.setInputsNames({"data", "second_input"}); - net.setInput(firstInp, "data", kScale); - net.setInput(secondInp, "second_input", kScaleInv); - net.setPreferableBackend(backendId); - net.setPreferableTarget(targetId); - Mat out = net.forward(); - - Mat ref; - addWeighted(firstInp, kScale, secondInp, kScaleInv, 0, ref, CV_32F); - // Output values are in range [0, 637.5]. - double l1 = (targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) ? 0.06 : 1e-6; - double lInf = (targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) ? 0.3 : 1e-5; - normAssert(out, ref, "", l1, lInf); - if (cvtest::debugLevel > 0 || HasFailure()) - { - std::cout << "input1 scale=" << kScale << " input2 scale=" << kScaleInv << std::endl; - std::cout << "input1: " << firstInp.size << " " << firstInp.reshape(1, 1) << std::endl; - std::cout << "input2: " << secondInp.size << " " << secondInp.reshape(1, 1) << std::endl; - std::cout << "ref: " << ref.reshape(1, 1) << std::endl; - std::cout << "out: " << out.reshape(1, 1) << std::endl; - } -} - -INSTANTIATE_TEST_CASE_P(/*nothing*/, Test_DLDT_two_inputs, Combine( - Values(CV_8U, CV_32F), Values(CV_8U, CV_32F), - dnnBackendsAndTargets() -)); - class UnsupportedLayer : public Layer { public: diff --git a/modules/dnn/test/test_misc.cpp b/modules/dnn/test/test_misc.cpp index 11e0f0ec2d..9971450478 100644 --- a/modules/dnn/test/test_misc.cpp +++ b/modules/dnn/test/test_misc.cpp @@ -828,4 +828,64 @@ INSTANTIATE_TEST_CASE_P(/**/, Test_Model_Optimizer, #endif // HAVE_INF_ENGINE +typedef testing::TestWithParam > > Test_two_inputs; +TEST_P(Test_two_inputs, basic) +{ + static const float kScale = 0.5f; + static const float kScaleInv = 1.0f / kScale; + + Backend backendId = get<0>(get<2>(GetParam())); + Target targetId = get<1>(get<2>(GetParam())); + + Net net; + LayerParams lp; + lp.type = "Eltwise"; + lp.name = "testLayer"; + lp.set("operation", "sum"); + int eltwiseId = net.addLayerToPrev(lp.name, lp.type, lp); // connect to a first input + net.connect(0, 1, eltwiseId, 1); // connect to a second input + + int inpSize[] = {1, 2, 3, 4}; + Mat firstInp(4, &inpSize[0], get<0>(GetParam())); + Mat secondInp(4, &inpSize[0], get<1>(GetParam())); + randu(firstInp, 0, 100); + randu(secondInp, 0, 100); + +#ifndef CV_CXX11 + std::vector input_names; + input_names.push_back("data"); + input_names.push_back("second_input"); + net.setInputsNames(input_names); +#else + net.setInputsNames({"data", "second_input"}); +#endif + net.setInput(firstInp, "data", kScale); + net.setInput(secondInp, "second_input", kScaleInv); + net.setPreferableBackend(backendId); + net.setPreferableTarget(targetId); + Mat out = net.forward(); + + Mat ref; + addWeighted(firstInp, kScale, secondInp, kScaleInv, 0, ref, CV_32F); + + double l1 = (targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) ? 0.06 : 1e-6; + double lInf = (targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) ? 0.3 : 1e-5; + normAssert(out, ref, "", l1, lInf); + + if (cvtest::debugLevel > 0 || HasFailure()) + { + std::cout << "input1 scale=" << kScale << " input2 scale=" << kScaleInv << std::endl; + std::cout << "input1: " << firstInp.size << " " << firstInp.reshape(1, 1) << std::endl; + std::cout << "input2: " << secondInp.size << " " << secondInp.reshape(1, 1) << std::endl; + std::cout << "ref: " << ref.reshape(1, 1) << std::endl; + std::cout << "out: " << out.reshape(1, 1) << std::endl; + } +} + +INSTANTIATE_TEST_CASE_P(/*nothing*/, Test_two_inputs, Combine( + Values(CV_32F, CV_8U), + Values(CV_32F, CV_8U), + dnnBackendsAndTargets() +)); + }} // namespace