Fallback for "SAME" padMode in ocl convolution and pooling
authorLi Peng <peng.li@intel.com>
Mon, 19 Feb 2018 12:56:40 +0000 (20:56 +0800)
committerLi Peng <peng.li@intel.com>
Thu, 22 Feb 2018 13:17:59 +0000 (21:17 +0800)
It fixes tensorflow ocl testcase of MobileNetSSD and Inception_v2_SSD

Signed-off-by: Li Peng <peng.li@intel.com>
modules/dnn/src/layers/convolution_layer.cpp
modules/dnn/src/layers/pooling_layer.cpp
modules/dnn/test/test_backends.cpp
modules/dnn/test/test_tf_importer.cpp

index 2517499..3250501 100644 (file)
@@ -824,15 +824,9 @@ public:
         for (int i = 0; i < inputs.size(); ++i)
             CV_Assert(inputs[i].u != outputs[0].u);
 
-        int inpH = inputs[0].size[2];
-        int inpW = inputs[0].size[3];
-        int out_h = (inpH + 2 * pad.height - (dilation.height * (kernel.height - 1) + 1)) / stride.height + 1;
-        int out_w = (inpW + 2 * pad.width - (dilation.width * (kernel.width - 1) + 1)) / stride.width + 1;
-        if (out_h != outputs[0].size[2] || out_w != outputs[0].size[3])
+        if (padMode == "SAME")
             return false;
 
-        int group = inputs[0].size[1] / umat_blobs[0].size[1];
-
         if (convolutionOp.empty())
         {
             OCL4DNNConvConfig config;
@@ -842,7 +836,7 @@ public:
             config.pad = pad;
             config.stride = stride;
             config.dilation = dilation;
-            config.group = group;
+            config.group = inputs[0].size[1] / umat_blobs[0].size[1];
             config.bias_term = (hasBias()) ? true : false;
 
             convolutionOp = Ptr<OCL4DNNConvSpatial<float> >(new OCL4DNNConvSpatial<float>(config));
index 2f5f041..10952ed 100644 (file)
@@ -145,6 +145,9 @@ public:
         inps.getUMatVector(inputs);
         outs.getUMatVector(outputs);
 
+        if (type == AVE && padMode == "SAME")
+            return false;
+
         if (poolOp.empty())
         {
             OCL4DNNPoolConfig config;
index 89ed5d0..f88737d 100644 (file)
@@ -233,9 +233,7 @@ TEST_P(DNNTestNetwork, opencv_face_detector)
 
 TEST_P(DNNTestNetwork, Inception_v2_SSD_TensorFlow)
 {
-    if (backend == DNN_BACKEND_DEFAULT && target == DNN_TARGET_OPENCL ||
-        backend == DNN_BACKEND_HALIDE)
-        throw SkipTestException("");
+    if (backend == DNN_BACKEND_HALIDE) throw SkipTestException("");
     Mat sample = imread(findDataFile("dnn/street.png", false));
     Mat inp = blobFromImage(sample, 1.0f / 127.5, Size(300, 300), Scalar(127.5, 127.5, 127.5), false);
     processNet("dnn/ssd_inception_v2_coco_2017_11_17.pb", "dnn/ssd_inception_v2_coco_2017_11_17.pbtxt",
index a2b6df6..15b293a 100644 (file)
@@ -317,11 +317,44 @@ OCL_TEST(Test_TensorFlow, MobileNet_SSD)
     std::vector<Mat> output;
     net.forward(output, outNames);
 
-    normAssert(target[0].reshape(1, 1), output[0].reshape(1, 1), "", 1e-5, 1.5e-4);
+    normAssert(target[0].reshape(1, 1), output[0].reshape(1, 1));
     normAssert(target[1].reshape(1, 1), output[1].reshape(1, 1), "", 1e-5, 3e-4);
     normAssert(target[2].reshape(1, 1), output[2].reshape(1, 1), "", 4e-5, 1e-2);
 }
 
+OCL_TEST(Test_TensorFlow, Inception_v2_SSD)
+{
+    std::string proto = findDataFile("dnn/ssd_inception_v2_coco_2017_11_17.pbtxt", false);
+    std::string model = findDataFile("dnn/ssd_inception_v2_coco_2017_11_17.pb", false);
+
+    Net net = readNetFromTensorflow(model, proto);
+    Mat img = imread(findDataFile("dnn/street.png", false));
+    Mat blob = blobFromImage(img, 1.0f / 127.5, Size(300, 300), Scalar(127.5, 127.5, 127.5), true, false);
+
+    net.setPreferableBackend(DNN_BACKEND_DEFAULT);
+    net.setPreferableTarget(DNN_TARGET_OPENCL);
+
+    net.setInput(blob);
+    // Output has shape 1x1xNx7 where N - number of detections.
+    // An every detection is a vector of values [id, classId, confidence, left, top, right, bottom]
+    Mat out = net.forward();
+    out = out.reshape(1, out.total() / 7);
+
+    Mat detections;
+    for (int i = 0; i < out.rows; ++i)
+    {
+        if (out.at<float>(i, 2) > 0.5)
+          detections.push_back(out.row(i).colRange(1, 7));
+    }
+
+    Mat ref = (Mat_<float>(5, 6) << 1, 0.90176028, 0.19872092, 0.36311883, 0.26461923, 0.63498729,
+                                    3, 0.93569964, 0.64865261, 0.45906419, 0.80675775, 0.65708131,
+                                    3, 0.75838411, 0.44668293, 0.45907149, 0.49459291, 0.52197015,
+                                    10, 0.95932811, 0.38349164, 0.32528657, 0.40387636, 0.39165527,
+                                    10, 0.93973452, 0.66561931, 0.37841269, 0.68074018, 0.42907384);
+    normAssert(detections, ref);
+}
+
 TEST(Test_TensorFlow, lstm)
 {
     runTensorFlowNet("lstm", DNN_TARGET_CPU, true);