In this lesson we're gonna be learning how to make predictions on individual images using Charisse after
our model has been trained.
I'll add a markdown cell here to create a subsection for us.
And now what I'll do is I'll quickly pull up the documentation from Caris to see how it is that we can
make a prediction.
So scrolling down on this page here the model functional API we had our compile method we had our fit
method and then down here we have evaluate which will we'll use later on and here predict this seems
what we're looking for right.
This method will generate output predictions for the input samples.
And if you give it a lot of input samples then it will do the computation in batches as an output.
This method will give us a name pi array of predictions.
So let's try this out.
Let's try and predict a single image.
I want to use my validation data set him X Val and I'll quickly remind ourselves of the shape of the
validation data set.
It had 10000 images and each image had three thousand seventy two values in it that corresponded to
the individual pixels.
So that means if I wanted to pull out the very first image in the validation data set then I could do
so with square brackets and then zero.
This here will give us the very first image all right.
So now that we've got a single image let's take a look at what the shape of this image is.
And here you can see that this is actually flattened right.
It's a one dimensional array.
If we want to make this a two dimensional right then we could use num PI's capabilities and use N.P.
dot expand after school.
DRM is four dimensions.
So here we could supply our X Vale square brackets zero and then we can say it should add another dimension
here with access equals zero shift tab on my keyboard and bring up the quick documentation then I can
see that axis is an integer and it's the position in the expanded axis where the new axis is placed.
I want to store this whole thing in a variable called test and I'm very quickly going to take a look
at what the shape of test now is.
And here you can see that all we've done is add an extra dimension here so that we can now feed this
into our predict method.
Why is that.
Well it's because it actually usually expects more than one prediction.
It will want to make predictions on a number of samples here's how we would use the predict method.
I want to pick model number to model on a score to predict.
And if I wanted to make a predict on my test image.
So that's our argument.
Let's take a look at what the output is here.
What we get here are actually our probabilities.
All of these should sum to 1.
So if I put that sum afterwards then I can prove this to you.
So these here are the probabilities for each of the different categories.
Each of the different classes for this image the reason we've got these nice probabilities is because
we've used soft Max in our output layer for our activation function right now.
I think this is some really ugly formatting and I think we could do a little better than this and the
way we're going to change the way these numbers are formatted in our Jupiter notebook is to change the
print options for num pi.
So with MP dot set on a scope print options we can actually say how precise how many decimal points
we want to put a notebook to print out so we can do that by setting the precision.
And if I said that 2 3 and hit shift enter on this and hit shift enter again on my prediction then you
can see that I'm only printing the probability up to three decimal points now.
What if we wanted to predict more than one image.
Well in this case we use our model model number two and then as an argument to the predict method we
can actually supply the entire evaluation dataset.
In this case what we'd get is an enormous array with all the predictions.
Let me show you what I mean.
If I had shift enter on the cell then we get something like this.
These probabilities are in scientific notation which is why you see this e afterwards.
But if I take a look at the shape of this array then what you'll see is I get 10 predictions or 10 probabilities
for each and every single of the 10000 images in our valuation dataset.
Now what if we're just interested in the predicted class.
What if we're just interested in the largest number of these 10.
Well of course there's a handy method from Caris once again.
So model underscored to dot predict underscore classes and then test will give us the predicted class.
So in this case it will predict class number six.
Now of course the question is did it get it right.
The place we would look for that of course is why on the score Val.
So the labels for evaluation valuation dataset y underscore Val square bracket 0 will give us the actual
class the actual category that this belongs to.
In this case our model predicted the correct class for the very first image in the validation data set.
Now as a challenge can you write a for loop where you print out the actual value and the predicted value
for the first 10 images in the validation dataset.
You will get bonus points if you format this nicely and use an F string.
I'll give you a few seconds to pause the video and give this a go.
Ready.
Here's the solution.
It's gonna be four and then let's say number in range 10.
And here what I'll do is I'll say test on a score.
Image is equal to number pi.
Expand dimensions parentheses x on a score Val square brackets No.
So as I'm iterating through my loop I'm going to pull out the corresponding image from my validation
data set.
Put a comma C axis is equal to zero.
Then I'll say predicted underscore Val is equal to model on a score to don't predict classes.
Test image.
And since the output from predict classes is an array I'm going to pick out the first element here with
square brackets zero and now I'll write my print statement using an F string or I'll see actual value.
Curly braces y on the score bough square brackets No.
And since this is also an array I'll add another pair of square brackets here put a 0 c versus predicted.
Curly braces predicted underscore Val.
There we go.
Let's see if I got this right.
Here we are and what we should see is that our model got approximately 50 percent of the predictions
correct.
We've already seen that the first image in the valuation data set was predicted correctly.
But the second one I got wrong.
The third one to get right.
Fourth one I got wrong.
Fifth one.
Wrong again and so on.
So that's how you would use the predict method and the predict class's method from Caris.
You can use these methods on individual items or you can use these methods on entire arrays in the next
lesson.
We're going to be evaluating our favorite model and a bit more detail.
We're gonna be judging it based on its accuracy its precision its recall and its F school for all of
that and more.
I'll see you in the next lesson.
Take care.