• 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 #include "tensorflow/lite/kernels/internal/types.h"
20 
21 namespace tflite {
22 
23 // TODO(renjieliu): Migrate others to use ComputePaddingWithLeftover.
ComputePadding(int stride,int dilation_rate,int in_size,int filter_size,int out_size)24 inline int ComputePadding(int stride, int dilation_rate, int in_size,
25                           int filter_size, int out_size) {
26   int effective_filter_size = (filter_size - 1) * dilation_rate + 1;
27   int padding = ((out_size - 1) * stride + effective_filter_size - in_size) / 2;
28   return padding > 0 ? padding : 0;
29 }
30 
31 // It's not guaranteed that padding is symmetric. It's important to keep
32 // offset for algorithms need all paddings.
ComputePaddingWithOffset(int stride,int dilation_rate,int in_size,int filter_size,int out_size,int * offset)33 inline int ComputePaddingWithOffset(int stride, int dilation_rate, int in_size,
34                                     int filter_size, int out_size,
35                                     int* offset) {
36   int effective_filter_size = (filter_size - 1) * dilation_rate + 1;
37   int total_padding =
38       ((out_size - 1) * stride + effective_filter_size - in_size);
39   total_padding = total_padding > 0 ? total_padding : 0;
40   *offset = total_padding % 2;
41   return total_padding / 2;
42 }
43 
44 // Matching GetWindowedOutputSize in TensorFlow.
45 inline int ComputeOutSize(TfLitePadding padding, int image_size,
46                           int filter_size, int stride, int dilation_rate = 1) {
47   int effective_filter_size = (filter_size - 1) * dilation_rate + 1;
48   switch (padding) {
49     case kTfLitePaddingSame:
50       return (image_size + stride - 1) / stride;
51     case kTfLitePaddingValid:
52       return (image_size + stride - effective_filter_size) / stride;
53     default:
54       return 0;
55   }
56 }
57 
ComputePaddingHeightWidth(int stride_height,int stride_width,int dilation_rate_height,int dilation_rate_width,int in_height,int in_width,int filter_height,int filter_width,TfLitePadding padding,int * out_height,int * out_width)58 inline TfLitePaddingValues ComputePaddingHeightWidth(
59     int stride_height, int stride_width, int dilation_rate_height,
60     int dilation_rate_width, int in_height, int in_width, int filter_height,
61     int filter_width, TfLitePadding padding, int* out_height, int* out_width) {
62   *out_width = ComputeOutSize(padding, in_width, filter_width, stride_width,
63                               dilation_rate_width);
64   *out_height = ComputeOutSize(padding, in_height, filter_height, stride_height,
65                                dilation_rate_height);
66 
67   TfLitePaddingValues padding_values;
68   int offset = 0;
69   padding_values.height =
70       ComputePaddingWithOffset(stride_height, dilation_rate_height, in_height,
71                                filter_height, *out_height, &offset);
72   padding_values.height_offset = offset;
73   padding_values.width =
74       ComputePaddingWithOffset(stride_width, dilation_rate_width, in_width,
75                                filter_width, *out_width, &offset);
76   padding_values.width_offset = offset;
77   return padding_values;
78 }
79 
ComputePadding3DValues(int stride_height,int stride_width,int stride_depth,int dilation_rate_height,int dilation_rate_width,int dilation_rate_depth,int in_height,int in_width,int in_depth,int filter_height,int filter_width,int filter_depth,TfLitePadding padding,int * out_height,int * out_width,int * out_depth)80 inline Padding3DValues ComputePadding3DValues(
81     int stride_height, int stride_width, int stride_depth,
82     int dilation_rate_height, int dilation_rate_width, int dilation_rate_depth,
83     int in_height, int in_width, int in_depth, int filter_height,
84     int filter_width, int filter_depth, TfLitePadding padding, int* out_height,
85     int* out_width, int* out_depth) {
86   *out_width = ComputeOutSize(padding, in_width, filter_width, stride_width,
87                               dilation_rate_width);
88   *out_height = ComputeOutSize(padding, in_height, filter_height, stride_height,
89                                dilation_rate_height);
90   *out_depth = ComputeOutSize(padding, in_depth, filter_depth, stride_depth,
91                               dilation_rate_depth);
92 
93   Padding3DValues padding_values;
94   int offset = 0;
95   padding_values.depth =
96       ComputePaddingWithOffset(stride_depth, dilation_rate_depth, in_depth,
97                                filter_depth, *out_depth, &offset);
98   padding_values.depth_offset = offset;
99   padding_values.height =
100       ComputePaddingWithOffset(stride_height, dilation_rate_height, in_height,
101                                filter_height, *out_height, &offset);
102   padding_values.height_offset = offset;
103   padding_values.width =
104       ComputePaddingWithOffset(stride_width, dilation_rate_width, in_width,
105                                filter_width, *out_width, &offset);
106   padding_values.width_offset = offset;
107   return padding_values;
108 }
109 }  // namespace tflite
110 
111 #endif  // TENSORFLOW_LITE_KERNELS_PADDING_H_
112