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