Daniel Konegen / MNIST_example

Dependencies:   mbed-os

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers depthwiseconv_float.h Source File

depthwiseconv_float.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_INTERNAL_REFERENCE_DEPTHWISECONV_FLOAT_H_
00016 #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_DEPTHWISECONV_FLOAT_H_
00017 
00018 #include "tensorflow/lite/kernels/internal/common.h"
00019 #include "tensorflow/lite/kernels/internal/compatibility.h"
00020 #include "tensorflow/lite/kernels/internal/types.h"
00021 
00022 namespace tflite {
00023 namespace reference_ops {
00024 
00025 inline void DepthwiseConv(
00026     const DepthwiseParams& params, const RuntimeShape& input_shape,
00027     const float* input_data, const RuntimeShape& filter_shape,
00028     const float* filter_data, const RuntimeShape& bias_shape,
00029     const float* bias_data, const RuntimeShape& output_shape,
00030     float* output_data) {
00031   const int stride_width = params.stride_width;
00032   const int stride_height = params.stride_height;
00033   const int dilation_width_factor = params.dilation_width_factor;
00034   const int dilation_height_factor = params.dilation_height_factor;
00035   const int pad_width = params.padding_values.width;
00036   const int pad_height = params.padding_values.height;
00037   const int depth_multiplier = params.depth_multiplier;
00038   const float output_activation_min = params.float_activation_min;
00039   const float output_activation_max = params.float_activation_max;
00040   TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4);
00041   TFLITE_DCHECK_EQ(filter_shape.DimensionsCount(), 4);
00042   TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4);
00043 
00044   const int batches = MatchingDim(input_shape, 0, output_shape, 0);
00045   const int output_depth = MatchingDim(filter_shape, 3, output_shape, 3);
00046   const int input_height = input_shape.Dims(1);
00047   const int input_width = input_shape.Dims(2);
00048   const int input_depth = input_shape.Dims(3);
00049   const int filter_height = filter_shape.Dims(1);
00050   const int filter_width = filter_shape.Dims(2);
00051   const int output_height = output_shape.Dims(1);
00052   const int output_width = output_shape.Dims(2);
00053   TFLITE_DCHECK_EQ(output_depth, input_depth * depth_multiplier);
00054   TFLITE_DCHECK_EQ(bias_shape.FlatSize(), output_depth);
00055 
00056   for (int b = 0; b < batches; ++b) {
00057     for (int out_y = 0; out_y < output_height; ++out_y) {
00058       for (int out_x = 0; out_x < output_width; ++out_x) {
00059         for (int ic = 0; ic < input_depth; ++ic) {
00060           for (int m = 0; m < depth_multiplier; m++) {
00061             const int oc = m + ic * depth_multiplier;
00062             const int in_x_origin = (out_x * stride_width) - pad_width;
00063             const int in_y_origin = (out_y * stride_height) - pad_height;
00064             float total = 0.f;
00065             for (int filter_y = 0; filter_y < filter_height; ++filter_y) {
00066               for (int filter_x = 0; filter_x < filter_width; ++filter_x) {
00067                 const int in_x = in_x_origin + dilation_width_factor * filter_x;
00068                 const int in_y =
00069                     in_y_origin + dilation_height_factor * filter_y;
00070                 // If the location is outside the bounds of the input image,
00071                 // use zero as a default value.
00072                 if ((in_x >= 0) && (in_x < input_width) && (in_y >= 0) &&
00073                     (in_y < input_height)) {
00074                   float input_value =
00075                       input_data[Offset(input_shape, b, in_y, in_x, ic)];
00076                   float filter_value = filter_data[Offset(
00077                       filter_shape, 0, filter_y, filter_x, oc)];
00078                   total += (input_value * filter_value);
00079                 }
00080               }
00081             }
00082             float bias_value = 0.0f;
00083             if (bias_data) {
00084               bias_value = bias_data[oc];
00085             }
00086             output_data[Offset(output_shape, b, out_y, out_x, oc)] =
00087                 ActivationFunctionWithMinMax(total + bias_value,
00088                                              output_activation_min,
00089                                              output_activation_max);
00090           }
00091         }
00092       }
00093     }
00094   }
00095 }
00096 
00097 }  // end namespace reference_ops
00098 }  // end namespace tflite
00099 
00100 #endif  // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_DEPTHWISECONV_FLOAT_H_