This repository has been archived by the owner on May 25, 2024. It is now read-only.
forked from agrimagsrl/micromlgen
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
66a8fbb
commit a9a59c4
Showing
8 changed files
with
82 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,5 +16,6 @@ | |
'GaussianNB', | ||
'LogisticRegression', | ||
'PCA', | ||
'PrincipalFFT' | ||
'PrincipalFFT', | ||
'LinearRegression' | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{% if tree['left'][i] != tree['right'][i] %} | ||
if (x[{{ tree['features'][i] }}] <= {{ tree['thresholds'][i] }}) { | ||
{% with i = tree['left'][i] %} | ||
{% include 'xgboost/tree.jinja' %} | ||
{% endwith %} | ||
} | ||
else { | ||
{% with i = tree['right'][i] %} | ||
{% include 'xgboost/tree.jinja' %} | ||
{% endwith %} | ||
} | ||
{% else %} | ||
votes[{{ class_idx }}] += {{ tree['thresholds'][i] }}; | ||
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{% extends '_skeleton.jinja' %} | ||
|
||
{% block predict %} | ||
float votes[{{ n_classes }}] = { 0.0f }; | ||
|
||
{% for k, tree in f.enumerate(trees) %} | ||
{% with i = 0, class_idx = k % n_classes %} | ||
// tree #{{ k + 1 }} | ||
{% include 'xgboost/tree.jinja' %} | ||
{% endwith %} | ||
{% endfor %} | ||
|
||
{% include 'vote.jinja' %} | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from micromlgen.utils import jinja, check_type | ||
from tempfile import NamedTemporaryFile | ||
import json | ||
|
||
|
||
def format_tree(tree): | ||
""" | ||
Format xgboost tree like a sklearn DecisionTree | ||
:param tree: | ||
:return: | ||
""" | ||
split_indices = tree['split_indices'] | ||
split_conditions = tree['split_conditions'] | ||
left_children = tree['left_children'] | ||
right_children = tree['right_children'] | ||
return { | ||
'left': left_children, | ||
'right': right_children, | ||
'features': split_indices, | ||
'thresholds': split_conditions | ||
} | ||
|
||
|
||
def is_xgboost(clf): | ||
"""Test if classifier can be ported""" | ||
return check_type(clf, 'XGBClassifier') | ||
|
||
|
||
def port_xgboost(clf, **kwargs): | ||
"""Port a XGBoost classifier""" | ||
with NamedTemporaryFile('w+', suffix='.json', encoding='utf-8') as tmp: | ||
clf.save_model(tmp.name) | ||
tmp.seek(0) | ||
decoded = json.load(tmp) | ||
trees = [format_tree(tree) for tree in decoded['learner']['gradient_booster']['model']['trees']] | ||
print(trees) | ||
return jinja('xgboost/xgboost.jinja', { | ||
'n_classes': int(decoded['learner']['learner_model_param']['num_class']), | ||
'trees': trees, | ||
}, { | ||
'classname': 'XGBClassifier' | ||
}, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,13 +7,13 @@ | |
setup( | ||
name = 'micromlgen', | ||
packages = ['micromlgen'], | ||
version = '1.1.10', | ||
version = '1.1.11', | ||
license='MIT', | ||
description = 'Generate C code for microcontrollers from Python\'s sklearn classifiers', | ||
author = 'Simone Salerno', | ||
author_email = '[email protected]', | ||
url = 'https://github.com/eloquentarduino/micromlgen', | ||
download_url = 'https://github.com/eloquentarduino/micromlgen/archive/v_1110.tar.gz', | ||
download_url = 'https://github.com/eloquentarduino/micromlgen/archive/v_1111.tar.gz', | ||
keywords = [ | ||
'ML', | ||
'microcontrollers', | ||
|