Skip to content

Commit 9eec038

Browse files
SineFromSDExample
1 parent 4b4a6bc commit 9eec038

File tree

6 files changed

+122
-1
lines changed

6 files changed

+122
-1
lines changed

examples/.DS_Store

8 KB
Binary file not shown.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <SPI.h>
2+
#include <SD.h>
3+
#include <EloquentTinyML.h>
4+
5+
#define NUMBER_OF_INPUTS 1
6+
#define NUMBER_OF_OUTPUTS 1
7+
#define TENSOR_ARENA_SIZE 2*1024
8+
9+
uint8_t *loadedModel;
10+
Eloquent::TinyML::TfLite<NUMBER_OF_INPUTS, NUMBER_OF_OUTPUTS, TENSOR_ARENA_SIZE> ml;
11+
12+
13+
void loadModel(void);
14+
15+
16+
/**
17+
*
18+
*/
19+
void setup() {
20+
Serial.begin(115200);
21+
SPI.begin();
22+
delay(3000);
23+
24+
if (!SD.begin(4)) {
25+
Serial.println("Cannot init SD");
26+
delay(60000);
27+
}
28+
29+
loadModel();
30+
31+
if (!ml.begin(loadedModel)) {
32+
Serial.println("Cannot inialize model");
33+
Serial.println(ml.errorMessage());
34+
delay(60000);
35+
}
36+
}
37+
38+
39+
/**
40+
*
41+
*/
42+
void loop() {
43+
// pick up a random x and predict its sine
44+
float x = 3.14 * random(100) / 100;
45+
float y = sin(x);
46+
float input[1] = { x };
47+
float predicted = ml.predict(input);
48+
49+
Serial.print("sin(");
50+
Serial.print(x);
51+
Serial.print(") = ");
52+
Serial.print(y);
53+
Serial.print("\t predicted: ");
54+
Serial.println(predicted);
55+
delay(1000);
56+
}
57+
58+
59+
/**
60+
* Load model from SD
61+
*/
62+
void loadModel() {
63+
File file = SD.open("/sine.bin", FILE_READ);
64+
size_t modelSize = file.size();
65+
66+
Serial.print("Found model on filesystem of size ");
67+
Serial.println(modelSize);
68+
69+
// allocate memory
70+
loadedModel = (uint8_t*) malloc(modelSize);
71+
72+
// copy data from file
73+
for (size_t i = 0; i < modelSize; i++)
74+
loadedModel[i] = file.read();
75+
76+
file.close();
77+
}

examples/SineFromSDExample/sine.bin

2.16 KB
Binary file not shown.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import math
2+
import numpy as np
3+
import tensorflow as tf
4+
from tensorflow.keras import layers
5+
6+
7+
def get_model():
8+
SAMPLES = 1000
9+
np.random.seed(1337)
10+
x_values = np.random.uniform(low=0, high=2*math.pi, size=SAMPLES)
11+
# shuffle and add noise
12+
np.random.shuffle(x_values)
13+
y_values = np.sin(x_values)
14+
y_values += 0.1 * np.random.randn(*y_values.shape)
15+
16+
# split into train, validation, test
17+
TRAIN_SPLIT = int(0.6 * SAMPLES)
18+
TEST_SPLIT = int(0.2 * SAMPLES + TRAIN_SPLIT)
19+
x_train, x_test, x_validate = np.split(x_values, [TRAIN_SPLIT, TEST_SPLIT])
20+
y_train, y_test, y_validate = np.split(y_values, [TRAIN_SPLIT, TEST_SPLIT])
21+
22+
# create a NN with 2 layers of 16 neurons
23+
model = tf.keras.Sequential()
24+
model.add(layers.Dense(8, activation='relu', input_shape=(1,)))
25+
model.add(layers.Dense(16, activation='relu'))
26+
model.add(layers.Dense(1))
27+
model.compile(optimizer='rmsprop', loss='mse', metrics=['mae'])
28+
model.fit(x_train, y_train, epochs=100, batch_size=16,
29+
validation_data=(x_validate, y_validate))
30+
return model
31+
32+
33+
if __name__ == '__main__':
34+
model = get_model()
35+
36+
# save to binary file
37+
with open('sine.bin', 'wb') as file:
38+
converter = tf.lite.TFLiteConverter.from_keras_model(model)
39+
tflite_model = converter.convert()
40+
file.write(tflite_model)
41+
42+
# read back binary file
43+
with open('sine.bin', 'rb') as file:
44+
print(['%x' % byte for byte in file.read()])

examples/SineFromSPIFFSExample/SineFromSPIFFSExample.ino renamed to examples/SineFromSpiffsExampleReadWrite/SineFromSPIFFSExampleReadWrite.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void storeModel() {
5858
* Load model from SPIFFS
5959
*/
6060
void loadModel() {
61-
File file = SPIFFS.open("/sine.bin");
61+
File file = SPIFFS.open("/sine.bin", "rb");
6262
size_t modelSize = file.size();
6363

6464
Serial.print("Found model on filesystem of size ");

0 commit comments

Comments
 (0)