Introduction to CNNs — Without using MNIST!
By: Pratik Kumar
Tags: Pytorch, Convolutional Network, Image Classification, Deep Learning, Python

Introduction 🍵

The MNIST dataset is the most overused dataset for getting started with image classification. MNIST dataset comprising of 10-class handwritten digits introduced by Yann LeCun in 1998 come up over and over again, in scientific papers, blog posts, and so on. It contains 28×28 (also 32x32) grayscale images of handwritten digits, each with integers between 0 and 9.

The reason MNIST is so popular has to do with its size, allowing deep learning practitioners to quickly check, train, and publish their algorithms. There are certain variations and limitations of MNIST.

The main objective is to come up with a fresh dataset to understand Image Classification using CNNs.

If not MNIST then what? 🤔

For the purpose of simple explanation, I have chosen Quick-Draw images by Google. These images are doodles that were generated in an A.I. experiment.

Know more about the dataset from this video,

You can also try Quick Draw by yourself here,

The data is formatted into various types. I have used the NumPy version (.npy files). These have been rendered into a 28x28 grayscale bitmap in NumPy format. Know more via its documents shared below,

Images and Classes used 🧐

The data set is big and has a wide range of classes. For the purpose of the demonstration, I have used 10 Classes and around 60,0000 images. Originally dataset is very big. If you are using Colab then it might crash while performing operations. The following is a visual representation of 10 classes considered.

The classes are :

'cloud', 'sun', 'pants', 'umbrella', 'table', 'ladder', 'eyeglasses', 'clock', 'scissors', 'cup'

I will share the complete code and script to change the number of images and classes. Please find it at the end of this post.

As you may notice this is somewhat similar to MNIST data as,

It has greyscale images (No RGB Channels)

28 x 28 dimension images

It has 10 Classes as MNIST has 0–9 classes.

Convolution Neural Networks 💪

The CNN yield better results in classifying images as compared to traditional linear and non-linear classification algorithms. This is because they reduce the number of parameters to learn.

The CNNs use convolutions of the image and the filter to extract complex invariant features at each layer. The complexity of features extracted increases with the depth of layers. They successfully capture the spatial and temporal dependencies within the image.

The complexity of Features —

Layer 1: edges

Layer 2: shapes

Layer 3: pattern of shapes

….

Output

To know more about what actually happens between the layers follow,

Components of CNNs 🛠

The following are the characteristics if Convolution Neural Networks,

1. Convolutions

A convolution is a type of matrix operation, consisting of a kernel, a small matrix of weights, that slides over input data performing element-wise multiplication with the part of the input it is on, then summing the results into an output.

These are basically merging two sets of information. Mathematically, convolution refers to the combination of two functions to yield a third function. In the case of CNN, convolution is the application of filters/kernels to an input image resulting in activations.

2. Pooling

Pooling layers provide an approach to downsampling feature maps by summarizing the presence of features in patches of the feature map. Two common pooling methods are average pooling and max pooling.

PyTorch Implementation 💻

The coding part is done using PyTorch. It can be divided into,

Defining model

Defining Hyperparameters

Training and Testing

1. Defining Model

We define a model or a CNN architecture using PyTorch’s nn module. Following is a neural network architecture with two convolution layers and two fully-connected layers.

class CNN(nn.Module):

def __init__(self, input_size, n_feature, output_size):

super(CNN, self).__init__()

self.n_feature = n_feature

self.conv1 = nn.Conv2d(in_channels=1,

out_channels=n_feature,

kernel_size=5)

self.conv2 = nn.Conv2d(n_feature, n_feature, kernel_size=5)

self.fc1 = nn.Linear(n_feature*4*4, 50)

self.fc2 = nn.Linear(50, 10)



def forward(self, x, verbose=False):

x = F.relu(self.conv1(x))

x = F.max_pool2d(x, kernel_size=2)

x = F.relu(self.conv2(x))

x = F.max_pool2d(x, kernel_size=2)

x = x.view(-1, self.n_feature*4*4)

x = F.relu(self.fc1(x))

x = F.log_softmax(x, dim=1)

return x

2. Defining Hyperparameters

The model dimensions taken are dependant upon input and output considered. CNN will take 784(= 28x28) as input and result in 10 classes.

The Loss function, I have chosen is Negative Log-Likelihood Loss. The optimization function is Stochastic Gradient Descent. This will update our network’s weights and biases. I have set the learning rate to 0.03 and momentum to 0.9.

model = CNN(784, 24, 10)

model = model.to(device)



#Loss criterian

criterion = nn.NLLLoss()



#Optimization Function/Weights and Bias Updating Rule

optimizer = optim.SGD(model.parameters(), lr=0.03, momentum=0.9)

3. Training and Testing

For training and testing follow the below-shared blog post. Here, I have trained and tested the network. This my personal blog site, where I have shared complete code and Jupyter Notebook. Also, I have explored dimensionality reduction techniques too.

My network is performing well with accuracy — 96.7% and Loss — 0.122. You can also run and test yourself the implementation through Colab.

Resources and References 📚

CS231n Jupyter Notebook — Code Blog Post — Doodle Images Classification for a complete explanation.

Thank you! 😇

Photo by Niclas Illg on Unsplash

Hope you liked it. Thank you for giving this post a read! Please do check the repository shared above and contact if in case the explanation needed. To know more about me visit my portfolio website here.

To conclude we have explored the CNNs and its implementations. Further, you may explore capsule networks and RNNs.

You may also follow my blog post shared, where I have explored t-SNE and PCA representations of this data as well. Feel free to reach me out regarding any doubts in this post.

Thank-you!