47
47
48
48
namespace opencv_test { namespace {
49
49
50
+ CV_ENUM (DNNTarget, DNN_TARGET_CPU, DNN_TARGET_OPENCL)
51
+ static testing::internal::ParamGenerator<DNNTarget> availableBackends ()
52
+ {
53
+ static std::vector<DNNTarget> targets;
54
+ if (targets.empty ())
55
+ {
56
+ targets.push_back (DNN_TARGET_CPU);
57
+ #ifdef HAVE_OPENCL
58
+ if (cv::ocl::useOpenCL ())
59
+ targets.push_back (DNN_TARGET_OPENCL);
60
+ #endif
61
+ }
62
+ return testing::ValuesIn (targets);
63
+ }
64
+
50
65
template <typename TString>
51
66
static std::string _tf (TString filename)
52
67
{
@@ -83,44 +98,10 @@ TEST(Test_Caffe, read_googlenet)
83
98
ASSERT_FALSE (net.empty ());
84
99
}
85
100
86
- typedef testing::TestWithParam<bool > Reproducibility_AlexNet;
101
+ typedef testing::TestWithParam<tuple< bool , DNNTarget> > Reproducibility_AlexNet;
87
102
TEST_P (Reproducibility_AlexNet, Accuracy)
88
103
{
89
- bool readFromMemory = GetParam ();
90
- Net net;
91
- {
92
- const string proto = findDataFile (" dnn/bvlc_alexnet.prototxt" , false );
93
- const string model = findDataFile (" dnn/bvlc_alexnet.caffemodel" , false );
94
- if (readFromMemory)
95
- {
96
- string dataProto;
97
- ASSERT_TRUE (readFileInMemory (proto, dataProto));
98
- string dataModel;
99
- ASSERT_TRUE (readFileInMemory (model, dataModel));
100
-
101
- net = readNetFromCaffe (dataProto.c_str (), dataProto.size (),
102
- dataModel.c_str (), dataModel.size ());
103
- }
104
- else
105
- net = readNetFromCaffe (proto, model);
106
- ASSERT_FALSE (net.empty ());
107
- }
108
-
109
- Mat sample = imread (_tf (" grace_hopper_227.png" ));
110
- ASSERT_TRUE (!sample.empty ());
111
-
112
- net.setInput (blobFromImage (sample, 1 .0f , Size (227 , 227 ), Scalar (), false ), " data" );
113
- Mat out = net.forward (" prob" );
114
- Mat ref = blobFromNPY (_tf (" caffe_alexnet_prob.npy" ));
115
- normAssert (ref, out);
116
- }
117
-
118
- INSTANTIATE_TEST_CASE_P (Test_Caffe, Reproducibility_AlexNet, testing::Bool());
119
-
120
- typedef testing::TestWithParam<bool > Reproducibility_OCL_AlexNet;
121
- OCL_TEST_P (Reproducibility_OCL_AlexNet, Accuracy)
122
- {
123
- bool readFromMemory = GetParam ();
104
+ bool readFromMemory = get<0 >(GetParam ());
124
105
Net net;
125
106
{
126
107
const string proto = findDataFile (" dnn/bvlc_alexnet.prototxt" , false );
@@ -140,8 +121,7 @@ OCL_TEST_P(Reproducibility_OCL_AlexNet, Accuracy)
140
121
ASSERT_FALSE (net.empty ());
141
122
}
142
123
143
- net.setPreferableBackend (DNN_BACKEND_DEFAULT);
144
- net.setPreferableTarget (DNN_TARGET_OPENCL);
124
+ net.setPreferableTarget (get<1 >(GetParam ()));
145
125
146
126
Mat sample = imread (_tf (" grace_hopper_227.png" ));
147
127
ASSERT_TRUE (!sample.empty ());
@@ -152,7 +132,7 @@ OCL_TEST_P(Reproducibility_OCL_AlexNet, Accuracy)
152
132
normAssert (ref, out);
153
133
}
154
134
155
- OCL_INSTANTIATE_TEST_CASE_P (Test_Caffe, Reproducibility_OCL_AlexNet, testing::Bool());
135
+ INSTANTIATE_TEST_CASE_P ( /* */ , Reproducibility_AlexNet, Combine( testing::Bool(), availableBackends() ));
156
136
157
137
#if !defined(_WIN32) || defined(_WIN64)
158
138
TEST (Reproducibility_FCN, Accuracy)
@@ -207,43 +187,14 @@ TEST(Reproducibility_SSD, Accuracy)
207
187
normAssert (ref, out);
208
188
}
209
189
210
- TEST (Reproducibility_MobileNet_SSD, Accuracy)
211
- {
212
- const string proto = findDataFile (" dnn/MobileNetSSD_deploy.prototxt" , false );
213
- const string model = findDataFile (" dnn/MobileNetSSD_deploy.caffemodel" , false );
214
- Net net = readNetFromCaffe (proto, model);
215
-
216
- Mat sample = imread (_tf (" street.png" ));
217
-
218
- Mat inp = blobFromImage (sample, 1 .0f / 127.5 , Size (300 , 300 ), Scalar (127.5 , 127.5 , 127.5 ), false );
219
- net.setInput (inp);
220
- Mat out = net.forward ();
221
-
222
- Mat ref = blobFromNPY (_tf (" mobilenet_ssd_caffe_out.npy" ));
223
- normAssert (ref, out);
224
-
225
- // Check that detections aren't preserved.
226
- inp.setTo (0 .0f );
227
- net.setInput (inp);
228
- out = net.forward ();
229
-
230
- const int numDetections = out.size [2 ];
231
- ASSERT_NE (numDetections, 0 );
232
- for (int i = 0 ; i < numDetections; ++i)
233
- {
234
- float confidence = out.ptr <float >(0 , 0 , i)[2 ];
235
- ASSERT_EQ (confidence, 0 );
236
- }
237
- }
238
-
239
- OCL_TEST (Reproducibility_MobileNet_SSD, Accuracy)
190
+ typedef testing::TestWithParam<DNNTarget> Reproducibility_MobileNet_SSD;
191
+ TEST_P (Reproducibility_MobileNet_SSD, Accuracy)
240
192
{
241
193
const string proto = findDataFile (" dnn/MobileNetSSD_deploy.prototxt" , false );
242
194
const string model = findDataFile (" dnn/MobileNetSSD_deploy.caffemodel" , false );
243
195
Net net = readNetFromCaffe (proto, model);
244
196
245
- net.setPreferableBackend (DNN_BACKEND_DEFAULT);
246
- net.setPreferableTarget (DNN_TARGET_OPENCL);
197
+ net.setPreferableTarget (GetParam ());
247
198
248
199
Mat sample = imread (_tf (" street.png" ));
249
200
@@ -258,38 +209,39 @@ OCL_TEST(Reproducibility_MobileNet_SSD, Accuracy)
258
209
inp.setTo (0 .0f );
259
210
net.setInput (inp);
260
211
out = net.forward ();
212
+ out = out.reshape (1 , out.total () / 7 );
261
213
262
- const int numDetections = out.size [ 2 ] ;
214
+ const int numDetections = out.rows ;
263
215
ASSERT_NE (numDetections, 0 );
264
216
for (int i = 0 ; i < numDetections; ++i)
265
217
{
266
- float confidence = out.ptr <float >(0 , 0 , i)[2 ];
218
+ float confidence = out.ptr <float >(i)[2 ];
267
219
ASSERT_EQ (confidence, 0 );
268
220
}
269
- }
270
221
271
- TEST (Reproducibility_ResNet50, Accuracy)
272
- {
273
- Net net = readNetFromCaffe (findDataFile (" dnn/ResNet-50-deploy.prototxt" , false ),
274
- findDataFile (" dnn/ResNet-50-model.caffemodel" , false ));
275
-
276
- Mat input = blobFromImage (imread (_tf (" googlenet_0.png" )), 1 .0f , Size (224 ,224 ), Scalar (), false );
277
- ASSERT_TRUE (!input.empty ());
278
-
279
- net.setInput (input);
280
- Mat out = net.forward ();
281
-
282
- Mat ref = blobFromNPY (_tf (" resnet50_prob.npy" ));
283
- normAssert (ref, out);
222
+ // Check batching mode.
223
+ ref = ref.reshape (1 , numDetections);
224
+ inp = blobFromImages (std::vector<Mat>(2 , sample), 1 .0f / 127.5 , Size (300 , 300 ), Scalar (127.5 , 127.5 , 127.5 ), false );
225
+ net.setInput (inp);
226
+ Mat outBatch = net.forward ();
227
+
228
+ // Output blob has a shape 1x1x2Nx7 where N is a number of detection for
229
+ // a single sample in batch. The first numbers of detection vectors are batch id.
230
+ outBatch = outBatch.reshape (1 , outBatch.total () / 7 );
231
+ EXPECT_EQ (outBatch.rows , 2 * numDetections);
232
+ normAssert (outBatch.rowRange (0 , numDetections), ref);
233
+ normAssert (outBatch.rowRange (numDetections, 2 * numDetections).colRange (1 , 7 ), ref.colRange (1 , 7 ));
284
234
}
235
+ INSTANTIATE_TEST_CASE_P (/* */ , Reproducibility_MobileNet_SSD, availableBackends());
285
236
286
- OCL_TEST (Reproducibility_ResNet50, Accuracy)
237
+ typedef testing::TestWithParam<DNNTarget> Reproducibility_ResNet50;
238
+ TEST_P (Reproducibility_ResNet50, Accuracy)
287
239
{
288
240
Net net = readNetFromCaffe (findDataFile (" dnn/ResNet-50-deploy.prototxt" , false ),
289
241
findDataFile (" dnn/ResNet-50-model.caffemodel" , false ));
290
242
291
- net. setPreferableBackend (DNN_BACKEND_DEFAULT );
292
- net.setPreferableTarget (DNN_TARGET_OPENCL );
243
+ int targetId = GetParam ( );
244
+ net.setPreferableTarget (targetId );
293
245
294
246
Mat input = blobFromImage (imread (_tf (" googlenet_0.png" )), 1 .0f , Size (224 ,224 ), Scalar (), false );
295
247
ASSERT_TRUE (!input.empty ());
@@ -300,52 +252,46 @@ OCL_TEST(Reproducibility_ResNet50, Accuracy)
300
252
Mat ref = blobFromNPY (_tf (" resnet50_prob.npy" ));
301
253
normAssert (ref, out);
302
254
303
- UMat out_umat;
304
- net.forward (out_umat);
305
- normAssert (ref, out_umat, " out_umat" );
306
-
307
- std::vector<UMat> out_umats;
308
- net.forward (out_umats);
309
- normAssert (ref, out_umats[0 ], " out_umat_vector" );
310
- }
311
-
312
- TEST (Reproducibility_SqueezeNet_v1_1, Accuracy)
313
- {
314
- Net net = readNetFromCaffe (findDataFile (" dnn/squeezenet_v1.1.prototxt" , false ),
315
- findDataFile (" dnn/squeezenet_v1.1.caffemodel" , false ));
316
-
317
- Mat input = blobFromImage (imread (_tf (" googlenet_0.png" )), 1 .0f , Size (227 ,227 ), Scalar (), false );
318
- ASSERT_TRUE (!input.empty ());
319
-
320
- net.setInput (input);
321
- Mat out = net.forward ();
255
+ if (targetId == DNN_TARGET_OPENCL)
256
+ {
257
+ UMat out_umat;
258
+ net.forward (out_umat);
259
+ normAssert (ref, out_umat, " out_umat" );
322
260
323
- Mat ref = blobFromNPY (_tf (" squeezenet_v1.1_prob.npy" ));
324
- normAssert (ref, out);
261
+ std::vector<UMat> out_umats;
262
+ net.forward (out_umats);
263
+ normAssert (ref, out_umats[0 ], " out_umat_vector" );
264
+ }
325
265
}
266
+ INSTANTIATE_TEST_CASE_P (/* */ , Reproducibility_ResNet50, availableBackends());
326
267
327
- OCL_TEST (Reproducibility_SqueezeNet_v1_1, Accuracy)
268
+ typedef testing::TestWithParam<DNNTarget> Reproducibility_SqueezeNet_v1_1;
269
+ TEST_P (Reproducibility_SqueezeNet_v1_1, Accuracy)
328
270
{
329
271
Net net = readNetFromCaffe (findDataFile (" dnn/squeezenet_v1.1.prototxt" , false ),
330
272
findDataFile (" dnn/squeezenet_v1.1.caffemodel" , false ));
331
273
332
- net. setPreferableBackend (DNN_BACKEND_DEFAULT );
333
- net.setPreferableTarget (DNN_TARGET_OPENCL );
274
+ int targetId = GetParam ( );
275
+ net.setPreferableTarget (targetId );
334
276
335
277
Mat input = blobFromImage (imread (_tf (" googlenet_0.png" )), 1 .0f , Size (227 ,227 ), Scalar (), false );
336
278
ASSERT_TRUE (!input.empty ());
337
279
338
- // Firstly set a wrong input blob and run the model to receive a wrong output.
339
- net.setInput (input * 2 .0f );
340
- Mat out = net.forward ();
341
-
342
- // Then set a correct input blob to check CPU->GPU synchronization is working well.
280
+ Mat out;
281
+ if (targetId == DNN_TARGET_OPENCL)
282
+ {
283
+ // Firstly set a wrong input blob and run the model to receive a wrong output.
284
+ // Then set a correct input blob to check CPU->GPU synchronization is working well.
285
+ net.setInput (input * 2 .0f );
286
+ out = net.forward ();
287
+ }
343
288
net.setInput (input);
344
289
out = net.forward ();
345
290
346
291
Mat ref = blobFromNPY (_tf (" squeezenet_v1.1_prob.npy" ));
347
292
normAssert (ref, out);
348
293
}
294
+ INSTANTIATE_TEST_CASE_P (/* */ , Reproducibility_SqueezeNet_v1_1, availableBackends());
349
295
350
296
TEST (Reproducibility_AlexNet_fp16, Accuracy)
351
297
{
@@ -456,7 +402,6 @@ TEST(Test_Caffe, multiple_inputs)
456
402
normAssert (out, first_image + second_image);
457
403
}
458
404
459
- CV_ENUM (DNNTarget, DNN_TARGET_CPU, DNN_TARGET_OPENCL)
460
405
typedef testing::TestWithParam<tuple<std::string, DNNTarget> > opencv_face_detector;
461
406
TEST_P (opencv_face_detector, Accuracy)
462
407
{
0 commit comments