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/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 <string>
29 #include "tensorflow/lite/nnapi/nnapi_implementation.h"
30 #include "tensorflow/lite/testing/parse_testdata.h"
31 #include "tensorflow/lite/testing/tflite_driver.h"
32
dirname(const std::string & s)33 std::string dirname(const std::string& s) {
34 return s.substr(0, s.find_last_of('/'));
35 }
36
Interpret(const char * examples_filename,bool use_nnapi)37 bool Interpret(const char* examples_filename, bool use_nnapi) {
38 std::ifstream tflite_stream(examples_filename);
39 if (!tflite_stream.is_open()) {
40 fprintf(stderr, "Can't open input file.");
41 return false;
42 }
43
44 printf("Use nnapi is set to: %d\n", use_nnapi);
45 tflite::testing::TfLiteDriver test_driver(
46 use_nnapi ? tflite::testing::TfLiteDriver::DelegateType::kNnapi
47 : tflite::testing::TfLiteDriver::DelegateType::kNone);
48
49 test_driver.SetModelBaseDir(dirname(examples_filename));
50 if (!tflite::testing::ParseAndRunTests(&tflite_stream, &test_driver)) {
51 fprintf(stderr, "Results from tflite don't match.");
52 return false;
53 }
54
55 return true;
56 }
57
main(int argc,char * argv[])58 int main(int argc, char* argv[]) {
59 bool use_nnapi = true;
60 if (argc == 4) {
61 use_nnapi = strcmp(argv[3], "1") == 0 ? true : false;
62 }
63 if (argc < 3) {
64 fprintf(stderr,
65 "Compiled " __DATE__ __TIME__
66 "\n"
67 "Usage!!!: %s <tflite model> <examples to test> "
68 "{ use nn api i.e. 0,1}\n",
69 argv[0]);
70 return 1;
71 }
72
73 std::string base_dir = dirname(argv[1]);
74 DIR* dir = opendir(base_dir.c_str());
75 if (dir == nullptr) {
76 fprintf(stderr, "Can't open dir %s\n", base_dir.c_str());
77 return 1;
78 }
79 while (struct dirent* ent = readdir(dir)) {
80 std::string name = ent->d_name;
81 if (name.rfind(".txt") == name.length() - 4) {
82 printf("%s: ", name.c_str());
83 if (Interpret((base_dir + "/" + name).c_str(), use_nnapi)) {
84 printf(" %s\n", "OK");
85 } else {
86 printf(" %s\n", "FAIL");
87 }
88 }
89 }
90 closedir(dir);
91
92 return 0;
93 }
94