View All Posts
Want to keep up to date with the latest posts and videos? Subscribe to the newsletter
HELP SUPPORT MY WORK: If you're feeling flush then please stop by Patreon Or you can make a one off donation via ko-fi

Learn how to train a simple TensorFlow Lite model and run it on the ESP32 using PlatformIO! With clear instructions and a helpful video, this tutorial will have your project up and running in no time.

Related Content

[0:02] Hey everyone. In this video, we’re going to get
[0:05] TensorFlow Lite up and running on the esp32 using Platform io
[0:11] First off let’s train up a very simple model.
[0:16] I’ve created a Jupyter notebook here.
[0:20] We import TensorFlow.
[0:22] and then we’ll create a training data set
[0:26] This code will generate a data set
[0:30] that has two input numbers and a label that is one or zero
[0:34] depending on the expected result
[0:36] We can now create a very simple model
[0:41] and compile and train it
[0:45] as you can see it doesn’t take very long
[0:47] to get very good accuracy
[0:53] and now our model is trained we can test it out with some simple values
[0:58] We’ll feed in some numbers and we can see the predictions here
[1:04] so it seems to be able to do the job pretty well
[1:07] Now we have a trained model we need
[1:11] to export it for use with TFLite
[1:15] This code will convert the model into something that is optimized for running
[1:19] on an embedded device.
[1:21] We need to give the code some examples of what our inputs will look like
[1:26] so i have a another Python generator here that will just generate
[1:30] random numbers to feed into the converter
[1:34] so that it can quantize the model effectively
[1:40] So that’s our model converted to TFLite
[1:43] We can now convert this model to C code by using xxd.
[1:48] So we’ll jump into the terminal and we’ll run the xxd command
[1:55] so we run xxd
[1:56] we pass in our converted model and we send the output
[2:00] to the c file that we want our model to be in
[2:03] So let’s run that
[2:04] and now we can see the contents of what it created
[2:11] Here’s our model converted into some data that we can now use in
[2:17] TensorFlow Lite
[2:19] I’ve created a simple project and
[2:23] I’ve copied in the tfmicro code
[2:26] into the lib folder there’s a link in the video description
[2:31] to instructions on how to obtain the tfmicro source code
[2:36] We’ve also got our model data that we exported
[2:41] I’ve added a header file to expose the variable from that file
[2:46] and I’ve also created a wrapper around the TFLite code
[2:50] So the first thing we need to do
[2:53] is create a model from the exported data we then need to create an OP resolver
[3:01] and add the operations that our model uses
[3:05] you can just specify all operations but it’s better to actually work out
[3:10] which ones your model needs and just add them
[3:13] We then need to allocate some working space
[3:17] for the TFLite interpreter to run
[3:19] and then create the actual interpreter
[3:22] once we’ve got the interpreter we can ask it to allocate space for the
[3:27] tenses it needs and once we’ve done that we can get hold
[3:31] of the input and the output tensor
[3:34] I’ve added a couple of helper functions here
[3:36] so we can get hold of the input buffer
[3:39] and we can run a prediction and return the result
[3:43] Our main file is very simple
[3:46] In the setup, we create our neural network class
[3:50] and then in the loop, I just create two random numbers
[3:53] I put those into the input buffer of the neural network
[3:57] Then I run the prediction and get the result back.
[4:00] then we just output the values
[4:02] So let’s try running this and see how well it works
[4:11] As expected it works pretty well.
[4:15] You can see that when the second
[4:17] number is smaller than the first number it predicts false and when the second
[4:21] number is larger than the first number it predicts true
[4:24] Obviously, this is a very simple model you can train models up to do all sorts of things
[4:30] But that’s it for this video I hope you found it useful
[4:34] and if you did please hit the subscribe button
[4:37] All the codes in GitHub - the link is in the description.
[4:41] So thanks for watching!
[4:43] and I’ll see you in the next video

HELP SUPPORT MY WORK: If you're feeling flush then please stop by Patreon Or you can make a one off donation via ko-fi
Want to keep up to date with the latest posts and videos? Subscribe to the newsletter
Blog Logo

Chris Greening

> Image


A collection of slightly mad projects, instructive/educational videos, and generally interesting stuff. Building projects around the Arduino and ESP32 platforms - we'll be exploring AI, Computer Vision, Audio, 3D Printing - it may get a bit eclectic...

View All Posts