I'm a friend.
Are you ready to build that convolutional neural network?
We have a long but yet very exciting journey ahead of us.
Let's do this. Let's kick this off.
All right,
so we're going to start with the very essential step
importing the libraries, which will only consist of importing TensorFlow.
And actually the preprocessing module by the Keras library.
So let's do this first quickly and efficiently in a new code cell.
So actually you know how to import TensorFlow.
We start with the import command.
Then we specify the name of the library TensorFlow.
And we add the shortcut tf.
Just like before with the a.
And then I would like to import something else which will allow us to do
the image preprocessing in part one, and which is the image
submodule of the pre-processing module of the Keras library.
And therefore here we're going to start from, well, the Keras library
from which we're going to get access to the pre-processing module
or from which we're going to import the image submodule.
And the reason why we want to import this is because we want to import
a specific class, which is the image data generator.
And I will explain very quickly what this is about.
But this is absolutely compulsory in part one data preprocessing.
You know when pre-processing your images.
So let's import it.
Here we just need to add import and then image data generator.
Okay good.
I will explain very soon what this is about and how we will use it.
All right.
And then you know this other thing that I like doing
just to show you that indeed we are working with TensorFlow 2.0.
I just want to print the version of TensorFlow we're using right now.
And remember to do this we need to call TensorFlow first.
And then after a dot a double underscore and then version
and double underscore again.
And this will you know print in the output.
The version of TensorFlow we're using.
This is just to make sure we're working with TensorFlow 2.0.
However you know depending on
when you run this code you know after I record this tutorial
you might have a different version, but you will definitely get a TensorFlow
two version. Okay.
All right.
So here I just execute the first cell
importing TensorFlow end image preprocessing module like Keras.
And now let's run this to indeed reassure ourselves
that we are working with TensorFlow 2.0, which is
so much better than TensorFlow one.
All right. Good.
So now we can move on to part one data preprocessing
which will be done in two steps.
First, preprocessing the training set.
And second preprocessing the test set.
So let's start with the training set.
And let's create a new code cell.
And now let me explain how we're going to do this.
All right.
So how are we going to preprocess our images.
Well we're actually going to do multiple things.
The first thing we'll do is we will apply some transformations
on all the images of the training set.
The images are the training set only.
We won't apply these same transformations on the test set.
The reason why we want to apply some transformations
on the images of the training set is for only one purpose.
It is to avoid overfitting.
Indeed, if we don't apply these transformations
well, when training our CNN on the training set,
we will get a huge difference between the accuracy on the training set
and the one on the test set, you know, on the evaluation set.
Actually, we will get very high accuracies on the training set,
you know, close to 98% and much lower accuracies on the test set.
And that is called overfitting.
And that's something we absolutely need to avoid.
Anyway, you know, whether you're
working on a classic data set or working for computer vision and for computer
vision, well, the way to avoid overfitting
is, as I said, to apply transformations.
So that was the why.
And now let me explain to what you know, what are these transformations.
And then I will proceed in the end to the how how are we going to implement that.
So the what what are these transformations is.
Well, some simple geometrical transformations
or some zooms or some rotations on your images.
So basically we're going to apply some geometrical transformations
like transactions to shift some of the pixels.
Then we're going to rotate a bit the images.
We're going to do some horizontal flips.
We're going to do some zoom in and zoom out.
Well you know we're going to apply a series of transformation so as to modify
the images and get them as we say, augment it.
In fact, the technical term
of what we're going to do now, you know, with all these transformations
is called image augmentation, which consists basically of transforming
your images of the training set so that your CNN model doesn't over learn,
you know, it's not over trained on the existing images,
because by applying these transformations, we will get new images,
which is the reason why we call this image augmentation.
We basically augment the variety, you know, the diversity of the training
set images.
All right. So that is the what.
And now we're going to proceed to the how and to proceed to the how.
I'm going to take you to the Keras API because you have to see it.
You know, just like what we did with scikit
learn, I'm going to show you and guide you through the curves API
to find the exact tool we're going to use for this.
So let's open a new tab here.
There we go.
And in the search bar let's enter just Keras Keras like that.
Let's press enter.
And let's just get the first link.
There is only one Keras.
And that's of course the deep learning library in Python
developed by Francois Shelley.
By the way, a very talented French data scientist.
All right.
So let's go now to API docs.
And now, my friends, welcome to the Keras API.
This is probably my favorite deep learning library.
It's absolutely fantastic.
And now where we want to go is
of course to data preprocessing which includes of course three things.
Actually you have to know it
image data preprocessing, which is what we're about to use right now, but then
also time series data preprocessing and also text data preprocessing.
You can also do some deep NLP, you know, NLP with deep learning with CAS.
But now of course we're looking for something in image data preprocessing.
And let me show you exactly what that something is.
We just need to scroll down.
Well actually you already know what this is
because we already import the class, but there it is.
I'm talking, of course, about the image data generator class, which will indeed
generate batches of tensor image data with real time data augmentation,
which is exactly what I've just explained and I haven't mentioned the batches yet.
Well, that's because, you know,
we will create different batches of actually 32 images.
And these images will either be the original ones or, you know,
the augmented ones, the transformed ones after we apply the transformations.
And speaking of applying these transformations, well,
we're going to do that exactly with this image data.
Generate a class for which you will find all the arguments here.
And, you know, most of them correspond to different transformations.
I can already tell you that we will use the zoom range,
which consists of zooming in or zooming out on the images, but also we'll
use the horizontal flip, which consists of flipping the images horizontally.
And then we will also use this one, the shear range,
which is some kind of transfection.
You can check it online, but no need to understand this and all the details.
Just know that it's a geometrical transformation.
And if you want to go further that it's actually some kind of transfection.
But there we go.
These are the three transformations.
We'll use the shear range, the zoom range and the horizontal flip.
And now I'm sure some of you are asking why do we use these transformations?
Well, I'll be honest with you.
The reason I'm using them is because I simply took, you know, the code
snippet example from Keras, which is right below exactly here.
This is the code snippet example using the image data generator class.
And as you can see, we use a shearing transformation,
a zoom transformation, and a horizontal flip transformation.
And we're just going to do the same.
But of course feel free to try some other transformations.
Who knows, maybe you'll get better accuracy in the end.
Okay. But let's just trust is.
And actually I trust this because of course I tried it on our future CNN,
which we're about to build.
And you're going to see that the results in the end will be absolutely amazing.
Okay. So let's just take this.
Let's just take this code snippet to, you know,
actually get the tool that will apply these transformations.
Then of course we'll have to connect the tool to our training set.
So back into our implementation.
Well let's base that right here.
And this as you can see creates an object which we call train
data gen of the image data generated class.
So train data gen is an instance of that image data generator class.
And which represents of course the tool that will apply
all the transformations on the images of the training set.
And there is one I haven't mentioned.
You know, I mentioned and explain these three ones which are the transformations.
But we also notice this one.
Rescale equals one divided by 255.
Can you guess what this is about?
You know, we already saw this many times on our classic data set.
Well, this is of course about feature scaling.
This will apply feature scaling to each and every single one of your pixels
by dividing their value by 255.
Because remember that each pixel takes a value between 0 and 255.
So by dividing all of them by 255, we indeed get all the pixel values
between 0 and 1, which is just like a normalization.
And once again, feature scaling is absolutely compulsory
for neural networks, you know, in training neural networks.
All right. So basically this is feature scaling.
And these are the transformations that will perform
image augmentation on the images of the training set.
And this I remind is in order to prevent overfitting in the end
you can try actually you know the future training will have without these.
And you will see what I mean by overfitting.
All right. Good. So that's not it.
That's not it.
You know, for the training set preprocessing, we need to of course now
connect that train data gen object to our training set.
You know to our training set images.
So far this is just the object.
And so the way we're going to do this is we will
of course go back to our Keras API.
Because indeed the way to do this is just to take this next code here
that will actually import the training set
by accessing it from, you know, our directory.
And at the same time creating these batches and resizing
the images, you know, in case we need to resize them in order to
reduce the computations of the machine, you know, to make it less compute
intensive, which is what we'll do, because we will see that
with a lower size will still get amazing results in the end.
So let's get this.
And once again I will explain all this code.
And mostly we will have to change it the right way
so that we can adapt it indeed to our situation.
So let's take it step by step.
This is actually the name you want to give to
your training set, which you are importing in the notebook.
And let's just give the usual names.
We're going to call that training underscore set just like before.
Then we take indeed our train data gen object,
that instance of the image data generator class.
And from this object we're going to call a method of this class.
Right. Because this class is every class contains methods.
And one of them is this flow from directory which will just simply,
you know, connect this image augmentation tool to the images of your training set.
All right.
Then let's have a look at the different parameters.
So the first one here is actually the path leading to your training set.
And so of course we have to change this because we have a different path
to our data set.
So this is a whole folder
which I've shared with you at the beginning of this section.
And this is also the root folder.
You know this is the base of the folder.
You know the beginning of the path.
And so now in order to access the training set
well we first need to specify that we want to go into this data
set folder and then into this training set folder.
And that's exactly the path leading to the training set.
And therefore here you know in this parameter of the flow
from directory function.
Well we simply need to replace data here by data set
and then train here by training set.
All right.
This is a simple path leading to the train set folder starting
from the root of our directory folder okay good.
Now next argument target size.
That's indeed the final size of your images
when they, you know, will be fed into the convolutional neural network.
And actually I tried with 150 by 150.
And that's actually made the training very very long.
So I actually wanted to reduce that to, you know, 64 by
64. And that's totally fine.
This will make the training much faster.
And still we will have amazing results.
You'll see that at the end.
Then the batch size is, you know, the size of the batches, meaning
how many images we want to have in each batch.
And the 32 is a classic default value.
And we're going to keep that. That will be totally fine.
And finally
we have to specify the class mode, which is either binary or categorical.
And of course since now we have a binary outcome, you know, cat
or dog, well, we have to choose of course class mode equals binary.
Okay, perfect.
And that closes the pre-processing of the training set.
We are done with this first step of data preprocessing.
And so now we're going to move on to the next step preprocessing the test set.
And of course in the spirit of always be as much efficient as we can,
well, we're going to go back to our Keras API.
And we're just going to take this time, this line of code
to, you know, get that same image data generator
object to, you know, apply the transformations to the test images.
But be careful.
We're not going to apply the same transformations here,
such as shearing the zoom and the horizontal flip,
because of course we don't want to touch the test images
because they're like new images, like when deploying our model in production.
And therefore, of course, we have to keep them intact like the original ones.
However, what we have to do to them is indeed to rescale their pixels.
And that's the same as before, you know,
remember when we were applying feature scaling to our training set and test it?
Well, we used the fit transform method on the training set,
but only the trend for method on the test set.
And that was of course to avoid information leakage from the test set.
And well, here that's exactly the same.
We have to keep
the images of the test set intact by not applying any transformation.
However, we have to feature scaled them because once again, the future predict
method of the CNN will have to be applied to the same scale
as the one that was applied on the training set.
So you see, this is exactly the same as before.
It's just that
we are using some different classes, but which are, after all, the same tools.
All right.
So let's get this and let's
put that back into our implementation in a new coat cell.
So we're going to paste that.
We're going to keep the same name for the object.
That's solely fine.
And then well same we're going to go back to this and we're going to get exactly
this which will actually import the test
set images into our notebook.
All right.
So let's test that.
And now let's do the required change.
Actually please press pause on the video and do the changes yourself.
I'm sure you're going to do this successfully
because this is exactly the same as before.
All right. So now let's do it together.
The first thing I would like to do is just change its name,
which is exactly the name of the variable that will contain the test set.
And just to be consistent with before, well, let's just call the test set.
All right.
So test set then this is correct.
We call our test data in here which will only apply
which is scaling to the pixels of the test images.
Then we call that same function
flow from directory to access the test set from our directory.
And here once again we need to replace data here by data set
and then validation by you know remember
now we want to get the path that leads to the test set.
And therefore that first data set and then test set.
All right.
So here we just need to replace validation by test set.
Good.
Then of course we need to have the same target size.
Because basically the break method has to be called on the exact
same format as the one that was used for the images of the training.
So here we need to get the same size as in the training set.
Therefore, 64 by 64 and the same batch size.
Basically our Mo will be evaluated on batches of 32 images.
And of course the same class modes. Binary.
Good.
Well there you go.
We're done with data preprocessing.
It was very different. It was actually brand new.
But we recognize some of the same process steps as what we did before.
And so now I'm very excited because we can move on
to the exciting part, which is about building the CNN.
Yes, we're ready for part two now, which we're going to tackle in several steps.
And so make sure to get good energy for this.
And as soon as it is a case, join me in the next tutorial
to smash this time part to building the CNN.
And until then, enjoy machine learning.