Section 4.1
Convolution

A convolution is a mathematical operation applied to two functions (or signals) that produces a third function, and is typically used to describe filtering of a signal. It can be applied to functions of any shape. We will start with the one-dimensional case. Let \(x \in \reals^n\) be an input signal and \(a \in \reals^p\) be a filter (or kernel). We write the convolution operation as1

\begin{align} y &= a * x & (\in \reals^{n-p+1}) \tag{144}\\ y_i &= \sum_{j=1}^{p} a_{j} x_{i+j-1} & \text{for $i = 1, \ldots, n - p + 1$} \tag{145}\end{align}

A worked example for a one-dimensional convolution operation is shown in Figure 34. Notice that the output signal is shorter than the input signal. Specifically, for input signal of length \(n\) and kernel of length \(p\), the output signal will have length \(n - p + 1\). We will address a strategy of how this can be avoided later in the lecture.

Worked example of a one-dimensional convolution operation
Figure 34: Worked example of a one-dimensional convolution operation, \(y = a * x\).

Instead of writing the convolution operation as an explicit summation, we can write it as a matrix-vector multiplication,

\begin{align} \begin{bmatrix} y_1 \\ y_2 \\ \vdots \\ y_{n-p+1} \end{bmatrix} &= \begin{bmatrix} a_1 & \ldots & a_p & 0 & \ldots & 0 \\ 0 & a_1 & \ldots & a_p & \ldots & 0 \\ \vdots & & \ddots & & \ddots & \vdots \\ 0 & \ldots & 0 & a_1 & \ldots & a_p \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \\ \vdots \\ x_n \end{bmatrix} \tag{146}\end{align}

or in a more compact form,

\begin{align} y &= A x \tag{147}\end{align}

Here the matrix \(A\), formed by rows containing shifted version of \(a^T\), is called a Toeplitz matrix. Viewing convolution in this way we see that convolution is a type of linear operator with weight sharing (i.e., sharing of parameters).

Convolutions on two-dimensional signals (or feature maps) follow much the same pattern as one-dimensional convolutions. Let \(X \in \reals^{n \times m}\) be an input signal and \(A \in \reals^{p \times q}\) be a filter. Then

\begin{align} Y_{ij} &= \sum_{k=1}^{p} \sum_{\ell=1}^{q} A_{k,\ell} X_{i+k-1,j+\ell-1} \tag{148}\end{align}

for \(i = 1, \ldots, n - p + 1\) and \(j = 1, \ldots, m - q + 1\). Note that here the kernel is also two-dimensional and not only slides left-to-right over the signal, but also top-to-bottom, i.e., the kernel shifts over the plane. An illustration of two-dimensional convolution is shown in Figure 35. Once again, the output signal is smaller than the input signal.

Two-dimensional convolution, , shifts the kernel over the plane
Figure 35: Two-dimensional convolution, \(Y = A * X\), shifts the kernel over the plane.

Now, recall from the first lecture that a feature map can have multiple channels. For example, an \(n\)-by-\(m\) colour image has three channels, \(X_R, X_G, X_B \in \reals^{n \times m}\), for red, green and blue colour information, respectively.

We can produce a feature map \(Y \in \reals^{(n-p+1) \times (m-q+1)}\) by convolving each channel with a filter \(A_R\), \(A_G\) and \(A_B\), and summing the output,

\begin{align} Y &= A_R * X_R + A_G * X_G + A_B * X_B \tag{149}\end{align}

as depicted in Figure 36.

Two-dimensional convolution over multiple channels
Figure 36: Two-dimensional convolution over multiple channels, \(Y = A_R * X_R + A_G * X_G + A_B * X_B\).

This is equivalent to stacking the channels into a 3-tensor and convolving with a 3D kernel. Convolutions on images and feature maps are defined as such. Let \(X \in \reals^{n \times m \times d}\) be an input signal and \(A \in \reals^{p \times q \times d}\) be a filter. Then

\begin{align} Y_{ij} &= \sum_{p'=1}^{p} \sum_{q'=1}^{q} \sum_{d'=1}^{d} A_{p',q',d'} X_{i+p'-1,j+q'-1,d'} \tag{150}\end{align}

for \(i = 1, \ldots, n - p + 1\) and \(j = 1, \ldots, m - q + 1\). This is illustrated in Figure 37.

Convolutions over images and feature maps are really three-dimensional convolutions with the input and kernel have the same number of channels
Figure 37: Convolutions over images and feature maps are really three-dimensional convolutions with the input and kernel have the same number of channels.

Observe that the input signal and kernel are both 3-dimensional tensors, but with same size third dimension (i.e., same number of channels \(d\)). This means that the filter kernel does not shift along the third dimension and the output signal only has one channel. Confusingly, this is called Conv2d in PyTorch.

So far we have considered a single convolutional filter applied to the input signal or feature map. Convolutional layers in deep learning apply multiple filters and stack each of their single-channel outputs into a multi-channel tensor as illustrated in Figure 38. So, in general, we have as input a (\(C_{\text{in}} \times H \times W\))-tensor and apply \(K\) convolutional filter kernels, \(\{A^{(1)}, \ldots, A^{(K)}\}\), which are learned parameters of the layer. Here each \(A^{(k)}\) has shape \(C_{\text{in}} \times P \times Q\). Then, with appropriate padding (to be discussed shortly in Section 4.1.1), the layer will produce as output a (\(C_{\text{out}} \times H \times W\))-tensor with \(C_{\text{out}} = K\).

Applying multiple convolutions to the input to generate a multi-channel output. Here we show five convolutions being applied to a three-channel input, producing
Figure 38: Applying multiple convolutions to the input to generate a multi-channel output. Here we show five convolutions being applied to a three-channel input, producing a five-channel output, one from each convolutional kernel.

4.1.1 Padding, Stride, Dilation, and Bias

It is a little annoying that the convolution operation results in an output feature map that is smaller than the input feature map, i.e., reduces the signal size. This can be addressed by padding the input signal, typically done by appending zeros to the beginning and end of the signal, although other values are possible (such as cyclic padding or extending the end values). Given a filter kernel of length \(p\), appending \(p-1\) values will result in an output signal the same size as the input signal as shown in Figure 39.

Other variations of convolution are also possible. For example, we can change how far we move the convolutional filter for each successive output element. This is known as stride, and can be expressed mathematically for the one-dimensional case as,

\begin{align} y_i &= \sum_{j=1}^{p} a_{j} x_{s(i-1)+j} & \text{for $i = 1, \ldots, \left\lfloor \frac{n - p + 1}{s} \right\rfloor$} \tag{151}\end{align}

for a stride of \(s\). This reduces the size of the output signal by roughly a factor of \(s\) and, similarly, reduces computation. Figure 39 shows an example for \(s = 2\).

Instead of skipping inputs as we move to the next output, we can also skip inputs when multiplying by filter coefficients. This is called dilation or (atrous) convolutions, and is equivalent to having a larger filter kernel with zeros inserted, but computationally more efficient. Mathematically, we have

\begin{align} y_i &= \sum_{j=1}^{p} a_{j} x_{i+d(j-1)} & \text{for $i = 1, \ldots, n - dp + d$} \tag{152}\end{align}

for a dilation factor of \(d \geq 1\). Here \(d = 1\) indicates no dilation.

Last, it is standard to add a constant term, called a bias, to the result of the convolution, which can be written as,

\begin{align} y &= a * x + b \tag{153}\end{align}

for scalar parameter \(b\), which is learned during training along with the kernel parameters \(a\). The striking similarity to the affine transform, \(y = Ax + b\), within a multi-layer perceptron should not go unnoticed.

Results from (zero) padding, stride, dilation and bias shown going left-to-right then top-to-bottom for a one-dimensional convolution example
Figure 39: Results from (zero) padding, stride, dilation and bias shown going left-to-right then top-to-bottom for a one-dimensional convolution example.

4.1.2 Back-propagation through Convolutions

To make convolutions learnable we need to be able to compute the derivative of the loss with respect to the kernel \(a\). Consider again the one-dimensional convolution,

\begin{align} y_i &= \sum_{j=1}^{p} a_{j} x_{i+j-1} & \text{for $i = 1, \ldots, n - p + 1$} \tag{154}\end{align}

and observe that each \(a_j\) affects every \(y_i\). As such, when back-propagating to compute the gradient of the loss with respect to \(a\) we need to sum the contributions for all the \(y_i\)’s. We have,

\begin{align} \fracdd[L]{a_j} &= \sum_{i=1}^{n-p+1} \fracdd[L]{y_i} \fracdd[y_i]{a_j} \tag{155}\\ &= \sum_{i=1}^{n-p+1} \fracdd[L]{y_i} \cdot x_{i+j-1} \tag{156}\\ \therefore \; \fracdd[L]{a} &= \fracdd[L]{y} * x \tag{157}\end{align}

So it turns out that back-propagating through a convolutional layer in a deep learning network is itself a convolution operation!

Likewise, each \(x_k\) affects many \(y_i\). Here, however, the resulting expression has the convolution filter reversed

\begin{align} \fracdd[L]{x_k} &= \sum_{i=1}^{n-p+1} \fracdd[L]{y_i} \fracdd[y_i]{x_k} \tag{158}\\ &= \sum_{i=1}^{n-p+1} \fracdd[L]{y_i} \fracdd{x_k} \overbrace{\left(\sum_{j=1}^{p} a_{j} x_{i+j-1}\right)}^{y_i} \tag{159}\\ &= \sum_{i=1}^{n-p+1} \fracdd[L]{y_i} \cdot a_{\underbrace{k-i+1}_{\text{reversed}}} & \text{(setting $k = i+j-1$)} \tag{160}\\ \therefore \; \fracdd[L]{x} &= \fracdd[L]{y} * \textbf{rev}(a) \tag{161}\end{align}

Similar expressions can be derived for higher-order convolutions.

4.1.3 Pooling

In addition to convolution layers, convolutional neural networks include pooling layers that reduce the size of features maps by aggregating feature values over a small local window, which is stepped across the feature map. This not only saves computation in downstream convolution layers but also acts to compress information and make the network more robust to data perturbations and small shifts within the scene. There are two common pooling operations. Average pooling computes the average of feature values in the window, where as max pooling takes the maximum value, and is hence a non-linear operation. These pooling methods are illustrated in Figure 40.

Example average and max pooling operations for a simple feature map
Figure 40: Example average and max pooling operations for a simple \(4 \times 4\) feature map.

A \(2 \times 2\) window is very typical, with a stride of 2 (i.e., stepping the full window size), so that windows do not overlap. This results in downsizing a 2D feature map by a factor of four. Note that average pooling is the same as filtering (i.e., convolution) with a uniform kernel. For multi-channel feature maps, the pooling operation is applied separately on each channel.2

Since pooling layers do not have any learnable parameters we only need to be concerned with propagating gradients from the pooled outputs back to the inputs (so that parameters in earlier convolution layers can be learned). Calculating the gradients is fairly straightforward. Assuming that windows do not overlap (i.e., the stride is equal to, or greater than, the window dimensions), then for average pooling we have \(\fracdd[L]{x_i} = \frac{1}{N^2} \fracdd[L]{y_j}\) for window of size \(N \times N\) and where input \(x_i\) contributed to output \(y_j\). For max pooling the output gradient is copied to the argmax input and the other inputs get zero gradient, e.g., let \(y = \max\, \{x_i\}_{i=1}^{n}\). Then \(\fracdd[L]{x_i} = 0\) if \(y \neq x_i\) and \(\fracdd[L]{y}\) otherwise.3 If there are multiple inputs that tie for the maximum value then the gradient is split between them.

4.1.4 Convolutional Neural Networks

Architecture diagram of the famous AlexNet convolutional neural network for image classification (from [61]). The diagram shows the data shapes (i.e., tensors)
Figure 41: Architecture diagram of the famous AlexNet convolutional neural network for image classification (from [61]). The diagram shows the data shapes (i.e., tensors) at each stage of the network, together with convolutional filter size, and annotated with the pooling operation being performed. For example, the leftmost part of the diagram shows that the input to the network is a 224-by-224 pixel colour image, which padded to 227-by-227 (not indicated in the diagram) and then filtered by 48 convolutional kernels of size 11-by-11 with stride of 4 to produce a 48-channel, 55-by-55 element feature map. Each layer is followed by a ReLU. Max pooling is done using a 3-by-3 window with stride of 2. Processing through the convolutional layers is performed in two streams (for practical memory capacity reasons) and combined in the final fully-connected layers.

We now have all the ingredients necessary for constructing a convolutional neural network (CNN), which takes an image, i.e., \((3 \times H \times W)\)-tensor, and performs successive layers of convolutions, elementwise non-linear transforms (such as ReLU), and pooling. Early CNNs also performed features normalization, i.e., \(z \gets z / \|z\|_2\), but that is less common today. The final layers of a CNN take the last feature map, flatten it, and then process it through a multi-layer perceptron, also called fully-connected or dense layers in the context of CNNs. An architecture diagram for the famous AlexNet CNN [61] is shown in Figure 41. There are many other variants appearing in the literature and that we’ll see in future lectures.

As can be seen in the diagram for AlexNex, the feature maps have smaller spatial dimension in later layers of the network compared to earlier layers. If we map back the size of the convolutional filter kernels to the original image then we see that a small kernel at a later layer in the network is processing data from quite a large area in the image. This is known as the receptive field of the filter. Figure 43 illustrates the idea using three successive layers of 2-by-2 pooling on a 12-by-12 image. By the third layer, each element of the feature map corresponds to data from a 4-by-4 region of the image, and so a 3-by-3 kernel applied on the third-layer feature map would corresponds to a receptive field of 12-by-12 in the original image. Thus, deeper models allow for consideration of larger receptive fields, which are necessary for capturing context, but without the computational demand of larger filters.

The first and still most common use of convolutional neural networks is for image classification—that is, given an image, predict the class that it belongs to (from some pre-defined set of categories). Instead of directly producing a class label, CNNs produce a probability distribution over classes, using a softmax function on the final layer logits, from which the most likely class can be trivially obtained. So for a \(K\)-class problem, the CNN backbone maps from a (\(3 \times H \times W\))-tensor representing the image to a \(K\)-vector as shown diagrammatically below:

diagram

Here \(f\) represents the CNN model and \(\theta\) encapsulates all of the learnable parameters in the model, i.e., the convolution filter kernels and linear transforms in the final MLP layers. The size of the input to the CNN is fixed, and therefore images must be resized before processing. We will see later architectures that deal with inputs of varying sizes.

Common datasets used in image classification (see Figure 42) include MNIST [62], SVHN [76], CIFAR10/100 [60], ImageNet [21], Places365 [69], and many more. The first three are very small by modern standards and only used to try out new ideas, not in any real application. Training is done using stochastic gradient descent on the cross-entropy loss, and evaluation performed by reporting top-1 and top-5 accuracy on a held out test set. Here top-1 accuracy is the same as one minus the misclassification rate. Top-5 accuracy measures the number of times the true positive class appears within the set of five most probable classes (ranked by softmax probability, or equivalently, logit value). More nuanced metrics such as confusion matrices and precision-vs-recall will be discussed shortly.

Common datasets used for the image classification task
Figure 42: Common datasets used for the image classification task.

In addition to being a useful task in and of itself, image classification is often used as a test bed for developing new learning algorithms, and as a building block for more sophisticated image processing pipelines that we will see in later lectures.

Receptive field of convolutional layers in the image increases as feature maps are downsized
Figure 43: Receptive field of convolutional layers in the image increases as feature maps are downsized.

  1. 1. In the signal processing literature \(a\) is reversed before summing. But since in deep learning we learn the coefficients of the kernel it is more standard to use the definition provided here.
  2. 2. A early novel pooling method called max-out [32] operates across channels, but is less popular these days.
  3. 3. Technically this is called a subgradient, but the distinction is beyond the scope of this course.