In this lecture, we are going to look at a CoLab notebook that does image classification using PI torch
with a convolutional known that work on the fashion éminence data set.
This lecture is going to walk you through a prepared CoLab notebook, although a very good exercise,
which I always recommend is once you know how this is done, to try and recreate it yourself with as
few references as possible.
As usual, you can look at the title of the notebook to determine what notebook we are currently looking
at.
All right, so let's start by recognizing everything that should seem familiar.
First, we have our imports.
There should be nothing unexpected here.
Notice how we're also importing the daytime module.
This is useful since as your data grows and your models grow, training is going to take longer and
longer.
When training starts to take a non-trivial amount of time, we naturally start to ask, so how long
is this taking?
And so we can use the daytime module to get a sense of how long each iteration of the training loop
takes.
In more high level libraries like Carus, this all happens for you, but in Pittsburgh, if you want
these features, you have to build them yourself.
Next, we load in the data, which is, as we mentioned, the fashion administrator said, since it's
included in the toy division library, nothing has to change in the way we load in this data.
Next, we're going to check a few of the data sets attributes to see how it compares to.
Remember, it's supposed to be a drop in replacement.
As you can see, the maximum value is to 55, which is what we expect.
The shape of the data is sixty thousand by twenty eight by twenty eight, which is what we expect.
And the targets contain integers that look like they're between zero and nine inclusive, which is also
what we expect.
Next, we load in the test data set and then we get the number of classes by casting all the targets
into a set and then taking the length of that, sir.
As expected, the data set contains 10 classes.
The next step is to define our CNN model.
As promised, we're going to start looking at how to build models using inheritance rather than stacking
layers together in a sequential.
On the first line, you can see that the CNN class inherits from the and module class.
This makes our class an official title.
Each module, meaning it has all the useful functions and attributes that a built in PI module has,
even doing automatic differentiation inside the constructor.
The first thing we do is call the parent classes constructor.
Next, we create our layers.
The first set of layers is a series of convolution layers, followed by the YOU activation function.
We can wrap this in a sequential to make it easier to call in the future.
Notice that the first convolution layer has only one input channel.
That's because, as you know, the most intense fashion this data sets are greyscale.
As you recall, we need to do some convolutional arithmetic in order to calculate the arguments into
the first linear layer.
So I've left some useful links here on this topic.
If you want to check them out, I would recommend you do the calculation yourself to make sure that
what I've written below is correct.
Next, we have a series of dense layers, along with some dropouts and a new activation.
Again, we wrap this in a sequential so it's easier to call later.
Next, we have the forward function.
This is where we actually pass a given input through the CNN to get the neural networks output.
As you can see, it's made up of just three lines of code thanks to our sequential objects.
First, we pass our input into the convolution sequentially.
Next, we reshape those outputs in order to flatten them using the view function.
Notice how there's no need to specify both dimensions of the flattened output.
However, it may be easier to specify just the first dimension and not the second one.
The first dimension simply refers to the size which we can obtain by using out of zero.
This is because if we have any samples in, we also must have N samples out.
Clearly, the number of samples we pass into the input of a neural network must also be the number of
predictions that we get out.
The minus one means that PI talks will automatically calculate the leftover dimension, given whatever
dimensions are left.
Next, the third step is to pass the data through the dense layers and finally we return the output.
The next step is to instantiate the model we pass in one argument into the constructor, which is the
number of classes K.
In the next block of code, I show you how to build an equivalent CNN using the sequential module instead
of building a custom class, as you can see, it's much simpler, but still requires you to do convolutional
arithmetic to determine the first linear layer's input size.
You might want to try both models and confirm that they are equivalent.
The next step is to move our model to the GPU, which you already know how to do.
The next step is to create our laws and optimizer, which you already know how to do.
The next step is to create our data loaders, which, again, you already know how to do.
The next step is to create a training function, which, again, you already know how to do.
The only minor difference here is that I have some time in code and when each player completes, I print
the duration of that epoch.
So you can see here I'm printing out the pocket duration.
But now you see how important it is to understand what we were doing previously.
We'll be seeing that exact same code again and again.
All right, so training completed successfully at around six seconds per ibrc.
Next, we plot the lost per iteration, as usual.
So this appears to look at.
Next, we get the train and test accuracy using the same code we saw before.
As you can see, we do pretty well, about 94 percent on the train set and about 89 percent on the test
set, but this is definitely less than the accuracy we got on the original mass data set.
Next, we have our confusion matrix code.
So if we scroll down, we can see what our model is most confused by.
Unfortunately, the axes are labeled with integers, so it's hard to tell what's what, but we can see
if we look at the labels below what each of them means.
So it seems we often confuse zero with six and six with zero, zero corresponds to t shirt and top,
while six corresponds to shirt.
So it definitely makes sense that our model would confuse these.
In fact, one might argue that they are the same thing and that even humans would confuse these.
That's similar to what we saw with minus.
We can also see that sex gets confused with fallot.
As we saw six men shirt, and now we can see that four means coat.
This also makes sense given the very low resolution of the images, basically the little white blobs
on a black background.
We would expect that a court has a similar shape to a shirt.
We also see that two gets confused with forensics as well, pretty often to Stanzler pullover, which
again is pretty much a shirt, especially when it's just a 28 by 28 blob.
On the other end of this, we see that seven often gets confused with nine and vice versa.
Seven corresponds to sneaker and nine corresponds to include.
So, again, considering that the images are just little blobs, this makes total sense.
Let's now look at some misclassified samples to check if our reasoning holds.
So the first one we see is a T-shirt top getting predicted as a shirt, which makes sense.
Here's a shirt predicted as a pullover that makes sense.
Here's a shirt protected as a coat, I wouldn't really consider that to be a coat.
Use a T-shirt predicted as a shirt, that makes sense.
It's a pullover, I think we saw this one.
Here's a sandal predicted as an ankle boot that makes sense.
Here's a quote predicted as a pull over.
That makes sense.
So you can see that all the things that are getting misclassified are generally the same.
OK, so as a final exercise for this lecture, what you should try is mix and match the models and data
that we've seen so far.
Previously, we used an anonymous and now we're using a CNN en fashion Imust.
You should have some intuition that a CNN should perform better than an in it, but also that fashion
amnesty is a harder classification problem than amnesty.
So what will happen if you try to use in an N on fashion amnesty?
What kind of results do you expect to see what will happen if you try to use a CNN on amnesty?
What kind of results do you expect to see?
As always, remember my rule machine learning is experimentation and not philosophy.
Whenever you want to know the outcome of running some computer code, never try to guess the outcome
with your mind.
Well, in this case, it's OK because I've asked you to in order to check whether your intuition is
correct.
But what you really want to do is write these scripts in code and see the answer for yourself.