-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.cpp
61 lines (48 loc) · 1.5 KB
/
test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>
#include <string>
#include <vector>
#include "tensorflow/contrib/lite/kernels/register.h"
#include "tensorflow/contrib/lite/model.h"
#include "tensorflow/contrib/lite/string_util.h"
#include "tensorflow/contrib/lite/tools/mutable_op_resolver.h"
int main(){
const char graph_path[14] = "xorGate.lite\0";
const int num_threads = 1;
std::string input_layer_type = "float";
std::vector<int> sizes = {2};
float x,y;
std::unique_ptr<tflite::FlatBufferModel> model(
tflite::FlatBufferModel::BuildFromFile(graph_path));
if(!model){
printf("Failed to mmap model\n");
exit(0);
}
tflite::ops::builtin::BuiltinOpResolver resolver;
std::unique_ptr<tflite::Interpreter> interpreter;
tflite::InterpreterBuilder(*model, resolver)(&interpreter);
if(!interpreter){
printf("Failed to construct interpreter\n");
exit(0);
}
if(num_threads != 1){
interpreter->SetNumThreads(num_threads);
}
interpreter->ResizeInputTensor(0, sizes);
float* input = interpreter->typed_input_tensor<float>(0);
if(interpreter->AllocateTensors() != kTfLiteOk){
printf("Failed to allocate tensors\n");
exit(0);
}
//read two numbers
std::printf("Type two float numbers : ");
std::scanf("%f %f", &x, &y);
interpreter->typed_input_tensor<float>(0)[0] = x;
interpreter->typed_input_tensor<float>(0)[1] = y;
if(interpreter->Invoke() != kTfLiteOk){
std::printf("Failed to invoke!\n");
exit(0);
}
float* output = interpreter->typed_output_tensor<float>(0);
printf("output = %f\n", output[0]);
return 0;
}