MORE

Convolutional neural networks

Use Python to implement a simple network that classifies handwritten digits

At some point in your daily life, you’ve probably seen some form of an object recognition algorithm in action, for example, face detection on your phone’s camera. How does this work, though? At the heart of computer vision solutions such as these are convolutional neural networks (CNN). Simply put, these are neural networks that are particularly adept at building complex features from less complex ones. A classic example is a face detector, where early layers pick out vertical and horizontal lines while later stages find noses and mouths.

This article explains how these convolutional nets work. It also shows how to use Python to implement a simple network that classifies handwritten digits. So, let’s jump straight in!

Primer on neural networks

This article does not go into detail on how neural nets work in general, but you do need a little background before tackling convolutional nets. Neural networks have a layered architecture. Each layer comprises some number of nodes, and each node effectively conducts some mathematical operation on some input to calculate an output. The input to any given node is a weighted sum of the outputs of the previous layer (plus a bias term usually equal to one or zero). It is these weights that are learned by the algorithm during training. To learn these parameters, the output of a training run is compared with the true value, and the error is backpropagated through the network to update the weights.

Convolution

Convolution is a mathematical operation, where a function is “applied” in some manner to another function. The result can be understood as a “mixture” of the two functions. Convolution is represented by an asterisk (*), which might be confused with the * operator that is generally used for multiplication in many programming languages.

How does this help detect objects in an image, though? Well, it turns out that convolutions are really good at detecting simple structures in an image, and then putting those simple features together to construct even more complex features. In a convolutional network, this process occurs over a series of many layers, each of which conduct a convolution on the output of the previous layer.

So what sort of convolutions do you use in computer vision? To understand that, you must first understand exactly what an image is. An image is an array of bytes, either Rank 2 (two-dimensional, having a width and a height) or Rank 3 (three-dimensional, with width, height, and more than one channel). So a grayscale image is Rank 2, while an RGB image is Rank 3 (with three channels). The values of the bytes are simply interpreted as integer values, describing the amount of that particular channel that must be used on the corresponding pixel. So basically, when dealing with computer vision, you imagine an image as a 2D array of numbers (for an RGB or RGBA image, three or four such arrays overlaid on each other).

Therefore, my convolution takes this array (I am going to assume that the image is grayscale for now) and convolves it with a second array, called a filter. The convolution process proceeds as follows. First, the filter is overlaid in the upper left of the image array. Next, the elementwise product of the filter is taken with the subsection of the image over which the filter currently lies. That is, the upper-left element of the filter is multiplied by the upper-left element of the image, and so on. These results are then added to produce one value. The filter is then moved along the image by a distance that is called the stride, and the process is repeated. The output of this is a new array, of different dimensions than the image array (usually the result has a smaller width and height, but more channels). To illustrate how this works, look at an example. Here is a 3 x 3 filter:

The following image is the one I’ll apply this filter to.

After applying one pass of the filter to this image, I get the following result.

Hopefully, you can see that the filter seems to be arranged with the values going vertically. This means that it picks out vertical features in the image, as you can see in the result. It is the filter values that are learned when running a CNN.

It should be noted that the stride and filter size are hyperparameters, meaning that they are not learned by the model. So you must apply your scientific mind to work out what values of these quantities will work best for your application.

A final concept that you need to understand about convolution is the idea of padding. If your image won’t fit the filter in an integer number of times (with the stride taken into account), then you must pad the image. There are two ways of doing this: VALID padding and SAME padding. The former basically drops any remaining values on the edge of the image. That is, if the filter is 2 x 2 with a stride of 2, and the image has a width of 3, then VALID padding ignores the third column of values from the image. Meanwhile, SAME padding adds values (usually zeros) to the edges of the images to increase its dimension until the filter can fit an integer number of times. This padding is generally done symmetrically (that is, it tries to add the same number of columns/rows on either side of the image).

It is also interesting to note that image convolutions have uses not limited just to computer vision. A lot of image filtering techniques can be implemented using convolution, for example, blurring and sharpening.

The following basic Python code shows how the convolution operation works (you could make this neater by using numpy, for example):

Note that if you want to write the result out to an image file (to visualize it like I did above), then you must clamp the output values so that they do not exceed 255.

Pooling and fully connected layers

Real convolutional networks are rarely built from just convolutional layers. They usually have other types of layers, too. The simplest is the fully connected layer. This is just a normal neural network layer where all of the outputs of the previous layer are connected to all of the nodes on the next layer. Typically, these layers come toward the end of the network.

The other key type of layer you will see in convolutional nets is the pooling layer. This comes in a few forms, but the most commonly used is max pooling, in which the input matrix is split into equal-sized segments, and the maximum value in each segment is taken forward to fill the corresponding element of the output matrix.

In the code listing above, the input was split into 2 x 2 quadrants, and max pooling was applied. Therefore, I can describe this particular operation as having a filter of dimension 2 and a stride of 2. What this process has done is picked out the broad sectors in which a feature lies. Imagine that this network is looking for faces. In this case, you can interpret the result of this pooling as showing that there is a strong possibility of there being a face in the lower right, some possibility of a face in the upper left, and that there is likely no face in the upper right or lower left.

Example background

Now, let’s work through a simple computer vision problem by making a network that identifies handwritten digits in the image. This is one of the most common baseline examples used to show off the power of neural nets. The example is written in Python, using the TensorFlow library, so that you do not have to focus too much on specific implementation details and can instead look more at the overall architecture. TensorFlow has another benefit. It provides the MNIST data set built-in, though it should be noted that other machine learning frameworks (such as SciKit-Learn) also do this.

For training and testing, I use this MNIST data set. I use a relatively simple convolutional network architecture based on LeNet-5. This achieved an error rate of 0.9% on the MNIST set, though I won’t hit quite that level of accuracy because I’ll be foregoing a lot of the manipulations that LeCun and others conducted to make the network perform better, and I will be simplifying certain aspects of the architecture.

The full code can be found at my GitHub repository. This repository also includes the code for demonstrating the earlier edge-detection image filter.

Architecture

The architecture that I will be using is as follows:

A convolutional layer, reducing the 32x32x1 MNIST image to a 28x28x6 output

A max-pooling layer, halving the width and height of the features

A convolutional layer, bringing the dimensions to 10x10x16

A max-pooling layer, again halving the width and height

A fully connected layer, bringing the number of features down from 400 to 120

A second fully connected layer

A final fully connected layer, outputting a vector of size 10

Each intermediate layer uses a ReLU nonlinearity and each convolutional layer uses a 5×5 filter with a stride of 1 and VALID padding. Meanwhile, the max-pooling filter has dimension 2.

Helper methods

The code has several helper methods that abstract away some of the details that are repeated in the architecture, such as creating filters (each filter is 5×5, but has different depths) and convolutional layers. Note that I use a truncated Gaussian distribution in the weights initialization because it does not matter what the weights start as, so long as they are not all identical. This is to break symmetry. The following code shows an example of one of the helper methods.

The training labels are reformed as a one-hot vector. A one-hot vector is a vector where each element represents a class, and the element is equal to one if the example belongs to that class, and is equal to zero otherwise. So for an image depicting the number 1, the one-hot representation would be:

I then set up the operations for defining how I am going to train the model, for example, defining what quantity I want to minimize during the training.

I use the cross-entropy as my measure of loss, and use the AdamOptimiser to conduct the optimization.

The training set is split into batches, of size 128, and I run the training over some number of epochs (ten in this case). After each epoch, the resulting set of weights is checked against the cross-validation set.

The resulting model is then saved out to be used later on the test set.

Performance

When run against the test set, the model obtained an accuracy of 98.5% (a 1.5% error rate). This is a slightly lower performance than LeCun’s implementation due to differences in data preparation and so on. However, for a relatively simple implementation such as mine, this is quite a high performance.

Conclusion

In this article, you learned the basics of convolutional neural networks, including the convolution process itself, max pooling, and fully connected layers. You then looked at an implementation of a comparatively simple CNN architecture. Hopefully, this will help you understand CNNs when you use them in your work, or help start your journey into learning about this fascinating branch of machine learning.