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