Path: blob/master/notebooks/book1/14/cnn_cifar_pytorch.ipynb
1192 views
CNN for image classification using PyTorch
In this section, we follow Chap. 8 of the Deep Learning With PyTorch book, and illustrate how to fit a CNN to a two-class version of CIFAR. We modify the code from here
Get the data
We standardize the data. (The mean/std of the RGB channels are precomputed in the MLP version of this colab.)
Basics of convolution
Lets apply a set of nfeatures
convolutional kernels to a gray scale image.
Now we adding padding to ensure output size is same as input.
Currently the filter parameters are random.
Let's make the first filter just be an averaging operation.
Let's make the first filter be a vertical edge detector.
Max-pooling
We can reduce the size of the internal feature maps by using max-pooling.
Making our first CNN
Training loop
Validation accuracy
Save/load model
Dropout
We can use dropout as a form of regularization. Let's see how it works for a single convolutional layer. We pass in a single image of size 1x3x32x32 and get back a tensor of size 1x10x16x16, where 10 is the number of filters we choose, and size 16 arises because we use maxpool of 2.
In training model, the model is stochastic.
In testing model, the model is deterministic, since dropout is turned off. This is controlled by the fact that the dropout layer inherits state from the parent nn.Module.
MNIST
We've written the model definition to work with images with any number of input channels, including just 1 (i.e., gray-scale images). It can also handle any number of output classes. Let's check that it works on 10-class gray-scale MNIST.