1 /* Copyright 2021 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_INTERNAL_REFERENCE_CONV3D_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_CONV3D_H_
17
18 #include "tensorflow/lite/kernels/internal/common.h"
19 #include "tensorflow/lite/kernels/internal/types.h"
20
21 namespace tflite {
22 namespace reference_ops {
23
Conv3D(const Conv3DParams & params,const RuntimeShape & input_shape,const float * input_data,const RuntimeShape & filter_shape,const float * filter_data,const RuntimeShape & bias_shape,const float * bias_data,const RuntimeShape & output_shape,float * output_data)24 inline void Conv3D(const Conv3DParams& params, const RuntimeShape& input_shape,
25 const float* input_data, const RuntimeShape& filter_shape,
26 const float* filter_data, const RuntimeShape& bias_shape,
27 const float* bias_data, const RuntimeShape& output_shape,
28 float* output_data) {
29 TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 5);
30 TFLITE_DCHECK_EQ(filter_shape.DimensionsCount(), 5);
31 TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 5);
32
33 const int batches = MatchingDim(input_shape, 0, output_shape, 0);
34 const int input_num_channels = MatchingDim(input_shape, 4, filter_shape, 3);
35 const int output_num_channels = MatchingDim(filter_shape, 4, output_shape, 4);
36 if (bias_data) {
37 TFLITE_DCHECK_EQ(bias_shape.FlatSize(), output_num_channels);
38 }
39
40 // Only NDHWC format is currently supported.
41 const int input_width = input_shape.Dims(3);
42 const int input_height = input_shape.Dims(2);
43 const int input_depth = input_shape.Dims(1);
44 const int filter_width = filter_shape.Dims(2);
45 const int filter_height = filter_shape.Dims(1);
46 const int filter_depth = filter_shape.Dims(0);
47 const int output_width = output_shape.Dims(3);
48 const int output_height = output_shape.Dims(2);
49 const int output_depth = output_shape.Dims(1);
50 const int pad_width = params.padding_values.width;
51 const int pad_height = params.padding_values.height;
52 const int pad_depth = params.padding_values.depth;
53
54 for (int batch = 0; batch < batches; ++batch) {
55 for (int out_d = 0; out_d < output_depth; ++out_d) {
56 const int in_d_origin = (out_d * params.stride_depth) - pad_depth;
57 for (int out_y = 0; out_y < output_height; ++out_y) {
58 const int in_y_origin = (out_y * params.stride_height) - pad_height;
59 for (int out_x = 0; out_x < output_width; ++out_x) {
60 const int in_x_origin = (out_x * params.stride_width) - pad_width;
61 for (int out_channel = 0; out_channel < output_num_channels;
62 ++out_channel) {
63 float total = 0.f;
64 for (int filter_d = 0; filter_d < filter_depth; ++filter_d) {
65 const int in_d = in_d_origin + params.dilation_depth * filter_d;
66 for (int filter_y = 0; filter_y < filter_height; ++filter_y) {
67 const int in_y =
68 in_y_origin + params.dilation_height * filter_y;
69 for (int filter_x = 0; filter_x < filter_width; ++filter_x) {
70 const int in_x =
71 in_x_origin + params.dilation_width * filter_x;
72
73 // Zero padding by omitting the areas outside the image.
74 const bool is_point_inside_image =
75 (in_x >= 0) && (in_x < input_width) && (in_y >= 0) &&
76 (in_y < input_height) && (in_d >= 0) &&
77 (in_d < input_depth);
78
79 if (!is_point_inside_image) {
80 continue;
81 }
82
83 for (int in_channel = 0; in_channel < input_num_channels;
84 ++in_channel) {
85 float input_value = input_data[Offset(
86 input_shape, batch, in_d, in_y, in_x, in_channel)];
87 float filter_value =
88 filter_data[Offset(filter_shape, filter_d, filter_y,
89 filter_x, in_channel, out_channel)];
90 total += (input_value * filter_value);
91 }
92 }
93 }
94 }
95 float bias_value = 0.0f;
96 if (bias_data) {
97 bias_value = bias_data[out_channel];
98 }
99 output_data[Offset(output_shape, batch, out_d, out_y, out_x,
100 out_channel)] =
101 ActivationFunctionWithMinMax(total + bias_value,
102 params.float_activation_min,
103 params.float_activation_max);
104 }
105 }
106 }
107 }
108 }
109 }
110
111 } // namespace reference_ops
112 } // namespace tflite
113
114 #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_CONV3D_H_
115