Skip to content

Commit c943947

Browse files
committed
Merge pull request opencv#11970 from dkurt:dnn_enable_tf_tests
2 parents 6099438 + 6eb8fae commit c943947

File tree

3 files changed

+53
-27
lines changed

3 files changed

+53
-27
lines changed

modules/dnn/src/layers/detection_output_layer.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,9 @@ class DetectionOutputLayerImpl CV_FINAL : public DetectionOutputLayer
295295
for (int i = 0; i < num; i++)
296296
confPreds.push_back(Mat(2, shape, CV_32F));
297297

298-
UMat umat = inp1.reshape(1, num * numPredsPerClass);
298+
shape[0] = num * numPredsPerClass;
299+
shape[1] = inp1.total() / shape[0];
300+
UMat umat = inp1.reshape(1, 2, &shape[0]);
299301
for (int i = 0; i < num; ++i)
300302
{
301303
Range ranges[] = { Range(i * numPredsPerClass, (i + 1) * numPredsPerClass), Range::all() };
@@ -342,7 +344,7 @@ class DetectionOutputLayerImpl CV_FINAL : public DetectionOutputLayer
342344
// Decode all loc predictions to bboxes
343345
bool ret = ocl_DecodeBBoxesAll(inputs[0], inputs[2], num, numPriors,
344346
_shareLocation, _numLocClasses, _backgroundLabelId,
345-
_codeType, _varianceEncodedInTarget, false,
347+
_codeType, _varianceEncodedInTarget, _clip,
346348
allDecodedBBoxes);
347349
if (!ret)
348350
return false;

modules/dnn/src/layers/softmax_layer.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,27 +110,26 @@ class SoftMaxLayerImpl CV_FINAL : public SoftmaxLayer
110110
outputs_.getUMatVector(outputs);
111111
internals_.getUMatVector(internals);
112112

113+
UMat& src = inputs[0];
114+
UMat& dstMat = outputs[0];
115+
int axis = clamp(axisRaw, src.dims);
116+
113117
if (softmaxOp.empty())
114118
{
115119
OCL4DNNSoftmaxConfig config;
116-
117120
config.in_shape = shape(inputs[0]);
118-
config.axis = axisRaw;
119-
config.channels = inputs[0].size[axisRaw];
121+
config.axis = axis;
122+
config.channels = inputs[0].size[axis];
120123
config.logsoftmax = logSoftMax;
121124
config.use_half = use_half;
122125

123126
softmaxOp = Ptr<OCL4DNNSoftmax<float> >(new OCL4DNNSoftmax<float>(config));
124127
}
125128

126-
UMat& src = inputs[0];
127-
UMat& dstMat = outputs[0];
128-
129129
if (softmaxOp->Forward(src, dstMat))
130130
return true;
131131

132132
UMat& bufMat = internals[0];
133-
int axis = clamp(axisRaw, src.dims);
134133
MatShape s = shape(src);
135134
size_t outerSize = total(s, 0, axis);
136135
size_t channels = src.size[axis];

modules/dnn/test/test_tf_importer.cpp

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,15 @@ TEST_P(Test_TensorFlow_layers, l2_normalize_3d)
243243
runTensorFlowNet("l2_normalize_3d");
244244
}
245245

246-
typedef testing::TestWithParam<Target> Test_TensorFlow_nets;
246+
class Test_TensorFlow_nets : public DNNTestLayer {};
247247

248248
TEST_P(Test_TensorFlow_nets, MobileNet_SSD)
249249
{
250+
checkBackend();
251+
if ((backend == DNN_BACKEND_INFERENCE_ENGINE && target != DNN_TARGET_CPU) ||
252+
(backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16))
253+
throw SkipTestException("");
254+
250255
std::string netPath = findDataFile("dnn/ssd_mobilenet_v1_coco.pb", false);
251256
std::string netConfig = findDataFile("dnn/ssd_mobilenet_v1_coco.pbtxt", false);
252257
std::string imgPath = findDataFile("dnn/street.png", false);
@@ -260,38 +265,39 @@ TEST_P(Test_TensorFlow_nets, MobileNet_SSD)
260265
outNames[1] = "concat_1";
261266
outNames[2] = "detection_out";
262267

263-
std::vector<Mat> target(outNames.size());
268+
std::vector<Mat> refs(outNames.size());
264269
for (int i = 0; i < outNames.size(); ++i)
265270
{
266271
std::string path = findDataFile("dnn/tensorflow/ssd_mobilenet_v1_coco." + outNames[i] + ".npy", false);
267-
target[i] = blobFromNPY(path);
272+
refs[i] = blobFromNPY(path);
268273
}
269274

270275
Net net = readNetFromTensorflow(netPath, netConfig);
271-
net.setPreferableBackend(DNN_BACKEND_OPENCV);
272-
net.setPreferableTarget(GetParam());
276+
net.setPreferableBackend(backend);
277+
net.setPreferableTarget(target);
273278

274279
net.setInput(inp);
275280

276281
std::vector<Mat> output;
277282
net.forward(output, outNames);
278283

279-
normAssert(target[0].reshape(1, 1), output[0].reshape(1, 1), "", 1e-5, 1.5e-4);
280-
normAssert(target[1].reshape(1, 1), output[1].reshape(1, 1), "", 1e-5, 3e-4);
281-
normAssertDetections(target[2], output[2], "", 0.2);
284+
normAssert(refs[0].reshape(1, 1), output[0].reshape(1, 1), "", 1e-5, 1.5e-4);
285+
normAssert(refs[1].reshape(1, 1), output[1].reshape(1, 1), "", 1e-5, 3e-4);
286+
normAssertDetections(refs[2], output[2], "", 0.2);
282287
}
283288

284289
TEST_P(Test_TensorFlow_nets, Inception_v2_SSD)
285290
{
291+
checkBackend();
286292
std::string proto = findDataFile("dnn/ssd_inception_v2_coco_2017_11_17.pbtxt", false);
287293
std::string model = findDataFile("dnn/ssd_inception_v2_coco_2017_11_17.pb", false);
288294

289295
Net net = readNetFromTensorflow(model, proto);
290296
Mat img = imread(findDataFile("dnn/street.png", false));
291297
Mat blob = blobFromImage(img, 1.0f / 127.5, Size(300, 300), Scalar(127.5, 127.5, 127.5), true, false);
292298

293-
net.setPreferableBackend(DNN_BACKEND_OPENCV);
294-
net.setPreferableTarget(GetParam());
299+
net.setPreferableBackend(backend);
300+
net.setPreferableTarget(target);
295301

296302
net.setInput(blob);
297303
// Output has shape 1x1xNx7 where N - number of detections.
@@ -302,16 +308,24 @@ TEST_P(Test_TensorFlow_nets, Inception_v2_SSD)
302308
0, 3, 0.75838411, 0.44668293, 0.45907149, 0.49459291, 0.52197015,
303309
0, 10, 0.95932811, 0.38349164, 0.32528657, 0.40387636, 0.39165527,
304310
0, 10, 0.93973452, 0.66561931, 0.37841269, 0.68074018, 0.42907384);
305-
normAssertDetections(ref, out, "", 0.5);
311+
double scoreDiff = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 5e-3 : default_l1;
312+
double iouDiff = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.025 : default_lInf;
313+
normAssertDetections(ref, out, "", 0.5, scoreDiff, iouDiff);
306314
}
307315

308316
TEST_P(Test_TensorFlow_nets, Inception_v2_Faster_RCNN)
309317
{
318+
checkBackend();
319+
if (backend == DNN_BACKEND_INFERENCE_ENGINE ||
320+
(backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16))
321+
throw SkipTestException("");
322+
310323
std::string proto = findDataFile("dnn/faster_rcnn_inception_v2_coco_2018_01_28.pbtxt", false);
311324
std::string model = findDataFile("dnn/faster_rcnn_inception_v2_coco_2018_01_28.pb", false);
312325

313326
Net net = readNetFromTensorflow(model, proto);
314-
net.setPreferableBackend(DNN_BACKEND_OPENCV);
327+
net.setPreferableBackend(backend);
328+
net.setPreferableTarget(target);
315329
Mat img = imread(findDataFile("dnn/dog416.png", false));
316330
Mat blob = blobFromImage(img, 1.0f / 127.5, Size(800, 600), Scalar(127.5, 127.5, 127.5), true, false);
317331

@@ -324,16 +338,20 @@ TEST_P(Test_TensorFlow_nets, Inception_v2_Faster_RCNN)
324338

325339
TEST_P(Test_TensorFlow_nets, opencv_face_detector_uint8)
326340
{
341+
checkBackend();
342+
if (backend == DNN_BACKEND_INFERENCE_ENGINE &&
343+
(target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD))
344+
throw SkipTestException("");
345+
327346
std::string proto = findDataFile("dnn/opencv_face_detector.pbtxt", false);
328347
std::string model = findDataFile("dnn/opencv_face_detector_uint8.pb", false);
329348

330349
Net net = readNetFromTensorflow(model, proto);
331350
Mat img = imread(findDataFile("gpu/lbpcascade/er.png", false));
332351
Mat blob = blobFromImage(img, 1.0, Size(), Scalar(104.0, 177.0, 123.0), false, false);
333352

334-
net.setPreferableBackend(DNN_BACKEND_OPENCV);
335-
net.setPreferableTarget(GetParam());
336-
353+
net.setPreferableBackend(backend);
354+
net.setPreferableTarget(target);
337355
net.setInput(blob);
338356
// Output has shape 1x1xNx7 where N - number of detections.
339357
// An every detection is a vector of values [id, classId, confidence, left, top, right, bottom]
@@ -346,7 +364,9 @@ TEST_P(Test_TensorFlow_nets, opencv_face_detector_uint8)
346364
0, 1, 0.98977017, 0.23901358, 0.09084064, 0.29902688, 0.1769477,
347365
0, 1, 0.97203469, 0.67965847, 0.06876482, 0.73999709, 0.1513494,
348366
0, 1, 0.95097077, 0.51901293, 0.45863652, 0.5777427, 0.5347801);
349-
normAssertDetections(ref, out, "", 0.9, 3.4e-3, 1e-2);
367+
double scoreDiff = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 4e-3 : 3.4e-3;
368+
double iouDiff = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.017 : 1e-2;
369+
normAssertDetections(ref, out, "", 0.9, scoreDiff, iouDiff);
350370
}
351371

352372
// inp = cv.imread('opencv_extra/testdata/cv/ximgproc/sources/08.png')
@@ -360,14 +380,19 @@ TEST_P(Test_TensorFlow_nets, opencv_face_detector_uint8)
360380
// np.save('east_text_detection.geometry.npy', geometry)
361381
TEST_P(Test_TensorFlow_nets, EAST_text_detection)
362382
{
383+
checkBackend();
384+
if (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD)
385+
throw SkipTestException("");
386+
363387
std::string netPath = findDataFile("dnn/frozen_east_text_detection.pb", false);
364388
std::string imgPath = findDataFile("cv/ximgproc/sources/08.png", false);
365389
std::string refScoresPath = findDataFile("dnn/east_text_detection.scores.npy", false);
366390
std::string refGeometryPath = findDataFile("dnn/east_text_detection.geometry.npy", false);
367391

368392
Net net = readNet(findDataFile("dnn/frozen_east_text_detection.pb", false));
369393

370-
net.setPreferableTarget(GetParam());
394+
net.setPreferableBackend(backend);
395+
net.setPreferableTarget(target);
371396

372397
Mat img = imread(imgPath);
373398
Mat inp = blobFromImage(img, 1.0, Size(), Scalar(123.68, 116.78, 103.94), true, false);
@@ -386,7 +411,7 @@ TEST_P(Test_TensorFlow_nets, EAST_text_detection)
386411
normAssert(geometry, blobFromNPY(refGeometryPath), "geometry", 1e-4, 3e-3);
387412
}
388413

389-
INSTANTIATE_TEST_CASE_P(/**/, Test_TensorFlow_nets, availableDnnTargets());
414+
INSTANTIATE_TEST_CASE_P(/**/, Test_TensorFlow_nets, dnnBackendsAndTargets());
390415

391416
TEST_P(Test_TensorFlow_layers, fp16_weights)
392417
{

0 commit comments

Comments
 (0)