Skip to content

Commit e9527d7

Browse files
committed
add Neural Network as classifier
1 parent d36ff6e commit e9527d7

File tree

6 files changed

+68
-16
lines changed

6 files changed

+68
-16
lines changed

src/server/codegen/knowledgepack_model_graph_mixin.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ def get_classifier_init(self, knowledgepack):
614614
elif classifier_config["classifier"] in [
615615
"TF Micro",
616616
"TensorFlow Lite for Microcontrollers",
617-
"Neural Network"
617+
"Neural Network",
618618
]:
619619
return {"classifier": "Neural Network"}
620620

@@ -1618,5 +1618,7 @@ def get_classifier_type(knowledgepack):
16181618

16191619
if classifier_type == "tensorflow_lite_for_microcontrollers":
16201620
classifier_type = "tf_micro"
1621+
if classifier_type == "neural_network":
1622+
classifier_type = "tf_micro"
16211623

16221624
return classifier_type

src/server/codegen/model_gen/model_gen.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
linear_regression,
2525
pme,
2626
tf_micro,
27-
nnom
27+
nnom,
2828
)
2929
from django.core.exceptions import ValidationError
3030

@@ -35,12 +35,13 @@
3535
"bonsai",
3636
"pme",
3737
"linear_regression",
38-
"nnom"
38+
"nnom",
3939
]
4040

4141
CLASSIFER_MAP = {
4242
"decision tree ensemble": "decision_tree_ensemble",
4343
"tensorflow lite for microcontrollers": "tf_micro",
44+
"Neural Network": "tf_micro",
4445
"nnom": "nnom",
4546
"pme": "pme",
4647
"boosted tree ensemble": "boosted_tree_ensemble",
@@ -61,7 +62,7 @@ def get_classifier_type(model_configuration):
6162
return classifier_type.lower()
6263

6364

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

8384
if classifier_type == "linear_regression":
8485
return linear_regression.create_classifier_structures(kb_models)
85-
86+
8687
if classifier_type == "nnom":
8788
return nnom.create_classifier_structures(kb_models)
88-
89+
8990
return ""
9091

9192
@staticmethod
@@ -107,7 +108,7 @@ def create_max_tmp_parameters(classifier_type, kb_models):
107108

108109
if classifier_type == "linear_regression":
109110
return linear_regression.create_max_tmp_parameters(kb_models)
110-
111+
111112
if classifier_type == "nnom":
112113
return nnom.create_max_tmp_parameters(kb_models)
113114

@@ -151,7 +152,7 @@ def validate_model_parameters(model_parameters, model_configuration):
151152

152153
if classifier_type == "linear_regression":
153154
return linear_regression.validate_model_parameters(model_parameters)
154-
155+
155156
if classifier_type == "nnom":
156157
return nnom.validate_model_parameters(model_parameters)
157158

@@ -180,8 +181,7 @@ def validate_model_configuration(model_configuration):
180181

181182
if classifier_type == "linear_regression":
182183
return linear_regression.validate_model_configuration(model_configuration)
183-
184-
184+
185185
if classifier_type == "nnom":
186186
return nnom.validate_model_configuration(model_configuration)
187187

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

205205
if classifier_type == "linear_regression":
206206
return linear_regression.get_output_tensor_size(model)
207-
207+
208208
if classifier_type == "nnom":
209209
return nnom.get_output_tensor_size(model)
210210

@@ -232,7 +232,7 @@ def get_input_feature_type(model):
232232

233233
if classifier_type == "linear_regression":
234234
return FLOAT
235-
235+
236236
if classifier_type == "nnom":
237237
return UINT8_T
238238

@@ -263,7 +263,7 @@ def get_input_feature_def(model):
263263

264264
if classifier_type == "nnom":
265265
return UINT8_T
266-
266+
267267
raise ValueError("No classifier type found")
268268

269269
@staticmethod
@@ -273,7 +273,7 @@ def get_model_type(model):
273273
CLASSIFICATION = 1
274274
if classifier_type == "tf_micro":
275275
return CLASSIFICATION
276-
276+
277277
if classifier_type == "nnom":
278278
return CLASSIFICATION
279279

src/server/library/core_functions/mg_contracts.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,23 @@ def tensorflow_micro():
12741274
tensorflow_micro_contracts = {"input_contract": [], "output_contract": []}
12751275

12761276

1277+
def neural_network():
1278+
"""
1279+
The Neural Network uses Tensorflow Lite for Microcontrollers, an inference engine
1280+
from Google optimized run machine learning models on embedded devices.
1281+
1282+
Tensorflow Lite for Microcontrollers supports a subset of all Tensorflow functions. For a full
1283+
list see `all_ops_resolver.cc <https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/micro/all_ops_resolver.cc>`_.
1284+
1285+
For additional documentation on Tensorflow Lite for Microcontrollers see `here <https://www.tensorflow.org/lite/microcontrollers>`_.
1286+
"""
1287+
1288+
return None
1289+
1290+
1291+
neural_network_contracts = {"input_contract": [], "output_contract": []}
1292+
1293+
12771294
def load_model_tensorflow_micro(
12781295
input_data,
12791296
label_column,
@@ -1433,7 +1450,9 @@ def load_model_tensorflow_micro(
14331450
"type": "list",
14341451
"element_type": "str",
14351452
"handle_by_set": True,
1436-
"options": [{"name": "TensorFlow Lite for Microcontrollers"}],
1453+
"options": [
1454+
{"name": "TensorFlow Lite for Microcontrollers", "name": "Neural Network"}
1455+
],
14371456
},
14381457
{"name": "class_map", "type": "dict", "handle_by_set": True, "default": None},
14391458
{

src/server/library/fixtures/functions_prod.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,23 @@
831831
automl_available: False
832832
model: library.transform
833833

834+
- fields:
835+
core: True
836+
name: Neural Network
837+
version: 1
838+
type: Classifier
839+
subtype: NN
840+
path: core_functions/mg_contracts.py
841+
function_in_file: neural_network
842+
has_c_version: False
843+
c_file_name:
844+
c_function_name:
845+
deprecated: False
846+
dcl_executable: False
847+
uuid: 3994d48f-7b8a-4807-b919-284bbe045928
848+
automl_available: False
849+
model: library.transform
850+
834851
- fields:
835852
core: True
836853
name: Load Model TensorFlow Lite for Microcontrollers

src/server/library/fixtures/parameter_inventory.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,3 +322,16 @@
322322
binary_classifiers: 0
323323
allow_unknown: 1
324324
model: library.parameterinventory
325+
326+
- fields:
327+
uuid: e040c660-45c2-4e48-92fc-2a22310076c9
328+
function: 3994d48f-7b8a-4807-b919-284bbe045928
329+
function_name: Neural Network
330+
pipeline_key: classifiers
331+
variable_name: null
332+
variable_type: null
333+
variable_values: null
334+
classifiers_optimizers_group: 4
335+
binary_classifiers: 0
336+
allow_unknown: 1
337+
model: library.parameterinventory

src/server/library/fixtures/pipeline_schema.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,8 @@
312312
"TransformList": null,
313313
"Set": false,
314314
"Exclude": [
315-
"TF Micro"
315+
"TF Micro",
316+
"TensorFlow Lite for Microcontrollers"
316317
],
317318
"Limit": 1
318319
},

0 commit comments

Comments
 (0)