-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtflite_eon.cpp
372 lines (314 loc) · 13.7 KB
/
tflite_eon.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
/*
* Copyright (c) 2022 EdgeImpulse Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS
* IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef _EI_CLASSIFIER_INFERENCING_ENGINE_TFLITE_EON_H_
#define _EI_CLASSIFIER_INFERENCING_ENGINE_TFLITE_EON_H_
#if (EI_CLASSIFIER_INFERENCING_ENGINE == EI_CLASSIFIER_TFLITE) && (EI_CLASSIFIER_COMPILED == 1)
#include "model-parameters/model_metadata.h"
#if EI_CLASSIFIER_HAS_MODEL_VARIABLES == 1
#include "model-parameters/model_variables.h"
#endif
#include "edge-impulse-sdk/tensorflow/lite/c/common.h"
#include "edge-impulse-sdk/tensorflow/lite/kernels/internal/tensor_ctypes.h"
#include "tflite-model/trained_model_compiled.h"
#include "edge-impulse-sdk/classifier/ei_aligned_malloc.h"
#include "edge-impulse-sdk/classifier/ei_fill_result_struct.h"
#include "edge-impulse-sdk/classifier/ei_model_types.h"
#if defined(EI_CLASSIFIER_ENABLE_DETECTION_POSTPROCESS_OP)
namespace tflite {
namespace ops {
namespace micro {
extern TfLiteRegistration Register_TFLite_Detection_PostProcess(void);
} // namespace micro
} // namespace ops
extern float post_process_boxes[10 * 4 * sizeof(float)];
extern float post_process_classes[10];
extern float post_process_scores[10];
} // namespace tflite
static TfLiteRegistration post_process_op = tflite::ops::micro::Register_TFLite_Detection_PostProcess();
#endif // defined(EI_CLASSIFIER_ENABLE_DETECTION_POSTPROCESS_OP)
/**
* Setup the TFLite runtime
*
* @param ctx_start_us Pointer to the start time
* @param input Pointer to input tensor
* @param output Pointer to output tensor
* @param micro_tensor_arena Pointer to the arena that will be allocated
*
* @return EI_IMPULSE_OK if successful
*/
static EI_IMPULSE_ERROR inference_tflite_setup(const ei_impulse_t *impulse, uint64_t *ctx_start_us, TfLiteTensor** input, TfLiteTensor** output,
TfLiteTensor** output_labels,
TfLiteTensor** output_scores,
ei_unique_ptr_t& p_tensor_arena) {
*ctx_start_us = ei_read_timer_us();
TfLiteStatus init_status = trained_model_init(ei_aligned_calloc);
if (init_status != kTfLiteOk) {
ei_printf("Failed to allocate TFLite arena (error code %d)\n", init_status);
return EI_IMPULSE_TFLITE_ARENA_ALLOC_FAILED;
}
static bool tflite_first_run = true;
*input = impulse->model_input(0);
*output = impulse->model_output(0);
if (impulse->object_detection_last_layer == EI_CLASSIFIER_LAST_LAYER_SSD) {
*output_scores = impulse->model_output(impulse->tflite_output_score_tensor);
*output_labels = impulse->model_output(impulse->tflite_output_labels_tensor);
}
// Assert that our quantization parameters match the model
if (tflite_first_run) {
assert((*input)->type == impulse->tflite_input_datatype);
assert((*output)->type == impulse->tflite_output_datatype);
if (impulse->object_detection_last_layer == EI_CLASSIFIER_LAST_LAYER_SSD) {
assert((*output_scores)->type == impulse->tflite_output_datatype);
assert((*output_labels)->type == impulse->tflite_output_datatype);
}
if (impulse->tflite_input_quantized) {
assert((*input)->params.scale == impulse->tflite_input_scale);
assert((*input)->params.zero_point == impulse->tflite_input_zeropoint);
}
if (impulse->tflite_output_quantized) {
assert((*output)->params.scale == impulse->tflite_output_scale);
assert((*output)->params.zero_point == impulse->tflite_output_zeropoint);
}
tflite_first_run = false;
}
return EI_IMPULSE_OK;
}
/**
* Run TFLite model
*
* @param ctx_start_us Start time of the setup function (see above)
* @param output Output tensor
* @param interpreter TFLite interpreter (non-compiled models)
* @param tensor_arena Allocated arena (will be freed)
* @param result Struct for results
* @param debug Whether to print debug info
*
* @return EI_IMPULSE_OK if successful
*/
static EI_IMPULSE_ERROR inference_tflite_run(const ei_impulse_t *impulse,
uint64_t ctx_start_us,
TfLiteTensor* output,
TfLiteTensor* labels_tensor,
TfLiteTensor* scores_tensor,
uint8_t* tensor_arena,
ei_impulse_result_t *result,
bool debug) {
if(trained_model_invoke() != kTfLiteOk) {
return EI_IMPULSE_TFLITE_ERROR;
}
uint64_t ctx_end_us = ei_read_timer_us();
result->timing.classification_us = ctx_end_us - ctx_start_us;
result->timing.classification = (int)(result->timing.classification_us / 1000);
// Read the predicted y value from the model's output tensor
if (debug) {
ei_printf("Predictions (time: %d ms.):\n", result->timing.classification);
}
EI_IMPULSE_ERROR fill_res = EI_IMPULSE_OK;
if (impulse->object_detection) {
switch (impulse->object_detection_last_layer) {
case EI_CLASSIFIER_LAST_LAYER_FOMO: {
bool int8_output = output->type == TfLiteType::kTfLiteInt8;
if (int8_output) {
fill_res = fill_result_struct_i8_fomo(impulse, result, output->data.int8, output->params.zero_point, output->params.scale,
(int)output->dims->data[1], (int)output->dims->data[2]);
}
else {
fill_res = fill_result_struct_f32_fomo(impulse, result, output->data.f, (int)output->dims->data[1], (int)output->dims->data[2]);
}
break;
}
case EI_CLASSIFIER_LAST_LAYER_SSD: {
#if EI_CLASSIFIER_ENABLE_DETECTION_POSTPROCESS_OP
fill_res = fill_result_struct_f32_object_detection(impulse, result, tflite::post_process_boxes, tflite::post_process_scores, tflite::post_process_classes, debug);
#else
ei_printf("ERR: Cannot run SSD model, EI_CLASSIFIER_ENABLE_DETECTION_POSTPROCESS_OP is disabled\n");
return EI_IMPULSE_UNSUPPORTED_INFERENCING_ENGINE;
#endif
break;
}
case EI_CLASSIFIER_LAST_LAYER_YOLOV5:
case EI_CLASSIFIER_LAST_LAYER_YOLOV5_V5_DRPAI: {
ei_printf("ERR: YOLOv5 models are not supported using EON Compiler, use full TFLite (%d)\n",
impulse->object_detection_last_layer);
return EI_IMPULSE_UNSUPPORTED_INFERENCING_ENGINE;
}
case EI_CLASSIFIER_LAST_LAYER_YOLOX: {
ei_printf("ERR: YOLOX models are not supported using EON Compiler, use full TFLite (%d)\n",
impulse->object_detection_last_layer);
return EI_IMPULSE_UNSUPPORTED_INFERENCING_ENGINE;
}
default: {
ei_printf("ERR: Unsupported object detection last layer (%d)\n",
impulse->object_detection_last_layer);
return EI_IMPULSE_UNSUPPORTED_INFERENCING_ENGINE;
}
}
}
else {
bool int8_output = output->type == TfLiteType::kTfLiteInt8;
if (int8_output) {
fill_res = fill_result_struct_i8(impulse, result, output->data.int8, output->params.zero_point, output->params.scale, debug);
}
else {
fill_res = fill_result_struct_f32(impulse, result, output->data.f, debug);
}
}
trained_model_reset(ei_aligned_free);
if (fill_res != EI_IMPULSE_OK) {
return fill_res;
}
if (ei_run_impulse_check_canceled() == EI_IMPULSE_CANCELED) {
return EI_IMPULSE_CANCELED;
}
return EI_IMPULSE_OK;
}
/**
* @brief Do neural network inferencing over the processed feature matrix
*
* @param fmatrix Processed matrix
* @param result Output classifier results
* @param[in] debug Debug output enable
*
* @return The ei impulse error.
*/
EI_IMPULSE_ERROR run_nn_inference(
const ei_impulse_t *impulse,
ei::matrix_t *fmatrix,
ei_impulse_result_t *result,
bool debug = false)
{
TfLiteTensor* input;
TfLiteTensor* output;
TfLiteTensor* output_scores;
TfLiteTensor* output_labels;
uint64_t ctx_start_us = ei_read_timer_us();
ei_unique_ptr_t p_tensor_arena(nullptr, ei_aligned_free);
EI_IMPULSE_ERROR init_res = inference_tflite_setup(impulse,
&ctx_start_us,
&input, &output,
&output_labels,
&output_scores,
p_tensor_arena);
if (init_res != EI_IMPULSE_OK) {
return init_res;
}
uint8_t* tensor_arena = static_cast<uint8_t*>(p_tensor_arena.get());
switch (input->type) {
case kTfLiteFloat32: {
ei_printf("STATUS: kTfLite Type = Float32\n");
for (size_t ix = 0; ix < fmatrix->rows * fmatrix->cols; ix++) {
input->data.f[ix] = fmatrix->buffer[ix];
}
break;
}
case kTfLiteInt8: {
ei_printf("STATUS: kTfLite Type = Int8\n");
for (size_t ix = 0; ix < fmatrix->rows * fmatrix->cols; ix++) {
float pixel = (float)fmatrix->buffer[ix];
input->data.int8[ix] = static_cast<int8_t>(pixel);
}
break;
}
case kTfLiteUInt8: {
ei_printf("STATUS: kTfLite Type = UInt8\n");
for (size_t ix = 0; ix < fmatrix->rows * fmatrix->cols; ix++) {
float pixel = (float)fmatrix->buffer[ix];
input->data.uint8[ix] = static_cast<uint8_t>((pixel / impulse->tflite_input_scale) + impulse->tflite_input_zeropoint);
}
}
default: {
ei_printf("ERR: Cannot handle input type (%d)\n", input->type);
return EI_IMPULSE_INPUT_TENSOR_WAS_NULL;
}
}
EI_IMPULSE_ERROR run_res = inference_tflite_run(impulse, ctx_start_us,
output, output_labels, output_scores,
tensor_arena, result, debug);
result->timing.classification_us = ei_read_timer_us() - ctx_start_us;
if (run_res != EI_IMPULSE_OK) {
return run_res;
}
return EI_IMPULSE_OK;
}
#if EI_CLASSIFIER_TFLITE_INPUT_QUANTIZED == 1
/**
* Special function to run the classifier on images, only works on TFLite models (either interpreter or EON or for tensaiflow)
* that allocates a lot less memory by quantizing in place. This only works if 'can_run_classifier_image_quantized'
* returns EI_IMPULSE_OK.
*/
EI_IMPULSE_ERROR run_nn_inference_image_quantized(
const ei_impulse_t *impulse,
signal_t *signal,
ei_impulse_result_t *result,
bool debug = false) {
memset(result, 0, sizeof(ei_impulse_result_t));
uint64_t ctx_start_us;
TfLiteTensor* input;
TfLiteTensor* output;
TfLiteTensor* output_scores;
TfLiteTensor* output_labels;
ei_unique_ptr_t p_tensor_arena(nullptr, ei_aligned_free);
EI_IMPULSE_ERROR init_res = inference_tflite_setup(impulse,
&ctx_start_us, &input, &output,
&output_labels,
&output_scores,
p_tensor_arena);
if (init_res != EI_IMPULSE_OK) {
return init_res;
}
if (input->type != TfLiteType::kTfLiteInt8) {
return EI_IMPULSE_ONLY_SUPPORTED_FOR_IMAGES;
}
uint64_t dsp_start_us = ei_read_timer_us();
// features matrix maps around the input tensor to not allocate any memory
ei::matrix_i8_t features_matrix(1, impulse->nn_input_frame_size, input->data.int8);
// run DSP process and quantize automatically
int ret = extract_image_features_quantized(impulse, signal, &features_matrix, ei_dsp_blocks[0].config, impulse->frequency);
if (ret != EIDSP_OK) {
ei_printf("ERR: Failed to run DSP process (%d)\n", ret);
return EI_IMPULSE_DSP_ERROR;
}
if (ei_run_impulse_check_canceled() == EI_IMPULSE_CANCELED) {
return EI_IMPULSE_CANCELED;
}
result->timing.dsp_us = ei_read_timer_us() - dsp_start_us;
result->timing.dsp = (int)(result->timing.dsp_us / 1000);
if (debug) {
ei_printf("Features (%d ms.): ", result->timing.dsp);
for (size_t ix = 0; ix < features_matrix.cols; ix++) {
ei_printf_float((features_matrix.buffer[ix] - impulse->tflite_input_zeropoint) * impulse->tflite_input_scale);
ei_printf(" ");
}
ei_printf("\n");
}
ctx_start_us = ei_read_timer_us();
EI_IMPULSE_ERROR run_res = inference_tflite_run(impulse,
ctx_start_us,
output,
output_labels,
output_scores,
static_cast<uint8_t*>(p_tensor_arena.get()),
result, debug);
if (run_res != EI_IMPULSE_OK) {
return run_res;
}
result->timing.classification_us = ei_read_timer_us() - ctx_start_us;
return EI_IMPULSE_OK;
}
#endif // EI_CLASSIFIER_TFLITE_INPUT_QUANTIZED == 1
#endif // (EI_CLASSIFIER_INFERENCING_ENGINE == EI_CLASSIFIER_TFLITE) && (EI_CLASSIFIER_COMPILED == 1)
#endif // _EI_CLASSIFIER_INFERENCING_ENGINE_TFLITE_EON_H_