The first thing we will do is download the set of images.
They're stored as a zip file containing 3,000 images,
2,000 of which we'll use for training,
and 1,000 for testing.
Once they're downloaded, you'll need access to
the underlying operating system of
the virtual machine on which this Colab runs.
This is available in the OS namespace.
This code will unzip the cats and dogs data
that you just downloaded, into the directory.
In there, the sub-directories will be recreated,
because they're stored that were in the zip file.
The next thing is for us to set up
our directories as variables,
so we can point the generators at them.
Now, that we have the directories as variables,
we can pass them to the os.listdir
to take the files from those directories,
and load them into Python lists.
We can see there's a list of
file names like cat253.jpg, etc.
On the files tab,
we can also inspect the file system and
we can see our cats versus dogs folder,
within which we have
training and validation sub-directories,
which each contain cats and dogs directories.
The images, of course, are stored in there.
If we then count the images in each directory,
we can be sure that we have the right amounts of images.
As we can see, we have a thousand
of each animal in Training
and 500 of each in Tests
for total of 3000.
Next up, we can visualize some of the data,
so we can see how diverse it is.
This code would set a matplotlib
which is a Python library for drawing graphics.
This code will pick up some random cats and dogs
and draw them in a grid.
We can then see once it's drawn
that there's a lot of diversity in these images.
There's different colors of animal,
there's different location within the picture,
and there's even sometimes multiple items in the picture,
like the lady here holding a cat.
We can then run it again to see some more.
This image containing multiple cats
is particularly challenging.
Okay. Now, let's build our neural network.
We'll import TensorFlow, and then we'll define our model.
We'll print the summary,
and here you can see the output shape
of how the image passed through the layers,
and gradually reducing in
size through the convolution and pooling.
Here, is where we compile our model,
defining the loss function and the optimizer.
Here, is where we set up the two generators,
pointing them at the training
and validation sub-directories.
These contains sub-directories of their own,
each with cats and dogs.
When I run it, you'll see the printout.
It found 2,000 images in two classes,
and that's the training, and
1,000 images in two classes, that's the testing.
Now, we'll start the training
and we'll watch it progress.
It should take 3-3.5 minutes.
When it's done, you can see the accuracy is about 73%
and it's not bad.
It's not great but it's not bad.
Okay. So let's take a look at
how the model predicts on some images.
So I'm going to choose these five images
from my hard drive and upload them.
Once they're uploaded, you can see that
the classifier gives me a prediction on each.
So let's now compare.
This first one is really impressive and as you can see,
the dog is in a very small part of
the image and there's lots of
other features like trees, mountains,
skies and lakes, and the dog
also has its face turned away,
but the classifier still got it right.
This one is obviously a dog,
but again it's partially obscured.
You can't see the body.
It's just a white mass and the face while pronounced,
is also hidden amongst the fur, but it got it right.
This one's a bit more obvious,
but it's also a very different
looking dog than the previous one.
The eyes, nose and mouth are all part of a dark patch,
but the neural network still recognize the dog.
This one's also very impressive.
There are two cats in the image and one is mostly hidden,
but that didn't confuse the model
that correctly recognized the cat here.
Strangely enough though,
this is the one that the model got wrong.
Maybe it's because we can only see one eye,
or maybe the sharply defined branches
to the left confused the model.
So take a look,
think about what you would do to go through
this workbooks at this point and have some fun with it.
Find some images of cats and dogs
and upload them to the notebook
and get it to classify them,
then see if you can find one that you would
expect to be easy that the prediction gets wrong.
Once you've done that, see if you can edit
that image in a way that gets it to work.