-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTensorFlow_Lite_Class.cpp
175 lines (145 loc) · 6.06 KB
/
TensorFlow_Lite_Class.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <fstream>
#include <opencv2/core/ocl.hpp>
#include "tensorflow/lite/interpreter.h"
#include "tensorflow/lite/kernels/register.h"
#include "tensorflow/lite/string_util.h"
#include "tensorflow/lite/examples/label_image/get_top_n.h"
#include "tensorflow/lite/model.h"
#include <cmath>
#include <fstream>
/// Besides your regular TensorFlow Lite and flatbuffers library,
/// you must also compiled TensorFlow Lite from scratch by bazel
/// with the option GPU delegate set, before you can use the GPU delegates
/// see https://qengineering.eu/install-tensorflow-2-lite-on-jetson-nano.html
/// note also, it will not bring any speed improvement.
//#define GPU_DELEGATE //remove comment to deploy GPU delegates
#ifdef GPU_DELEGATE
#include "tensorflow/lite/delegates/gpu/delegate.h"
#endif // GPU_DELEGATE
using namespace cv;
using namespace std;
int model_width;
int model_height;
int model_channels;
std::vector<std::string> Labels;
std::unique_ptr<tflite::Interpreter> interpreter;
static bool getFileContent(std::string fileName)
{
// Open the File
std::ifstream in(fileName.c_str());
// Check if object is valid
if(!in.is_open()) return false;
std::string str;
// Read the next line from File untill it reaches the end.
while (std::getline(in, str))
{
// Line contains string of length > 0 then save it in vector
if(str.size()>0) Labels.push_back(str);
}
// Close The File
in.close();
return true;
}
int main(int argc,char ** argv)
{
int f;
int In;
Mat frame;
Mat image;
chrono::steady_clock::time_point Tbegin, Tend;
// Load model
// std::unique_ptr<tflite::FlatBufferModel> model = tflite::FlatBufferModel::BuildFromFile("inception_v4_299_quant.tflite");
std::unique_ptr<tflite::FlatBufferModel> model = tflite::FlatBufferModel::BuildFromFile("mobilenet_v1_1.0_224_quant.tflite");
// Build the interpreter
tflite::ops::builtin::BuiltinOpResolver resolver;
tflite::InterpreterBuilder(*model.get(), resolver)(&interpreter);
#ifdef GPU_DELEGATE
TfLiteDelegate *MyDelegate = NULL;
const TfLiteGpuDelegateOptionsV2 options = {
.is_precision_loss_allowed = 1, //FP16,
.inference_preference = TFLITE_GPU_INFERENCE_PREFERENCE_FAST_SINGLE_ANSWER,
.inference_priority1 = TFLITE_GPU_INFERENCE_PRIORITY_MIN_LATENCY,
.inference_priority2 = TFLITE_GPU_INFERENCE_PRIORITY_AUTO,
.inference_priority3 = TFLITE_GPU_INFERENCE_PRIORITY_AUTO,
};
MyDelegate = TfLiteGpuDelegateV2Create(&options);
if(interpreter->ModifyGraphWithDelegate(MyDelegate) != kTfLiteOk) {
cerr << "ERROR: Unable to use delegate" << endl;
return 0;
}
#endif // GPU_DELEGATE
interpreter->AllocateTensors();
interpreter->SetAllowFp16PrecisionForFp32(true);
interpreter->SetNumThreads(4); //quad core
// Get input dimension from the input tensor metadata
// Assuming one input only
In = interpreter->inputs()[0];
model_height = interpreter->tensor(In)->dims->data[1];
model_width = interpreter->tensor(In)->dims->data[2];
model_channels = interpreter->tensor(In)->dims->data[3];
cout << "height : "<< model_height << endl;
cout << "width : "<< model_width << endl;
cout << "channels : "<< model_channels << endl;
// Get the names
bool result = getFileContent("labels.txt");
if(!result)
{
cout << "loading labels failed";
exit(-1);
}
while(1){
frame=imread("tabby.jpeg");//schoolbus.jpg"); //need to refresh frame before dnn class detection
// frame=imread("schoolbus.jpg"); //need to refresh frame before dnn class detection
if (frame.empty()) {
cerr << "Can not load picture!" << endl;
exit(-1);
}
// copy image to input as input tensor
cv::resize(frame, image, Size(model_width,model_height),INTER_NEAREST);
memcpy(interpreter->typed_input_tensor<uchar>(0), image.data, image.total() * image.elemSize());
// cout << "tensors size: " << interpreter->tensors_size() << "\n";
// cout << "nodes size: " << interpreter->nodes_size() << "\n";
// cout << "inputs: " << interpreter->inputs().size() << "\n";
// cout << "outputs: " << interpreter->outputs().size() << "\n";
Tbegin = chrono::steady_clock::now();
interpreter->Invoke(); // run your model
Tend = chrono::steady_clock::now();
const float threshold = 0.001f;
std::vector<std::pair<float, int>> top_results;
int output = interpreter->outputs()[0];
TfLiteIntArray* output_dims = interpreter->tensor(output)->dims;
// assume output dims to be something like (1, 1, ... ,size)
auto output_size = output_dims->data[output_dims->size - 1];
cout << "output_size: " << output_size <<"\n";
switch (interpreter->tensor(output)->type) {
case kTfLiteFloat32:
tflite::label_image::get_top_n<float>(interpreter->typed_output_tensor<float>(0), output_size,
5, threshold, &top_results, kTfLiteFloat32);
break;
case kTfLiteUInt8:
tflite::label_image::get_top_n<uint8_t>(interpreter->typed_output_tensor<uint8_t>(0), output_size,
5, threshold, &top_results, kTfLiteUInt8);
break;
default:
cerr << "cannot handle output type " << interpreter->tensor(output)->type << endl;
exit(-1);
}
for (const auto& result : top_results) {
const float confidence = result.first;
const int index = result.second;
cout << confidence << " : " << Labels[index] << "\n";
}
//calculate time
f = chrono::duration_cast <chrono::milliseconds> (Tend - Tbegin).count();
cout << "Process time: " << f << " mSec" << endl;
}
#ifdef GPU_DELEGATE
interpreter.reset();
TfLiteGpuDelegateV2Delete(MyDelegate);
#endif // GPU_DELEGATE
return 0;
}