Skip to content

Commit 678c83c

Browse files
add SineFromSPIFFSExample
1 parent 46adc65 commit 678c83c

File tree

9 files changed

+330
-3
lines changed

9 files changed

+330
-3
lines changed

.DS_Store

6 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 <FS.h>
2+
#include <SPIFFS.h>
3+
#include <EloquentTinyML.h>
4+
#include "sine_model.h"
5+
6+
#define NUMBER_OF_INPUTS 1
7+
#define NUMBER_OF_OUTPUTS 1
8+
// in future projects you may need to tweek this value: it's a trial and error process
9+
#define TENSOR_ARENA_SIZE 2*1024
10+
11+
uint8_t *loadedModel;
12+
Eloquent::TinyML::TfLite<NUMBER_OF_INPUTS, NUMBER_OF_OUTPUTS, TENSOR_ARENA_SIZE> ml;
13+
14+
15+
void setup() {
16+
Serial.begin(115200);
17+
SPIFFS.begin(true);
18+
delay(3000);
19+
20+
storeModel();
21+
loadModel();
22+
23+
if (!ml.begin(loadedModel)) {
24+
Serial.println("Cannot inialize model");
25+
Serial.println(ml.errorMessage());
26+
delay(60000);
27+
}
28+
}
29+
30+
void loop() {
31+
// pick up a random x and predict its sine
32+
float x = 3.14 * random(100) / 100;
33+
float y = sin(x);
34+
float input[1] = { x };
35+
float predicted = ml.predict(input);
36+
37+
Serial.print("sin(");
38+
Serial.print(x);
39+
Serial.print(") = ");
40+
Serial.print(y);
41+
Serial.print("\t predicted: ");
42+
Serial.println(predicted);
43+
delay(1000);
44+
}
45+
46+
/**
47+
* Save model to SPIFFS
48+
*/
49+
void storeModel() {
50+
File file = SPIFFS.open("/sine.bin", "wb");
51+
52+
file.write(sine_model, sine_model_len);
53+
file.close();
54+
}
55+
56+
57+
/**
58+
* Load model from SPIFFS
59+
*/
60+
void loadModel() {
61+
File file = SPIFFS.open("/sine.bin");
62+
size_t modelSize = file.size();
63+
64+
Serial.print("Found model on filesystem of size ");
65+
Serial.print(modelSize);
66+
Serial.print(": it should be ");
67+
Serial.println(sine_model_len);
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/SineFromSPIFFSExample/sine_model.h

Lines changed: 252 additions & 0 deletions
Large diffs are not rendered by default.

src/.DS_Store

0 Bytes
Binary file not shown.

src/EloquentTinyML.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ namespace Eloquent {
5252

5353
// assert model version and runtime version match
5454
if (model->version() != TFLITE_SCHEMA_VERSION) {
55-
Serial.println("Version mismatch"); delay(1000);
5655
failed = true;
5756
error = VERSION_MISMATCH;
5857

@@ -66,9 +65,7 @@ namespace Eloquent {
6665

6766
static tflite::MicroInterpreter interpreter(model, resolver, tensorArena, tensorArenaSize, reporter);
6867

69-
Serial.println("Allocating tensors..."); delay(1000);
7068
if (interpreter.AllocateTensors() != kTfLiteOk) {
71-
Serial.println("Cannot allocate tensors"); delay(1000);
7269
failed = true;
7370
error = CANNOT_ALLOCATE_TENSORS;
7471

src/tensorflow/.DS_Store

0 Bytes
Binary file not shown.

src/tensorflow/lite/.DS_Store

0 Bytes
Binary file not shown.
6 KB
Binary file not shown.

src/tensorflow/lite/kernels/internal/compatibility.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ using int8 = std::int8_t;
8181
using uint8 = std::uint8_t;
8282
using int16 = std::int16_t;
8383
using uint16 = std::uint16_t;
84+
#define int32 foo
8485
using int32 = std::int32_t;
8586
using uint32 = std::uint32_t;
8687

0 commit comments

Comments
 (0)