Knowledge / Artificial Intelligence and Data Science / Convolutional neural networks

Convolutional neural networks

Artificial Intelligence and Data Science (B.E./B.Tech) · Deep learning · free study notes for exams, placements and interviews

In short

A convolutional neural network is a neural network in which most layers replace full connectivity with a small learnable kernel slid across the input, so that nearby pixels are combined locally and the same weights are reused at every position. This gives three properties a plain dense network lacks — local connectivity, weight sharing and built-in equivariance to translation — and cuts parameters by orders of magnitude on image-sized inputs. CNNs are the standard model for images, video, spectrograms and 1-D signals, and remain the deployed choice on edge hardware even where transformers win on accuracy. In India they are examined in GATE DA/CS (deep learning section), in university AI&DS courses as the core vision unit, and asked in interviews at Qualcomm, Bosch, Samsung R&D, Tata Elxsi, Nvidia, ISRO/NRSC, Qure.ai, Niramai and most computer-vision startups. Numerical questions on output size, parameter count and receptive field are near-certain in both exams and screening tests.

Explained

The convolution layer

Pooling, activation and the rest of the stack

Receptive field and the arithmetic of depth

How a CNN is trained (what is specific to convolution)

  1. Forward: Z = W ∗ X + b per layer; cache X and pooling indices.
  2. Loss: cross-entropy for classification; IoU/Dice-type losses for segmentation; smooth-L1 + classification for detection.
  3. Gradient to weights: ∂L/∂W is itself a convolution of the input patch with the output gradient — each weight accumulates contributions from every spatial position (consequence of weight sharing).
  4. Gradient to input: ∂L/∂X is a *full* convolution of ∂L/∂Z with the 180°-rotated kernel — equivalently a transposed convolution. Stride S in the forward pass becomes dilation of the gradient map in the backward pass.
  5. Bias gradient: sum of ∂L/∂Z over all positions of that channel.
  6. Implementation: im2col + GEMM (BLAS/cuBLAS), FFT for large kernels, Winograd for 3×3 — the reason 3×3 is unusually fast on GPUs and NPUs.
  7. Initialisation: He/Kaiming for ReLU nets, with fan_in = k²·C_in. Xavier/Glorot for tanh.

Landmark architectures (what each contributed)

YearModelKey idea
1998LeNet-5First practical CNN; conv + pool + FC on digits
2012AlexNetReLU, dropout, GPU training, large-scale ImageNet win
2014VGG-16/19Uniform 3×3 stacks, depth over kernel size; very heavy FC head
2014GoogLeNet / InceptionParallel multi-scale branches, 1×1 bottlenecks, GAP head
2015ResNetResidual/skip connection y = F(x) + x; enables 50–152+ layers
2015U-NetEncoder–decoder with skip concatenations for segmentation
2016DenseNetEvery layer sees all previous feature maps (concatenation)
2017MobileNet / ShuffleNetDepthwise separable + grouped convs for mobile inference
2018SENetSqueeze-and-excitation channel attention
2019EfficientNetCompound scaling of depth, width and resolution
2022ConvNeXtModernised pure CNN, competitive with vision transformers

Advantages, limitations and comparisons

Key points

Formulas and examples

Output spatial size: H_out = ⌊(H_in − k + 2P)/S⌋ + 1 (same for W). H_in, k, P, S in pixels; result in pixels.
Dilated kernel: k_eff = k + (k − 1)(d − 1), then use k_eff in the output-size formula.
Transposed convolution: H_out = (H_in − 1)·S − 2P + k + output_padding.
Pooling: H_out = ⌊(H_in − k)/S⌋ + 1 (no padding, no learnable parameters).
Learnable parameters of a conv layer: (k_h·k_w·C_in + 1)·C_out (drop the +1·C_out if bias = False).
Multiply–accumulates (MACs) of a conv layer: H_out·W_out·C_out·(k_h·k_w·C_in); FLOPs ≈ 2 × MACs.
Dense layer parameters: (n_in + 1)·n_out, where n_in = H·W·C after flatten.
Receptive field: RF_l = RF_{l−1} + (k_l − 1)·J_{l−1}, J_l = J_{l−1}·S_l, RF₀ = 1, J₀ = 1.
Depthwise separable cost ratio vs standard: (k² C_in + C_in C_out)/(k² C_in C_out) = 1/C_out + 1/k².
Memory for activations of one layer (float32): H_out·W_out·C_out·4 bytes per image.

Example 1 — Output size, parameters and MACs of one layer. Input 32×32×3 (CIFAR-10); 64 filters of 3×3, S = 1, P = 1, bias on.

Example 2 — AlexNet first layer and the pooling that follows. Input 227×227×3; 96 filters 11×11, S = 4, P = 0.

Note the asymmetry: this layer has 35 k weights but 105 M MACs, while AlexNet's first dense layer has ~37 M weights and only ~37 M MACs.

Example 3 — Full LeNet-style network on MNIST; total parameters. Input 28×28×1.

LayerConfigOutputParameters
Conv16 @ 5×5, S1, P024×24×6(25+1)·6 = 156
Pool12×2, S212×12×60
Conv216 @ 5×5, S1, P08×8×16(5·5·6+1)·16 = 2,416
Pool22×2, S24×4×160
Flatten—2560
FC1120 units120256·120+120 = 30,840
FC284 units84120·84+84 = 10,164
FC310 units1084·10+10 = 850

Example 4 — Receptive field of a small stack. Layers: Conv 3×3 S1 → MaxPool 2×2 S2 → Conv 3×3 S1 → MaxPool 2×2 S2 → Conv 3×3 S1.

With dilation d = 2 in the last conv (k_eff = 5): RF = 10 + 4·4 = 26 pixels, parameters unchanged.

Example 5 — Standard vs depthwise separable convolution. Input 14×14×256, output 14×14×256, k = 3, S = 1, P = 1, no bias.

Example 6 — Upsampling in a decoder. Transposed conv, input 7×7, k = 4, S = 2, P = 1: H_out = (7−1)·2 − 2 + 4 = 14. With k = 3, S = 2, P = 1, output_padding = 1: (7−1)·2 − 2 + 3 + 1 = 14 (exact 2× upsampling).

import torch, torch.nn as nn
block = nn.Sequential(
    nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False),  # 'same'
    nn.BatchNorm2d(64), nn.ReLU(inplace=True),
    nn.MaxPool2d(2, 2),                                                # 32 -> 16
    nn.Conv2d(64, 128, 3, padding=1, bias=False),
    nn.BatchNorm2d(128), nn.ReLU(inplace=True),
    nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(128, 10))
x = torch.randn(8, 3, 32, 32)
print(block(x).shape)                                     # torch.Size([8, 10])
print(sum(p.numel() for p in block.parameters()))         # 77,130

Where it is used

Interview questions

Common mistakes

Related topics

Neural networks and backpropagation · Training deep networks: optimisers, normalisation and regularisation · Transfer learning and fine-tuning · Transformers and attention · Generative models: GANs, VAEs and diffusion · Digital image processing and feature extraction

Open in the Knowledge reader →Find jobs →