Skip to content

Commit 23a0d81

Browse files
committed
examples: Improve xor_trees and mnist_cnn
1 parent eae355f commit 23a0d81

File tree

5 files changed

+144
-8
lines changed

5 files changed

+144
-8
lines changed

examples/mnist_cnn/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
# Image classification using Convolutional Neural Network
3+
4+
5+
## Install requirements
6+
7+
Make sure to have Python **3.10** installed.
8+
At the time TinyMaix, used for CNN, only supports Tensorflow <2.14,
9+
which is not supported on Python 3.12 or later.
10+
11+
Make sure to have the Unix port of MicroPython 1.23 setup.
12+
On Windows you can use Windows Subsystem for Linux (WSL), or Docker.
13+
14+
Install the dependencies of this example:
15+
```console
16+
python -m venv venv
17+
source venv/bin/activate
18+
pip install -r requirements.txt
19+
```
20+
21+
## Run training
22+
23+
This will train a Convolutional Neural Network using Keras.
24+
The process will output `.tmdl` that is our built model.
25+
26+
```console
27+
python mnist_train.py
28+
```
29+
30+
## Running on host
31+
32+
```console
33+
curl -o tinymaix_cnn.mpy https://github.com/emlearn/emlearn-micropython/raw/refs/heads/gh-pages/builds/master/x64_6.3/tinymaix_cnn.mpy
34+
35+
micropython mnist_cnn_run.py
36+
```
37+
38+
## Running on device
39+
40+
!Make sure you have it running successfully on host first.
41+
42+
Flash your device with a standard MicroPython firmware,
43+
from the MicroPython.org downloads page.
44+
45+
```console
46+
curl -o device/tinymaix_cnn.mpy https://github.com/emlearn/emlearn-micropython/raw/refs/heads/gh-pages/builds/master/xtensawin_6.3/tinymaix_cnn.mpy
47+
```
48+
49+
```console
50+
mpremote cp device/tinymaix_cnn.mpy :
51+
mpremote cp mnist_cnn.tmdl :
52+
mpremote cp -r data/ :
53+
mpremote run mnist_cnn_run.py
54+
```
55+
56+
## Running with live camera input
57+
58+
This example requires hardware with an ESP32 chip, and an OV2640 camera module.
59+
It has been tested on Lilygo T-Camera Mic v1.6.
60+
61+
You need to build a custom ESP32 firmware,
62+
and add this C module for camera access:
63+
https://github.com/cnadler86/micropython-camera-API
64+
65+
```
66+
mpremote run mnist_cnn_camera.py
67+
```
68+

examples/mnist_cnn/mnist_train.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,15 @@ def train_mnist(h5_file, epochs=10):
5454
model.summary()
5555

5656
model.compile(optimizer='adam', loss = "categorical_crossentropy", metrics = ["categorical_accuracy"])
57-
H = model.fit(x_train, y_train, batch_size=128, epochs=EPOCHS, verbose=1, validation_data = (x_test, y_test), shuffle=True)
57+
H = model.fit(x_train, y_train, batch_size=128, epochs=epochs, verbose=1, validation_data = (x_test, y_test), shuffle=True)
5858

5959
model.save(h5_file)
6060

6161
def generate_test_files(out_dir, x, y):
6262

63+
if not os.path.exists(out_dir):
64+
os.makedirs(out_dir)
65+
6366
expect_bytes = 28*28*1
6467
classes = numpy.unique(y)
6568
X_series = pandas.Series([s for s in x])
@@ -146,29 +149,34 @@ def format_shape(t : tuple[int]):
146149
assert os.path.exists(tmld_file), tmld_file
147150
assert os.path.exists(header_file), header_file
148151

152+
return tmld_file
149153

150154
def main():
151155

152156
h5_file = "mnist_cnn.h5"
153-
tinymaix_tools_dir = './TinyMaix/tools'
157+
tinymaix_tools_dir = '../../dependencies/TinyMaix/tools'
158+
assert os.path.exists(tinymaix_tools_dir), tinymaix_tools_dir
159+
160+
quantize_data = None # disables quantization
161+
quantize_data = os.path.join(tinymaix_tools_dir, 'quant_img_mnist/')
162+
if quantize_data is not None:
163+
assert os.path.exists(quantize_data)
164+
precision = 'int8' if quantize_data else 'fp32'
154165

166+
# Run training
155167
train_mnist(h5_file)
156168

157169
#data = x_test[1]
158170

159-
quantize_data = True # disables quantization
160-
quantize_data = './TinyMaix/tools/quant_img_mnist/'
161-
precision = 'int8' if quantize_data else 'fp32'
162-
163171
# Export the model using TinyMaix
164-
165-
generate_tinymaix_model(h5_file,
172+
out = generate_tinymaix_model(h5_file,
166173
input_shape=(28,28,1),
167174
output_shape=(1,),
168175
tools_dir=tinymaix_tools_dir,
169176
precision=precision,
170177
quantize_data=quantize_data,
171178
)
179+
print('Wrote model to', out)
172180

173181
if __name__ == '__main__':
174182
main()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
tensorflow-cpu<2.14.1
2+
numpy<2.0.0
3+
pandas>=2.0.0
4+
pillow>=10.0.0

examples/xor_trees/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
# XOR using Random Forest
3+
4+
The XOR problem stems from the famous Perceptrons (1969) book
5+
by Marvin Minsky and Seymour Papert.
6+
It can be trivially solved by a decision-tree or decision-tree ensemble.
7+
Thus it is a kind of "Hello World" example.
8+
Simple and an OK sanity check, but particularly useful.
9+
10+
## Install requirements
11+
12+
Make sure to have Python 3.10+ installed.
13+
14+
Make sure to have the Unix port of MicroPython 1.23 setup.
15+
On Windows you can use Windows Subsystem for Linux (WSL), or Docker.
16+
17+
```console
18+
python -m venv venv
19+
source venv/bin/activate
20+
pip install -r requirements.txt
21+
```
22+
23+
## Run training
24+
25+
This will train a RandomForest model using scikit-learn, and output `xor_model.csv`
26+
27+
```console
28+
python xor_train.py
29+
```
30+
31+
## Running on host
32+
33+
```console
34+
curl -o emltrees.mpy https://github.com/emlearn/emlearn-micropython/raw/refs/heads/gh-pages/builds/master/x64_6.3/emltrees.mpy
35+
micropython xor_run.py
36+
```
37+
38+
## Running on device
39+
40+
!Make sure you have it running successfully on host first.
41+
42+
This command is for ESP32 (xtensawin).
43+
For other hardware, replace the string.
44+
45+
```console
46+
mkdir -p device
47+
curl -o device/emltrees.mpy https://github.com/emlearn/emlearn-micropython/raw/refs/heads/gh-pages/builds/master/xtensawin_6.3/emltrees.mpy
48+
mpremote cp emltrees.mpy :
49+
```
50+
51+
```
52+
mpremote run xor_run.py
53+
```
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
emlearn>=0.21.0
2+
scikit-learn>=1.5.0
3+
setuptools>=75.0.0

0 commit comments

Comments
 (0)