Okay, my friends.
So now we're about to have a very exciting tutorial
because we're about to watch the demo, you know, the demo of our eye
being built first and trained and finally making our single predictions.
So this is where we left off in the previous tutorial.
Basically all the implementation is done and now I'm going to guide you through
how to run this on Jupyter Notebook.
And therefore the first thing I'll do will be to download
and install everything we need to have a Jupyter notebook.
If you already installed Anaconda, you know which contains Jupyter Notebook,
and if you already have Jupyter notebook on your machine, then feel free to skip
the five first minutes of this tutorial and start directly from minute five.
Okay.
This is when we will start using Jupyter Notebook,
and for the rest of you, well follow me and will guide you
through the installation of Anaconda and therefore Jupyter Notebook.
So let's open a new search tab on your favorite browser.
And in the search bar let's type and conduct.
There we go.
So Anaconda then we're going to go to the first link.
Anaconda has become so popular now.
And we're going to go to well get started and install Anaconda Individual Edition.
Then you can click download here.
And then you're going to choose the Anaconda installer for your system,
whether you have a windows, Mac OS or Linux.
I'm on Mac OS, so I'm going to go for this.
And you're going to take the graphic installer, not the command line installer.
All right.
So graphic installer and you're going to take the Python 3.7 version.
Not the Python 2.7 right.
So only this row here.
So choose your system.
Click the file and this will download Anaconda.
You know the installer.
And in a few seconds you'll get you know the installer on your machine
probably in your download folder which is done for me.
Good.
So let's just get rid of this download.
And now let's go to my machine. There we go.
This is my download folder on which the installer was download it.
So make sure to find it and then double click it.
And this will launch the installation of Anaconda.
Here you can just click continue continue continue continue.
Just click agree and continue. You can read that if you want
and then install.
Then this will install Anaconda.
This will just take a few seconds not more than one minute.
And you know okay less than a minute.
There we go. Good.
So right now Anaconda is installing.
And let me tell you a few words about Anaconda.
So this is basically a platform that contains several Ides
on which you can code
in Python and even R, because you will see that it contains now hours to you.
But the Python Ides that it contains include Jupyter Notebook and also Spyder.
Spyder is another great IDE to code in Python,
but here
we're going to go with Jupyter Notebook because we have been used to, you know,
the Ipynb format, which was the format we used on Google Colab.
But this time, since we have a huge data set which we can't import on Google Colab,
well, we're going to run this with Jupyter Notebook.
All right. So let's see where we are.
So still installing well you know running the package scripts.
But you will have Anaconda on your machine with no issue.
All right.
So here this is just to give indeed access to your downloads folder.
So of course you click okay.
And then it should progress now.
All right let's see.
And there we go.
Perfect. So now we have Anaconda.
You can click continue and installation and yes close.
And then you can either choose to keep the installer or remove it as you want.
I'll just keep it.
All right. So good. Now we have Anaconda.
Then it's not over.
We still need to install a few things.
And these things are unfortunately
the TensorFlow library and the Keras library.
Because I remind that the beauty of Google Colab and the reason
why I wanted to code everything on Google Colab
was that we didn't have anything to install because all the libraries
and packages are already pre-installed, except for the rare ones.
But here on Jupyter Notebook or Spyder or any Anaconda
ID, well, we have to install manually the packages.
And that's exactly what I'm about to show you.
So for Mac and Linux users, please open your terminal Linux users.
You'll find it very easily.
And Mac users, well, you can just press command plus space.
And then in the spotlight search you can type terminal.
And this will open your terminal.
And for windows users no worries I'm not forgetting you.
Windows users, please go.
You know, at the lower left corner of your monitor, please click
that window button and then find Anaconda in your list of programs.
And in the Anaconda tab you will find Command Prompt.
Then please click it and this will be the equivalent of the terminal
on which you can run commands to install packages from the web.
All right. So now we should all be on the same page.
You know, windows users, Mac users or Linux users.
And we're about to install TensorFlow and Keras.
And you will see that it will be still super quick and easy,
because the simple commands that we have to enter here
in order to install TensorFlow and Keras are the following.
So we have to enter them separately. Let's start with TensorFlow.
We simply need to enter pip, then install
and then tensor flow.
All right.
And then we just need to press enter.
And this will download TensorFlow from the web
and install it on your Anaconda environment.
All right.
So right now as you can see it is downloading it and installing it.
All right. So don't worry about all this.
This is totally fine right C installing connected packages
including indeed TensorFlow.
So it is not only installing TensorFlow
but also some what we call dependencies which work with TensorFlow.
But please don't worry about all this.
This will just install TensorFlow which is exactly what we need.
Okay, good. So installation done.
As we can see successfully installed all these dependencies including TensorFlow.
And now to install Keras.
Well we're going to do the same.
We're going to enter pip and then install
and then Keras and then press enter.
And this will install Keras on your machine.
There we go successfully installed Keras.
And that's the version that we just installed.
You might get a different version if you take this course way after I recorded it.
Good. Done with the terminal?
No worries, we won't have to deal with it anymore.
I hope it wasn't too traumatizing for you.
Know, those of you who used it for the first time,
as I know it can be a bit surprising at first, but indeed, this
is the classic way of installing packages when you're not working on Google Colab.
So then you can quit the terminal.
And then there we go.
Now we we're ready to finally open Anaconda and mostly open
Jupyter Notebook.
So we're going to find Anaconda in either, you know, the list of applications
from Mac users
or in your list of programs for windows users and same for Linux users.
So on Mac here is Anaconda.
So let's just double click it and this will open it.
I can just go back to, you know, my desktop here
because Anaconda is now opening.
And in a second we should see that beautiful platform, very user friendly,
containing all the different Ides on which you can code with Python.
All right. Loading applications.
And in a second there we go.
Welcome to Anaconda. Good.
So as I told you you have several Ides.
You have this studio for all,
but you have several of them for Python, including Jupyter Notebook and Spyder.
And as we said, we're going to run our implementation on Jupyter Notebook.
So here we're going to click launch.
All right.
And this will launch Jupyter Notebook.
And this will automatically open it on a new tab on your browser.
You'll see we should see pop up in a second.
There we go.
So welcome to Jupyter Notebook.
So this is your machine.
You will recognize the folders of your machine.
And of course
now you're going to go to the folder that contains, you know, the data set.
Know what you downloaded at the beginning of this practical activity.
So remember mine is on my desktop.
And here is the folder, you know, section 40 convolutional neural networks.
So we're going to click it.
And inside.
Well as you notice I only kept the data set.
I removed the two previous implementations which were in that folder.
Because now what I want to do is actually take our exact implementation.
We code it during this practical activity.
I'm going to download it by clicking file here
and then download dot ipynb.
Make sure it's a Ipy and B, because that's the format used for Jupyter Notebook.
All right then I'm going to close this.
And now we're going to go to where this was downloaded
meaning in the downloads folder we're going to put that into this folder
you know containing the data set right here.
So you have to put it in the exact same folder.
That's important because you have to run this file and Jupyter
notebook within the same directory folder that contains the data set.
That's why it's important to do it.
So this is the exact implementation we, you know, coded together
during the practical activity.
And now we're going to check that it works
by running each and every single one of the cells.
So let's do this.
Let's go back to Jupyter Notebook which is right here okay good.
And now indeed we have well our implementation.
Good. So let's open it by just clicking it.
And welcome to our implementation of the Convolutional Neural Network.
And now now is really the Showtime.
Because the only thing will have to do will be to click
this run button cell after cell, even the text cell.
You know, we'll just start from the beginning and see what happens.
Okay. So are you ready?
Let me just, you know, clear all the outputs
which were the outputs we got in Google Colab.
So to do this we can click kernel here.
And then restart and clear output. Right.
We won't get these outputs.
We will just really start from scratch okay.
Good.
So now let's do this 3 to 1.
Go run okay.
So that doesn't run anything because this is a text cell run again.
And now it is starting.
So we're going to click our first run for the first cell.
And this will import TensorFlow.
This means that it is running right now you know the cell is running.
And when it is done running we will see the first number one meaning
that the cell is successfully run okay there we go using TensorFlow back end.
That's the output of this cell.
And in order for this to work this way will make sure to have run
like me the pip install commands of TensorFlow and Keras.
All right. Good. Now let's check the version.
It's still going to be you know TensorFlow 22.2.0.
And then let's just run the rest of the cells.
There we go.
So now we're entering part one preprocess the training set.
And now running this cell we will get in the output that indeed
we imported and preprocessed you know with data augmentation
8000 images belonging to two classes dogs and cats.
Okay then let's click run for the next cell.
Preprocessing the test set.
And now running this cell, we will get indeed
this time 2000 images belonging to two classes.
And of course no image augmentation was applied.
Only feature scaling was applied.
All right.
And now entering part two building the CNN.
So there we go.
First we initialize the CNN as a sequence of layers.
Then we start the first step, step one convolution
where we add a convolutional layer then pooling.
Let me just scroll down a bit actually right then pooling.
So here we apply max pooling to this first convolutional layer.
Good. Done.
Then we add a second convolutional layer.
And at the same time we apply max pooling.
Done.
Then we proceed to step three
where we flatten the result of all these convolutions and pooling
into a one dimensional single vector, which will become.
In this new step for full connection, the input of a fully connected
neural network, which will contain only one fully
connected layer with this cell that we're about to run.
Done.
And now let me scroll down a bit again.
Right. Let's scroll down like that okay.
And now in the final step of the construction process of our CNN,
we will connect all these to the final output
layer which will contain the final prediction.
Right.
So let's run this cell and there we go.
Now we're done building the CNN.
Everything looks good so far.
We now have a brain with some eyes that are able to see images.
You know, just like we humans do with our eyes.
And now now that we have this brain and these eyes will time to make them
smart by training the CNN to recognize cats and dogs in images.
So there we go.
We end to part three now, first compiling the CNN
with an Adam optimizer, which will perform stochastic gradient descent.
The best way then a binary cross entropy loss function.
Because we're doing binary classification and an accuracy metrics there, we go.
Now our CNN is compiled.
And now now, my friends, it's time for the training.
Are you ready.
This following cell will run the training for 25 epochs.
And at the same time we will get the accuracy on the test set.
So this will be very exciting because we will see the accuracy on both
the training set and a data set getting increased and increased over time.
Are you ready?
I'm not going to make us wait longer.
There we go.
Now the training is starting.
There we go with the first epoch one of 25.
And here you see the loss, the accuracy.
You know, progressing, increasing.
So basically the 250 here correspond
to the fact that, you know, the batch size is 32.
And we have in total 8000 images.
So that's, you know, 32 times 250 equals 8000.
So basically, you know, we have 32 images in the batch
and we have 250 steps in each epoch
to, you know, reach the total amount of 8000 images.
All right. So first epoch done. Let's see what we got.
We got an accuracy of 61% on the training set.
Be careful.
And on the data set we got an accuracy of 67.75% which is pretty good.
All right.
And now the other epochs are running.
But as you can see it's going to take some time.
So I'm just going to do a fast forward here with some exciting music
so that we can observe and mostly admire the results and the progression of,
you know, getting higher and higher accuracies in accelerated mode.
All right. Are you ready?
3 to 1 action.
All right.
Here I am, back in the game.
I'm super excited to see the final result here of the 25th epoch.
There we go.
We end up with a final accuracy on the training set of 89%
and a final accuracy on the test set of 80%, which is great.
And I remind that if we hadn't done the image augmentation preprocessing,
you know, in part one,
well, we would have ended up, you know, you can try with an accuracy here
if the training set around 98 or even 99%, which clearly indicates
overfitting and a lower accuracy here on the test set around 70%.
So that's why I insist image augmentation is absolutely fundamental.
Okay, so the training is done. There you go.
You had your first advanced training.
Congratulations again.
And now well, let's test our model in production
by not making that single prediction of a single image.
All right.
So just before we run this let's just make sure we know what we're predicting.
So we're going to go back to our folder which is right here.
We're going to go into our data set.
And remember those single images are in the single prediction folder.
And we're going to start with this one
character one which contains of course a dog.
And now we're going to check
that our CNN can predict that indeed there is a dog inside this image.
Are you ready?
Let's do this. Where is it? It's right here.
All right. So play.
Now we're first going to play this cell to you know get the prediction.
Make sure to have cat or dog one here which corresponds
to the image we just saw.
And now let's play the cell.
And now we're about to get the final prediction printed in the console.
And we were certainly hoping to see Doug in the output.
Are you ready.
3 to 1 run and perfect RC
and then predicted that there was a dog inside the image okay good.
First test passed successfully.
Now let's see for the other image.
This one which contains of course a cat.
So let's deploy our model on this single image and check
that indeed our CNN returns a cat.
So to do this we just need to,
you know, replace this image name here by cat or dog two.
Then we can just play this cell again by just clicking run again.
And now we're going to print this again to print a new prediction
of this other image. So let's do this.
And let's hope that indeed we get cat in the output of the console.
Perfect.
So our CNN got all the answers correct.
But what if I make it even more challenging,
you know, because maybe I was sneaky and maybe I chose images
in a test set that I checked was working well with our CNN.
You know, that our CNN was able to predict correctly.
So to make it more challenging, my idea is to open a new tab here.
Then in the search bar I would like to generate random number.
And we're going to generate a random number using this.
You know the classic tool by Google.
And we're going to generate a random number from our test set,
you know, which contains the new images on which the model wasn't trained.
And we're going to take randomly an image from this test set
and test it as a final test, you know, for CNN.
So let's see the images of the dogs in the test set
go from 4001 to 4000.
Well, 5000.
And the cats go from same 4001 up to once again 5000.
Okay. So let's start with dog.
First let's generate a random number, you know, between
4000 and 5000.
And let's generate this.
And we get 4689 okay.
So let's get that image 4689 which is a bit below okay.
Right here 4689.
All right.
So that's actually going to be quite challenging.
Our CNN might not recognize that it is a dog.
Well let's see.
So I'm going to take that image
I just copied it and into the single prediction folder
I'm placing it and I'm going to rename this.
You know I'm going to rename this cat or dog three.
All right. So that will be our third image.
And now let's generate a new number for the cat.
All right 4538.
And so in the test set let's go into the cats folder and at stake.
Sorry what is it again 4538.
Yes 4538.
Which is right here.
All right.
Whoa. It's going to be challenging again. Let's see.
We only see a cat head here.
So let's see okay.
Well I didn't expect such a hard challenge.
But let's see how our CNN is going to do it anyway.
So I just copied it and into this single prediction folder let's paste it.
Let's, you know, copy the name of this
and then paste that here to replace that by cat or dog for.
All right.
So let's deploy our model on this single image first.
And let's hope that you know it predicts this time a dog.
So here it is cat or dog three I'm scared.
And let's play run and then print prediction dog.
Woo hoo. Great.
I was really scared because, you know, it's not that obvious for a machine
that the dog in the image is a dog, but that's really good.
Okay.
And now that special cat in this red gift wrapping thing, cat or dog for.
Let's hope that our model recognizes that it is a cat.
You know, it's obvious for us humans,
but here we only see a head actually, you know, with the features of a cat.
So yeah, let's hope that it will work.
So cat or dog four.
And now let's play the cell.
I'm scared again.
And, Excellent.
So 100% correct answers from our CNN.
So that's awesome.
I'm really happy that it worked 100%.
That's because, you know, we have, after all, an accuracy of 80% on the test set.
So which means that indeed eight predictions will be correct out of ten.
So yes, even if the images are quite challenging, you know, with not
some obvious dogs or cats, well, our model can do pretty well.
So congratulations, you just built a pretty advanced artificial neural network.
You know, the convolutional neural network is quite technically advanced.
So congratulations.
And not only you built it, but mostly you built it successfully.
It is now a really good cat or dog predictor.
And feel free to, you know, play the same game by generating
a random number and test some other single images you might be surprised
by, you know, the predictive power of the model you just built.
All right, so we're making great progress here.
Now we done with the deep learning part.
So we're going to move on to the next part which is about dimensionality reduction
and which is super important for you in your career because you know
you're going to work with huge data sets with many, many features.
So you need to have the right tools in order to reduce the dimensionality
of your data set without losing, of course, the information you know,
the information that allows to learn some correlations between the features
and the dependent variable.
So that's a super important chapter.
I'll look forward to seeing you in this next part.
And until then, enjoy machine learning.