LAURENCE MORONEY: Hi.
And welcome back to Machine Learning Foundations.
This is Episode 6, where we'll take
what you've learned about convolutional neural networks
in the previous few episodes and apply them to a computer vision
scenario that was a Kaggle challenge not that long ago.
But before we get there, let's take a look
at the answer to the exercise from the last video.
So this was a very simple exercise.
Let's take a look.
First of all, we'll import everything
that we need to import, and we'll
download the data set, which is just
some simple happy or sad faces.
You'll then extract the data set,
and you'll put it into a folder called tmp/h-or-s.
And if we take a look at our files,
we could see in there tmp/h-or-s has been downloaded.
And we have happy images in there,
and we have sad images in there, as we can see.
I've also set up a callback.
So once it hits 99.9% accuracy, it will cancel training.
It's a very small data set.
It may be overfitting hugely for the data set.
But that's OK.
We'll just want to use this to test out some convolutions.
So here we can define our network.
And we define it three convolutional 2D layers,
three max pooling 2D layers that will then flatten and feed
into a Dense.
We can use the RMSprop optimizer, set the learning
rate to be 1 times 10 to the minus 3.
You could even tweak this as much
as you think will work best.
So now we just use our image data generator.
And we'll set up a train data gen,
which will rescale the images.
And then we'll flow from directory.
And it will flow from the directory that we've just seen.
It will resize to 150 by 150.
And it will flow them in batches of 10.
There are 80 images in the data set.
So when we then do the model.fit,
we should set steps per epoch to be 8 because 8 times 10 is 80.
And that makes it run nice and fast.
So we can do a model.fit now.
And we can see it's running really, really quickly.
It's 0.2 seconds per step.
And we hit 100% accuracy very quickly.
It's a super simple data set.
But hopefully, the scaffolding that you're building out here
is something that you can learn from when you're building
for more complex models.
Now, that wasn't so bad, right?
It was a super simple exercise, but it
will lay the foundations for what you're
going to study in this video.
So let's get started.
The dogs versus cats data set has 25,000 images
of cats and dogs in various poses.
It was used for a Kaggle challenge a few years ago
in determining state-of-the-art computer vision techniques.
In the next few minutes, you'll see
how to use what you've learned so far to build
a classifier for cats and dogs that's over 96%
accurate on the training site.
As before, you're going to split the data into training
and validation directories.
And each of these will have cats and dogs sub-directories.
You will then be able to train a classifier on cats and dogs
images using a generator that pulls from the training
sub-directories and which validates
by using a generator that pulls from the validation
sub-directories.
So to get started, let's first import an image data generator,
which allows us to use them.
For our training data, we'll then simply
create an image data generator and flow
from the training directory.
Note that the image data generator
will handle normalizing the images by dividing
their byte values by 255 to turn a value from 0 to 255
into a value from 0 to 1.
To get the training images from the training sub-directories,
we can call flow_from_directory, passing in a bunch
of parameters--
the first of which, of course, is the training directory
itself.
This should only contain sub-directories
for which we'll provide the labels for the classes.
We have cats and dogs sub-directories.
So we'll have cats and dogs labels.
Next is our target size.
And as the images come in many sizes,
we'll need to have a consistent shape
to feed into the neural network.
I've set them to be 150 by 150 here.
But you can choose whatever you want, of course.
All images, regardless of their shape, will end up as 150
by 150.
So choose carefully.
And remember when creating your model to have the input layers
use the same dimensions.
Next, choose a batch size for which
the images will be loaded.
Pick something that divides evenly into a step size
that you'll use later.
So for example, if I have 22,500 training images,
and that's taking 90% of the data for training,
and then a batch size of 250, then I
will need 90 steps to load this into the neural network.
Finally, there's the class mode.
And as the two classes, we'll set it to binary.
For validation you'll do exactly the same.
Except, of course, that you want to flow
from the validation directory and not the training one.
And here's where you define your model.
This should look familiar by now.
It is a convolutional neural network.
I've designed this one with three convolutional layers,
each paired with a max pool layer.
The first convolutional layer will learn 16 filters,
the next 32, and the next 64.
It's important to remember the input shape.
Remember earlier we resized everything to be 150 by 150.
And that's what we use here.
The 3 is for the color channels.
If you use a different size, make
sure to adjust this to match.
And similar for your output layer.
This should match to the number of classes in your model.
One exception, which we're following here,
is that a binary classifier can get by with just one neuron,
provided you use a sigmoid activation
function, which sets it to 0 for one class and 1 for the other.
If we take a look at our model summary, it looks like this.
You can see the familiar resizing of the image
as it travels through the network
by convolutions and pooling.
By the end, you can see that we have 9 and 1/2
million trainable parameters.
So this can take a while.
I've chosen to use RMS prop as the optimizer here.
It's set with a high learning rate.
And you can tweak this to try for better
performance on your network.
Finally, we'll train the network by specifying the training
and validation generators as our data sources.
Don't forget to set the steps per epoch and validation steps
for performance.
And these should be calculated by dividing the amount of data
by the batch size.
This gives us 90 and 10, respectively in this case.
Now that gives you everything you
need to train a cat versus dogs classifier.
Let's take a look at it in action, after which I'll
give you the URL with all the code
so you can try it for yourself.
Let's take a look at a much larger data set.
And this is cats and dogs.
The cats and dogs data set has about 25,000 images in it.
And those images are various different cats and dogs
in various poses.
They're real photographs.
And this data set was used for a Kaggle challenge
a couple of years ago.
We're going to do our imports first.
And as well as TensorFlow, we're going
to import a bunch of stuff that we can use for manipulating
the file system.
Because this data set is just a raw zip containing
cats and dogs, we're going to have
to do a bit of data pre-processing on it
so that we can use it with image data generators.
So we can see here it's about 800 meg to download.
And now I'm unzipping that to create the /tmp folder
containing the cats and dogs.
And if we go to the file system and we take a look at /tmp,
we'll see in here as pet images.
And within pet images are cat and dog.
I'm not going to open them to look at the files
right now, because it takes a little while for them
to all list out, and it will look like code that was frozen.
But what I can do is just list the directory,
list how many images are in it.
We can see there's 12,501 in each, which is our 25,000.
And now I'm going to create my own directory.
So cats versus dogs master.
And these will contain training and testing.
And each of those will have cats and dogs images within them--
or cats and dogs folders within them
that will contain the images.
I've created this function called Split Data.
And as its name suggests, what it does
is it will split the data into training and testing for us.
So if you specify a split size of 0.9, what it's going to do
is it's going to get 90% of the images
at random that will be for training,
and 10%, which will be for testing.
And then it will split these into cats and dogs for us.
It'll also filter out any of the images that are corrupt.
And as we can see here, two of them are 0 length.
So we'll ignore them.
Just to make sure I got them right, [INAUDIBLE]
that my sums were correct.
We can see that 90% of the images--
11,250 of each are in the training.
And 1,250 of each are in the testing.
I can then create my model.
And it's just going to be simple convolutional neural network
with three layers of Conv2D combined
with three layers of max pooling that get flattened and fed
into Dense.
I'm going to compile this with an RMS prop
with a learning rate of 1e to the minus 3.
You can tweak this, maybe, to make it more accurate.
I've set it to be a binary cross entropy because there
are only two classes.
Next, we're going to create generators.
And our generators are going to rescale the images
by dividing by 255.
There are better ways to rescale,
but this one will work for now.
And then we're going to flow from the training directory
to the training generator.
And we're going to flow from the validation directory
to the validation generator.
Note that I'm setting batch sizes here so that when we're
training later on, we'll load them in batches so we
can be a bit faster.
So next up we run that cell.
So next up, we're just going to do the model.fit,
passing it the train_generator and the validation_generator.
And we're setting steps per epoch and validation steps
to match the number of batches that we have
to match the number of images.
So this should run quite fast.
So in the case of training, it's going
to take 90 steps of 250 images each.
And it's going to take 10 steps of 250 images each
in order to do validation.
This gives us a much faster training.
If you were not to do this, it would be loading the images one
by one into the GPU, and there would be a lot of wasted time.
But as we can see, it's still a big and complex data set.
So it's taking maybe 45 seconds per epoch.
So I'm just going to speed it up until we get to the end.
Before we do that, actually, just note
that you'll see warnings like this-- possibly corrupt EXIF
data.
Don't worry about those.
The EXIF data is additional metadata
that goes onto the image with things
like the location of the image.
So when the TIF plugin is actually decoding those images,
it's trying to read that, it sees as corrupt,
and it's giving us that warning.
It's not going to impact your training in any way.
And now we see it's almost done.
We're about 3 seconds to go in the final epoch.
We've only trained for 15 epochs.
And we see it's just a little over a minute per epoch
like this.
Let's take a look at what our final figures are.
We can say it was 97% on the training set
and 82% on the validation set.
So not bad.
It is overfitting a bit.
We can work on that.
But right now it looks pretty good on the validation
set, even 82%.
We can actually take a plot and see
what our history looks like.
And we can see like, our validation accuracy
against our training accuracy.
And we can even see our loss of validation against training.
So this indication of the validation curve going up
like this is clear indication of overfitting.
So we could do a bit of tweaking there.
And we'll learn about that with image augmentation later.
And if we want to try to test a few images, we can do so.
So I'm going to do this.
I'm going to choose some files.
I'm going to go to my downloads, where I've
downloaded a few files already.
You can guess from their names what their contents are.
And let's see how the classifier does with them.
So we can see it thought puppy was a dog.
It thought pug was a dog.
It thought dog was a dog.
It thought this cat was a dog.
And it got both of these cats right.
And if we take a look at cat 253662,
so this was the one that it got wrong
and it thought it was a dog.
So it's an interesting picture.
And maybe we can learn from this and how to optimize it better
for the future.
Now that you've seen it in action,
here's the URL for the lab so you can try it out
for yourself.
As you saw on the screencast, there
is room for improvement because of overfitting.
And in the next video, you'll learn some techniques for that.
But have fun with the lab in the meantime.
And I'll see you next time.
[MUSIC PLAYING]