We've gone from the fashion dataset where
the images were small and focused on the subject,
to a new situation where we had images
of horses and humans and action poses.
We use convolutions to help us identify
features in the image regardless of their location.
This is a nice primer in solving
some common data science problems on places like Kaggle.
We'll next look at an old competition where you were
encouraged to build a classifier
to determine cats versus dogs.
If you're not familiar with Kaggle,
it's where ML challenges are posted often with prizes.
Cats versus dogs was a famous one from a few years back.
The techniques you've just
learned can actually apply to that problem.
So let's recap some of the concepts.
One of the nice things with TensorFlow and Keras
is that if you put your images into named subdirectories,
an image generated will auto label them for you.
So the cats and dogs dataset you could actually do that
and you've already got a massive head
start in building the classifier.
Then you can subdivide that into
a training set and a validation set.
Then you can use image generators
that appointed at those folders.
To use an image generator,
you should create an instance of one.
If the data isn't already normalized,
you can do that with the rescale parameter.
You then call the flow from
directory to get a generator object.
For the training dataset,
you will then point at the training directory
and then specify the target size.
In this case, the images are an all shapes and sizes.
So we will resize them to 150 by 150 on the fly.
We'll set the batch sizes to be 20.
There's 2,000 images, so we'll
use a 100 batches of 20 each.
Because there are two classes that we want to
classify for its still stays as a binary class mode.
Similarly for validation, we set up
a generator and pointed at the validation directory.
We can explore the convolutions and
pooling and the journey of the image through them.
It's very similar to what you
saw with the horses and humans.
It has three sets of convolutions followed by pooling.
Of course, the image is 150 by 150.
Similarly, there's a single neuron
with a sigmoid activation on the output.
The summary of the layers is very similar
to before but note that the size changes.
We start with 150 by 150.
So the convolution reduces that to 148 by 148.
From there, we'll go until we end up with 17 by
17 that we feed into the dense layers.
Compilation is as before.
Now remember you can tweak
the learning rate by adjusting the lr parameter.
So now to train, and we can call model.fit generator
and pass it the training generator
and the validation generator.
That's it. As you can see,
it's very similar to what you
built for horses versus humans.
So let's see it in action.