-
Notifications
You must be signed in to change notification settings - Fork 640
/
CTfLiteClass.cpp
301 lines (226 loc) · 6.64 KB
/
CTfLiteClass.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
#include "CTfLiteClass.h"
#include "ClassLogFile.h"
#include "Helper.h"
#include "esp_log.h"
#include "../../include/defines.h"
#include <sys/stat.h>
// #define DEBUG_DETAIL_ON
static const char *TAG = "TFLITE";
float CTfLiteClass::GetOutputValue(int nr)
{
TfLiteTensor* output2 = this->interpreter->output(0);
int numeroutput = output2->dims->data[1];
if ((nr+1) > numeroutput)
return -1000;
return output2->data.f[nr];
}
int CTfLiteClass::GetClassFromImageBasis(CImageBasis *rs)
{
if (!LoadInputImageBasis(rs))
return -1000;
Invoke();
return GetOutClassification();
}
int CTfLiteClass::GetOutClassification(int _von, int _bis)
{
TfLiteTensor* output2 = interpreter->output(0);
float zw_max;
float zw;
int zw_class;
if (output2 == NULL)
return -1;
int numeroutput = output2->dims->data[1];
//ESP_LOGD(TAG, "number output neurons: %d", numeroutput);
if (_bis == -1)
_bis = numeroutput -1;
if (_von == -1)
_von = 0;
if (_bis >= numeroutput)
{
ESP_LOGD(TAG, "NUMBER OF OUTPUT NEURONS does not match required classification!");
return -1;
}
zw_max = output2->data.f[_von];
zw_class = _von;
for (int i = _von + 1; i <= _bis; ++i)
{
zw = output2->data.f[i];
if (zw > zw_max)
{
zw_max = zw;
zw_class = i;
}
}
return (zw_class - _von);
}
void CTfLiteClass::GetInputDimension(bool silent = false)
{
TfLiteTensor* input2 = this->interpreter->input(0);
int numdim = input2->dims->size;
if (!silent) ESP_LOGD(TAG, "NumDimension: %d", numdim);
int sizeofdim;
for (int j = 0; j < numdim; ++j)
{
sizeofdim = input2->dims->data[j];
if (!silent) ESP_LOGD(TAG, "SizeOfDimension %d: %d", j, sizeofdim);
if (j == 1) im_height = sizeofdim;
if (j == 2) im_width = sizeofdim;
if (j == 3) im_channel = sizeofdim;
}
}
int CTfLiteClass::ReadInputDimenstion(int _dim)
{
if (_dim == 0)
return im_width;
if (_dim == 1)
return im_height;
if (_dim == 2)
return im_channel;
return -1;
}
int CTfLiteClass::GetAnzOutPut(bool silent)
{
TfLiteTensor* output2 = this->interpreter->output(0);
int numdim = output2->dims->size;
if (!silent) ESP_LOGD(TAG, "NumDimension: %d", numdim);
int sizeofdim;
for (int j = 0; j < numdim; ++j)
{
sizeofdim = output2->dims->data[j];
if (!silent) ESP_LOGD(TAG, "SizeOfDimension %d: %d", j, sizeofdim);
}
float fo;
// Process the inference results.
int numeroutput = output2->dims->data[1];
for (int i = 0; i < numeroutput; ++i)
{
fo = output2->data.f[i];
if (!silent) ESP_LOGD(TAG, "Result %d: %f", i, fo);
}
return numeroutput;
}
void CTfLiteClass::Invoke()
{
if (interpreter != nullptr)
interpreter->Invoke();
}
bool CTfLiteClass::LoadInputImageBasis(CImageBasis *rs)
{
std::string zw = "ClassFlowCNNGeneral::doNeuralNetwork after LoadInputResizeImage: ";
unsigned int w = rs->width;
unsigned int h = rs->height;
unsigned char red, green, blue;
// ESP_LOGD(TAG, "Image: %s size: %d x %d\n", _fn.c_str(), w, h);
input_i = 0;
float* input_data_ptr = (interpreter->input(0))->data.f;
for (int y = 0; y < h; ++y)
for (int x = 0; x < w; ++x)
{
red = rs->GetPixelColor(x, y, 0);
green = rs->GetPixelColor(x, y, 1);
blue = rs->GetPixelColor(x, y, 2);
*(input_data_ptr) = (float) red;
input_data_ptr++;
*(input_data_ptr) = (float) green;
input_data_ptr++;
*(input_data_ptr) = (float) blue;
input_data_ptr++;
}
#ifdef DEBUG_DETAIL_ON
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "After loading in input");
#endif
return true;
}
void CTfLiteClass::MakeAllocate()
{
static tflite::AllOpsResolver resolver;
// ESP_LOGD(TAG, "%s", LogFile.getESPHeapInfo().c_str());
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Make Allocate");
this->interpreter = new tflite::MicroInterpreter(this->model, resolver, this->tensor_arena, this->kTensorArenaSize, this->error_reporter);
// ESP_LOGD(TAG, "%s", LogFile.getESPHeapInfo().c_str());
TfLiteStatus allocate_status = this->interpreter->AllocateTensors();
if (allocate_status != kTfLiteOk) {
TF_LITE_REPORT_ERROR(error_reporter, "AllocateTensors() failed");
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "AllocateTensors() failed");
this->GetInputDimension();
return;
}
// ESP_LOGD(TAG, "Allocate Done");
}
void CTfLiteClass::GetInputTensorSize(){
#ifdef DEBUG_DETAIL_ON
float *zw = this->input;
int test = sizeof(zw);
ESP_LOGD(TAG, "Input Tensor Dimension: %d", test);
#endif
}
long CTfLiteClass::GetFileSize(std::string filename)
{
struct stat stat_buf;
long rc = stat(filename.c_str(), &stat_buf);
return rc == 0 ? stat_buf.st_size : -1;
}
unsigned char* CTfLiteClass::ReadFileToCharArray(std::string _fn)
{
long size;
size = GetFileSize(_fn);
if (size == -1)
{
ESP_LOGD(TAG, "File doesn't exist");
return NULL;
}
unsigned char *result = (unsigned char*) malloc(size);
int anz = 1;
while (!result && (anz < 6)) // Try a maximum of 5x (= 5s)
{
#ifdef DEBUG_DETAIL_ON
ESP_LOGD(TAG, "Speicher ist voll - Versuche es erneut: %d", anz);
#endif
result = (unsigned char*) malloc(size);
anz++;
}
if(result != NULL) {
FILE* f = OpenFileAndWait(_fn.c_str(), "rb"); // previously only "r
fread(result, 1, size, f);
fclose(f);
}else {
ESP_LOGD(TAG, "No free memory available");
}
return result;
}
bool CTfLiteClass::LoadModel(std::string _fn){
#ifdef SUPRESS_TFLITE_ERRORS
this->error_reporter = new tflite::OwnMicroErrorReporter;
#else
this->error_reporter = new tflite::MicroErrorReporter;
#endif
modelload = ReadFileToCharArray(_fn.c_str());
if (modelload == NULL)
return false;
model = tflite::GetModel(modelload);
// free(rd);
TFLITE_MINIMAL_CHECK(model != nullptr);
return true;
}
CTfLiteClass::CTfLiteClass()
{
this->model = nullptr;
this->interpreter = nullptr;
this->input = nullptr;
this->output = nullptr;
this->kTensorArenaSize = 800 * 1024; /// according to testfile: 108000 - so far 600;; 2021-09-11: 200 * 1024
this->tensor_arena = new uint8_t[kTensorArenaSize];
}
CTfLiteClass::~CTfLiteClass()
{
delete this->tensor_arena;
delete this->interpreter;
delete this->error_reporter;
if (modelload)
free(modelload);
}
namespace tflite {
int OwnMicroErrorReporter::Report(const char* format, va_list args) {
return 0;
}
}