diff --git a/MANIFEST b/MANIFEST index 8425fb4..0011e56 100644 --- a/MANIFEST +++ b/MANIFEST @@ -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 diff --git a/dist/micromlgen-0.5.tar.gz b/dist/micromlgen-0.5.tar.gz deleted file mode 100644 index fdc33cf..0000000 Binary files a/dist/micromlgen-0.5.tar.gz and /dev/null differ diff --git a/dist/micromlgen-0.6.tar.gz b/dist/micromlgen-0.6.tar.gz new file mode 100644 index 0000000..7e13d15 Binary files /dev/null and b/dist/micromlgen-0.6.tar.gz differ diff --git a/micromlgen/__pycache__/micromlgen.cpython-36.pyc b/micromlgen/__pycache__/micromlgen.cpython-36.pyc index 2b4cacd..0a2752a 100644 Binary files a/micromlgen/__pycache__/micromlgen.cpython-36.pyc and b/micromlgen/__pycache__/micromlgen.cpython-36.pyc differ diff --git a/micromlgen/micromlgen.py b/micromlgen/micromlgen.py index 010f256..92ec266 100644 --- a/micromlgen/micromlgen.py +++ b/micromlgen/micromlgen.py @@ -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 = { @@ -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) diff --git a/micromlgen/templates/compute_kernels.jinja b/micromlgen/templates/compute_kernels.jinja index e86bcc1..9decd6b 100644 --- a/micromlgen/templates/compute_kernels.jinja +++ b/micromlgen/templates/compute_kernels.jinja @@ -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 %} \ No newline at end of file diff --git a/micromlgen/templates/kernel_function.jinja b/micromlgen/templates/kernel_function.jinja index 0ca4bb2..c38fc9d 100644 --- a/micromlgen/templates/kernel_function.jinja +++ b/micromlgen/templates/kernel_function.jinja @@ -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 %} diff --git a/publish b/publish index b745c14..61a4270 100755 --- a/publish +++ b/publish @@ -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/* \ No newline at end of file diff --git a/setup.py b/setup.py index ef7267b..ca929ca 100644 --- a/setup.py +++ b/setup.py @@ -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 = 'web@agrimag.it', 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',