Convolutional Neural Networks
Level 3 — Production (from-scratch derivations, code-from-memory, explain-out-loud) Time limit: 45 minutes Total marks: 60
Instructions: Answer all questions. Show every derivation step. Where code is requested, write it from memory (minor syntax slips are tolerated if logic is correct). Use for math.
Question 1 — Output shape & parameter derivation (10 marks)
A convolutional layer receives an input tensor of shape .
(a) Derive the general formula for the spatial output dimension of a convolution with kernel size , padding , stride , and dilation . (3)
(b) Using your formula, compute the output height for , , , . (2)
(c) The layer has filters. Compute the total number of learnable parameters (with bias). (2)
(d) Compute the number of multiply–accumulate (MAC) operations for producing the full output tensor. (3)
Question 2 — Receptive field derivation (10 marks)
Consider a stack of three convolutions, each with stride 1, no dilation.
(a) Derive from scratch the recurrence for the receptive field at layer in terms of , kernel size , and the cumulative stride (jump) . (4)
(b) Compute the receptive field after the three layers. (2)
(c) Explain out loud (in words) why VGG replaces a single conv with three stacked convs — address both receptive field and parameter count. (4)
Question 3 — Code from memory: 2D convolution (12 marks)
(a) Write a NumPy function conv2d(x, w, stride, pad) that performs a valid single-channel 2D cross-correlation with zero padding. Assume x is (H,W) and w is (kH,kW). (8)
(b) State the shape of the output and explain what happens numerically if you forget to flip the kernel (cross-correlation vs. true convolution). (4)
Question 4 — ResNet residual block (out-loud + derivation) (12 marks)
(a) Draw/describe the structure of a basic ResNet residual block, writing the output equation . (3)
(b) Derive the gradient through the block and explain out loud why the skip connection mitigates vanishing gradients. (5)
(c) When the number of channels changes across a block, the identity path cannot be added directly. Explain the two standard fixes and their parameter trade-off. (4)
Question 5 — Architecture reasoning (8 marks)
(a) Explain the purpose of the convolution in the Inception module and compute the parameter saving of a (32 filters) bottleneck placed before a (64 filters) conv, given a input, versus applying the directly. (6)
(b) State one architectural idea each that DenseNet and EfficientNet contribute. (2)
Question 6 — Transfer learning & pipeline (8 marks)
(a) You have 2,000 labelled medical images and a model pretrained on ImageNet. Describe an out-loud strategy: which layers to freeze, learning rate choice, and why. (4)
(b) List four data augmentation transforms suitable for natural-image classification and one that would be inappropriate for digit (e.g. MNIST) recognition, justifying the exclusion. (4)
Answer keyMark scheme & solutions
Question 1 (10 marks)
(a) With dilation, the effective kernel size is . Sliding it across a padded input of length with stride :
- Effective kernel size (1)
- Padding term & numerator (1)
- Floor + 1 (1)
(b) . (2)
(c) Params . (2)
- Correct weight count 7·7·3·64 (1), bias +64 (1)
(d) Output is . Each output element = MACs.
- Output positions (1), MACs per output 147 (1), product (1)
Question 2 (10 marks)
(a) Cumulative jump: , with . Receptive field recurrence: with . Reasoning: each new layer's kernel spans units, but each unit at the previous layer already covers input pixels, so the extra units add . (4)
(b) Stride 1 ⇒ throughout. ; ; . RF = 7. (2)
(c) Three convs give the same receptive field. Parameter count: vs. — a ~45% reduction. Additionally, the stack inserts two extra non-linearities, increasing representational (decision-boundary) capacity. (4: RF equivalence 1, param comparison 2, non-linearity point 1)
Question 3 (12 marks)
(a) (8 marks: padding 2, output alloc 2, loops/window 2, correctness 2)
import numpy as np
def conv2d(x, w, stride=1, pad=0):
if pad > 0:
x = np.pad(x, ((pad, pad), (pad, pad)), mode='constant')
H, W = x.shape
kH, kW = w.shape
oH = (H - kH) // stride + 1
oW = (W - kW) // stride + 1
out = np.zeros((oH, oW))
for i in range(oH):
for j in range(oW):
r, c = i * stride, j * stride
region = x[r:r+kH, c:c+kW]
out[i, j] = np.sum(region * w)
return out(b) Output shape . (2) Cross-correlation (no flip) is what deep-learning frameworks actually use; a true convolution flips the kernel both horizontally and vertically (). For a learned filter it makes no functional difference — the network just learns the mirrored weights — so omitting the flip is standard and correct in ML. (2)
Question 4 (12 marks)
(a) Block: conv3x3 → BN → ReLU → conv3x3 → BN, then add the input (identity), then final ReLU. Output:
(3)
(b) . Out loud: because of the identity term , the upstream gradient flows directly to even if . The gradient never fully vanishes as depth grows — the "" guarantees a highway for gradient flow, enabling very deep networks (100+ layers) to train. (5: chain rule + I term 3, out-loud explanation 2)
(c) (1) Projection shortcut — a conv with stride on the identity path to match channels/spatial dims (adds parameters). (2) Zero-padding the identity's extra channels (parameter-free but less expressive). Trade-off: projection costs params but preserves representational capacity; padding is free but adds no learning. (4)
Question 5 (8 marks)
(a) The conv is a channel-wise bottleneck: it reduces the depth cheaply before an expensive spatial conv, cutting computation/params.
Direct : params . Bottleneck: (192→32): ; then (32→64): . Total . Saving params (≈81% reduction). (6: direct 2, bottleneck two terms 2, saving 2)
(b) DenseNet: dense connectivity — each layer receives feature maps of all preceding layers (concatenation) for feature reuse. EfficientNet: compound scaling — jointly scale depth, width, and resolution by a fixed ratio. (2)
Question 6 (8 marks)
(a) With only 2,000 images: freeze the early/mid convolutional layers (generic edge/texture features transfer well) and fine-tune only the later layers + new classifier head. Use a small learning rate (e.g. ) so pretrained weights aren't destroyed; optionally a higher LR on the new head. Small dataset ⇒ heavy freezing prevents overfitting. (4)
(b) Suitable: random horizontal flip, random crop/scale, color jitter, rotation (small), random erasing. (any 4 → 2 marks) Inappropriate for MNIST digits: horizontal/vertical flip or large rotation — because they change semantic identity (a flipped/rotated "6" becomes a "9" or invalid glyph), corrupting labels. (2)
[
{"claim":"Q1b output height = 112","code":"K,P,S,D=7,3,2,1; H=224; result=(( (H+2*P-D*(K-1)-1)//S)+1)==112"},
{"claim":"Q1c params = 9472","code":"result=((7*7*3+1)*64)==9472"},
{"claim":"Q1d MACs = 118013952","code":"result=(112*112*64*147)==118013952"},
{"claim":"Q2b receptive field of three 3x3 = 7","code":"r=1\nfor _ in range(3): r=r+(3-1)*1\nresult=r==7"},
{"claim":"Q5 bottleneck saving = 249856","code":"direct=5*5*192*64; bott=1*1*192*32+5*5*32*64; result=(direct-bott)==249856"}
]