Lecture 13
Department of Intelligent Systems, University of Lahore
C_in × C_out × k × k learnable weights, applied identically at every spatial location — this weight sharing is what makes CNNs far more parameter-efficient than a fully-connected layer on image-sized input.SmallCNN in Lecture 12's code).128×128×64 feature map to 128 outputs: \(128\times128\times64\times128 \approx 134\) million parameters — this is why classification heads use global average pooling before the final FC layer, not a flatten.train() vs eval() mode in PyTorch — a frequent source of the "works in training, breaks at inference" bug.1998 2012 2014 2015 2017/19
LeNet-5 → AlexNet → VGG → ResNet → MobileNet / EfficientNet
digits ImageNet deeper residual mobile / scaled
(7 layers) breakthrough (16-19) connections for the edge
Each step fixed a limitation of the previous one — trace why, not just what, because the same reasoning shows up whenever you pick a backbone for a new project.
This is the exact method you will apply twice this semester: to U-Net this week (Lab 13), and to the Transformer in Lecture 15 (Lab 15).
class DoubleConv(nn.Module):
def __init__(self, in_ch, out_ch):
super().__init__()
self.block = nn.Sequential(
nn.Conv2d(in_ch, out_ch, 3, padding=1), nn.BatchNorm2d(out_ch), nn.ReLU(),
nn.Conv2d(out_ch, out_ch, 3, padding=1), nn.BatchNorm2d(out_ch), nn.ReLU())
def forward(self, x):
return self.block(x)
# Decoder step: upsample, concatenate the matching encoder feature map, then DoubleConv
class Up(nn.Module):
def __init__(self, in_ch, out_ch):
super().__init__()
self.up = nn.ConvTranspose2d(in_ch, in_ch // 2, kernel_size=2, stride=2)
self.conv = DoubleConv(in_ch, out_ch)
def forward(self, x, skip):
x = self.up(x)
x = torch.cat([skip, x], dim=1) # the skip connection
return self.conv(x)
You will assemble the full encoder/bottleneck/decoder + IoU/Dice evaluation in Lab 13.
Lab 13 — Paper Implementation I: U-Net from Scratch (PyTorch).
Deliverable: a from-scratch U-Net that segments a dataset + a one-page paper summary.
Intelligent Vision Systems; University of Lahore | Dr. Muhammad Kazim