Convolutional Neural Networks

A convolutional neural network (CNN) is a network built mainly from convolutional layers. Its design follows three principles:

  • Locality: small filters look only at small neighborhoods.
  • Symmetry: weight sharing builds in translation equivariance, and data augmentation teaches the other symmetries.
  • Hierarchy: depth and processing at several resolutions let parts combine into wholes.

A classification CNN is a series of stages. Each stage applies a few convolutions with ReLU activations and then downsamples by pooling or a stride-2 convolution. From stage to stage the height and width halve and the number of channels doubles. At the end, the feature map is flattened or globally average-pooled, passed through one or more dense layers, and turned into class probabilities by a softmax. A network that must output an image, such as a per-pixel segmentation, adds a mirrored decoder that upsamples back to the input size. The downsampling half is then called the encoder, and the whole is an encoder-decoder or hourglass network.

AlexNet (2012) was the first CNN to win the ImageNet challenge, a 1000-class classification task on RGB images. It has five convolutional layers and three dense layers, all with ReLU activations:

  • An convolution with stride gives .
  • After a max pool, a convolution gives .
  • After another max pool, three convolutions give , and .
  • A final max pool gives , a vector of length .
  • Dense layers of sizes , and follow.

AlexNet has about million parameters, most of them in the dense layers. It was trained with data augmentation that enlarged the dataset by a factor of , dropout in the dense layers, weight decay, and SGD with momentum . Its error rates were top-5 and top-1.

VGG (2014) uses only convolutions and max pools. Its feature maps go from through , and to . A last max pool gives , a vector of values, and three dense layers follow. With layers and million parameters it reached top-5 and top-1 error. The main change from AlexNet was depth. Adding still more layers made both training and test error worse, which motivated residual networks.

Two Uses of the Word Encoder

In a CNN the encoder is the downsampling half of one network, and the decoder is the upsampling half. In transformer encoder-decoders the same words name two stacks of attention layers. Both are compressed internal representations, but the mechanisms differ.

Intuition

Every architectural choice in a CNN encodes a belief about images. Small filters say that nearby pixels matter most. Weight sharing says that a pattern means the same thing wherever it appears. Stacking layers says that images are built from parts: edges form textures and corners, which form object parts, which form objects. A network that holds these beliefs searches a much smaller set of functions than a dense network of the same size. When the beliefs are true, as they are for natural images, it learns from far fewer examples. This prior knowledge built into an architecture is called its inductive bias.

The figure shows the standard shape. Each stage trades resolution for channels. Early stages have large maps with few channels and detect small local patterns. Later stages have small maps with many channels, and each of their units sees a large part of the image. The last stage forgets position altogether and reports which features are present, which is what a classifier needs. Images processed this way flow through a funnel. A decoder for per-pixel outputs runs the funnel in reverse.

\usepackage{tikz}
\begin{document}
\definecolor{hot}{HTML}{D9483B}
\definecolor{cold}{HTML}{2F80ED}
\begin{tikzpicture}[x=1cm,y=1cm,>=latex]
\draw[thick] (0,-1.6) rectangle (0.25,1.6);
\node[below] at (0.12,-1.7) {\small $224^2 \times 3$};
\draw[cold,very thick] (1.6,-1.3) rectangle (2.1,1.3);
\node[below] at (1.85,-2.2) {\small $112^2 \times 64$};
\draw[cold,very thick] (3.3,-0.9) rectangle (4.1,0.9);
\node[below] at (3.7,-1.7) {\small $56^2 \times 128$};
\draw[cold,very thick] (5.3,-0.6) rectangle (6.5,0.6);
\node[below] at (5.9,-2.2) {\small $28^2 \times 256$};
\draw[cold,very thick] (7.7,-0.35) rectangle (9.4,0.35);
\node[below] at (8.55,-1.7) {\small $14^2 \times 512$};
\draw[hot,very thick] (10.6,-0.12) rectangle (10.8,1.6);
\node[below] at (10.7,-2.2) {\small vector};
\draw[hot,very thick] (12.0,-0.12) rectangle (12.2,0.7);
\node[below] at (12.1,-1.7) {\small classes};
\foreach \a/\b in {0.3/1.55,2.15/3.25,4.15/5.25,6.55/7.65,9.45/10.55,10.85/11.95} \draw[->,thick] (\a,0) -- (\b,0);
\node[cold] at (4.8,2.1) {conv + ReLU, then pool};
\node[hot] at (11.4,2.1) {dense};
\end{tikzpicture}
\end{document}

Properties

(A Constrained Dense Network) A CNN can represent only a small part of what a dense network of the same size can, and the restriction is what makes it generalize.

Every convolutional layer is a dense layer with most weights fixed at zero and the rest tied together. Training a CNN is therefore like training a dense network under an infinite penalty on every solution that is not convolutional. On MNIST-1D, a 1D digit dataset whose templates are randomly shifted, a three-layer CNN with parameters fits the training set and reaches about test error. A dense network with the same layers and widths has parameters. It also fits the training set, but its test error is about . The dense network could represent the CNN exactly and still fails to find it. The CNN learns each digit once and reuses that knowledge at every position, while the dense network must learn each digit separately at every position.

(When the Bias Is Wrong) The convolutional prior hurts when the data lack local structure or when the built-in structure is too rigid.

Apply one fixed random permutation to the pixels of every image. A dense network loses nothing, because it can permute the weights of its first layer to match. A CNN loses the locality it relies on, since neighboring pixels in the permuted image are unrelated. It then does worse than the dense network, and smaller kernels can make it worse still. Too much built-in structure also hurts. Fixing the first layers to hand-designed edge detectors and training only the classifier may help when data are scarce. With plenty of data the frozen network does worse, because the fixed filters add bias that more data cannot remove.

(Most Parameters Sit in the Dense Layers) In AlexNet and VGG the convolutions do most of the computation, but the dense layers hold most of the parameters.

A convolution’s parameters do not depend on the image size, while its computation is proportional to the number of output positions. A dense layer is the opposite: one weight per input-output pair, each used once. AlexNet’s three dense layers hold about million of its roughly million parameters. Replacing the flattening step with global average pooling removes most of these weights. It also makes the network accept images of any size, since the pooled vector has one entry per channel regardless of the map size.

(Depth Helps Until Training Fails) Deeper CNNs classified better until about twenty layers, after which both training and test error rose.

From AlexNet’s layers to VGG’s , ImageNet error fell sharply, and the main change was depth. Plain CNNs with many more layers performed worse, and their training error was worse too. A worse training error means the problem is optimization, not overfitting. One explanation is that gradients become unpredictable as the number of layers grows. Residual connections and normalization fixed this and allowed networks with hundreds of layers.

(The Encoder-Decoder Tension) An image-to-image network needs both wide context and fine detail, and downsampling trades one for the other.

Labeling a pixel correctly often requires seeing far away. Downsampling provides that context cheaply. It also discards the high-resolution detail that decides exactly where an object’s boundary lies. In a plain encoder-decoder, the low-resolution middle of the network must somehow carry that detail to the decoder. Early segmentation networks used max unpooling to return a little of it. The U-Net instead copies each encoder feature map across to the decoder stage of the same resolution, so the detail never has to pass through the bottleneck.

Examples

(Easy) AlexNet's First Layer

AlexNet’s first layer applies filters of size with stride to a image and produces a map.

(Medium) Where AlexNet's Parameters Are

Use the layer sizes in the callout and assume every convolution sees all of its input channels.

(Medium) Why VGG Uses Only Small Kernels

A designer wants each unit to see a window and has channels in every layer.

(Hard) Scrambled Pixels

A dataset of images is preprocessed with one fixed random permutation of the pixel positions, applied identically to every training and test image. A dense network and a CNN with kernels are trained on the scrambled data.

See Also