Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/dnn/test/test_backends.cpp
16354 views
1
// This file is part of OpenCV project.
2
// It is subject to the license terms in the LICENSE file found in the top-level directory
3
// of this distribution and at http://opencv.org/license.html.
4
//
5
// Copyright (C) 2018, Intel Corporation, all rights reserved.
6
// Third party copyrights are property of their respective owners.
7
8
#include "test_precomp.hpp"
9
#include "opencv2/core/ocl.hpp"
10
11
namespace opencv_test { namespace {
12
13
class DNNTestNetwork : public DNNTestLayer
14
{
15
public:
16
void processNet(const std::string& weights, const std::string& proto,
17
Size inpSize, const std::string& outputLayer = "",
18
const std::string& halideScheduler = "",
19
double l1 = 0.0, double lInf = 0.0)
20
{
21
// Create a common input blob.
22
int blobSize[] = {1, 3, inpSize.height, inpSize.width};
23
Mat inp(4, blobSize, CV_32FC1);
24
randu(inp, 0.0f, 1.0f);
25
26
processNet(weights, proto, inp, outputLayer, halideScheduler, l1, lInf);
27
}
28
29
void processNet(std::string weights, std::string proto,
30
Mat inp, const std::string& outputLayer = "",
31
std::string halideScheduler = "",
32
double l1 = 0.0, double lInf = 0.0, double detectionConfThresh = 0.2)
33
{
34
checkBackend();
35
l1 = l1 ? l1 : default_l1;
36
lInf = lInf ? lInf : default_lInf;
37
38
weights = findDataFile(weights, false);
39
if (!proto.empty())
40
proto = findDataFile(proto, false);
41
42
// Create two networks - with default backend and target and a tested one.
43
Net netDefault = readNet(weights, proto);
44
netDefault.setPreferableBackend(DNN_BACKEND_OPENCV);
45
netDefault.setInput(inp);
46
Mat outDefault = netDefault.forward(outputLayer).clone();
47
48
Net net = readNet(weights, proto);
49
net.setInput(inp);
50
net.setPreferableBackend(backend);
51
net.setPreferableTarget(target);
52
if (backend == DNN_BACKEND_HALIDE && !halideScheduler.empty())
53
{
54
halideScheduler = findDataFile(halideScheduler, false);
55
net.setHalideScheduler(halideScheduler);
56
}
57
Mat out = net.forward(outputLayer).clone();
58
59
check(outDefault, out, outputLayer, l1, lInf, detectionConfThresh, "First run");
60
61
// Test 2: change input.
62
float* inpData = (float*)inp.data;
63
for (int i = 0; i < inp.size[0] * inp.size[1]; ++i)
64
{
65
Mat slice(inp.size[2], inp.size[3], CV_32F, inpData);
66
cv::flip(slice, slice, 1);
67
inpData += slice.total();
68
}
69
netDefault.setInput(inp);
70
net.setInput(inp);
71
outDefault = netDefault.forward(outputLayer).clone();
72
out = net.forward(outputLayer).clone();
73
check(outDefault, out, outputLayer, l1, lInf, detectionConfThresh, "Second run");
74
}
75
76
void check(Mat& ref, Mat& out, const std::string& outputLayer, double l1, double lInf,
77
double detectionConfThresh, const char* msg)
78
{
79
if (outputLayer == "detection_out")
80
{
81
if (backend == DNN_BACKEND_INFERENCE_ENGINE)
82
{
83
// Inference Engine produces detections terminated by a row which starts from -1.
84
out = out.reshape(1, out.total() / 7);
85
int numDetections = 0;
86
while (numDetections < out.rows && out.at<float>(numDetections, 0) != -1)
87
{
88
numDetections += 1;
89
}
90
out = out.rowRange(0, numDetections);
91
}
92
normAssertDetections(ref, out, msg, detectionConfThresh, l1, lInf);
93
}
94
else
95
normAssert(ref, out, msg, l1, lInf);
96
}
97
};
98
99
TEST_P(DNNTestNetwork, AlexNet)
100
{
101
processNet("dnn/bvlc_alexnet.caffemodel", "dnn/bvlc_alexnet.prototxt",
102
Size(227, 227), "prob",
103
target == DNN_TARGET_OPENCL ? "dnn/halide_scheduler_opencl_alexnet.yml" :
104
"dnn/halide_scheduler_alexnet.yml");
105
}
106
107
TEST_P(DNNTestNetwork, ResNet_50)
108
{
109
processNet("dnn/ResNet-50-model.caffemodel", "dnn/ResNet-50-deploy.prototxt",
110
Size(224, 224), "prob",
111
target == DNN_TARGET_OPENCL ? "dnn/halide_scheduler_opencl_resnet_50.yml" :
112
"dnn/halide_scheduler_resnet_50.yml");
113
}
114
115
TEST_P(DNNTestNetwork, SqueezeNet_v1_1)
116
{
117
processNet("dnn/squeezenet_v1.1.caffemodel", "dnn/squeezenet_v1.1.prototxt",
118
Size(227, 227), "prob",
119
target == DNN_TARGET_OPENCL ? "dnn/halide_scheduler_opencl_squeezenet_v1_1.yml" :
120
"dnn/halide_scheduler_squeezenet_v1_1.yml");
121
}
122
123
TEST_P(DNNTestNetwork, GoogLeNet)
124
{
125
processNet("dnn/bvlc_googlenet.caffemodel", "dnn/bvlc_googlenet.prototxt",
126
Size(224, 224), "prob");
127
}
128
129
TEST_P(DNNTestNetwork, Inception_5h)
130
{
131
if (backend == DNN_BACKEND_INFERENCE_ENGINE) throw SkipTestException("");
132
processNet("dnn/tensorflow_inception_graph.pb", "", Size(224, 224), "softmax2",
133
target == DNN_TARGET_OPENCL ? "dnn/halide_scheduler_opencl_inception_5h.yml" :
134
"dnn/halide_scheduler_inception_5h.yml");
135
}
136
137
TEST_P(DNNTestNetwork, ENet)
138
{
139
if ((backend == DNN_BACKEND_INFERENCE_ENGINE) ||
140
(backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16))
141
throw SkipTestException("");
142
processNet("dnn/Enet-model-best.net", "", Size(512, 512), "l367_Deconvolution",
143
target == DNN_TARGET_OPENCL ? "dnn/halide_scheduler_opencl_enet.yml" :
144
"dnn/halide_scheduler_enet.yml",
145
2e-5, 0.15);
146
}
147
148
TEST_P(DNNTestNetwork, MobileNet_SSD_Caffe)
149
{
150
if (backend == DNN_BACKEND_HALIDE)
151
throw SkipTestException("");
152
Mat sample = imread(findDataFile("dnn/street.png", false));
153
Mat inp = blobFromImage(sample, 1.0f / 127.5, Size(300, 300), Scalar(127.5, 127.5, 127.5), false);
154
float diffScores = (target == DNN_TARGET_OPENCL_FP16) ? 6e-3 : 0.0;
155
processNet("dnn/MobileNetSSD_deploy.caffemodel", "dnn/MobileNetSSD_deploy.prototxt",
156
inp, "detection_out", "", diffScores);
157
}
158
159
TEST_P(DNNTestNetwork, MobileNet_SSD_v1_TensorFlow)
160
{
161
if (backend == DNN_BACKEND_HALIDE)
162
throw SkipTestException("");
163
Mat sample = imread(findDataFile("dnn/street.png", false));
164
Mat inp = blobFromImage(sample, 1.0f, Size(300, 300), Scalar(), false);
165
float l1 = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.011 : 0.0;
166
float lInf = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.06 : 0.0;
167
processNet("dnn/ssd_mobilenet_v1_coco_2017_11_17.pb", "dnn/ssd_mobilenet_v1_coco_2017_11_17.pbtxt",
168
inp, "detection_out", "", l1, lInf);
169
}
170
171
TEST_P(DNNTestNetwork, MobileNet_SSD_v2_TensorFlow)
172
{
173
if (backend == DNN_BACKEND_HALIDE)
174
throw SkipTestException("");
175
Mat sample = imread(findDataFile("dnn/street.png", false));
176
Mat inp = blobFromImage(sample, 1.0f, Size(300, 300), Scalar(), false);
177
float l1 = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.011 : 0.0;
178
float lInf = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.062 : 0.0;
179
processNet("dnn/ssd_mobilenet_v2_coco_2018_03_29.pb", "dnn/ssd_mobilenet_v2_coco_2018_03_29.pbtxt",
180
inp, "detection_out", "", l1, lInf, 0.25);
181
}
182
183
TEST_P(DNNTestNetwork, SSD_VGG16)
184
{
185
if (backend == DNN_BACKEND_HALIDE && target == DNN_TARGET_CPU)
186
throw SkipTestException("");
187
double scoreThreshold = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.0252 : 0.0;
188
Mat sample = imread(findDataFile("dnn/street.png", false));
189
Mat inp = blobFromImage(sample, 1.0f, Size(300, 300), Scalar(), false);
190
processNet("dnn/VGG_ILSVRC2016_SSD_300x300_iter_440000.caffemodel",
191
"dnn/ssd_vgg16.prototxt", inp, "detection_out", "", scoreThreshold);
192
}
193
194
TEST_P(DNNTestNetwork, OpenPose_pose_coco)
195
{
196
if (backend == DNN_BACKEND_HALIDE ||
197
backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)
198
throw SkipTestException("");
199
processNet("dnn/openpose_pose_coco.caffemodel", "dnn/openpose_pose_coco.prototxt",
200
Size(368, 368));
201
}
202
203
TEST_P(DNNTestNetwork, OpenPose_pose_mpi)
204
{
205
if (backend == DNN_BACKEND_HALIDE ||
206
backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)
207
throw SkipTestException("");
208
processNet("dnn/openpose_pose_mpi.caffemodel", "dnn/openpose_pose_mpi.prototxt",
209
Size(368, 368));
210
}
211
212
TEST_P(DNNTestNetwork, OpenPose_pose_mpi_faster_4_stages)
213
{
214
if (backend == DNN_BACKEND_HALIDE ||
215
backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)
216
throw SkipTestException("");
217
// The same .caffemodel but modified .prototxt
218
// See https://github.com/CMU-Perceptual-Computing-Lab/openpose/blob/master/src/openpose/pose/poseParameters.cpp
219
processNet("dnn/openpose_pose_mpi.caffemodel", "dnn/openpose_pose_mpi_faster_4_stages.prototxt",
220
Size(368, 368));
221
}
222
223
TEST_P(DNNTestNetwork, OpenFace)
224
{
225
#if defined(INF_ENGINE_RELEASE) && INF_ENGINE_RELEASE < 2018030000
226
if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)
227
throw SkipTestException("Test is enabled starts from OpenVINO 2018R3");
228
#endif
229
if (backend == DNN_BACKEND_HALIDE ||
230
(backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_OPENCL_FP16))
231
throw SkipTestException("");
232
processNet("dnn/openface_nn4.small2.v1.t7", "", Size(96, 96), "");
233
}
234
235
TEST_P(DNNTestNetwork, opencv_face_detector)
236
{
237
if (backend == DNN_BACKEND_HALIDE)
238
throw SkipTestException("");
239
Mat img = imread(findDataFile("gpu/lbpcascade/er.png", false));
240
Mat inp = blobFromImage(img, 1.0, Size(), Scalar(104.0, 177.0, 123.0), false, false);
241
processNet("dnn/opencv_face_detector.caffemodel", "dnn/opencv_face_detector.prototxt",
242
inp, "detection_out");
243
}
244
245
TEST_P(DNNTestNetwork, Inception_v2_SSD_TensorFlow)
246
{
247
if (backend == DNN_BACKEND_HALIDE)
248
throw SkipTestException("");
249
Mat sample = imread(findDataFile("dnn/street.png", false));
250
Mat inp = blobFromImage(sample, 1.0f, Size(300, 300), Scalar(), false);
251
float l1 = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.015 : 0.0;
252
float lInf = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.0731 : 0.0;
253
processNet("dnn/ssd_inception_v2_coco_2017_11_17.pb", "dnn/ssd_inception_v2_coco_2017_11_17.pbtxt",
254
inp, "detection_out", "", l1, lInf);
255
}
256
257
TEST_P(DNNTestNetwork, DenseNet_121)
258
{
259
if (backend == DNN_BACKEND_HALIDE)
260
throw SkipTestException("");
261
262
float l1 = 0.0, lInf = 0.0;
263
if (target == DNN_TARGET_OPENCL_FP16)
264
{
265
l1 = 9e-3; lInf = 5e-2;
266
}
267
else if (target == DNN_TARGET_MYRIAD)
268
{
269
l1 = 6e-2; lInf = 0.27;
270
}
271
processNet("dnn/DenseNet_121.caffemodel", "dnn/DenseNet_121.prototxt", Size(224, 224), "", "", l1, lInf);
272
}
273
274
TEST_P(DNNTestNetwork, FastNeuralStyle_eccv16)
275
{
276
if (backend == DNN_BACKEND_HALIDE ||
277
(backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16) ||
278
(backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD))
279
throw SkipTestException("");
280
Mat img = imread(findDataFile("dnn/googlenet_1.png", false));
281
Mat inp = blobFromImage(img, 1.0, Size(320, 240), Scalar(103.939, 116.779, 123.68), false, false);
282
// Output image has values in range [-143.526, 148.539].
283
float l1 = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.3 : 4e-5;
284
float lInf = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 7.0 : 2e-3;
285
processNet("dnn/fast_neural_style_eccv16_starry_night.t7", "", inp, "", "", l1, lInf);
286
}
287
288
INSTANTIATE_TEST_CASE_P(/*nothing*/, DNNTestNetwork, dnnBackendsAndTargets(true, true, false, true));
289
290
}} // namespace
291
292