21
21
#include < stdint.h>
22
22
23
23
#include " edge-impulse-sdk/classifier/ei_classifier_types.h"
24
+ #include " edge-impulse-sdk/dsp/ei_dsp_handle.h"
24
25
#include " edge-impulse-sdk/dsp/numpy.hpp"
25
26
#if EI_CLASSIFIER_USE_FULL_TFLITE || (EI_CLASSIFIER_INFERENCING_ENGINE == EI_CLASSIFIER_AKIDA) || (EI_CLASSIFIER_INFERENCING_ENGINE == EI_CLASSIFIER_MEMRYX)
26
27
#include " tensorflow-lite/tensorflow/lite/c/common.h"
61
62
#define EI_CLASSIFIER_LAST_LAYER_YOLOX 4
62
63
#define EI_CLASSIFIER_LAST_LAYER_YOLOV5_V5_DRPAI 5
63
64
#define EI_CLASSIFIER_LAST_LAYER_YOLOV7 6
65
+ #define EI_CLASSIFIER_LAST_LAYER_TAO_RETINANET 7
66
+ #define EI_CLASSIFIER_LAST_LAYER_TAO_SSD 8
67
+ #define EI_CLASSIFIER_LAST_LAYER_TAO_YOLOV3 9
68
+ #define EI_CLASSIFIER_LAST_LAYER_TAO_YOLOV4 10
64
69
65
70
#define EI_CLASSIFIER_IMAGE_SCALING_NONE 0
66
71
#define EI_CLASSIFIER_IMAGE_SCALING_0_255 1
67
72
#define EI_CLASSIFIER_IMAGE_SCALING_TORCH 2
68
73
#define EI_CLASSIFIER_IMAGE_SCALING_MIN1_1 3
74
+ #define EI_CLASSIFIER_IMAGE_SCALING_MIN128_127 4
75
+
76
+ // maps back to ClassificationMode in keras-types.ts
77
+ #define EI_CLASSIFIER_CLASSIFICATION_MODE_CLASSIFICATION 1
78
+ #define EI_CLASSIFIER_CLASSIFICATION_MODE_REGRESSION 2
79
+ #define EI_CLASSIFIER_CLASSIFICATION_MODE_OBJECT_DETECTION 3
80
+ #define EI_CLASSIFIER_CLASSIFICATION_MODE_ANOMALY_GMM 4
81
+ #define EI_CLASSIFIER_CLASSIFICATION_MODE_VISUAL_ANOMALY 5
82
+ #define EI_CLASSIFIER_CLASSIFICATION_MODE_ANOMALY_KMEANS 6
83
+ #define EI_CLASSIFIER_CLASSIFICATION_MODE_DSP 7
69
84
70
85
struct ei_impulse ;
71
86
87
+ typedef struct {
88
+ ei::matrix_t * matrix;
89
+ uint32_t blockId;
90
+ } ei_feature_t ;
91
+
72
92
typedef struct {
73
93
uint16_t implementation_version;
74
94
bool is_configured;
@@ -79,11 +99,15 @@ typedef struct {
79
99
} ei_model_performance_calibration_t ;
80
100
81
101
typedef struct {
102
+ uint32_t blockId;
82
103
size_t n_output_features;
83
104
int (*extract_fn)(ei::signal_t *signal, ei::matrix_t *output_matrix, void *config, const float frequency);
84
105
void *config;
85
106
uint8_t *axes;
86
107
size_t axes_size;
108
+ int version; // future proof, can easily add to this struct now
109
+ DspHandle* (*factory)(void * config); // nullptr means no state
110
+ // v1 ends here
87
111
} ei_model_dsp_t ;
88
112
89
113
typedef struct {
@@ -92,9 +116,14 @@ typedef struct {
92
116
} ei_classifier_anom_cluster_t ;
93
117
94
118
typedef struct {
95
- EI_IMPULSE_ERROR (* infer_fn )(const ei_impulse * impulse , ei ::matrix_t * fmatrix , ei_impulse_result_t * result , void * config , bool debug );
119
+ uint32_t blockId;
120
+ bool keep_output;
121
+ EI_IMPULSE_ERROR (*infer_fn)(const ei_impulse *impulse, ei_feature_t *fmatrix, uint32_t learn_block_index, uint32_t * input_block_ids, uint32_t input_block_ids_size, ei_impulse_result_t *result, void *config, bool debug);
96
122
void *config;
97
123
int image_scaling;
124
+ const uint32_t * input_block_ids;
125
+ const uint32_t input_block_ids_size;
126
+ uint32_t output_features_count;
98
127
} ei_learning_block_t ;
99
128
100
129
typedef struct {
@@ -127,6 +156,7 @@ typedef struct {
127
156
128
157
typedef struct {
129
158
uint16_t implementation_version;
159
+ uint8_t classification_mode;
130
160
uint32_t block_id;
131
161
/* object detection */
132
162
bool object_detection;
@@ -143,6 +173,7 @@ typedef struct {
143
173
144
174
typedef struct {
145
175
uint16_t implementation_version;
176
+ uint8_t classification_mode;
146
177
const uint16_t *anom_axis;
147
178
uint16_t anom_axes_size;
148
179
const ei_classifier_anom_cluster_t *anom_clusters;
@@ -153,11 +184,19 @@ typedef struct {
153
184
154
185
typedef struct {
155
186
uint16_t implementation_version;
187
+ uint8_t classification_mode;
156
188
const uint16_t *anom_axis;
157
189
uint16_t anom_axes_size;
190
+ float anomaly_threshold;
191
+ bool visual;
158
192
void * graph_config;
159
193
} ei_learning_block_config_anomaly_gmm_t ;
160
194
195
+ typedef struct {
196
+ float confidence_threshold;
197
+ float iou_threshold;
198
+ } ei_object_detection_nms_config_t ;
199
+
161
200
typedef struct ei_impulse {
162
201
/* project details */
163
202
uint32_t project_id;
@@ -200,12 +239,77 @@ typedef struct ei_impulse {
200
239
uint32_t slices_per_model_window;
201
240
202
241
/* output details */
203
- bool has_anomaly ;
242
+ uint16_t has_anomaly;
204
243
uint16_t label_count;
205
244
const ei_model_performance_calibration_t calibration;
206
245
const char **categories;
246
+ ei_object_detection_nms_config_t object_detection_nms;
207
247
} ei_impulse_t ;
208
248
249
+ class ei_impulse_state_t {
250
+ typedef DspHandle* _dsp_handle_ptr_t ;
251
+ public:
252
+ const ei_impulse_t *impulse; // keep a pointer to the impulse
253
+ _dsp_handle_ptr_t *dsp_handles;
254
+ bool is_temp_handle = false ; // to know if we're using the old (stateless) API
255
+ ei_impulse_state_t (const ei_impulse_t *impulse)
256
+ : impulse(impulse)
257
+ {
258
+ const auto num_dsp_blocks = impulse->dsp_blocks_size ;
259
+ dsp_handles = (_dsp_handle_ptr_t *)ei_malloc (sizeof (_dsp_handle_ptr_t )*num_dsp_blocks);
260
+ for (size_t ix = 0 ; ix < num_dsp_blocks; ix++) {
261
+ dsp_handles[ix] = nullptr ;
262
+ }
263
+ }
264
+
265
+ DspHandle* get_dsp_handle (size_t ix) {
266
+ if (dsp_handles[ix] == nullptr ) {
267
+ dsp_handles[ix] = impulse->dsp_blocks [ix].factory (impulse->dsp_blocks [ix].config );
268
+ }
269
+ return dsp_handles[ix];
270
+ }
271
+
272
+ void reset ()
273
+ {
274
+ for (size_t ix = 0 ; ix < impulse->dsp_blocks_size ; ix++) {
275
+ if (dsp_handles[ix] != nullptr ) {
276
+ delete dsp_handles[ix];
277
+ dsp_handles[ix] = nullptr ;
278
+ }
279
+ }
280
+ }
281
+
282
+ void * operator new (size_t size) {
283
+ return ei_malloc (size);
284
+ }
285
+
286
+ void operator delete (void * ptr) {
287
+ ei_free (ptr);
288
+ }
289
+
290
+ void * operator new [](size_t size) {
291
+ return ei_malloc (size);
292
+ }
293
+
294
+ void operator delete[] (void * ptr) {
295
+ ei_free (ptr);
296
+ }
297
+
298
+ ~ei_impulse_state_t ()
299
+ {
300
+ reset ();
301
+ ei_free (dsp_handles);
302
+ }
303
+ };
304
+
305
+ class ei_impulse_handle_t {
306
+ public:
307
+ ei_impulse_handle_t (const ei_impulse_t *impulse)
308
+ : state(impulse), impulse(impulse) {};
309
+ ei_impulse_state_t state;
310
+ const ei_impulse_t *impulse;
311
+ };
312
+
209
313
typedef struct {
210
314
uint32_t block_id;
211
315
uint16_t implementation_version;
0 commit comments