Skip to content
This repository has been archived by the owner on Jan 15, 2020. It is now read-only.

Commit

Permalink
add attiny support
Browse files Browse the repository at this point in the history
  • Loading branch information
agrimagsrl committed Dec 21, 2019
1 parent 1dcb1ef commit 915f055
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 9 deletions.
1 change: 0 additions & 1 deletion MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ setup.cfg
setup.py
micromlgen/__init__.py
micromlgen/micromlgen.py
micromlgen/micromlgen_test.py
micromlgen/templates/binary_classification.jinja
micromlgen/templates/classmap.jinja
micromlgen/templates/compute_class.jinja
Expand Down
Binary file removed dist/micromlgen-0.5.tar.gz
Binary file not shown.
Binary file added dist/micromlgen-0.6.tar.gz
Binary file not shown.
Binary file modified micromlgen/__pycache__/micromlgen.cpython-36.pyc
Binary file not shown.
5 changes: 3 additions & 2 deletions micromlgen/micromlgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from jinja2 import FileSystemLoader, Environment


def port(clf, test_set=None, classmap=None, **kwargs):
def port(clf, test_set=None, classmap=None, platform='arduino', **kwargs):
assert type(clf).__name__ == 'SVC', 'Only sklearn.svm.SVC is supported for now'
support_v = clf.support_vectors_
template_data = {
Expand All @@ -25,7 +25,8 @@ def port(clf, test_set=None, classmap=None, **kwargs):
'classmap': classmap,
'F': {
'enumerate': enumerate,
}
},
'isAttiny': platform == 'attiny',
}
dir_path = os.path.dirname(os.path.realpath(__file__))
print(dir_path)
Expand Down
11 changes: 10 additions & 1 deletion micromlgen/templates/compute_kernels.jinja
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
{% if platform == 'attiny' %}
float w[{{ FEATURES_DIM }}];
{% endif %}

{% for i, w in F.enumerate(support_v) %}
kernels[{{ i }}] = compute_kernel(x, {% for j, wj in F.enumerate(w) %} {% if j > 0 %},{% endif %} {{ wj }} {% endfor %});
{% if isAttiny %}
{% for j, wj in F.enumerate(w) %} w[{{ j }}] = {{ wj }}; {% endfor %}
kernels[{{ i }}] = compute_kernel(x, w);
{% else %}
kernels[{{ i }}] = compute_kernel(x, {% for j, wj in F.enumerate(w) %} {% if j > 0 %},{% endif %} {{ wj }} {% endfor %});
{% endif %}
{% endfor %}
9 changes: 7 additions & 2 deletions micromlgen/templates/kernel_function.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@
* Compute kernel between feature vector and support vector.
* Kernel type: {{ KERNEL_TYPE }}
*/
{% if isAttiny %}
double compute_kernel(double x[{{ FEATURES_DIM }}], double w[{{ FEATURES_DIM }}]) {
double kernel = 0.0;
{% else %}
double compute_kernel(double x[{{ FEATURES_DIM }}], ...) {
va_list w;
double kernel = 0.0;

va_start(w, {{ FEATURES_DIM }});
{% endif %}

for (uint16_t i = 0; i < {{ FEATURES_DIM }}; i++)
{% if KERNEL_TYPE in ['linear', 'poly', 'sigmoid'] %}
kernel += x[i] * va_arg(w, double);
kernel += x[i] * {% if isAttiny %} w[i] {% else %} va_arg(w, double), 2) {% endif %};
{% elif KERNEL_TYPE == 'rbf' %}
kernel += pow(x[i] - va_arg(w, double), 2);
kernel += pow(x[i] - {% if isAttiny %} w[i] {% else %} va_arg(w, double), 2) {% endif %};
{% else %}
#error "UNKNOWN KERNEL {{ kernel }}";
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion publish
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

git push origin master -f
#git push origin master -f
rm -rf dist/*
python setup.py sdist
twine upload dist/*
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
setup(
name = 'micromlgen',
packages = ['micromlgen'],
version = '0.5',
version = '0.6',
license='MIT',
description = 'Generate C code for microcontrollers from Python\'s sklearn classifiers',
author = 'Simone Salerno',
author_email = '[email protected]',
url = 'https://github.com/agrimagsrl/micromlgen',
download_url = 'https://github.com/agrimagsrl/micromlgen/archive/v_05.tar.gz',
download_url = 'https://github.com/agrimagsrl/micromlgen/archive/v_06.tar.gz',
keywords = ['ML', 'microcontrollers', 'sklearn', 'machine learning'],
install_requires=[
'jinja2',
Expand Down

0 comments on commit 915f055

Please sign in to comment.