From 09d910191e986c2fa0477b7274669c24d721f3c7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EC=9C=A4=ED=98=84=EC=8B=9D/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Principal=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Tue, 27 Nov 2018 11:40:08 +0900 Subject: [PATCH] [enco/tfl/frontend] Padding when top == bottom -1 or left = right-1 (#2411) * [enco/tfl/frontend] Padding when top == bottom -1 or left = right-1 get_padding() now calculates padding when top == bottom -1 or left = right-1 Signed-off-by: Hyun Sik Yoon * pr fix: calculate -> compute, ifm -> in_size --- contrib/enco/frontend/tflite/src/Op/Padding.cpp | 31 ++++++++++++----- contrib/enco/test/tflite/Conv2D_004/INFERENCE | 0 contrib/enco/test/tflite/Conv2D_004/test.recipe | 45 +++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 9 deletions(-) create mode 100644 contrib/enco/test/tflite/Conv2D_004/INFERENCE create mode 100644 contrib/enco/test/tflite/Conv2D_004/test.recipe diff --git a/contrib/enco/frontend/tflite/src/Op/Padding.cpp b/contrib/enco/frontend/tflite/src/Op/Padding.cpp index 7edaee9..5c2b5f7 100644 --- a/contrib/enco/frontend/tflite/src/Op/Padding.cpp +++ b/contrib/enco/frontend/tflite/src/Op/Padding.cpp @@ -27,6 +27,7 @@ #include #include +#include using namespace nncc::core::ADT; @@ -39,17 +40,28 @@ coco::Padding2D get_padding(const tensor::Shape &ifm_shape, const int kernel_w, { assert(ifm_shape.rank() == 4); + /** + * Compute [top padding + bottom padding] (or [left padding + right padding]). + * If this returns an even number, top = return value / 2 and bottom = return value - top + * If this returns an odd number, top = return value / 2 and bottom = return value - top (so, + * bottom = top + 1) + * + * Code based on https://www.tensorflow.org/api_guides/python/nn#Convolution + */ auto compute_padding = [](tflite::Padding padding, int stride, int dilation_rate, int in_size, int filter_size) { - // code from tensorflow lite 1.9 int effective_filter_size = (filter_size - 1) * dilation_rate + 1; - int out_size = (padding == tflite::Padding_SAME) - ? (in_size + stride - 1) / stride - : (padding == tflite::Padding_VALID) - ? (in_size - effective_filter_size + stride) / stride - : 0; - int value = ((out_size - 1) * stride + effective_filter_size - in_size) / 2; - return value > 0 ? value : 0; + if (padding == tflite::Padding_SAME) + { + if (in_size % stride == 0) + return std::max(effective_filter_size - stride, 0); + else + return std::max(effective_filter_size - (in_size % stride), 0); + } + else // padding == VALID + { + return 0; + } }; // ifm shape is from order of NHWC. ifm W = dim(2), ifm H = dim(1) @@ -57,7 +69,8 @@ coco::Padding2D get_padding(const tensor::Shape &ifm_shape, const int kernel_w, int padding_h = compute_padding(padding, stride_h, dilation_h_factor, ifm_shape.dim(1), kernel_h); coco::Padding2D coco_padding; - coco_padding.top(padding_h).bottom(padding_h).left(padding_w).right(padding_w); + coco_padding.top(padding_h / 2).bottom(padding_h - padding_h / 2); + coco_padding.left(padding_w / 2).right(padding_w - padding_w / 2); return coco_padding; } diff --git a/contrib/enco/test/tflite/Conv2D_004/INFERENCE b/contrib/enco/test/tflite/Conv2D_004/INFERENCE new file mode 100644 index 0000000..e69de29 diff --git a/contrib/enco/test/tflite/Conv2D_004/test.recipe b/contrib/enco/test/tflite/Conv2D_004/test.recipe new file mode 100644 index 0000000..20f4a99 --- /dev/null +++ b/contrib/enco/test/tflite/Conv2D_004/test.recipe @@ -0,0 +1,45 @@ +# Conv2D with ifm w, h = 14, 14 && ofm w, h = 7, 7 && stride = 2, 2 && padding = SAME (similar case from Mobile) +operand { + name: "ifm" + type: FLOAT32 + shape { dim: 1 dim: 14 dim: 14 dim: 2 } +} +operand { + name: "ker" + type: FLOAT32 + shape { dim: 1 dim: 3 dim: 3 dim: 2 } + filler { + tag: "gaussian" + arg: "0.0" + arg: "1.0" + } +} +operand { + name: "bias" + type: FLOAT32 + shape { dim: 1 } + filler { + tag: "gaussian" + arg: "0.0" + arg: "1.0" + } +} +operand { + name: "ofm" + type: FLOAT32 + shape { dim: 1 dim: 7 dim: 7 dim: 1 } +} +operation { + type: "Conv2D" + conv2d_options { + padding: SAME + stride_w: 2 + stride_h: 2 + } + input: "ifm" + input: "ker" + input: "bias" + output: "ofm" +} +input: "ifm" +output: "ofm" -- 2.7.4