[mir_caffe2] Switch to AvgPool2D and MaxPool2D ops (#7032)
authorСергей Баранников/AI Tools Lab /SRR/Engineer/삼성전자 <s.barannikov@samsung.com>
Thu, 29 Aug 2019 21:52:24 +0000 (06:52 +0900)
committerAlexander Efimov/AI Tools Lab/./Samsung Electronics <a.efimov@samsung.com>
Thu, 29 Aug 2019 21:52:24 +0000 (00:52 +0300)
Switch from `PoolOp` to `AvgPool2DOp` and `MaxPool2DOp`. The former ones are deprecated.

Signed-off-by: Sergei Barannikov <s.barannikov@samsung.com>
compiler/mir-caffe2-importer/caffe2_op_creator.cpp

index 430fbad..46b0125 100644 (file)
 #include "caffe2_proto_helper.h"
 
 #include "mir/ops/AddOp.h"
+#include "mir/ops/AvgPool2DOp.h"
 #include "mir/ops/CappedReluOp.h"
 #include "mir/ops/ConcatOp.h"
 #include "mir/ops/ConstantOp.h"
 #include "mir/ops/Conv2DOp.h"
 #include "mir/ops/DepthwiseConv2DOp.h"
 #include "mir/ops/FullyConnectedOp.h"
+#include "mir/ops/MaxPool2DOp.h"
 #include "mir/ops/MulOp.h"
-#include "mir/ops/PoolOp.h"
 #include "mir/ops/ReluOp.h"
 #include "mir/ops/ReshapeOp.h"
 #include "mir/ops/ResizeOp.h"
@@ -145,13 +146,13 @@ getPadding(const ::caffe2::OperatorDef &op)
   return {{pad, pad}, {pad, pad}};
 }
 
-static std::vector<int32_t> getStrides(const ::caffe2::OperatorDef &op)
+static std::vector<std::int32_t> getStrides(const ::caffe2::OperatorDef &op)
 {
-  std::vector<int32_t> strides;
+  std::vector<std::int32_t> strides;
 
   if (hasArgument(op.arg(), "stride"))
   {
-    int stride = getSingleArgument(op, "stride", 1);
+    std::int32_t stride = getSingleArgument(op, "stride", 1);
     strides = {stride, stride};
   }
 
@@ -168,8 +169,8 @@ static std::vector<int32_t> getStrides(const ::caffe2::OperatorDef &op)
   return strides;
 }
 
-static Shape getWindowShape(const ::caffe2::OperatorDef &op,
-                            const std::vector<mir::Operation::Output *> &inputs)
+static std::vector<std::int32_t> getWindowSize(const ::caffe2::OperatorDef &op,
+                                               const std::vector<mir::Operation::Output *> &inputs)
 {
   int is_global_pooling = getSingleArgument(op, "global_pooling", 0);
   bool has_custom_kernel_size =
@@ -180,7 +181,7 @@ static Shape getWindowShape(const ::caffe2::OperatorDef &op,
   if (is_global_pooling)
   {
     const auto &input_shape = inputs[0]->getShape();
-    assert(input_shape.rank() == 4 && "getWindowShape() inputs must be of rank 4");
+    assert(input_shape.rank() == 4 && "getWindowSize() inputs must be of rank 4");
     kernel_h = input_shape.dim(2);
     kernel_w = input_shape.dim(3);
   }
@@ -210,7 +211,7 @@ static Shape getWindowShape(const ::caffe2::OperatorDef &op,
       }
     }
   }
-  return Shape{kernel_h, kernel_w};
+  return {kernel_h, kernel_w};
 }
 
 mir::Operation::Output *Caffe2OpCreator::convertCaffeToMIR(mir::Operation::Output *arg)
@@ -335,26 +336,27 @@ Caffe2OpCreator::convertAdd(const std::vector<mir::Operation::Output *> &inputs,
   auto result = createOp<ops::AddOp>(inputs[0], inputs[1])->getOutput(0);
   return {result};
 }
+
 std::vector<mir::Operation::Output *>
 Caffe2OpCreator::convertAveragePool(const std::vector<mir::Operation::Output *> &inputs,
                                     const OperatorDef &op)
 {
   checkConvLikeOp(op);
 
-  Shape window_shape = getWindowShape(op, inputs);
-
-  Shape strides(getStrides(op));
-
-  ops::PoolOp::PoolingType pool_type = ops::PoolOp::PoolingType::AVG;
-  ops::PoolOp::BorderType border_type = ops::PoolOp::BorderType::EMPTY;
+  assert(inputs.size() == 1);
+  auto input = inputs[0];
 
-  std::vector<int32_t> pad_before, pad_after;
+  const auto window_size = getWindowSize(op, inputs);
+  const auto strides = getStrides(op);
+  std::vector<std::int32_t> pad_before, pad_after;
   std::tie(pad_before, pad_after) = getPadding(op);
 
-  auto pooling = createOp<ops::PoolOp>(convertCaffeToMIR(inputs[0]), pool_type, window_shape,
-                                       strides, pad_before, pad_after, border_type);
-
-  return {convertMIRToCaffe(pooling->getOutput(0))};
+  input = convertCaffeToMIR(input);
+  auto result = createOp<ops::AvgPool2DOp>(input, window_size, strides, pad_before, pad_after,
+                                           false, mir::DataFormat::NHWC)
+                    ->getOutput(0);
+  result = convertMIRToCaffe(result);
+  return {result};
 }
 
 std::vector<mir::Operation::Output *>
@@ -460,19 +462,20 @@ Caffe2OpCreator::convertMaxPool(const std::vector<mir::Operation::Output *> &inp
 {
   checkConvLikeOp(op);
 
-  Shape window_shape = getWindowShape(op, inputs);
-  Shape strides(getStrides(op));
-
-  ops::PoolOp::PoolingType pool_type = ops::PoolOp::PoolingType::MAX;
-  ops::PoolOp::BorderType border_type = ops::PoolOp::BorderType::EMPTY;
+  assert(inputs.size() == 1);
+  auto input = inputs[0];
 
-  std::vector<int32_t> pad_before, pad_after;
+  const auto window_size = getWindowSize(op, inputs);
+  const auto strides = getStrides(op);
+  std::vector<std::int32_t> pad_before, pad_after;
   std::tie(pad_before, pad_after) = getPadding(op);
 
-  auto pooling = createOp<ops::PoolOp>(convertCaffeToMIR(inputs[0]), pool_type, window_shape,
-                                       strides, pad_before, pad_after, border_type);
-
-  return {convertMIRToCaffe(pooling->getOutput(0))};
+  input = convertCaffeToMIR(input);
+  auto result = createOp<ops::MaxPool2DOp>(input, window_size, strides, pad_before, pad_after,
+                                           mir::DataFormat::NHWC)
+                    ->getOutput(0);
+  result = convertMIRToCaffe(result);
+  return {result};
 }
 
 std::vector<mir::Operation::Output *>