Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 39 additions & 16 deletions src/server/codegen/knowledgepack_codegen_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,20 +206,19 @@ def create_tensorflow_build_flags(self, models):
for model in models:
if self.is_tensorflow(model["classifier_config"]["classifier"]):
if self.nn_inference_engine == "nnom":
#TODO: NNoM
# TODO: NNoM
pass

if self.nn_inference_engine == "tf_micro":
return tf_micro_cflags


return ""

def create_kb_model_tf_micro_binary(self, models):
for model in models:
if self.is_tensorflow(model["classifier_config"]["classifier"]):
if self.nn_inference_engine == "nnom":
#TODO: NNoM
# TODO: NNoM
pass

if self.nn_inference_engine == "tf_micro":
Expand All @@ -233,6 +232,9 @@ def create_sml_classification_result_info(self, models_data):
model_fill["TensorFlow Lite for Microcontrollers"] = (
"{\n\ttf_micro_model_results_object(kb_models[model_index].classifier_id, (model_results_t *)model_results);\n}"
)
model_fill["Neural Network"] = (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs an if statement that switchest between the two based on the inference engine selected

"{\n\ttf_micro_model_results_object(kb_models[model_index].classifier_id, (model_results_t *)model_results);\n}"
)

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

"""

model_fill[
"Neural Network"
] = """{
sml_classification_result_info(model_index, &model_result);\n
pbuf += sprintf(pbuf, ",\\"ModelDebug\\":[");
for (int32_t i=0; i<model_result.num_outputs; i++)
{
pbuf += sprintf(pbuf, "%f, ", model_result.output_tensor[i]);
}
pbuf += sprintf(pbuf, "]");
}

"""

return self.create_case_fill_template_classifier_type(models_data, model_fill)

def create_classifier_structures(self, classifier_types, kb_models):
Expand All @@ -264,16 +280,17 @@ def create_classifier_structures(self, classifier_types, kb_models):
formated_classifier_types = [
x.lower().replace(" ", "_") for x in classifier_types
]

def is_neural_netwrok(classifier_type):
return (classifier_type == "tensorflow_lite_for_microcontrollers" or classifier_type == 'neural_network')
return (
classifier_type == "tensorflow_lite_for_microcontrollers"
or classifier_type == "neural_network"
)

formated_classifier_types = [
x if not is_neural_netwrok(x) else self.nn_inference_engine
for x in formated_classifier_types
]



logger.info(
{
Expand Down Expand Up @@ -313,12 +330,18 @@ def get_classifier_type_map(self, classifier_config):
return 3
elif classifier_type in ["Bonsai"]:
return 4
elif classifier_type in ["TF Micro", "TensorFlow Lite for Microcontrollers", "Neural Network"]:
elif classifier_type in [
"TF Micro",
"TensorFlow Lite for Microcontrollers",
"Neural Network",
]:
return 5
elif classifier_type in ["Linear Regression"]:
return 6
else:
raise Exception(f"{classifier_type} not supported Classifier Type for code generation")
raise Exception(
f"{classifier_type} not supported Classifier Type for code generation"
)

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

if self.is_tensorflow(classifier_types):
if self.nn_inference_engine == "nnom":
if self.nn_inference_engine == "nnom":
output.append('#include "nnom_trained_models.h"')
output.append('#include "nnom_middleware.h"')

Expand Down Expand Up @@ -385,7 +408,7 @@ def create_kb_classifier_header_calls_only(self, classifier_types):

if self.is_tensorflow(classifier_types):
if self.nn_inference_engine == "nnom":
#TODO: NNoM
# TODO: NNoM
pass

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

if self.is_tensorflow(classifier_types):
if self.nn_inference_engine == "nnom":
output.append(c_line(1, "nnom_init(nnom_classifier_rows, 0);"))
output.append(c_line(1, "nnom_init(nnom_classifier_rows, 0);"))

if self.nn_inference_engine == "tf_micro":
output.append(c_line(1, "tf_micro_init(tf_micro_classifier_rows, 0);"))
Expand Down Expand Up @@ -482,15 +505,15 @@ def create_classifier_calls(self, models_data):
elif model["classifier_config"]["classifier"] in [
"TF Micro",
"TensorFlow Lite for Microcontrollers",
"Neural Network"
"Neural Network",
]:
if self.nn_inference_engine=='tf_micro':
if self.nn_inference_engine == "tf_micro":
output_str += c_line(
1,
"ret = tf_micro_simple_submit(kb_model->classifier_id, kb_model->pfeature_vector, kb_model->pmodel_results);",
)
if self.nn_inference_engine=='nnom':
output_str += c_line(
if self.nn_inference_engine == "nnom":
output_str += c_line(
1,
"ret = nnom_simple_submit(kb_model->classifier_id, kb_model->pfeature_vector, kb_model->pmodel_results);",
)
Expand Down
4 changes: 3 additions & 1 deletion src/server/codegen/knowledgepack_model_graph_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ def get_classifier_init(self, knowledgepack):
elif classifier_config["classifier"] in [
"TF Micro",
"TensorFlow Lite for Microcontrollers",
"Neural Network"
"Neural Network",
]:
return {"classifier": "Neural Network"}

Expand Down Expand Up @@ -1618,5 +1618,7 @@ def get_classifier_type(knowledgepack):

if classifier_type == "tensorflow_lite_for_microcontrollers":
classifier_type = "tf_micro"
if classifier_type == "neural_network":
classifier_type = "tf_micro"

return classifier_type
26 changes: 13 additions & 13 deletions src/server/codegen/model_gen/model_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
linear_regression,
pme,
tf_micro,
nnom
nnom,
)
from django.core.exceptions import ValidationError

Expand All @@ -35,12 +35,13 @@
"bonsai",
"pme",
"linear_regression",
"nnom"
"nnom",
]

CLASSIFER_MAP = {
"decision tree ensemble": "decision_tree_ensemble",
"tensorflow lite for microcontrollers": "tf_micro",
"Neural Network": "tf_micro",
"nnom": "nnom",
"pme": "pme",
"boosted tree ensemble": "boosted_tree_ensemble",
Expand All @@ -61,7 +62,7 @@ def get_classifier_type(model_configuration):
return classifier_type.lower()


#TODO: Make this an interface that returns the object instead of having all of these if statements
# TODO: Make this an interface that returns the object instead of having all of these if statements
class ModelGen:
@staticmethod
def create_classifier_structures(classifier_type, kb_models):
Expand All @@ -82,10 +83,10 @@ def create_classifier_structures(classifier_type, kb_models):

if classifier_type == "linear_regression":
return linear_regression.create_classifier_structures(kb_models)

if classifier_type == "nnom":
return nnom.create_classifier_structures(kb_models)

return ""

@staticmethod
Expand All @@ -107,7 +108,7 @@ def create_max_tmp_parameters(classifier_type, kb_models):

if classifier_type == "linear_regression":
return linear_regression.create_max_tmp_parameters(kb_models)

if classifier_type == "nnom":
return nnom.create_max_tmp_parameters(kb_models)

Expand Down Expand Up @@ -151,7 +152,7 @@ def validate_model_parameters(model_parameters, model_configuration):

if classifier_type == "linear_regression":
return linear_regression.validate_model_parameters(model_parameters)

if classifier_type == "nnom":
return nnom.validate_model_parameters(model_parameters)

Expand Down Expand Up @@ -180,8 +181,7 @@ def validate_model_configuration(model_configuration):

if classifier_type == "linear_regression":
return linear_regression.validate_model_configuration(model_configuration)



if classifier_type == "nnom":
return nnom.validate_model_configuration(model_configuration)

Expand All @@ -204,7 +204,7 @@ def get_output_tensor_size(classifier_type, model):

if classifier_type == "linear_regression":
return linear_regression.get_output_tensor_size(model)

if classifier_type == "nnom":
return nnom.get_output_tensor_size(model)

Expand Down Expand Up @@ -232,7 +232,7 @@ def get_input_feature_type(model):

if classifier_type == "linear_regression":
return FLOAT

if classifier_type == "nnom":
return UINT8_T

Expand Down Expand Up @@ -263,7 +263,7 @@ def get_input_feature_def(model):

if classifier_type == "nnom":
return UINT8_T

raise ValueError("No classifier type found")

@staticmethod
Expand All @@ -273,7 +273,7 @@ def get_model_type(model):
CLASSIFICATION = 1
if classifier_type == "tf_micro":
return CLASSIFICATION

if classifier_type == "nnom":
return CLASSIFICATION

Expand Down
41 changes: 20 additions & 21 deletions src/server/codegen/model_gen/nnom.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@


def create_classifier_arrays(model_index, model):

pass


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


def create_classifier_structures(models):
""" typedef struct nnom_classifier_rows
{
uint16_t num_inputs;
uint8_t num_outputs;
float threshold;
uint8_t estimator_type;
nnom_model_t* model;
} nnom_classifier_rows_t;
"""typedef struct nnom_classifier_rows
{
uint16_t num_inputs;
uint8_t num_outputs;
float threshold;
uint8_t estimator_type;
nnom_model_t* model;
} nnom_classifier_rows_t;
"""

outputs = []

iterations = 0
for model in models:
if model["classifier_config"].get("classifier", "PME") in [
"TF Micro",
"TensorFlow Lite for Microcontrollers",
"Neural Network"
"Neural Network",
]:
#outputs.extend(
# outputs.extend(
# create_tf_micro_classifier_arrays(iterations, model["model_arrays"])
#)
# )
iterations += 1

iterations = 0

outputs.append(
(
"nnom_classifier_rows_t nnom_classifier_rows[{}] = ".format(
utils.get_number_classifiers(
models, "TF Micro"
)+utils.get_number_classifiers(
utils.get_number_classifiers(models, "TF Micro")
+ utils.get_number_classifiers(
models, "TensorFlow Lite for Microcontrollers"
)+utils.get_number_classifiers(
models, "Neural Network"
)
+ utils.get_number_classifiers(models, "Neural Network")
)
+ "{"
)
Expand All @@ -77,7 +75,7 @@ def create_classifier_structures(models):
if model["classifier_config"].get("classifier", "PME") in [
"TF Micro",
"TensorFlow Lite for Microcontrollers",
"Neural Network"
"Neural Network",
]:
outputs.append("\n\t{")
outputs.append(
Expand Down Expand Up @@ -111,13 +109,14 @@ def create_classifier_structures(models):
return outputs



def create_max_tmp_parameters(kb_models):
return []
return []


def validate_model_parameters(data):
pass


def validate_model_configuration(data):
return data

Expand Down
3 changes: 2 additions & 1 deletion src/server/codegen/model_gen/tf_micro.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def create_classifier_structures(models):
if model["classifier_config"].get("classifier", "PME") in [
"TF Micro",
"TensorFlow Lite for Microcontrollers",
"Neural Network"
"Neural Network",
]:
outputs.extend(
create_tf_micro_classifier_arrays(iterations, model["model_arrays"])
Expand All @@ -112,6 +112,7 @@ def create_classifier_structures(models):
if model["classifier_config"].get("classifier", "PME") in [
"TF Micro",
"TensorFlow Lite for Microcontrollers",
"Neural Network",
]:
outputs.append("\n\t{")
outputs.append(
Expand Down
3 changes: 2 additions & 1 deletion src/server/engine/recognitionengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def initialize_classifier(self, config):
elif self.classifier_type in [
"TF Micro",
"TensorFlow Lite for Microcontrollers",
"Neural Network",
]:
self.classifier = TensorFlowMicro()
self.classifier.load_model(self.neuron_array)
Expand Down Expand Up @@ -435,7 +436,7 @@ def reco_kb_pipeline(self, kb_description):
"target_compiler": CompilerDescription.objects.get(
uuid="62aabe7e-4f5d-4167-a786-072e4a8dc158"
),
"nn_inference_engine":'nnom'
"nn_inference_engine": "nnom",
}

logger.userlog(
Expand Down
Loading