Now we've seen what an image looks like intensive form.
Let's build this little function that's gonna do these steps here.
So what we might do is just copy them so we can see them down below.
Am I gonna just write a little sell here.
Now we've seen what an image looks like as a tensor let's make a function to pre process them already.
And so what we might do is define a variable here image size to 24 and you might be wondering like I
always do when I see things like this that aren't explained what is 224 whether Look get that will we're
going to see that in a later video when we pick out a machine learning model that we can use.
And so a lot of machine learning is lining up the size of your inputs or the shape of your inputs in
our case images with the model that you're using.
Again we'll cover this later on but essentially we're making sure that our images that we feed into
a machine learning model are of this shape so to 24 high to 24 wide because that is what the machine
learning model that we're going to use which trained on images in that same shape.
Remember how it's important to feed data in the same format and shape to your machine learning model
as it was trained on when you're trying to make predictions.
But we'll see that in practice later on.
But let's write this function.
So create a function for pre processing images.
So I call this nice and simple process image.
It's going to take an image path as input and
little doctoring could be takes an image file path and turns the image into a tensor something simple
like that we could make it a little bit more intuitive later on but to begin with that's enough to explain
it for ourselves.
So we want to read in an image file and to do that we'll save it to a variable called image.
And if we read through loading images that you can do this for other types of data to write there's
text data there.
This pan this data frame num pi CSA but reading through there we see that we can go t after Io for input
output read file.
There we go.
What's this say.
Reads and outputs the entire contents of the input file name.
Well that makes sense.
Returns a tensor of type String.
Beautiful.
Let's do image path but don't take my word for it.
What we'll do is and I want you to adopt this practice too.
If you ever see a function that you're not entirely sure of what it does.
Rewrite it line by line so let's check this out.
We'll go tensor equals t after Io read read file and then we need an image path.
So what do file names.
I'm 26 so we'll get file names 26 and what's that look like
all.
So it's shape of a string and then a whole bunch of characters I don't understand but that's an alright
thing because the computer understands them better than I do.
So what it's done is it just taken the file name and turned the contents into this long tensor.
Wonderful.
So the next step.
Use tens flow to read the file save it to image.
Yep.
To now image a j peg into tenses.
Okay.
So let's do that.
Turn the j peg image into numerical tensor with three color channels Red Rain blue
and again checking out the loading images documentation so we're reusing this variable here.
Now we're going to reset it to B T after image dot decode see how we've got some options here we go
to PJ which is the type of image file we're gonna use J peg because our images are in j peg image and
then we pass it the variable channels the outer channels indicates the desired number of coloured channels
for the decoded image in our case we want three for red green on blue beautiful.
Now again if we wanted to check out exactly what this is doing let's take tensor and go after the image
that decode j peg we'll take tensor and then we'll go channels equals three
Oh look at that you've got exactly the same sort of data as that we have up here wonderful right now
to save in the interest of time we're going to write a couple more lines in this function but we won't
go through these similar steps that's what I want I want you to have some practice doing.
If you do see a function and you're curious to what it does try writing it out line by line and see
what happens.
So you're just gonna have to trust me with what these lines do.
So convert the color channel values so this is called normalization I'll write the code first and then
tell you about it from 0 to 255 to 0 2 1 values.
So we want to go image equals t after image convert image D type image TAF float 32.
Now what's the doctoring say here.
Convert image to day type scaling its values if needed.
Images that are represented using floating point values are expected to have values in the range 0 1.
Wonderful.
Now you might be thinking how how do you know this.
Daniel how do you know that you have to convert it from 0 to 255 to between 0 1 Well actually I lied.
I said we weren't going to look at that but this is a good one to investigate
tensor just so you can see we've got values there between 0 and 255 and now I'll run this Oh no I don't
actually want that messing myself up here rerun this rerun this so just when I demonstrate what this
is doing so image convert.
So basically all it's doing is it converts our values.
It scales them instead of being between 0 and 255.
They now are between 0 and 1 with the same scale float 32
TFT float 32
wonderful.
So we've got the same shape.
But now instead of being between zero activity five between zero one and now this type of process makes
computing on these more efficient.
That's the most you need to know it's called normalization.
And again this is just straight from the loading images documentation.
Now what do we have to do.
Actually I don't think we had a step here for that we might have to put in for normalize our image convert
color channel values from 0 to 255 to 0 to 1
5 6.
OK.
Now finally we can resize the image now by now.
I believe you trust me and what this line is going to do so we not not going to type it out.
I'm going to leave that to you two twenty four to twenty four.
So basically a square exactly a square actually
image dot resize resize as images to size using the specified method.
OK.
And now we want to resize image and the size is going to be height width.
Now you see why we define this here.
Maybe that if we wrote this function a bit better we could input image size as image size
that's probably a little bit better.
Wonderful.
And now you'll get this kind of practice as you start to write your own functions.
The first time you write a function are probably pretty bad pretty terrible.
But then as you start to work out what your workflow is you'll start to understand how to write a better
function.
Now we can return the image.
So what has this done.
Has it done exactly what we just said.
It takes an image path and an image size.
We can define that here.
It reads in the image file from the path named beautiful it turns the j peg into numerical tensor.
So we're just converting it to a tensor with three color channels red green and blue because that's
the convention for most deplaning image models.
They want red.
I said read that but that's a bad thing.
Reading is a great thing we then converted the color channels from 0 to 255 to 0 to 1 values and we
finally resize the image to our desired value.
Now we've written this in a function.
So the beautiful thing is that we can use this later on.
This is what I used to do right.
I used to run through these lines of code every time I wanted to process an image.
Terrible workflow.
So we can actually remove these remember if you want to check out what the function is actually doing.
Break it down line by line takes an image path as input and then see what the rest of it does.
Now I think that's enough for this video.
We've got our function created ready to pre process some images what we might do now is again if we
were to read through the loading data intensive low.
I think it's this one yes.
Back through here there's a little section in here that tends to flow likes to see things in the form
of batches.
So we'll figure what batches are in the next video.