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
16 #include "tensorflow/lite/kernels/internal/reference/resize_nearest_neighbor.h"
17
18 #include "tensorflow/lite/c/builtin_op_data.h"
19 #include "tensorflow/lite/c/common.h"
20 #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
21 #include "tensorflow/lite/kernels/kernel_util.h"
22 #include "tensorflow/lite/kernels/op_macros.h"
23 #include "tensorflow/lite/micro/kernels/kernel_util.h"
24
25 namespace tflite {
26 namespace ops {
27 namespace micro {
28 namespace resize_nearest_neighbor {
29
30 constexpr int kInputTensor = 0;
31 constexpr int kSizeTensor = 1;
32 constexpr int kOutputTensor = 0;
33
Prepare(TfLiteContext * context,TfLiteNode * node)34 TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
35 TF_LITE_ENSURE_EQ(context, NumInputs(node), 2);
36 TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1);
37
38 const TfLiteTensor* input = GetInput(context, node, kInputTensor);
39 const TfLiteTensor* size = GetInput(context, node, kSizeTensor);
40 TfLiteTensor* output = GetOutput(context, node, kOutputTensor);
41
42 // Our current implementations rely on the input being 4D,
43 // and the size being 1D tensor with exactly 2 elements.
44 TF_LITE_ENSURE_EQ(context, NumDimensions(input), 4);
45 TF_LITE_ENSURE_EQ(context, NumDimensions(size), 1);
46 TF_LITE_ENSURE_EQ(context, size->type, kTfLiteInt32);
47 TF_LITE_ENSURE_EQ(context, size->dims->data[0], 2);
48
49 output->type = input->type;
50
51 if (!IsConstantTensor(size)) {
52 TF_LITE_KERNEL_LOG(context, "Dynamic tensors are unsupported in tfmicro.");
53 return kTfLiteError;
54 }
55 return kTfLiteOk;
56 }
57
Eval(TfLiteContext * context,TfLiteNode * node)58 TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
59 auto* params =
60 reinterpret_cast<TfLiteResizeNearestNeighborParams*>(node->builtin_data);
61
62 const TfLiteEvalTensor* input =
63 tflite::micro::GetEvalInput(context, node, kInputTensor);
64 const TfLiteEvalTensor* size =
65 tflite::micro::GetEvalInput(context, node, kSizeTensor);
66 TfLiteEvalTensor* output =
67 tflite::micro::GetEvalOutput(context, node, kOutputTensor);
68
69 tflite::ResizeNearestNeighborParams op_params;
70 op_params.align_corners = params->align_corners;
71 op_params.half_pixel_centers = false;
72
73 if (output->type == kTfLiteFloat32) {
74 reference_ops::ResizeNearestNeighbor(
75 op_params, tflite::micro::GetTensorShape(input),
76 tflite::micro::GetTensorData<int32_t>(input),
77 tflite::micro::GetTensorShape(size),
78 tflite::micro::GetTensorData<int32_t>(size),
79 tflite::micro::GetTensorShape(output),
80 tflite::micro::GetTensorData<int32_t>(output));
81 } else if (output->type == kTfLiteUInt8) {
82 reference_ops::ResizeNearestNeighbor(
83 op_params, tflite::micro::GetTensorShape(input),
84 tflite::micro::GetTensorData<uint8_t>(input),
85 tflite::micro::GetTensorShape(size),
86 tflite::micro::GetTensorData<int32_t>(size),
87 tflite::micro::GetTensorShape(output),
88 tflite::micro::GetTensorData<uint8_t>(output));
89 } else if (output->type == kTfLiteInt8) {
90 reference_ops::ResizeNearestNeighbor(
91 op_params, tflite::micro::GetTensorShape(input),
92 tflite::micro::GetTensorData<int8_t>(input),
93 tflite::micro::GetTensorShape(size),
94 tflite::micro::GetTensorData<int32_t>(size),
95 tflite::micro::GetTensorShape(output),
96 tflite::micro::GetTensorData<int8_t>(output));
97 } else {
98 TF_LITE_KERNEL_LOG(context,
99 "Output type is %d, requires float, uint8_t or int8_t.",
100 output->type);
101 return kTfLiteError;
102 }
103
104 return kTfLiteOk;
105 }
106 } // namespace resize_nearest_neighbor
107
Register_RESIZE_NEAREST_NEIGHBOR()108 TfLiteRegistration Register_RESIZE_NEAREST_NEIGHBOR() {
109 return {/*init=*/nullptr,
110 /*free=*/nullptr,
111 /*prepare=*/resize_nearest_neighbor::Prepare,
112 /*invoke=*/resize_nearest_neighbor::Eval,
113 /*profiling_string=*/nullptr,
114 /*builtin_code=*/0,
115 /*custom_name=*/nullptr,
116 /*version=*/0};
117 }
118
119 } // namespace micro
120 } // namespace ops
121 } // namespace tflite
122