3e0ab75beef8e1dd0f3d33890f464fc72221f794
[platform/upstream/opencv.git] / modules / dnn / test / test_torch_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 #include <opencv2/dnn/layer.details.hpp>  // CV_DNN_REGISTER_LAYER_CLASS
46
47 namespace opencv_test
48 {
49
50 using namespace std;
51 using namespace testing;
52 using namespace cv;
53 using namespace cv::dnn;
54
55 template<typename TStr>
56 static std::string _tf(TStr filename, bool inTorchDir = true, bool required = true)
57 {
58     String path = "dnn/";
59     if (inTorchDir)
60         path += "torch/";
61     path += filename;
62     return findDataFile(path, required);
63 }
64
65 TEST(Torch_Importer, simple_read)
66 {
67     Net net;
68     ASSERT_NO_THROW(net = readNetFromTorch(_tf("net_simple_net.txt"), false));
69     ASSERT_FALSE(net.empty());
70 }
71
72 class Test_Torch_layers : public DNNTestLayer
73 {
74 public:
75     void runTorchNet(const String& prefix, String outLayerName = "",
76                      bool check2ndBlob = false, bool isBinary = false, bool evaluate = true,
77                      double l1 = 0.0, double lInf = 0.0)
78     {
79         String suffix = (isBinary) ? ".dat" : ".txt";
80
81         Mat inp, outRef;
82         ASSERT_NO_THROW( inp = readTorchBlob(_tf(prefix + "_input" + suffix), isBinary) );
83         ASSERT_NO_THROW( outRef = readTorchBlob(_tf(prefix + "_output" + suffix), isBinary) );
84
85         checkBackend(backend, target, &inp, &outRef);
86
87         Net net = readNetFromTorch(_tf(prefix + "_net" + suffix), isBinary, evaluate);
88         ASSERT_FALSE(net.empty());
89
90         net.setPreferableBackend(backend);
91         net.setPreferableTarget(target);
92
93         if (outLayerName.empty())
94             outLayerName = net.getLayerNames().back();
95
96         net.setInput(inp);
97         std::vector<Mat> outBlobs;
98         net.forward(outBlobs, outLayerName);
99         l1 = l1 ? l1 : default_l1;
100         lInf = lInf ? lInf : default_lInf;
101         normAssert(outRef, outBlobs[0], "", l1, lInf);
102
103         if (check2ndBlob && backend != DNN_BACKEND_INFERENCE_ENGINE)
104         {
105             Mat out2 = outBlobs[1];
106             Mat ref2 = readTorchBlob(_tf(prefix + "_output_2" + suffix), isBinary);
107             normAssert(out2, ref2, "", l1, lInf);
108         }
109     }
110 };
111
112 TEST_P(Test_Torch_layers, run_convolution)
113 {
114     // Output reference values are in range [23.4018, 72.0181]
115     double l1 = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.08 : default_l1;
116     double lInf = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.42 : default_lInf;
117     runTorchNet("net_conv", "", false, true, true, l1, lInf);
118 }
119
120 TEST_P(Test_Torch_layers, run_pool_max)
121 {
122     if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
123         applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
124     runTorchNet("net_pool_max", "", true);
125 }
126
127 TEST_P(Test_Torch_layers, run_pool_ave)
128 {
129     runTorchNet("net_pool_ave");
130 }
131
132 TEST_P(Test_Torch_layers, run_reshape_change_batch_size)
133 {
134     runTorchNet("net_reshape");
135 }
136
137 TEST_P(Test_Torch_layers, run_reshape)
138 {
139     if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)
140         applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD);
141     runTorchNet("net_reshape_batch");
142     runTorchNet("net_reshape_channels", "", false, true);
143 }
144
145 TEST_P(Test_Torch_layers, run_reshape_single_sample)
146 {
147     // Reference output values in range [14.4586, 18.4492].
148     runTorchNet("net_reshape_single_sample", "", false, false, true,
149                 (target == DNN_TARGET_MYRIAD || target == DNN_TARGET_OPENCL_FP16) ? 0.033 : default_l1,
150                 (target == DNN_TARGET_MYRIAD || target == DNN_TARGET_OPENCL_FP16) ? 0.05 : default_lInf);
151 }
152
153 TEST_P(Test_Torch_layers, run_linear)
154 {
155     if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
156         applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
157     runTorchNet("net_linear_2d");
158 }
159
160 TEST_P(Test_Torch_layers, run_concat)
161 {
162     runTorchNet("net_concat", "l5_torchMerge");
163 }
164
165 TEST_P(Test_Torch_layers, run_depth_concat)
166 {
167     runTorchNet("net_depth_concat", "", false, true, true, 0.0,
168                 target == DNN_TARGET_OPENCL_FP16 ? 0.021 : 0.0);
169 }
170
171 TEST_P(Test_Torch_layers, run_deconv)
172 {
173     runTorchNet("net_deconv");
174 }
175
176 TEST_P(Test_Torch_layers, run_batch_norm)
177 {
178     runTorchNet("net_batch_norm", "", false, true);
179     runTorchNet("net_batch_norm_train", "", false, true, false);
180 }
181
182 TEST_P(Test_Torch_layers, net_prelu)
183 {
184     runTorchNet("net_prelu");
185 }
186
187 TEST_P(Test_Torch_layers, net_cadd_table)
188 {
189     runTorchNet("net_cadd_table");
190 }
191
192 TEST_P(Test_Torch_layers, net_softmax)
193 {
194     runTorchNet("net_softmax");
195     runTorchNet("net_softmax_spatial");
196 }
197
198 TEST_P(Test_Torch_layers, net_logsoftmax)
199 {
200     runTorchNet("net_logsoftmax");
201     runTorchNet("net_logsoftmax_spatial");
202 }
203
204 TEST_P(Test_Torch_layers, net_lp_pooling)
205 {
206     runTorchNet("net_lp_pooling_square", "", false, true);
207     runTorchNet("net_lp_pooling_power", "", false, true);
208 }
209
210 TEST_P(Test_Torch_layers, net_conv_gemm_lrn)
211 {
212     if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)
213         applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD);
214     runTorchNet("net_conv_gemm_lrn", "", false, true, true,
215                 target == DNN_TARGET_OPENCL_FP16 ? 0.046 : 0.0,
216                 target == DNN_TARGET_OPENCL_FP16 ? 0.023 : 0.0);
217 }
218
219 TEST_P(Test_Torch_layers, net_inception_block)
220 {
221     runTorchNet("net_inception_block", "", false, true);
222 }
223
224 TEST_P(Test_Torch_layers, net_normalize)
225 {
226     runTorchNet("net_normalize", "", false, true);
227 }
228
229 TEST_P(Test_Torch_layers, net_padding)
230 {
231     runTorchNet("net_padding", "", false, true);
232     runTorchNet("net_spatial_zero_padding", "", false, true);
233     runTorchNet("net_spatial_reflection_padding", "", false, true);
234 }
235
236 TEST_P(Test_Torch_layers, net_non_spatial)
237 {
238     if (backend == DNN_BACKEND_INFERENCE_ENGINE &&
239         (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16))
240         applyTestTag(target == DNN_TARGET_OPENCL ? CV_TEST_TAG_DNN_SKIP_IE_OPENCL : CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16);
241     runTorchNet("net_non_spatial", "", false, true);
242 }
243
244 TEST_P(Test_Torch_layers, run_paralel)
245 {
246     if (backend != DNN_BACKEND_OPENCV || target != DNN_TARGET_CPU)
247         throw SkipTestException("");  // TODO: Check this
248     runTorchNet("net_parallel", "l5_torchMerge");
249 }
250
251 TEST_P(Test_Torch_layers, net_residual)
252 {
253 #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_RELEASE == 2018050000
254     if (backend == DNN_BACKEND_INFERENCE_ENGINE && (target == DNN_TARGET_OPENCL ||
255                                                     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);
257 #endif
258     runTorchNet("net_residual", "", false, true);
259 }
260
261 class Test_Torch_nets : public DNNTestLayer {};
262
263 TEST_P(Test_Torch_nets, OpenFace_accuracy)
264 {
265 #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2018050000)
266     if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)
267         applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD);
268 #endif
269     checkBackend();
270
271     const string model = findDataFile("dnn/openface_nn4.small2.v1.t7", false);
272     Net net = readNetFromTorch(model);
273
274     net.setPreferableBackend(backend);
275     net.setPreferableTarget(target);
276
277     Mat sample = imread(findDataFile("cv/shared/lena.png"));
278     Mat sampleF32(sample.size(), CV_32FC3);
279     sample.convertTo(sampleF32, sampleF32.type());
280     sampleF32 /= 255;
281     resize(sampleF32, sampleF32, Size(96, 96), 0, 0, INTER_NEAREST);
282
283     Mat inputBlob = blobFromImage(sampleF32, 1.0, Size(), Scalar(), /*swapRB*/true);
284
285     net.setInput(inputBlob);
286     Mat out = net.forward();
287
288     // Reference output values are in range [-0.17212, 0.263492]
289     // on Myriad problem layer: l4_Pooling - does not use pads_begin
290     float l1 = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 2e-3 : 1e-5;
291     float lInf = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 5e-3 : 1e-3;
292     Mat outRef = readTorchBlob(_tf("net_openface_output.dat"), true);
293     normAssert(out, outRef, "", l1, lInf);
294 }
295
296 static Mat getSegmMask(const Mat& scores)
297 {
298     const int rows = scores.size[2];
299     const int cols = scores.size[3];
300     const int numClasses = scores.size[1];
301
302     Mat maxCl = Mat::zeros(rows, cols, CV_8UC1);
303     Mat maxVal(rows, cols, CV_32FC1, Scalar(0));
304     for (int ch = 0; ch < numClasses; ch++)
305     {
306         for (int row = 0; row < rows; row++)
307         {
308             const float *ptrScore = scores.ptr<float>(0, ch, row);
309             uint8_t *ptrMaxCl = maxCl.ptr<uint8_t>(row);
310             float *ptrMaxVal = maxVal.ptr<float>(row);
311             for (int col = 0; col < cols; col++)
312             {
313                 if (ptrScore[col] > ptrMaxVal[col])
314                 {
315                     ptrMaxVal[col] = ptrScore[col];
316                     ptrMaxCl[col] = (uchar)ch;
317                 }
318             }
319         }
320     }
321     return maxCl;
322 }
323
324 // Computer per-class intersection over union metric.
325 static void normAssertSegmentation(const Mat& ref, const Mat& test)
326 {
327     CV_Assert_N(ref.dims == 4, test.dims == 4);
328     const int numClasses = ref.size[1];
329     CV_Assert(numClasses == test.size[1]);
330
331     Mat refMask = getSegmMask(ref);
332     Mat testMask = getSegmMask(test);
333     EXPECT_EQ(countNonZero(refMask != testMask), 0);
334 }
335
336 TEST_P(Test_Torch_nets, ENet_accuracy)
337 {
338     applyTestTag(target == DNN_TARGET_CPU ? "" : CV_TEST_TAG_MEMORY_512MB);
339     checkBackend();
340     if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
341         throw SkipTestException("");
342     if (backend == DNN_BACKEND_INFERENCE_ENGINE && target != DNN_TARGET_CPU)
343     {
344         if (target == DNN_TARGET_OPENCL_FP16) applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16);
345         if (target == DNN_TARGET_OPENCL)      applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL);
346         if (target == DNN_TARGET_MYRIAD)      applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD);
347         throw SkipTestException("");
348     }
349
350     Net net;
351     {
352         const string model = findDataFile("dnn/Enet-model-best.net", false);
353         net = readNetFromTorch(model, true);
354         ASSERT_TRUE(!net.empty());
355     }
356
357     net.setPreferableBackend(backend);
358     net.setPreferableTarget(target);
359
360     Mat sample = imread(_tf("street.png", false));
361     Mat inputBlob = blobFromImage(sample, 1./255, Size(), Scalar(), /*swapRB*/true);
362
363     net.setInput(inputBlob, "");
364     Mat out = net.forward();
365     Mat ref = blobFromNPY(_tf("torch_enet_prob.npy", false));
366     // Due to numerical instability in Pooling-Unpooling layers (indexes jittering)
367     // thresholds for ENet must be changed. Accuracy of results was checked on
368     // Cityscapes dataset and difference in mIOU with Torch is 10E-4%
369     normAssert(ref, out, "", 0.00044, /*target == DNN_TARGET_CPU ? 0.453 : */0.552);
370     normAssertSegmentation(ref, out);
371
372     const int N = 3;
373     for (int i = 0; i < N; i++)
374     {
375         net.setInput(inputBlob, "");
376         Mat out = net.forward();
377         normAssert(ref, out, "", 0.00044, /*target == DNN_TARGET_CPU ? 0.453 : */0.552);
378         normAssertSegmentation(ref, out);
379     }
380 }
381
382 // Check accuracy of style transfer models from https://github.com/jcjohnson/fast-neural-style
383 // th fast_neural_style.lua \
384 //   -input_image ~/opencv_extra/testdata/dnn/googlenet_1.png \
385 //   -output_image lena.png \
386 //   -median_filter 0 \
387 //   -image_size 0 \
388 //   -model models/eccv16/starry_night.t7
389 // th fast_neural_style.lua \
390 //   -input_image ~/opencv_extra/testdata/dnn/googlenet_1.png \
391 //   -output_image lena.png \
392 //   -median_filter 0 \
393 //   -image_size 0 \
394 //   -model models/instance_norm/feathers.t7
395 TEST_P(Test_Torch_nets, FastNeuralStyle_accuracy)
396 {
397 #if defined INF_ENGINE_RELEASE
398     if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD
399             && getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
400         applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X);
401 #endif
402
403     checkBackend();
404
405 #if defined(INF_ENGINE_RELEASE)
406 #if INF_ENGINE_RELEASE <= 2018050000
407     if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_OPENCL)
408         applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL, CV_TEST_TAG_DNN_SKIP_IE_2018R5);
409 #endif
410 #endif
411
412     std::string models[] = {"dnn/fast_neural_style_eccv16_starry_night.t7",
413                             "dnn/fast_neural_style_instance_norm_feathers.t7"};
414     std::string targets[] = {"dnn/lena_starry_night.png", "dnn/lena_feathers.png"};
415
416     for (int i = 0; i < 2; ++i)
417     {
418         const string model = findDataFile(models[i], false);
419         Net net = readNetFromTorch(model);
420
421         net.setPreferableBackend(backend);
422         net.setPreferableTarget(target);
423
424         Mat img = imread(findDataFile("dnn/googlenet_1.png"));
425         Mat inputBlob = blobFromImage(img, 1.0, Size(), Scalar(103.939, 116.779, 123.68), false);
426
427         net.setInput(inputBlob);
428         Mat out = net.forward();
429
430         // Deprocessing.
431         getPlane(out, 0, 0) += 103.939;
432         getPlane(out, 0, 1) += 116.779;
433         getPlane(out, 0, 2) += 123.68;
434         out = cv::min(cv::max(0, out), 255);
435
436         Mat ref = imread(findDataFile(targets[i]));
437         Mat refBlob = blobFromImage(ref, 1.0, Size(), Scalar(), false);
438
439         if (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD)
440         {
441             double normL1 = cvtest::norm(refBlob, out, cv::NORM_L1) / refBlob.total();
442             if (target == DNN_TARGET_MYRIAD)
443                 EXPECT_LE(normL1, 4.0f);
444             else
445                 EXPECT_LE(normL1, 0.6f);
446         }
447         else
448             normAssert(out, refBlob, "", 0.5, 1.1);
449     }
450 }
451
452 INSTANTIATE_TEST_CASE_P(/**/, Test_Torch_nets, dnnBackendsAndTargets());
453
454 // Test a custom layer
455 // https://github.com/torch/nn/blob/master/doc/convolution.md#nn.SpatialUpSamplingNearest
456 class SpatialUpSamplingNearestLayer CV_FINAL : public Layer
457 {
458 public:
459     SpatialUpSamplingNearestLayer(const LayerParams &params) : Layer(params)
460     {
461         scale = params.get<int>("scale_factor");
462     }
463
464     static Ptr<Layer> create(LayerParams& params)
465     {
466         return Ptr<Layer>(new SpatialUpSamplingNearestLayer(params));
467     }
468
469     virtual bool getMemoryShapes(const std::vector<std::vector<int> > &inputs,
470                                  const int requiredOutputs,
471                                  std::vector<std::vector<int> > &outputs,
472                                  std::vector<std::vector<int> > &internals) const CV_OVERRIDE
473     {
474         std::vector<int> outShape(4);
475         outShape[0] = inputs[0][0];  // batch size
476         outShape[1] = inputs[0][1];  // number of channels
477         outShape[2] = scale * inputs[0][2];
478         outShape[3] = scale * inputs[0][3];
479         outputs.assign(1, outShape);
480         return false;
481     }
482
483     void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays) CV_OVERRIDE
484     {
485         CV_TRACE_FUNCTION();
486         CV_TRACE_ARG_VALUE(name, "name", name.c_str());
487
488         std::vector<Mat> inputs, outputs;
489         inputs_arr.getMatVector(inputs);
490         outputs_arr.getMatVector(outputs);
491
492         Mat& inp = inputs[0];
493         Mat& out = outputs[0];
494         const int outHeight = out.size[2];
495         const int outWidth = out.size[3];
496         for (size_t n = 0; n < inp.size[0]; ++n)
497         {
498             for (size_t ch = 0; ch < inp.size[1]; ++ch)
499             {
500                 resize(getPlane(inp, n, ch), getPlane(out, n, ch),
501                        Size(outWidth, outHeight), 0, 0, INTER_NEAREST);
502             }
503         }
504     }
505
506 private:
507     int scale;
508 };
509
510 TEST_P(Test_Torch_layers, upsampling_nearest)
511 {
512     // Test a custom layer.
513     CV_DNN_REGISTER_LAYER_CLASS(SpatialUpSamplingNearest, SpatialUpSamplingNearestLayer);
514     try
515     {
516         runTorchNet("net_spatial_upsampling_nearest", "", false, true);
517     }
518     catch (...)
519     {
520         LayerFactory::unregisterLayer("SpatialUpSamplingNearest");
521         throw;
522     }
523     LayerFactory::unregisterLayer("SpatialUpSamplingNearest");
524
525     // Test an implemented layer.
526     runTorchNet("net_spatial_upsampling_nearest", "", false, true);
527 }
528
529 INSTANTIATE_TEST_CASE_P(/**/, Test_Torch_layers, dnnBackendsAndTargets());
530
531 }