Skip to content

Commit 5b868cc

Browse files
committed
Merge pull request opencv#10992 from dkurt:dnn_opencl_tests
2 parents 687394f + 0f01b40 commit 5b868cc

11 files changed

+258
-476
lines changed

modules/dnn/src/layers/convolution_layer.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,9 @@ class ConvolutionLayerImpl : public BaseConvolutionLayerImpl
273273
for(int i = 0; i < outCn; i++ )
274274
biasvec[i] = biasMat.at<float>(i);
275275
}
276+
#ifdef HAVE_OPENCL
277+
convolutionOp.release();
278+
#endif
276279
}
277280

278281
bool setActivation(const Ptr<ActivationLayer>& layer)

modules/dnn/src/layers/fully_connected_layer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,11 @@ class FullyConnectedLayerImpl : public InnerProductLayer
267267
};
268268

269269
#ifdef HAVE_OPENCL
270+
void finalize(const std::vector<Mat*> &inputs, std::vector<Mat> &outputs)
271+
{
272+
innerProductOp.release();
273+
}
274+
270275
bool forward_ocl(InputArrayOfArrays inps, OutputArrayOfArrays outs, InputArrayOfArrays internals)
271276
{
272277
std::vector<UMat> inputs;

modules/dnn/src/layers/lrn_layer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ class LRNLayerImpl : public LRNLayer
9696
}
9797

9898
#ifdef HAVE_OPENCL
99+
void finalize(const std::vector<Mat*> &inputs, std::vector<Mat> &outputs)
100+
{
101+
lrnOp.release();
102+
}
103+
99104
bool forward_ocl(InputArrayOfArrays inps, OutputArrayOfArrays outs, OutputArrayOfArrays internals)
100105
{
101106
std::vector<UMat> inputs;

modules/dnn/src/layers/pooling_layer.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ class PoolingLayerImpl : public PoolingLayer
127127
}
128128

129129
getConvPoolPaddings(inp, out, kernel, stride, padMode, Size(1, 1), pad);
130+
131+
#ifdef HAVE_OPENCL
132+
poolOp.release();
133+
#endif
130134
}
131135

132136
virtual bool supportBackend(int backendId)

modules/dnn/src/layers/softmax_layer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ class SoftMaxLayerImpl : public SoftmaxLayer
9494
}
9595

9696
#ifdef HAVE_OPENCL
97+
virtual void finalize(const std::vector<Mat*> &inputs, std::vector<Mat> &outputs)
98+
{
99+
softmaxOp.release();
100+
}
101+
97102
bool forward_ocl(InputArrayOfArrays inps, OutputArrayOfArrays outs, OutputArrayOfArrays itns)
98103
{
99104
std::vector<UMat> inputs;

modules/dnn/test/test_backends.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010

1111
namespace opencv_test { namespace {
1212

13-
CV_ENUM(DNNBackend, DNN_BACKEND_DEFAULT, DNN_BACKEND_HALIDE, DNN_BACKEND_INFERENCE_ENGINE)
14-
CV_ENUM(DNNTarget, DNN_TARGET_CPU, DNN_TARGET_OPENCL)
15-
1613
static void loadNet(const std::string& weights, const std::string& proto,
1714
const std::string& framework, Net* net)
1815
{

modules/dnn/test/test_caffe_importer.cpp

Lines changed: 50 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@
4242
#include "test_precomp.hpp"
4343
#include "npy_blob.hpp"
4444
#include <opencv2/dnn/shape_utils.hpp>
45-
#include <opencv2/core/ocl.hpp>
46-
#include <opencv2/ts/ocl_test.hpp>
4745

4846
namespace opencv_test { namespace {
4947

@@ -83,10 +81,10 @@ TEST(Test_Caffe, read_googlenet)
8381
ASSERT_FALSE(net.empty());
8482
}
8583

86-
typedef testing::TestWithParam<bool> Reproducibility_AlexNet;
84+
typedef testing::TestWithParam<tuple<bool, DNNTarget> > Reproducibility_AlexNet;
8785
TEST_P(Reproducibility_AlexNet, Accuracy)
8886
{
89-
bool readFromMemory = GetParam();
87+
bool readFromMemory = get<0>(GetParam());
9088
Net net;
9189
{
9290
const string proto = findDataFile("dnn/bvlc_alexnet.prototxt", false);
@@ -106,42 +104,7 @@ TEST_P(Reproducibility_AlexNet, Accuracy)
106104
ASSERT_FALSE(net.empty());
107105
}
108106

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()));
145108

146109
Mat sample = imread(_tf("grace_hopper_227.png"));
147110
ASSERT_TRUE(!sample.empty());
@@ -152,7 +115,7 @@ OCL_TEST_P(Reproducibility_OCL_AlexNet, Accuracy)
152115
normAssert(ref, out);
153116
}
154117

155-
OCL_INSTANTIATE_TEST_CASE_P(Test_Caffe, Reproducibility_OCL_AlexNet, testing::Bool());
118+
INSTANTIATE_TEST_CASE_P(/**/, Reproducibility_AlexNet, Combine(testing::Bool(), availableDnnTargets()));
156119

157120
#if !defined(_WIN32) || defined(_WIN64)
158121
TEST(Reproducibility_FCN, Accuracy)
@@ -207,43 +170,14 @@ TEST(Reproducibility_SSD, Accuracy)
207170
normAssert(ref, out);
208171
}
209172

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)
240175
{
241176
const string proto = findDataFile("dnn/MobileNetSSD_deploy.prototxt", false);
242177
const string model = findDataFile("dnn/MobileNetSSD_deploy.caffemodel", false);
243178
Net net = readNetFromCaffe(proto, model);
244179

245-
net.setPreferableBackend(DNN_BACKEND_DEFAULT);
246-
net.setPreferableTarget(DNN_TARGET_OPENCL);
180+
net.setPreferableTarget(GetParam());
247181

248182
Mat sample = imread(_tf("street.png"));
249183

@@ -258,38 +192,39 @@ OCL_TEST(Reproducibility_MobileNet_SSD, Accuracy)
258192
inp.setTo(0.0f);
259193
net.setInput(inp);
260194
out = net.forward();
195+
out = out.reshape(1, out.total() / 7);
261196

262-
const int numDetections = out.size[2];
197+
const int numDetections = out.rows;
263198
ASSERT_NE(numDetections, 0);
264199
for (int i = 0; i < numDetections; ++i)
265200
{
266-
float confidence = out.ptr<float>(0, 0, i)[2];
201+
float confidence = out.ptr<float>(i)[2];
267202
ASSERT_EQ(confidence, 0);
268203
}
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());
278204

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));
284217
}
218+
INSTANTIATE_TEST_CASE_P(/**/, Reproducibility_MobileNet_SSD, availableDnnTargets());
285219

286-
OCL_TEST(Reproducibility_ResNet50, Accuracy)
220+
typedef testing::TestWithParam<DNNTarget> Reproducibility_ResNet50;
221+
TEST_P(Reproducibility_ResNet50, Accuracy)
287222
{
288223
Net net = readNetFromCaffe(findDataFile("dnn/ResNet-50-deploy.prototxt", false),
289224
findDataFile("dnn/ResNet-50-model.caffemodel", false));
290225

291-
net.setPreferableBackend(DNN_BACKEND_DEFAULT);
292-
net.setPreferableTarget(DNN_TARGET_OPENCL);
226+
int targetId = GetParam();
227+
net.setPreferableTarget(targetId);
293228

294229
Mat input = blobFromImage(imread(_tf("googlenet_0.png")), 1.0f, Size(224,224), Scalar(), false);
295230
ASSERT_TRUE(!input.empty());
@@ -300,52 +235,46 @@ OCL_TEST(Reproducibility_ResNet50, Accuracy)
300235
Mat ref = blobFromNPY(_tf("resnet50_prob.npy"));
301236
normAssert(ref, out);
302237

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

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+
}
325248
}
249+
INSTANTIATE_TEST_CASE_P(/**/, Reproducibility_ResNet50, availableDnnTargets());
326250

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)
328253
{
329254
Net net = readNetFromCaffe(findDataFile("dnn/squeezenet_v1.1.prototxt", false),
330255
findDataFile("dnn/squeezenet_v1.1.caffemodel", false));
331256

332-
net.setPreferableBackend(DNN_BACKEND_DEFAULT);
333-
net.setPreferableTarget(DNN_TARGET_OPENCL);
257+
int targetId = GetParam();
258+
net.setPreferableTarget(targetId);
334259

335260
Mat input = blobFromImage(imread(_tf("googlenet_0.png")), 1.0f, Size(227,227), Scalar(), false);
336261
ASSERT_TRUE(!input.empty());
337262

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+
}
343271
net.setInput(input);
344272
out = net.forward();
345273

346274
Mat ref = blobFromNPY(_tf("squeezenet_v1.1_prob.npy"));
347275
normAssert(ref, out);
348276
}
277+
INSTANTIATE_TEST_CASE_P(/**/, Reproducibility_SqueezeNet_v1_1, availableDnnTargets());
349278

350279
TEST(Reproducibility_AlexNet_fp16, Accuracy)
351280
{
@@ -456,7 +385,6 @@ TEST(Test_Caffe, multiple_inputs)
456385
normAssert(out, first_image + second_image);
457386
}
458387

459-
CV_ENUM(DNNTarget, DNN_TARGET_CPU, DNN_TARGET_OPENCL)
460388
typedef testing::TestWithParam<tuple<std::string, DNNTarget> > opencv_face_detector;
461389
TEST_P(opencv_face_detector, Accuracy)
462390
{

modules/dnn/test/test_googlenet.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ OCL_TEST(Reproducibility_GoogLeNet, Accuracy)
7777
net.setPreferableBackend(DNN_BACKEND_DEFAULT);
7878
net.setPreferableTarget(DNN_TARGET_OPENCL);
7979

80+
// Initialize network for a single image in the batch but test with batch size=2.
81+
net.setInput(blobFromImage(Mat(224, 224, CV_8UC3)));
82+
net.forward();
83+
8084
std::vector<Mat> inpMats;
8185
inpMats.push_back( imread(_tf("googlenet_0.png")) );
8286
inpMats.push_back( imread(_tf("googlenet_1.png")) );

modules/dnn/test/test_precomp.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,31 @@
4444
#include "opencv2/ts.hpp"
4545
#include "opencv2/ts/ts_perf.hpp"
4646
#include "opencv2/core/utility.hpp"
47+
#include "opencv2/core/ocl.hpp"
4748

4849
#include "opencv2/dnn.hpp"
4950
#include "test_common.hpp"
5051

5152
namespace opencv_test {
5253
using namespace cv::dnn;
54+
55+
CV_ENUM(DNNBackend, DNN_BACKEND_DEFAULT, DNN_BACKEND_HALIDE, DNN_BACKEND_INFERENCE_ENGINE)
56+
CV_ENUM(DNNTarget, DNN_TARGET_CPU, DNN_TARGET_OPENCL)
57+
58+
static testing::internal::ParamGenerator<DNNTarget> availableDnnTargets()
59+
{
60+
static std::vector<DNNTarget> targets;
61+
if (targets.empty())
62+
{
63+
targets.push_back(DNN_TARGET_CPU);
64+
#ifdef HAVE_OPENCL
65+
if (cv::ocl::useOpenCL())
66+
targets.push_back(DNN_TARGET_OPENCL);
67+
#endif
68+
}
69+
return testing::ValuesIn(targets);
70+
}
71+
5372
}
5473

5574
#endif

0 commit comments

Comments
 (0)