Skip to content

Commit e1c3237

Browse files
committed
Parametric OpenCL deep learning tests
1 parent 667f5b6 commit e1c3237

File tree

4 files changed

+270
-480
lines changed

4 files changed

+270
-480
lines changed

modules/dnn/src/layers/softmax_layer.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,18 @@ class SoftMaxLayerImpl : public SoftmaxLayer
9393
}
9494

9595
#ifdef HAVE_OPENCL
96+
virtual void finalize(const std::vector<Mat*> &inputs, std::vector<Mat> &outputs)
97+
{
98+
OCL4DNNSoftmaxConfig config;
99+
100+
config.in_shape = shape(*inputs[0]);
101+
config.axis = axisRaw;
102+
config.channels = inputs[0]->size[axisRaw];
103+
config.logsoftmax = logSoftMax;
104+
105+
softmaxOp = Ptr<OCL4DNNSoftmax<float> >(new OCL4DNNSoftmax<float>(config));
106+
}
107+
96108
bool forward_ocl(InputArrayOfArrays inps, OutputArrayOfArrays outs, OutputArrayOfArrays itns)
97109
{
98110
std::vector<UMat> inputs;
@@ -103,18 +115,6 @@ class SoftMaxLayerImpl : public SoftmaxLayer
103115
outs.getUMatVector(outputs);
104116
itns.getUMatVector(internals);
105117

106-
if (softmaxOp.empty())
107-
{
108-
OCL4DNNSoftmaxConfig config;
109-
110-
config.in_shape = shape(inputs[0]);
111-
config.axis = axisRaw;
112-
config.channels = inputs[0].size[axisRaw];
113-
config.logsoftmax = logSoftMax;
114-
115-
softmaxOp = Ptr<OCL4DNNSoftmax<float> >(new OCL4DNNSoftmax<float>(config));
116-
}
117-
118118
UMat& src = inputs[0];
119119
UMat& dstMat = outputs[0];
120120

modules/dnn/test/test_caffe_importer.cpp

Lines changed: 65 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@
4747

4848
namespace opencv_test { namespace {
4949

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+
5065
template<typename TString>
5166
static std::string _tf(TString filename)
5267
{
@@ -83,44 +98,10 @@ TEST(Test_Caffe, read_googlenet)
8398
ASSERT_FALSE(net.empty());
8499
}
85100

86-
typedef testing::TestWithParam<bool> Reproducibility_AlexNet;
101+
typedef testing::TestWithParam<tuple<bool, DNNTarget> > Reproducibility_AlexNet;
87102
TEST_P(Reproducibility_AlexNet, Accuracy)
88103
{
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());
124105
Net net;
125106
{
126107
const string proto = findDataFile("dnn/bvlc_alexnet.prototxt", false);
@@ -140,8 +121,7 @@ OCL_TEST_P(Reproducibility_OCL_AlexNet, Accuracy)
140121
ASSERT_FALSE(net.empty());
141122
}
142123

143-
net.setPreferableBackend(DNN_BACKEND_DEFAULT);
144-
net.setPreferableTarget(DNN_TARGET_OPENCL);
124+
net.setPreferableTarget(get<1>(GetParam()));
145125

146126
Mat sample = imread(_tf("grace_hopper_227.png"));
147127
ASSERT_TRUE(!sample.empty());
@@ -152,7 +132,7 @@ OCL_TEST_P(Reproducibility_OCL_AlexNet, Accuracy)
152132
normAssert(ref, out);
153133
}
154134

155-
OCL_INSTANTIATE_TEST_CASE_P(Test_Caffe, Reproducibility_OCL_AlexNet, testing::Bool());
135+
INSTANTIATE_TEST_CASE_P(/**/, Reproducibility_AlexNet, Combine(testing::Bool(), availableBackends()));
156136

157137
#if !defined(_WIN32) || defined(_WIN64)
158138
TEST(Reproducibility_FCN, Accuracy)
@@ -207,43 +187,14 @@ TEST(Reproducibility_SSD, Accuracy)
207187
normAssert(ref, out);
208188
}
209189

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)
240192
{
241193
const string proto = findDataFile("dnn/MobileNetSSD_deploy.prototxt", false);
242194
const string model = findDataFile("dnn/MobileNetSSD_deploy.caffemodel", false);
243195
Net net = readNetFromCaffe(proto, model);
244196

245-
net.setPreferableBackend(DNN_BACKEND_DEFAULT);
246-
net.setPreferableTarget(DNN_TARGET_OPENCL);
197+
net.setPreferableTarget(GetParam());
247198

248199
Mat sample = imread(_tf("street.png"));
249200

@@ -258,38 +209,39 @@ OCL_TEST(Reproducibility_MobileNet_SSD, Accuracy)
258209
inp.setTo(0.0f);
259210
net.setInput(inp);
260211
out = net.forward();
212+
out = out.reshape(1, out.total() / 7);
261213

262-
const int numDetections = out.size[2];
214+
const int numDetections = out.rows;
263215
ASSERT_NE(numDetections, 0);
264216
for (int i = 0; i < numDetections; ++i)
265217
{
266-
float confidence = out.ptr<float>(0, 0, i)[2];
218+
float confidence = out.ptr<float>(i)[2];
267219
ASSERT_EQ(confidence, 0);
268220
}
269-
}
270221

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));
284234
}
235+
INSTANTIATE_TEST_CASE_P(/**/, Reproducibility_MobileNet_SSD, availableBackends());
285236

286-
OCL_TEST(Reproducibility_ResNet50, Accuracy)
237+
typedef testing::TestWithParam<DNNTarget> Reproducibility_ResNet50;
238+
TEST_P(Reproducibility_ResNet50, Accuracy)
287239
{
288240
Net net = readNetFromCaffe(findDataFile("dnn/ResNet-50-deploy.prototxt", false),
289241
findDataFile("dnn/ResNet-50-model.caffemodel", false));
290242

291-
net.setPreferableBackend(DNN_BACKEND_DEFAULT);
292-
net.setPreferableTarget(DNN_TARGET_OPENCL);
243+
int targetId = GetParam();
244+
net.setPreferableTarget(targetId);
293245

294246
Mat input = blobFromImage(imread(_tf("googlenet_0.png")), 1.0f, Size(224,224), Scalar(), false);
295247
ASSERT_TRUE(!input.empty());
@@ -300,52 +252,46 @@ OCL_TEST(Reproducibility_ResNet50, Accuracy)
300252
Mat ref = blobFromNPY(_tf("resnet50_prob.npy"));
301253
normAssert(ref, out);
302254

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");
322260

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+
}
325265
}
266+
INSTANTIATE_TEST_CASE_P(/**/, Reproducibility_ResNet50, availableBackends());
326267

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)
328270
{
329271
Net net = readNetFromCaffe(findDataFile("dnn/squeezenet_v1.1.prototxt", false),
330272
findDataFile("dnn/squeezenet_v1.1.caffemodel", false));
331273

332-
net.setPreferableBackend(DNN_BACKEND_DEFAULT);
333-
net.setPreferableTarget(DNN_TARGET_OPENCL);
274+
int targetId = GetParam();
275+
net.setPreferableTarget(targetId);
334276

335277
Mat input = blobFromImage(imread(_tf("googlenet_0.png")), 1.0f, Size(227,227), Scalar(), false);
336278
ASSERT_TRUE(!input.empty());
337279

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+
}
343288
net.setInput(input);
344289
out = net.forward();
345290

346291
Mat ref = blobFromNPY(_tf("squeezenet_v1.1_prob.npy"));
347292
normAssert(ref, out);
348293
}
294+
INSTANTIATE_TEST_CASE_P(/**/, Reproducibility_SqueezeNet_v1_1, availableBackends());
349295

350296
TEST(Reproducibility_AlexNet_fp16, Accuracy)
351297
{
@@ -456,7 +402,6 @@ TEST(Test_Caffe, multiple_inputs)
456402
normAssert(out, first_image + second_image);
457403
}
458404

459-
CV_ENUM(DNNTarget, DNN_TARGET_CPU, DNN_TARGET_OPENCL)
460405
typedef testing::TestWithParam<tuple<std::string, DNNTarget> > opencv_face_detector;
461406
TEST_P(opencv_face_detector, Accuracy)
462407
{

0 commit comments

Comments
 (0)