This is a quick skim notes for CS231n Introduction to CNN lecture 7, I used slides from this lecture to create notes and this is NOT an attempt to replicate notes by cs231n, its already the best notes on CNNs out there.

CNNs are similar to ordinary neural networks, they have trainable weights and bias, receives input, bunch of trainable layers followed with non-linearity. Each layer is completely differentiable, means they can learn. At the end an output layer predicting classes or so.

So what changes? ConvNet architecture make the explicit assumption that the inputs are image which allows us to encode certain properties into the architecture. These then make the forward function more efficient to implement and vastly reduce the amount of parameter in the network.

Why can’t regular NNs don’t scale to images?

Consider an image from CIFAR-10 dataset, size 32x32x3, input neurons would be 32x32x3 = 3072 weights. Good? Now consider an image 200x200x3 image, 120,000 weights in the first layer, moreover we would almost certainly want to have several such neurons, so the parameters would add up quickly. Clearly, this full connectivity is wasteful and the huge number of parameters would quickly lead to overfitting.

3D volumes of neurons

CNN’s take the advantage of the fact that the input are images and use the architecture in a more sensible way. In particular, unlike the regular neural networks the layer of convolution net have neurons arranged in three dimensions: Height, width and depth. The depth here is not the depth of the whole neural net. But instead, it’s the depth of trainable weights. A good visualization:


Regular 3-layer fully connected neural network diagram
Fig. 1. A regular 3-layer fully connected Neural Network (source: CS231n).
Convolutional Neural Network arranging neurons in 3D volumes (height, width, depth)
Fig. 2. A ConvNet arranges neurons in three dimensions (height, width, depth); the red block represents an RGB input image of size 28 × 28 × 3 (source: CS231n).

Convolve

Filters, also called kernels, are small (typically 3x3 or 5x5) used to slide over the image spatially, computing dot products. The filters also have depth, eg. 3x3x96 here 96 is depth of the kernel. The depth of kernel should be equal to the depth of the input. If the input is 32x32x3 image, where 3 is ~RGB. then kernel would have depth 3 (3x3x3 or 5x5x3).

in the above case, spatial dimension is the 32x32 (height and width of input).


Convolutional kernel sliding across an input image volume
Fig. 3. Kernels slide over the image, to convolve/feature extract.
Activation map spatial dimensions shrinking relative to the input volume
Fig. 4. The dimension of the activation map (right) depends on input (left) size & depth and the kernel size & depth. Notice the reduction in size of the output image.
Multiple filters producing separate activation maps across input channels
Fig. 5. let's say, there are 2 input channel (shown in the image 'consider a second green filter', blue and green) the kernel will also be slide through all location of the 2nd channel. Creates another output activations. Each kernel create a output activation for each channel.
Six 5x5 kernels producing a 6x28x28 stack of activation maps
Fig. 6. Now, if there are 6, 5x5 kernel. we will get 6 seperate activation maps. 6x28x28 in this case.
Stacked convolutional layers interleaved with ReLU non-linearities
Fig. 7. Stack this up, plus non-linearlity (ReLU) on the activation maps.
Overview of stacked Conv and ReLU layers transforming an input image into feature maps
Fig. 8. an overview of input to the conv layers and its output. each convs are followed by non-linearity (for example, ReLU), just stack them and we have a architecture?

An overview of the ConvNet architecture

Full ConvNet architecture transforming raw image pixels into class scores
Fig. 9. The activations of an example ConvNet architecture. The initial volume stores the raw image pixels (left) and the last volume stores the class scores (right). The middle shows visulizations of activations, since it is difficult to visualize 3D, here the representation is in 2D.

How do we know, the size of the output after convolution?
(N-F)/S + 1, where N is the number if input dimension, F is the kernel dimension. S is stride.

Oh yes, what is Stride?

Kernels slide through the input layer, Stride refers to number of pixels the filter mover over the input image during convolution (see some gifs)

Padding

In practice, it is common to pad the input image with 0s around the edges.

Zero-padding around the spatial border of an input feature map
Fig. 10. Good effect, we can control the spatial size of the output. Example, to retain the original size of input image, we can use (F-1)/2 padding. where F is the filter dim.
Output spatial dimensions calculation with zero-padding applied
Fig. 11. Worked example verifying that zero-padding preserves spatial dimensions after convolution.

math time, calculate how many trainable params

Calculation of trainable kernel parameters and biases in a convolutional layer
Fig. 12. kernels are trainable
Common hyperparameter settings for convolutional filter size, stride, and padding
Fig. 13. some common settings for Convolutions

we can also have 1x1 Kernels

1x1 convolution reducing channel depth from 64 to 32 while keeping spatial dimensions unchanged
Fig. 14. 1x1 kernels are used for dimensionality reduction without altering spatial dimensions, 64 -> 32 (halved but the spatial dimension remains same)

Summary 1: Convolution Layer

  • Accepts input of shape W, H, D
  • Requires hyperparameters
    • Number of Kernels– N
    • Kernel dim– F
    • Stride– S
    • Padding– P

The brain/neuron view of CONV layer

Recall how the activation map depth is equal to the number of kernels.

Neuron-level view of a local receptive field in a convolutional layer
Fig. 15. a neuron view of convolution
Element-wise dot product between a local image patch and a convolutional filter
Fig. 16. the output of a patch from img and element wise multiplication of kernel is the size of kernel itself, then we calculate the sum of it. ~Dot product. This creates a single activation in the activation layer
Spatially arranged neurons sharing weights across stacked activation channels
Fig. 17. Create layers spatially, they are not interconnected. It similar to channels stacked.

Pooling Layer

Spatial downsampling via a pooling layer reducing height and width
Fig. 18. This throws away some spatial information, but dont worry, not all information is equally valuable. Hypothetically, Pooling layer discards some less important details.
2x2 max pooling with stride 2 taking the maximum value in each window
Fig. 19. Max pooling is generally better than average pooling because it preserves the most prominent features and introduces non-linearity, making it more effective in capturing key patterns and reducing overfitting.
Common pooling hyperparameters: F=2, S=2 and F=3, S=2
Fig. 20. Common pooling hyperparameters: kernel size F = 2, stride S = 2 and F = 3, S = 2.

Case Study

LeNet-5

LeNet-5 architecture diagram: Conv-Pool-Conv-Pool-Conv-FC
Fig. 21. Architecture: [Conv-pool-Conv-pool-conv-FC]

AlexNet

AlexNet first convolutional layer using 96 kernels of size 11x11 at stride 4
Fig. 22. Increased the kernel depth, from 6 to 96. Applied kernel of size 11x11 at stride of 4.
AlexNet layer-by-layer architecture with ReLU activations and local response normalization
Fig. 23. AlexNet was the first paper to use ReLU. This made ReLU popular. There are ReLU after each convolution layers and FC layers. We don't use the NORM layers used in AlexNet now, because it doesn't actually gives any improvement.

Architecture of AlexNet: C = Conv, P = Pool, O = Output layer
[C-P-C-P-C-C-C-P-FC-FC-O-Softmax]

some details:

AlexNet training details including learning rate decay by 10x on validation accuracy plateau
Fig. 24. LR is reduced by 10, when Val accuracy plateaus

ZFNet

ZFNet architecture refining AlexNet with smaller stride and more filters
Fig. 25. Similar architecture to AlexNet but increased number of kernels (N)

VGGNet

VGGNet architecture diagram showing 13 convolutional layers and pooling stages
Fig. 26. VGGNet architecture overview (source: Simonyan & Zisserman, 2014).
Hand-drawn diagram of VGGNet's 13 convolutional layers and pooling stages
Fig. 27. A rough diagram that I made, shows there are 13 Conv Layers used in VGG net (there are also pooling)
VGGNet memory and parameter footprint breakdown by layer
Fig. 28. ~by Andrej Karpathy

Memory footprint of VGGNet shows,

  • Most memory is in early CONV layer (3M)
  • Most Parameters are in late FC layers (10M)
  • 93MB/image in forward pass alone

Average Pooling Layer replaces FC layer in the end

Global average pooling reducing a 7x7x512 feature map to a 1x512 vector
Fig. 29. Global average pooling collapsing each 7×7 spatial slice of a 7×7×512 volume into a single value (1×512), replacing parameter-heavy FC layers.
The example is from the FC layer of VGG net, the last POOL2 output is of shape: 7x7x512, where 7x7 are spatial dim and 512 is depth/volume. each 7x7 is averaged pool to 1. reducing the size quite a lot.

GoogLeNet (introduced inception)

GoogLeNet Inception module architecture diagram
Fig. 30. GoogLeNet Inception module combining multi-scale convolutions in parallel (source: Szegedy et al., 2014).
GoogLeNet full network replacing final FC layers with average pooling
Fig. 31. This paper introduced Inception module, remove last FC layer with AveragePool layer to reduce parameters.

ResNet

In 2015, ResNet won 1st place in many competition at the same time. GoogleNet was 22 layers, introduced resnet was 152 layers.

Stack more layers meme illustrating naive depth scaling
Fig. 32.
So increasing layers == win?
Training and test error curves showing 56-layer plain network underperforming 20-layer network
Fig. 33. Well, yes thats what scaling hypothesis says. But increasing layers is challenging. Problems with vanilla nets, increasing the number of layers converges in training (left img, dashed lines) but the validation error of 56 layer > 20 layer net. Which makes no sense.

ResNet took, 2-3 weeks of training on 8 GPU, but at runtime, its faster than VGGNet, even though it has 8x more layers.

Residual block diagram with identity skip connection F(x) + x
Fig. 34. Residual block with an identity skip connection computing F(x) + x (source: He et al., 2015).
Bottleneck residual block with 1x1, 3x3, and 1x1 convolutions
Fig. 35. Bottleneck residual block using 1×1 convolutions to reduce and restore channel dimensions around a 3×3 convolution.
Comparison of ResNet identity skip connections and Highway Network gated skip connections
Fig. 36. Highway network is also a good paper by Jürgen Schmidhuber. Introduced just before ResNet. The Skip connection in Highway network had trainable weights. where as ResNet Skip connections is literally vanilla addition of input to output.

AlphaGo (policy network?)

Playing Go (harder than chess) using CNNs? The 48 rules of playing Go embedding into the image’s channel?

AlphaGo convolutional policy network architecture taking a 19x19x48 board representation
Fig. 37. AlphaGo policy network: a 19×19×48 board representation processed by convolutional layers to output move probabilities (source: Silver et al., 2016).
  • ConvNets Stack Conv, pool, FC layers
  • Smaller filters and deep architecture
  • get rid of Pool/FC layers (just convs)
    • Treads toward stride-convs only layer, where you try to reduce image dimension (not depth) using convs instead of using pooling layer.
    • ResNet/GoogleNet challenge this paradigm

References