Home | History | Annotate | Download | only in testing
      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 // NOTE: this is an example driver that converts a tflite model to TensorFlow.
     16 // This is an example that will be integrated more tightly into tflite in
     17 // the future.
     18 //
     19 // Usage: bazel run -c opt \
     20 // tensorflow/contrib/lite/nnapi:nnapi_example -- <filename>
     21 //
     22 #include <dirent.h>
     23 #include <cstdarg>
     24 #include <cstdio>
     25 #include <fstream>
     26 #include <iostream>
     27 #include <sstream>
     28 #include "tensorflow/contrib/lite/nnapi/NeuralNetworksShim.h"
     29 #include "tensorflow/contrib/lite/testing/parse_testdata.h"
     30 #include "tensorflow/contrib/lite/testing/tflite_driver.h"
     31 
     32 string dirname(const string& s) { return s.substr(0, s.find_last_of("/")); }
     33 
     34 bool Interpret(const char* examples_filename, bool use_nnapi) {
     35   std::ifstream tflite_stream(examples_filename);
     36   if (!tflite_stream.is_open()) {
     37     fprintf(stderr, "Can't open input file.");
     38     return false;
     39   }
     40 
     41   printf("Use nnapi is set to: %d\n", use_nnapi);
     42   tflite::testing::TfLiteDriver test_driver(use_nnapi);
     43 
     44   test_driver.SetModelBaseDir(dirname(examples_filename));
     45   if (!tflite::testing::ParseAndRunTests(&tflite_stream, &test_driver)) {
     46     fprintf(stderr, "Results from tflite don't match.");
     47     return false;
     48   }
     49 
     50   return true;
     51 }
     52 
     53 int main(int argc, char* argv[]) {
     54   bool use_nnapi = true;
     55   if (argc == 4) {
     56     use_nnapi = strcmp(argv[3], "1") == 0 ? true : false;
     57   }
     58   if (argc < 3) {
     59     fprintf(stderr,
     60             "Compiled " __DATE__ __TIME__
     61             "\n"
     62             "Usage!!!: %s <tflite model> <examples to test> "
     63             "{ use nn api i.e. 0,1}\n",
     64             argv[0]);
     65     return 1;
     66   }
     67 
     68   string base_dir = dirname(argv[1]);
     69   DIR* dir = opendir(base_dir.c_str());
     70   if (dir == nullptr) {
     71     fprintf(stderr, "Can't open dir %s\n", base_dir.c_str());
     72     return 1;
     73   }
     74   while (struct dirent* ent = readdir(dir)) {
     75     string name = ent->d_name;
     76     if (name.rfind(".txt") == name.length() - 4) {
     77       printf("%s: ", name.c_str());
     78       if (Interpret((base_dir + "/" + name).c_str(), use_nnapi)) {
     79         printf(" %s\n", "OK");
     80       } else {
     81         printf(" %s\n", "FAIL");
     82       }
     83     }
     84   }
     85   closedir(dir);
     86 
     87   return 0;
     88 }
     89