Skip to content

Commit 8878e5a

Browse files
committed
added Neural Network as alternative for TensorFlow Lite for Microcontrollers
1 parent b054727 commit 8878e5a

File tree

6 files changed

+80
-39
lines changed

6 files changed

+80
-39
lines changed

src/server/codegen/knowledgepack_codegen_mixin.py

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -206,20 +206,19 @@ def create_tensorflow_build_flags(self, models):
206206
for model in models:
207207
if self.is_tensorflow(model["classifier_config"]["classifier"]):
208208
if self.nn_inference_engine == "nnom":
209-
#TODO: NNoM
209+
# TODO: NNoM
210210
pass
211211

212212
if self.nn_inference_engine == "tf_micro":
213213
return tf_micro_cflags
214214

215-
216215
return ""
217216

218217
def create_kb_model_tf_micro_binary(self, models):
219218
for model in models:
220219
if self.is_tensorflow(model["classifier_config"]["classifier"]):
221220
if self.nn_inference_engine == "nnom":
222-
#TODO: NNoM
221+
# TODO: NNoM
223222
pass
224223

225224
if self.nn_inference_engine == "tf_micro":
@@ -233,6 +232,9 @@ def create_sml_classification_result_info(self, models_data):
233232
model_fill["TensorFlow Lite for Microcontrollers"] = (
234233
"{\n\ttf_micro_model_results_object(kb_models[model_index].classifier_id, (model_results_t *)model_results);\n}"
235234
)
235+
model_fill["Neural Network"] = (
236+
"{\n\ttf_micro_model_results_object(kb_models[model_index].classifier_id, (model_results_t *)model_results);\n}"
237+
)
236238

237239
model_fill["Decision Tree Ensemble"] = (
238240
"{\n\ttree_ensemble_model_results_object(kb_models[model_index].classifier_id, (model_results_t *)model_results);\n}"
@@ -256,6 +258,20 @@ def create_sml_classification_result_print_info(self, models_data):
256258
257259
"""
258260

261+
model_fill[
262+
"Neural Network"
263+
] = """{
264+
sml_classification_result_info(model_index, &model_result);\n
265+
pbuf += sprintf(pbuf, ",\\"ModelDebug\\":[");
266+
for (int32_t i=0; i<model_result.num_outputs; i++)
267+
{
268+
pbuf += sprintf(pbuf, "%f, ", model_result.output_tensor[i]);
269+
}
270+
pbuf += sprintf(pbuf, "]");
271+
}
272+
273+
"""
274+
259275
return self.create_case_fill_template_classifier_type(models_data, model_fill)
260276

261277
def create_classifier_structures(self, classifier_types, kb_models):
@@ -264,16 +280,17 @@ def create_classifier_structures(self, classifier_types, kb_models):
264280
formated_classifier_types = [
265281
x.lower().replace(" ", "_") for x in classifier_types
266282
]
267-
283+
268284
def is_neural_netwrok(classifier_type):
269-
return (classifier_type == "tensorflow_lite_for_microcontrollers" or classifier_type == 'neural_network')
285+
return (
286+
classifier_type == "tensorflow_lite_for_microcontrollers"
287+
or classifier_type == "neural_network"
288+
)
270289

271290
formated_classifier_types = [
272291
x if not is_neural_netwrok(x) else self.nn_inference_engine
273292
for x in formated_classifier_types
274293
]
275-
276-
277294

278295
logger.info(
279296
{
@@ -313,12 +330,18 @@ def get_classifier_type_map(self, classifier_config):
313330
return 3
314331
elif classifier_type in ["Bonsai"]:
315332
return 4
316-
elif classifier_type in ["TF Micro", "TensorFlow Lite for Microcontrollers", "Neural Network"]:
333+
elif classifier_type in [
334+
"TF Micro",
335+
"TensorFlow Lite for Microcontrollers",
336+
"Neural Network",
337+
]:
317338
return 5
318339
elif classifier_type in ["Linear Regression"]:
319340
return 6
320341
else:
321-
raise Exception(f"{classifier_type} not supported Classifier Type for code generation")
342+
raise Exception(
343+
f"{classifier_type} not supported Classifier Type for code generation"
344+
)
322345

323346
def create_debug_flagging(self):
324347
ret = []
@@ -354,7 +377,7 @@ def create_kb_classifier_headers(self, classifier_types):
354377
output.append('#include "bonsai_trained_models.h"')
355378

356379
if self.is_tensorflow(classifier_types):
357-
if self.nn_inference_engine == "nnom":
380+
if self.nn_inference_engine == "nnom":
358381
output.append('#include "nnom_trained_models.h"')
359382
output.append('#include "nnom_middleware.h"')
360383

@@ -385,7 +408,7 @@ def create_kb_classifier_header_calls_only(self, classifier_types):
385408

386409
if self.is_tensorflow(classifier_types):
387410
if self.nn_inference_engine == "nnom":
388-
#TODO: NNoM
411+
# TODO: NNoM
389412
pass
390413

391414
if self.nn_inference_engine == "tf_micro":
@@ -424,7 +447,7 @@ def create_kb_classifier_init(self, classifier_types):
424447

425448
if self.is_tensorflow(classifier_types):
426449
if self.nn_inference_engine == "nnom":
427-
output.append(c_line(1, "nnom_init(nnom_classifier_rows, 0);"))
450+
output.append(c_line(1, "nnom_init(nnom_classifier_rows, 0);"))
428451

429452
if self.nn_inference_engine == "tf_micro":
430453
output.append(c_line(1, "tf_micro_init(tf_micro_classifier_rows, 0);"))
@@ -482,15 +505,15 @@ def create_classifier_calls(self, models_data):
482505
elif model["classifier_config"]["classifier"] in [
483506
"TF Micro",
484507
"TensorFlow Lite for Microcontrollers",
485-
"Neural Network"
508+
"Neural Network",
486509
]:
487-
if self.nn_inference_engine=='tf_micro':
510+
if self.nn_inference_engine == "tf_micro":
488511
output_str += c_line(
489512
1,
490513
"ret = tf_micro_simple_submit(kb_model->classifier_id, kb_model->pfeature_vector, kb_model->pmodel_results);",
491514
)
492-
if self.nn_inference_engine=='nnom':
493-
output_str += c_line(
515+
if self.nn_inference_engine == "nnom":
516+
output_str += c_line(
494517
1,
495518
"ret = nnom_simple_submit(kb_model->classifier_id, kb_model->pfeature_vector, kb_model->pmodel_results);",
496519
)

src/server/codegen/model_gen/nnom.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424

2525
def create_classifier_arrays(model_index, model):
26-
26+
2727
pass
2828

2929

@@ -32,42 +32,40 @@ def create_classifier_struct(model_index, model):
3232

3333

3434
def create_classifier_structures(models):
35-
""" typedef struct nnom_classifier_rows
36-
{
37-
uint16_t num_inputs;
38-
uint8_t num_outputs;
39-
float threshold;
40-
uint8_t estimator_type;
41-
nnom_model_t* model;
42-
} nnom_classifier_rows_t;
35+
"""typedef struct nnom_classifier_rows
36+
{
37+
uint16_t num_inputs;
38+
uint8_t num_outputs;
39+
float threshold;
40+
uint8_t estimator_type;
41+
nnom_model_t* model;
42+
} nnom_classifier_rows_t;
4343
"""
44-
44+
4545
outputs = []
4646

4747
iterations = 0
4848
for model in models:
4949
if model["classifier_config"].get("classifier", "PME") in [
5050
"TF Micro",
5151
"TensorFlow Lite for Microcontrollers",
52-
"Neural Network"
52+
"Neural Network",
5353
]:
54-
#outputs.extend(
54+
# outputs.extend(
5555
# create_tf_micro_classifier_arrays(iterations, model["model_arrays"])
56-
#)
56+
# )
5757
iterations += 1
5858

5959
iterations = 0
6060

6161
outputs.append(
6262
(
6363
"nnom_classifier_rows_t nnom_classifier_rows[{}] = ".format(
64-
utils.get_number_classifiers(
65-
models, "TF Micro"
66-
)+utils.get_number_classifiers(
64+
utils.get_number_classifiers(models, "TF Micro")
65+
+ utils.get_number_classifiers(
6766
models, "TensorFlow Lite for Microcontrollers"
68-
)+utils.get_number_classifiers(
69-
models, "Neural Network"
7067
)
68+
+ utils.get_number_classifiers(models, "Neural Network")
7169
)
7270
+ "{"
7371
)
@@ -77,7 +75,7 @@ def create_classifier_structures(models):
7775
if model["classifier_config"].get("classifier", "PME") in [
7876
"TF Micro",
7977
"TensorFlow Lite for Microcontrollers",
80-
"Neural Network"
78+
"Neural Network",
8179
]:
8280
outputs.append("\n\t{")
8381
outputs.append(
@@ -111,13 +109,14 @@ def create_classifier_structures(models):
111109
return outputs
112110

113111

114-
115112
def create_max_tmp_parameters(kb_models):
116-
return []
113+
return []
114+
117115

118116
def validate_model_parameters(data):
119117
pass
120118

119+
121120
def validate_model_configuration(data):
122121
return data
123122

src/server/codegen/model_gen/tf_micro.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def create_classifier_structures(models):
8888
if model["classifier_config"].get("classifier", "PME") in [
8989
"TF Micro",
9090
"TensorFlow Lite for Microcontrollers",
91-
"Neural Network"
91+
"Neural Network",
9292
]:
9393
outputs.extend(
9494
create_tf_micro_classifier_arrays(iterations, model["model_arrays"])
@@ -112,6 +112,7 @@ def create_classifier_structures(models):
112112
if model["classifier_config"].get("classifier", "PME") in [
113113
"TF Micro",
114114
"TensorFlow Lite for Microcontrollers",
115+
"Neural Network",
115116
]:
116117
outputs.append("\n\t{")
117118
outputs.append(

src/server/engine/recognitionengine.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ def initialize_classifier(self, config):
136136
elif self.classifier_type in [
137137
"TF Micro",
138138
"TensorFlow Lite for Microcontrollers",
139+
"Neural Network",
139140
]:
140141
self.classifier = TensorFlowMicro()
141142
self.classifier.load_model(self.neuron_array)
@@ -435,7 +436,7 @@ def reco_kb_pipeline(self, kb_description):
435436
"target_compiler": CompilerDescription.objects.get(
436437
uuid="62aabe7e-4f5d-4167-a786-072e4a8dc158"
437438
),
438-
"nn_inference_engine":'nnom'
439+
"nn_inference_engine": "nnom",
439440
}
440441

441442
logger.userlog(

src/server/library/classifiers/classifiers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def get_classifier(config, save_model_parameters=True):
3939
"Boosted Tree Ensemble": BoostedTreeEnsemble,
4040
"TF Micro": TensorFlowMicro,
4141
"TensorFlow Lite for Microcontrollers": TensorFlowMicro,
42+
"Neural Network": TensorFlowMicro,
4243
"Linear Regression": LinearRegression,
4344
}
4445
try:

src/server/library/fixtures/functions_prod.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,22 @@
848848
automl_available: False
849849
model: library.transform
850850

851+
- fields:
852+
core: True
853+
name: Load Model Neural Network
854+
version: 1
855+
type: Training Algorithm
856+
subtype: Load
857+
path: core_functions/mg_contracts.py
858+
function_in_file: load_model_tensorflow_micro
859+
has_c_version: False
860+
c_file_name:
861+
c_function_name:
862+
deprecated: False
863+
dcl_executable: False
864+
automl_available: False
865+
model: library.transform
866+
851867
- fields:
852868
core: True
853869
name: Bonsai Tree Optimizer

0 commit comments

Comments
 (0)