Remove undocumented feature to retreive layers outputs by indices
authorDmitry Kurtaev <dmitry.kurtaev+github@gmail.com>
Wed, 20 Jun 2018 11:25:24 +0000 (14:25 +0300)
committerDmitry Kurtaev <dmitry.kurtaev+github@gmail.com>
Wed, 20 Jun 2018 11:44:21 +0000 (14:44 +0300)
modules/dnn/src/dnn.cpp
modules/dnn/test/test_layers.cpp
modules/dnn/test/test_torch_importer.cpp

index 84967ce..6a7c9d5 100644 (file)
@@ -988,52 +988,26 @@ struct Net::Impl
         ld.inputBlobsId[inNum] = from;
     }
 
-    static void splitPin(const String &pinAlias, String &layerName, String &outName)
-    {
-        size_t delimPos = pinAlias.find('.');
-        layerName = pinAlias.substr(0, delimPos);
-        outName = (delimPos == String::npos) ? String() : pinAlias.substr(delimPos + 1);
-    }
-
     int resolvePinOutputName(LayerData &ld, const String &outName)
     {
         if (outName.empty())
             return 0;
-
-        if (std::isdigit(outName[0]))
-        {
-            char *lastChar;
-            long inum = std::strtol(outName.c_str(), &lastChar, 10);
-
-            if (*lastChar == 0)
-            {
-                CV_Assert(inum == (int)inum);
-                return (int)inum;
-            }
-        }
-
         return ld.getLayerInstance()->outputNameToIndex(outName);
     }
 
-    LayerPin getPinByAlias(const String &pinAlias)
+    LayerPin getPinByAlias(const String &layerName)
     {
         LayerPin pin;
-        String layerName, outName;
-        splitPin(pinAlias, layerName, outName);
-
         pin.lid = (layerName.empty()) ? 0 : getLayerId(layerName);
 
         if (pin.lid >= 0)
-            pin.oid = resolvePinOutputName(getLayerData(pin.lid), outName);
+            pin.oid = resolvePinOutputName(getLayerData(pin.lid), layerName);
 
         return pin;
     }
 
-    std::vector<LayerPin> getLayerOutPins(const String &pinAlias)
+    std::vector<LayerPin> getLayerOutPins(const String &layerName)
     {
-        String layerName, outName;
-        splitPin(pinAlias, layerName, outName);
-
         int lid = (layerName.empty()) ? 0 : getLayerId(layerName);
 
         std::vector<LayerPin> pins;
@@ -2044,12 +2018,6 @@ int Net::addLayer(const String &name, const String &type, LayerParams &params)
 {
     CV_TRACE_FUNCTION();
 
-    if (name.find('.') != String::npos)
-    {
-        CV_Error(Error::StsBadArg, "Added layer name \"" + name + "\" must not contain dot symbol");
-        return -1;
-    }
-
     if (impl->getLayerId(name) >= 0)
     {
         CV_Error(Error::StsBadArg, "Layer \"" + name + "\" already into net");
@@ -2689,7 +2657,7 @@ int Layer::inputNameToIndex(String)
 
 int Layer::outputNameToIndex(const String&)
 {
-    return -1;
+    return 0;
 }
 
 bool Layer::supportBackend(int backendId)
index 111f354..77d69ce 100644 (file)
@@ -1144,4 +1144,46 @@ TEST(Layer_Test_Interp, Accuracy)
     LayerFactory::unregisterLayer("Interp");
 }
 
+TEST(Layer_Test_PoolingIndices, Accuracy)
+{
+    Net net;
+
+    LayerParams lp;
+    lp.set("pool", "max");
+    lp.set("kernel_w", 2);
+    lp.set("kernel_h", 2);
+    lp.set("stride_w", 2);
+    lp.set("stride_h", 2);
+    lp.set("pad_w", 0);
+    lp.set("pad_h", 0);
+    lp.name = "testLayer.name";  // This test also checks that OpenCV lets use names with dots.
+    lp.type = "Pooling";
+    net.addLayerToPrev(lp.name, lp.type, lp);
+
+    Mat inp(10, 10, CV_8U);
+    randu(inp, 0, 255);
+
+    Mat maxValues(5, 5, CV_32F, Scalar(-1)), indices(5, 5, CV_32F, Scalar(-1));
+    for (int y = 0; y < 10; ++y)
+    {
+        int dstY = y / 2;
+        for (int x = 0; x < 10; ++x)
+        {
+            int dstX = x / 2;
+            uint8_t val = inp.at<uint8_t>(y, x);
+            if ((float)inp.at<uint8_t>(y, x) > maxValues.at<float>(dstY, dstX))
+            {
+                maxValues.at<float>(dstY, dstX) = val;
+                indices.at<float>(dstY, dstX) = y * 10 + x;
+            }
+        }
+    }
+    net.setInput(blobFromImage(inp));
+
+    std::vector<Mat> outputs;
+    net.forward(outputs, lp.name);
+    normAssert(maxValues, outputs[0].reshape(1, 5));
+    normAssert(indices, outputs[1].reshape(1, 5));
+}
+
 }} // namespace
index a8c1d15..5fe3fe1 100644 (file)
@@ -87,7 +87,7 @@ static void runTorchNet(String prefix, int targetId = DNN_TARGET_CPU, String out
     if (outLayerName.empty())
         outLayerName = net.getLayerNames().back();
 
-    net.setInput(inp, "0");
+    net.setInput(inp);
     std::vector<Mat> outBlobs;
     net.forward(outBlobs, outLayerName);
     normAssert(outRef, outBlobs[0]);