Home | History | Annotate | Download | only in tools
      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 <cstdarg>
     17 #include <cstdlib>
     18 #include <iostream>
     19 #include <memory>
     20 #include <string>
     21 #include <unordered_set>
     22 #include <vector>
     23 
     24 #include "tensorflow/contrib/lite/kernels/register.h"
     25 #include "tensorflow/contrib/lite/model.h"
     26 #include "tensorflow/contrib/lite/string_util.h"
     27 #include "tensorflow/contrib/lite/tools/mutable_op_resolver.h"
     28 
     29 #ifdef TFLITE_CUSTOM_OPS_HEADER
     30 void RegisterSelectedOps(::tflite::MutableOpResolver* resolver);
     31 #endif
     32 
     33 #define LOG(x) std::cerr
     34 
     35 #define CHECK(x)                  \
     36   if (!(x)) {                     \
     37     LOG(ERROR) << #x << "failed"; \
     38     exit(1);                      \
     39   }
     40 
     41 namespace tensorflow {
     42 namespace benchmark_tflite_model {
     43 
     44 std::unique_ptr<tflite::FlatBufferModel> model;
     45 std::unique_ptr<tflite::Interpreter> interpreter;
     46 
     47 void InitImpl(const std::string& graph, const std::vector<int>& sizes,
     48               const std::string& input_layer_type, int num_threads) {
     49   CHECK(graph.c_str());
     50 
     51   model = tflite::FlatBufferModel::BuildFromFile(graph.c_str());
     52   if (!model) {
     53     LOG(FATAL) << "Failed to mmap model " << graph;
     54   }
     55   LOG(INFO) << "Loaded model " << graph;
     56   model->error_reporter();
     57   LOG(INFO) << "resolved reporter";
     58 
     59 #ifdef TFLITE_CUSTOM_OPS_HEADER
     60   tflite::MutableOpResolver resolver;
     61   RegisterSelectedOps(&resolver);
     62 #else
     63   tflite::ops::builtin::BuiltinOpResolver resolver;
     64 #endif
     65 
     66   tflite::InterpreterBuilder(*model, resolver)(&interpreter);
     67   if (!interpreter) {
     68     LOG(FATAL) << "Failed to construct interpreter";
     69   }
     70 
     71   if (num_threads != -1) {
     72     interpreter->SetNumThreads(num_threads);
     73   }
     74 
     75   int input = interpreter->inputs()[0];
     76 
     77   if (input_layer_type != "string") {
     78     interpreter->ResizeInputTensor(input, sizes);
     79   }
     80 
     81   if (interpreter->AllocateTensors() != kTfLiteOk) {
     82     LOG(FATAL) << "Failed to allocate tensors!";
     83   }
     84 }
     85 
     86 int Main(int argc, char** argv) {
     87   InitImpl("", {}, "", 1);
     88   return 0;
     89 }
     90 
     91 }  // namespace benchmark_tflite_model
     92 }  // namespace tensorflow
     93 
     94 int main(int argc, char** argv) {
     95   return tensorflow::benchmark_tflite_model::Main(argc, argv);
     96 }
     97