All right.
So in this lesson we're gonna be using our inception resonant model and making some predictions with
it.
So let's dive straight in so I'll add a markdown cell here.
And this one going to read making predictions.
So the question is how do we make a prediction with our inception model going to the carris documentation.
We can click on model functional API.
And here we will find a predictor method the predict method will require some input data as a num pi
array or a list of an umpire race.
We're only going to predict one image at a time.
So the rest of the arguments we can leave as the defaults and we're not really going to be too concerned
with them.
All right so let's try and make a prediction.
So I'll say Inception model not predict parentheses and then I have to pass in the array.
Now let's go up here see what our array is called.
Call it pick array.
Very creative I know.
So let's go down here and we'll just supply this and we'll see what happens.
We in fact will get an error if we try this.
So when I move this over slightly scroll down and read the error message.
Error when checking input expected the input to have four dimensions but got an array with three dimensions.
This is what the error message is telling us right here.
So it seems like we can't do just this.
And the reason is is that this predict method will require an extra dimension.
But what would be in this extra dimensional what kind of information would be in this extra dimension.
Well imagine that you were looking to predict more than one image then what you would do is you would
have a whole list that you would supply.
So this essentially would be that extra dimension.
So it's not going to just be the height of the pixels the width of the pixels and the colors of the
pixels.
It's also gonna be which image you're actually using.
That's going to be that fourth piece of information.
So the question is how do I get that extra dimension for our picture array.
Well I you use num PI for this.
So up here under our pre processing I'll add another cell and I'll say expanded expanded is equal to
NPR.
Don't expand underscore DIMIA use parentheses and then I'll provide our picture array here and I'll
say axis is equal to zero and then we'll see expanded dot shape.
And this will show us that we now have this extra dimension here.
It's just the one.
But this would allow us to meet that requirement of the predicate method.
So let's try to let's come down here and instead of pick underscore array I'll put an expanded and it
shift enter at this point I'll get a different era.
The arrow reads the expected input is expected to have a shape of 2 9 9 2 9 9 and then 3.
But instead I'll picture had the following shape 2 5 6 by 2 5 6 by 3.
Why is that.
Well coming back to the documentation and going back to our inception resonant version to model I can
see that the default size that this model expects is 2 9 9 by 2 9 9.
Now again it looks like we have to change our input data in order to run this predict method.
So let's get back up here and we've done the first step which was expanded our array.
Now all we need to do is change this resolution of the picture and we can actually do this with our
load image method.
Check it out if I put my curse on here and hit tab then I can bring up the documentation inside the
Google collab notebook.
And one thing that you'll see here is this target on this course size input.
At the moment we've left it blank.
So we've got our original image size but we can also supply a height and a width for our desired dimensions.
So let's try it out.
So I want to put a comma here and I'll say Target underscore size and then parentheses 2 9 9 comma 2
9 9 limit shift into we see that our image has just been scaled up.
It's become a little bit more pixilated but that's fine.
We've reached our target resolution.
We've scaled up our image slightly.
And then what we'll do is refresh this cell just to check its dimensions and indeed it's 2 9 9 by 2
9 9 by 3 because we still have those three color channels.
And then when we expand dimensions we go from three dimensions to four dimensions.
Brilliant.
So let's come down here and try again see if our predict method gives us another era.
Let's try and so in this case we actually get some output and it looks really nonsensical.
Right.
In scientific notation but this here is the raw output for our prediction.
All we need to do now is decode this prediction and carers actually has a very handy method for us going
back up to our import statements.
We can go to the same line where we imported our model Inception resonant version to put a comma there
and say decode underscore predictions.
So let's shift enter on that scroll all the way back down and then let's store this output in a variable
called prediction then we can start a new line and see decode underscore predictions and provide this
prediction as an input.
There we go.
That's it.
Shift enter and see what we get what we see now are the top five predictions we see something called
the class name.
So this is the category name that's internal to the model.
Then we see the class description.
Basically what this means in English.
And in this case it's comic book.
And then we see the probability here.
This is the third line item for this very first prediction.
And here the probability is equal to 1 that this is a comic book even though we know it's an umbrella.
The other predictions all have a probability of zero.
So something is still going wrong.
And I can tell you what it is.
It's the fact that our data isn't formatted in the way that our model expected to be we're actually
missing another step.
So scrolling back up here I'm going to quickly close this.
Put my cursor here at a comma and then I'll add pre process underscore input.
I'm going to import the pre process input function from the caris applications Inception underscore
resonant underscore V2.
Now to that I've done this I can shift enter on the cell scroll down here and under pre processing images
I'm going to say pre processed is equal to pre process underscore input and I provide the expanded num
pi array this will format the data in the way that our model expects it to be formatted in so late shift
enter I'll scroll down here and I want to make my prediction not using our expanded number hiring.
I'm going to make my prediction using our pre processed pie array and if I refresh this now we should
get some more sensible output.
And indeed we do umbrella with a 3 percent probability and then mountain tent with almost zero percent
probability on place two.
So that worked pretty well right.
The important takeaway here is that every single model that you use out of the box expects the data
to be formatted in a particular way.
And what we've seen is that to an extent the documentation helps us.
It will provide some information on this front but also the error messages in the call up notebook will
also give us some more feedback and some more hints as to what to change and what we did wrong.
Perhaps the most difficult thing to discover is what happens when the neural network does its calculations
gives us an output but the output actually doesn't make any sense.
And this is what happened when we forgot to pre process our data and the right way.
So in order that this doesn't happen again I'd like to pose a challenge to you as a challenge.
Can you create a function called format underscore image and a score Inception resonate.
That takes a final name as an argument and then this function will need to load the image and the default
resolution for Inception resonant.
Convert the image to an array and then return to pre processed image for the Inception resonant version
to a model essentially.
Can we summarize all the pre processing steps in a simple function so that we can call this function
over and over again for different file names and try and predict what the classification is for other
images.
I'll give you a few seconds to pause the video before I show you the solution
here we go.
We'll go deaf if the key word for defining a function.
And then we'll say format underscore image underscore.
Inception resonant will open the parentheses and then file name shall be our argument semicolon there
and then inside the function will divide up the work we'll see the picture that's gonna be equal to
load underscore image.
So this is the caris function and it shall load the image based on the file name which is effectively
the relative path and then the target size.
It's gonna be equal to a tuple 29 buying 2 9 9.
Then we're going to save all of this information in an array Pick underscore a are r equal to image
on a score to a score array which is going to take our picture.
But remember we have to expand it by one dimension.
So expanded.
It's gonna be num PI's job MP dot expand on the score dims parentheses pick the score a r comma.
Axis is equal to zero and then pre processed input shall be equal to pre process underscore input parentheses
expanded and finally we can return the pre processed on a score input this value right here.
Now if we want to make this a little bit more succinct we can even take this bit of code here.
Cut it.
Take this one here replace it and delete the rest of this line.
There we go.
That does it.
And this pretty much summarizes all the pre processing steps that we've done in separate cells above.
Now we've got them all in one place and we can easily call this function for the other images.
So let's try this out right now.
I'll store the pre processed input in a variable called data and then I'll say format underscore image
on a score.
Inception resonate and I'll provide a file number to our prediction store in a variable called prediction
which will be equal to Inception underscore model thought predict parentheses data and just so that
we can see what the images which the predictions pertain to.
I'll say display parentheses load on the square image parentheses file underscore 2 and just below that
I'll decode the predictions so decode underscore predictions parentheses prediction.
There we go.
Let's run this and see what happens.
So here we see a typical wedding photograph and we've got this at the seaside but the number one prediction
seems to be groom with 70 percent followed by gown with 11 percent I think rapeseed is a little bit
a weird one but Inception resident seems to detect something about the picture that suggests a sandbar
bar which you often find by the beach which I think is pretty good too how I leave it to you to try
the predictions and a couple of other of these files.
Some of them work better.
Some of them work less well and I'll leave it to you to find out which ones those are.
But I think what would be more interesting to do right now is to go back to the carer's home page and
look at what other models are available because we've got one of them working and for good measure.
I think we should get another one working as well.
In particular I'd be interested to set this as a challenge to you.
I'd like you to load this veggie 19 model and make a prediction on some of the sample images that we've
already uploaded to our collab notebook so pack in our collab notebook.
I'll add another markdown cell and this one is gonna read testing the veggie 19 model.
Now Fiji actually stands for Visual geometry group and this model was created by a couple of researchers
out of Oxford University in the UK and it was also trained on image net hand.
We can try it out with the weights and see how we get on.
What I'd like you to do as a challenge is use this g 19 model from Kara's loaded with the image net
weights and I'd like you to make a prediction on some of these files.
Some of these sample images that we've uploaded to our collab notebook.
Now the way to figure out how to do this is to take a close look at the official documentation.
You're going to have to process the data for this neural network in order to get a prediction that makes
sense.
So have a look at this documentation and see what we've done previously with our inception resonant
model because the steps are going to be very very similar.
There's a few tricks to be aware of to get this right.
I'll see you in a bit.