1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2017, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of the copyright holders may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
42 #include "test_precomp.hpp"
43 #include <opencv2/core/ocl.hpp>
44 #include "npy_blob.hpp"
45 #include <opencv2/dnn/shape_utils.hpp>
46 #include <opencv2/dnn/all_layers.hpp>
47 #include <opencv2/dnn/layer.details.hpp> // CV_DNN_REGISTER_LAYER_CLASS
49 #ifdef HAVE_INF_ENGINE
53 namespace opencv_test { namespace {
55 template<typename TString>
56 static String _tf(TString filename)
58 String basetestdir = getOpenCVExtraDir();
59 size_t len = basetestdir.size();
60 if(len > 0 && basetestdir[len-1] != '/' && basetestdir[len-1] != '\\')
61 return (basetestdir + "/dnn/layers") + filename;
62 return (basetestdir + "dnn/layers/") + filename;
65 void runLayer(Ptr<Layer> layer, std::vector<Mat> &inpBlobs, std::vector<Mat> &outBlobs)
67 size_t ninputs = inpBlobs.size();
68 std::vector<Mat> inp(ninputs), outp, intp;
69 std::vector<MatShape> inputs, outputs, internals;
71 for (size_t i = 0; i < ninputs; i++)
73 inp[i] = inpBlobs[i].clone();
74 inputs.push_back(shape(inp[i]));
77 layer->getMemoryShapes(inputs, 0, outputs, internals);
78 for (size_t i = 0; i < outputs.size(); i++)
80 outp.push_back(Mat(outputs[i], CV_32F));
82 for (size_t i = 0; i < internals.size(); i++)
84 intp.push_back(Mat(internals[i], CV_32F));
87 layer->finalize(inp, outp);
88 layer->forward(inp, outp, intp);
90 size_t noutputs = outp.size();
91 outBlobs.resize(noutputs);
92 for (size_t i = 0; i < noutputs; i++)
93 outBlobs[i] = outp[i];
96 class Test_Caffe_layers : public DNNTestLayer
99 void testLayerUsingCaffeModels(const String& basename, bool useCaffeModel = false,
100 bool useCommonInputBlob = true, double l1 = 0.0, double lInf = 0.0,
101 int numInps = 1, int numOuts = 1)
103 CV_Assert_N(numInps >= 1, numInps <= 10, numOuts >= 1, numOuts <= 10);
104 String prototxt = _tf(basename + ".prototxt");
105 String caffemodel = _tf(basename + ".caffemodel");
107 std::vector<Mat> inps, refs, outs;
111 for (int i = 0; i < numInps; i++)
113 String inpfile = _tf(basename + ".input_" + (i + '0') + ".npy");
114 inps.push_back(blobFromNPY(inpfile));
119 String inpfile = (useCommonInputBlob) ? _tf("blob.npy") : _tf(basename + ".input.npy");
120 inps.push_back(blobFromNPY(inpfile));
125 for (int i = 0; i < numOuts; i++)
127 String outfile = _tf(basename + "_" + (i + '0') + ".npy");
128 refs.push_back(blobFromNPY(outfile));
133 String outfile = _tf(basename + ".npy");
134 refs.push_back(blobFromNPY(outfile));
137 Net net = readNetFromCaffe(prototxt, (useCaffeModel) ? caffemodel : String());
138 ASSERT_FALSE(net.empty());
139 checkBackend(&inps[0], &refs[0]);
141 net.setPreferableBackend(backend);
142 net.setPreferableTarget(target);
144 String inp_name = "input";
147 for (int i = 0; i < numInps; i++)
149 net.setInput(inps[i], inp_name + "_" + (i + '0'));
154 net.setInput(inps.back(), inp_name);
158 for (int i = 0; i < refs.size(); i++)
160 normAssert(refs[i], outs[i], "", l1 ? l1 : default_l1, lInf ? lInf : default_lInf);
165 TEST_P(Test_Caffe_layers, Softmax)
167 testLayerUsingCaffeModels("layer_softmax");
170 TEST_P(Test_Caffe_layers, LRN)
172 testLayerUsingCaffeModels("layer_lrn_spatial");
173 testLayerUsingCaffeModels("layer_lrn_channels");
176 TEST_P(Test_Caffe_layers, Convolution)
178 testLayerUsingCaffeModels("layer_convolution", true);
181 TEST_P(Test_Caffe_layers, DeConvolution)
183 testLayerUsingCaffeModels("layer_deconvolution", true, false);
186 TEST_P(Test_Caffe_layers, InnerProduct)
188 if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
189 applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
190 if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
191 applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
193 if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
194 applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
195 testLayerUsingCaffeModels("layer_inner_product", true);
198 TEST_P(Test_Caffe_layers, Pooling_max)
200 testLayerUsingCaffeModels("layer_pooling_max");
203 TEST_P(Test_Caffe_layers, Pooling_ave)
205 testLayerUsingCaffeModels("layer_pooling_ave");
208 TEST_P(Test_Caffe_layers, MVN)
210 testLayerUsingCaffeModels("layer_mvn");
213 void testReshape(const MatShape& inputShape, const MatShape& targetShape,
214 int axis = 0, int num_axes = -1,
215 MatShape mask = MatShape())
218 params.set("axis", axis);
219 params.set("num_axes", num_axes);
222 params.set("dim", DictValue::arrayInt<int*>(&mask[0], mask.size()));
225 Mat inp(inputShape.size(), &inputShape[0], CV_32F);
226 std::vector<Mat> inpVec(1, inp);
227 std::vector<Mat> outVec, intVec;
229 Ptr<Layer> rl = LayerFactory::createLayerInstance("Reshape", params);
230 runLayer(rl, inpVec, outVec);
232 Mat& out = outVec[0];
233 MatShape shape(out.size.p, out.size.p + out.dims);
234 EXPECT_EQ(shape, targetShape);
237 TEST(Layer_Test_Reshape, Accuracy)
240 int inp[] = {4, 3, 1, 2};
241 int out[] = {4, 3, 2};
242 testReshape(MatShape(inp, inp + 4), MatShape(out, out + 3), 2, 1);
245 int inp[] = {1, 128, 4, 4};
246 int out[] = {1, 2048};
247 int mask[] = {-1, 2048};
248 testReshape(MatShape(inp, inp + 4), MatShape(out, out + 2), 0, -1,
249 MatShape(mask, mask + 2));
252 int inp[] = {1, 2, 3};
253 int out[] = {3, 1, 2};
254 int mask[] = {3, 1, 2};
255 testReshape(MatShape(inp, inp + 3), MatShape(out, out + 3), 0, -1,
256 MatShape(mask, mask + 3));
260 TEST_P(Test_Caffe_layers, BatchNorm)
262 testLayerUsingCaffeModels("layer_batch_norm", true);
263 testLayerUsingCaffeModels("layer_batch_norm_local_stats", true, false);
266 TEST_P(Test_Caffe_layers, ReLU)
268 testLayerUsingCaffeModels("layer_relu");
271 TEST_P(Test_Caffe_layers, Dropout)
273 testLayerUsingCaffeModels("layer_dropout");
276 TEST_P(Test_Caffe_layers, Concat)
278 #if defined(INF_ENGINE_RELEASE)
279 #if INF_ENGINE_VER_MAJOR_GE(2019010000) && INF_ENGINE_VER_MAJOR_LT(2019020000)
280 if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD)
281 applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
282 #elif INF_ENGINE_VER_MAJOR_EQ(2019020000)
283 if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 &&
284 (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16))
285 applyTestTag(target == DNN_TARGET_OPENCL ? CV_TEST_TAG_DNN_SKIP_IE_OPENCL : CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16,
286 CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
289 if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH &&
290 (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16))
291 applyTestTag(target == DNN_TARGET_OPENCL ? CV_TEST_TAG_DNN_SKIP_IE_OPENCL : CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16,
292 CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
295 testLayerUsingCaffeModels("layer_concat");
296 testLayerUsingCaffeModels("layer_concat_optim", true, false);
297 testLayerUsingCaffeModels("layer_concat_shared_input", true, false);
300 TEST_P(Test_Caffe_layers, Fused_Concat)
302 if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16))
303 applyTestTag(target == DNN_TARGET_OPENCL ? CV_TEST_TAG_DNN_SKIP_IE_OPENCL : CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16,
304 CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
321 lp.name = "someLayer";
322 interLayer = net.addLayerToPrev(lp.name, lp.type, lp);
328 lp.name = "testConcat";
329 int id = net.addLayer(lp.name, lp.type, lp);
330 net.connect(interLayer, 0, id, 0);
331 net.connect(interLayer, 0, id, 1);
333 int shape[] = {1, 2, 3, 4};
334 Mat input(4, shape, CV_32F);
335 randu(input, 0.0f, 1.0f); // [0, 1] to make AbsVal an identity transformation.
338 net.setPreferableBackend(backend);
339 net.setPreferableTarget(target);
340 Mat out = net.forward();
342 normAssert(slice(out, Range::all(), Range(0, 2), Range::all(), Range::all()), input, "", default_l1, default_lInf);
343 normAssert(slice(out, Range::all(), Range(2, 4), Range::all(), Range::all()), input, "", default_l1, default_lInf);
346 TEST_P(Test_Caffe_layers, Eltwise)
348 if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD)
349 applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD);
350 testLayerUsingCaffeModels("layer_eltwise");
353 TEST_P(Test_Caffe_layers, PReLU)
355 double lInf = (target == DNN_TARGET_MYRIAD || target == DNN_TARGET_OPENCL_FP16) ? 0.021 : 0.0;
356 testLayerUsingCaffeModels("layer_prelu", true, true, 0.0, lInf);
359 // TODO: fix an unstable test case
360 TEST_P(Test_Caffe_layers, layer_prelu_fc)
362 if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
363 applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
364 // Reference output values are in range [-0.0001, 10.3906]
365 double l1 = (target == DNN_TARGET_MYRIAD) ? 0.005 : 0.0;
366 double lInf = (target == DNN_TARGET_MYRIAD) ? 0.021 : 0.0;
367 #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2020040000)
368 if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL)
370 l1 = 0.006f; lInf = 0.05f;
372 if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL_FP16)
374 l1 = 0.01f; lInf = 0.05f;
377 testLayerUsingCaffeModels("layer_prelu_fc", true, false, l1, lInf);
380 TEST_P(Test_Caffe_layers, Reshape_Split_Slice)
382 if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
383 applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
384 if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
385 applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
387 Net net = readNetFromCaffe(_tf("reshape_and_slice_routines.prototxt"));
388 ASSERT_FALSE(net.empty());
390 net.setPreferableBackend(backend);
391 net.setPreferableTarget(target);
393 Mat input(6, 12, CV_32F);
395 rng.fill(input, RNG::UNIFORM, -1, 1);
397 net.setInput(input, "input");
398 Mat output = net.forward("output");
400 normAssert(input, output, "", default_l1, default_lInf);
403 TEST_P(Test_Caffe_layers, Conv_Elu)
405 #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_RELEASE <= 2018050000
406 if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD)
407 applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
410 Net net = readNetFromTensorflow(_tf("layer_elu_model.pb"));
411 ASSERT_FALSE(net.empty());
413 Mat inp = blobFromNPY(_tf("layer_elu_in.npy"));
414 Mat ref = blobFromNPY(_tf("layer_elu_out.npy"));
416 net.setInput(inp, "input");
417 net.setPreferableBackend(backend);
418 net.setPreferableTarget(target);
419 Mat out = net.forward();
421 normAssert(ref, out, "", default_l1, default_lInf);
424 class Layer_LSTM_Test : public ::testing::Test
429 Ptr<LSTMLayer> layer;
430 std::vector<Mat> inputs, outputs;
434 void init(const MatShape &inpShape_, const MatShape &outShape_,
435 bool produceCellOutput, bool useTimestampDim)
437 numInp = total(inpShape_);
438 numOut = total(outShape_);
440 Wh = Mat::ones(4 * numOut, numOut, CV_32F);
441 Wx = Mat::ones(4 * numOut, numInp, CV_32F);
442 b = Mat::ones(4 * numOut, 1, CV_32F);
449 lp.set<bool>("produce_cell_output", produceCellOutput);
450 lp.set<bool>("use_timestamp_dim", useTimestampDim);
452 layer = LSTMLayer::create(lp);
453 layer->setOutShape(outShape_);
457 TEST_F(Layer_LSTM_Test, get_set_test)
460 MatShape inpShape = shape(5, 3, 2);
461 MatShape outShape = shape(3, 1, 2);
462 MatShape inpResShape = concat(shape(TN), inpShape);
463 MatShape outResShape = concat(shape(TN), outShape);
465 init(inpShape, outShape, true, false);
466 layer->setOutShape(outShape);
468 Mat C((int)outResShape.size(), &outResShape[0], CV_32F);
473 Mat inp((int)inpResShape.size(), &inpResShape[0], CV_32F);
476 inputs.push_back(inp);
477 runLayer(layer, inputs, outputs);
479 EXPECT_EQ(2u, outputs.size());
481 print(outResShape, "outResShape");
482 print(shape(outputs[0]), "out0");
483 print(shape(outputs[0]), "out1");
485 EXPECT_EQ(outResShape, shape(outputs[0]));
486 EXPECT_EQ(outResShape, shape(outputs[1]));
488 EXPECT_EQ(0, layer->inputNameToIndex("x"));
489 EXPECT_EQ(0, layer->outputNameToIndex("h"));
490 EXPECT_EQ(1, layer->outputNameToIndex("c"));
493 TEST(Layer_LSTM_Test_Accuracy_with_, CaffeRecurrent)
497 lp.blobs[0] = blobFromNPY(_tf("lstm.prototxt.w_2.npy")); // Wh
498 lp.blobs[1] = blobFromNPY(_tf("lstm.prototxt.w_0.npy")); // Wx
499 lp.blobs[2] = blobFromNPY(_tf("lstm.prototxt.w_1.npy")); // bias
500 Ptr<LSTMLayer> layer = LSTMLayer::create(lp);
502 Mat inp = blobFromNPY(_tf("recurrent.input.npy"));
503 std::vector<Mat> inputs(1, inp), outputs;
504 runLayer(layer, inputs, outputs);
506 Mat h_t_reference = blobFromNPY(_tf("lstm.prototxt.h_1.npy"));
507 normAssert(h_t_reference, outputs[0]);
510 TEST(Layer_RNN_Test_Accuracy_with_, CaffeRecurrent)
512 Ptr<RNNLayer> layer = RNNLayer::create(LayerParams());
515 blobFromNPY(_tf("rnn.prototxt.w_0.npy")),
516 blobFromNPY(_tf("rnn.prototxt.w_1.npy")),
517 blobFromNPY(_tf("rnn.prototxt.w_2.npy")),
518 blobFromNPY(_tf("rnn.prototxt.w_3.npy")),
519 blobFromNPY(_tf("rnn.prototxt.w_4.npy")) );
521 std::vector<Mat> output, input(1, blobFromNPY(_tf("recurrent.input.npy")));
522 runLayer(layer, input, output);
524 Mat h_ref = blobFromNPY(_tf("rnn.prototxt.h_1.npy"));
525 normAssert(h_ref, output[0]);
528 TEST(Layer_LSTM_Test_Accuracy_, Reverse)
530 // This handcrafted setup calculates (approximately) the prefix sum of the
531 // input, assuming the inputs are suitably small.
532 cv::Mat input(2, 1, CV_32FC1);
533 input.at<float>(0, 0) = 1e-5f;
534 input.at<float>(1, 0) = 2e-5f;
536 cv::Mat Wx(4, 1, CV_32FC1);
537 Wx.at<float>(0, 0) = 0.f; // Input gate
538 Wx.at<float>(1, 0) = 0.f; // Forget gate
539 Wx.at<float>(2, 0) = 0.f; // Output gate
540 Wx.at<float>(3, 0) = 1.f; // Update signal
542 cv::Mat Wh(4, 1, CV_32FC1);
543 Wh.at<float>(0, 0) = 0.f; // Input gate
544 Wh.at<float>(1, 0) = 0.f; // Forget gate
545 Wh.at<float>(2, 0) = 0.f; // Output gate
546 Wh.at<float>(3, 0) = 0.f; // Update signal
548 cv::Mat bias(4, 1, CV_32FC1);
549 bias.at<float>(0, 0) = 1e10f; // Input gate - always allows input to c
550 bias.at<float>(1, 0) = 1e10f; // Forget gate - never forget anything on c
551 bias.at<float>(2, 0) = 1e10f; // Output gate - always output everything
552 bias.at<float>(3, 0) = 0.f; // Update signal
555 lp.set("reverse", true);
556 lp.set("use_timestamp_dim", true);
558 lp.blobs.push_back(Wh);
559 lp.blobs.push_back(Wx);
560 lp.blobs.push_back(bias);
562 cv::Ptr<cv::dnn::LSTMLayer> layer = LSTMLayer::create(lp);
563 std::vector<cv::Mat> outputs;
564 std::vector<cv::Mat> inputs;
565 inputs.push_back(input);
566 runLayer(layer, inputs, outputs);
568 ASSERT_EQ(1, outputs.size());
569 cv::Mat out = outputs[0];
570 ASSERT_EQ(3, out.dims);
571 ASSERT_EQ(shape(2, 1, 1), shape(out));
572 float* data = reinterpret_cast<float*>(out.data);
573 EXPECT_NEAR(std::tanh(1e-5f) + std::tanh(2e-5f), data[0], 1e-10);
574 EXPECT_NEAR(std::tanh(2e-5f), data[1], 1e-10);
578 class Layer_RNN_Test : public ::testing::Test
581 int nX, nH, nO, nT, nS;
582 Mat Whh, Wxh, bh, Who, bo;
585 std::vector<Mat> inputs, outputs;
595 Whh = Mat::ones(nH, nH, CV_32F);
596 Wxh = Mat::ones(nH, nX, CV_32F);
597 bh = Mat::ones(nH, 1, CV_32F);
598 Who = Mat::ones(nO, nH, CV_32F);
599 bo = Mat::ones(nO, 1, CV_32F);
601 layer = RNNLayer::create(LayerParams());
602 layer->setProduceHiddenOutput(true);
603 layer->setWeights(Wxh, bh, Whh, Who, bo);
607 TEST_F(Layer_RNN_Test, get_set_test)
609 int sz[] = { nT, nS, 1, nX };
610 Mat inp(4, sz, CV_32F);
612 inputs.push_back(inp);
613 runLayer(layer, inputs, outputs);
615 EXPECT_EQ(outputs.size(), 2u);
616 EXPECT_EQ(shape(outputs[0]), shape(nT, nS, nO));
617 EXPECT_EQ(shape(outputs[1]), shape(nT, nS, nH));
620 TEST_P(Test_Caffe_layers, Accum)
622 if (backend == DNN_BACKEND_OPENCV && target != DNN_TARGET_CPU)
623 applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL, CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
625 testLayerUsingCaffeModels("accum", false, false, 0.0, 0.0, 2);
626 testLayerUsingCaffeModels("accum_ref", false, false, 0.0, 0.0, 2);
629 TEST_P(Test_Caffe_layers, FlowWarp)
631 if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
632 applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
634 testLayerUsingCaffeModels("flow_warp", false, false, 0.0, 0.0, 2);
637 TEST_P(Test_Caffe_layers, ChannelNorm)
639 if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
640 applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
641 testLayerUsingCaffeModels("channel_norm", false, false);
644 TEST_P(Test_Caffe_layers, DataAugmentation)
646 if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
647 applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
648 testLayerUsingCaffeModels("data_augmentation", true, false);
649 testLayerUsingCaffeModels("data_augmentation_2x1", true, false);
650 testLayerUsingCaffeModels("data_augmentation_8x6", true, false);
653 TEST_P(Test_Caffe_layers, Resample)
655 if (backend != DNN_BACKEND_OPENCV)
656 applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
657 testLayerUsingCaffeModels("nearest_2inps", false, false, 0.0, 0.0, 2);
658 testLayerUsingCaffeModels("nearest", false, false);
661 TEST_P(Test_Caffe_layers, Correlation)
663 if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
664 applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER,
665 CV_TEST_TAG_DNN_SKIP_OPENCL, CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
666 testLayerUsingCaffeModels("correlation", false, false, 0.0, 0.0, 2);
669 TEST_P(Test_Caffe_layers, Convolution2Inputs)
671 testLayerUsingCaffeModels("conv_2_inps", true, false, 0.0, 0.0, 2);
674 TEST_P(Test_Caffe_layers, ROIPooling_Accuracy)
676 Net net = readNetFromCaffe(_tf("net_roi_pooling.prototxt"));
677 ASSERT_FALSE(net.empty());
679 Mat inp = blobFromNPY(_tf("net_roi_pooling.input.npy"));
680 Mat rois = blobFromNPY(_tf("net_roi_pooling.rois.npy"));
681 Mat ref = blobFromNPY(_tf("net_roi_pooling.npy"));
683 checkBackend(&inp, &ref);
685 net.setPreferableBackend(backend);
686 net.setPreferableTarget(target);
688 net.setInput(inp, "input");
689 net.setInput(rois, "rois");
691 Mat out = net.forward();
693 double l1 = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 1e-3 : 1e-5;
694 double lInf = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 1e-3 : 1e-4;
695 normAssert(out, ref, "", l1, lInf);
698 TEST_P(Test_Caffe_layers, FasterRCNN_Proposal)
700 if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
701 applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
702 if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
703 applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
704 if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
705 applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
707 Net net = readNetFromCaffe(_tf("net_faster_rcnn_proposal.prototxt"));
709 Mat scores = blobFromNPY(_tf("net_faster_rcnn_proposal.scores.npy"));
710 Mat deltas = blobFromNPY(_tf("net_faster_rcnn_proposal.deltas.npy"));
711 Mat imInfo = (Mat_<float>(1, 3) << 600, 800, 1.6f);
713 net.setInput(scores, "rpn_cls_prob_reshape");
714 net.setInput(deltas, "rpn_bbox_pred");
715 net.setInput(imInfo, "im_info");
717 std::vector<Mat> outs;
718 net.setPreferableBackend(backend);
719 net.setPreferableTarget(target);
720 net.forward(outs, "output");
722 for (int i = 0; i < 2; ++i)
724 Mat ref = blobFromNPY(_tf(i == 0 ? "net_faster_rcnn_proposal.out_rois.npy" :
725 "net_faster_rcnn_proposal.out_scores.npy"));
726 const int numDets = ref.size[0];
727 EXPECT_LE(numDets, outs[i].size[0]);
728 normAssert(outs[i].rowRange(0, numDets), ref);
730 if (numDets < outs[i].size[0])
732 EXPECT_EQ(countNonZero(outs[i].rowRange(numDets, outs[i].size[0])), 0);
737 typedef testing::TestWithParam<tuple<Vec4i, Vec2i, bool> > Scale_untrainable;
738 TEST_P(Scale_untrainable, Accuracy)
740 Vec4i inpShapeVec = get<0>(GetParam());
741 int axis = get<1>(GetParam())[0];
742 int weightsDims = get<1>(GetParam())[1];
743 bool testFusion = get<2>(GetParam());
744 const int inpShape[] = {inpShapeVec[0], inpShapeVec[1], inpShapeVec[2], inpShapeVec[3]};
746 // Create a network with two inputs. Scale layer multiplies a first input to
747 // a second one. See http://caffe.berkeleyvision.org/tutorial/layers/scale.html
749 // Check that this version of Scale layer won't be fused with Convolution layer.
753 lp.set("kernel_size", 1);
754 lp.set("num_output", 3);
756 lp.set("bias_term", false);
757 lp.type = "Convolution";
758 lp.name = "testConv";
760 std::vector<int> weightsShape(4);
761 weightsShape[0] = 3; // #outChannels
762 weightsShape[1] = 1; // #inpChannels / group
763 weightsShape[2] = 1; // height
764 weightsShape[3] = 1; // width
765 Mat weights(weightsShape, CV_32F);
767 lp.blobs.push_back(weights);
768 net.addLayerToPrev(lp.name, lp.type, lp);
772 lp.name = "testLayer";
773 lp.set("axis", axis);
774 int id = net.addLayerToPrev(lp.name, lp.type, lp);
775 net.connect(0, 1, id, 1);
777 Mat input(4, inpShape, CV_32F);
778 Mat weights(weightsDims, &inpShape[axis], CV_32F);
780 randu(weights, -1, 1);
782 std::vector<String> inpNames(2);
783 inpNames[0] = "scale_input";
784 inpNames[1] = "scale_weights";
785 net.setInputsNames(inpNames);
786 net.setInput(input, inpNames[0]);
787 net.setInput(weights, inpNames[1]);
788 net.setPreferableBackend(DNN_BACKEND_OPENCV);
789 Mat out = net.forward();
791 Mat ref(input.dims, input.size, CV_32F);
792 float* inpData = (float*)input.data;
793 float* refData = (float*)ref.data;
794 float* weightsData = (float*)weights.data;
796 for (int i = axis + weightsDims; i < 4; ++i)
797 spatialSize *= inpShape[i];
798 for (int i = 0; i < ref.total(); ++i)
800 float w = weightsData[(i / spatialSize) % weights.total()];
801 refData[i] = inpData[i] * w;
803 normAssert(out, ref);
806 INSTANTIATE_TEST_CASE_P(Layer_Test, Scale_untrainable, Combine(
807 /*input size*/ Values(Vec4i(2, 3, 4, 5)),
808 /*axis, #dims*/ Values(Vec2i(0, 1), Vec2i(0, 2), Vec2i(0, 3), Vec2i(0, 4),
809 Vec2i(1, 1), Vec2i(1, 2), Vec2i(1, 3),
810 Vec2i(2, 1), Vec2i(2, 2),
812 /*conv fusion*/ testing::Bool()
815 typedef testing::TestWithParam<tuple<Vec4i, Vec4i, int, int, int> > Crop;
816 TEST_P(Crop, Accuracy)
818 Vec4i inpShapeVec = get<0>(GetParam());
819 Vec4i sizShapeVec = get<1>(GetParam());
820 int axis = get<2>(GetParam());
821 int numOffsets = get<3>(GetParam());
822 int offsetVal = get<4>(GetParam());
823 const int inpShape[] = {inpShapeVec[0], inpShapeVec[1], inpShapeVec[2], inpShapeVec[3]};
824 const int sizShape[] = {sizShapeVec[0], sizShapeVec[1], sizShapeVec[2], sizShapeVec[3]};
826 // Create a network with two inputs. Crop layer crops a first input to
827 // the size of a second one.
828 // See http://caffe.berkeleyvision.org/tutorial/layers/crop.html
832 lp.name = "testCrop";
834 lp.set("axis", axis);
837 std::vector<int> offsets(numOffsets, offsetVal);
838 lp.set("offset", DictValue::arrayInt<int*>(&offsets[0], offsets.size()));
842 int id = net.addLayerToPrev(lp.name, lp.type, lp);
843 net.connect(0, 1, id, 1);
845 Mat inpImage(4, inpShape, CV_32F);
846 Mat sizImage(4, sizShape, CV_32F);
847 randu(inpImage, -1, 1);
848 randu(sizImage, -1, 1);
850 std::vector<String> inpNames(2);
851 inpNames[0] = "cropImage";
852 inpNames[1] = "sizImage";
853 net.setInputsNames(inpNames);
854 net.setInput(inpImage, inpNames[0]);
855 net.setInput(sizImage, inpNames[1]);
856 net.setPreferableBackend(DNN_BACKEND_OPENCV);
858 // There are a few conditions that represent invalid input to the crop
859 // layer, so in those cases we want to verify an exception is thrown.
861 bool shouldThrowException = false;
862 if (numOffsets > 1 && numOffsets != 4 - axis)
863 shouldThrowException = true;
865 for (int i = axis; i < 4; i++)
866 if (sizShape[i] + offsetVal > inpShape[i])
867 shouldThrowException = true;
870 if (shouldThrowException)
872 ASSERT_ANY_THROW(out = net.forward());
878 // Finally, compare the cropped output blob from the DNN layer (out)
879 // to a reference blob (ref) that we compute here.
881 std::vector<Range> crop_range;
882 crop_range.resize(4, Range::all());
883 for (int i = axis; i < 4; i++)
884 crop_range[i] = Range(offsetVal, sizShape[i] + offsetVal);
886 Mat ref(sizImage.dims, sizImage.size, CV_32F);
887 inpImage(&crop_range[0]).copyTo(ref);
888 normAssert(out, ref);
891 INSTANTIATE_TEST_CASE_P(Layer_Test, Crop, Combine(
892 /*input blob shape*/ Values(Vec4i(1, 3, 20, 30)),
893 /*cropsize blob shape*/ Values(Vec4i(1, 3, 10, 12)),
894 /*start axis*/ Values(0, 1, 2),
895 /*number of offsets*/ Values(0, 1, 2, 4),
896 /*offset value*/ Values(3, 4)
899 // Check that by default average pooling layer should not count zero padded values
900 // into the normalization area.
901 TEST_P(Test_Caffe_layers, Average_pooling_kernel_area)
904 lp.name = "testAvePool";
906 lp.set("kernel_size", 2);
908 lp.set("pool", "AVE");
911 net.addLayerToPrev(lp.name, lp.type, lp);
916 Mat inp = (Mat_<float>(3, 3) << 1, 2, 3, 4, 5, 6, 7, 8, 9);
917 Mat ref = (Mat_<float>(2, 2) << (1 + 2 + 4 + 5) / 4.f, (3 + 6) / 2.f, (7 + 8) / 2.f, 9);
918 Mat tmp = blobFromImage(inp);
919 net.setInput(blobFromImage(inp));
920 net.setPreferableBackend(backend);
921 net.setPreferableTarget(target);
922 Mat out = net.forward();
923 normAssert(out, blobFromImage(ref));
926 TEST_P(Test_Caffe_layers, PriorBox_repeated)
928 Net net = readNet(_tf("prior_box.prototxt"));
929 int inp_size[] = {1, 3, 10, 10};
930 int shape_size[] = {1, 2, 3, 4};
931 Mat inp(4, inp_size, CV_32F);
932 randu(inp, -1.0f, 1.0f);
933 Mat shape(4, shape_size, CV_32F);
934 randu(shape, -1.0f, 1.0f);
935 net.setInput(inp, "data");
936 net.setInput(shape, "shape");
937 net.setPreferableBackend(backend);
938 net.setPreferableTarget(target);
939 Mat out = net.forward();
940 Mat ref = blobFromNPY(_tf("priorbox_output.npy"));
942 double l1 = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 1e-3 : 1e-5;
943 double lInf = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 1e-3 : 1e-4;
944 normAssert(out, ref, "", l1, lInf);
947 // Test PriorBoxLayer in case of no aspect ratios (just squared proposals).
948 TEST_P(Test_Caffe_layers, PriorBox_squares)
950 if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD)
951 applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
952 if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
953 applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
955 lp.name = "testPriorBox";
956 lp.type = "PriorBox";
957 lp.set("min_size", 2);
958 lp.set("flip", true);
959 lp.set("clip", true);
960 float variance[] = {0.1f, 0.1f, 0.2f, 0.2f};
961 float aspectRatios[] = {1.0f}; // That should be ignored.
962 lp.set("variance", DictValue::arrayReal<float*>(&variance[0], 4));
963 lp.set("aspect_ratio", DictValue::arrayReal<float*>(&aspectRatios[0], 1));
966 int id = net.addLayerToPrev(lp.name, lp.type, lp);
967 net.connect(0, 0, id, 1); // The second input is an input image. Shapes are used for boxes normalization.
968 Mat inp(1, 2, CV_32F);
970 net.setInput(blobFromImage(inp));
971 net.setPreferableBackend(backend);
972 net.setPreferableTarget(target);
973 Mat out = net.forward();
975 Mat ref = (Mat_<float>(4, 4) << 0.0, 0.0, 0.75, 1.0,
977 0.1f, 0.1f, 0.2f, 0.2f,
978 0.1f, 0.1f, 0.2f, 0.2f);
979 double l1 = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 2e-5 : 1e-5;
980 normAssert(out.reshape(1, 4), ref, "", l1);
983 typedef TestWithParam<tuple<int, int> > Layer_Test_DWconv_Prelu;
984 TEST_P(Layer_Test_DWconv_Prelu, Accuracy)
987 // input img size 3x16x16 value all 1
990 // dw_conv weight[0]=-1 weight[1]=-2 weight[2]=-3 bias={1,2,3}
993 // prelu weight={1,2,3}
996 // output out size 3x14x14 if right: out[0]=-8 out[0]=-32 out[0]=-72
997 // but current opencv output: out[0]=-24 out[0]=-48 out[0]=-72
999 const int num_input = get<0>(GetParam()); //inpChannels
1000 const int group = 3; //outChannels=group when group>1
1001 const int num_output = get<1>(GetParam());
1002 const int kernel_depth = num_input/group;
1003 CV_Assert_N(num_output >= group, num_output % group == 0, num_input % group == 0);
1009 lp.type = "Convolution";
1010 lp.set("kernel_size", 3);
1011 lp.set("num_output", num_output);
1013 lp.set("group", group);
1014 lp.set("stride", 1);
1015 lp.set("engine", "CAFFE");
1016 lp.set("bias_term", "true");
1018 std::vector<int> weightsShape(4);
1019 weightsShape[0] = num_output; // #outChannels
1020 weightsShape[1] = kernel_depth; // #inpChannels / group
1021 weightsShape[2] = 3; // height
1022 weightsShape[3] = 3; // width
1023 Mat weights(weightsShape, CV_32F, Scalar(1));
1026 for (int i = 0; i < weightsShape[0]; ++i)
1028 for (int j = 0; j < weightsShape[1]; ++j)
1030 for (int k = 0; k < weightsShape[2]; ++k)
1032 for (int l = 0; l < weightsShape[3]; ++l)
1034 weights.ptr<float>(i, j, k)[l]=-1*(i+1);
1039 lp.blobs.push_back(weights);
1042 Mat bias(1, num_output, CV_32F, Scalar(1));
1043 for (int i = 0; i < 1; ++i)
1045 for (int j = 0; j < num_output; ++j)
1047 bias.ptr<float>(i)[j]=j+1;
1050 lp.blobs.push_back(bias);
1051 net.addLayerToPrev(lp.name, lp.type, lp);
1055 lpr.name = "dw_relu";
1057 Mat weightsp(1, num_output, CV_32F, Scalar(1));
1060 for (int i = 0; i < 1; ++i)
1062 for (int j = 0; j < num_output; ++j)
1064 weightsp.ptr<float>(i)[j]=j+1;
1068 lpr.blobs.push_back(weightsp);
1069 net.addLayerToPrev(lpr.name, lpr.type, lpr);
1071 int shape[] = {1, num_input, 16, 16};
1072 Mat in_blob(4, &shape[0], CV_32FC1, Scalar(1));
1074 net.setPreferableBackend(DNN_BACKEND_OPENCV);
1075 net.setInput(in_blob);
1076 Mat out = net.forward();
1079 std::vector<int> outShape(4);
1081 outShape[1] = num_output; // outChannels
1082 outShape[2] = 14; // height
1083 outShape[3] = 14; // width
1084 Mat target(outShape, CV_32F, Scalar(1));
1085 for (int i = 0; i < outShape[0]; ++i)
1087 for (int j = 0; j < outShape[1]; ++j)
1089 for (int k = 0; k < outShape[2]; ++k)
1091 for (int l = 0; l < outShape[3]; ++l)
1093 target.ptr<float>(i, j, k)[l]=(-9*kernel_depth*(j+1)+j+1)*(j+1);
1099 normAssert(out, target);
1101 INSTANTIATE_TEST_CASE_P(/**/, Layer_Test_DWconv_Prelu, Combine(Values(3, 6), Values(3, 6)));
1103 #ifdef HAVE_INF_ENGINE
1104 // Using Intel's Model Optimizer generate .xml and .bin files:
1105 // ./ModelOptimizer -w /path/to/caffemodel -d /path/to/prototxt \
1106 // -p FP32 -i -b ${batch_size} -o /path/to/output/folder
1107 typedef testing::TestWithParam<tuple<Backend, Target> > Layer_Test_Convolution_DLDT;
1108 TEST_P(Layer_Test_Convolution_DLDT, Accuracy)
1110 const Backend backendId = get<0>(GetParam());
1111 const Target targetId = get<1>(GetParam());
1113 if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && targetId == DNN_TARGET_MYRIAD)
1114 applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
1116 if (backendId != DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && backendId != DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
1117 throw SkipTestException("No support for async forward");
1119 if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
1120 setInferenceEngineBackendType(CV_DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_API);
1121 else if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
1122 setInferenceEngineBackendType(CV_DNN_BACKEND_INFERENCE_ENGINE_NGRAPH);
1124 FAIL() << "Unknown backendId";
1126 Net netDefault = readNet(_tf("layer_convolution.caffemodel"), _tf("layer_convolution.prototxt"));
1127 Net net = readNet(_tf("layer_convolution.xml"), _tf("layer_convolution.bin"));
1129 Mat inp = blobFromNPY(_tf("blob.npy"));
1131 netDefault.setInput(inp);
1132 netDefault.setPreferableBackend(DNN_BACKEND_OPENCV);
1133 Mat outDefault = netDefault.forward();
1136 net.setPreferableBackend(backendId);
1137 net.setPreferableTarget(targetId);
1139 Mat out = net.forward();
1141 double l1 = (targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) ? 1.5e-3 : 1e-5;
1142 double lInf = (targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) ? 1.8e-2 : 1e-4;
1143 normAssert(outDefault, out, "", l1, lInf);
1145 std::vector<int> outLayers = net.getUnconnectedOutLayers();
1146 ASSERT_EQ(net.getLayer(outLayers[0])->name, "output");
1147 if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
1148 ASSERT_EQ(net.getLayer(outLayers[0])->type, "Convolution");
1150 ASSERT_EQ(net.getLayer(outLayers[0])->type, "Add");
1153 TEST_P(Layer_Test_Convolution_DLDT, setInput_uint8)
1155 const Backend backendId = get<0>(GetParam());
1156 const Target targetId = get<1>(GetParam());
1158 if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && targetId == DNN_TARGET_MYRIAD)
1159 applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
1161 if (backendId != DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && backendId != DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
1162 throw SkipTestException("No support for async forward");
1164 if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
1165 setInferenceEngineBackendType(CV_DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_API);
1166 else if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
1167 setInferenceEngineBackendType(CV_DNN_BACKEND_INFERENCE_ENGINE_NGRAPH);
1169 FAIL() << "Unknown backendId";
1171 int blobSize[] = {2, 6, 75, 113};
1172 Mat inputs[] = {Mat(4, &blobSize[0], CV_8U), Mat()};
1174 randu(inputs[0], 0, 255);
1175 inputs[0].convertTo(inputs[1], CV_32F);
1178 for (int i = 0; i < 2; ++i)
1180 Net net = readNet(_tf("layer_convolution.xml"), _tf("layer_convolution.bin"));
1181 net.setPreferableBackend(backendId);
1182 net.setPreferableTarget(targetId);
1183 net.setInput(inputs[i]);
1184 outs[i] = net.forward();
1185 ASSERT_EQ(outs[i].type(), CV_32F);
1187 if (targetId != DNN_TARGET_MYRIAD)
1188 normAssert(outs[0], outs[1]);
1191 TEST_P(Layer_Test_Convolution_DLDT, multithreading)
1193 const Backend backendId = get<0>(GetParam());
1194 const Target targetId = get<1>(GetParam());
1196 if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && targetId == DNN_TARGET_MYRIAD)
1197 applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
1199 if (backendId != DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && backendId != DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
1200 throw SkipTestException("No support for async forward");
1202 if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
1203 setInferenceEngineBackendType(CV_DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_API);
1204 else if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
1205 setInferenceEngineBackendType(CV_DNN_BACKEND_INFERENCE_ENGINE_NGRAPH);
1207 FAIL() << "Unknown backendId";
1209 std::string xmlPath = _tf("layer_convolution.xml");
1210 std::string binPath = _tf("layer_convolution.bin");
1211 Net firstNet = readNet(xmlPath, binPath);
1212 Net secondNet = readNet(xmlPath, binPath);
1213 Mat inp = blobFromNPY(_tf("blob.npy"));
1215 firstNet.setInput(inp);
1216 secondNet.setInput(inp);
1217 firstNet.setPreferableBackend(backendId);
1218 firstNet.setPreferableTarget(targetId);
1219 secondNet.setPreferableBackend(backendId);
1220 secondNet.setPreferableTarget(targetId);
1223 std::thread t1([&]{out1 = firstNet.forward();});
1224 std::thread t2([&]{out2 = secondNet.forward();});
1229 Mat ref = blobFromNPY(_tf("layer_convolution.npy"));
1230 double l1 = (targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) ? 1.5e-3 : 1e-5;
1231 double lInf = (targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) ? 1.8e-2 : 1e-4;
1232 normAssert(out1, ref, "first thread", l1, lInf);
1233 normAssert(out2, ref, "second thread", l1, lInf);
1236 INSTANTIATE_TEST_CASE_P(/**/, Layer_Test_Convolution_DLDT,
1237 dnnBackendsAndTargetsIE()
1240 // 1. Create a .prototxt file with the following network:
1242 // type: "Input" name: "data" top: "data"
1243 // input_param { shape { dim: 1 dim: 2 dim: 3 } }
1246 // type: "Input" name: "second_input" top: "second_input"
1247 // input_param { shape { dim: 1 dim: 2 dim: 3 } }
1250 // type: "Eltwise" name: "output" top: "output"
1251 // bottom: "data" bottom: "second_input"
1252 // eltwise_param { operation: SUM }
1255 // 2. Create a .caffemodel file using Caffe:
1258 // net = caffe.Net('/path/to/prototxt', caffe.TEST)
1259 // net.save('/path/to/caffemodel')
1261 // 3. Convert using ModelOptimizer.
1262 typedef testing::TestWithParam<tuple<int, int, Target, std::vector<int> > > Test_DLDT_two_inputs_3dim;
1263 TEST_P(Test_DLDT_two_inputs_3dim, as_IR)
1265 int firstInpType = get<0>(GetParam());
1266 int secondInpType = get<1>(GetParam());
1267 Target targetId = get<2>(GetParam());
1269 Net net = readNet(_tf("net_two_inputs.xml"), _tf("net_two_inputs.bin"));
1270 std::vector<int> inpSize = get<3>(GetParam());
1271 Mat firstInp(3, inpSize.data(), firstInpType);
1272 Mat secondInp(3, inpSize.data(), secondInpType);
1273 randu(firstInp, 0, 255);
1274 randu(secondInp, 0, 255);
1276 net.setInput(firstInp, "data");
1277 net.setInput(secondInp, "second_input");
1278 net.setPreferableTarget(targetId);
1280 double l1 = ((targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) &&
1281 (firstInpType == CV_32F || secondInpType == CV_32F)) ? 0.06 : 0.0;
1282 double lInf = ((targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) &&
1283 (firstInpType == CV_32F || secondInpType == CV_32F)) ? 0.23 : 0.0;
1285 Mat out = net.forward();
1288 cv::add(firstInp, secondInp, ref, Mat(), CV_32F);
1289 normAssert(out, ref, "", l1, lInf);
1292 std::vector< std::vector<int> > list_sizes{ {1, 2, 3}, {3, 2, 1}, {5, 5, 5}, {13, 7, 11} };
1294 INSTANTIATE_TEST_CASE_P(/*nothing*/, Test_DLDT_two_inputs_3dim, Combine(
1295 Values(CV_8U, CV_32F), Values(CV_8U, CV_32F),
1296 testing::ValuesIn(getAvailableTargets(DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)),
1297 testing::ValuesIn(list_sizes)
1300 typedef testing::TestWithParam<tuple<int, int, tuple<Backend, Target> > > Test_DLDT_two_inputs;
1301 TEST_P(Test_DLDT_two_inputs, as_backend)
1303 static const float kScale = 0.5f;
1304 static const float kScaleInv = 1.0f / kScale;
1306 Backend backendId = get<0>(get<2>(GetParam()));
1307 Target targetId = get<1>(get<2>(GetParam()));
1311 lp.type = "Eltwise";
1312 lp.name = "testLayer";
1313 lp.set("operation", "sum");
1314 int eltwiseId = net.addLayerToPrev(lp.name, lp.type, lp); // connect to a first input
1315 net.connect(0, 1, eltwiseId, 1); // connect to a second input
1317 int inpSize[] = {1, 2, 3, 4};
1318 Mat firstInp(4, &inpSize[0], get<0>(GetParam()));
1319 Mat secondInp(4, &inpSize[0], get<1>(GetParam()));
1320 randu(firstInp, 0, 255);
1321 randu(secondInp, 0, 255);
1323 net.setInputsNames({"data", "second_input"});
1324 net.setInput(firstInp, "data", kScale);
1325 net.setInput(secondInp, "second_input", kScaleInv);
1326 net.setPreferableBackend(backendId);
1327 net.setPreferableTarget(targetId);
1328 Mat out = net.forward();
1331 addWeighted(firstInp, kScale, secondInp, kScaleInv, 0, ref, CV_32F);
1332 // Output values are in range [0, 637.5].
1333 double l1 = (targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) ? 0.06 : 1e-6;
1334 double lInf = (targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) ? 0.3 : 1e-5;
1335 normAssert(out, ref, "", l1, lInf);
1338 INSTANTIATE_TEST_CASE_P(/*nothing*/, Test_DLDT_two_inputs, Combine(
1339 Values(CV_8U, CV_32F), Values(CV_8U, CV_32F),
1340 dnnBackendsAndTargets()
1343 class UnsupportedLayer : public Layer
1346 UnsupportedLayer(const LayerParams ¶ms) : Layer(params) {}
1348 static Ptr<Layer> create(const LayerParams& params)
1350 return Ptr<Layer>(new UnsupportedLayer(params));
1353 virtual bool supportBackend(int backendId) CV_OVERRIDE
1355 return backendId == DNN_BACKEND_OPENCV;
1358 virtual void forward(cv::InputArrayOfArrays inputs, cv::OutputArrayOfArrays outputs, cv::OutputArrayOfArrays internals) CV_OVERRIDE {}
1361 typedef DNNTestLayer Test_DLDT_layers;
1363 static void test_dldt_fused_output(Backend backend, Target target)
1365 static const int kNumChannels = 3;
1369 lp.set("kernel_size", 1);
1370 lp.set("num_output", 3);
1371 lp.set("bias_term", false);
1372 lp.type = "Convolution";
1373 lp.name = "testConv";
1374 lp.blobs.push_back(Mat({kNumChannels, 1, 1, 1}, CV_32F, Scalar(1)));
1375 net.addLayerToPrev(lp.name, lp.type, lp);
1379 lp.set("bias_term", false);
1381 lp.name = "testScale";
1382 lp.blobs.push_back(Mat({kNumChannels}, CV_32F, Scalar(1)));
1383 net.addLayerToPrev(lp.name, lp.type, lp);
1387 net.addLayerToPrev("unsupported_layer", "Unsupported", lp);
1389 net.setPreferableBackend(backend);
1390 net.setPreferableTarget(target);
1391 net.setInput(Mat({1, 1, 2, 3}, CV_32FC1, Scalar(1)));
1395 TEST_P(Test_DLDT_layers, fused_output)
1397 CV_DNN_REGISTER_LAYER_CLASS(Unsupported, UnsupportedLayer);
1400 test_dldt_fused_output(backend, target);
1402 catch (const std::exception& e)
1404 ADD_FAILURE() << "Exception: " << e.what();
1408 ADD_FAILURE() << "Unknown exception";
1410 LayerFactory::unregisterLayer("Unsupported");
1413 TEST_P(Test_DLDT_layers, multiple_networks)
1416 for (int i = 0; i < 2; ++i)
1418 nets[i].setInputsNames(std::vector<String>(1, format("input_%d", i)));
1421 lp.set("kernel_size", 1);
1422 lp.set("num_output", 1);
1423 lp.set("bias_term", false);
1424 lp.type = "Convolution";
1425 lp.name = format("testConv_%d", i);
1426 lp.blobs.push_back(Mat({1, 1, 1, 1}, CV_32F, Scalar(1 + i)));
1427 nets[i].addLayerToPrev(lp.name, lp.type, lp);
1428 nets[i].setPreferableBackend(backend);
1429 nets[i].setPreferableTarget(target);
1430 nets[i].setInput(Mat({1, 1, 2, 3}, CV_32FC1, Scalar(1)));
1432 Mat out_1 = nets[0].forward();
1433 Mat out_2 = nets[1].forward();
1434 // After the second model is initialized we try to receive an output from the first network again.
1435 out_1 = nets[0].forward();
1436 normAssert(2 * out_1, out_2);
1439 INSTANTIATE_TEST_CASE_P(/*nothing*/, Test_DLDT_layers, dnnBackendsAndTargets());
1441 #endif // HAVE_INF_ENGINE
1443 // Test a custom layer.
1444 class CustomInterpLayer CV_FINAL : public Layer
1447 CustomInterpLayer(const LayerParams ¶ms) : Layer(params)
1449 zoomFactor = params.get<int>("zoom_factor", 0);
1450 outWidth = params.get<int>("width", 0);
1451 outHeight = params.get<int>("height", 0);
1454 static Ptr<Layer> create(LayerParams& params)
1456 return Ptr<Layer>(new CustomInterpLayer(params));
1459 virtual bool getMemoryShapes(const std::vector<std::vector<int> > &inputs,
1460 const int requiredOutputs,
1461 std::vector<std::vector<int> > &outputs,
1462 std::vector<std::vector<int> > &internals) const CV_OVERRIDE
1464 const int batchSize = inputs[0][0];
1465 const int numChannels = inputs[0][1];
1466 const int inpHeight = inputs[0][2];
1467 const int inpWidth = inputs[0][3];
1469 std::vector<int> outShape(4);
1470 outShape[0] = batchSize;
1471 outShape[1] = numChannels;
1472 outShape[2] = outHeight != 0 ? outHeight : (inpHeight + (inpHeight - 1) * (zoomFactor - 1));
1473 outShape[3] = outWidth != 0 ? outWidth : (inpWidth + (inpWidth - 1) * (zoomFactor - 1));
1474 outputs.assign(1, outShape);
1478 virtual void finalize(InputArrayOfArrays, OutputArrayOfArrays outputs_arr) CV_OVERRIDE
1480 std::vector<Mat> outputs;
1481 outputs_arr.getMatVector(outputs);
1483 if (!outWidth && !outHeight)
1485 outHeight = outputs[0].size[2];
1486 outWidth = outputs[0].size[3];
1490 // Implementation of this custom layer is based on https://github.com/cdmh/deeplab-public/blob/master/src/caffe/layers/interp_layer.cpp
1491 void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE
1493 CV_TRACE_FUNCTION();
1494 CV_TRACE_ARG_VALUE(name, "name", name.c_str());
1496 if (inputs_arr.depth() == CV_16S)
1498 forward_fallback(inputs_arr, outputs_arr, internals_arr);
1502 std::vector<Mat> inputs, outputs;
1503 inputs_arr.getMatVector(inputs);
1504 outputs_arr.getMatVector(outputs);
1506 Mat& inp = inputs[0];
1507 Mat& out = outputs[0];
1508 const float* inpData = (float*)inp.data;
1509 float* outData = (float*)out.data;
1511 const int batchSize = inp.size[0];
1512 const int numChannels = inp.size[1];
1513 const int inpHeight = inp.size[2];
1514 const int inpWidth = inp.size[3];
1516 const float rheight = (outHeight > 1) ? static_cast<float>(inpHeight - 1) / (outHeight - 1) : 0.f;
1517 const float rwidth = (outWidth > 1) ? static_cast<float>(inpWidth - 1) / (outWidth - 1) : 0.f;
1518 for (int h2 = 0; h2 < outHeight; ++h2)
1520 const float h1r = rheight * h2;
1522 const int h1p = (h1 < inpHeight - 1) ? 1 : 0;
1523 const float h1lambda = h1r - h1;
1524 const float h0lambda = 1.f - h1lambda;
1525 for (int w2 = 0; w2 < outWidth; ++w2)
1527 const float w1r = rwidth * w2;
1529 const int w1p = (w1 < inpWidth - 1) ? 1 : 0;
1530 const float w1lambda = w1r - w1;
1531 const float w0lambda = 1.f - w1lambda;
1532 const float* pos1 = inpData + h1 * inpWidth + w1;
1533 float* pos2 = outData + h2 * outWidth + w2;
1534 for (int c = 0; c < batchSize * numChannels; ++c)
1537 h0lambda * (w0lambda * pos1[0] + w1lambda * pos1[w1p]) +
1538 h1lambda * (w0lambda * pos1[h1p * inpWidth] + w1lambda * pos1[h1p * inpWidth + w1p]);
1539 pos1 += inpWidth * inpHeight;
1540 pos2 += outWidth * outHeight;
1547 int outWidth, outHeight, zoomFactor;
1550 #ifndef OPENCV_DNN_EXTERNAL_PROTOBUF
1551 TEST_P(Test_Caffe_layers, Interp)
1553 TEST_P(Test_Caffe_layers, DISABLED_Interp) // requires patched protobuf (available in OpenCV source tree only)
1556 if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD)
1557 applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD);
1559 // Test a custom layer.
1560 CV_DNN_REGISTER_LAYER_CLASS(Interp, CustomInterpLayer);
1563 testLayerUsingCaffeModels("layer_interp", false, false);
1567 LayerFactory::unregisterLayer("Interp");
1570 LayerFactory::unregisterLayer("Interp");
1572 // Test an implemented layer.
1573 testLayerUsingCaffeModels("layer_interp", false, false);
1576 INSTANTIATE_TEST_CASE_P(/*nothing*/, Test_Caffe_layers, dnnBackendsAndTargets());
1578 TEST(Layer_Test_PoolingIndices, Accuracy)
1583 lp.set("pool", "max");
1584 lp.set("kernel_w", 2);
1585 lp.set("kernel_h", 2);
1586 lp.set("stride_w", 2);
1587 lp.set("stride_h", 2);
1590 lp.name = "testLayer.name"; // This test also checks that OpenCV lets use names with dots.
1591 lp.type = "Pooling";
1592 net.addLayerToPrev(lp.name, lp.type, lp);
1594 Mat inp(10, 10, CV_8U);
1597 Mat maxValues(5, 5, CV_32F, Scalar(-1)), indices(5, 5, CV_32F, Scalar(-1));
1598 for (int y = 0; y < 10; ++y)
1601 for (int x = 0; x < 10; ++x)
1604 uint8_t val = inp.at<uint8_t>(y, x);
1605 if ((float)inp.at<uint8_t>(y, x) > maxValues.at<float>(dstY, dstX))
1607 maxValues.at<float>(dstY, dstX) = val;
1608 indices.at<float>(dstY, dstX) = y * 10 + x;
1612 net.setPreferableBackend(DNN_BACKEND_OPENCV);
1613 net.setInput(blobFromImage(inp));
1615 std::vector<Mat> outputs;
1616 net.forward(outputs, lp.name);
1617 normAssert(maxValues, outputs[0].reshape(1, 5));
1618 normAssert(indices, outputs[1].reshape(1, 5));
1621 typedef testing::TestWithParam<tuple<Vec4i, int, tuple<Backend, Target> > > Layer_Test_ShuffleChannel;
1622 TEST_P(Layer_Test_ShuffleChannel, Accuracy)
1624 Vec4i inpShapeVec = get<0>(GetParam());
1625 int group = get<1>(GetParam());
1626 ASSERT_EQ(inpShapeVec[1] % group, 0);
1627 const int groupSize = inpShapeVec[1] / group;
1628 int backendId = get<0>(get<2>(GetParam()));
1629 int targetId = get<1>(get<2>(GetParam()));
1633 lp.set("group", group);
1634 lp.type = "ShuffleChannel";
1635 lp.name = "testLayer";
1636 net.addLayerToPrev(lp.name, lp.type, lp);
1638 const int inpShape[] = {inpShapeVec[0], inpShapeVec[1], inpShapeVec[2], inpShapeVec[3]};
1639 Mat inp(4, inpShape, CV_32F);
1643 net.setPreferableBackend(backendId);
1644 net.setPreferableTarget(targetId);
1645 Mat out = net.forward();
1647 double l1 = (targetId == DNN_TARGET_OPENCL_FP16) ? 5e-2 : 1e-5;
1648 double lInf = (targetId == DNN_TARGET_OPENCL_FP16) ? 7e-2 : 1e-4;
1649 for (int n = 0; n < inpShapeVec[0]; ++n)
1651 for (int c = 0; c < inpShapeVec[1]; ++c)
1653 Mat outChannel = getPlane(out, n, c);
1654 Mat inpChannel = getPlane(inp, n, groupSize * (c % group) + c / group);
1655 normAssert(outChannel, inpChannel, "", l1, lInf);
1659 INSTANTIATE_TEST_CASE_P(/**/, Layer_Test_ShuffleChannel, Combine(
1660 /*input shape*/ Values(Vec4i(1, 6, 5, 7), Vec4i(3, 12, 1, 4)),
1661 /*group*/ Values(1, 2, 3, 6), dnnBackendsAndTargets(/*with IE*/ false)
1664 // Check if relu is not fused to convolution if we requested it's output
1665 TEST(Layer_Test_Convolution, relu_fusion)
1670 lp.set("kernel_size", 1);
1671 lp.set("num_output", 1);
1672 lp.set("bias_term", false);
1673 lp.type = "Convolution";
1674 lp.name = "testConv";
1676 int weightsShape[] = {1, 1, 1, 1};
1677 Mat weights(4, &weightsShape[0], CV_32F, Scalar(1));
1678 lp.blobs.push_back(weights);
1679 net.addLayerToPrev(lp.name, lp.type, lp);
1684 lp.name = "testReLU";
1685 net.addLayerToPrev(lp.name, lp.type, lp);
1687 int sz[] = {1, 1, 2, 3};
1688 Mat input(4, &sz[0], CV_32F);
1689 randu(input, -1.0, -0.1);
1690 net.setInput(input);
1691 net.setPreferableBackend(DNN_BACKEND_OPENCV);
1692 Mat output = net.forward("testConv");
1693 normAssert(input, output);
1696 typedef testing::TestWithParam<tuple<bool, tuple<Backend, Target> > > Layer_Test_Eltwise_unequal;
1697 TEST_P(Layer_Test_Eltwise_unequal, accuracy_input_0_truncate)
1699 bool weighted = get<0>(GetParam());
1700 int backendId = get<0>(get<1>(GetParam()));
1701 int targetId = get<1>(get<1>(GetParam()));
1705 lp.type = "Eltwise";
1706 lp.name = "testLayer";
1707 lp.set<std::string>("output_channels_mode", "input_0_truncate");
1709 const int inpShapes[][4] = {{1, 4, 2, 2}, {1, 5, 2, 2}, {1, 3, 2, 2}};
1710 const int out_channels = inpShapes[0][1];
1711 std::vector<String> inpNames(3);
1712 std::vector<Mat> inputs(3);
1714 std::vector<float> weights(3, 1);
1717 for (int i = 0; i < inputs.size(); ++i)
1718 weights[i] = -0.125f + i * 0.25f;
1719 lp.set("coeff", DictValue::arrayReal<float*>(&weights[0], weights.size()));
1722 int eltwiseId = net.addLayer(lp.name, lp.type, lp);
1723 for (int i = 0; i < inputs.size(); ++i)
1725 inputs[i].create(4, inpShapes[i], CV_32F);
1726 size_t total = inputs[i].total();
1727 for (size_t j = 0; j < total; j++)
1728 inputs[i].ptr<float>()[j] = j + i * 100;
1729 inpNames[i] = format("input_%d", i);
1730 net.connect(0, i, eltwiseId, i);
1732 Mat ref(4, inpShapes[0], CV_32F, Scalar(0));
1734 net.setInputsNames(inpNames);
1735 for (int i = 0; i < inputs.size(); ++i)
1737 //std::cout << ref.reshape(1,1) << endl;
1738 net.setInput(inputs[i], inpNames[i]);
1739 for (size_t batchId = 0; batchId < ref.size[0]; batchId++)
1741 int input_channels = inputs[i].size[1];
1742 Range ranges[4] = { Range(batchId, batchId + 1), Range(0, std::min(out_channels, input_channels)), Range::all(), Range::all() };
1743 Mat ref_slice = ref(ranges);
1744 Mat input_slice = inputs[i](ranges);
1745 ref_slice += weights[i] * input_slice;
1749 net.setPreferableBackend(backendId);
1750 net.setPreferableTarget(targetId);
1751 Mat out = net.forward();
1752 normAssert(out, ref);
1753 if (testing::Test::HasFailure())
1755 std::cout << out.reshape(1,1) << endl;
1756 std::cout << ref.reshape(1,1) << endl;
1760 TEST_P(Layer_Test_Eltwise_unequal, accuracy_input_0)
1762 bool weighted = get<0>(GetParam());
1763 int backendId = get<0>(get<1>(GetParam()));
1764 int targetId = get<1>(get<1>(GetParam()));
1768 lp.type = "Eltwise";
1769 lp.name = "testLayer";
1770 lp.set<std::string>("output_channels_mode", "input_0");
1772 const int inpShapes[][4] = {{1, 4, 2, 2}, {1, 2, 2, 2}, {1, 3, 2, 2}};
1773 const int out_channels = inpShapes[0][1];
1774 std::vector<String> inpNames(3);
1775 std::vector<Mat> inputs(3);
1777 std::vector<float> weights(3, 1);
1780 for (int i = 0; i < inputs.size(); ++i)
1781 weights[i] = -0.125f + i * 0.25f;
1782 lp.set("coeff", DictValue::arrayReal<float*>(&weights[0], weights.size()));
1785 int eltwiseId = net.addLayer(lp.name, lp.type, lp);
1786 for (int i = 0; i < inputs.size(); ++i)
1788 inputs[i].create(4, inpShapes[i], CV_32F);
1789 size_t total = inputs[i].total();
1790 for (size_t j = 0; j < total; j++)
1791 inputs[i].ptr<float>()[j] = j + i * 100;
1792 inpNames[i] = format("input_%d", i);
1793 net.connect(0, i, eltwiseId, i);
1795 Mat ref(4, inpShapes[0], CV_32F, Scalar(0));
1797 net.setInputsNames(inpNames);
1798 for (int i = 0; i < inputs.size(); ++i)
1800 //std::cout << ref.reshape(1,1) << endl;
1801 net.setInput(inputs[i], inpNames[i]);
1802 for (size_t batchId = 0; batchId < ref.size[0]; batchId++)
1804 int input_channels = inputs[i].size[1];
1805 Range ranges[4] = { Range(batchId, batchId + 1), Range(0, std::min(out_channels, input_channels)), Range::all(), Range::all() };
1806 Mat ref_slice = ref(ranges);
1807 Mat input_slice = inputs[i](ranges);
1808 ref_slice += weights[i] * input_slice;
1812 net.setPreferableBackend(backendId);
1813 net.setPreferableTarget(targetId);
1814 Mat out = net.forward();
1815 normAssert(out, ref);
1816 if (testing::Test::HasFailure())
1818 std::cout << out.reshape(1,1) << endl;
1819 std::cout << ref.reshape(1,1) << endl;
1823 INSTANTIATE_TEST_CASE_P(/**/, Layer_Test_Eltwise_unequal, Combine(
1825 dnnBackendsAndTargets()
1828 typedef testing::TestWithParam<tuple<Backend, Target> > Layer_Test_Resize;
1829 TEST_P(Layer_Test_Resize, change_input)
1831 int backendId = get<0>(GetParam());
1832 int targetId = get<1>(GetParam());
1837 lp.name = "testLayer";
1838 lp.set("zoom_factor", 2);
1839 lp.set("interpolation", "nearest");
1840 net.addLayerToPrev(lp.name, lp.type, lp);
1842 for (int i = 0; i < 2; ++i)
1844 Mat inp(4 + i, 5 + i, CV_8UC3), ref;
1846 resize(inp, ref, Size(0, 0), 2, 2, INTER_NEAREST);
1847 ref = blobFromImage(ref);
1849 net.setInput(blobFromImage(inp));
1850 net.setPreferableBackend(backendId);
1851 net.setPreferableTarget(targetId);
1852 Mat out = net.forward();
1853 normAssert(out, ref);
1857 INSTANTIATE_TEST_CASE_P(/**/, Layer_Test_Resize, dnnBackendsAndTargets());
1859 struct Layer_Test_Slice : public testing::TestWithParam<tuple<Backend, Target> >
1862 void test_slice(const int* inputShape, const int* begin, const int* end)
1864 int backendId = get<0>(GetParam());
1865 int targetId = get<1>(GetParam());
1867 Mat input(DIMS, inputShape, CV_32FC1, Scalar::all(0));
1868 for (int i = 0; i < (int)input.total(); ++i)
1869 input.ptr<float>()[i] = (float)i;
1871 std::vector<Range> range(DIMS);
1872 for (int i = 0; i < DIMS; ++i)
1873 range[i] = Range(begin[i], end[i]);
1878 lp.name = "testLayer";
1879 lp.set("begin", DictValue::arrayInt<int*>((int*)&begin[0], DIMS));
1880 lp.set("end", DictValue::arrayInt<int*>((int*)&end[0], DIMS));
1881 net.addLayerToPrev(lp.name, lp.type, lp);
1884 net.setInput(input);
1885 net.setPreferableBackend(backendId);
1886 net.setPreferableTarget(targetId);
1887 Mat out = net.forward();
1889 EXPECT_GT(cv::norm(out, NORM_INF), 0);
1890 normAssert(out, input(range));
1892 cout << input(range).clone().reshape(1, 1) << endl;
1893 cout << out.reshape(1, 1) << endl;
1899 TEST_P(Layer_Test_Slice, slice_channels_17762)
1901 const int inputShape[4] = {1, 16, 6, 8};
1902 const int begin[] = {0, 4, 0, 0};
1903 const int end[] = {1, 8, 6, 8};
1904 test_slice<4>(inputShape, begin, end);
1907 TEST_P(Layer_Test_Slice, slice_channels_with_batch_17762)
1909 const int inputShape[4] = {4, 4, 3, 4};
1910 const int begin[] = {0, 1, 0, 0};
1911 const int end[] = {4, 3, 3, 4};
1912 test_slice<4>(inputShape, begin, end);
1915 TEST_P(Layer_Test_Slice, slice_channels_and_batch_17762)
1917 int backend = get<0>(GetParam());
1918 if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
1919 applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
1921 const int inputShape[4] = {4, 4, 3, 4};
1922 const int begin[] = {2, 1, 0, 0};
1923 const int end[] = {4, 3, 3, 4};
1924 test_slice<4>(inputShape, begin, end);
1927 TEST_P(Layer_Test_Slice, slice_rows)
1929 const int inputShape[4] = {1, 2, 6, 4};
1930 const int begin[] = {0, 0, 4, 0};
1931 const int end[] = {1, 2, 6, 4};
1932 test_slice<4>(inputShape, begin, end);
1935 TEST_P(Layer_Test_Slice, slice_cols)
1937 const int inputShape[4] = {1, 2, 3, 8};
1938 const int begin[] = {0, 0, 0, 4};
1939 const int end[] = {1, 2, 3, 8};
1940 test_slice<4>(inputShape, begin, end);
1944 TEST_P(Layer_Test_Slice, slice_complex_1_unaligned)
1946 const int inputShape[4] = {1, 4, 2, 3};
1947 const int begin[] = {0, 2, 1, 0};
1948 const int end[] = {1, 3, 2, 2};
1949 test_slice<4>(inputShape, begin, end);
1952 TEST_P(Layer_Test_Slice, slice_complex_2_x4)
1954 const int inputShape[4] = {1, 3, 2, 4};
1955 const int begin[] = {0, 2, 1, 0};
1956 const int end[] = {1, 3, 2, 2};
1957 test_slice<4>(inputShape, begin, end);
1960 TEST_P(Layer_Test_Slice, slice_complex_3)
1962 const int inputShape[4] = {1, 6, 4, 8};
1963 const int begin[] = {0, 2, 1, 4};
1964 const int end[] = {1, 4, 3, 8};
1965 test_slice<4>(inputShape, begin, end);
1968 TEST_P(Layer_Test_Slice, variable_input_shape)
1970 int backendId = get<0>(GetParam());
1971 int targetId = get<1>(GetParam());
1973 int begin[] = {0, 0, 0, 0};
1974 int end[] = {-1, -1, -1, -1};
1979 lp.name = "testLayer";
1980 lp.set("begin", DictValue::arrayInt<int*>(&begin[0], 4));
1981 lp.set("end", DictValue::arrayInt<int*>(&end[0], 4));
1982 net.addLayerToPrev(lp.name, lp.type, lp);
1984 for (int i = 0; i < 2; ++i)
1986 Mat inp(4 + i, 5 + i, CV_8UC1);
1988 inp = blobFromImage(inp);
1991 net.setPreferableBackend(backendId);
1992 net.setPreferableTarget(targetId);
1993 Mat out = net.forward();
1995 normAssert(out, inp);
1999 INSTANTIATE_TEST_CASE_P(/**/, Layer_Test_Slice, dnnBackendsAndTargets());
2001 typedef testing::TestWithParam<tuple<Backend, Target> > Layer_Test_BatchNorm;
2002 TEST_P(Layer_Test_BatchNorm, fusion)
2004 // This tests reinitializes network by forwarding different batch size input.
2005 // We check BatchNorm layer weights restoring after fusion.
2006 int backendId = get<0>(GetParam());
2007 int targetId = get<1>(GetParam());
2010 Mat mean(1, ch, CV_32F), var(1, ch, CV_32F), weights(1, ch, CV_32F);
2013 randu(weights, 0, 1);
2018 lp.type = "BatchNorm";
2020 lp.set("has_weight", false);
2021 lp.set("has_bias", false);
2022 lp.blobs.push_back(mean);
2023 lp.blobs.push_back(var);
2024 net.addLayerToPrev(lp.name, lp.type, lp);
2030 lp.set("has_bias", false);
2031 lp.blobs.push_back(weights);
2032 net.addLayerToPrev(lp.name, lp.type, lp);
2035 Mat inp(4, 5, CV_32FC(ch));
2038 net.setPreferableBackend(backendId);
2039 net.setPreferableTarget(targetId);
2041 net.setInput(blobFromImage(inp));
2042 Mat ref = net.forward();
2044 net.setInput(blobFromImages(std::vector<Mat>(2, inp)));
2045 Mat out = net.forward();
2047 for (int i = 0; i < 2; ++i)
2049 std::vector<Range> ranges(4, Range::all());
2050 ranges[0].start = i;
2051 ranges[0].end = i + 1;
2052 normAssert(out(ranges), ref);
2056 INSTANTIATE_TEST_CASE_P(/**/, Layer_Test_BatchNorm, dnnBackendsAndTargets());
2058 class TestLayerFusion : public DNNTestLayer {
2060 static void makeDefaultTestConvolutionLayer(LayerParams& convParams, int in_channels, int num_filters, bool bias_term)
2062 const int kernel_h = 3, kernel_w = 3;
2063 const int pad_h = kernel_h / 2, pad_w = kernel_w / 2;
2065 convParams.set("kernel_h", kernel_h);
2066 convParams.set("kernel_w", kernel_w);
2067 convParams.set("pad_h", pad_h);
2068 convParams.set("pad_w", pad_w);
2069 convParams.set("num_output", num_filters);
2070 convParams.set("bias_term", bias_term);
2071 convParams.type = "Convolution";
2072 convParams.name = "convolution";
2074 float conv_init_magnitude = 1.0f / in_channels / kernel_h / kernel_w;
2075 int weightsShape[] = {num_filters, in_channels, kernel_h, kernel_w};
2076 Mat weights(4, &weightsShape[0], CV_32F);
2077 randu(weights, -conv_init_magnitude, conv_init_magnitude);
2078 convParams.blobs.push_back(weights);
2081 Mat bias(1, num_filters, CV_32F);
2082 randu(bias, -1.0f, 1.0f);
2083 convParams.blobs.push_back(bias);
2087 static void makeDefaultTestActivationLayer(LayerParams& activationParams, const std::string& type, int in_channels)
2089 activationParams.type = type;
2090 activationParams.name = "activation";
2091 if (activationParams.type == "ReLU")
2092 activationParams.set("negative_slope", 0.1f);
2093 else if (activationParams.type == "Power")
2095 activationParams.set("power", 2.0f);
2096 activationParams.set("scale", 0.5f);
2097 activationParams.set("shift", 0.3f);
2099 else if (activationParams.type == "ReLU6")
2101 activationParams.set("min_value", -1.0f);
2102 activationParams.set("max_value", 1.0f);
2104 else if (activationParams.type == "ChannelsPReLU")
2106 Mat scales(1, in_channels, CV_32F);
2107 randu(scales, -1.0f, 1.0f);
2108 activationParams.blobs.push_back(scales);
2112 static void makeDefaultTestEltwiseLayer(LayerParams& eltwiseParams, const std::string& op, bool withCoefficients)
2114 eltwiseParams.type = "Eltwise";
2115 eltwiseParams.name = "eltwise";
2116 eltwiseParams.set("operation", op);
2117 if (withCoefficients)
2119 float coeff[] = {0.3f, 0.5f};
2120 eltwiseParams.set("coeff", DictValue::arrayReal<float*>(coeff, 2));
2124 static void test(Mat& input, Net& net, Backend backendId, Target targetId, std::vector<int> expectedFusedLayers = std::vector<int>(), double l1 = 0.0, double lInf = 0.0)
2126 DNNTestLayer::checkBackend(backendId, targetId);
2128 net.enableFusion(false);
2129 net.setPreferableBackend(DNN_BACKEND_OPENCV);
2130 net.setPreferableTarget(DNN_TARGET_CPU);
2131 net.setInput(input);
2132 Mat outputReference = net.forward().clone();
2133 std::vector<double> refTimings;
2134 net.getPerfProfile(refTimings);
2135 for (int i = 0; i < refTimings.size(); i++)
2137 CV_Assert(refTimings[i] != 0.0);
2140 net.enableFusion(true);
2141 net.setPreferableBackend(backendId);
2142 net.setPreferableTarget(targetId);
2143 net.setInput(input);
2144 Mat outputTest = net.forward().clone();
2145 std::vector<double> testTimings;
2146 net.getPerfProfile(testTimings);
2147 for (int i = 0; i < testTimings.size(); i++)
2149 if(std::find(expectedFusedLayers.begin(), expectedFusedLayers.end(), i + 1) != expectedFusedLayers.end())
2151 EXPECT_EQ(testTimings[i], 0.0);
2155 EXPECT_NE(testTimings[i], 0.0);
2159 // double ref_max_value, ref_min_value;
2160 // minMaxLoc(outputReference.reshape(1, 1), &ref_min_value, &ref_max_value);
2161 // std::cout << "reference range: " << ref_min_value << ' ' << ref_max_value << std::endl;
2163 double default_l1, default_lInf;
2164 DNNTestLayer::getDefaultThresholds(backendId, targetId, &default_l1, &default_lInf);
2168 lInf = default_lInf;
2169 normAssert(outputReference, outputTest, "", l1, lInf);
2172 static testing::internal::ParamGenerator<std::string> eltwiseOpList()
2174 // TODO: automate list generation
2175 return Values("sum", "max", "prod", "div");
2178 static testing::internal::ParamGenerator<std::string> activationLayersList()
2180 // TODO: automate list generation
2181 return Values("ReLU", "ReLU6", "ChannelsPReLU", "TanH", "Swish", "Mish", "Sigmoid", "ELU", "AbsVal", "BNLL", "Power");
2184 static testing::internal::ParamGenerator<tuple<Backend, Target> > dnnBackendsAndTargetsForFusionTests()
2186 return dnnBackendsAndTargets(false, false, true, false); // OCV OpenCL + OCV CPU
2190 typedef TestWithParam<tuple<bool, std::string, tuple<Backend, Target> > > ConvolutionActivationFusion;
2191 TEST_P(ConvolutionActivationFusion, Accuracy)
2195 // -----------------------
2197 // -----------------------
2199 // -----------------------
2201 // -----------------------
2205 const int batch_size = 2, in_channels = 16;
2206 const int in_height = 16, in_width = 16;
2207 int inputShape[] = {batch_size, in_channels, in_height, in_width};
2208 Mat input(4, &inputShape[0], CV_32F);
2209 randu(input, 1.0f, 2.0f);
2211 bool bias_term = get<0>(GetParam());
2212 LayerParams convParams;
2213 TestLayerFusion::makeDefaultTestConvolutionLayer(convParams, in_channels, in_channels, bias_term);
2215 std::string actType = get<1>(GetParam());
2216 LayerParams activationParams;
2217 TestLayerFusion::makeDefaultTestActivationLayer(activationParams, actType, in_channels);
2219 Backend backendId = get<0>(get<2>(GetParam()));
2220 Target targetId = get<1>(get<2>(GetParam()));
2222 // bug: https://github.com/opencv/opencv/issues/17964
2223 if (actType == "Power" && backendId == DNN_BACKEND_OPENCV && (targetId == DNN_TARGET_OPENCL || targetId == DNN_TARGET_OPENCL_FP16))
2224 applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
2227 int convId = net.addLayer(convParams.name, convParams.type, convParams);
2228 int activId = net.addLayerToPrev(activationParams.name, activationParams.type, activationParams);
2229 net.connect(0, 0, convId, 0);
2231 std::vector<int> expectedFusedLayers;
2232 if (backendId == DNN_BACKEND_OPENCV)
2234 if (targetId == DNN_TARGET_CPU)
2235 expectedFusedLayers.push_back(activId); // all activations are fused
2236 else if (targetId == DNN_TARGET_OPENCL || targetId == DNN_TARGET_OPENCL_FP16)
2238 if (actType == "ReLU" || actType == "ChannelsPReLU" || actType == "ReLU6" || actType == "TanH" || actType == "Power")
2239 expectedFusedLayers.push_back(activId);
2243 TestLayerFusion::test(input, net, backendId, targetId, expectedFusedLayers);
2245 INSTANTIATE_TEST_CASE_P(TestLayerFusion, ConvolutionActivationFusion, Combine(
2246 /* bias */ testing::Bool(),
2247 /* activation */ TestLayerFusion::activationLayersList(),
2248 TestLayerFusion::dnnBackendsAndTargetsForFusionTests()
2251 typedef TestWithParam<tuple<bool, std::string, bool, tuple<Backend, Target> > > ConvolutionEltwiseFusion;
2252 TEST_P(ConvolutionEltwiseFusion, Accuracy)
2256 // -------------------------------
2258 // | ---------------
2259 // | | convolution |
2260 // | ---------------
2262 // | ---------------- |
2263 // --------| eltwise op |-------
2268 const int batch_size = 2, in_channels = 16;
2269 const int in_height = 16, in_width = 16;
2270 int inputShape[] = {batch_size, in_channels, in_height, in_width};
2271 Mat input(4, &inputShape[0], CV_32F);
2272 randu(input, 1.0f, 2.0f); // avoid small values to test eltwise div
2274 bool bias_term = get<0>(GetParam());
2275 LayerParams convParams;
2276 TestLayerFusion::makeDefaultTestConvolutionLayer(convParams, in_channels, in_channels, bias_term);
2278 std::string eltwiseOp = get<1>(GetParam());
2279 bool weightedEltwise = get<2>(GetParam());
2280 if (eltwiseOp != "sum" && weightedEltwise)
2281 throw SkipTestException("weighted eltwise not supported");
2282 LayerParams eltwiseParams;
2283 TestLayerFusion::makeDefaultTestEltwiseLayer(eltwiseParams, eltwiseOp, weightedEltwise);
2286 int convId = net.addLayer(convParams.name, convParams.type, convParams);
2287 int eltwiseId = net.addLayer(eltwiseParams.name, eltwiseParams.type, eltwiseParams);
2288 net.connect(0, 0, convId, 0);
2289 net.connect(convId, 0, eltwiseId, 0);
2290 net.connect(0, 0, eltwiseId, 1);
2292 Backend backendId = get<0>(get<3>(GetParam()));
2293 Target targetId = get<1>(get<3>(GetParam()));
2294 TestLayerFusion::test(input, net, backendId, targetId);
2296 INSTANTIATE_TEST_CASE_P(TestLayerFusion, ConvolutionEltwiseFusion, Combine(
2297 /* bias */ testing::Bool(),
2298 /* eltwise op */ TestLayerFusion::eltwiseOpList(),
2299 /* eltwise weighted */ testing::Bool(),
2300 TestLayerFusion::dnnBackendsAndTargetsForFusionTests()
2303 typedef TestWithParam<tuple<bool, std::string, bool, std::string, tuple<Backend, Target> > > ConvolutionEltwiseActivationFusion;
2304 TEST_P(ConvolutionEltwiseActivationFusion, Accuracy)
2308 // -------------------------------
2310 // | ---------------
2311 // | | convolution |
2312 // | ---------------
2314 // | ---------------- |
2315 // --------| eltwise op |-------
2324 const int batch_size = 2, in_channels = 16;
2325 const int in_height = 16, in_width = 16;
2326 int inputShape[] = {batch_size, in_channels, in_height, in_width};
2327 Mat input(4, &inputShape[0], CV_32F);
2328 randu(input, 1.0f, 2.0f); // avoid small values to test eltwise div
2330 bool bias_term = get<0>(GetParam());
2331 LayerParams convParams;
2332 TestLayerFusion::makeDefaultTestConvolutionLayer(convParams, in_channels, in_channels, bias_term);
2334 std::string eltwiseOp = get<1>(GetParam());
2335 bool weightedEltwise = get<2>(GetParam());
2336 if (eltwiseOp != "sum" && weightedEltwise)
2337 throw SkipTestException("weighted eltwise not supported");
2338 LayerParams eltwiseParams;
2339 TestLayerFusion::makeDefaultTestEltwiseLayer(eltwiseParams, eltwiseOp, weightedEltwise);
2341 std::string actType = get<3>(GetParam());
2342 LayerParams activationParams;
2343 TestLayerFusion::makeDefaultTestActivationLayer(activationParams, actType, in_channels);
2345 Backend backendId = get<0>(get<4>(GetParam()));
2346 Target targetId = get<1>(get<4>(GetParam()));
2348 // bug: https://github.com/opencv/opencv/issues/17945
2349 if ((eltwiseOp != "sum" || weightedEltwise) && backendId == DNN_BACKEND_OPENCV && (targetId == DNN_TARGET_OPENCL || targetId == DNN_TARGET_OPENCL_FP16))
2350 applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
2352 // bug: https://github.com/opencv/opencv/issues/17953
2353 if (eltwiseOp == "sum" && actType == "ChannelsPReLU" && bias_term == false &&
2354 backendId == DNN_BACKEND_OPENCV && (targetId == DNN_TARGET_OPENCL || targetId == DNN_TARGET_OPENCL_FP16))
2356 applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
2359 // bug: https://github.com/opencv/opencv/issues/17964
2360 if (actType == "Power" && backendId == DNN_BACKEND_OPENCV && (targetId == DNN_TARGET_OPENCL || targetId == DNN_TARGET_OPENCL_FP16))
2361 applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
2364 int convId = net.addLayer(convParams.name, convParams.type, convParams);
2365 int eltwiseId = net.addLayer(eltwiseParams.name, eltwiseParams.type, eltwiseParams);
2366 int activId = net.addLayer(activationParams.name, activationParams.type, activationParams);
2367 net.connect(0, 0, convId, 0);
2368 net.connect(convId, 0, eltwiseId, 0);
2369 net.connect(0, 0, eltwiseId, 1);
2370 net.connect(eltwiseId, 0, activId, 0);
2372 std::vector<int> expectedFusedLayers;
2373 if (backendId == DNN_BACKEND_OPENCV)
2375 if (targetId == DNN_TARGET_CPU)
2376 expectedFusedLayers.push_back(activId); // activation is fused with eltwise layer
2377 else if (targetId == DNN_TARGET_OPENCL || targetId == DNN_TARGET_OPENCL_FP16)
2379 if (actType == "ReLU" || actType == "ChannelsPReLU" || actType == "Power")
2381 expectedFusedLayers.push_back(eltwiseId);
2382 expectedFusedLayers.push_back(activId);
2387 TestLayerFusion::test(input, net, backendId, targetId, expectedFusedLayers);
2389 INSTANTIATE_TEST_CASE_P(TestLayerFusion, ConvolutionEltwiseActivationFusion, Combine(
2390 /* bias */ testing::Bool(),
2391 /* eltwise op */ TestLayerFusion::eltwiseOpList(),
2392 /* eltwise weighted */ testing::Bool(),
2393 /* activation */ TestLayerFusion::activationLayersList(),
2394 TestLayerFusion::dnnBackendsAndTargetsForFusionTests()
2397 typedef TestWithParam<tuple<bool, std::string, std::string, bool, tuple<Backend, Target> > > ConvolutionActivationEltwiseFusion;
2398 TEST_P(ConvolutionActivationEltwiseFusion, Accuracy)
2402 // -------------------------------
2404 // | ----------------
2405 // | | convolution |
2406 // | ----------------
2408 // | ----------------
2410 // | ----------------
2412 // | ---------------- |
2413 // --------| eltwise sum |-------
2417 const int batch_size = 2, in_channels = 16;
2418 const int in_height = 16, in_width = 16;
2419 int inputShape[] = {batch_size, in_channels, in_height, in_width};
2420 Mat input(4, &inputShape[0], CV_32F);
2421 randu(input, 1.0f, 2.0f); // avoid small values to test eltwise div
2423 bool bias_term = get<0>(GetParam());
2424 LayerParams convParams;
2425 TestLayerFusion::makeDefaultTestConvolutionLayer(convParams, in_channels, in_channels, bias_term);
2427 std::string actType = get<1>(GetParam());
2428 LayerParams activationParams;
2429 TestLayerFusion::makeDefaultTestActivationLayer(activationParams, actType, in_channels);
2431 std::string eltwiseOp = get<2>(GetParam());
2432 bool weightedEltwise = get<3>(GetParam());
2433 if (eltwiseOp != "sum" && weightedEltwise)
2434 throw SkipTestException("weighted eltwise not supported");
2435 LayerParams eltwiseParams;
2436 TestLayerFusion::makeDefaultTestEltwiseLayer(eltwiseParams, eltwiseOp, weightedEltwise);
2438 Backend backendId = get<0>(get<4>(GetParam()));
2439 Target targetId = get<1>(get<4>(GetParam()));
2441 // bug: https://github.com/opencv/opencv/issues/17964
2442 if (actType == "Power" && backendId == DNN_BACKEND_OPENCV && (targetId == DNN_TARGET_OPENCL || targetId == DNN_TARGET_OPENCL_FP16))
2443 applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
2445 // bug: https://github.com/opencv/opencv/issues/17953
2446 if (actType == "ChannelsPReLU" && bias_term == false &&
2447 backendId == DNN_BACKEND_OPENCV && (targetId == DNN_TARGET_OPENCL || targetId == DNN_TARGET_OPENCL_FP16))
2449 applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
2453 int convId = net.addLayer(convParams.name, convParams.type, convParams);
2454 int activId = net.addLayer(activationParams.name, activationParams.type, activationParams);
2455 int eltwiseId = net.addLayer(eltwiseParams.name, eltwiseParams.type, eltwiseParams);
2456 net.connect(0, 0, convId, 0);
2457 net.connect(convId, 0, activId, 0);
2458 net.connect(activId, 0, eltwiseId, 0);
2459 net.connect(0, 0, eltwiseId, 1);
2461 std::vector<int> expectedFusedLayers;
2462 if (backendId == DNN_BACKEND_OPENCV)
2464 if (targetId == DNN_TARGET_CPU)
2465 expectedFusedLayers.push_back(activId); // activation fused with convolution
2466 else if (targetId == DNN_TARGET_OPENCL || targetId == DNN_TARGET_OPENCL_FP16)
2468 if (actType == "ReLU" || actType == "ChannelsPReLU" || actType == "ReLU6" || actType == "TanH" || actType == "Power")
2469 expectedFusedLayers.push_back(activId); // activation fused with convolution
2473 TestLayerFusion::test(input, net, backendId, targetId, expectedFusedLayers);
2475 INSTANTIATE_TEST_CASE_P(TestLayerFusion, ConvolutionActivationEltwiseFusion, Combine(
2476 /* bias */ testing::Bool(),
2477 /* activation */ TestLayerFusion::activationLayersList(),
2478 /* eltwise op */ TestLayerFusion::eltwiseOpList(),
2479 /* eltwise weighted */ testing::Bool(),
2480 TestLayerFusion::dnnBackendsAndTargetsForFusionTests()