42
42
#include " test_precomp.hpp"
43
43
#include " npy_blob.hpp"
44
44
#include < opencv2/dnn/shape_utils.hpp>
45
- #include < opencv2/core/ocl.hpp>
46
- #include < opencv2/ts/ocl_test.hpp>
47
45
48
46
namespace opencv_test { namespace {
49
47
@@ -83,10 +81,10 @@ TEST(Test_Caffe, read_googlenet)
83
81
ASSERT_FALSE (net.empty ());
84
82
}
85
83
86
- typedef testing::TestWithParam<bool > Reproducibility_AlexNet;
84
+ typedef testing::TestWithParam<tuple< bool , DNNTarget> > Reproducibility_AlexNet;
87
85
TEST_P (Reproducibility_AlexNet, Accuracy)
88
86
{
89
- bool readFromMemory = GetParam ();
87
+ bool readFromMemory = get< 0 >( GetParam () );
90
88
Net net;
91
89
{
92
90
const string proto = findDataFile (" dnn/bvlc_alexnet.prototxt" , false );
@@ -106,42 +104,7 @@ TEST_P(Reproducibility_AlexNet, Accuracy)
106
104
ASSERT_FALSE (net.empty ());
107
105
}
108
106
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 ();
124
- Net net;
125
- {
126
- const string proto = findDataFile (" dnn/bvlc_alexnet.prototxt" , false );
127
- const string model = findDataFile (" dnn/bvlc_alexnet.caffemodel" , false );
128
- if (readFromMemory)
129
- {
130
- string dataProto;
131
- ASSERT_TRUE (readFileInMemory (proto, dataProto));
132
- string dataModel;
133
- ASSERT_TRUE (readFileInMemory (model, dataModel));
134
-
135
- net = readNetFromCaffe (dataProto.c_str (), dataProto.size (),
136
- dataModel.c_str (), dataModel.size ());
137
- }
138
- else
139
- net = readNetFromCaffe (proto, model);
140
- ASSERT_FALSE (net.empty ());
141
- }
142
-
143
- net.setPreferableBackend (DNN_BACKEND_DEFAULT);
144
- net.setPreferableTarget (DNN_TARGET_OPENCL);
107
+ net.setPreferableTarget (get<1 >(GetParam ()));
145
108
146
109
Mat sample = imread (_tf (" grace_hopper_227.png" ));
147
110
ASSERT_TRUE (!sample.empty ());
@@ -152,7 +115,7 @@ OCL_TEST_P(Reproducibility_OCL_AlexNet, Accuracy)
152
115
normAssert (ref, out);
153
116
}
154
117
155
- OCL_INSTANTIATE_TEST_CASE_P (Test_Caffe, Reproducibility_OCL_AlexNet, testing::Bool());
118
+ INSTANTIATE_TEST_CASE_P ( /* */ , Reproducibility_AlexNet, Combine( testing::Bool(), availableDnnTargets() ));
156
119
157
120
#if !defined(_WIN32) || defined(_WIN64)
158
121
TEST (Reproducibility_FCN, Accuracy)
@@ -207,43 +170,14 @@ TEST(Reproducibility_SSD, Accuracy)
207
170
normAssert (ref, out);
208
171
}
209
172
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)
173
+ typedef testing::TestWithParam<DNNTarget> Reproducibility_MobileNet_SSD;
174
+ TEST_P (Reproducibility_MobileNet_SSD, Accuracy)
240
175
{
241
176
const string proto = findDataFile (" dnn/MobileNetSSD_deploy.prototxt" , false );
242
177
const string model = findDataFile (" dnn/MobileNetSSD_deploy.caffemodel" , false );
243
178
Net net = readNetFromCaffe (proto, model);
244
179
245
- net.setPreferableBackend (DNN_BACKEND_DEFAULT);
246
- net.setPreferableTarget (DNN_TARGET_OPENCL);
180
+ net.setPreferableTarget (GetParam ());
247
181
248
182
Mat sample = imread (_tf (" street.png" ));
249
183
@@ -258,38 +192,39 @@ OCL_TEST(Reproducibility_MobileNet_SSD, Accuracy)
258
192
inp.setTo (0 .0f );
259
193
net.setInput (inp);
260
194
out = net.forward ();
195
+ out = out.reshape (1 , out.total () / 7 );
261
196
262
- const int numDetections = out.size [ 2 ] ;
197
+ const int numDetections = out.rows ;
263
198
ASSERT_NE (numDetections, 0 );
264
199
for (int i = 0 ; i < numDetections; ++i)
265
200
{
266
- float confidence = out.ptr <float >(0 , 0 , i)[2 ];
201
+ float confidence = out.ptr <float >(i)[2 ];
267
202
ASSERT_EQ (confidence, 0 );
268
203
}
269
- }
270
-
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
204
279
- net.setInput (input);
280
- Mat out = net.forward ();
281
-
282
- Mat ref = blobFromNPY (_tf (" resnet50_prob.npy" ));
283
- normAssert (ref, out);
205
+ // Check batching mode.
206
+ ref = ref.reshape (1 , numDetections);
207
+ inp = blobFromImages (std::vector<Mat>(2 , sample), 1 .0f / 127.5 , Size (300 , 300 ), Scalar (127.5 , 127.5 , 127.5 ), false );
208
+ net.setInput (inp);
209
+ Mat outBatch = net.forward ();
210
+
211
+ // Output blob has a shape 1x1x2Nx7 where N is a number of detection for
212
+ // a single sample in batch. The first numbers of detection vectors are batch id.
213
+ outBatch = outBatch.reshape (1 , outBatch.total () / 7 );
214
+ EXPECT_EQ (outBatch.rows , 2 * numDetections);
215
+ normAssert (outBatch.rowRange (0 , numDetections), ref);
216
+ normAssert (outBatch.rowRange (numDetections, 2 * numDetections).colRange (1 , 7 ), ref.colRange (1 , 7 ));
284
217
}
218
+ INSTANTIATE_TEST_CASE_P (/* */ , Reproducibility_MobileNet_SSD, availableDnnTargets());
285
219
286
- OCL_TEST (Reproducibility_ResNet50, Accuracy)
220
+ typedef testing::TestWithParam<DNNTarget> Reproducibility_ResNet50;
221
+ TEST_P (Reproducibility_ResNet50, Accuracy)
287
222
{
288
223
Net net = readNetFromCaffe (findDataFile (" dnn/ResNet-50-deploy.prototxt" , false ),
289
224
findDataFile (" dnn/ResNet-50-model.caffemodel" , false ));
290
225
291
- net. setPreferableBackend (DNN_BACKEND_DEFAULT );
292
- net.setPreferableTarget (DNN_TARGET_OPENCL );
226
+ int targetId = GetParam ( );
227
+ net.setPreferableTarget (targetId );
293
228
294
229
Mat input = blobFromImage (imread (_tf (" googlenet_0.png" )), 1 .0f , Size (224 ,224 ), Scalar (), false );
295
230
ASSERT_TRUE (!input.empty ());
@@ -300,52 +235,46 @@ OCL_TEST(Reproducibility_ResNet50, Accuracy)
300
235
Mat ref = blobFromNPY (_tf (" resnet50_prob.npy" ));
301
236
normAssert (ref, out);
302
237
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 ();
238
+ if (targetId == DNN_TARGET_OPENCL)
239
+ {
240
+ UMat out_umat;
241
+ net.forward (out_umat);
242
+ normAssert (ref, out_umat, " out_umat" );
322
243
323
- Mat ref = blobFromNPY (_tf (" squeezenet_v1.1_prob.npy" ));
324
- normAssert (ref, out);
244
+ std::vector<UMat> out_umats;
245
+ net.forward (out_umats);
246
+ normAssert (ref, out_umats[0 ], " out_umat_vector" );
247
+ }
325
248
}
249
+ INSTANTIATE_TEST_CASE_P (/* */ , Reproducibility_ResNet50, availableDnnTargets());
326
250
327
- OCL_TEST (Reproducibility_SqueezeNet_v1_1, Accuracy)
251
+ typedef testing::TestWithParam<DNNTarget> Reproducibility_SqueezeNet_v1_1;
252
+ TEST_P (Reproducibility_SqueezeNet_v1_1, Accuracy)
328
253
{
329
254
Net net = readNetFromCaffe (findDataFile (" dnn/squeezenet_v1.1.prototxt" , false ),
330
255
findDataFile (" dnn/squeezenet_v1.1.caffemodel" , false ));
331
256
332
- net. setPreferableBackend (DNN_BACKEND_DEFAULT );
333
- net.setPreferableTarget (DNN_TARGET_OPENCL );
257
+ int targetId = GetParam ( );
258
+ net.setPreferableTarget (targetId );
334
259
335
260
Mat input = blobFromImage (imread (_tf (" googlenet_0.png" )), 1 .0f , Size (227 ,227 ), Scalar (), false );
336
261
ASSERT_TRUE (!input.empty ());
337
262
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.
263
+ Mat out;
264
+ if (targetId == DNN_TARGET_OPENCL)
265
+ {
266
+ // Firstly set a wrong input blob and run the model to receive a wrong output.
267
+ // Then set a correct input blob to check CPU->GPU synchronization is working well.
268
+ net.setInput (input * 2 .0f );
269
+ out = net.forward ();
270
+ }
343
271
net.setInput (input);
344
272
out = net.forward ();
345
273
346
274
Mat ref = blobFromNPY (_tf (" squeezenet_v1.1_prob.npy" ));
347
275
normAssert (ref, out);
348
276
}
277
+ INSTANTIATE_TEST_CASE_P (/* */ , Reproducibility_SqueezeNet_v1_1, availableDnnTargets());
349
278
350
279
TEST (Reproducibility_AlexNet_fp16, Accuracy)
351
280
{
@@ -456,7 +385,6 @@ TEST(Test_Caffe, multiple_inputs)
456
385
normAssert (out, first_image + second_image);
457
386
}
458
387
459
- CV_ENUM (DNNTarget, DNN_TARGET_CPU, DNN_TARGET_OPENCL)
460
388
typedef testing::TestWithParam<tuple<std::string, DNNTarget> > opencv_face_detector;
461
389
TEST_P (opencv_face_detector, Accuracy)
462
390
{
0 commit comments