Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ShawnHymel
Created March 18, 2020 17:42
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 17 You must be signed in to fork a gist
  • Save ShawnHymel/79237fe6aee5a3653c497d879f746c0c to your computer and use it in GitHub Desktop.
Save ShawnHymel/79237fe6aee5a3653c497d879f746c0c to your computer and use it in GitHub Desktop.
TensorFlow Lite Sinewave Regression Training and Conversion
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@bonadio
Copy link

bonadio commented Jun 8, 2020

For the new tensorflow in colab 2.2 you need to increase the number of samples to get a good result, I tried with 5000 and worked

@ShawnHymel
Copy link
Author

For the new tensorflow in colab 2.2 you need to increase the number of samples to get a good result, I tried with 5000 and worked

@bonadio good to know, thank you! I thought it was a limitation with the model itself. I'll up the number of samples.

@ron333
Copy link

ron333 commented Aug 13, 2020

Using TensorFlow 2.3.0 and Keras 2.4.0
So, I changed the code near the end as follows:
# Convert Keras model to a tflite model
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()

with tf.io.gfile.GFile('model.tflite', 'wb') as f:
f.write(tflite_model)

@ShawnHymel
Copy link
Author

I found that shuffling the data before splitting it into the different sets improved the performance. Makes sense cause otherwise you're limiting your different sets to sequential points of a small section of the sinusoid, as opposed to points across the entire sinusoid.

from sklearn.utils import shuffle
x_values, y_values = shuffle(x_values, y_values)

@Martin-Biomed Good find, thanks! I thought I did provide random data with np.random.uniform(). You found that shuffling it a second time helped training improved accuracy?

@Martin-Biomed
Copy link

My bad Shawn, I tried altering several parameters, including the shuffling. I assumed that the shuffling did the trick, but I think it was most likely the increased learning rate value I used. The one I originally used from the tutorial hit a plateau of around 0.2 in the loss. With a higher learning rate, I could get past the local minima (which was at around 0.2).

I better delete the previous comment because it might confuse people.

@ShawnHymel
Copy link
Author

@Martin-Biomed No worries! Thanks for letting me know :)

@Martin-Biomed
Copy link

Hi Shawn,

Out of curiosity, have you come across any tutorial that teaches people how to use multiple layers of neurons in TinyML, or even more advanced ML methods such as LSTM in TinyML?

Any help would be great, cause I haven't had a lot of luck with finding this information for the Arduino, and even in Micropython, TF Lite LSTM information is rare.

@ShawnHymel
Copy link
Author

@Martin-Biomed Using multiple layers of supported operations in TinyML with TensorFlow should be trivial if you know how to make a 1 layer network. The example above demonstrates a DNN with 3 layers (16 fully-connected nodes, 16 fully-connected nodes, 1 fully-connected node as output).

I have not seen any TinyML frameworks that allow for RNNs yet (including LSTM). I have to imagine that is on the roadmap, so I hope to see RNN support in the near future.

@michel4731
Copy link

hi. my model is vary big(15.23Mb) and Compression on cube is not good(after the Compression model size:14.81Mb)
please help me for create project NN on the STM32 or ESP32

@suman346
Copy link

I am getting some error by the converter
I can't fix it it's showing it can't concate the str and byte

@ShawnHymel
Copy link
Author

@michel4731 That sounds way too big for the model. I can't imagine a Dense NN with 321 parameters is more than a few kB.

@ShawnHymel
Copy link
Author

@suman346 Where are you seeing this error? Please keep in mind that this example is over 2 years old. Newer versions of TF Lite have changed a lot of things, and I have not tested them recently to see if this example is still relevant.

@ericobropinto
Copy link

I am getting some error by the converter I can't fix it it's showing it can't concate the str and byte

I am also getting a error here.

hex_str = format(val, '04x')

ValueError: Unknown format code 'x' for object of type 'str'

were you able to solve?

@suman346
Copy link

suman346 commented Oct 20, 2021 via email

@ericobropinto
Copy link

Sorry for flooding but something strange is happening here:
I am on windows using the spyder and python 3.8.3 - everytime I try to make the conversion I get this error " hex_str = format(val, '#04x') - ValueError: Unknown format code 'x' for object of type 'str".
However the code works perfectly in colab.
I must say I am no expert and I am using this project as a way to understand how to deploy a ML model into esp32. I don't mind having to go to colab just to make the conversion but I would like to understand why I get this unknown format error.
Can somebody help me?

btw, great example @ShawnHymel !!

@ShawnHymel
Copy link
Author

@ericobropinto it looks like format(val, '#04x') only works when val is an integer. In my original code, the tflite model file was a collection (list, I think) of integers that could be iterated over and converted to hex strings (i.e. using format()). That may have changed in recent versions of TensorFlow Lite (as this example is over 2 years old). I'm not sure what format the tflite models are using now.

@abhi-84
Copy link

abhi-84 commented Nov 16, 2021

What alternative board would be used to implement this example instead of NUCLEO-32 STM32L432KC EVAL BRD since this board is not available online. please suggest

@ShawnHymel
Copy link
Author

@abhi-84 Almost any STM32 ARM Cortex-M3 or M4 should work (setting it up in CubeMX might be a little different, depending on the particular MCU or board). Some Cortex-M0 or M0+ MCUs might work...I just haven't tried them yet.

@whubaichuan
Copy link

@suman346 @ericobropinto @ShawnHymel Some possible solutions for Unknown format code 'x' for object of type 'str".

I have met this problem but solved it by changing it slightly. So be careful about the file and directory.

From

file.write(hex_to_c_array('./modellite.tflite', c_model_name))

to

file.write(hex_to_c_array(tflite_model, c_model_name))

Then it works.

@soumengoroi
Copy link

Create header guard

c_str += '#ifndef ' + var_name.upper() + '_H\n'
c_str += '#define ' + var_name.upper() + '_H\n\n'


-- Here I am facing error
"AttributeError: 'Interpreter' object has no attribute 'upper''

@ShawnHymel
Copy link
Author

@soumengoroi Check the value of var_name being passed into that function. It should be a string (and set by c_model_name as the argument)

@richardhemphill
Copy link

richardhemphill commented Sep 23, 2023

There is a typo in the 3rd code cell. Change versionS to version.
!python --version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment