‫Now we've seen what an image looks like, intensive form.
‫Let's build this little function that's going to do the steps here.
‫So what we might do is just copy them so we can see them down below.
‫And I'm going to just write a little cell here.
‫Now, we've seen what an image looks like as a tensor.
‫Let's make a function to pre process them.
‫All righty.
‫And so what we might do is define a variable here image size 224 and you might be wondering.
‫Like I always do when I see things like this that aren't explained.
‫What is 224?
‫Where did I get that?
‫Well, 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 out 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 right this function.
‫So create a function for preprocessing images.
‫So I'm going to call this nice and simple process image.
‫It's going to take an image path as input.
‫And.
‫Little docstring could be tags 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 now you can do this for other types of data too, right?
‫There's text data there.
‫There's pandas, dataframe, numpy, csv.
‫But reading through there, we see that we can go to IO for input, output, read, file.
‫There we go.
‫What's it 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 io read, read, file, and then we need an image path.
‫So we'll do file names.
‫I'm 26, so we'll get file names.
‫26.
‫And what's that look like?
‫Oh.
‫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, tense.
‫Wonderful.
‫So the next step, use TensorFlow to read the file.
‫Save it to image.
‫Yep.
‫Turn our image a jpeg into tenses.
‫Okay, so let's do that.
‫Turn the JPEG 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 tf.
‫Image dot decode.
‫See how we've got some options here.
‫We've got PNG, which is the type of image file.
‫We're going to use JPEG because our images are in JPEG image and then we pass it the variable.
‫Channels.
‫The Atta channels indicates the desired number of colour channels for the decoded image.
‫In our case, we want three, four, red, green and blue.
‫Beautiful.
‫Now, again, if we wanted to check out exactly what this is doing.
‫Let's take tensor and go tf dot image decode.
‫JPEG.
‫We'll take tensor and then we'll go.
‫Channels equals three.
‫Oh, look at that.
‫They've got exactly the same sort of data as that we have up here.
‫Wonderful.
‫All 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 the 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 going to have to trust me with what these lines do.
‫So convert the color channel value.
‫So this is called normalization.
‫I'll write the code first and then tell you about it from 0 to 2, 55 to 0 to one values.
‫So we want to go image equals TF to image convert image d type image tf float 32.
‫Now what's the docstring say here?
‫Convert image to d type scaling its values if needed.
‫Images that are represented using floating point values are expected to have values in the range zero
‫one.
‫Wonderful.
‫Now you might be thinking, how?
‫How do you know this, Daniel?
‫How do you know that you have to convert it from zero to to 55 to between zero and one?
‫Well, actually, I lied.
‫I said we weren't going to look at that, but this is a good one to investigate.
‫Tense up just so you can see, we've got values there between zero and 255 and now.
‫I'll run this.
‫Oh, no, I don't actually want that messing myself up here.
‫Rerun this.
‫So I just want to 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 zero
‫and 255.
‫They now are between zero and one with the same scale.
‫Float 32.
‫Oh.
‫Freefloat 32.
‫Wonderful.
‫So we've got the same shape.
‫But now instead of being between zero and two or 55, they're between zero and 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.
‫Make it five.
‫Six.
‫Okay.
‫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 are not going to type it out.
‫I'm going to leave that to you to 24.
‫To 24.
‫So basically a square.
‫Exactly.
‫A square, actually.
‫Image resize.
‫Resizes images to size using the specified method.
‫And now we want to resize image and the size is going to be height.
‫Wait.
‫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, you'll probably be 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 does 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 name beautiful.
‫It turns the JPEG 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 deep learning image models.
‫They want red.
‫I said read.
‫Not that that's a bad thing.
‫Reading is a great thing.
‫We then converted the colour channels from 0 to 55 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.
‫It 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 process some images.
‫What we might do now is, again, if we were to read through the loading data in TensorFlow, I think
‫it's this one.
‫Yeah, back through here.
‫There's a little section in here that TensorFlow likes to see things in the form of batches.
‫So we'll figure what batches are in the next video.