Merge remote-tracking branch 'upstream/3.4' into merge-3.4
[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_OPENCV)
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 = default_l1, lInf = default_lInf;
116     if (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD)
117     {
118         l1 = 0.08;
119         lInf = 0.43;
120     }
121     else if (target == DNN_TARGET_CUDA_FP16)
122     {
123         l1 = 0.08;
124         lInf = 0.5;
125     }
126     runTorchNet("net_conv", "", false, true, true, l1, lInf);
127 }
128
129 TEST_P(Test_Torch_layers, run_pool_max)
130 {
131     if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
132         applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
133     if (target == DNN_TARGET_CUDA_FP16)
134         applyTestTag(CV_TEST_TAG_DNN_SKIP_CUDA_FP16);
135     double l1 = 0.0, lInf = 0.0;
136     runTorchNet("net_pool_max", "", true, false, true, l1, lInf);
137 }
138
139 TEST_P(Test_Torch_layers, run_pool_ave)
140 {
141     runTorchNet("net_pool_ave");
142 }
143
144 TEST_P(Test_Torch_layers, run_reshape_change_batch_size)
145 {
146     runTorchNet("net_reshape");
147 }
148
149 TEST_P(Test_Torch_layers, run_reshape)
150 {
151     if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD)
152         applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
153     runTorchNet("net_reshape_batch");
154     runTorchNet("net_reshape_channels", "", false, true);
155 }
156
157 TEST_P(Test_Torch_layers, run_reshape_single_sample)
158 {
159     // Reference output values in range [14.4586, 18.4492].
160     double l1 = default_l1, lInf = default_lInf;
161     if (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD)
162     {
163         l1 = 0.033;
164         lInf = 0.05;
165     }
166     else if (target == DNN_TARGET_CUDA_FP16)
167     {
168         l1 = 0.01;
169     }
170     runTorchNet("net_reshape_single_sample", "", false, false, true, l1, lInf);
171 }
172
173 TEST_P(Test_Torch_layers, run_linear)
174 {
175     if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
176         applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
177     runTorchNet("net_linear_2d");
178 }
179
180 TEST_P(Test_Torch_layers, run_concat)
181 {
182     runTorchNet("net_concat", "l5_torchMerge");
183 }
184
185 TEST_P(Test_Torch_layers, run_depth_concat)
186 {
187     double lInf = 0.0;
188     if (target == DNN_TARGET_OPENCL_FP16)
189     {
190         lInf = 0.032;
191     }
192     else if (target == DNN_TARGET_CUDA_FP16)
193     {
194         lInf = 0.03;
195     }
196     runTorchNet("net_depth_concat", "", false, true, true, 0.0, lInf);
197 }
198
199 TEST_P(Test_Torch_layers, run_deconv)
200 {
201     runTorchNet("net_deconv");
202 }
203
204 TEST_P(Test_Torch_layers, run_batch_norm)
205 {
206     runTorchNet("net_batch_norm", "", false, true);
207     runTorchNet("net_batch_norm_train", "", false, true, false);
208 }
209
210 TEST_P(Test_Torch_layers, net_prelu)
211 {
212     runTorchNet("net_prelu");
213 }
214
215 TEST_P(Test_Torch_layers, net_cadd_table)
216 {
217     runTorchNet("net_cadd_table");
218 }
219
220 TEST_P(Test_Torch_layers, net_softmax)
221 {
222     runTorchNet("net_softmax");
223     runTorchNet("net_softmax_spatial");
224 }
225
226 TEST_P(Test_Torch_layers, net_logsoftmax)
227 {
228     runTorchNet("net_logsoftmax");
229     runTorchNet("net_logsoftmax_spatial");
230 }
231
232 TEST_P(Test_Torch_layers, net_lp_pooling_square)
233 {
234     runTorchNet("net_lp_pooling_square", "", false, true);
235 }
236 TEST_P(Test_Torch_layers, net_lp_pooling_power)
237 {
238     if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
239         applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
240     runTorchNet("net_lp_pooling_power", "", false, true);
241 }
242
243 TEST_P(Test_Torch_layers, net_conv_gemm_lrn)
244 {
245     if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD)
246         applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
247     if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD)
248         applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
249     double l1 = 0.0, lInf = 0.0;
250     if (target == DNN_TARGET_OPENCL_FP16)
251     {
252         l1 = 0.046;
253         lInf = 0.023;
254     }
255     else if (target == DNN_TARGET_CUDA_FP16)
256     {
257         l1 = 0.0042;
258         lInf = 0.021;
259     }
260     runTorchNet("net_conv_gemm_lrn", "", false, true, true, l1, lInf);
261 }
262
263 TEST_P(Test_Torch_layers, net_inception_block)
264 {
265     runTorchNet("net_inception_block", "", false, true);
266 }
267
268 TEST_P(Test_Torch_layers, net_normalize)
269 {
270     if(backend == DNN_BACKEND_CUDA)
271         applyTestTag(CV_TEST_TAG_DNN_SKIP_CUDA); /* only L1 and L2 norms are supported */
272     runTorchNet("net_normalize", "", false, true);
273 }
274
275 TEST_P(Test_Torch_layers, net_padding)
276 {
277     runTorchNet("net_padding", "", false, true);
278     runTorchNet("net_spatial_zero_padding", "", false, true);
279     runTorchNet("net_spatial_reflection_padding", "", false, true);
280 }
281
282 TEST_P(Test_Torch_layers, net_non_spatial)
283 {
284     if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 &&
285         (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16))
286         applyTestTag(target == DNN_TARGET_OPENCL ? CV_TEST_TAG_DNN_SKIP_IE_OPENCL : CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16,
287                      CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
288     runTorchNet("net_non_spatial", "", false, true);
289 }
290
291 TEST_P(Test_Torch_layers, run_paralel)
292 {
293     if (backend != DNN_BACKEND_OPENCV || target != DNN_TARGET_CPU)
294         throw SkipTestException("");  // TODO: Check this
295     runTorchNet("net_parallel", "l5_torchMerge");
296 }
297
298 TEST_P(Test_Torch_layers, net_residual)
299 {
300 #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_RELEASE == 2018050000
301     if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && (target == DNN_TARGET_OPENCL ||
302                                                     target == DNN_TARGET_OPENCL_FP16))
303         applyTestTag(target == DNN_TARGET_OPENCL ? CV_TEST_TAG_DNN_SKIP_IE_OPENCL : CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16,
304                      CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
305 #endif
306     runTorchNet("net_residual", "", false, true);
307 }
308
309 class Test_Torch_nets : public DNNTestLayer {};
310
311 TEST_P(Test_Torch_nets, OpenFace_accuracy)
312 {
313 #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2018050000)
314     if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD)
315         applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
316 #endif
317     checkBackend();
318
319     const string model = findDataFile("dnn/openface_nn4.small2.v1.t7", false);
320     Net net = readNetFromTorch(model);
321
322     net.setPreferableBackend(backend);
323     net.setPreferableTarget(target);
324
325     Mat sample = imread(findDataFile("cv/shared/lena.png"));
326     Mat sampleF32(sample.size(), CV_32FC3);
327     sample.convertTo(sampleF32, sampleF32.type());
328     sampleF32 /= 255;
329     resize(sampleF32, sampleF32, Size(96, 96), 0, 0, INTER_NEAREST);
330
331     Mat inputBlob = blobFromImage(sampleF32, 1.0, Size(), Scalar(), /*swapRB*/true);
332
333     net.setInput(inputBlob);
334     Mat out = net.forward();
335
336     // Reference output values are in range [-0.17212, 0.263492]
337     // on Myriad problem layer: l4_Pooling - does not use pads_begin
338     float l1 = 1e-5, lInf = 1e-3;
339     if (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD)
340     {
341         l1 = 2e-3;
342         lInf = 5e-3;
343     }
344     else if (target == DNN_TARGET_CUDA_FP16)
345     {
346         l1 = 0.0004;
347         lInf = 0.0012;
348     }
349     Mat outRef = readTorchBlob(_tf("net_openface_output.dat"), true);
350     normAssert(out, outRef, "", l1, lInf);
351 }
352
353 static Mat getSegmMask(const Mat& scores)
354 {
355     const int rows = scores.size[2];
356     const int cols = scores.size[3];
357     const int numClasses = scores.size[1];
358
359     Mat maxCl = Mat::zeros(rows, cols, CV_8UC1);
360     Mat maxVal(rows, cols, CV_32FC1, Scalar(0));
361     for (int ch = 0; ch < numClasses; ch++)
362     {
363         for (int row = 0; row < rows; row++)
364         {
365             const float *ptrScore = scores.ptr<float>(0, ch, row);
366             uint8_t *ptrMaxCl = maxCl.ptr<uint8_t>(row);
367             float *ptrMaxVal = maxVal.ptr<float>(row);
368             for (int col = 0; col < cols; col++)
369             {
370                 if (ptrScore[col] > ptrMaxVal[col])
371                 {
372                     ptrMaxVal[col] = ptrScore[col];
373                     ptrMaxCl[col] = (uchar)ch;
374                 }
375             }
376         }
377     }
378     return maxCl;
379 }
380
381 // Computer per-class intersection over union metric.
382 static void normAssertSegmentation(const Mat& ref, const Mat& test)
383 {
384     CV_Assert_N(ref.dims == 4, test.dims == 4);
385     const int numClasses = ref.size[1];
386     CV_Assert(numClasses == test.size[1]);
387
388     Mat refMask = getSegmMask(ref);
389     Mat testMask = getSegmMask(test);
390     EXPECT_EQ(countNonZero(refMask != testMask), 0);
391 }
392
393 TEST_P(Test_Torch_nets, ENet_accuracy)
394 {
395     applyTestTag(target == DNN_TARGET_CPU ? "" : CV_TEST_TAG_MEMORY_512MB);
396     checkBackend();
397     if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
398         throw SkipTestException("");
399     if (backend == DNN_BACKEND_CUDA && target == DNN_TARGET_CUDA_FP16)
400         applyTestTag(CV_TEST_TAG_DNN_SKIP_CUDA_FP16);
401 #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2020010000)
402     if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
403         applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
404 #else
405     if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target != DNN_TARGET_CPU)
406     {
407         if (target == DNN_TARGET_OPENCL_FP16) applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
408         if (target == DNN_TARGET_OPENCL)      applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
409         if (target == DNN_TARGET_MYRIAD)      applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
410         throw SkipTestException("");
411     }
412 #endif
413 #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_GE(2021010000)
414     if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
415         applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
416 #endif
417     if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target != DNN_TARGET_CPU)
418     {
419         if (target == DNN_TARGET_OPENCL_FP16) applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
420         if (target == DNN_TARGET_OPENCL)      applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
421         if (target == DNN_TARGET_MYRIAD)      applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
422         throw SkipTestException("");
423     }
424
425     Net net;
426     {
427         const string model = findDataFile("dnn/Enet-model-best.net", false);
428         net = readNetFromTorch(model, true);
429         ASSERT_TRUE(!net.empty());
430     }
431
432     net.setPreferableBackend(backend);
433     net.setPreferableTarget(target);
434
435     Mat sample = imread(_tf("street.png", false));
436     Mat inputBlob = blobFromImage(sample, 1./255, Size(), Scalar(), /*swapRB*/true);
437
438     net.setInput(inputBlob, "");
439     Mat out = net.forward();
440     Mat ref = blobFromNPY(_tf("torch_enet_prob.npy", false));
441     // Due to numerical instability in Pooling-Unpooling layers (indexes jittering)
442     // thresholds for ENet must be changed. Accuracy of results was checked on
443     // Cityscapes dataset and difference in mIOU with Torch is 10E-4%
444     normAssert(ref, out, "", 0.00044, /*target == DNN_TARGET_CPU ? 0.453 : */0.552);
445     normAssertSegmentation(ref, out);
446
447     const int N = 3;
448     for (int i = 0; i < N; i++)
449     {
450         net.setInput(inputBlob, "");
451         Mat out = net.forward();
452         normAssert(ref, out, "", 0.00044, /*target == DNN_TARGET_CPU ? 0.453 : */0.552);
453         normAssertSegmentation(ref, out);
454     }
455 }
456
457 // Check accuracy of style transfer models from https://github.com/jcjohnson/fast-neural-style
458 // th fast_neural_style.lua \
459 //   -input_image ~/opencv_extra/testdata/dnn/googlenet_1.png \
460 //   -output_image lena.png \
461 //   -median_filter 0 \
462 //   -image_size 0 \
463 //   -model models/eccv16/starry_night.t7
464 // th fast_neural_style.lua \
465 //   -input_image ~/opencv_extra/testdata/dnn/googlenet_1.png \
466 //   -output_image lena.png \
467 //   -median_filter 0 \
468 //   -image_size 0 \
469 //   -model models/instance_norm/feathers.t7
470 TEST_P(Test_Torch_nets, FastNeuralStyle_accuracy)
471 {
472 #if defined INF_ENGINE_RELEASE
473     if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_MYRIAD
474             && getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
475         applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
476     if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_MYRIAD
477             && getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
478         applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
479 #endif
480
481     checkBackend();
482
483 #if defined(INF_ENGINE_RELEASE)
484 #if INF_ENGINE_RELEASE <= 2018050000
485     if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_OPENCL)
486         applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
487 #endif
488 #endif
489
490     std::string models[] = {"dnn/fast_neural_style_eccv16_starry_night.t7",
491                             "dnn/fast_neural_style_instance_norm_feathers.t7"};
492     std::string targets[] = {"dnn/lena_starry_night.png", "dnn/lena_feathers.png"};
493
494     for (int i = 0; i < 2; ++i)
495     {
496         const string model = findDataFile(models[i], false);
497         Net net = readNetFromTorch(model);
498
499         net.setPreferableBackend(backend);
500         net.setPreferableTarget(target);
501
502         Mat img = imread(findDataFile("dnn/googlenet_1.png"));
503         Mat inputBlob = blobFromImage(img, 1.0, Size(), Scalar(103.939, 116.779, 123.68), false);
504
505         net.setInput(inputBlob);
506         Mat out = net.forward();
507
508         // Deprocessing.
509         getPlane(out, 0, 0) += 103.939;
510         getPlane(out, 0, 1) += 116.779;
511         getPlane(out, 0, 2) += 123.68;
512         out = cv::min(cv::max(0, out), 255);
513
514         Mat ref = imread(findDataFile(targets[i]));
515         Mat refBlob = blobFromImage(ref, 1.0, Size(), Scalar(), false);
516
517         if (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD)
518         {
519             double normL1 = cvtest::norm(refBlob, out, cv::NORM_L1) / refBlob.total();
520             if (target == DNN_TARGET_MYRIAD)
521                 EXPECT_LE(normL1, 4.0f);
522             else
523                 EXPECT_LE(normL1, 0.6f);
524         }
525         else if(target == DNN_TARGET_CUDA_FP16)
526         {
527             normAssert(out, refBlob, "", 0.6, 25);
528         }
529         else
530             normAssert(out, refBlob, "", 0.5, 1.1);
531     }
532 }
533
534 INSTANTIATE_TEST_CASE_P(/**/, Test_Torch_nets, dnnBackendsAndTargets());
535
536 // Test a custom layer
537 // https://github.com/torch/nn/blob/master/doc/convolution.md#nn.SpatialUpSamplingNearest
538 class SpatialUpSamplingNearestLayer CV_FINAL : public Layer
539 {
540 public:
541     SpatialUpSamplingNearestLayer(const LayerParams &params) : Layer(params)
542     {
543         scale = params.get<int>("scale_factor");
544     }
545
546     static Ptr<Layer> create(LayerParams& params)
547     {
548         return Ptr<Layer>(new SpatialUpSamplingNearestLayer(params));
549     }
550
551     virtual bool getMemoryShapes(const std::vector<std::vector<int> > &inputs,
552                                  const int requiredOutputs,
553                                  std::vector<std::vector<int> > &outputs,
554                                  std::vector<std::vector<int> > &internals) const CV_OVERRIDE
555     {
556         std::vector<int> outShape(4);
557         outShape[0] = inputs[0][0];  // batch size
558         outShape[1] = inputs[0][1];  // number of channels
559         outShape[2] = scale * inputs[0][2];
560         outShape[3] = scale * inputs[0][3];
561         outputs.assign(1, outShape);
562         return false;
563     }
564
565     void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays) CV_OVERRIDE
566     {
567         CV_TRACE_FUNCTION();
568         CV_TRACE_ARG_VALUE(name, "name", name.c_str());
569
570         std::vector<Mat> inputs, outputs;
571         inputs_arr.getMatVector(inputs);
572         outputs_arr.getMatVector(outputs);
573
574         Mat& inp = inputs[0];
575         Mat& out = outputs[0];
576         const int outHeight = out.size[2];
577         const int outWidth = out.size[3];
578         for (size_t n = 0; n < inp.size[0]; ++n)
579         {
580             for (size_t ch = 0; ch < inp.size[1]; ++ch)
581             {
582                 resize(getPlane(inp, n, ch), getPlane(out, n, ch),
583                        Size(outWidth, outHeight), 0, 0, INTER_NEAREST);
584             }
585         }
586     }
587
588 private:
589     int scale;
590 };
591
592 TEST_P(Test_Torch_layers, upsampling_nearest)
593 {
594     // Test a custom layer.
595     CV_DNN_REGISTER_LAYER_CLASS(SpatialUpSamplingNearest, SpatialUpSamplingNearestLayer);
596     try
597     {
598         runTorchNet("net_spatial_upsampling_nearest", "", false, true);
599     }
600     catch (...)
601     {
602         LayerFactory::unregisterLayer("SpatialUpSamplingNearest");
603         throw;
604     }
605     LayerFactory::unregisterLayer("SpatialUpSamplingNearest");
606
607     // Test an implemented layer.
608     runTorchNet("net_spatial_upsampling_nearest", "", false, true);
609 }
610
611 INSTANTIATE_TEST_CASE_P(/**/, Test_Torch_layers, dnnBackendsAndTargets());
612
613 }