The chapter opens with a definition from DeepAI: “A Convolutional Neural Network (CNN) is a deep learning neural network designed for processing structured arrays of data such as images.” Today CNNs are used to solve identity recognition, image classification, object detection, scene labeling, visual search, action recognition, document analysis, anomaly detection and video analysis.
But first, why not just use an MLP? The slides give three drawbacks, and the first one is arithmetic.
MLPs use one neuron for each input. Take a modest image: 600×400 pixels with 3 colour channels. Flattened, that is
600 · 400 · 3 = 720 000 input neurons
Connect it to a hidden layer of just five neurons and you already have
720 000 · 5 = 3.6 million weights (for a single layer)
The amount of weights rapidly becomes unmanageable for large images.
MLPs react differently to an image and its shifted version, because they are not translation invariant. The same cat, moved ten pixels to the right, hits an entirely different set of weights.
Most important of all: spatial information is lost when the image is flattened into an MLP. Pixels that are close together matter, because they help to define the features of an image; flattening throws that adjacency away and the network has to rediscover it from scratch.
In 1968, D. H. Hubel and T. N. Wiesel demonstrated that mammals visually perceive the world around them using a layered architecture of neurons in the brain. As information passes from our eyes to the brain, higher and higher order representations are formed.
Within the visual cortex, complex functional responses generated by complex cells are constructed by combining more simplistic responses from simple cells:
| Cell type | Responds to | Property obtained |
|---|---|---|
| Simple cells | Edges with a specific orientation in a particular position (called the receptive field) | Orientation selectivity |
| Complex cells | Edges with a specific orientation regardless of the position where they are located | Spatial invariance |
Spatial invariance is obtained by “summing” the contribution of simple cells responding to the same orientation but with different receptive fields. Hold on to that sentence: it is a description of pooling, arrived at thirty years before pooling layers were invented.
This is the inspiration behind CNNs. Higher and higher representations are formed through the layers:
CNNs were introduced in 1998 by Y. LeCun and Y. Bengio. Their architecture is based on local connections, layering and spatial invariance. The main differences compared with an MLP:
| Property | What it means | What it buys |
|---|---|---|
| Local connections | Neurons are only locally connected to neurons of the previous level. | A strong reduction of the number of connections. |
| Shared weights | Different neurons of the same level perform the same operation on different portions (receptive fields) of the input. | A strong reduction of the number of weights. |
| Alternation of feature extraction and pooling layers | Convolution and pooling alternate through the network. | Progressive spatial reduction. (The slides note this is no longer true for the most recent CNNs.) |
A CNN is a combination of two basic building parts:
More formally, a CNN is a sequence of layers, and every layer transforms one volume of activations to another through a differentiable function. Three main types of layers build the architecture:
| Layer | What it does |
|---|---|
| Convolutional | Contains a set of learnable filters. The width and height of the filters are smaller than those of the input volume. The filter slides across the input and the Frobenius inner products between the input and the filter are computed at every spatial position. |
| Pooling | Reduces the number of parameters and computation by down-sampling the representation. |
| Fully-connected | Neurons have full connections to all activations in the previous layer, as in traditional ANNs. |
Convolution is one of the most important image processing operations. A filter strides across the width and height of the input and the Frobenius inner product between the filter and the input is computed at each position — the operation defined in Chapter 2, now put to work.
The slides show that classical image filters are exactly this operation with hand-chosen numbers: an edge detection kernel and a sharpen kernel are just two different 3×3 matrices slid over the image. The insight of a CNN is that those numbers do not need to be chosen at all.
A convolutional layer is a bank of classical image filters whose coefficients are trainable parameters. Instead of a human deciding that this 3×3 matrix detects vertical edges, backpropagation discovers which filters are useful for the task.
In a CNN, the objective of the convolution operation is to extract features from the input volume while maintaining the spatial relationship between input elements. Each convolutional layer contains a set of filters (or convolutional kernels).
The shape contract:
| Object | Shape |
|---|---|
| 3D input volume (for example an RGB image) | WI × HI × C |
| A set of K kernels | F × F × C each |
| Output volume | WO × HO × K, composed of K feature maps of size WO × HO |
The slides give a concrete instance: WI = HI = 32, C = 3, K = 10, F = 5, giving WO = HO = 32.
Local connections and shared weights ensure that different portions of the input volume are processed in the same way. This is a desired behaviour, since different field-of-view regions contain the same type of information.
This distinction is the heart of the chapter. Each element of the output volume is connected to a number of input elements equal to the size of the filter, so the total number of connections is:
(WO · HO · K) · (F · F · C)
But the total number of weights is much smaller, since the weights of each filter are shared by all the elements contained in the same feature map:
(F · F · C + 1) · K (the +1 is the bias, if present)
Being asked to count parameters is almost guaranteed. Keep the two formulas apart: connections scale with the output resolution WO·HO, weights do not. Weight sharing is what decouples the parameter count from the image size, and it is the property an MLP lacks.
Four hyper-parameters control the size of the output volume:
| Hyper-parameter | Definition |
|---|---|
| Filter size | The dimensions of the filter (F) applied to the input volume. |
| Depth | The number of filters (K), each learning to look for something different in the input, such as oriented edges or blobs of colour. |
| Stride | The amount of movement between applications of the filter. It reduces the spatial size of the output volume and consequently the number of connections. |
| Padding | Adds a border to the input volume to obtain a specific spatial size. It allows us to control the spatial size of the output volume. |
They combine into one formula, which you should be able to write from memory:
WO = (WI − F + 2 · Padding) / Stride + 1
The slides take a 5×5 input holding the values 0 to 24 and the kernel
0 1 0
0 1 0
0 1 0
which simply sums the middle column of each 3×3 window, and run it three ways:
| Configuration | Output | Size check |
|---|---|---|
| Stride 1, Padding 0 | 18 21 24 |
WO = (5−3+2·0)/1 + 1 = 3 |
| Stride 2, Padding 0 | 18 24 |
WO = (5−3+2·0)/2 + 1 = 2 |
| Stride 2, Padding 1 | 5 9 13 |
WO = (5−3+2·1)/2 + 1 = 3 |
Reproduce all three below, then change the kernel and watch which structures each one responds to.
When the input volume contains multiple channels (C), we use convolutional kernels with the same number of channels. To yield a two-dimensional feature map for each kernel:
The slides work a two-channel example with 2×2 kernels: the first channel produces
19 25 37 47
37 43 and 67 77
for the second, and adding them element by element gives the single feature map
56 72
104 120
A common misconception is that a kernel produces one feature map per channel. It does not. One kernel produces exactly one feature map, however many channels the input has, because the per-channel results are summed. The depth of the output volume equals K, the number of kernels — never C.
The receptive field of a particular feature map unit is the size of the region in the input that influences its activation. In other words, it indicates how much of the input image contributes to the computation of a specific feature at any layer of the CNN.
This is the quantity that grows as you stack layers: a unit two convolutions deep sees a wider patch of the original image than a unit one convolution deep, which is what allows later layers to represent larger structures.
A CNN autonomously learns the kernel weights during the training process. Empirically:
Which is precisely the simple-cell to complex-cell progression Hubel and Wiesel described in the visual cortex.
Two observations motivate pooling. First, our ultimate task usually asks some global question about the image, so the units of the final layer should be sensitive to the entire input. Second, when detecting lower-level features such as edges, we often want our representations to be invariant to translation.
A pooling layer therefore serves a dual purpose:
Mechanically, a pooling operator consists of a fixed-shape window that slides over all regions in the input, computing a single output for each location. The crucial difference from convolution:
Unlike convolutional layers, the pooling layer contains no trainable parameters. It is not a trainable layer but deterministic, typically calculating either the maximum or the average value of the elements in the pooling window.
The slides work both on the same 4×4 input with a 2×2 window and stride 2:
| Input | Max pool 2×2, stride 2 | Avg pool 2×2, stride 2 |
|---|---|---|
1 1 2 4 |
6 8 |
3.25 5.25 |
The average-pooling panel of the deck reads 3.25, 5.25, 2.50, 2.00. The first two are exactly the window averages ((1+1+5+6)/4 = 3.25 and (2+4+7+8)/4 = 5.25), but the bottom-left window (3, 2, 1, 2) averages to 2.00, not 2.50, so the third figure is most likely a transcription artifact of the slide extraction. The widget below computes every window from the definition, so you can check any of them yourself.
The trade-off between the two operators:
| Operator | Behaviour |
|---|---|
| Max pooling | Also performs as de-noising, by discarding the noisy activations. Usually max pooling performs better than average pooling. |
| Average pooling | Simply performs dimensionality reduction as a noise suppressing mechanism. |
Two structural facts: the pooling layer operates independently on every channel of the input volume, keeping the depth size unchanged; and, as with convolutional layers, pooling layers can change the output shape by padding the input and adjusting the stride.
Usually the fully-connected part is a simple MLP, consisting of two or three hidden layers and an output layer, that performs the classification among a large number of categories.
The bridge between the two halves is flattening: the output volume of the convolutional part, of size W × H × C, becomes a vector of
m = W · H · C
elements x1, …, xm, which are the inputs of the MLP.
Why bother with dense layers at all after all that convolution? Because adding fully-connected layers is a way of learning non-linear combinations of the high-level features represented by the output of the convolutional part.
Generally, the output layer is implemented using softmax as activation function, to ensure that the sum of the outputs is 1:
softmax(y)i = eyi / ∑j=1n eyj, y ∈ ℝn
Notice that flattening happens here too — the very operation criticised in Section 1. The difference is when: an MLP flattens raw pixels and destroys spatial structure before any processing; a CNN flattens only after the convolutional part has already exploited that structure and reduced the volume to a compact set of high-level features.
Everything so far assumed images, but the machinery generalises. The key difference between 2D and 1D convolutional layers is the dimensionality of the input/output data and how the filter slides across the data:
| 2D convolutional layer | 1D convolutional layer | |
|---|---|---|
| Input and output | 3-dimensional | 2-dimensional |
| Filter | 3D filter, moves in 2 directions | 2D filter, moves in 1 direction |
1D CNNs are very effective at extracting features from shorter, fixed-length segments of the overall dataset, and in cases where it is not so important where the features are located in the segment. They work well for the analysis of time sequences or any kind of signal data over a fixed-length period, such as accelerometer data or audio signals. Recently 1D CNNs have also been applied to problems in natural language processing, obtaining interesting results.
(1) MLPs use one neuron for each input, so the amount of weights rapidly becomes unmanageable for large images: a 600×400 RGB image flattens to 720 000 input neurons, and a five-neuron hidden layer already costs 3.6 million weights. (2) MLPs react differently to an image and its shifted version, because they are not translation invariant. (3) Most important, spatial information is lost when the image is flattened, and pixels that are close together matter because they define the features of an image.
In 1968 they demonstrated that mammals perceive the world using a layered architecture of neurons, with higher and higher order representations formed as information passes from the eyes to the brain. Simple cells respond to edges with a specific orientation in a particular position (the receptive field); complex cells respond to edges with a specific orientation regardless of position, obtaining spatial invariance by summing the contributions of simple cells with the same orientation but different receptive fields.
Local connections: neurons are only locally connected to the previous level, strongly reducing the number of connections. Shared weights: different neurons of the same level perform the same operation on different receptive fields, strongly reducing the number of weights. Alternation of feature extraction and pooling layers, which progressively reduces the spatial resolution — though the slides note this is no longer true for the most recent CNNs.
The Frobenius inner product between the filter and the corresponding patch of the input: the sum of the products of corresponding elements, over all axes of the patch. The filter slides across the width and height of the input, computing that scalar at every spatial position to build the feature map.
Connections: (WO · HO · K) · (F · F · C). Weights: (F · F · C + 1) · K, where the +1 accounts for the bias if present. The weights are far fewer because the weights of each filter are shared by all the elements contained in the same feature map.
WO = (WI − F + 2 · Padding) / Stride + 1. Here: (5 − 3 + 2·1)/2 + 1 = 4/2 + 1 = 3, so the output is 3×3. With the same filter at stride 2 and padding 0 it would be (5−3+0)/2 + 1 = 2.
We use kernels with the same number of channels as the input. A convolution is performed between each input channel and the corresponding channel of the kernel, and then the C results are added all together, summing over the channels. The result is a single two-dimensional feature map per kernel, so the output depth equals the number of kernels K, not the number of input channels.
The receptive field of a particular feature map unit is the size of the region in the input that influences its activation — how much of the input image contributes to the computation of a specific feature at any layer of the CNN. It grows with depth, which is what lets deeper layers represent larger structures.
It aggregates information to reduce the spatial resolution while maintaining dominant features (reducing parameters, computational power required and the risk of overfitting), and it mitigates the sensitivity of convolutional layers to location, obtaining approximate translation invariance. It has no trainable parameters: it is deterministic, typically taking the maximum or the average of the window.
Max pooling also acts as de-noising, by discarding the noisy activations; average pooling simply performs dimensionality reduction as a noise-suppressing mechanism. Usually max pooling performs better. Both operate independently on every channel, keeping the depth unchanged.
It learns non-linear combinations of the high-level features represented by the output of the convolutional part, and performs the classification among a large number of categories. It is typically an MLP of two or three hidden layers plus an output layer that uses softmax, so that the outputs sum to 1. Its input is the flattened volume of m = W · H · C elements.
For time sequences or any signal data over a fixed-length period, such as accelerometer data or audio signals, and recently also for natural language processing. 1D CNNs are effective at extracting features from shorter fixed-length segments where it is not so important where the features are located in the segment. Structurally, input and output are 2-dimensional and the filter moves in only 1 direction, against 3-dimensional data and 2 directions of movement for a 2D layer.