• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #ifndef TENSORFLOW_LITE_KERNELS_PADDING_H_
16 #define TENSORFLOW_LITE_KERNELS_PADDING_H_
17 
18 #include "tensorflow/lite/c/builtin_op_data.h"
19 
20 namespace tflite {
21 
ComputePadding(int stride,int dilation_rate,int in_size,int filter_size,int out_size)22 inline int ComputePadding(int stride, int dilation_rate, int in_size,
23                           int filter_size, int out_size) {
24   int effective_filter_size = (filter_size - 1) * dilation_rate + 1;
25   int padding = ((out_size - 1) * stride + effective_filter_size - in_size) / 2;
26   return padding > 0 ? padding : 0;
27 }
28 
29 // Matching GetWindowedOutputSize in TensorFlow.
ComputeOutSize(TfLitePadding padding,int image_size,int filter_size,int stride)30 inline int ComputeOutSize(TfLitePadding padding, int image_size,
31                           int filter_size, int stride) {
32   switch (padding) {
33     case kTfLitePaddingSame:
34       return (image_size + stride - 1) / stride;
35     case kTfLitePaddingValid:
36       return (image_size + stride - filter_size) / stride;
37     default:
38       return 0;
39   }
40 }
41 
ComputePaddingHeightWidth(int stride_height,int stride_width,int dilation_rate,int in_height,int in_width,int filter_height,int filter_width,TfLitePadding padding)42 inline TfLitePaddingValues ComputePaddingHeightWidth(
43     int stride_height, int stride_width, int dilation_rate, int in_height,
44     int in_width, int filter_height, int filter_width, TfLitePadding padding) {
45   int out_width = ComputeOutSize(padding, in_width, filter_width, stride_width);
46   int out_height =
47       ComputeOutSize(padding, in_height, filter_height, stride_height);
48 
49   TfLitePaddingValues padding_values;
50   padding_values.height =
51       ComputePadding(stride_height, 1, in_height, filter_height, out_height);
52   padding_values.width =
53       ComputePadding(stride_width, 1, in_width, filter_width, out_width);
54   return padding_values;
55 }
56 }  // namespace tflite
57 
58 #endif  // TENSORFLOW_LITE_KERNELS_PADDING_H_
59