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 #ifndef TENSORFLOW_LITE_EXAMPLES_LABEL_IMAGE_BITMAP_HELPERS_IMPL_H_
17 #define TENSORFLOW_LITE_EXAMPLES_LABEL_IMAGE_BITMAP_HELPERS_IMPL_H_
18
19 #include "tensorflow/lite/examples/label_image/label_image.h"
20
21 #include "tensorflow/lite/builtin_op_data.h"
22 #include "tensorflow/lite/interpreter.h"
23 #include "tensorflow/lite/kernels/register.h"
24 #include "tensorflow/lite/string_util.h"
25 #include "tensorflow/lite/version.h"
26
27 namespace tflite {
28 namespace label_image {
29
30 template <class T>
resize(T * out,uint8_t * in,int image_height,int image_width,int image_channels,int wanted_height,int wanted_width,int wanted_channels,Settings * s)31 void resize(T* out, uint8_t* in, int image_height, int image_width,
32 int image_channels, int wanted_height, int wanted_width,
33 int wanted_channels, Settings* s) {
34 int number_of_pixels = image_height * image_width * image_channels;
35 std::unique_ptr<Interpreter> interpreter(new Interpreter);
36
37 int base_index = 0;
38
39 // two inputs: input and new_sizes
40 interpreter->AddTensors(2, &base_index);
41 // one output
42 interpreter->AddTensors(1, &base_index);
43 // set input and output tensors
44 interpreter->SetInputs({0, 1});
45 interpreter->SetOutputs({2});
46
47 // set parameters of tensors
48 TfLiteQuantizationParams quant;
49 interpreter->SetTensorParametersReadWrite(
50 0, kTfLiteFloat32, "input",
51 {1, image_height, image_width, image_channels}, quant);
52 interpreter->SetTensorParametersReadWrite(1, kTfLiteInt32, "new_size", {2},
53 quant);
54 interpreter->SetTensorParametersReadWrite(
55 2, kTfLiteFloat32, "output",
56 {1, wanted_height, wanted_width, wanted_channels}, quant);
57
58 ops::builtin::BuiltinOpResolver resolver;
59 const TfLiteRegistration* resize_op =
60 resolver.FindOp(BuiltinOperator_RESIZE_BILINEAR, 1);
61 auto* params = reinterpret_cast<TfLiteResizeBilinearParams*>(
62 malloc(sizeof(TfLiteResizeBilinearParams)));
63 params->align_corners = false;
64 interpreter->AddNodeWithParameters({0, 1}, {2}, nullptr, 0, params, resize_op,
65 nullptr);
66
67 interpreter->AllocateTensors();
68
69 // fill input image
70 // in[] are integers, cannot do memcpy() directly
71 auto input = interpreter->typed_tensor<float>(0);
72 for (int i = 0; i < number_of_pixels; i++) {
73 input[i] = in[i];
74 }
75
76 // fill new_sizes
77 interpreter->typed_tensor<int>(1)[0] = wanted_height;
78 interpreter->typed_tensor<int>(1)[1] = wanted_width;
79
80 interpreter->Invoke();
81
82 auto output = interpreter->typed_tensor<float>(2);
83 auto output_number_of_pixels = wanted_height * wanted_width * wanted_channels;
84
85 for (int i = 0; i < output_number_of_pixels; i++) {
86 if (s->input_floating)
87 out[i] = (output[i] - s->input_mean) / s->input_std;
88 else
89 out[i] = (uint8_t)output[i];
90 }
91 }
92
93 } // namespace label_image
94 } // namespace tflite
95
96 #endif // TENSORFLOW_LITE_EXAMPLES_LABEL_IMAGE_BITMAP_HELPERS_IMPL_H_
97