All right so in this lesson the goal is to make predictions based on what it is that we've been drawing
on our canvas.
So we're now getting to the point where we're moving away from the data processing and getting to the
exciting part which is using the process data to make predictions using the tenth floor light model
that we've trained.
And that means all we need to do is create a tensor and then make predictions on our data.
So how do we create a tensor.
Do you remember if we look at this page on the documentation then we can scroll down and we get a little
code snippet here and what we see is that creating a tensor is very very simple.
We just use the tensor flow module so TAF put a doctorate and then called us tensor method and inside
the parentheses we supply our data and this data here is in the form of an array and we've got two ways
that we can create a tensor either using the square bracket notation like so or providing a shape of
the tens of first and then providing the data in form of an array like so but also providing the shape
when we tested our tensor flow J.S. model in the very beginning of this module just after converting
it to tensor flow jazz we use the double square bracket notation.
So at the very top of our processing jazz file we had some sample data and we had the double square
brackets and then we used that to create our tensor.
So we're gonna stick with this notation but what we're going to do next is we're gonna use the pixel
values that we're grabbing from the canvas instead of the sample data.
So let's create our tensor down here and I want to stored a constant called Capital X to go with the
convention for naming feature vectors and I'm going to use tensor flow with adult and then tensor and
in the parentheses going to supply the pixel values within a pair of square brackets.
That's because we want our tensor to be two dimensional.
Right.
So I've got two pairs of square brackets the one inside pixel values and then one just outside of it
like so.
So now that we have our tensor why don't we log the shape and the data type for this tensor for let's
add two lock statements and the first one is gonna read shape of tensor and that's going to be X dot
shape which is a method from tensor flow jars.
And we're also going to log the data type just so we see what kind of data is actually stored inside
our tensor.
Now if you remember we're hoping that it's gonna be a floating point number but we should verify this
in the logs like so.
All right.
So now that we have our two log statements and we are logging this property here the shape of property
of our tensor and the data type property of our tensor let's save and refresh and head back into our
Web site which should be running on your local host server.
So when I refresh this and I'm going to have my logs here up on the right hand side and when I draw
something in here and then just click check and so now I get a lot of logs but I don't actually see
the log statements that I've just added so I've saved this file.
This is saved but I still don't see it.
So you might have to once again clear browsing data just to be sure that that's not the reason why I
refresh again draw something in here.
Click check answer then I see my two log statements here.
So this is the shape that I'm expecting to see one by seven hundred and eighty four and my tensor is
indeed of data type float 32.
Fantastic.
So now we can use our tensor to make a prediction.
We've actually already seen how to make predictions at the very top of this file.
So the line that I'm talking about is this one right here.
Model don't predict and then the tensor in the parentheses.
That's how we made a prediction based on our sample data at this point.
What I'd like you to do is try and make a prediction based on the data coming from the canvas as a challenge.
What I'd like you to do is reuse this model here and make the prediction inside are predict image method.
I'll give you a few seconds to pause the video before I show you the solution.
All right.
Ready.
So we've loaded our model inside the load model function but we can't use this model inside our predict
image function because of the scope of this variable.
So at the moment it has a local scope it only exists inside load model.
If we wanted to access the model variable inside predict image we would only be able to do that if our
model had a global scope.
So what we need to do is change this variable scope and we can do that simply by defining or declaring
our variable appear.
So VaR model and instead of creating the model down here on line 7 I'm instead assigning it a value
at this point.
So I've created it up here and it's getting the value inside Load model and that promotes it to a global
variable allowing me to use it down here to make my prediction.
So now I can see model not predict and I can supply my capital X feature vector as an input.
I'm actually going to store the result of this prediction in a constant called result.
So I'll set result equal to model not predict and then what I'll do is I'm going to call it result dot
print in order to show what the result is in the console and that completes the challenge.
Let's say the changes come back into chrome and see this works.
So I'm going to refresh.
I'm going to draw something in here.
Click check answer and there at the very bottom.
I should see a log output which gives the prediction of what I drew.
So I draw three.
Let's try again.
So it seems to be working which is great news for us right.
Let's try a few more numbers.
Let's try a 1 Let's try a 4.
Like so let's try a 4 maybe drawn like so that's not bad.
That's pretty encouraging right.
So it seems to be correctly interpreting what it is that we're drawing into our canvas.
Now do you remember the cleanup code that we added with open C.V. at the bottom of our predict image
function.
We had these lines here that deleted all of our open CV objects.
Let's investigate if there's something similar that we have to do with tensor flow light just below
where we're printing our results.
I'm going to add a log statement and this one is going to simply log P.F. dot memory and that's it.
So going to save my changes and then what I'll do is I'll coming out a few of the things that are clogging
up my console someone coming out this line right here where we're logging our scaled array and and then
coming out the pixel values right here.
And I'm comin out these two lines right here.
Now if you're wondering how I'm doing this so quickly it's because I'm using a keyboard shortcut I'm
passing command and then the forward slash.
But if you're working on Windows it'll be the control key and the forward slash on your keyboard.
All right.
So I'm going to save my changes head back into him and refresh.
Now let's see what happens as we are using this Web site and predicting our answer over and over again.
So I want to write a little two here click check and then here's the log coming from TFT memory at the
moment.
I've got 13 tenses and I've got this many bytes in my memory.
Let's try another number.
Quick check answer again and what we see is that I've got two more tenses and the number of bytes has
increased so I'm using more and more memory as I'm checking my answer again and again and hitting that
predict image function in my code.
So that's a problem actually we are bleeding memory as the site is being used more and more and this
suggests that we should manage our memory just like we did with open CV.
If we take a look at this documentation here on tensor flow and we scroll down a little bit you've actually
got this section here on memory and here what it says is that we must manage our memory explicitly and
we can do that by calling this disposed method on our tenses.
In other words calling dispose will free up our memory.
Looking back at our code let's investigate how many tenses we've actually got.
We've got this one right here which we've just created but result actually is also a tensor.
So one of the things we should do is maybe we're we're adding our cleanup code at the end is called
dispose on capital X which is our tensor holding onto our features and we should also call dispose on
the other tensor that we've got like result but this isn't the only place that we've got tense it's
right.
We also have tenses up here namely this one right here and this one right here.
Now given that this is sample data we are actually not going to call this pose on these tenses.
Instead what we're gonna do is we're just gonna delete this code here and we're just going to be left
with loading the model inside the load model function.
So that solves that problem.
Let's save the changes in processing got Jesus and head back into chrome and refresh.
Now let's check what happens as we draw on canvas and check the answers so the first time I've got 11
tenses and I've got this many bytes in memory.
The second time round I still have 11 tenses and I'll have the same number of bytes in memory.
So this is really really promising news right.
We're no longer bleeding memory and we're using tensor flow Jesus as intended.
In other words we're being good citizens and we're cleaning up after ourselves.
So what's left to do now.
Well we still have to write the logic for checking our equation.
Right.
Three plus two is not equal to one as far as I know.
So let's head into this code and make sure we add the logic to actually check that what's being drawn
into the canvas is correct or not.
And that's exactly what we're going to be doing in the next lesson.
But before we finish off this lesson let's do a little bit more cleaning up.
Let's stop creating a new canvas every time we push the button and also let's clean up what's going
on in our console here on the right.
It's getting very very busy and starting to be a little bit distracting.
So what we're going to do is we're simply going to comment out all the long statements that we've previously
added.
And we're also going to delete the testing code that we had at the bottom in order to sort of visualize
what we're doing with the data pre processing.
So I'm just going to comment everything out both in the processing not J.S. and inside the drawing not
J.S. when a comment on all the log statements I encourage you to do the same so that in the next lesson
we can look at the console and just see very very few log statements in there and focus on what's important
and relevant for that lesson.
All right.
I think that's all of them.
Let me just save the changes here and both of these files and when I refresh in hand just make sure
that up looks like I missed this one and this one or did I.
Let me just go to more tools.
Clear the browsing data.
I'm not really sure why I have to do this so often and refresh again and see if I've got a busy console
or a clear console.
So now all I'm seeing here are my predictions from my tensor.
So this is all I want to be left with at this point.
I just want to be left with the predictions from printing out the result and that's it.
So I'll see you in the next lesson where we're gonna be using our prediction and writing our gaming
logic.