1 /* Copyright 2018 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 #include <cstdio>
16 #include "tensorflow/lite/interpreter.h"
17 #include "tensorflow/lite/kernels/register.h"
18 #include "tensorflow/lite/model.h"
19 #include "tensorflow/lite/optional_debug_tools.h"
20
21 // This is an example that is minimal to read a model
22 // from disk and perform inference. There is no data being loaded
23 // that is up to you to add as a user.
24 //
25 // NOTE: Do not add any dependencies to this that cannot be built with
26 // the minimal makefile. This example must remain trivial to build with
27 // the minimal build tool.
28 //
29 // Usage: minimal <tflite model>
30
31 using namespace tflite;
32
33 #define TFLITE_MINIMAL_CHECK(x) \
34 if (!(x)) { \
35 fprintf(stderr, "Error at %s:%d\n", __FILE__, __LINE__); \
36 exit(1); \
37 }
38
main(int argc,char * argv[])39 int main(int argc, char* argv[]) {
40 if(argc != 2) {
41 fprintf(stderr, "minimal <tflite model>\n");
42 return 1;
43 }
44 const char* filename = argv[1];
45
46 // Load model
47 std::unique_ptr<tflite::FlatBufferModel> model =
48 tflite::FlatBufferModel::BuildFromFile(filename);
49 TFLITE_MINIMAL_CHECK(model != nullptr);
50
51 // Build the interpreter
52 tflite::ops::builtin::BuiltinOpResolver resolver;
53 InterpreterBuilder builder(*model, resolver);
54 std::unique_ptr<Interpreter> interpreter;
55 builder(&interpreter);
56 TFLITE_MINIMAL_CHECK(interpreter != nullptr);
57
58 // Allocate tensor buffers.
59 TFLITE_MINIMAL_CHECK(interpreter->AllocateTensors() == kTfLiteOk);
60 printf("=== Pre-invoke Interpreter State ===\n");
61 tflite::PrintInterpreterState(interpreter.get());
62
63 // Fill input buffers
64 // TODO(user): Insert code to fill input tensors
65
66 // Run inference
67 TFLITE_MINIMAL_CHECK(interpreter->Invoke() == kTfLiteOk);
68 printf("\n\n=== Post-invoke Interpreter State ===\n");
69 tflite::PrintInterpreterState(interpreter.get());
70
71 // Read output buffers
72 // TODO(user): Insert getting data out code.
73
74 return 0;
75 }
76