dnn(test): replace SkipTestException with tags
[platform/upstream/opencv.git] / modules / dnn / test / test_caffe_importer.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
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.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
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.
25 //
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.
28 //
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.
39 //
40 //M*/
41
42 #include "test_precomp.hpp"
43 #include "npy_blob.hpp"
44 #include <opencv2/dnn/shape_utils.hpp>
45
46 namespace opencv_test { namespace {
47
48 template<typename TString>
49 static std::string _tf(TString filename)
50 {
51     return findDataFile(std::string("dnn/") + filename);
52 }
53
54 class Test_Caffe_nets : public DNNTestLayer
55 {
56 public:
57     void testFaster(const std::string& proto, const std::string& model, const Mat& ref,
58                     double scoreDiff = 0.0, double iouDiff = 0.0)
59     {
60         checkBackend();
61         Net net = readNetFromCaffe(findDataFile("dnn/" + proto),
62                                    findDataFile("dnn/" + model, false));
63         net.setPreferableBackend(backend);
64         net.setPreferableTarget(target);
65         Mat img = imread(findDataFile("dnn/dog416.png"));
66         resize(img, img, Size(800, 600));
67         Mat blob = blobFromImage(img, 1.0, Size(), Scalar(102.9801, 115.9465, 122.7717), false, false);
68         Mat imInfo = (Mat_<float>(1, 3) << img.rows, img.cols, 1.6f);
69
70         net.setInput(blob, "data");
71         net.setInput(imInfo, "im_info");
72         // Output has shape 1x1xNx7 where N - number of detections.
73         // An every detection is a vector of values [id, classId, confidence, left, top, right, bottom]
74         Mat out = net.forward();
75         scoreDiff = scoreDiff ? scoreDiff : default_l1;
76         iouDiff = iouDiff ? iouDiff : default_lInf;
77         normAssertDetections(ref, out, ("model name: " + model).c_str(), 0.8, scoreDiff, iouDiff);
78     }
79 };
80
81 TEST(Test_Caffe, memory_read)
82 {
83     const string proto = findDataFile("dnn/bvlc_googlenet.prototxt");
84     const string model = findDataFile("dnn/bvlc_googlenet.caffemodel", false);
85
86     std::vector<char> dataProto;
87     readFileContent(proto, dataProto);
88
89     std::vector<char> dataModel;
90     readFileContent(model, dataModel);
91
92     Net net = readNetFromCaffe(dataProto.data(), dataProto.size());
93     net.setPreferableBackend(DNN_BACKEND_OPENCV);
94     ASSERT_FALSE(net.empty());
95
96     Net net2 = readNetFromCaffe(dataProto.data(), dataProto.size(),
97                                 dataModel.data(), dataModel.size());
98     ASSERT_FALSE(net2.empty());
99 }
100
101 TEST(Test_Caffe, read_gtsrb)
102 {
103     Net net = readNetFromCaffe(_tf("gtsrb.prototxt"));
104     ASSERT_FALSE(net.empty());
105 }
106
107 TEST(Test_Caffe, read_googlenet)
108 {
109     Net net = readNetFromCaffe(_tf("bvlc_googlenet.prototxt"));
110     ASSERT_FALSE(net.empty());
111 }
112
113 TEST_P(Test_Caffe_nets, Axpy)
114 {
115     if (backend == DNN_BACKEND_INFERENCE_ENGINE)
116         applyTestTag(CV_TEST_TAG_DNN_SKIP_IE);
117
118     String proto = _tf("axpy.prototxt");
119     Net net = readNetFromCaffe(proto);
120
121     checkBackend();
122     net.setPreferableBackend(backend);
123     net.setPreferableTarget(target);
124
125     int size[] = {1, 2, 3, 4};
126     int scale_size[] = {1, 2, 1, 1};
127     Mat scale(4, &scale_size[0], CV_32F);
128     Mat shift(4, &size[0], CV_32F);
129     Mat inp(4, &size[0], CV_32F);
130     randu(scale, -1.0f, 1.0f);
131     randu(shift, -1.0f, 1.0f);
132     randu(inp, -1.0f, 1.0f);
133
134     net.setInput(scale, "scale");
135     net.setInput(shift, "shift");
136     net.setInput(inp, "data");
137
138     Mat out = net.forward();
139
140     Mat ref(4, &size[0], inp.type());
141     for (int i = 0; i < inp.size[1]; i++) {
142         for (int h = 0; h < inp.size[2]; h++) {
143             for (int w = 0; w < inp.size[3]; w++) {
144                 int idx[] = {0, i, h, w};
145                 int scale_idx[] = {0, i, 0, 0};
146                 ref.at<float>(idx) = inp.at<float>(idx) * scale.at<float>(scale_idx) +
147                                      shift.at<float>(idx);
148             }
149         }
150     }
151     float l1 = (target == DNN_TARGET_OPENCL_FP16) ? 2e-4 : 1e-5;
152     float lInf = (target == DNN_TARGET_OPENCL_FP16) ? 1e-3 : 1e-4;
153     normAssert(ref, out, "", l1, lInf);
154 }
155
156 typedef testing::TestWithParam<tuple<bool, Target> > Reproducibility_AlexNet;
157 TEST_P(Reproducibility_AlexNet, Accuracy)
158 {
159     Target targetId = get<1>(GetParam());
160     applyTestTag(targetId == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_512MB : CV_TEST_TAG_MEMORY_1GB);
161     ASSERT_TRUE(ocl::useOpenCL() || targetId == DNN_TARGET_CPU);
162
163     bool readFromMemory = get<0>(GetParam());
164     Net net;
165     {
166         const string proto = findDataFile("dnn/bvlc_alexnet.prototxt");
167         const string model = findDataFile("dnn/bvlc_alexnet.caffemodel", false);
168         if (readFromMemory)
169         {
170             std::vector<char> dataProto;
171             readFileContent(proto, dataProto);
172             std::vector<char> dataModel;
173             readFileContent(model, dataModel);
174
175             net = readNetFromCaffe(dataProto.data(), dataProto.size(),
176                                    dataModel.data(), dataModel.size());
177         }
178         else
179             net = readNetFromCaffe(proto, model);
180         ASSERT_FALSE(net.empty());
181     }
182
183     const float l1 = 1e-5;
184     const float lInf = (targetId == DNN_TARGET_OPENCL_FP16) ? 3e-3 : 1e-4;
185
186     net.setPreferableBackend(DNN_BACKEND_OPENCV);
187     net.setPreferableTarget(targetId);
188
189     Mat sample = imread(_tf("grace_hopper_227.png"));
190     ASSERT_TRUE(!sample.empty());
191
192     net.setInput(blobFromImage(sample, 1.0f, Size(227, 227), Scalar(), false), "data");
193     Mat out = net.forward("prob");
194     Mat ref = blobFromNPY(_tf("caffe_alexnet_prob.npy"));
195     normAssert(ref, out, "", l1, lInf);
196 }
197
198 INSTANTIATE_TEST_CASE_P(/**/, Reproducibility_AlexNet, Combine(testing::Bool(),
199                         testing::ValuesIn(getAvailableTargets(DNN_BACKEND_OPENCV))));
200
201 TEST(Reproducibility_FCN, Accuracy)
202 {
203     applyTestTag(CV_TEST_TAG_LONG, CV_TEST_TAG_DEBUG_VERYLONG, CV_TEST_TAG_MEMORY_2GB);
204
205     Net net;
206     {
207         const string proto = findDataFile("dnn/fcn8s-heavy-pascal.prototxt");
208         const string model = findDataFile("dnn/fcn8s-heavy-pascal.caffemodel");
209         net = readNetFromCaffe(proto, model);
210         ASSERT_FALSE(net.empty());
211     }
212     net.setPreferableBackend(DNN_BACKEND_OPENCV);
213
214     Mat sample = imread(_tf("street.png"));
215     ASSERT_TRUE(!sample.empty());
216
217     std::vector<int> layerIds;
218     std::vector<size_t> weights, blobs;
219     net.getMemoryConsumption(shape(1,3,227,227), layerIds, weights, blobs);
220
221     net.setInput(blobFromImage(sample, 1.0f, Size(500, 500), Scalar(), false), "data");
222     Mat out = net.forward("score");
223
224     Mat refData = imread(_tf("caffe_fcn8s_prob.png"), IMREAD_ANYDEPTH);
225     int shape[] = {1, 21, 500, 500};
226     Mat ref(4, shape, CV_32FC1, refData.data);
227
228     normAssert(ref, out);
229 }
230
231 TEST(Reproducibility_SSD, Accuracy)
232 {
233     applyTestTag(CV_TEST_TAG_MEMORY_512MB, CV_TEST_TAG_DEBUG_LONG);
234     Net net;
235     {
236         const string proto = findDataFile("dnn/ssd_vgg16.prototxt");
237         const string model = findDataFile("dnn/VGG_ILSVRC2016_SSD_300x300_iter_440000.caffemodel", false);
238         net = readNetFromCaffe(proto, model);
239         ASSERT_FALSE(net.empty());
240     }
241     net.setPreferableBackend(DNN_BACKEND_OPENCV);
242
243     Mat sample = imread(_tf("street.png"));
244     ASSERT_TRUE(!sample.empty());
245
246     if (sample.channels() == 4)
247         cvtColor(sample, sample, COLOR_BGRA2BGR);
248
249     Mat in_blob = blobFromImage(sample, 1.0f, Size(300, 300), Scalar(), false);
250     net.setInput(in_blob, "data");
251     Mat out = net.forward("detection_out");
252
253     Mat ref = blobFromNPY(_tf("ssd_out.npy"));
254     normAssertDetections(ref, out, "", FLT_MIN);
255 }
256
257 typedef testing::TestWithParam<tuple<Backend, Target> > Reproducibility_MobileNet_SSD;
258 TEST_P(Reproducibility_MobileNet_SSD, Accuracy)
259 {
260     const string proto = findDataFile("dnn/MobileNetSSD_deploy.prototxt", false);
261     const string model = findDataFile("dnn/MobileNetSSD_deploy.caffemodel", false);
262     Net net = readNetFromCaffe(proto, model);
263     int backendId = get<0>(GetParam());
264     int targetId = get<1>(GetParam());
265
266     net.setPreferableBackend(backendId);
267     net.setPreferableTarget(targetId);
268
269     Mat sample = imread(_tf("street.png"));
270
271     Mat inp = blobFromImage(sample, 1.0f / 127.5, Size(300, 300), Scalar(127.5, 127.5, 127.5), false);
272     net.setInput(inp);
273     Mat out = net.forward().clone();
274
275     ASSERT_EQ(out.size[2], 100);
276
277     const float scores_diff = (targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) ? 1.5e-2 : 1e-5;
278     const float boxes_iou_diff = (targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD) ? 6.3e-2 : 1e-4;
279     Mat ref = blobFromNPY(_tf("mobilenet_ssd_caffe_out.npy"));
280     normAssertDetections(ref, out, "", FLT_MIN, scores_diff, boxes_iou_diff);
281
282     // Check that detections aren't preserved.
283     inp.setTo(0.0f);
284     net.setInput(inp);
285     Mat zerosOut = net.forward();
286     zerosOut = zerosOut.reshape(1, zerosOut.total() / 7);
287
288     const int numDetections = zerosOut.rows;
289     ASSERT_NE(numDetections, 0);
290     for (int i = 0; i < numDetections; ++i)
291     {
292         float confidence = zerosOut.ptr<float>(i)[2];
293         ASSERT_EQ(confidence, 0);
294     }
295
296     // There is something wrong with Reshape layer in Myriad plugin and
297     // regression with DLIE/OCL_FP16 target.
298     if (backendId == DNN_BACKEND_INFERENCE_ENGINE)
299     {
300         if ((targetId == DNN_TARGET_MYRIAD && getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_2) ||
301             targetId == DNN_TARGET_OPENCL_FP16)
302             return;
303     }
304
305     // Check batching mode.
306     inp = blobFromImages(std::vector<Mat>(2, sample), 1.0f / 127.5, Size(300, 300), Scalar(127.5, 127.5, 127.5), false);
307     net.setInput(inp);
308     Mat outBatch = net.forward();
309
310     // Output blob has a shape 1x1x2Nx7 where N is a number of detection for
311     // a single sample in batch. The first numbers of detection vectors are batch id.
312     // For Inference Engine backend there is -1 delimiter which points the end of detections.
313     const int numRealDetections = ref.size[2];
314     EXPECT_EQ(outBatch.size[2], 2 * numDetections);
315     out = out.reshape(1, numDetections).rowRange(0, numRealDetections);
316     outBatch = outBatch.reshape(1, 2 * numDetections);
317     for (int i = 0; i < 2; ++i)
318     {
319         Mat pred = outBatch.rowRange(i * numRealDetections, (i + 1) * numRealDetections);
320         EXPECT_EQ(countNonZero(pred.col(0) != i), 0);
321         normAssert(pred.colRange(1, 7), out.colRange(1, 7));
322     }
323 }
324 INSTANTIATE_TEST_CASE_P(/**/, Reproducibility_MobileNet_SSD, dnnBackendsAndTargets());
325
326 typedef testing::TestWithParam<Target> Reproducibility_ResNet50;
327 TEST_P(Reproducibility_ResNet50, Accuracy)
328 {
329     Target targetId = GetParam();
330     applyTestTag(targetId == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_512MB : CV_TEST_TAG_MEMORY_1GB);
331     ASSERT_TRUE(ocl::useOpenCL() || targetId == DNN_TARGET_CPU);
332
333     Net net = readNetFromCaffe(findDataFile("dnn/ResNet-50-deploy.prototxt"),
334                                findDataFile("dnn/ResNet-50-model.caffemodel", false));
335
336     net.setPreferableBackend(DNN_BACKEND_OPENCV);
337     net.setPreferableTarget(targetId);
338
339     float l1 = (targetId == DNN_TARGET_OPENCL_FP16) ? 3e-5 : 1e-5;
340     float lInf = (targetId == DNN_TARGET_OPENCL_FP16) ? 6e-3 : 1e-4;
341
342     Mat input = blobFromImage(imread(_tf("googlenet_0.png")), 1.0f, Size(224,224), Scalar(), false);
343     ASSERT_TRUE(!input.empty());
344
345     net.setInput(input);
346     Mat out = net.forward();
347
348     Mat ref = blobFromNPY(_tf("resnet50_prob.npy"));
349     normAssert(ref, out, "", l1, lInf);
350
351     if (targetId == DNN_TARGET_OPENCL || targetId == DNN_TARGET_OPENCL_FP16)
352     {
353         UMat out_umat;
354         net.forward(out_umat);
355         normAssert(ref, out_umat, "out_umat", l1, lInf);
356
357         std::vector<UMat> out_umats;
358         net.forward(out_umats);
359         normAssert(ref, out_umats[0], "out_umat_vector", l1, lInf);
360     }
361 }
362 INSTANTIATE_TEST_CASE_P(/**/, Reproducibility_ResNet50,
363                         testing::ValuesIn(getAvailableTargets(DNN_BACKEND_OPENCV)));
364
365 typedef testing::TestWithParam<Target> Reproducibility_SqueezeNet_v1_1;
366 TEST_P(Reproducibility_SqueezeNet_v1_1, Accuracy)
367 {
368     int targetId = GetParam();
369     if(targetId == DNN_TARGET_OPENCL_FP16)
370         applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
371     Net net = readNetFromCaffe(findDataFile("dnn/squeezenet_v1.1.prototxt"),
372                                findDataFile("dnn/squeezenet_v1.1.caffemodel", false));
373     net.setPreferableBackend(DNN_BACKEND_OPENCV);
374     net.setPreferableTarget(targetId);
375
376     Mat input = blobFromImage(imread(_tf("googlenet_0.png")), 1.0f, Size(227,227), Scalar(), false, true);
377     ASSERT_TRUE(!input.empty());
378
379     Mat out;
380     if (targetId == DNN_TARGET_OPENCL)
381     {
382         // Firstly set a wrong input blob and run the model to receive a wrong output.
383         // Then set a correct input blob to check CPU->GPU synchronization is working well.
384         net.setInput(input * 2.0f);
385         out = net.forward();
386     }
387     net.setInput(input);
388     out = net.forward();
389
390     Mat ref = blobFromNPY(_tf("squeezenet_v1.1_prob.npy"));
391     normAssert(ref, out);
392 }
393 INSTANTIATE_TEST_CASE_P(/**/, Reproducibility_SqueezeNet_v1_1,
394     testing::ValuesIn(getAvailableTargets(DNN_BACKEND_OPENCV)));
395
396 TEST(Reproducibility_AlexNet_fp16, Accuracy)
397 {
398     applyTestTag(CV_TEST_TAG_MEMORY_512MB);
399     const float l1 = 1e-5;
400     const float lInf = 3e-3;
401
402     const string proto = findDataFile("dnn/bvlc_alexnet.prototxt");
403     const string model = findDataFile("dnn/bvlc_alexnet.caffemodel", false);
404
405     shrinkCaffeModel(model, "bvlc_alexnet.caffemodel_fp16");
406     Net net = readNetFromCaffe(proto, "bvlc_alexnet.caffemodel_fp16");
407     net.setPreferableBackend(DNN_BACKEND_OPENCV);
408
409     Mat sample = imread(findDataFile("dnn/grace_hopper_227.png"));
410
411     net.setInput(blobFromImage(sample, 1.0f, Size(227, 227), Scalar()));
412     Mat out = net.forward();
413     Mat ref = blobFromNPY(findDataFile("dnn/caffe_alexnet_prob.npy"));
414     normAssert(ref, out, "", l1, lInf);
415 }
416
417 TEST(Reproducibility_GoogLeNet_fp16, Accuracy)
418 {
419     const float l1 = 1e-5;
420     const float lInf = 3e-3;
421
422     const string proto = findDataFile("dnn/bvlc_googlenet.prototxt");
423     const string model = findDataFile("dnn/bvlc_googlenet.caffemodel", false);
424
425     shrinkCaffeModel(model, "bvlc_googlenet.caffemodel_fp16");
426     Net net = readNetFromCaffe(proto, "bvlc_googlenet.caffemodel_fp16");
427     net.setPreferableBackend(DNN_BACKEND_OPENCV);
428
429     std::vector<Mat> inpMats;
430     inpMats.push_back( imread(_tf("googlenet_0.png")) );
431     inpMats.push_back( imread(_tf("googlenet_1.png")) );
432     ASSERT_TRUE(!inpMats[0].empty() && !inpMats[1].empty());
433
434     net.setInput(blobFromImages(inpMats, 1.0f, Size(), Scalar(), false), "data");
435     Mat out = net.forward("prob");
436
437     Mat ref = blobFromNPY(_tf("googlenet_prob.npy"));
438     normAssert(out, ref, "", l1, lInf);
439 }
440
441 // https://github.com/richzhang/colorization
442 TEST_P(Test_Caffe_nets, Colorization)
443 {
444     applyTestTag(target == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_512MB : CV_TEST_TAG_MEMORY_1GB);
445     checkBackend();
446
447     Mat inp = blobFromNPY(_tf("colorization_inp.npy"));
448     Mat ref = blobFromNPY(_tf("colorization_out.npy"));
449     Mat kernel = blobFromNPY(_tf("colorization_pts_in_hull.npy"));
450
451     const string proto = findDataFile("dnn/colorization_deploy_v2.prototxt", false);
452     const string model = findDataFile("dnn/colorization_release_v2.caffemodel", false);
453     Net net = readNetFromCaffe(proto, model);
454     net.setPreferableBackend(backend);
455     net.setPreferableTarget(target);
456
457     net.getLayer(net.getLayerId("class8_ab"))->blobs.push_back(kernel);
458     net.getLayer(net.getLayerId("conv8_313_rh"))->blobs.push_back(Mat(1, 313, CV_32F, 2.606));
459
460     net.setInput(inp);
461     Mat out = net.forward();
462
463     // Reference output values are in range [-29.1, 69.5]
464     double l1 = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.25 : 4e-4;
465     double lInf = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 5.3 : 3e-3;
466     if (target == DNN_TARGET_MYRIAD && getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
467     {
468         l1 = 0.6; lInf = 15;
469     }
470     normAssert(out, ref, "", l1, lInf);
471     expectNoFallbacksFromIE(net);
472 }
473
474 TEST_P(Test_Caffe_nets, DenseNet_121)
475 {
476     applyTestTag(CV_TEST_TAG_MEMORY_512MB);
477     checkBackend();
478     const string proto = findDataFile("dnn/DenseNet_121.prototxt", false);
479     const string model = findDataFile("dnn/DenseNet_121.caffemodel", false);
480
481     Mat inp = imread(_tf("dog416.png"));
482     inp = blobFromImage(inp, 1.0 / 255, Size(224, 224), Scalar(), true, true);
483     Mat ref = blobFromNPY(_tf("densenet_121_output.npy"));
484
485     Net net = readNetFromCaffe(proto, model);
486     net.setPreferableBackend(backend);
487     net.setPreferableTarget(target);
488
489     net.setInput(inp);
490     Mat out = net.forward();
491
492     // Reference is an array of 1000 values from a range [-6.16, 7.9]
493     float l1 = default_l1, lInf = default_lInf;
494     if (target == DNN_TARGET_OPENCL_FP16)
495     {
496         l1 = 0.017; lInf = 0.0795;
497     }
498     else if (target == DNN_TARGET_MYRIAD)
499     {
500         l1 = 0.11; lInf = 0.5;
501     }
502     normAssert(out, ref, "", l1, lInf);
503     expectNoFallbacksFromIE(net);
504 }
505
506 TEST(Test_Caffe, multiple_inputs)
507 {
508     const string proto = findDataFile("dnn/layers/net_input.prototxt");
509     Net net = readNetFromCaffe(proto);
510     net.setPreferableBackend(DNN_BACKEND_OPENCV);
511
512     Mat first_image(10, 11, CV_32FC3);
513     Mat second_image(10, 11, CV_32FC3);
514     randu(first_image, -1, 1);
515     randu(second_image, -1, 1);
516
517     first_image = blobFromImage(first_image);
518     second_image = blobFromImage(second_image);
519
520     Mat first_image_blue_green = slice(first_image, Range::all(), Range(0, 2), Range::all(), Range::all());
521     Mat first_image_red = slice(first_image, Range::all(), Range(2, 3), Range::all(), Range::all());
522     Mat second_image_blue_green = slice(second_image, Range::all(), Range(0, 2), Range::all(), Range::all());
523     Mat second_image_red = slice(second_image, Range::all(), Range(2, 3), Range::all(), Range::all());
524
525     net.setInput(first_image_blue_green, "old_style_input_blue_green");
526     net.setInput(first_image_red, "different_name_for_red");
527     net.setInput(second_image_blue_green, "input_layer_blue_green");
528     net.setInput(second_image_red, "old_style_input_red");
529     Mat out = net.forward();
530
531     normAssert(out, first_image + second_image);
532 }
533
534 TEST(Test_Caffe, shared_weights)
535 {
536   const string proto = findDataFile("dnn/layers/shared_weights.prototxt");
537   const string model = findDataFile("dnn/layers/shared_weights.caffemodel");
538
539   Net net = readNetFromCaffe(proto, model);
540
541   Mat input_1 = (Mat_<float>(2, 2) << 0., 2., 4., 6.);
542   Mat input_2 = (Mat_<float>(2, 2) << 1., 3., 5., 7.);
543
544   Mat blob_1 = blobFromImage(input_1);
545   Mat blob_2 = blobFromImage(input_2);
546
547   net.setInput(blob_1, "input_1");
548   net.setInput(blob_2, "input_2");
549   net.setPreferableBackend(DNN_BACKEND_OPENCV);
550
551   Mat sum = net.forward();
552
553   EXPECT_EQ(sum.at<float>(0,0), 12.);
554   EXPECT_EQ(sum.at<float>(0,1), 16.);
555 }
556
557 typedef testing::TestWithParam<tuple<std::string, Target> > opencv_face_detector;
558 TEST_P(opencv_face_detector, Accuracy)
559 {
560     std::string proto = findDataFile("dnn/opencv_face_detector.prototxt", false);
561     std::string model = findDataFile(get<0>(GetParam()), false);
562     dnn::Target targetId = (dnn::Target)(int)get<1>(GetParam());
563
564     Net net = readNetFromCaffe(proto, model);
565     Mat img = imread(findDataFile("gpu/lbpcascade/er.png"));
566     Mat blob = blobFromImage(img, 1.0, Size(), Scalar(104.0, 177.0, 123.0), false, false);
567
568     net.setPreferableBackend(DNN_BACKEND_OPENCV);
569     net.setPreferableTarget(targetId);
570
571     net.setInput(blob);
572     // Output has shape 1x1xNx7 where N - number of detections.
573     // An every detection is a vector of values [id, classId, confidence, left, top, right, bottom]
574     Mat out = net.forward();
575     Mat ref = (Mat_<float>(6, 7) << 0, 1, 0.99520785, 0.80997437, 0.16379407, 0.87996572, 0.26685631,
576                                     0, 1, 0.9934696, 0.2831718, 0.50738752, 0.345781, 0.5985168,
577                                     0, 1, 0.99096733, 0.13629119, 0.24892329, 0.19756334, 0.3310290,
578                                     0, 1, 0.98977017, 0.23901358, 0.09084064, 0.29902688, 0.1769477,
579                                     0, 1, 0.97203469, 0.67965847, 0.06876482, 0.73999709, 0.1513494,
580                                     0, 1, 0.95097077, 0.51901293, 0.45863652, 0.5777427, 0.5347801);
581     normAssertDetections(ref, out, "", 0.5, 1e-5, 2e-4);
582 }
583 INSTANTIATE_TEST_CASE_P(Test_Caffe, opencv_face_detector,
584     Combine(
585         Values("dnn/opencv_face_detector.caffemodel",
586                "dnn/opencv_face_detector_fp16.caffemodel"),
587         Values(DNN_TARGET_CPU, DNN_TARGET_OPENCL)
588     )
589 );
590
591 TEST_P(Test_Caffe_nets, FasterRCNN_vgg16)
592 {
593     applyTestTag(
594         (target == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_1GB : CV_TEST_TAG_MEMORY_2GB),
595         CV_TEST_TAG_LONG,
596         CV_TEST_TAG_DEBUG_VERYLONG
597     );
598
599 #if defined(INF_ENGINE_RELEASE)
600     if (backend == DNN_BACKEND_INFERENCE_ENGINE && (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16))
601         applyTestTag(target == DNN_TARGET_OPENCL ? CV_TEST_TAG_DNN_SKIP_IE_OPENCL : CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16);
602
603     if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)
604         applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD);
605 #endif
606
607     static Mat ref = (Mat_<float>(3, 7) << 0, 2, 0.949398, 99.2454, 210.141, 601.205, 462.849,
608                                            0, 7, 0.997022, 481.841, 92.3218, 722.685, 175.953,
609                                            0, 12, 0.993028, 133.221, 189.377, 350.994, 563.166);
610     testFaster("faster_rcnn_vgg16.prototxt", "VGG16_faster_rcnn_final.caffemodel", ref);
611 }
612
613 TEST_P(Test_Caffe_nets, FasterRCNN_zf)
614 {
615     applyTestTag(
616         (target == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_512MB : CV_TEST_TAG_MEMORY_1GB),
617         CV_TEST_TAG_DEBUG_LONG
618     );
619     if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_OPENCL_FP16)
620         applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16);
621     if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)
622         applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD);
623     static Mat ref = (Mat_<float>(3, 7) << 0, 2, 0.90121, 120.407, 115.83, 570.586, 528.395,
624                                            0, 7, 0.988779, 469.849, 75.1756, 718.64, 186.762,
625                                            0, 12, 0.967198, 138.588, 206.843, 329.766, 553.176);
626     testFaster("faster_rcnn_zf.prototxt", "ZF_faster_rcnn_final.caffemodel", ref);
627 }
628
629 TEST_P(Test_Caffe_nets, RFCN)
630 {
631     applyTestTag(
632         (target == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_512MB : CV_TEST_TAG_MEMORY_2GB),
633         CV_TEST_TAG_LONG,
634         CV_TEST_TAG_DEBUG_VERYLONG
635     );
636     if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_OPENCL_FP16)
637         applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16);
638     if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)
639         applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD);
640     double scoreDiff = (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16) ? 4e-3 : default_l1;
641     double iouDiff = (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16) ? 8e-2 : default_lInf;
642     static Mat ref = (Mat_<float>(2, 7) << 0, 7, 0.991359, 491.822, 81.1668, 702.573, 178.234,
643                                            0, 12, 0.94786, 132.093, 223.903, 338.077, 566.16);
644     testFaster("rfcn_pascal_voc_resnet50.prototxt", "resnet50_rfcn_final.caffemodel", ref, scoreDiff, iouDiff);
645 }
646
647 INSTANTIATE_TEST_CASE_P(/**/, Test_Caffe_nets, dnnBackendsAndTargets());
648
649 }} // namespace