Skip to content

Commit e606253

Browse files
committed
Merge pull request opencv#10918 from pengli:dnn
2 parents 203ac0f + e7d35d5 commit e606253

File tree

6 files changed

+67
-22
lines changed

6 files changed

+67
-22
lines changed

modules/dnn/src/layers/convolution_layer.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -824,15 +824,9 @@ class ConvolutionLayerImpl : public BaseConvolutionLayerImpl
824824
for (int i = 0; i < inputs.size(); ++i)
825825
CV_Assert(inputs[i].u != outputs[0].u);
826826

827-
int inpH = inputs[0].size[2];
828-
int inpW = inputs[0].size[3];
829-
int out_h = (inpH + 2 * pad.height - (dilation.height * (kernel.height - 1) + 1)) / stride.height + 1;
830-
int out_w = (inpW + 2 * pad.width - (dilation.width * (kernel.width - 1) + 1)) / stride.width + 1;
831-
if (out_h != outputs[0].size[2] || out_w != outputs[0].size[3])
827+
if (padMode == "SAME")
832828
return false;
833829

834-
int group = inputs[0].size[1] / umat_blobs[0].size[1];
835-
836830
if (convolutionOp.empty())
837831
{
838832
OCL4DNNConvConfig config;
@@ -842,7 +836,7 @@ class ConvolutionLayerImpl : public BaseConvolutionLayerImpl
842836
config.pad = pad;
843837
config.stride = stride;
844838
config.dilation = dilation;
845-
config.group = group;
839+
config.group = inputs[0].size[1] / umat_blobs[0].size[1];
846840
config.bias_term = (hasBias()) ? true : false;
847841

848842
convolutionOp = Ptr<OCL4DNNConvSpatial<float> >(new OCL4DNNConvSpatial<float>(config));

modules/dnn/src/layers/normalize_bbox_layer.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,18 @@ class NormalizeBBoxLayerImpl : public NormalizeBBoxLayer
105105
float norm = pow(absSum, 1.0f / pnorm);
106106
multiply(src, 1.0f / norm, dst);
107107
}
108+
else
109+
{
110+
Mat norm;
111+
reduce(buffer, norm, 0, REDUCE_SUM);
112+
norm += epsilon;
113+
114+
// compute inverted norm to call multiply instead divide
115+
cv::pow(norm, -1.0f / pnorm, norm);
116+
117+
repeat(norm, channels, 1, buffer);
118+
multiply(src, buffer, dst);
119+
}
108120

109121
if (!blobs.empty())
110122
{

modules/dnn/src/layers/pooling_layer.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ class PoolingLayerImpl : public PoolingLayer
145145
inps.getUMatVector(inputs);
146146
outs.getUMatVector(outputs);
147147

148+
if (type == AVE && padMode == "SAME")
149+
return false;
150+
148151
if (poolOp.empty())
149152
{
150153
OCL4DNNPoolConfig config;

modules/dnn/test/test_backends.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,7 @@ TEST_P(DNNTestNetwork, OpenFace)
222222

223223
TEST_P(DNNTestNetwork, opencv_face_detector)
224224
{
225-
if (backend == DNN_BACKEND_HALIDE ||
226-
backend == DNN_BACKEND_DEFAULT && target == DNN_TARGET_OPENCL)
227-
throw SkipTestException("");
225+
if (backend == DNN_BACKEND_HALIDE) throw SkipTestException("");
228226
Mat img = imread(findDataFile("gpu/lbpcascade/er.png", false));
229227
Mat inp = blobFromImage(img, 1.0, Size(), Scalar(104.0, 177.0, 123.0), false, false);
230228
processNet("dnn/opencv_face_detector.caffemodel", "dnn/opencv_face_detector.prototxt",
@@ -233,9 +231,7 @@ TEST_P(DNNTestNetwork, opencv_face_detector)
233231

234232
TEST_P(DNNTestNetwork, Inception_v2_SSD_TensorFlow)
235233
{
236-
if (backend == DNN_BACKEND_DEFAULT && target == DNN_TARGET_OPENCL ||
237-
backend == DNN_BACKEND_HALIDE)
238-
throw SkipTestException("");
234+
if (backend == DNN_BACKEND_HALIDE) throw SkipTestException("");
239235
Mat sample = imread(findDataFile("dnn/street.png", false));
240236
Mat inp = blobFromImage(sample, 1.0f / 127.5, Size(300, 300), Scalar(127.5, 127.5, 127.5), false);
241237
processNet("dnn/ssd_inception_v2_coco_2017_11_17.pb", "dnn/ssd_inception_v2_coco_2017_11_17.pbtxt",

modules/dnn/test/test_caffe_importer.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -456,16 +456,21 @@ TEST(Test_Caffe, multiple_inputs)
456456
normAssert(out, first_image + second_image);
457457
}
458458

459-
typedef testing::TestWithParam<std::string> opencv_face_detector;
459+
CV_ENUM(DNNTarget, DNN_TARGET_CPU, DNN_TARGET_OPENCL)
460+
typedef testing::TestWithParam<tuple<std::string, DNNTarget> > opencv_face_detector;
460461
TEST_P(opencv_face_detector, Accuracy)
461462
{
462463
std::string proto = findDataFile("dnn/opencv_face_detector.prototxt", false);
463-
std::string model = findDataFile(GetParam(), false);
464+
std::string model = findDataFile(get<0>(GetParam()), false);
465+
dnn::Target targetId = (dnn::Target)(int)get<1>(GetParam());
464466

465467
Net net = readNetFromCaffe(proto, model);
466468
Mat img = imread(findDataFile("gpu/lbpcascade/er.png", false));
467469
Mat blob = blobFromImage(img, 1.0, Size(), Scalar(104.0, 177.0, 123.0), false, false);
468470

471+
net.setPreferableBackend(DNN_BACKEND_DEFAULT);
472+
net.setPreferableTarget(targetId);
473+
469474
net.setInput(blob);
470475
// Output has shape 1x1xNx7 where N - number of detections.
471476
// An every detection is a vector of values [id, classId, confidence, left, top, right, bottom]
@@ -479,11 +484,13 @@ TEST_P(opencv_face_detector, Accuracy)
479484
0.95097077, 0.51901293, 0.45863652, 0.5777427, 0.5347801);
480485
normAssert(out.reshape(1, out.total() / 7).rowRange(0, 6).colRange(2, 7), ref);
481486
}
482-
INSTANTIATE_TEST_CASE_P(Test_Caffe, opencv_face_detector, Values(
483-
"dnn/opencv_face_detector.caffemodel",
484-
"dnn/opencv_face_detector_fp16.caffemodel"
485-
));
486-
487+
INSTANTIATE_TEST_CASE_P(Test_Caffe, opencv_face_detector,
488+
Combine(
489+
Values("dnn/opencv_face_detector.caffemodel",
490+
"dnn/opencv_face_detector_fp16.caffemodel"),
491+
Values(DNN_TARGET_CPU, DNN_TARGET_OPENCL)
492+
)
493+
);
487494

488495
TEST(Test_Caffe, FasterRCNN_and_RFCN)
489496
{

modules/dnn/test/test_tf_importer.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,11 +317,44 @@ OCL_TEST(Test_TensorFlow, MobileNet_SSD)
317317
std::vector<Mat> output;
318318
net.forward(output, outNames);
319319

320-
normAssert(target[0].reshape(1, 1), output[0].reshape(1, 1), "", 1e-5, 1.5e-4);
320+
normAssert(target[0].reshape(1, 1), output[0].reshape(1, 1));
321321
normAssert(target[1].reshape(1, 1), output[1].reshape(1, 1), "", 1e-5, 3e-4);
322322
normAssert(target[2].reshape(1, 1), output[2].reshape(1, 1), "", 4e-5, 1e-2);
323323
}
324324

325+
OCL_TEST(Test_TensorFlow, Inception_v2_SSD)
326+
{
327+
std::string proto = findDataFile("dnn/ssd_inception_v2_coco_2017_11_17.pbtxt", false);
328+
std::string model = findDataFile("dnn/ssd_inception_v2_coco_2017_11_17.pb", false);
329+
330+
Net net = readNetFromTensorflow(model, proto);
331+
Mat img = imread(findDataFile("dnn/street.png", false));
332+
Mat blob = blobFromImage(img, 1.0f / 127.5, Size(300, 300), Scalar(127.5, 127.5, 127.5), true, false);
333+
334+
net.setPreferableBackend(DNN_BACKEND_DEFAULT);
335+
net.setPreferableTarget(DNN_TARGET_OPENCL);
336+
337+
net.setInput(blob);
338+
// Output has shape 1x1xNx7 where N - number of detections.
339+
// An every detection is a vector of values [id, classId, confidence, left, top, right, bottom]
340+
Mat out = net.forward();
341+
out = out.reshape(1, out.total() / 7);
342+
343+
Mat detections;
344+
for (int i = 0; i < out.rows; ++i)
345+
{
346+
if (out.at<float>(i, 2) > 0.5)
347+
detections.push_back(out.row(i).colRange(1, 7));
348+
}
349+
350+
Mat ref = (Mat_<float>(5, 6) << 1, 0.90176028, 0.19872092, 0.36311883, 0.26461923, 0.63498729,
351+
3, 0.93569964, 0.64865261, 0.45906419, 0.80675775, 0.65708131,
352+
3, 0.75838411, 0.44668293, 0.45907149, 0.49459291, 0.52197015,
353+
10, 0.95932811, 0.38349164, 0.32528657, 0.40387636, 0.39165527,
354+
10, 0.93973452, 0.66561931, 0.37841269, 0.68074018, 0.42907384);
355+
normAssert(detections, ref);
356+
}
357+
325358
TEST(Test_TensorFlow, lstm)
326359
{
327360
runTensorFlowNet("lstm", DNN_TARGET_CPU, true);

0 commit comments

Comments
 (0)