Skip to content

Commit 9eb3038

Browse files
committed
first commit
0 parents  commit 9eb3038

File tree

7 files changed

+103
-0
lines changed

7 files changed

+103
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
try.py
2+
publish
3+
venv
4+
.idea
5+
tinymlgen/__pycache__

MANIFEST

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# file GENERATED by distutils, do NOT edit
2+
setup.py
3+
tinymlgen/tinymlgen.py

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# TinyML gen
2+
3+
This is a simple package to export a model trained in Tensorflow Lite
4+
to a plain C array, ready to be used for inference on microcontrollers.
5+
6+
### Install
7+
8+
```shell script
9+
pip install tinymlgen
10+
```
11+
12+
### Use
13+
14+
```python
15+
from tinymlgen import port
16+
17+
if __name__ == '__main__':
18+
tf_model = create_tf_model()
19+
c_code = port(tf_model)
20+
```
21+
22+
### Configuration
23+
24+
You can pass a few parameters to the `port` function:
25+
26+
- `optimize (=True)`: apply optimizers to the exported model.
27+
Can either be a list of optimizers or a boolean, in which case
28+
`OPTIMIZE_FOR_SIZE` is applied
29+
- `variable_name (='model_data')`: give the exported array a custom name
30+
- `pretty_print (=False)`: print the array in a nicely formatted arrangement
31+
32+

dist/tinymlgen-0.1.tar.gz

1.38 KB
Binary file not shown.

setup.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from distutils.core import setup
2+
setup(
3+
name = 'tinymlgen',
4+
packages = ['tinymlgen'],
5+
version = '0.1',
6+
license='MIT',
7+
description = 'Generate C code for microcontrollers from Tensorflow models',
8+
author = 'Simone Salerno',
9+
author_email = '[email protected]',
10+
url = 'https://github.com/eloquentarduino/tinymlgen',
11+
download_url = 'https://github.com/eloquentarduino/tinymlgen/archive/v_01.tar.gz',
12+
keywords = ['ML', 'microcontrollers', 'tensorflow', 'machine learning'],
13+
install_requires=[
14+
'tensorflow',
15+
'hexdump'
16+
],
17+
classifiers=[
18+
'Development Status :: 3 - Alpha',
19+
'Intended Audience :: Developers',
20+
'Topic :: Software Development :: Code Generators',
21+
'License :: OSI Approved :: MIT License',
22+
'Programming Language :: Python :: 3',
23+
'Programming Language :: Python :: 3.4',
24+
'Programming Language :: Python :: 3.5',
25+
'Programming Language :: Python :: 3.6',
26+
],
27+
)

tinymlgen/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from tinymlgen.tinymlgen import port

tinymlgen/tinymlgen.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import re
2+
import hexdump
3+
import tensorflow as tf
4+
5+
6+
def port(model, optimize=True, variable_name='model_data', pretty_print=False):
7+
converter = tf.lite.TFLiteConverter.from_keras_model(model)
8+
if optimize:
9+
if isinstance(optimize, bool):
10+
optimizers = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]
11+
else:
12+
optimizers = optimize
13+
converter.optimizations = optimizers
14+
tflite_model = converter.convert()
15+
bytes = hexdump.dump(tflite_model).split(' ')
16+
c_array = ', '.join(['0x%02x' % int(byte, 16) for byte in bytes])
17+
c = 'const unsigned char %s[] DATA_ALIGN_ATTRIBUTE = {%s};' % (variable_name, c_array)
18+
if pretty_print:
19+
c = c.replace('{', '{\n\t').replace('}', '\n}')
20+
c = re.sub(r'(0x..?, ){12}', lambda x: '%s\n\t' % x.group(0), c)
21+
c += '\nconst int %s_len = %d;' % (variable_name, len(bytes))
22+
preamble = '''
23+
#ifdef __has_attribute
24+
#define HAVE_ATTRIBUTE(x) __has_attribute(x)
25+
#else
26+
#define HAVE_ATTRIBUTE(x) 0
27+
#endif
28+
#if HAVE_ATTRIBUTE(aligned) || (defined(__GNUC__) && !defined(__clang__))
29+
#define DATA_ALIGN_ATTRIBUTE __attribute__((aligned(4)))
30+
#else
31+
#define DATA_ALIGN_ATTRIBUTE
32+
#endif
33+
34+
'''
35+
return preamble + c

0 commit comments

Comments
 (0)