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,
103 String prototxt = _tf(basename + ".prototxt");
104 String caffemodel = _tf(basename + ".caffemodel");
106 String inpfile = (useCommonInputBlob) ? _tf("blob.npy") : _tf(basename + ".input.npy");
107 String outfile = _tf(basename + ".npy");
109 Mat inp = blobFromNPY(inpfile);
110 Mat ref = blobFromNPY(outfile);
111 checkBackend(&inp, &ref);
113 Net net = readNetFromCaffe(prototxt, (useCaffeModel) ? caffemodel : String());
114 ASSERT_FALSE(net.empty());
116 net.setPreferableBackend(backend);
117 net.setPreferableTarget(target);
119 net.setInput(inp, "input");
120 Mat out = net.forward("output");
122 normAssert(ref, out, "", l1 ? l1 : default_l1, lInf ? lInf : default_lInf);
126 TEST_P(Test_Caffe_layers, Softmax)
128 testLayerUsingCaffeModels("layer_softmax");
131 TEST_P(Test_Caffe_layers, LRN)
133 testLayerUsingCaffeModels("layer_lrn_spatial");
134 testLayerUsingCaffeModels("layer_lrn_channels");
137 TEST_P(Test_Caffe_layers, Convolution)
139 testLayerUsingCaffeModels("layer_convolution", true);
142 TEST_P(Test_Caffe_layers, DeConvolution)
144 testLayerUsingCaffeModels("layer_deconvolution", true, false);
147 TEST_P(Test_Caffe_layers, InnerProduct)
149 if (backend == DNN_BACKEND_INFERENCE_ENGINE)
150 applyTestTag(CV_TEST_TAG_DNN_SKIP_IE);
151 if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
152 applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
153 testLayerUsingCaffeModels("layer_inner_product", true);
156 TEST_P(Test_Caffe_layers, Pooling_max)
158 testLayerUsingCaffeModels("layer_pooling_max");
161 TEST_P(Test_Caffe_layers, Pooling_ave)
163 testLayerUsingCaffeModels("layer_pooling_ave");
166 TEST_P(Test_Caffe_layers, MVN)
168 if(backend == DNN_BACKEND_CUDA)
169 applyTestTag(CV_TEST_TAG_DNN_SKIP_CUDA); /* MVN is unsupported */
171 testLayerUsingCaffeModels("layer_mvn");
174 void testReshape(const MatShape& inputShape, const MatShape& targetShape,
175 int axis = 0, int num_axes = -1,
176 MatShape mask = MatShape())
179 params.set("axis", axis);
180 params.set("num_axes", num_axes);
183 params.set("dim", DictValue::arrayInt<int*>(&mask[0], mask.size()));
186 Mat inp(inputShape.size(), &inputShape[0], CV_32F);
187 std::vector<Mat> inpVec(1, inp);
188 std::vector<Mat> outVec, intVec;
190 Ptr<Layer> rl = LayerFactory::createLayerInstance("Reshape", params);
191 runLayer(rl, inpVec, outVec);
193 Mat& out = outVec[0];
194 MatShape shape(out.size.p, out.size.p + out.dims);
195 EXPECT_EQ(shape, targetShape);
198 TEST(Layer_Test_Reshape, Accuracy)
201 int inp[] = {4, 3, 1, 2};
202 int out[] = {4, 3, 2};
203 testReshape(MatShape(inp, inp + 4), MatShape(out, out + 3), 2, 1);
206 int inp[] = {1, 128, 4, 4};
207 int out[] = {1, 2048};
208 int mask[] = {-1, 2048};
209 testReshape(MatShape(inp, inp + 4), MatShape(out, out + 2), 0, -1,
210 MatShape(mask, mask + 2));
213 int inp[] = {1, 2, 3};
214 int out[] = {3, 1, 2};
215 int mask[] = {3, 1, 2};
216 testReshape(MatShape(inp, inp + 3), MatShape(out, out + 3), 0, -1,
217 MatShape(mask, mask + 3));
221 TEST_P(Test_Caffe_layers, BatchNorm)
223 testLayerUsingCaffeModels("layer_batch_norm", true);
224 testLayerUsingCaffeModels("layer_batch_norm_local_stats", true, false);
227 TEST_P(Test_Caffe_layers, ReLU)
229 testLayerUsingCaffeModels("layer_relu");
232 TEST_P(Test_Caffe_layers, Dropout)
234 testLayerUsingCaffeModels("layer_dropout");
237 TEST_P(Test_Caffe_layers, Concat)
239 #if defined(INF_ENGINE_RELEASE)
240 #if INF_ENGINE_VER_MAJOR_GE(2019010000) && INF_ENGINE_VER_MAJOR_LT(2019020000)
241 if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)
242 applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_2019R1, CV_TEST_TAG_DNN_SKIP_IE_2019R1_1);
243 #elif INF_ENGINE_VER_MAJOR_EQ(2019020000)
244 if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_OPENCL)
245 applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL, CV_TEST_TAG_DNN_SKIP_IE_2019R2);
248 testLayerUsingCaffeModels("layer_concat");
249 testLayerUsingCaffeModels("layer_concat_optim", true, false);
250 testLayerUsingCaffeModels("layer_concat_shared_input", true, false);
253 TEST_P(Test_Caffe_layers, Fused_Concat)
255 if (backend == DNN_BACKEND_INFERENCE_ENGINE && (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16))
256 applyTestTag(target == DNN_TARGET_OPENCL ? CV_TEST_TAG_DNN_SKIP_IE_OPENCL : CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16);
273 lp.name = "someLayer";
274 interLayer = net.addLayerToPrev(lp.name, lp.type, lp);
280 lp.name = "testConcat";
281 int id = net.addLayer(lp.name, lp.type, lp);
282 net.connect(interLayer, 0, id, 0);
283 net.connect(interLayer, 0, id, 1);
285 int shape[] = {1, 2, 3, 4};
286 Mat input(4, shape, CV_32F);
287 randu(input, 0.0f, 1.0f); // [0, 1] to make AbsVal an identity transformation.
290 net.setPreferableBackend(backend);
291 net.setPreferableTarget(target);
292 Mat out = net.forward();
294 normAssert(slice(out, Range::all(), Range(0, 2), Range::all(), Range::all()), input, "", default_l1, default_lInf);
295 normAssert(slice(out, Range::all(), Range(2, 4), Range::all(), Range::all()), input, "", default_l1, default_lInf);
298 TEST_P(Test_Caffe_layers, Eltwise)
300 if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)
301 applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD);
302 testLayerUsingCaffeModels("layer_eltwise");
305 TEST_P(Test_Caffe_layers, PReLU)
307 testLayerUsingCaffeModels("layer_prelu", true);
310 // TODO: fix an unstable test case
311 TEST_P(Test_Caffe_layers, layer_prelu_fc)
313 if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
314 applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
315 // Reference output values are in range [-0.0001, 10.3906]
316 double l1 = (target == DNN_TARGET_MYRIAD) ? 0.005 : 0.0;
317 double lInf = (target == DNN_TARGET_MYRIAD) ? 0.021 : 0.0;
318 testLayerUsingCaffeModels("layer_prelu_fc", true, false, l1, lInf);
321 TEST_P(Test_Caffe_layers, Reshape_Split_Slice)
323 if (backend == DNN_BACKEND_INFERENCE_ENGINE)
324 applyTestTag(CV_TEST_TAG_DNN_SKIP_IE);
326 Net net = readNetFromCaffe(_tf("reshape_and_slice_routines.prototxt"));
327 ASSERT_FALSE(net.empty());
329 net.setPreferableBackend(backend);
330 net.setPreferableTarget(target);
332 Mat input(6, 12, CV_32F);
334 rng.fill(input, RNG::UNIFORM, -1, 1);
336 net.setInput(input, "input");
337 Mat output = net.forward("output");
339 normAssert(input, output, "", default_l1, default_lInf);
342 TEST_P(Test_Caffe_layers, Conv_Elu)
344 #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_RELEASE <= 2018050000
345 if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)
346 applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_2018R5);
349 Net net = readNetFromTensorflow(_tf("layer_elu_model.pb"));
350 ASSERT_FALSE(net.empty());
352 Mat inp = blobFromNPY(_tf("layer_elu_in.npy"));
353 Mat ref = blobFromNPY(_tf("layer_elu_out.npy"));
355 net.setInput(inp, "input");
356 net.setPreferableBackend(backend);
357 net.setPreferableTarget(target);
358 Mat out = net.forward();
360 normAssert(ref, out, "", default_l1, default_lInf);
363 class Layer_LSTM_Test : public ::testing::Test
368 Ptr<LSTMLayer> layer;
369 std::vector<Mat> inputs, outputs;
373 void init(const MatShape &inpShape_, const MatShape &outShape_,
374 bool produceCellOutput, bool useTimestampDim)
376 numInp = total(inpShape_);
377 numOut = total(outShape_);
379 Wh = Mat::ones(4 * numOut, numOut, CV_32F);
380 Wx = Mat::ones(4 * numOut, numInp, CV_32F);
381 b = Mat::ones(4 * numOut, 1, CV_32F);
388 lp.set<bool>("produce_cell_output", produceCellOutput);
389 lp.set<bool>("use_timestamp_dim", useTimestampDim);
391 layer = LSTMLayer::create(lp);
392 layer->setOutShape(outShape_);
396 TEST_F(Layer_LSTM_Test, get_set_test)
399 MatShape inpShape = shape(5, 3, 2);
400 MatShape outShape = shape(3, 1, 2);
401 MatShape inpResShape = concat(shape(TN), inpShape);
402 MatShape outResShape = concat(shape(TN), outShape);
404 init(inpShape, outShape, true, false);
405 layer->setOutShape(outShape);
407 Mat C((int)outResShape.size(), &outResShape[0], CV_32F);
412 Mat inp((int)inpResShape.size(), &inpResShape[0], CV_32F);
415 inputs.push_back(inp);
416 runLayer(layer, inputs, outputs);
418 EXPECT_EQ(2u, outputs.size());
420 print(outResShape, "outResShape");
421 print(shape(outputs[0]), "out0");
422 print(shape(outputs[0]), "out1");
424 EXPECT_EQ(outResShape, shape(outputs[0]));
425 EXPECT_EQ(outResShape, shape(outputs[1]));
427 EXPECT_EQ(0, layer->inputNameToIndex("x"));
428 EXPECT_EQ(0, layer->outputNameToIndex("h"));
429 EXPECT_EQ(1, layer->outputNameToIndex("c"));
432 TEST(Layer_LSTM_Test_Accuracy_with_, CaffeRecurrent)
436 lp.blobs[0] = blobFromNPY(_tf("lstm.prototxt.w_2.npy")); // Wh
437 lp.blobs[1] = blobFromNPY(_tf("lstm.prototxt.w_0.npy")); // Wx
438 lp.blobs[2] = blobFromNPY(_tf("lstm.prototxt.w_1.npy")); // bias
439 Ptr<LSTMLayer> layer = LSTMLayer::create(lp);
441 Mat inp = blobFromNPY(_tf("recurrent.input.npy"));
442 std::vector<Mat> inputs(1, inp), outputs;
443 runLayer(layer, inputs, outputs);
445 Mat h_t_reference = blobFromNPY(_tf("lstm.prototxt.h_1.npy"));
446 normAssert(h_t_reference, outputs[0]);
449 TEST(Layer_RNN_Test_Accuracy_with_, CaffeRecurrent)
451 Ptr<RNNLayer> layer = RNNLayer::create(LayerParams());
454 blobFromNPY(_tf("rnn.prototxt.w_0.npy")),
455 blobFromNPY(_tf("rnn.prototxt.w_1.npy")),
456 blobFromNPY(_tf("rnn.prototxt.w_2.npy")),
457 blobFromNPY(_tf("rnn.prototxt.w_3.npy")),
458 blobFromNPY(_tf("rnn.prototxt.w_4.npy")) );
460 std::vector<Mat> output, input(1, blobFromNPY(_tf("recurrent.input.npy")));
461 runLayer(layer, input, output);
463 Mat h_ref = blobFromNPY(_tf("rnn.prototxt.h_1.npy"));
464 normAssert(h_ref, output[0]);
467 TEST(Layer_LSTM_Test_Accuracy_, Reverse)
469 // This handcrafted setup calculates (approximately) the prefix sum of the
470 // input, assuming the inputs are suitably small.
471 cv::Mat input(2, 1, CV_32FC1);
472 input.at<float>(0, 0) = 1e-5f;
473 input.at<float>(1, 0) = 2e-5f;
475 cv::Mat Wx(4, 1, CV_32FC1);
476 Wx.at<float>(0, 0) = 0.f; // Input gate
477 Wx.at<float>(1, 0) = 0.f; // Forget gate
478 Wx.at<float>(2, 0) = 0.f; // Output gate
479 Wx.at<float>(3, 0) = 1.f; // Update signal
481 cv::Mat Wh(4, 1, CV_32FC1);
482 Wh.at<float>(0, 0) = 0.f; // Input gate
483 Wh.at<float>(1, 0) = 0.f; // Forget gate
484 Wh.at<float>(2, 0) = 0.f; // Output gate
485 Wh.at<float>(3, 0) = 0.f; // Update signal
487 cv::Mat bias(4, 1, CV_32FC1);
488 bias.at<float>(0, 0) = 1e10f; // Input gate - always allows input to c
489 bias.at<float>(1, 0) = 1e10f; // Forget gate - never forget anything on c
490 bias.at<float>(2, 0) = 1e10f; // Output gate - always output everything
491 bias.at<float>(3, 0) = 0.f; // Update signal
494 lp.set("reverse", true);
495 lp.set("use_timestamp_dim", true);
497 lp.blobs.push_back(Wh);
498 lp.blobs.push_back(Wx);
499 lp.blobs.push_back(bias);
501 cv::Ptr<cv::dnn::LSTMLayer> layer = LSTMLayer::create(lp);
502 std::vector<cv::Mat> outputs;
503 std::vector<cv::Mat> inputs;
504 inputs.push_back(input);
505 runLayer(layer, inputs, outputs);
507 ASSERT_EQ(1, outputs.size());
508 cv::Mat out = outputs[0];
509 ASSERT_EQ(3, out.dims);
510 ASSERT_EQ(shape(2, 1, 1), shape(out));
511 float* data = reinterpret_cast<float*>(out.data);
512 EXPECT_NEAR(std::tanh(1e-5f) + std::tanh(2e-5f), data[0], 1e-10);
513 EXPECT_NEAR(std::tanh(2e-5f), data[1], 1e-10);
517 class Layer_RNN_Test : public ::testing::Test
520 int nX, nH, nO, nT, nS;
521 Mat Whh, Wxh, bh, Who, bo;
524 std::vector<Mat> inputs, outputs;
534 Whh = Mat::ones(nH, nH, CV_32F);
535 Wxh = Mat::ones(nH, nX, CV_32F);
536 bh = Mat::ones(nH, 1, CV_32F);
537 Who = Mat::ones(nO, nH, CV_32F);
538 bo = Mat::ones(nO, 1, CV_32F);
540 layer = RNNLayer::create(LayerParams());
541 layer->setProduceHiddenOutput(true);
542 layer->setWeights(Wxh, bh, Whh, Who, bo);
546 TEST_F(Layer_RNN_Test, get_set_test)
548 int sz[] = { nT, nS, 1, nX };
549 Mat inp(4, sz, CV_32F);
551 inputs.push_back(inp);
552 runLayer(layer, inputs, outputs);
554 EXPECT_EQ(outputs.size(), 2u);
555 EXPECT_EQ(shape(outputs[0]), shape(nT, nS, nO));
556 EXPECT_EQ(shape(outputs[1]), shape(nT, nS, nH));
559 TEST(Layer_Test_ROIPooling, Accuracy)
561 Net net = readNetFromCaffe(_tf("net_roi_pooling.prototxt"));
563 Mat inp = blobFromNPY(_tf("net_roi_pooling.input.npy"));
564 Mat rois = blobFromNPY(_tf("net_roi_pooling.rois.npy"));
565 Mat ref = blobFromNPY(_tf("net_roi_pooling.npy"));
567 net.setInput(inp, "input");
568 net.setInput(rois, "rois");
569 net.setPreferableBackend(DNN_BACKEND_OPENCV);
571 Mat out = net.forward();
573 normAssert(out, ref);
576 TEST_P(Test_Caffe_layers, FasterRCNN_Proposal)
578 if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
579 applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
580 if (backend == DNN_BACKEND_INFERENCE_ENGINE)
581 applyTestTag(CV_TEST_TAG_DNN_SKIP_IE);
582 if(backend == DNN_BACKEND_CUDA)
583 applyTestTag(CV_TEST_TAG_DNN_SKIP_CUDA); /* Proposal layer is unsupported */
585 Net net = readNetFromCaffe(_tf("net_faster_rcnn_proposal.prototxt"));
587 Mat scores = blobFromNPY(_tf("net_faster_rcnn_proposal.scores.npy"));
588 Mat deltas = blobFromNPY(_tf("net_faster_rcnn_proposal.deltas.npy"));
589 Mat imInfo = (Mat_<float>(1, 3) << 600, 800, 1.6f);
591 net.setInput(scores, "rpn_cls_prob_reshape");
592 net.setInput(deltas, "rpn_bbox_pred");
593 net.setInput(imInfo, "im_info");
595 std::vector<Mat> outs;
596 net.setPreferableBackend(backend);
597 net.setPreferableTarget(target);
598 net.forward(outs, "output");
600 for (int i = 0; i < 2; ++i)
602 Mat ref = blobFromNPY(_tf(i == 0 ? "net_faster_rcnn_proposal.out_rois.npy" :
603 "net_faster_rcnn_proposal.out_scores.npy"));
604 const int numDets = ref.size[0];
605 EXPECT_LE(numDets, outs[i].size[0]);
606 normAssert(outs[i].rowRange(0, numDets), ref);
608 if (numDets < outs[i].size[0])
610 EXPECT_EQ(countNonZero(outs[i].rowRange(numDets, outs[i].size[0])), 0);
615 typedef testing::TestWithParam<tuple<Vec4i, Vec2i, bool> > Scale_untrainable;
616 TEST_P(Scale_untrainable, Accuracy)
618 Vec4i inpShapeVec = get<0>(GetParam());
619 int axis = get<1>(GetParam())[0];
620 int weightsDims = get<1>(GetParam())[1];
621 bool testFusion = get<2>(GetParam());
622 const int inpShape[] = {inpShapeVec[0], inpShapeVec[1], inpShapeVec[2], inpShapeVec[3]};
624 // Create a network with two inputs. Scale layer multiplies a first input to
625 // a second one. See http://caffe.berkeleyvision.org/tutorial/layers/scale.html
627 // Check that this version of Scale layer won't be fused with Convolution layer.
631 lp.set("kernel_size", 1);
632 lp.set("num_output", 3);
634 lp.set("bias_term", false);
635 lp.type = "Convolution";
636 lp.name = "testConv";
638 std::vector<int> weightsShape(4);
639 weightsShape[0] = 3; // #outChannels
640 weightsShape[1] = 1; // #inpChannels / group
641 weightsShape[2] = 1; // height
642 weightsShape[3] = 1; // width
643 Mat weights(weightsShape, CV_32F);
645 lp.blobs.push_back(weights);
646 net.addLayerToPrev(lp.name, lp.type, lp);
650 lp.name = "testLayer";
651 lp.set("axis", axis);
652 int id = net.addLayerToPrev(lp.name, lp.type, lp);
653 net.connect(0, 1, id, 1);
655 Mat input(4, inpShape, CV_32F);
656 Mat weights(weightsDims, &inpShape[axis], CV_32F);
658 randu(weights, -1, 1);
660 std::vector<String> inpNames(2);
661 inpNames[0] = "scale_input";
662 inpNames[1] = "scale_weights";
663 net.setInputsNames(inpNames);
664 net.setInput(input, inpNames[0]);
665 net.setInput(weights, inpNames[1]);
666 net.setPreferableBackend(DNN_BACKEND_OPENCV);
667 Mat out = net.forward();
669 Mat ref(input.dims, input.size, CV_32F);
670 float* inpData = (float*)input.data;
671 float* refData = (float*)ref.data;
672 float* weightsData = (float*)weights.data;
674 for (int i = axis + weightsDims; i < 4; ++i)
675 spatialSize *= inpShape[i];
676 for (int i = 0; i < ref.total(); ++i)
678 float w = weightsData[(i / spatialSize) % weights.total()];
679 refData[i] = inpData[i] * w;
681 normAssert(out, ref);
684 INSTANTIATE_TEST_CASE_P(Layer_Test, Scale_untrainable, Combine(
685 /*input size*/ Values(Vec4i(2, 3, 4, 5)),
686 /*axis, #dims*/ Values(Vec2i(0, 1), Vec2i(0, 2), Vec2i(0, 3), Vec2i(0, 4),
687 Vec2i(1, 1), Vec2i(1, 2), Vec2i(1, 3),
688 Vec2i(2, 1), Vec2i(2, 2),
690 /*conv fusion*/ testing::Bool()
693 typedef testing::TestWithParam<tuple<Vec4i, Vec4i, int, int, int> > Crop;
694 TEST_P(Crop, Accuracy)
696 Vec4i inpShapeVec = get<0>(GetParam());
697 Vec4i sizShapeVec = get<1>(GetParam());
698 int axis = get<2>(GetParam());
699 int numOffsets = get<3>(GetParam());
700 int offsetVal = get<4>(GetParam());
701 const int inpShape[] = {inpShapeVec[0], inpShapeVec[1], inpShapeVec[2], inpShapeVec[3]};
702 const int sizShape[] = {sizShapeVec[0], sizShapeVec[1], sizShapeVec[2], sizShapeVec[3]};
704 // Create a network with two inputs. Crop layer crops a first input to
705 // the size of a second one.
706 // See http://caffe.berkeleyvision.org/tutorial/layers/crop.html
710 lp.name = "testCrop";
712 lp.set("axis", axis);
715 std::vector<int> offsets(numOffsets, offsetVal);
716 lp.set("offset", DictValue::arrayInt<int*>(&offsets[0], offsets.size()));
720 int id = net.addLayerToPrev(lp.name, lp.type, lp);
721 net.connect(0, 1, id, 1);
723 Mat inpImage(4, inpShape, CV_32F);
724 Mat sizImage(4, sizShape, CV_32F);
725 randu(inpImage, -1, 1);
726 randu(sizImage, -1, 1);
728 std::vector<String> inpNames(2);
729 inpNames[0] = "cropImage";
730 inpNames[1] = "sizImage";
731 net.setInputsNames(inpNames);
732 net.setInput(inpImage, inpNames[0]);
733 net.setInput(sizImage, inpNames[1]);
734 net.setPreferableBackend(DNN_BACKEND_OPENCV);
736 // There are a few conditions that represent invalid input to the crop
737 // layer, so in those cases we want to verify an exception is thrown.
739 bool shouldThrowException = false;
740 if (numOffsets > 1 && numOffsets != 4 - axis)
741 shouldThrowException = true;
743 for (int i = axis; i < 4; i++)
744 if (sizShape[i] + offsetVal > inpShape[i])
745 shouldThrowException = true;
748 if (shouldThrowException)
750 ASSERT_ANY_THROW(out = net.forward());
756 // Finally, compare the cropped output blob from the DNN layer (out)
757 // to a reference blob (ref) that we compute here.
759 std::vector<Range> crop_range;
760 crop_range.resize(4, Range::all());
761 for (int i = axis; i < 4; i++)
762 crop_range[i] = Range(offsetVal, sizShape[i] + offsetVal);
764 Mat ref(sizImage.dims, sizImage.size, CV_32F);
765 inpImage(&crop_range[0]).copyTo(ref);
766 normAssert(out, ref);
769 INSTANTIATE_TEST_CASE_P(Layer_Test, Crop, Combine(
770 /*input blob shape*/ Values(Vec4i(1, 3, 20, 30)),
771 /*cropsize blob shape*/ Values(Vec4i(1, 3, 10, 12)),
772 /*start axis*/ Values(0, 1, 2),
773 /*number of offsets*/ Values(0, 1, 2, 4),
774 /*offset value*/ Values(3, 4)
777 // Check that by default average pooling layer should not count zero padded values
778 // into the normalization area.
779 TEST_P(Test_Caffe_layers, Average_pooling_kernel_area)
782 lp.name = "testAvePool";
784 lp.set("kernel_size", 2);
786 lp.set("pool", "AVE");
789 net.addLayerToPrev(lp.name, lp.type, lp);
794 Mat inp = (Mat_<float>(3, 3) << 1, 2, 3, 4, 5, 6, 7, 8, 9);
795 Mat ref = (Mat_<float>(2, 2) << (1 + 2 + 4 + 5) / 4.f, (3 + 6) / 2.f, (7 + 8) / 2.f, 9);
796 Mat tmp = blobFromImage(inp);
797 net.setInput(blobFromImage(inp));
798 net.setPreferableBackend(backend);
799 net.setPreferableTarget(target);
800 Mat out = net.forward();
801 normAssert(out, blobFromImage(ref));
804 TEST_P(Test_Caffe_layers, PriorBox_repeated)
806 Net net = readNet(_tf("prior_box.prototxt"));
807 int inp_size[] = {1, 3, 10, 10};
808 int shape_size[] = {1, 2, 3, 4};
809 Mat inp(4, inp_size, CV_32F);
810 randu(inp, -1.0f, 1.0f);
811 Mat shape(4, shape_size, CV_32F);
812 randu(shape, -1.0f, 1.0f);
813 net.setInput(inp, "data");
814 net.setInput(shape, "shape");
815 Mat out = net.forward();
816 Mat ref = blobFromNPY(_tf("priorbox_output.npy"));
817 normAssert(out, ref, "");
820 // Test PriorBoxLayer in case of no aspect ratios (just squared proposals).
821 TEST_P(Test_Caffe_layers, PriorBox_squares)
823 if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)
824 applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD);
826 lp.name = "testPriorBox";
827 lp.type = "PriorBox";
828 lp.set("min_size", 2);
829 lp.set("flip", true);
830 lp.set("clip", true);
831 float variance[] = {0.1f, 0.1f, 0.2f, 0.2f};
832 float aspectRatios[] = {1.0f}; // That should be ignored.
833 lp.set("variance", DictValue::arrayReal<float*>(&variance[0], 4));
834 lp.set("aspect_ratio", DictValue::arrayReal<float*>(&aspectRatios[0], 1));
837 int id = net.addLayerToPrev(lp.name, lp.type, lp);
838 net.connect(0, 0, id, 1); // The second input is an input image. Shapes are used for boxes normalization.
839 Mat inp(1, 2, CV_32F);
841 net.setInput(blobFromImage(inp));
842 net.setPreferableBackend(backend);
843 net.setPreferableTarget(target);
844 Mat out = net.forward();
846 Mat ref = (Mat_<float>(4, 4) << 0.0, 0.0, 0.75, 1.0,
848 0.1f, 0.1f, 0.2f, 0.2f,
849 0.1f, 0.1f, 0.2f, 0.2f);
850 double l1 = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 2e-5 : 1e-5;
851 normAssert(out.reshape(1, 4), ref, "", l1);
854 typedef TestWithParam<tuple<int, int> > Layer_Test_DWconv_Prelu;
855 TEST_P(Layer_Test_DWconv_Prelu, Accuracy)
858 // input img size 3x16x16 value all 1
861 // dw_conv weight[0]=-1 weight[1]=-2 weight[2]=-3 bias={1,2,3}
864 // prelu weight={1,2,3}
867 // output out size 3x14x14 if right: out[0]=-8 out[0]=-32 out[0]=-72
868 // but current opencv output: out[0]=-24 out[0]=-48 out[0]=-72
870 const int num_input = get<0>(GetParam()); //inpChannels
871 const int group = 3; //outChannels=group when group>1
872 const int num_output = get<1>(GetParam());
873 const int kernel_depth = num_input/group;
874 CV_Assert_N(num_output >= group, num_output % group == 0, num_input % group == 0);
880 lp.type = "Convolution";
881 lp.set("kernel_size", 3);
882 lp.set("num_output", num_output);
884 lp.set("group", group);
886 lp.set("engine", "CAFFE");
887 lp.set("bias_term", "true");
889 std::vector<int> weightsShape(4);
890 weightsShape[0] = num_output; // #outChannels
891 weightsShape[1] = kernel_depth; // #inpChannels / group
892 weightsShape[2] = 3; // height
893 weightsShape[3] = 3; // width
894 Mat weights(weightsShape, CV_32F, Scalar(1));
897 for (int i = 0; i < weightsShape[0]; ++i)
899 for (int j = 0; j < weightsShape[1]; ++j)
901 for (int k = 0; k < weightsShape[2]; ++k)
903 for (int l = 0; l < weightsShape[3]; ++l)
905 weights.ptr<float>(i, j, k)[l]=-1*(i+1);
910 lp.blobs.push_back(weights);
913 Mat bias(1, num_output, CV_32F, Scalar(1));
914 for (int i = 0; i < 1; ++i)
916 for (int j = 0; j < num_output; ++j)
918 bias.ptr<float>(i)[j]=j+1;
921 lp.blobs.push_back(bias);
922 net.addLayerToPrev(lp.name, lp.type, lp);
926 lpr.name = "dw_relu";
928 Mat weightsp(1, num_output, CV_32F, Scalar(1));
931 for (int i = 0; i < 1; ++i)
933 for (int j = 0; j < num_output; ++j)
935 weightsp.ptr<float>(i)[j]=j+1;
939 lpr.blobs.push_back(weightsp);
940 net.addLayerToPrev(lpr.name, lpr.type, lpr);
942 int shape[] = {1, num_input, 16, 16};
943 Mat in_blob(4, &shape[0], CV_32FC1, Scalar(1));
945 net.setPreferableBackend(DNN_BACKEND_OPENCV);
946 net.setInput(in_blob);
947 Mat out = net.forward();
950 std::vector<int> outShape(4);
952 outShape[1] = num_output; // outChannels
953 outShape[2] = 14; // height
954 outShape[3] = 14; // width
955 Mat target(outShape, CV_32F, Scalar(1));
956 for (int i = 0; i < outShape[0]; ++i)
958 for (int j = 0; j < outShape[1]; ++j)
960 for (int k = 0; k < outShape[2]; ++k)
962 for (int l = 0; l < outShape[3]; ++l)
964 target.ptr<float>(i, j, k)[l]=(-9*kernel_depth*(j+1)+j+1)*(j+1);
970 normAssert(out, target);
972 INSTANTIATE_TEST_CASE_P(/**/, Layer_Test_DWconv_Prelu, Combine(Values(3, 6), Values(3, 6)));
974 #ifdef HAVE_INF_ENGINE
975 // Using Intel's Model Optimizer generate .xml and .bin files:
976 // ./ModelOptimizer -w /path/to/caffemodel -d /path/to/prototxt \
977 // -p FP32 -i -b ${batch_size} -o /path/to/output/folder
978 typedef testing::TestWithParam<Target> Layer_Test_Convolution_DLDT;
979 TEST_P(Layer_Test_Convolution_DLDT, Accuracy)
981 Target targetId = GetParam();
983 std::string suffix = (targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) ? "_fp16" : "";
984 Net netDefault = readNet(_tf("layer_convolution.caffemodel"), _tf("layer_convolution.prototxt"));
985 Net net = readNet(_tf("layer_convolution" + suffix + ".xml"), _tf("layer_convolution" + suffix + ".bin"));
987 Mat inp = blobFromNPY(_tf("blob.npy"));
989 netDefault.setInput(inp);
990 netDefault.setPreferableBackend(DNN_BACKEND_OPENCV);
991 Mat outDefault = netDefault.forward();
994 net.setPreferableTarget(targetId);
996 Mat out = net.forward();
998 double l1 = (targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) ? 1.5e-3 : 1e-5;
999 double lInf = (targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) ? 1.8e-2 : 1e-4;
1000 normAssert(outDefault, out, "", l1, lInf);
1002 std::vector<int> outLayers = net.getUnconnectedOutLayers();
1003 ASSERT_EQ(net.getLayer(outLayers[0])->name, "output");
1004 ASSERT_EQ(net.getLayer(outLayers[0])->type, "Convolution");
1007 TEST_P(Layer_Test_Convolution_DLDT, setInput_uint8)
1009 Target targetId = GetParam();
1010 Mat inp = blobFromNPY(_tf("blob.npy"));
1012 Mat inputs[] = {Mat(inp.dims, inp.size, CV_8U), Mat()};
1013 randu(inputs[0], 0, 255);
1014 inputs[0].convertTo(inputs[1], CV_32F);
1016 std::string suffix = (targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) ? "_fp16" : "";
1019 for (int i = 0; i < 2; ++i)
1021 Net net = readNet(_tf("layer_convolution" + suffix + ".xml"), _tf("layer_convolution" + suffix + ".bin"));
1022 net.setPreferableTarget(targetId);
1023 net.setInput(inputs[i]);
1024 outs[i] = net.forward();
1025 ASSERT_EQ(outs[i].type(), CV_32F);
1027 if (targetId != DNN_TARGET_MYRIAD)
1028 normAssert(outs[0], outs[1]);
1031 TEST_P(Layer_Test_Convolution_DLDT, multithreading)
1033 Target targetId = GetParam();
1034 std::string suffix = (targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) ? "_fp16" : "";
1035 std::string xmlPath = _tf("layer_convolution" + suffix + ".xml");
1036 std::string binPath = _tf("layer_convolution" + suffix + ".bin");
1037 Net firstNet = readNet(xmlPath, binPath);
1038 Net secondNet = readNet(xmlPath, binPath);
1039 Mat inp = blobFromNPY(_tf("blob.npy"));
1041 firstNet.setInput(inp);
1042 secondNet.setInput(inp);
1043 firstNet.setPreferableTarget(targetId);
1044 secondNet.setPreferableTarget(targetId);
1047 std::thread t1([&]{out1 = firstNet.forward();});
1048 std::thread t2([&]{out2 = secondNet.forward();});
1053 Mat ref = blobFromNPY(_tf("layer_convolution.npy"));
1054 double l1 = (targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) ? 1.5e-3 : 1e-5;
1055 double lInf = (targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) ? 1.8e-2 : 1e-4;
1056 normAssert(out1, ref, "first thread", l1, lInf);
1057 normAssert(out2, ref, "second thread", l1, lInf);
1060 INSTANTIATE_TEST_CASE_P(/**/, Layer_Test_Convolution_DLDT,
1061 testing::ValuesIn(getAvailableTargets(DNN_BACKEND_INFERENCE_ENGINE)));
1063 // 1. Create a .prototxt file with the following network:
1065 // type: "Input" name: "data" top: "data"
1066 // input_param { shape { dim: 1 dim: 2 dim: 3 } }
1069 // type: "Input" name: "second_input" top: "second_input"
1070 // input_param { shape { dim: 1 dim: 2 dim: 3 } }
1073 // type: "Eltwise" name: "output" top: "output"
1074 // bottom: "data" bottom: "second_input"
1075 // eltwise_param { operation: SUM }
1078 // 2. Create a .caffemodel file using Caffe:
1081 // net = caffe.Net('/path/to/prototxt', caffe.TEST)
1082 // net.save('/path/to/caffemodel')
1084 // 3. Convert using ModelOptimizer.
1085 typedef testing::TestWithParam<tuple<int, int, Target, std::vector<int> > > Test_DLDT_two_inputs_3dim;
1086 TEST_P(Test_DLDT_two_inputs_3dim, as_IR)
1088 int firstInpType = get<0>(GetParam());
1089 int secondInpType = get<1>(GetParam());
1090 Target targetId = get<2>(GetParam());
1092 std::string suffix = (targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) ? "_fp16" : "";
1093 Net net = readNet(_tf("net_two_inputs" + suffix + ".xml"), _tf("net_two_inputs.bin"));
1094 std::vector<int> inpSize = get<3>(GetParam());
1095 Mat firstInp(3, inpSize.data(), firstInpType);
1096 Mat secondInp(3, inpSize.data(), secondInpType);
1097 randu(firstInp, 0, 255);
1098 randu(secondInp, 0, 255);
1100 net.setInput(firstInp, "data");
1101 net.setInput(secondInp, "second_input");
1102 net.setPreferableTarget(targetId);
1104 double l1 = ((targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) &&
1105 (firstInpType == CV_32F || secondInpType == CV_32F)) ? 0.06 : 0.0;
1106 double lInf = ((targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) &&
1107 (firstInpType == CV_32F || secondInpType == CV_32F)) ? 0.23 : 0.0;
1109 Mat out = net.forward();
1112 cv::add(firstInp, secondInp, ref, Mat(), CV_32F);
1113 normAssert(out, ref, "", l1, lInf);
1116 std::vector< std::vector<int> > list_sizes{ {1, 2, 3}, {3, 2, 1}, {5, 5, 5}, {13, 7, 11} };
1118 INSTANTIATE_TEST_CASE_P(/*nothing*/, Test_DLDT_two_inputs_3dim, Combine(
1119 Values(CV_8U, CV_32F), Values(CV_8U, CV_32F),
1120 testing::ValuesIn(getAvailableTargets(DNN_BACKEND_INFERENCE_ENGINE)),
1121 testing::ValuesIn(list_sizes)
1124 typedef testing::TestWithParam<tuple<int, int, Target> > Test_DLDT_two_inputs;
1125 TEST_P(Test_DLDT_two_inputs, as_backend)
1127 static const float kScale = 0.5f;
1128 static const float kScaleInv = 1.0f / kScale;
1130 Target targetId = get<2>(GetParam());
1134 lp.type = "Eltwise";
1135 lp.name = "testLayer";
1136 lp.set("operation", "sum");
1137 int eltwiseId = net.addLayerToPrev(lp.name, lp.type, lp); // connect to a first input
1138 net.connect(0, 1, eltwiseId, 1); // connect to a second input
1140 int inpSize[] = {1, 2, 3, 4};
1141 Mat firstInp(4, &inpSize[0], get<0>(GetParam()));
1142 Mat secondInp(4, &inpSize[0], get<1>(GetParam()));
1143 randu(firstInp, 0, 255);
1144 randu(secondInp, 0, 255);
1146 net.setInputsNames({"data", "second_input"});
1147 net.setInput(firstInp, "data", kScale);
1148 net.setInput(secondInp, "second_input", kScaleInv);
1149 net.setPreferableBackend(DNN_BACKEND_INFERENCE_ENGINE);
1150 net.setPreferableTarget(targetId);
1151 Mat out = net.forward();
1154 addWeighted(firstInp, kScale, secondInp, kScaleInv, 0, ref, CV_32F);
1155 // Output values are in range [0, 637.5].
1156 double l1 = (targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) ? 0.06 : 1e-6;
1157 double lInf = (targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) ? 0.3 : 1e-5;
1158 normAssert(out, ref, "", l1, lInf);
1161 INSTANTIATE_TEST_CASE_P(/*nothing*/, Test_DLDT_two_inputs, Combine(
1162 Values(CV_8U, CV_32F), Values(CV_8U, CV_32F),
1163 testing::ValuesIn(getAvailableTargets(DNN_BACKEND_INFERENCE_ENGINE))
1166 class UnsupportedLayer : public Layer
1169 UnsupportedLayer(const LayerParams ¶ms) : Layer(params) {}
1171 static Ptr<Layer> create(const LayerParams& params)
1173 return Ptr<Layer>(new UnsupportedLayer(params));
1176 virtual bool supportBackend(int backendId) CV_OVERRIDE
1178 return backendId == DNN_BACKEND_OPENCV;
1181 virtual void forward(cv::InputArrayOfArrays inputs, cv::OutputArrayOfArrays outputs, cv::OutputArrayOfArrays internals) CV_OVERRIDE {}
1184 TEST(Test_DLDT, fused_output)
1186 static const int kNumChannels = 3;
1187 CV_DNN_REGISTER_LAYER_CLASS(Unsupported, UnsupportedLayer);
1191 lp.set("kernel_size", 1);
1192 lp.set("num_output", 3);
1193 lp.set("bias_term", false);
1194 lp.type = "Convolution";
1195 lp.name = "testConv";
1196 lp.blobs.push_back(Mat({kNumChannels, 1, 1, 1}, CV_32F, Scalar(1)));
1197 net.addLayerToPrev(lp.name, lp.type, lp);
1201 lp.set("bias_term", false);
1203 lp.name = "testScale";
1204 lp.blobs.push_back(Mat({kNumChannels}, CV_32F, Scalar(1)));
1205 net.addLayerToPrev(lp.name, lp.type, lp);
1209 net.addLayerToPrev("unsupported_layer", "Unsupported", lp);
1211 net.setPreferableBackend(DNN_BACKEND_INFERENCE_ENGINE);
1212 net.setInput(Mat({1, 1, 1, 1}, CV_32FC1, Scalar(1)));
1213 ASSERT_NO_THROW(net.forward());
1214 LayerFactory::unregisterLayer("Unsupported");
1217 TEST(Test_DLDT, multiple_networks)
1220 for (int i = 0; i < 2; ++i)
1222 nets[i].setInputsNames(std::vector<String>(1, format("input_%d", i)));
1225 lp.set("kernel_size", 1);
1226 lp.set("num_output", 1);
1227 lp.set("bias_term", false);
1228 lp.type = "Convolution";
1229 lp.name = format("testConv_%d", i);
1230 lp.blobs.push_back(Mat({1, 1, 1, 1}, CV_32F, Scalar(1 + i)));
1231 nets[i].addLayerToPrev(lp.name, lp.type, lp);
1232 nets[i].setPreferableBackend(DNN_BACKEND_INFERENCE_ENGINE);
1233 nets[i].setInput(Mat({1, 1, 1, 1}, CV_32FC1, Scalar(1)));
1235 Mat out_1 = nets[0].forward();
1236 Mat out_2 = nets[1].forward();
1237 // After the second model is initialized we try to receive an output from the first network again.
1238 out_1 = nets[0].forward();
1239 normAssert(2 * out_1, out_2);
1241 #endif // HAVE_INF_ENGINE
1243 // Test a custom layer.
1244 class CustomInterpLayer CV_FINAL : public Layer
1247 CustomInterpLayer(const LayerParams ¶ms) : Layer(params)
1249 zoomFactor = params.get<int>("zoom_factor", 0);
1250 outWidth = params.get<int>("width", 0);
1251 outHeight = params.get<int>("height", 0);
1254 static Ptr<Layer> create(LayerParams& params)
1256 return Ptr<Layer>(new CustomInterpLayer(params));
1259 virtual bool getMemoryShapes(const std::vector<std::vector<int> > &inputs,
1260 const int requiredOutputs,
1261 std::vector<std::vector<int> > &outputs,
1262 std::vector<std::vector<int> > &internals) const CV_OVERRIDE
1264 const int batchSize = inputs[0][0];
1265 const int numChannels = inputs[0][1];
1266 const int inpHeight = inputs[0][2];
1267 const int inpWidth = inputs[0][3];
1269 std::vector<int> outShape(4);
1270 outShape[0] = batchSize;
1271 outShape[1] = numChannels;
1272 outShape[2] = outHeight != 0 ? outHeight : (inpHeight + (inpHeight - 1) * (zoomFactor - 1));
1273 outShape[3] = outWidth != 0 ? outWidth : (inpWidth + (inpWidth - 1) * (zoomFactor - 1));
1274 outputs.assign(1, outShape);
1278 virtual void finalize(InputArrayOfArrays, OutputArrayOfArrays outputs_arr) CV_OVERRIDE
1280 std::vector<Mat> outputs;
1281 outputs_arr.getMatVector(outputs);
1283 if (!outWidth && !outHeight)
1285 outHeight = outputs[0].size[2];
1286 outWidth = outputs[0].size[3];
1290 // Implementation of this custom layer is based on https://github.com/cdmh/deeplab-public/blob/master/src/caffe/layers/interp_layer.cpp
1291 void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE
1293 CV_TRACE_FUNCTION();
1294 CV_TRACE_ARG_VALUE(name, "name", name.c_str());
1296 if (inputs_arr.depth() == CV_16S)
1298 forward_fallback(inputs_arr, outputs_arr, internals_arr);
1302 std::vector<Mat> inputs, outputs;
1303 inputs_arr.getMatVector(inputs);
1304 outputs_arr.getMatVector(outputs);
1306 Mat& inp = inputs[0];
1307 Mat& out = outputs[0];
1308 const float* inpData = (float*)inp.data;
1309 float* outData = (float*)out.data;
1311 const int batchSize = inp.size[0];
1312 const int numChannels = inp.size[1];
1313 const int inpHeight = inp.size[2];
1314 const int inpWidth = inp.size[3];
1316 const float rheight = (outHeight > 1) ? static_cast<float>(inpHeight - 1) / (outHeight - 1) : 0.f;
1317 const float rwidth = (outWidth > 1) ? static_cast<float>(inpWidth - 1) / (outWidth - 1) : 0.f;
1318 for (int h2 = 0; h2 < outHeight; ++h2)
1320 const float h1r = rheight * h2;
1322 const int h1p = (h1 < inpHeight - 1) ? 1 : 0;
1323 const float h1lambda = h1r - h1;
1324 const float h0lambda = 1.f - h1lambda;
1325 for (int w2 = 0; w2 < outWidth; ++w2)
1327 const float w1r = rwidth * w2;
1329 const int w1p = (w1 < inpWidth - 1) ? 1 : 0;
1330 const float w1lambda = w1r - w1;
1331 const float w0lambda = 1.f - w1lambda;
1332 const float* pos1 = inpData + h1 * inpWidth + w1;
1333 float* pos2 = outData + h2 * outWidth + w2;
1334 for (int c = 0; c < batchSize * numChannels; ++c)
1337 h0lambda * (w0lambda * pos1[0] + w1lambda * pos1[w1p]) +
1338 h1lambda * (w0lambda * pos1[h1p * inpWidth] + w1lambda * pos1[h1p * inpWidth + w1p]);
1339 pos1 += inpWidth * inpHeight;
1340 pos2 += outWidth * outHeight;
1347 int outWidth, outHeight, zoomFactor;
1350 #ifndef OPENCV_DNN_EXTERNAL_PROTOBUF
1351 TEST_P(Test_Caffe_layers, Interp)
1353 TEST_P(Test_Caffe_layers, DISABLED_Interp) // requires patched protobuf (available in OpenCV source tree only)
1356 if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)
1357 applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD);
1359 // Test a custom layer.
1360 CV_DNN_REGISTER_LAYER_CLASS(Interp, CustomInterpLayer);
1363 testLayerUsingCaffeModels("layer_interp", false, false);
1367 LayerFactory::unregisterLayer("Interp");
1370 LayerFactory::unregisterLayer("Interp");
1372 // Test an implemented layer.
1373 testLayerUsingCaffeModels("layer_interp", false, false);
1376 INSTANTIATE_TEST_CASE_P(/*nothing*/, Test_Caffe_layers, dnnBackendsAndTargets());
1378 TEST(Layer_Test_PoolingIndices, Accuracy)
1383 lp.set("pool", "max");
1384 lp.set("kernel_w", 2);
1385 lp.set("kernel_h", 2);
1386 lp.set("stride_w", 2);
1387 lp.set("stride_h", 2);
1390 lp.name = "testLayer.name"; // This test also checks that OpenCV lets use names with dots.
1391 lp.type = "Pooling";
1392 net.addLayerToPrev(lp.name, lp.type, lp);
1394 Mat inp(10, 10, CV_8U);
1397 Mat maxValues(5, 5, CV_32F, Scalar(-1)), indices(5, 5, CV_32F, Scalar(-1));
1398 for (int y = 0; y < 10; ++y)
1401 for (int x = 0; x < 10; ++x)
1404 uint8_t val = inp.at<uint8_t>(y, x);
1405 if ((float)inp.at<uint8_t>(y, x) > maxValues.at<float>(dstY, dstX))
1407 maxValues.at<float>(dstY, dstX) = val;
1408 indices.at<float>(dstY, dstX) = y * 10 + x;
1412 net.setPreferableBackend(DNN_BACKEND_OPENCV);
1413 net.setInput(blobFromImage(inp));
1415 std::vector<Mat> outputs;
1416 net.forward(outputs, lp.name);
1417 normAssert(maxValues, outputs[0].reshape(1, 5));
1418 normAssert(indices, outputs[1].reshape(1, 5));
1421 typedef testing::TestWithParam<tuple<Vec4i, int, tuple<Backend, Target> > > Layer_Test_ShuffleChannel;
1422 TEST_P(Layer_Test_ShuffleChannel, Accuracy)
1424 Vec4i inpShapeVec = get<0>(GetParam());
1425 int group = get<1>(GetParam());
1426 ASSERT_EQ(inpShapeVec[1] % group, 0);
1427 const int groupSize = inpShapeVec[1] / group;
1428 int backendId = get<0>(get<2>(GetParam()));
1429 int targetId = get<1>(get<2>(GetParam()));
1433 lp.set("group", group);
1434 lp.type = "ShuffleChannel";
1435 lp.name = "testLayer";
1436 net.addLayerToPrev(lp.name, lp.type, lp);
1438 const int inpShape[] = {inpShapeVec[0], inpShapeVec[1], inpShapeVec[2], inpShapeVec[3]};
1439 Mat inp(4, inpShape, CV_32F);
1443 net.setPreferableBackend(backendId);
1444 net.setPreferableTarget(targetId);
1445 Mat out = net.forward();
1447 double l1 = (targetId == DNN_TARGET_OPENCL_FP16) ? 5e-2 : 1e-5;
1448 double lInf = (targetId == DNN_TARGET_OPENCL_FP16) ? 7e-2 : 1e-4;
1449 for (int n = 0; n < inpShapeVec[0]; ++n)
1451 for (int c = 0; c < inpShapeVec[1]; ++c)
1453 Mat outChannel = getPlane(out, n, c);
1454 Mat inpChannel = getPlane(inp, n, groupSize * (c % group) + c / group);
1455 normAssert(outChannel, inpChannel, "", l1, lInf);
1459 INSTANTIATE_TEST_CASE_P(/**/, Layer_Test_ShuffleChannel, Combine(
1460 /*input shape*/ Values(Vec4i(1, 6, 5, 7), Vec4i(3, 12, 1, 4)),
1461 /*group*/ Values(1, 2, 3, 6), dnnBackendsAndTargets(/*with IE*/ false)
1464 // Check if relu is not fused to convolution if we requested it's output
1465 TEST(Layer_Test_Convolution, relu_fusion)
1470 lp.set("kernel_size", 1);
1471 lp.set("num_output", 1);
1472 lp.set("bias_term", false);
1473 lp.type = "Convolution";
1474 lp.name = "testConv";
1476 int weightsShape[] = {1, 1, 1, 1};
1477 Mat weights(4, &weightsShape[0], CV_32F, Scalar(1));
1478 lp.blobs.push_back(weights);
1479 net.addLayerToPrev(lp.name, lp.type, lp);
1484 lp.name = "testReLU";
1485 net.addLayerToPrev(lp.name, lp.type, lp);
1487 int sz[] = {1, 1, 2, 3};
1488 Mat input(4, &sz[0], CV_32F);
1489 randu(input, -1.0, -0.1);
1490 net.setInput(input);
1491 net.setPreferableBackend(DNN_BACKEND_OPENCV);
1492 Mat output = net.forward("testConv");
1493 normAssert(input, output);