[nnc] Assume SAME padding in Caffe importer (#1011)
authorDmitry Mozolev/AI Tools Lab /SRR/Engineer/삼성전자 <d.mozolev@samsung.com>
Tue, 14 Aug 2018 14:27:26 +0000 (17:27 +0300)
committerSergey Vostokov/AI Tools Lab /SRR/Staff Engineer/삼성전자 <s.vostokov@samsung.com>
Tue, 14 Aug 2018 14:27:26 +0000 (17:27 +0300)
Layers have numeric padding parameters in Caffe, as oppopsed to
SAME/VALID padding in Tensorflow Lite and Model IR.

Currently,
during the model import stage there is no information about tensor
shapes, so it is impossible to map numeric paddings to SAME/VALID
paddings correctly.

Despite that, current reference models for Caffe do follow the rule of
"if pad is zero then padding is VALID, otherwise SAME",
so this commit does exactly that (before, Caffe importer would
throw an exception if it found non-zero non-equal paddings for height
and width; but inception-v3 has non-square convolutions which lead to
exactly such case).

Signed-off-by: Dmitry Mozolev <d.mozolev@samsung.com>
contrib/nnc/libs/frontend/caffe/src/caffe_op_creator.cpp

index 5c2630c..edf18df 100644 (file)
@@ -95,21 +95,24 @@ static ops::PaddingType getPadTypeFromOneValue(bool hasPad, unsigned int pad)
   }
 }
 
+/**
+ * @brief Determine padding type (SAME/VALID) from two numeric values - height and width padding.
+ * In general, knowing the input tensor shape is required to properly determine the padding type.
+ * Also, these two padding values might not map to SAME or VALID padding.
+ * @todo Change cout to logging call.
+ */
 static ops::PaddingType getPadTypeFromTwoValues(unsigned int pad1, unsigned int pad2)
 {
   bool areBothPadsZero = pad1 == 0 && pad2 == 0;
-  bool doPadsDiffer = pad1 != pad2;
   if (areBothPadsZero)
   {
     return ops::PaddingType::Valid;
   }
-  else if (doPadsDiffer)
-  {
-    std::string pads = std::to_string(pad1) + ", " + std::to_string(pad2);
-    throw PluginException(std::string("Unsupported pads: ") + pads);
-  }
   else
   {
+    std::string pads = "[" + std::to_string(pad1) + ", " + std::to_string(pad2) + "]";
+    std::cout << "WARNING! Encountered padding " << pads
+              << ", assuming padding SAME, but it is not guaranteed to be correct." << std::endl;
     return ops::PaddingType::Same;
   }
 }