U-Net
An encoder that sees what, a decoder that recovers where, and skip connections that carry the detail across — plus a decoder bug to find and fix.
- Trace tensor shapes through a U-Net's encoder, bottleneck and decoder
- Explain what the skip connections give the decoder
- Find and fix the channel mismatch in the repository's decoder
A classifier answers what is in the image. Segmentation asks for a label at every pixel, so the output must be as large as the input. The U-Net, designed in 2015 for medical images, does this with a symmetric shape: an encoder that repeatedly halves the resolution and doubles the channels, a bottleneck, and a decoder that reverses the process.
Pooling throws away where things are in exchange for what they are. By the bottleneck, a 224×224 image is 14×14 — each position summarises a 16×16 block. The decoder cannot recover fine boundaries from that alone, so at every level it concatenates the encoder’s feature map of the same size: the skip connection. The encoder hands over the detail; the decoder supplies the context.
The encoder in unet.py
e1 = self.enc1(x) # (bs, 64, 224, 224)
e2 = self.enc2(nn.MaxPool2d(2)(e1)) # (bs, 128, 112, 112)
e3 = self.enc3(nn.MaxPool2d(2)(e2)) # (bs, 256, 56, 56)
e4 = self.enc4(nn.MaxPool2d(2)(e3)) # (bs, 512, 28, 28)
b = self.bottleneck(nn.MaxPool2d(2)(e4)) # (bs, 1024, 14, 14)
Each conv_block is two 3×3 convolutions with padding 1, so only the pooling changes the size. All five comments are correct.
Spot the bug
Now the first decoder step:
self.dec1 = self.upconv_block(1024, 512) # ConvTranspose2d(1024, 512, 2, 2) + conv_block
...
d1 = self.dec1(torch.cat([e4, nn.Upsample(scale_factor=2)(b)], dim=1))
Work out the shapes before reading on. Upsample makes b 1024 × 28 × 28; concatenating e4 adds 512 channels, giving 1536 — but the block’s first layer, ConvTranspose2d(1024, 512, ...), expects 1024. The very first forward pass stops with a RuntimeError about expecting 1024 channels and getting 1536. And had the channels matched, there would be a second problem: Upsample already doubled the size, and the transposed convolution doubles it again, to 56 × 56, one level too large for the next concatenation with e3.
The script never calls the model, only print(model), so neither shows up. residual_unet.py reuses the same decoder and fails the same way.
The fix
Split each decoder level into an up-convolution and a conv block, and concatenate between them:
self.up1 = nn.ConvTranspose2d(1024, 512, 2, 2)
self.dec1 = self.conv_block(1024, 512) # 512 from up1 + 512 from e4
...
d1 = self.dec1(torch.cat([e4, self.up1(b)], dim=1)) # (bs, 512, 28, 28)
d2 = self.dec2(torch.cat([e3, self.up2(d1)], dim=1)) # (bs, 256, 56, 56)
d3 = self.dec3(torch.cat([e2, self.up3(d2)], dim=1)) # (bs, 128, 112, 112)
d4 = self.dec4(torch.cat([e1, self.up4(d3)], dim=1)) # (bs, 64, 224, 224)
return self.final_conv(d4) # (bs, 2, 224, 224)
The pattern at every level: the up-convolution halves the channels while doubling the size, the skip brings the same number of channels back, and the conv block halves them again. The output has one score per class per pixel.
Two smaller notes. Creating nn.MaxPool2d(2) inside forward works — the layer has no parameters — but defining it once in __init__ is the convention. And unlike the original paper, which used unpadded convolutions and cropped the skips, padding 1 keeps every size a clean power of two, so no cropping is needed as long as the input side is divisible by 16.
Try it yourself. Apply the fix and confirm that model(torch.randn(1, 3, 224, 224)).shape is (1, 2, 224, 224). Then feed a (1, 3, 200, 200) image. Where does it fail, and why does 200 not divide cleanly through four poolings when 224 does?