forked from MechatronicsBlog/TensorFlowQtVPlay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tensorflow.cpp
411 lines (336 loc) · 10.4 KB
/
tensorflow.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include "tensorflow.h"
#include <QFile>
#include <QElapsedTimer>
#include <QDebug>
#include "tensorflow/core/graph/graph.h"
#include "tensorflow/core/platform/env.h"
#include "tensorflow/cc/ops/const_op.h"
using namespace tensorflow;
#include "get_top_n.h"
Tensorflow::Tensorflow()
{
initialized = false;
}
double Tensorflow::getThreshold() const
{
return threshold;
}
void Tensorflow::setThreshold(double value)
{
threshold = value;
}
QStringList Tensorflow::getResults()
{
return rCaption;
}
QList<double> Tensorflow::getConfidence()
{
return rConfidence;
}
QList<QRectF> Tensorflow::getBoxes()
{
return rBox;
}
int Tensorflow::getKindNetwork()
{
return kind_network;
}
QString Tensorflow::getModelFilename() const
{
return modelFilename;
}
void Tensorflow::setModelFilename(const QString &value)
{
modelFilename = value;
}
QString Tensorflow::getLabelsFilename() const
{
return labelsFilename;
}
void Tensorflow::setLabelsFilename(const QString &value)
{
labelsFilename = value;
}
int Tensorflow::getImgHeight() const
{
return img_height;
}
int Tensorflow::getImgWidth() const
{
return img_width;
}
double Tensorflow::getInfTime() const
{
return infTime;
}
bool Tensorflow::init(int imgHeight, int imgWidth)
{
if (getModelFilename().trimmed().isEmpty() || getLabelsFilename().trimmed().isEmpty()) return false;
GraphDef graph_def;
// No inference time yet
infTime = -1;
// Initial kind of network
kind_network = knIMAGE_CLASSIFIER;
// Load labels
readLabels();
// Load graph
Status load_graph_status = ReadBinaryProto(Env::Default(), getModelFilename().toStdString().c_str(), &graph_def);
if (!load_graph_status.ok()) load_graph_status = ReadTextProto(Env::Default(), getModelFilename().toStdString().c_str(), &graph_def);
if (!load_graph_status.ok())
{
qDebug() << QString::fromStdString(load_graph_status.error_message());
return false;
}
// Create session
(&session)->reset(NewSession(SessionOptions()));
Status session_create_status = session->Create(graph_def);
if (!session_create_status.ok())
{
qDebug() << QString::fromStdString(session_create_status.error_message());
return false;
}
// Find input nodes
std::vector<const tensorflow::NodeDef*> placeholders;
for (const auto& node : graph_def.node())
{
if (node.op() == "Placeholder")
placeholders.push_back(&node);
else if (node.name() == detection_boxes.toStdString().c_str())
kind_network = knOBJECT_DETECTION;
}
// Check there are inputs
if (placeholders.empty()) return false;
// Get first input
const tensorflow::NodeDef* input = placeholders.at(0);
// Get input name & type
input_name = QString::fromUtf8(input->name().c_str());
input_dtype = input->attr().at("dtype").type();
// Initialize input tensor
initInput(imgHeight,imgWidth);
qDebug() << "Neural network loaded";
initialized = true;
return initialized;
}
void Tensorflow::initInput(int imgHeight, int imgWidth)
{
// Create input tensor
tensorflow::TensorShape input_shape;
input_shape.AddDim(1); // Batch size
input_shape.AddDim((kind_network == knIMAGE_CLASSIFIER ? fixed_heigth : imgHeight)); // Img height
input_shape.AddDim((kind_network == knIMAGE_CLASSIFIER ? fixed_width : imgWidth)); // Img width
input_shape.AddDim(numChannels); // Img channels
input_tensor.reset(new tensorflow::Tensor(input_dtype, input_shape));
}
bool Tensorflow::readLabels()
{
if (!labelsFilename.trimmed().isEmpty())
{
QFile textFile(labelsFilename);
if (textFile.exists())
{
QByteArray line;
labels.clear();
textFile.open(QIODevice::ReadOnly);
line = textFile.readLine().trimmed();
while(!line.isEmpty()) // !textFile.atEnd() &&
{
labels.append(line);
line = textFile.readLine().trimmed();
}
textFile.close();
}
return true;
}
return false;
}
bool Tensorflow::run(QImage img)
{
if (initialized)
{
// Transform image format & copy data
QImage image = img.format() == format ? img : img.convertToFormat(format);
// Store original image properties
img_width = kind_network == knIMAGE_CLASSIFIER ? fixed_width : image.width();
img_height = kind_network == knIMAGE_CLASSIFIER ? fixed_heigth : image.height();
img_channels = numChannels;
// Set inputs
if (!setInputs(image)) return false;
// Perform inference
if (!inference()) return false;
// Clear previous outputs
rCaption.clear();
rConfidence.clear();
rBox.clear();
// Image classifier
if (kind_network == knIMAGE_CLASSIFIER)
{
int index;
double score;
if (!getClassfierOutputs(index,score)) return false;
rConfidence.append(score);
rCaption.append(getLabel(index));
}
// Object detection
else if (kind_network == knOBJECT_DETECTION)
if (!getObjectOutputs(rCaption,rConfidence,rBox)) return false;
return true;
}
return false;
}
QString Tensorflow::getLabel(int index)
{
if(index>=0 && index<labels.count())
{
QString label = labels[index];
// Capitalize label
return label.left(1).toUpper()+label.mid(1);
}
return "";
}
bool Tensorflow::inference()
{
QElapsedTimer timer;
Status run_status;
std::vector<std::string>listOutputs = kind_network == knIMAGE_CLASSIFIER ? listOutputsImgCla : listOutputsObjDet;
timer.start();
run_status = session->Run({},{{input_name.toStdString(), *input_tensor}},listOutputs,{},&outputs,{});
infTime = timer.elapsed();
if (!run_status.ok()) qDebug() << QString::fromStdString(run_status.error_message());
return run_status.ok();
}
template<class T>
bool formatImageTF(T* out, QImage image, int image_channels, bool input_floating)
{
const float input_mean = 127.5f;
const float input_std = 127.5f;
// Number of pixels
const int numberPixels = image.height()*image.width()*image_channels;
// Pointer to image data
const uint8_t *output = image.bits();
// Boolean to [0,1]
const int inputFloat = input_floating ? 1 : 0;
const int inputInt = input_floating ? 0 : 1;
// Transform to [-128,128]
for (int i = 0; i < numberPixels; i++)
{
out[i] = inputFloat*((output[i] - input_mean) / input_std) +
inputInt*(uint8_t)output[i];
}
return true;
}
bool Tensorflow::setInputs(QImage image)
{
// For image classification resize
if (kind_network == knIMAGE_CLASSIFIER)
image = image.scaled(img_width,img_height);
// QImage to data
switch (input_dtype)
{
case tensorflow::DT_FLOAT:
{
float* data = input_tensor->flat<float>().data();
formatImageTF<float>(data,image,img_channels,true);
break;
}
case tensorflow::DT_UINT8:
{
uint8_t* data = input_tensor->flat<uint8_t>().data();
formatImageTF<uint8_t>(data,image,img_channels,false);
break;
}
break;
default: return false;
}
return true;
}
template<typename T>
const T* TensorData(const tensorflow::Tensor& tensor, int batch_index);
template<>
const float* TensorData(const tensorflow::Tensor& tensor, int batch_index)
{
int nelems = tensor.dim_size(1) * tensor.dim_size(2) * tensor.dim_size(3);
switch (tensor.dtype())
{
case tensorflow::DT_FLOAT:
return tensor.flat<float>().data() + nelems * batch_index;
default:
LOG(FATAL) << "Should not reach here!";
}
return nullptr;
}
template<>
const uint8_t* TensorData(const tensorflow::Tensor& tensor, int batch_index)
{
int nelems = tensor.dim_size(1) * tensor.dim_size(2) * tensor.dim_size(3);
switch (tensor.dtype())
{
case tensorflow::DT_UINT8:
return tensor.flat<uint8_t>().data() + nelems * batch_index;
default:
LOG(FATAL) << "Should not reach here!";
}
return nullptr;
}
// TODO: naive algorithm
bool Tensorflow::getClassfierOutputs(int &index, double &score)
{
const int imagePos = 0;
index = -1;
score = 0;
if (outputs.size()>0)
{
for (auto &t : outputs)
{
tensorflow::TTypes<float, 2>::Tensor scores = t.flat_inner_dims<float>();
auto dims = scores.dimensions();
int classesCount = dims[1];
for(int i = 1; i<classesCount; i++)
{
float val = scores(imagePos,i);
if(val > score)
{
score = val;
index = i;
}
}
}
return true;
}
return false;
}
bool Tensorflow::getObjectOutputs(QStringList &captions, QList<double> &confidences, QList<QRectF> &locations)
{
if (outputs.size() >= 4)
{
const int num_detections = *TensorData<float>(outputs[0], 0);
const float* detection_classes = TensorData<float>(outputs[1], 0);
const float* detection_scores = TensorData<float>(outputs[2], 0);
const float* detection_boxes = TensorData<float>(outputs[3], 0);
for (int i=0; i<num_detections; i++)
{
// Get class
const int cls = detection_classes[i];
// Ignore first one
if (cls == 0) continue;
// Get score
float score = detection_scores[i];
// Check minimum score
if (score < getThreshold()) break;
// Get class label
const QString label = getLabel(cls);
// Get coordinates
const float top = detection_boxes[4 * i] * img_height;
const float left = detection_boxes[4 * i + 1] * img_width;
const float bottom = detection_boxes[4 * i + 2] * img_height;
const float right = detection_boxes[4 * i + 3] * img_width;
// Save coordinates
QRectF box(left,top,right-left,bottom-top);
// Save remaining data
captions.append(label);
confidences.append(score);
locations.append(box);
}
return true;
}
return false;
}