emlearn / emlearn-micropython

Efficient Machine Learning engine for MicroPython
51 stars 13 forks source link

Add emlearn as USER_C_MODULE #2

Open Carglglz opened 11 months ago

Carglglz commented 11 months ago

This is a POC to add emlearn as a frozen USER_C_MODULE in firmware. I had to modify/comment some lines, but I got it working (just git diff with the original files to see it)

MicroPython v1.20.0-363-g6d9f9d9ea-dirty on 2023-07-30; darwin [GCC 4.2.1] version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> import emlearn
>>> emlearn.
open            __dict__        addnode         addroot
factorial       predict
>>>

and

$ micropython xor_usermod_run.py 
nodes=7fb4dd606460 roots=7fb4dd6063a0 builder=7fb4dd606308
array('f', [0.0, 0.0]) 0
array('f', [1.0, 1.0]) 0
array('f', [0.0, 1.0]) 1
array('f', [1.0, 0.0]) 1

Not sure if you are interested but I hope this helps as a reference 👍🏼

jonnor commented 11 months ago

Hi @Carglglz. Very cool! Fastest external merge request I have seen - I just made the repository public some hours ago :D

I have actually never tried to make a user module, so this should be helpful. I don't even think I know how to include it right now, but I have seen there is documentation for that. I've been working on the native module case, as I really like the idea of getting started without needing rebuild/reflash. But it is ideal to have both options, I think.

Carglglz commented 11 months ago

@jonnor

I don't even think I know how to include it right now, but I have seen there is documentation for that.

Don't worry I was curious if this could work and it does, then I've just made the draft so you can have a reference in case you want to test/implement it see https://docs.micropython.org/en/latest/develop/cmodules.html

I've been working on the native module case, as I really like the idea of getting started without needing rebuild/reflash. But it is ideal to have both options, I think.

Yes the native module case is probably the way to go for fast iteration, but when you're happy with the result then adding it as a frozen module may save some RAM/ improve performance (although I have not tested it in this case)