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_CONTRIB_LITE_KERNELS_INTERNAL_REFERENCE_DEPTHWISECONV_FLOAT_H_ 16 #define TENSORFLOW_CONTRIB_LITE_KERNELS_INTERNAL_REFERENCE_DEPTHWISECONV_FLOAT_H_ 17 18 #include "tensorflow/contrib/lite/kernels/internal/common.h" 19 #include "tensorflow/contrib/lite/kernels/internal/compatibility.h" 20 #include "tensorflow/contrib/lite/kernels/internal/types.h" 21 22 namespace tflite { 23 namespace reference_ops { 24 25 inline void DepthwiseConv(const float* input_data, const Dims<4>& input_dims, 26 const float* filter_data, const Dims<4>& filter_dims, 27 const float* bias_data, const Dims<4>& bias_dims, 28 int stride_width, int stride_height, int pad_width, 29 int pad_height, int depth_multiplier, 30 float output_activation_min, 31 float output_activation_max, float* output_data, 32 const Dims<4>& output_dims) { 33 const int batches = MatchingArraySize(input_dims, 3, output_dims, 3); 34 const int output_depth = MatchingArraySize(filter_dims, 0, output_dims, 0); 35 const int input_height = ArraySize(input_dims, 2); 36 const int input_width = ArraySize(input_dims, 1); 37 const int input_depth = ArraySize(input_dims, 0); 38 const int filter_height = ArraySize(filter_dims, 2); 39 const int filter_width = ArraySize(filter_dims, 1); 40 const int output_height = ArraySize(output_dims, 2); 41 const int output_width = ArraySize(output_dims, 1); 42 TFLITE_DCHECK(output_depth == input_depth * depth_multiplier); 43 44 for (int b = 0; b < batches; ++b) { 45 for (int out_y = 0; out_y < output_height; ++out_y) { 46 for (int out_x = 0; out_x < output_width; ++out_x) { 47 for (int ic = 0; ic < input_depth; ++ic) { 48 for (int m = 0; m < depth_multiplier; m++) { 49 const int oc = m + ic * depth_multiplier; 50 const int in_x_origin = (out_x * stride_width) - pad_width; 51 const int in_y_origin = (out_y * stride_height) - pad_height; 52 float total = 0.f; 53 for (int filter_y = 0; filter_y < filter_height; ++filter_y) { 54 for (int filter_x = 0; filter_x < filter_width; ++filter_x) { 55 const int in_x = in_x_origin + filter_x; 56 const int in_y = in_y_origin + filter_y; 57 // If the location is outside the bounds of the input image, 58 // use zero as a default value. 59 if ((in_x >= 0) && (in_x < input_width) && (in_y >= 0) && 60 (in_y < input_height)) { 61 float input_value = 62 input_data[Offset(input_dims, ic, in_x, in_y, b)]; 63 float filter_value = filter_data[Offset( 64 filter_dims, oc, filter_x, filter_y, 0)]; 65 total += (input_value * filter_value); 66 } 67 } 68 } 69 float bias_value = 0.0f; 70 if (bias_data) { 71 bias_value = bias_data[Offset(bias_dims, oc, 0, 0, 0)]; 72 } 73 output_data[Offset(output_dims, oc, out_x, out_y, b)] = 74 ActivationFunctionWithMinMax(total + bias_value, 75 output_activation_min, 76 output_activation_max); 77 } 78 } 79 } 80 } 81 } 82 } 83 84 // Legacy, for compatibility with old checked-in code. 85 template <FusedActivationFunctionType Ac> 86 void DepthwiseConv(const float* input_data, const Dims<4>& input_dims, 87 const float* filter_data, const Dims<4>& filter_dims, 88 const float* bias_data, const Dims<4>& bias_dims, 89 int stride_width, int stride_height, int pad_width, 90 int pad_height, int depth_multiplier, float* output_data, 91 const Dims<4>& output_dims) { 92 float output_activation_min, output_activation_max; 93 GetActivationMinMax(Ac, &output_activation_min, &output_activation_max); 94 DepthwiseConv(input_data, input_dims, filter_data, filter_dims, bias_data, 95 bias_dims, stride_width, stride_height, pad_width, pad_height, 96 depth_multiplier, output_activation_min, output_activation_max, 97 output_data, output_dims); 98 } 99 100 // Legacy, for compatibility with old checked-in code. 101 template <FusedActivationFunctionType Ac> 102 void DepthwiseConv(const float* input_data, const Dims<4>& input_dims, 103 const float* filter_data, const Dims<4>& filter_dims, 104 const float* bias_data, const Dims<4>& bias_dims, int stride, 105 int pad_width, int pad_height, int depth_multiplier, 106 float* output_data, const Dims<4>& output_dims) { 107 DepthwiseConv<Ac>(input_data, input_dims, filter_data, filter_dims, bias_data, 108 bias_dims, stride, stride, pad_width, pad_height, 109 depth_multiplier, output_data, output_dims); 110 } 111 112 } // end namespace reference_ops 113 } // end namespace tflite 114 115 #endif // TENSORFLOW_CONTRIB_LITE_KERNELS_INTERNAL_REFERENCE_DEPTHWISECONV_FLOAT_H_ 116