https://github.com/h3x4g0ns/model2c
tool to convert ML models into low-level inference for embedded platforms
https://github.com/h3x4g0ns/model2c
arduino embedded-systems machine-learning onnx tensorflow tinyml torch
Last synced: about 2 months ago
JSON representation
tool to convert ML models into low-level inference for embedded platforms
- Host: GitHub
- URL: https://github.com/h3x4g0ns/model2c
- Owner: h3x4g0ns
- License: mit
- Created: 2022-07-08T05:00:27.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-07-29T04:55:46.000Z (almost 4 years ago)
- Last Synced: 2025-11-28T20:03:01.145Z (6 months ago)
- Topics: arduino, embedded-systems, machine-learning, onnx, tensorflow, tinyml, torch
- Language: Python
- Homepage:
- Size: 5.14 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# model2c

[](https://badge.fury.io/py/model2c)
## About the Project
Python API and Command Line tool to convert ML models into low-level inference for embedded platforms
## Getting Started
### Prerequisites
Make sure you have `tensorflow tf2onnx` or `torch` installed.
Furthermore, make sure you have `onnx2c` installed and added to `PATH`.
Lastly you need ProtocolBuffers libraries installed, e.g.:
- Ubuntu: `apt install libprotobuf-dev protobuf-compiler`
- MacOS: `brew install protobuf`
Get the sources:
```sh
git clone https://github.com/kraiskil/onnx2c.git
cd onnx2c
git submodule update --init
```
Then run a standard CMake build
```sh
mkdir build
cd build
cmake ..
make onnx2c
```
And finally add to path
```sh
export PATH=$PATH:/path/to/onnx2c/folder
```
## Installation
You can can install the package through `pypi`:
```sh
pip install model2c
```
Or you can clone the repo and build directly from source:
```sh
git clone git@github.com:h3x4g0ns/model2c.git
cd model2c
make install
```
### Usage
Train a model with correponding data until sufficient metrics are obtained.
```py
import torch
import model2c.pytorch import convert
# run convertor
convert(model=torch_model,
input_shape=(batch_size, 1, 224, 224),
quantization="fp32",
output_file="model.c")
print(f"size of output model: {os.path.getsize('model.c')/1024} kilobytes")
```
## Support
`model2c` currently supports the following ML frameworks
- `torch`
- `tf/keras`
## To Do
- [x] `torch` convert
- [x] `tf` convert
- [ ] make command line utility
- [ ] include dynamic axis for batch size