|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef TENSORFLOW_LITE_KERNELS_PADDING_H_ |
|
|
#define TENSORFLOW_LITE_KERNELS_PADDING_H_ |
|
|
|
|
|
#include "edge-impulse-sdk/tensorflow/lite/c/builtin_op_data.h" |
|
|
#include "edge-impulse-sdk/tensorflow/lite/kernels/internal/types.h" |
|
|
|
|
|
namespace tflite { |
|
|
|
|
|
|
|
|
inline int ComputePadding(int stride, int dilation_rate, int in_size, |
|
|
int filter_size, int out_size) { |
|
|
int effective_filter_size = (filter_size - 1) * dilation_rate + 1; |
|
|
int padding = ((out_size - 1) * stride + effective_filter_size - in_size) / 2; |
|
|
return padding > 0 ? padding : 0; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
inline int ComputePaddingWithOffset(int stride, int dilation_rate, int in_size, |
|
|
int filter_size, int out_size, |
|
|
int* offset) { |
|
|
int effective_filter_size = (filter_size - 1) * dilation_rate + 1; |
|
|
int total_padding = |
|
|
((out_size - 1) * stride + effective_filter_size - in_size); |
|
|
total_padding = total_padding > 0 ? total_padding : 0; |
|
|
*offset = total_padding % 2; |
|
|
return total_padding / 2; |
|
|
} |
|
|
|
|
|
|
|
|
inline int ComputeOutSize(TfLitePadding padding, int image_size, |
|
|
int filter_size, int stride, int dilation_rate = 1) { |
|
|
int effective_filter_size = (filter_size - 1) * dilation_rate + 1; |
|
|
|
|
|
|
|
|
|
|
|
if (stride == 0) return 0; |
|
|
|
|
|
switch (padding) { |
|
|
case kTfLitePaddingSame: |
|
|
return (image_size + stride - 1) / stride; |
|
|
case kTfLitePaddingValid: |
|
|
return (image_size + stride - effective_filter_size) / stride; |
|
|
default: |
|
|
return 0; |
|
|
} |
|
|
} |
|
|
|
|
|
inline TfLitePaddingValues 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) { |
|
|
*out_width = ComputeOutSize(padding, in_width, filter_width, stride_width, |
|
|
dilation_rate_width); |
|
|
*out_height = ComputeOutSize(padding, in_height, filter_height, stride_height, |
|
|
dilation_rate_height); |
|
|
|
|
|
TfLitePaddingValues padding_values; |
|
|
int offset = 0; |
|
|
padding_values.height = |
|
|
ComputePaddingWithOffset(stride_height, dilation_rate_height, in_height, |
|
|
filter_height, *out_height, &offset); |
|
|
padding_values.height_offset = offset; |
|
|
padding_values.width = |
|
|
ComputePaddingWithOffset(stride_width, dilation_rate_width, in_width, |
|
|
filter_width, *out_width, &offset); |
|
|
padding_values.width_offset = offset; |
|
|
return padding_values; |
|
|
} |
|
|
|
|
|
inline Padding3DValues 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) { |
|
|
*out_width = ComputeOutSize(padding, in_width, filter_width, stride_width, |
|
|
dilation_rate_width); |
|
|
*out_height = ComputeOutSize(padding, in_height, filter_height, stride_height, |
|
|
dilation_rate_height); |
|
|
*out_depth = ComputeOutSize(padding, in_depth, filter_depth, stride_depth, |
|
|
dilation_rate_depth); |
|
|
|
|
|
Padding3DValues padding_values; |
|
|
int offset = 0; |
|
|
padding_values.depth = |
|
|
ComputePaddingWithOffset(stride_depth, dilation_rate_depth, in_depth, |
|
|
filter_depth, *out_depth, &offset); |
|
|
padding_values.depth_offset = offset; |
|
|
padding_values.height = |
|
|
ComputePaddingWithOffset(stride_height, dilation_rate_height, in_height, |
|
|
filter_height, *out_height, &offset); |
|
|
padding_values.height_offset = offset; |
|
|
padding_values.width = |
|
|
ComputePaddingWithOffset(stride_width, dilation_rate_width, in_width, |
|
|
filter_width, *out_width, &offset); |
|
|
padding_values.width_offset = offset; |
|
|
return padding_values; |
|
|
} |
|
|
} |
|
|
|
|
|
#endif |
|
|
|