Esp32 tflite dataset neural network

Hello! I am trying to train a neural network model and convert using tflite (TensorFlow), but my archive.cc generated there is errors when I compile the code main.cpp.

“Null(s) character(s) ignored [Ln,Col]”
“Stray ‘\377’ in program”

The code is:

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras import Input
from tensorflow.data import Dataset
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder, MinMaxScaler, StandardScaler
from sklearn import datasets
import numpy as np
import os
import itertools

data = datasets.load_iris()

X = data['data']
Y = data['target']

# use and the scaled input the encoded output
enc = OneHotEncoder()
Y_enc = enc.fit_transform(Y[:, np.newaxis]).toarray()
Y = Y_enc

scaler = MinMaxScaler()
X_scaled = scaler.fit_transform(X)
X = X_scaled

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.3, random_state=0)

num_features = X.shape[1]
num_classes = Y.shape[1]

batch_size = 1
epochs = 100

model = Sequential([
    Input(shape=(4)),   
    Dense(5, activation='relu'),
    Dense(3, activation='softmax')
])

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

history = model.fit(X_train, Y_train,
                    batch_size=batch_size,
                    epochs=epochs,
                    verbose=1,
                    )

score_train = model.evaluate(X_train, Y_train, verbose=0)
print('Train loss:', score_train[0])
print('Train accuracy:', score_train[1])


converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_quant_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_quant_model)

You’d need to upload the whole PlatformIO project that throws this error so we can look at it. The Python code doesn’t show the source file that throws errors.

ok! I don’t know how to import here, but I put on GitHub: https://github.com/Victorio1209/Iris_nn_to_esp32/tree/main

The model_data.cc is a mess. There’s NUL (0x00) charcaters after each character.

The compiler can’t work with that. As you can see in the blue taskbar of VSCode, the encoding of the .cc file was detected as UTF-16 LE (little endian)

So for the compiler to be able to work with it, just click on it and “save with encoding” → UTF-8.

grafik

Doing that makes the compile stage go through.

Compiling .pio\build\esp32dev\src\model_data.cc.o
Linking .pio\build\esp32dev\firmware.elf

But linking shortly fails there after because you didn’t define the, for the Arduino framework necessary, loop() functions. Also the the definition of

int main() {
  setup();
}

is really, really, really wrong. You don’t want to try and redefine the main() function of the Arduino core where it gets the chance to initialize the ESP’s subsystems. That’s an instant die-on-boot right there.

Deleting the main() function and adding

void loop() {
}

instead, you get…

Linking .pio\build\esp32dev\firmware.elf
Retrieving maximum program size .pio\build\esp32dev\firmware.elf
Checking size .pio\build\esp32dev\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=         ]  10.3% (used 33612 bytes from 327680 bytes)  
Flash: [=====     ]  47.4% (used 621505 bytes from 1310720 bytes)
Building .pio\build\esp32dev\firmware.bin
esptool.py v4.5.1
Creating esp32 image...
Merged 2 ELF sections
Successfully created esp32 image.
================= [SUCCESS] Took 35.11 seconds =================

You are incredible! I wasted a lot of time to trying a solution for this. Now is compiled! Thank you!

This is the error when I upload the main in ESP32: [Guru Meditation Error: Core 0 panic’ed (LoadProhibited). Exception was unhandled.]

I am open the test dataset of Iris.

You can find out at which line it crashed with a proper decoding using the documented decoder and debug build options.

1 Like

Hello! Now I trying a different code to implement a neural network, and I need to compare metrics in ESP32 to Python Code, but the results of the serial monitor are just null characters.

You have Serial.begin(115200); in the code but not monitor_speed = 115200 in the platformio.ini… Please see documentation.

1 Like

Nice! I created a new project and forget that, but now I don’t have the expectation results.

rv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:1184
load:0x40078000,len:13192
load:0x40080400,len:3028
entry 0x400805e4
Only 1 subgraph is currently supported.

Only 1 subgraph is currently supported.

Guru Meditation Error: Core 1 panic’ed (LoadProhibited). Exception was unhandled.

Core 1 register dump:
PC : 0x400dcdcc PS : 0x00060230 A0 : 0x800dd2b6 A1 : 0x3ffb2170
A2 : 0x00000000 A3 : 0x3ffb45a8 A4 : 0x3ffb8c90 A5 : 0x00000014
A6 : 0x00000008 A7 : 0xb6fc16fd A8 : 0x800dcdc9 A9 : 0x3ffb2120
A10 : 0x00000000 A11 : 0x3f400aac A12 : 0xb6fc16fd A13 : 0x3ffbdb84
A14 : 0x000016a8 A15 : 0x000000ff SAR : 0x00000019 EXCCAUSE: 0x0000001c
EXCVADDR: 0x00000000 LBEG : 0x400873a5 LEND : 0x400873b5 LCOUNT : 0xffffffff

Backtrace: 0x400dcdc9:0x3ffb2170 0x400dd2b3:0x3ffb21b0 0x400ddfc3:0x3ffb21d0 0x400d228a:0x3ffb2210 0x400d2317:0x3ffb2260
0x400e06ca:0x3ffb2290

ELF file SHA256: 090f1056c117a66c

Rebooting…
ets Jun 8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:1184
load:0x40078000,len:13192
load:0x40080400,len:3028
entry 0x400805e4
Only 1 subgraph is currently supported.

Only 1 subgraph is currently supported.

Guru Meditation Error: Core 1 panic’ed (LoadProhibited). Exception was unhandled.

Core 1 register dump:
PC : 0x400dcdcc PS : 0x00060230 A0 : 0x800dd2b6 A1 : 0x3ffb2170
A2 : 0x00000000 A3 : 0x3ffb45a8 A4 : 0x3ffb8c90 A5 : 0x00000014
A6 : 0x00000008 A7 : 0x5b509671 A8 : 0x800dcdc9 A9 : 0x3ffb2120
A10 : 0x00000000 A11 : 0x3f400aac A12 : 0x5b509671 A13 : 0x3ffbdb84
A14 : 0x000016a8 A15 : 0x000000ff SAR : 0x00000019 EXCCAUSE: 0x0000001c
EXCVADDR: 0x00000000 LBEG : 0x400873a5 LEND : 0x400873b5 LCOUNT : 0xffffffff

Backtrace: 0x400dcdc9:0x3ffb2170 0x400dd2b3:0x3ffb21b0 0x400ddfc3:0x3ffb21d0 0x400d228a:0x3ffb2210 0x400d2317:0x3ffb2260
0x400e06ca:0x3ffb2290

ELF file SHA256: 090f1056c117a66c

Rebooting…
ets Jun 8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,le

Yep – that’s what the version of tensor flow lite / micro library that you put into the project supports only.

grafik

Either try and follow this

though I don’t know if it applies.

On the other side, when I look at the current version of the library and that function in

I don’t see that limitation of subgraph->size() == 1 anymore. So you might just need to update your library.

1 Like

I tried to upgrade the tflite, but it isn’t a solution for me.