Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/dnn/test/test_layers.cpp
16354 views
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) 2017, Intel Corporation, 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 <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
48
49
namespace opencv_test { namespace {
50
51
template<typename TString>
52
static String _tf(TString filename)
53
{
54
String basetestdir = getOpenCVExtraDir();
55
size_t len = basetestdir.size();
56
if(len > 0 && basetestdir[len-1] != '/' && basetestdir[len-1] != '\\')
57
return (basetestdir + "/dnn/layers") + filename;
58
return (basetestdir + "dnn/layers/") + filename;
59
}
60
61
void runLayer(Ptr<Layer> layer, std::vector<Mat> &inpBlobs, std::vector<Mat> &outBlobs)
62
{
63
size_t ninputs = inpBlobs.size();
64
std::vector<Mat> inp(ninputs), outp, intp;
65
std::vector<MatShape> inputs, outputs, internals;
66
67
for (size_t i = 0; i < ninputs; i++)
68
{
69
inp[i] = inpBlobs[i].clone();
70
inputs.push_back(shape(inp[i]));
71
}
72
73
layer->getMemoryShapes(inputs, 0, outputs, internals);
74
for (size_t i = 0; i < outputs.size(); i++)
75
{
76
outp.push_back(Mat(outputs[i], CV_32F));
77
}
78
for (size_t i = 0; i < internals.size(); i++)
79
{
80
intp.push_back(Mat(internals[i], CV_32F));
81
}
82
83
layer->finalize(inp, outp);
84
layer->forward(inp, outp, intp);
85
86
size_t noutputs = outp.size();
87
outBlobs.resize(noutputs);
88
for (size_t i = 0; i < noutputs; i++)
89
outBlobs[i] = outp[i];
90
}
91
92
class Test_Caffe_layers : public DNNTestLayer
93
{
94
public:
95
void testLayerUsingCaffeModels(const String& basename, bool useCaffeModel = false,
96
bool useCommonInputBlob = true, double l1 = 0.0,
97
double lInf = 0.0)
98
{
99
String prototxt = _tf(basename + ".prototxt");
100
String caffemodel = _tf(basename + ".caffemodel");
101
102
String inpfile = (useCommonInputBlob) ? _tf("blob.npy") : _tf(basename + ".input.npy");
103
String outfile = _tf(basename + ".npy");
104
105
Mat inp = blobFromNPY(inpfile);
106
Mat ref = blobFromNPY(outfile);
107
checkBackend(&inp, &ref);
108
109
Net net = readNetFromCaffe(prototxt, (useCaffeModel) ? caffemodel : String());
110
ASSERT_FALSE(net.empty());
111
112
net.setPreferableBackend(backend);
113
net.setPreferableTarget(target);
114
115
net.setInput(inp, "input");
116
Mat out = net.forward("output");
117
118
normAssert(ref, out, "", l1 ? l1 : default_l1, lInf ? lInf : default_lInf);
119
}
120
};
121
122
TEST_P(Test_Caffe_layers, Softmax)
123
{
124
testLayerUsingCaffeModels("layer_softmax");
125
}
126
127
TEST_P(Test_Caffe_layers, LRN)
128
{
129
testLayerUsingCaffeModels("layer_lrn_spatial");
130
testLayerUsingCaffeModels("layer_lrn_channels");
131
}
132
133
TEST_P(Test_Caffe_layers, Convolution)
134
{
135
testLayerUsingCaffeModels("layer_convolution", true);
136
}
137
138
TEST_P(Test_Caffe_layers, DeConvolution)
139
{
140
testLayerUsingCaffeModels("layer_deconvolution", true, false);
141
}
142
143
TEST_P(Test_Caffe_layers, InnerProduct)
144
{
145
if (backend == DNN_BACKEND_INFERENCE_ENGINE ||
146
(backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16))
147
throw SkipTestException("");
148
testLayerUsingCaffeModels("layer_inner_product", true);
149
}
150
151
TEST_P(Test_Caffe_layers, Pooling_max)
152
{
153
testLayerUsingCaffeModels("layer_pooling_max");
154
}
155
156
TEST_P(Test_Caffe_layers, Pooling_ave)
157
{
158
testLayerUsingCaffeModels("layer_pooling_ave");
159
}
160
161
TEST_P(Test_Caffe_layers, MVN)
162
{
163
testLayerUsingCaffeModels("layer_mvn");
164
}
165
166
void testReshape(const MatShape& inputShape, const MatShape& targetShape,
167
int axis = 0, int num_axes = -1,
168
MatShape mask = MatShape())
169
{
170
LayerParams params;
171
params.set("axis", axis);
172
params.set("num_axes", num_axes);
173
if (!mask.empty())
174
{
175
params.set("dim", DictValue::arrayInt<int*>(&mask[0], mask.size()));
176
}
177
178
Mat inp(inputShape.size(), &inputShape[0], CV_32F);
179
std::vector<Mat> inpVec(1, inp);
180
std::vector<Mat> outVec, intVec;
181
182
Ptr<Layer> rl = LayerFactory::createLayerInstance("Reshape", params);
183
runLayer(rl, inpVec, outVec);
184
185
Mat& out = outVec[0];
186
MatShape shape(out.size.p, out.size.p + out.dims);
187
EXPECT_EQ(shape, targetShape);
188
}
189
190
TEST(Layer_Test_Reshape, Accuracy)
191
{
192
{
193
int inp[] = {4, 3, 1, 2};
194
int out[] = {4, 3, 2};
195
testReshape(MatShape(inp, inp + 4), MatShape(out, out + 3), 2, 1);
196
}
197
{
198
int inp[] = {1, 128, 4, 4};
199
int out[] = {1, 2048};
200
int mask[] = {-1, 2048};
201
testReshape(MatShape(inp, inp + 4), MatShape(out, out + 2), 0, -1,
202
MatShape(mask, mask + 2));
203
}
204
{
205
int inp[] = {1, 2, 3};
206
int out[] = {3, 1, 2};
207
int mask[] = {3, 1, 2};
208
testReshape(MatShape(inp, inp + 3), MatShape(out, out + 3), 0, -1,
209
MatShape(mask, mask + 3));
210
}
211
}
212
213
TEST_P(Test_Caffe_layers, BatchNorm)
214
{
215
#if defined(INF_ENGINE_RELEASE) && INF_ENGINE_RELEASE < 2018030000
216
if (backend == DNN_BACKEND_INFERENCE_ENGINE)
217
throw SkipTestException("Test is enabled starts from OpenVINO 2018R3");
218
#endif
219
testLayerUsingCaffeModels("layer_batch_norm", true);
220
testLayerUsingCaffeModels("layer_batch_norm_local_stats", true, false);
221
}
222
223
TEST_P(Test_Caffe_layers, ReLU)
224
{
225
testLayerUsingCaffeModels("layer_relu");
226
}
227
228
TEST_P(Test_Caffe_layers, Dropout)
229
{
230
testLayerUsingCaffeModels("layer_dropout");
231
}
232
233
TEST_P(Test_Caffe_layers, Concat)
234
{
235
testLayerUsingCaffeModels("layer_concat");
236
testLayerUsingCaffeModels("layer_concat_optim", true, false);
237
testLayerUsingCaffeModels("layer_concat_shared_input", true, false);
238
}
239
240
TEST_P(Test_Caffe_layers, Fused_Concat)
241
{
242
if ((backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_CPU) ||
243
(backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_OPENCL))
244
throw SkipTestException("");
245
checkBackend();
246
247
// Test case
248
// input
249
// |
250
// v
251
// some_layer
252
// | |
253
// v v
254
// concat
255
Net net;
256
int interLayer;
257
{
258
LayerParams lp;
259
lp.type = "AbsVal";
260
lp.name = "someLayer";
261
interLayer = net.addLayerToPrev(lp.name, lp.type, lp);
262
}
263
{
264
LayerParams lp;
265
lp.set("axis", 1);
266
lp.type = "Concat";
267
lp.name = "testConcat";
268
int id = net.addLayer(lp.name, lp.type, lp);
269
net.connect(interLayer, 0, id, 0);
270
net.connect(interLayer, 0, id, 1);
271
}
272
int shape[] = {1, 2, 3, 4};
273
Mat input(4, shape, CV_32F);
274
randu(input, 0.0f, 1.0f); // [0, 1] to make AbsVal an identity transformation.
275
276
net.setInput(input);
277
net.setPreferableBackend(backend);
278
net.setPreferableTarget(target);
279
Mat out = net.forward();
280
281
normAssert(slice(out, Range::all(), Range(0, 2), Range::all(), Range::all()), input, "", default_l1, default_lInf);
282
normAssert(slice(out, Range::all(), Range(2, 4), Range::all(), Range::all()), input, "", default_l1, default_lInf);
283
}
284
285
TEST_P(Test_Caffe_layers, Eltwise)
286
{
287
if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)
288
throw SkipTestException("");
289
testLayerUsingCaffeModels("layer_eltwise");
290
}
291
292
TEST_P(Test_Caffe_layers, PReLU)
293
{
294
testLayerUsingCaffeModels("layer_prelu", true);
295
}
296
297
// TODO: fix an unstable test case
298
TEST_P(Test_Caffe_layers, layer_prelu_fc)
299
{
300
if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
301
throw SkipTestException("");
302
testLayerUsingCaffeModels("layer_prelu_fc", true, false);
303
}
304
305
//template<typename XMat>
306
//static void test_Layer_Concat()
307
//{
308
// Matx21f a(1.f, 1.f), b(2.f, 2.f), c(3.f, 3.f);
309
// std::vector<Blob> res(1), src = { Blob(XMat(a)), Blob(XMat(b)), Blob(XMat(c)) };
310
// Blob ref(XMat(Matx23f(1.f, 2.f, 3.f, 1.f, 2.f, 3.f)));
311
//
312
// runLayer(ConcatLayer::create(1), src, res);
313
// normAssert(ref, res[0]);
314
//}
315
//TEST(Layer_Concat, Accuracy)
316
//{
317
// test_Layer_Concat<Mat>());
318
//}
319
//OCL_TEST(Layer_Concat, Accuracy)
320
//{
321
// OCL_ON(test_Layer_Concat<Mat>());
322
// );
323
//}
324
325
TEST_P(Test_Caffe_layers, Reshape_Split_Slice)
326
{
327
if (backend == DNN_BACKEND_INFERENCE_ENGINE)
328
throw SkipTestException("");
329
330
Net net = readNetFromCaffe(_tf("reshape_and_slice_routines.prototxt"));
331
ASSERT_FALSE(net.empty());
332
333
net.setPreferableBackend(backend);
334
net.setPreferableTarget(target);
335
336
Mat input(6, 12, CV_32F);
337
RNG rng(0);
338
rng.fill(input, RNG::UNIFORM, -1, 1);
339
340
net.setInput(input, "input");
341
Mat output = net.forward("output");
342
343
normAssert(input, output, "", default_l1, default_lInf);
344
}
345
346
TEST_P(Test_Caffe_layers, Conv_Elu)
347
{
348
if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)
349
{
350
if (!checkMyriadTarget())
351
throw SkipTestException("Myriad is not available/disabled in OpenCV");
352
}
353
354
Net net = readNetFromTensorflow(_tf("layer_elu_model.pb"));
355
ASSERT_FALSE(net.empty());
356
357
Mat inp = blobFromNPY(_tf("layer_elu_in.npy"));
358
Mat ref = blobFromNPY(_tf("layer_elu_out.npy"));
359
360
net.setInput(inp, "input");
361
net.setPreferableBackend(backend);
362
net.setPreferableTarget(target);
363
Mat out = net.forward();
364
365
normAssert(ref, out, "", default_l1, default_lInf);
366
}
367
368
class Layer_LSTM_Test : public ::testing::Test
369
{
370
public:
371
int numInp, numOut;
372
Mat Wh, Wx, b;
373
Ptr<LSTMLayer> layer;
374
std::vector<Mat> inputs, outputs;
375
376
Layer_LSTM_Test() {}
377
378
void init(const MatShape &inpShape_, const MatShape &outShape_,
379
bool produceCellOutput, bool useTimestampDim)
380
{
381
numInp = total(inpShape_);
382
numOut = total(outShape_);
383
384
Wh = Mat::ones(4 * numOut, numOut, CV_32F);
385
Wx = Mat::ones(4 * numOut, numInp, CV_32F);
386
b = Mat::ones(4 * numOut, 1, CV_32F);
387
388
LayerParams lp;
389
lp.blobs.resize(3);
390
lp.blobs[0] = Wh;
391
lp.blobs[1] = Wx;
392
lp.blobs[2] = b;
393
lp.set<bool>("produce_cell_output", produceCellOutput);
394
lp.set<bool>("use_timestamp_dim", useTimestampDim);
395
396
layer = LSTMLayer::create(lp);
397
layer->setOutShape(outShape_);
398
}
399
};
400
401
TEST_F(Layer_LSTM_Test, get_set_test)
402
{
403
const int TN = 4;
404
MatShape inpShape = shape(5, 3, 2);
405
MatShape outShape = shape(3, 1, 2);
406
MatShape inpResShape = concat(shape(TN), inpShape);
407
MatShape outResShape = concat(shape(TN), outShape);
408
409
init(inpShape, outShape, true, false);
410
layer->setOutShape(outShape);
411
412
Mat C((int)outResShape.size(), &outResShape[0], CV_32F);
413
randu(C, -1., 1.);
414
Mat H = C.clone();
415
randu(H, -1., 1.);
416
417
Mat inp((int)inpResShape.size(), &inpResShape[0], CV_32F);
418
randu(inp, -1., 1.);
419
420
inputs.push_back(inp);
421
runLayer(layer, inputs, outputs);
422
423
EXPECT_EQ(2u, outputs.size());
424
425
print(outResShape, "outResShape");
426
print(shape(outputs[0]), "out0");
427
print(shape(outputs[0]), "out1");
428
429
EXPECT_EQ(outResShape, shape(outputs[0]));
430
EXPECT_EQ(outResShape, shape(outputs[1]));
431
432
EXPECT_EQ(0, layer->inputNameToIndex("x"));
433
EXPECT_EQ(0, layer->outputNameToIndex("h"));
434
EXPECT_EQ(1, layer->outputNameToIndex("c"));
435
}
436
437
TEST(Layer_LSTM_Test_Accuracy_with_, CaffeRecurrent)
438
{
439
LayerParams lp;
440
lp.blobs.resize(3);
441
lp.blobs[0] = blobFromNPY(_tf("lstm.prototxt.w_2.npy")); // Wh
442
lp.blobs[1] = blobFromNPY(_tf("lstm.prototxt.w_0.npy")); // Wx
443
lp.blobs[2] = blobFromNPY(_tf("lstm.prototxt.w_1.npy")); // bias
444
Ptr<LSTMLayer> layer = LSTMLayer::create(lp);
445
446
Mat inp = blobFromNPY(_tf("recurrent.input.npy"));
447
std::vector<Mat> inputs(1, inp), outputs;
448
runLayer(layer, inputs, outputs);
449
450
Mat h_t_reference = blobFromNPY(_tf("lstm.prototxt.h_1.npy"));
451
normAssert(h_t_reference, outputs[0]);
452
}
453
454
TEST(Layer_RNN_Test_Accuracy_with_, CaffeRecurrent)
455
{
456
Ptr<RNNLayer> layer = RNNLayer::create(LayerParams());
457
458
layer->setWeights(
459
blobFromNPY(_tf("rnn.prototxt.w_0.npy")),
460
blobFromNPY(_tf("rnn.prototxt.w_1.npy")),
461
blobFromNPY(_tf("rnn.prototxt.w_2.npy")),
462
blobFromNPY(_tf("rnn.prototxt.w_3.npy")),
463
blobFromNPY(_tf("rnn.prototxt.w_4.npy")) );
464
465
std::vector<Mat> output, input(1, blobFromNPY(_tf("recurrent.input.npy")));
466
runLayer(layer, input, output);
467
468
Mat h_ref = blobFromNPY(_tf("rnn.prototxt.h_1.npy"));
469
normAssert(h_ref, output[0]);
470
}
471
472
473
class Layer_RNN_Test : public ::testing::Test
474
{
475
public:
476
int nX, nH, nO, nT, nS;
477
Mat Whh, Wxh, bh, Who, bo;
478
Ptr<RNNLayer> layer;
479
480
std::vector<Mat> inputs, outputs;
481
482
Layer_RNN_Test()
483
{
484
nT = 3;
485
nS = 5;
486
nX = 31;
487
nH = 64;
488
nO = 100;
489
490
Whh = Mat::ones(nH, nH, CV_32F);
491
Wxh = Mat::ones(nH, nX, CV_32F);
492
bh = Mat::ones(nH, 1, CV_32F);
493
Who = Mat::ones(nO, nH, CV_32F);
494
bo = Mat::ones(nO, 1, CV_32F);
495
496
layer = RNNLayer::create(LayerParams());
497
layer->setProduceHiddenOutput(true);
498
layer->setWeights(Wxh, bh, Whh, Who, bo);
499
}
500
};
501
502
TEST_F(Layer_RNN_Test, get_set_test)
503
{
504
int sz[] = { nT, nS, 1, nX };
505
Mat inp(4, sz, CV_32F);
506
randu(inp, -1., 1.);
507
inputs.push_back(inp);
508
runLayer(layer, inputs, outputs);
509
510
EXPECT_EQ(outputs.size(), 2u);
511
EXPECT_EQ(shape(outputs[0]), shape(nT, nS, nO));
512
EXPECT_EQ(shape(outputs[1]), shape(nT, nS, nH));
513
}
514
515
TEST(Layer_Test_ROIPooling, Accuracy)
516
{
517
Net net = readNetFromCaffe(_tf("net_roi_pooling.prototxt"));
518
519
Mat inp = blobFromNPY(_tf("net_roi_pooling.input.npy"));
520
Mat rois = blobFromNPY(_tf("net_roi_pooling.rois.npy"));
521
Mat ref = blobFromNPY(_tf("net_roi_pooling.npy"));
522
523
net.setInput(inp, "input");
524
net.setInput(rois, "rois");
525
net.setPreferableBackend(DNN_BACKEND_OPENCV);
526
527
Mat out = net.forward();
528
529
normAssert(out, ref);
530
}
531
532
TEST_P(Test_Caffe_layers, FasterRCNN_Proposal)
533
{
534
if ((backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16) ||
535
backend == DNN_BACKEND_INFERENCE_ENGINE)
536
throw SkipTestException("");
537
Net net = readNetFromCaffe(_tf("net_faster_rcnn_proposal.prototxt"));
538
539
Mat scores = blobFromNPY(_tf("net_faster_rcnn_proposal.scores.npy"));
540
Mat deltas = blobFromNPY(_tf("net_faster_rcnn_proposal.deltas.npy"));
541
Mat imInfo = (Mat_<float>(1, 3) << 600, 800, 1.6f);
542
543
net.setInput(scores, "rpn_cls_prob_reshape");
544
net.setInput(deltas, "rpn_bbox_pred");
545
net.setInput(imInfo, "im_info");
546
547
std::vector<Mat> outs;
548
net.setPreferableBackend(backend);
549
net.setPreferableTarget(target);
550
net.forward(outs, "output");
551
552
for (int i = 0; i < 2; ++i)
553
{
554
Mat ref = blobFromNPY(_tf(i == 0 ? "net_faster_rcnn_proposal.out_rois.npy" :
555
"net_faster_rcnn_proposal.out_scores.npy"));
556
const int numDets = ref.size[0];
557
EXPECT_LE(numDets, outs[i].size[0]);
558
normAssert(outs[i].rowRange(0, numDets), ref);
559
560
if (numDets < outs[i].size[0])
561
EXPECT_EQ(countNonZero(outs[i].rowRange(numDets, outs[i].size[0])), 0);
562
}
563
}
564
565
typedef testing::TestWithParam<tuple<Vec4i, Vec2i, bool> > Scale_untrainable;
566
TEST_P(Scale_untrainable, Accuracy)
567
{
568
Vec4i inpShapeVec = get<0>(GetParam());
569
int axis = get<1>(GetParam())[0];
570
int weightsDims = get<1>(GetParam())[1];
571
bool testFusion = get<2>(GetParam());
572
const int inpShape[] = {inpShapeVec[0], inpShapeVec[1], inpShapeVec[2], inpShapeVec[3]};
573
574
// Create a network with two inputs. Scale layer multiplies a first input to
575
// a second one. See http://caffe.berkeleyvision.org/tutorial/layers/scale.html
576
Net net;
577
// Check that this version of Scale layer won't be fused with Convolution layer.
578
if (testFusion)
579
{
580
LayerParams lp;
581
lp.set("kernel_size", 1);
582
lp.set("num_output", 3);
583
lp.set("group", 3);
584
lp.set("bias_term", false);
585
lp.type = "Convolution";
586
lp.name = "testConv";
587
588
std::vector<int> weightsShape(4);
589
weightsShape[0] = 3; // #outChannels
590
weightsShape[1] = 1; // #inpChannels / group
591
weightsShape[2] = 1; // height
592
weightsShape[3] = 1; // width
593
Mat weights(weightsShape, CV_32F);
594
weights.setTo(1);
595
lp.blobs.push_back(weights);
596
net.addLayerToPrev(lp.name, lp.type, lp);
597
}
598
LayerParams lp;
599
lp.type = "Scale";
600
lp.name = "testLayer";
601
lp.set("axis", axis);
602
int id = net.addLayerToPrev(lp.name, lp.type, lp);
603
net.connect(0, 1, id, 1);
604
605
Mat input(4, inpShape, CV_32F);
606
Mat weights(weightsDims, &inpShape[axis], CV_32F);
607
randu(input, -1, 1);
608
randu(weights, -1, 1);
609
610
std::vector<String> inpNames(2);
611
inpNames[0] = "scale_input";
612
inpNames[1] = "scale_weights";
613
net.setInputsNames(inpNames);
614
net.setInput(input, inpNames[0]);
615
net.setInput(weights, inpNames[1]);
616
net.setPreferableBackend(DNN_BACKEND_OPENCV);
617
Mat out = net.forward();
618
619
Mat ref(input.dims, input.size, CV_32F);
620
float* inpData = (float*)input.data;
621
float* refData = (float*)ref.data;
622
float* weightsData = (float*)weights.data;
623
int spatialSize = 1;
624
for (int i = axis + weightsDims; i < 4; ++i)
625
spatialSize *= inpShape[i];
626
for (int i = 0; i < ref.total(); ++i)
627
{
628
float w = weightsData[(i / spatialSize) % weights.total()];
629
refData[i] = inpData[i] * w;
630
}
631
normAssert(out, ref);
632
}
633
634
INSTANTIATE_TEST_CASE_P(Layer_Test, Scale_untrainable, Combine(
635
/*input size*/ Values(Vec4i(2, 3, 4, 5)),
636
/*axis, #dims*/ Values(Vec2i(0, 1), Vec2i(0, 2), Vec2i(0, 3), Vec2i(0, 4),
637
Vec2i(1, 1), Vec2i(1, 2), Vec2i(1, 3),
638
Vec2i(2, 1), Vec2i(2, 2),
639
Vec2i(3, 1)),
640
/*conv fusion*/ testing::Bool()
641
));
642
643
typedef testing::TestWithParam<tuple<Vec4i, Vec4i, int, int, int> > Crop;
644
TEST_P(Crop, Accuracy)
645
{
646
Vec4i inpShapeVec = get<0>(GetParam());
647
Vec4i sizShapeVec = get<1>(GetParam());
648
int axis = get<2>(GetParam());
649
int numOffsets = get<3>(GetParam());
650
int offsetVal = get<4>(GetParam());
651
const int inpShape[] = {inpShapeVec[0], inpShapeVec[1], inpShapeVec[2], inpShapeVec[3]};
652
const int sizShape[] = {sizShapeVec[0], sizShapeVec[1], sizShapeVec[2], sizShapeVec[3]};
653
654
// Create a network with two inputs. Crop layer crops a first input to
655
// the size of a second one.
656
// See http://caffe.berkeleyvision.org/tutorial/layers/crop.html
657
Net net;
658
659
LayerParams lp;
660
lp.name = "testCrop";
661
lp.type = "Crop";
662
lp.set("axis", axis);
663
if (numOffsets > 0)
664
{
665
std::vector<int> offsets(numOffsets, offsetVal);
666
lp.set("offset", DictValue::arrayInt<int*>(&offsets[0], offsets.size()));
667
}
668
else
669
offsetVal = 0;
670
int id = net.addLayerToPrev(lp.name, lp.type, lp);
671
net.connect(0, 1, id, 1);
672
673
Mat inpImage(4, inpShape, CV_32F);
674
Mat sizImage(4, sizShape, CV_32F);
675
randu(inpImage, -1, 1);
676
randu(sizImage, -1, 1);
677
678
std::vector<String> inpNames(2);
679
inpNames[0] = "cropImage";
680
inpNames[1] = "sizImage";
681
net.setInputsNames(inpNames);
682
net.setInput(inpImage, inpNames[0]);
683
net.setInput(sizImage, inpNames[1]);
684
net.setPreferableBackend(DNN_BACKEND_OPENCV);
685
686
// There are a few conditions that represent invalid input to the crop
687
// layer, so in those cases we want to verify an exception is thrown.
688
689
bool shouldThrowException = false;
690
if (numOffsets > 1 && numOffsets != 4 - axis)
691
shouldThrowException = true;
692
else
693
for (int i = axis; i < 4; i++)
694
if (sizShape[i] + offsetVal > inpShape[i])
695
shouldThrowException = true;
696
697
Mat out;
698
if (shouldThrowException)
699
{
700
ASSERT_ANY_THROW(out = net.forward());
701
return;
702
}
703
else
704
out = net.forward();
705
706
// Finally, compare the cropped output blob from the DNN layer (out)
707
// to a reference blob (ref) that we compute here.
708
709
std::vector<Range> crop_range;
710
crop_range.resize(4, Range::all());
711
for (int i = axis; i < 4; i++)
712
crop_range[i] = Range(offsetVal, sizShape[i] + offsetVal);
713
714
Mat ref(sizImage.dims, sizImage.size, CV_32F);
715
inpImage(&crop_range[0]).copyTo(ref);
716
normAssert(out, ref);
717
}
718
719
INSTANTIATE_TEST_CASE_P(Layer_Test, Crop, Combine(
720
/*input blob shape*/ Values(Vec4i(1, 3, 20, 30)),
721
/*cropsize blob shape*/ Values(Vec4i(1, 3, 10, 12)),
722
/*start axis*/ Values(0, 1, 2),
723
/*number of offsets*/ Values(0, 1, 2, 4),
724
/*offset value*/ Values(3, 4)
725
));
726
727
// Check that by default average pooling layer should not count zero padded values
728
// into the normalization area.
729
TEST_P(Test_Caffe_layers, Average_pooling_kernel_area)
730
{
731
#if defined(INF_ENGINE_RELEASE) && INF_ENGINE_RELEASE < 2018030000
732
if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)
733
throw SkipTestException("Test is enabled starts from OpenVINO 2018R3");
734
#endif
735
LayerParams lp;
736
lp.name = "testAvePool";
737
lp.type = "Pooling";
738
lp.set("kernel_size", 2);
739
lp.set("stride", 2);
740
lp.set("pool", "AVE");
741
742
Net net;
743
net.addLayerToPrev(lp.name, lp.type, lp);
744
// 1 2 | 3
745
// 4 5 | 6
746
// ----+--
747
// 7 8 | 9
748
Mat inp = (Mat_<float>(3, 3) << 1, 2, 3, 4, 5, 6, 7, 8, 9);
749
Mat ref = (Mat_<float>(2, 2) << (1 + 2 + 4 + 5) / 4.f, (3 + 6) / 2.f, (7 + 8) / 2.f, 9);
750
Mat tmp = blobFromImage(inp);
751
net.setInput(blobFromImage(inp));
752
net.setPreferableBackend(backend);
753
net.setPreferableTarget(target);
754
Mat out = net.forward();
755
normAssert(out, blobFromImage(ref));
756
}
757
758
// Test PriorBoxLayer in case of no aspect ratios (just squared proposals).
759
TEST_P(Test_Caffe_layers, PriorBox_squares)
760
{
761
if (backend == DNN_BACKEND_INFERENCE_ENGINE)
762
throw SkipTestException("");
763
LayerParams lp;
764
lp.name = "testPriorBox";
765
lp.type = "PriorBox";
766
lp.set("min_size", 2);
767
lp.set("flip", true);
768
lp.set("clip", true);
769
float variance[] = {0.1f, 0.1f, 0.2f, 0.2f};
770
float aspectRatios[] = {1.0f}; // That should be ignored.
771
lp.set("variance", DictValue::arrayReal<float*>(&variance[0], 4));
772
lp.set("aspect_ratio", DictValue::arrayReal<float*>(&aspectRatios[0], 1));
773
774
Net net;
775
int id = net.addLayerToPrev(lp.name, lp.type, lp);
776
net.connect(0, 0, id, 1); // The second input is an input image. Shapes are used for boxes normalization.
777
Mat inp(1, 2, CV_32F);
778
randu(inp, -1, 1);
779
net.setInput(blobFromImage(inp));
780
net.setPreferableBackend(backend);
781
net.setPreferableTarget(target);
782
Mat out = net.forward();
783
784
Mat ref = (Mat_<float>(4, 4) << 0.0, 0.0, 0.75, 1.0,
785
0.25, 0.0, 1.0, 1.0,
786
0.1f, 0.1f, 0.2f, 0.2f,
787
0.1f, 0.1f, 0.2f, 0.2f);
788
double l1 = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 2e-5 : 1e-5;
789
normAssert(out.reshape(1, 4), ref, "", l1);
790
}
791
792
typedef TestWithParam<tuple<int, int> > Layer_Test_DWconv_Prelu;
793
TEST_P(Layer_Test_DWconv_Prelu, Accuracy)
794
{
795
// Test case
796
// input img size 3x16x16 value all 1
797
// |
798
// v
799
// dw_conv weight[0]=-1 weight[1]=-2 weight[2]=-3 bias={1,2,3}
800
// |
801
// v
802
// prelu weight={1,2,3}
803
// |
804
// v
805
// output out size 3x14x14 if right: out[0]=-8 out[0]=-32 out[0]=-72
806
// but current opencv output: out[0]=-24 out[0]=-48 out[0]=-72
807
808
const int num_input = get<0>(GetParam()); //inpChannels
809
const int group = 3; //outChannels=group when group>1
810
const int num_output = get<1>(GetParam());
811
const int kernel_depth = num_input/group;
812
CV_Assert_N(num_output >= group, num_output % group == 0, num_input % group == 0);
813
814
Net net;
815
//layer 1: dwconv
816
LayerParams lp;
817
lp.name = "dwconv";
818
lp.type = "Convolution";
819
lp.set("kernel_size", 3);
820
lp.set("num_output", num_output);
821
lp.set("pad", 0);
822
lp.set("group", group);
823
lp.set("stride", 1);
824
lp.set("engine", "CAFFE");
825
lp.set("bias_term", "true");
826
827
std::vector<int> weightsShape(4);
828
weightsShape[0] = num_output; // #outChannels
829
weightsShape[1] = kernel_depth; // #inpChannels / group
830
weightsShape[2] = 3; // height
831
weightsShape[3] = 3; // width
832
Mat weights(weightsShape, CV_32F, Scalar(1));
833
834
//assign weights
835
for (int i = 0; i < weightsShape[0]; ++i)
836
{
837
for (int j = 0; j < weightsShape[1]; ++j)
838
{
839
for (int k = 0; k < weightsShape[2]; ++k)
840
{
841
for (int l = 0; l < weightsShape[3]; ++l)
842
{
843
weights.ptr<float>(i, j, k)[l]=-1*(i+1);
844
}
845
}
846
}
847
}
848
lp.blobs.push_back(weights);
849
850
//assign bias
851
Mat bias(1, num_output, CV_32F, Scalar(1));
852
for (int i = 0; i < 1; ++i)
853
{
854
for (int j = 0; j < num_output; ++j)
855
{
856
bias.ptr<float>(i)[j]=j+1;
857
}
858
}
859
lp.blobs.push_back(bias);
860
net.addLayerToPrev(lp.name, lp.type, lp);
861
862
//layer 2: prelu
863
LayerParams lpr;
864
lpr.name = "dw_relu";
865
lpr.type = "PReLU";
866
Mat weightsp(1, num_output, CV_32F, Scalar(1));
867
868
//assign weights
869
for (int i = 0; i < 1; ++i)
870
{
871
for (int j = 0; j < num_output; ++j)
872
{
873
weightsp.ptr<float>(i)[j]=j+1;
874
}
875
}
876
877
lpr.blobs.push_back(weightsp);
878
net.addLayerToPrev(lpr.name, lpr.type, lpr);
879
880
int shape[] = {1, num_input, 16, 16};
881
Mat in_blob(4, &shape[0], CV_32FC1, Scalar(1));
882
883
net.setPreferableBackend(DNN_BACKEND_OPENCV);
884
net.setInput(in_blob);
885
Mat out = net.forward();
886
887
//assign target
888
std::vector<int> outShape(4);
889
outShape[0] = 1;
890
outShape[1] = num_output; // outChannels
891
outShape[2] = 14; // height
892
outShape[3] = 14; // width
893
Mat target(outShape, CV_32F, Scalar(1));
894
for (int i = 0; i < outShape[0]; ++i)
895
{
896
for (int j = 0; j < outShape[1]; ++j)
897
{
898
for (int k = 0; k < outShape[2]; ++k)
899
{
900
for (int l = 0; l < outShape[3]; ++l)
901
{
902
target.ptr<float>(i, j, k)[l]=(-9*kernel_depth*(j+1)+j+1)*(j+1);
903
}
904
}
905
}
906
}
907
908
normAssert(out, target);
909
}
910
INSTANTIATE_TEST_CASE_P(/**/, Layer_Test_DWconv_Prelu, Combine(Values(3, 6), Values(3, 6)));
911
912
#ifdef HAVE_INF_ENGINE
913
// Using Intel's Model Optimizer generate .xml and .bin files:
914
// ./ModelOptimizer -w /path/to/caffemodel -d /path/to/prototxt \
915
// -p FP32 -i -b ${batch_size} -o /path/to/output/folder
916
TEST(Layer_Test_Convolution_DLDT, Accuracy)
917
{
918
Net netDefault = readNet(_tf("layer_convolution.caffemodel"), _tf("layer_convolution.prototxt"));
919
Net net = readNet(_tf("layer_convolution.xml"), _tf("layer_convolution.bin"));
920
921
Mat inp = blobFromNPY(_tf("blob.npy"));
922
923
netDefault.setInput(inp);
924
netDefault.setPreferableBackend(DNN_BACKEND_OPENCV);
925
Mat outDefault = netDefault.forward();
926
927
net.setInput(inp);
928
Mat out = net.forward();
929
930
normAssert(outDefault, out);
931
932
std::vector<int> outLayers = net.getUnconnectedOutLayers();
933
ASSERT_EQ(net.getLayer(outLayers[0])->name, "output_merge");
934
ASSERT_EQ(net.getLayer(outLayers[0])->type, "Concat");
935
}
936
937
TEST(Layer_Test_Convolution_DLDT, setInput_uint8)
938
{
939
Mat inp = blobFromNPY(_tf("blob.npy"));
940
941
Mat inputs[] = {Mat(inp.dims, inp.size, CV_8U), Mat()};
942
randu(inputs[0], 0, 255);
943
inputs[0].convertTo(inputs[1], CV_32F);
944
945
Mat outs[2];
946
for (int i = 0; i < 2; ++i)
947
{
948
Net net = readNet(_tf("layer_convolution.xml"), _tf("layer_convolution.bin"));
949
net.setInput(inputs[i]);
950
outs[i] = net.forward();
951
ASSERT_EQ(outs[i].type(), CV_32F);
952
}
953
normAssert(outs[0], outs[1]);
954
}
955
956
// 1. Create a .prototxt file with the following network:
957
// layer {
958
// type: "Input" name: "data" top: "data"
959
// input_param { shape { dim: 1 dim: 2 dim: 3 } }
960
// }
961
// layer {
962
// type: "Input" name: "second_input" top: "second_input"
963
// input_param { shape { dim: 1 dim: 2 dim: 3 } }
964
// }
965
// layer {
966
// type: "Eltwise" name: "output" top: "output"
967
// bottom: "data" bottom: "second_input"
968
// eltwise_param { operation: SUM }
969
// }
970
//
971
// 2. Create a .caffemodel file using Caffe:
972
//
973
// import caffe
974
// net = caffe.Net('/path/to/prototxt', caffe.TEST)
975
// net.save('/path/to/caffemodel')
976
//
977
// 3. Convert using ModelOptimizer.
978
typedef testing::TestWithParam<tuple<int, int> > Test_DLDT_two_inputs;
979
TEST_P(Test_DLDT_two_inputs, as_IR)
980
{
981
int firstInpType = get<0>(GetParam());
982
int secondInpType = get<1>(GetParam());
983
// TODO: It looks like a bug in Inference Engine.
984
if (secondInpType == CV_8U)
985
throw SkipTestException("");
986
987
Net net = readNet(_tf("net_two_inputs.xml"), _tf("net_two_inputs.bin"));
988
int inpSize[] = {1, 2, 3};
989
Mat firstInp(3, &inpSize[0], firstInpType);
990
Mat secondInp(3, &inpSize[0], secondInpType);
991
randu(firstInp, 0, 255);
992
randu(secondInp, 0, 255);
993
994
net.setInput(firstInp, "data");
995
net.setInput(secondInp, "second_input");
996
Mat out = net.forward();
997
998
Mat ref;
999
cv::add(firstInp, secondInp, ref, Mat(), CV_32F);
1000
normAssert(out, ref);
1001
}
1002
1003
TEST_P(Test_DLDT_two_inputs, as_backend)
1004
{
1005
static const float kScale = 0.5f;
1006
static const float kScaleInv = 1.0f / kScale;
1007
1008
Net net;
1009
LayerParams lp;
1010
lp.type = "Eltwise";
1011
lp.name = "testLayer";
1012
lp.set("operation", "sum");
1013
int eltwiseId = net.addLayerToPrev(lp.name, lp.type, lp); // connect to a first input
1014
net.connect(0, 1, eltwiseId, 1); // connect to a second input
1015
1016
int inpSize[] = {1, 2, 3};
1017
Mat firstInp(3, &inpSize[0], get<0>(GetParam()));
1018
Mat secondInp(3, &inpSize[0], get<1>(GetParam()));
1019
randu(firstInp, 0, 255);
1020
randu(secondInp, 0, 255);
1021
1022
net.setInputsNames({"data", "second_input"});
1023
net.setInput(firstInp, "data", kScale);
1024
net.setInput(secondInp, "second_input", kScaleInv);
1025
net.setPreferableBackend(DNN_BACKEND_INFERENCE_ENGINE);
1026
Mat out = net.forward();
1027
1028
Mat ref;
1029
addWeighted(firstInp, kScale, secondInp, kScaleInv, 0, ref, CV_32F);
1030
normAssert(out, ref);
1031
}
1032
1033
INSTANTIATE_TEST_CASE_P(/*nothing*/, Test_DLDT_two_inputs, Combine(
1034
Values(CV_8U, CV_32F), Values(CV_8U, CV_32F)
1035
));
1036
1037
class UnsupportedLayer : public Layer
1038
{
1039
public:
1040
UnsupportedLayer(const LayerParams &params) {}
1041
1042
static Ptr<Layer> create(const LayerParams& params)
1043
{
1044
return Ptr<Layer>(new UnsupportedLayer(params));
1045
}
1046
1047
virtual bool supportBackend(int backendId) CV_OVERRIDE
1048
{
1049
return backendId == DNN_BACKEND_OPENCV;
1050
}
1051
1052
virtual void forward(cv::InputArrayOfArrays inputs, cv::OutputArrayOfArrays outputs, cv::OutputArrayOfArrays internals) CV_OVERRIDE {}
1053
};
1054
1055
TEST(Test_DLDT, fused_output)
1056
{
1057
static const int kNumChannels = 3;
1058
CV_DNN_REGISTER_LAYER_CLASS(Unsupported, UnsupportedLayer);
1059
Net net;
1060
{
1061
LayerParams lp;
1062
lp.set("kernel_size", 1);
1063
lp.set("num_output", 3);
1064
lp.set("bias_term", false);
1065
lp.type = "Convolution";
1066
lp.name = "testConv";
1067
lp.blobs.push_back(Mat({kNumChannels, 1, 1, 1}, CV_32F, Scalar(1)));
1068
net.addLayerToPrev(lp.name, lp.type, lp);
1069
}
1070
{
1071
LayerParams lp;
1072
lp.set("bias_term", false);
1073
lp.type = "Scale";
1074
lp.name = "testScale";
1075
lp.blobs.push_back(Mat({kNumChannels}, CV_32F, Scalar(1)));
1076
net.addLayerToPrev(lp.name, lp.type, lp);
1077
}
1078
{
1079
LayerParams lp;
1080
net.addLayerToPrev("unsupported_layer", "Unsupported", lp);
1081
}
1082
net.setPreferableBackend(DNN_BACKEND_INFERENCE_ENGINE);
1083
net.setInput(Mat({1, 1, 1, 1}, CV_32FC1, Scalar(1)));
1084
ASSERT_NO_THROW(net.forward());
1085
LayerFactory::unregisterLayer("Unsupported");
1086
}
1087
1088
TEST(Test_DLDT, multiple_networks)
1089
{
1090
Net nets[2];
1091
for (int i = 0; i < 2; ++i)
1092
{
1093
nets[i].setInputsNames(std::vector<String>(1, format("input_%d", i)));
1094
1095
LayerParams lp;
1096
lp.set("kernel_size", 1);
1097
lp.set("num_output", 1);
1098
lp.set("bias_term", false);
1099
lp.type = "Convolution";
1100
lp.name = format("testConv_%d", i);
1101
lp.blobs.push_back(Mat({1, 1, 1, 1}, CV_32F, Scalar(1 + i)));
1102
nets[i].addLayerToPrev(lp.name, lp.type, lp);
1103
nets[i].setPreferableBackend(DNN_BACKEND_INFERENCE_ENGINE);
1104
nets[i].setInput(Mat({1, 1, 1, 1}, CV_32FC1, Scalar(1)));
1105
}
1106
Mat out_1 = nets[0].forward();
1107
Mat out_2 = nets[1].forward();
1108
// After the second model is initialized we try to receive an output from the first network again.
1109
out_1 = nets[0].forward();
1110
normAssert(2 * out_1, out_2);
1111
}
1112
#endif // HAVE_INF_ENGINE
1113
1114
// Test a custom layer.
1115
class CustomInterpLayer CV_FINAL : public Layer
1116
{
1117
public:
1118
CustomInterpLayer(const LayerParams &params) : Layer(params)
1119
{
1120
zoomFactor = params.get<int>("zoom_factor", 0);
1121
outWidth = params.get<int>("width", 0);
1122
outHeight = params.get<int>("height", 0);
1123
}
1124
1125
static Ptr<Layer> create(LayerParams& params)
1126
{
1127
return Ptr<Layer>(new CustomInterpLayer(params));
1128
}
1129
1130
virtual bool getMemoryShapes(const std::vector<std::vector<int> > &inputs,
1131
const int requiredOutputs,
1132
std::vector<std::vector<int> > &outputs,
1133
std::vector<std::vector<int> > &internals) const CV_OVERRIDE
1134
{
1135
const int batchSize = inputs[0][0];
1136
const int numChannels = inputs[0][1];
1137
const int inpHeight = inputs[0][2];
1138
const int inpWidth = inputs[0][3];
1139
1140
std::vector<int> outShape(4);
1141
outShape[0] = batchSize;
1142
outShape[1] = numChannels;
1143
outShape[2] = outHeight != 0 ? outHeight : (inpHeight + (inpHeight - 1) * (zoomFactor - 1));
1144
outShape[3] = outWidth != 0 ? outWidth : (inpWidth + (inpWidth - 1) * (zoomFactor - 1));
1145
outputs.assign(1, outShape);
1146
return false;
1147
}
1148
1149
virtual void finalize(InputArrayOfArrays, OutputArrayOfArrays outputs_arr) CV_OVERRIDE
1150
{
1151
std::vector<Mat> outputs;
1152
outputs_arr.getMatVector(outputs);
1153
1154
if (!outWidth && !outHeight)
1155
{
1156
outHeight = outputs[0].size[2];
1157
outWidth = outputs[0].size[3];
1158
}
1159
}
1160
1161
// Implementation of this custom layer is based on https://github.com/cdmh/deeplab-public/blob/master/src/caffe/layers/interp_layer.cpp
1162
void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE
1163
{
1164
CV_TRACE_FUNCTION();
1165
CV_TRACE_ARG_VALUE(name, "name", name.c_str());
1166
1167
if (inputs_arr.depth() == CV_16S)
1168
{
1169
forward_fallback(inputs_arr, outputs_arr, internals_arr);
1170
return;
1171
}
1172
1173
std::vector<Mat> inputs, outputs;
1174
inputs_arr.getMatVector(inputs);
1175
outputs_arr.getMatVector(outputs);
1176
1177
Mat& inp = inputs[0];
1178
Mat& out = outputs[0];
1179
const float* inpData = (float*)inp.data;
1180
float* outData = (float*)out.data;
1181
1182
const int batchSize = inp.size[0];
1183
const int numChannels = inp.size[1];
1184
const int inpHeight = inp.size[2];
1185
const int inpWidth = inp.size[3];
1186
1187
const float rheight = (outHeight > 1) ? static_cast<float>(inpHeight - 1) / (outHeight - 1) : 0.f;
1188
const float rwidth = (outWidth > 1) ? static_cast<float>(inpWidth - 1) / (outWidth - 1) : 0.f;
1189
for (int h2 = 0; h2 < outHeight; ++h2)
1190
{
1191
const float h1r = rheight * h2;
1192
const int h1 = h1r;
1193
const int h1p = (h1 < inpHeight - 1) ? 1 : 0;
1194
const float h1lambda = h1r - h1;
1195
const float h0lambda = 1.f - h1lambda;
1196
for (int w2 = 0; w2 < outWidth; ++w2)
1197
{
1198
const float w1r = rwidth * w2;
1199
const int w1 = w1r;
1200
const int w1p = (w1 < inpWidth - 1) ? 1 : 0;
1201
const float w1lambda = w1r - w1;
1202
const float w0lambda = 1.f - w1lambda;
1203
const float* pos1 = inpData + h1 * inpWidth + w1;
1204
float* pos2 = outData + h2 * outWidth + w2;
1205
for (int c = 0; c < batchSize * numChannels; ++c)
1206
{
1207
pos2[0] =
1208
h0lambda * (w0lambda * pos1[0] + w1lambda * pos1[w1p]) +
1209
h1lambda * (w0lambda * pos1[h1p * inpWidth] + w1lambda * pos1[h1p * inpWidth + w1p]);
1210
pos1 += inpWidth * inpHeight;
1211
pos2 += outWidth * outHeight;
1212
}
1213
}
1214
}
1215
}
1216
1217
private:
1218
int outWidth, outHeight, zoomFactor;
1219
};
1220
1221
#ifndef OPENCV_DNN_EXTERNAL_PROTOBUF
1222
TEST_P(Test_Caffe_layers, Interp)
1223
#else
1224
TEST_P(Test_Caffe_layers, DISABLED_Interp) // requires patched protobuf (available in OpenCV source tree only)
1225
#endif
1226
{
1227
if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)
1228
throw SkipTestException("");
1229
// Test a custom layer.
1230
CV_DNN_REGISTER_LAYER_CLASS(Interp, CustomInterpLayer);
1231
try
1232
{
1233
testLayerUsingCaffeModels("layer_interp", false, false);
1234
}
1235
catch (...)
1236
{
1237
LayerFactory::unregisterLayer("Interp");
1238
throw;
1239
}
1240
LayerFactory::unregisterLayer("Interp");
1241
1242
// Test an implemented layer.
1243
testLayerUsingCaffeModels("layer_interp", false, false);
1244
}
1245
1246
INSTANTIATE_TEST_CASE_P(/*nothing*/, Test_Caffe_layers, dnnBackendsAndTargets());
1247
1248
TEST(Layer_Test_PoolingIndices, Accuracy)
1249
{
1250
Net net;
1251
1252
LayerParams lp;
1253
lp.set("pool", "max");
1254
lp.set("kernel_w", 2);
1255
lp.set("kernel_h", 2);
1256
lp.set("stride_w", 2);
1257
lp.set("stride_h", 2);
1258
lp.set("pad_w", 0);
1259
lp.set("pad_h", 0);
1260
lp.name = "testLayer.name"; // This test also checks that OpenCV lets use names with dots.
1261
lp.type = "Pooling";
1262
net.addLayerToPrev(lp.name, lp.type, lp);
1263
1264
Mat inp(10, 10, CV_8U);
1265
randu(inp, 0, 255);
1266
1267
Mat maxValues(5, 5, CV_32F, Scalar(-1)), indices(5, 5, CV_32F, Scalar(-1));
1268
for (int y = 0; y < 10; ++y)
1269
{
1270
int dstY = y / 2;
1271
for (int x = 0; x < 10; ++x)
1272
{
1273
int dstX = x / 2;
1274
uint8_t val = inp.at<uint8_t>(y, x);
1275
if ((float)inp.at<uint8_t>(y, x) > maxValues.at<float>(dstY, dstX))
1276
{
1277
maxValues.at<float>(dstY, dstX) = val;
1278
indices.at<float>(dstY, dstX) = y * 10 + x;
1279
}
1280
}
1281
}
1282
net.setPreferableBackend(DNN_BACKEND_OPENCV);
1283
net.setInput(blobFromImage(inp));
1284
1285
std::vector<Mat> outputs;
1286
net.forward(outputs, lp.name);
1287
normAssert(maxValues, outputs[0].reshape(1, 5));
1288
normAssert(indices, outputs[1].reshape(1, 5));
1289
}
1290
1291
typedef testing::TestWithParam<tuple<Vec4i, int, tuple<Backend, Target> > > Layer_Test_ShuffleChannel;
1292
TEST_P(Layer_Test_ShuffleChannel, Accuracy)
1293
{
1294
Vec4i inpShapeVec = get<0>(GetParam());
1295
int group = get<1>(GetParam());
1296
ASSERT_EQ(inpShapeVec[1] % group, 0);
1297
const int groupSize = inpShapeVec[1] / group;
1298
int backendId = get<0>(get<2>(GetParam()));
1299
int targetId = get<1>(get<2>(GetParam()));
1300
1301
Net net;
1302
LayerParams lp;
1303
lp.set("group", group);
1304
lp.type = "ShuffleChannel";
1305
lp.name = "testLayer";
1306
net.addLayerToPrev(lp.name, lp.type, lp);
1307
1308
const int inpShape[] = {inpShapeVec[0], inpShapeVec[1], inpShapeVec[2], inpShapeVec[3]};
1309
Mat inp(4, inpShape, CV_32F);
1310
randu(inp, 0, 255);
1311
1312
net.setInput(inp);
1313
net.setPreferableBackend(backendId);
1314
net.setPreferableTarget(targetId);
1315
Mat out = net.forward();
1316
1317
double l1 = (targetId == DNN_TARGET_OPENCL_FP16) ? 5e-2 : 1e-5;
1318
double lInf = (targetId == DNN_TARGET_OPENCL_FP16) ? 7e-2 : 1e-4;
1319
for (int n = 0; n < inpShapeVec[0]; ++n)
1320
{
1321
for (int c = 0; c < inpShapeVec[1]; ++c)
1322
{
1323
Mat outChannel = getPlane(out, n, c);
1324
Mat inpChannel = getPlane(inp, n, groupSize * (c % group) + c / group);
1325
normAssert(outChannel, inpChannel, "", l1, lInf);
1326
}
1327
}
1328
}
1329
INSTANTIATE_TEST_CASE_P(/**/, Layer_Test_ShuffleChannel, Combine(
1330
/*input shape*/ Values(Vec4i(1, 6, 5, 7), Vec4i(3, 12, 1, 4)),
1331
/*group*/ Values(1, 2, 3, 6), dnnBackendsAndTargets(/*with IE*/ false)
1332
));
1333
1334
// Check if relu is not fused to convolution if we requested it's output
1335
TEST(Layer_Test_Convolution, relu_fusion)
1336
{
1337
Net net;
1338
{
1339
LayerParams lp;
1340
lp.set("kernel_size", 1);
1341
lp.set("num_output", 1);
1342
lp.set("bias_term", false);
1343
lp.type = "Convolution";
1344
lp.name = "testConv";
1345
1346
int weightsShape[] = {1, 1, 1, 1};
1347
Mat weights(4, &weightsShape[0], CV_32F, Scalar(1));
1348
lp.blobs.push_back(weights);
1349
net.addLayerToPrev(lp.name, lp.type, lp);
1350
}
1351
{
1352
LayerParams lp;
1353
lp.type = "ReLU";
1354
lp.name = "testReLU";
1355
net.addLayerToPrev(lp.name, lp.type, lp);
1356
}
1357
int sz[] = {1, 1, 2, 3};
1358
Mat input(4, &sz[0], CV_32F);
1359
randu(input, -1.0, -0.1);
1360
net.setInput(input);
1361
net.setPreferableBackend(DNN_BACKEND_OPENCV);
1362
Mat output = net.forward("testConv");
1363
normAssert(input, output);
1364
}
1365
1366
}} // namespace
1367
1368