Daniel Konegen / MNIST_example

Dependencies:   mbed-os

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers padding.h Source File

padding.h

00001 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
00002 
00003 Licensed under the Apache License, Version 2.0 (the "License");
00004 you may not use this file except in compliance with the License.
00005 You may obtain a copy of the License at
00006 
00007     http://www.apache.org/licenses/LICENSE-2.0
00008 
00009 Unless required by applicable law or agreed to in writing, software
00010 distributed under the License is distributed on an "AS IS" BASIS,
00011 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 See the License for the specific language governing permissions and
00013 limitations under the License.
00014 ==============================================================================*/
00015 #ifndef TENSORFLOW_LITE_KERNELS_PADDING_H_
00016 #define TENSORFLOW_LITE_KERNELS_PADDING_H_
00017 
00018 #include "tensorflow/lite/c/builtin_op_data.h"
00019 
00020 namespace tflite {
00021 
00022 // TODO(renjieliu): Migrate others to use ComputePaddingWithLeftover.
00023 inline int ComputePadding(int stride, int dilation_rate, int in_size,
00024                           int filter_size, int out_size) {
00025   int effective_filter_size = (filter_size - 1) * dilation_rate + 1;
00026   int padding = ((out_size - 1) * stride + effective_filter_size - in_size) / 2;
00027   return padding > 0 ? padding : 0;
00028 }
00029 
00030 // It's not guaranteed that padding is symmetric. It's important to keep
00031 // offset for algorithms need all paddings.
00032 inline int ComputePaddingWithOffset(int stride, int dilation_rate, int in_size,
00033                                     int filter_size, int out_size,
00034                                     int* offset) {
00035   int effective_filter_size = (filter_size - 1) * dilation_rate + 1;
00036   int total_padding =
00037       ((out_size - 1) * stride + effective_filter_size - in_size);
00038   total_padding = total_padding > 0 ? total_padding : 0;
00039   *offset = total_padding % 2;
00040   return total_padding / 2;
00041 }
00042 
00043 // Matching GetWindowedOutputSize in TensorFlow.
00044 inline int ComputeOutSize(TfLitePadding padding, int image_size,
00045                           int filter_size, int stride, int dilation_rate = 1) {
00046   int effective_filter_size = (filter_size - 1) * dilation_rate + 1;
00047   switch (padding) {
00048     case kTfLitePaddingSame:
00049       return (image_size + stride - 1) / stride;
00050     case kTfLitePaddingValid:
00051       return (image_size + stride - effective_filter_size) / stride;
00052     default:
00053       return 0;
00054   }
00055 }
00056 
00057 inline TfLitePaddingValues ComputePaddingHeightWidth(
00058     int stride_height, int stride_width, int dilation_rate_height,
00059     int dilation_rate_width, int in_height, int in_width, int filter_height,
00060     int filter_width, TfLitePadding padding, int* out_height, int* out_width) {
00061   *out_width = ComputeOutSize(padding, in_width, filter_width, stride_width,
00062                               dilation_rate_width);
00063   *out_height = ComputeOutSize(padding, in_height, filter_height, stride_height,
00064                                dilation_rate_height);
00065 
00066   TfLitePaddingValues padding_values;
00067   int offset = 0;
00068   padding_values.height =
00069       ComputePaddingWithOffset(stride_height, dilation_rate_height, in_height,
00070                                filter_height, *out_height, &offset);
00071   padding_values.height_offset = offset;
00072   padding_values.width =
00073       ComputePaddingWithOffset(stride_width, dilation_rate_width, in_width,
00074                                filter_width, *out_width, &offset);
00075   padding_values.width_offset = offset;
00076   return padding_values;
00077 }
00078 }  // namespace tflite
00079 
00080 #endif  // TENSORFLOW_LITE_KERNELS_PADDING_H_