Sub topic 8
Encoder-Decoder Networks and Upsampling
This chapter assumes you know basic Python, NumPy, convolutional neural networks, image classification, and gradient-based training. You should also understand that a feature map is a grid of learned values produced inside a network.
Why segmentation needs an encoder and decoder
Image classification can reduce an image to one label. Segmentation must produce a label for every pixel. A model therefore needs both broad context and precise location information.
An encoder is the feature-extracting part of the network. It applies convolutions and downsampling to turn the input image into smaller feature maps. These maps contain increasingly useful semantic information, such as whether a region looks like road or a car. Downsampling also reduces computation and gives later layers a larger effective view of the image.
A decoder is the reconstruction part. It converts the small, rich feature maps back into a grid with the input image's spatial resolution. A final 1-by-1 convolution produces one score, or logit, per class at each pixel. Applying argmax across the class dimension gives the predicted label map.
How upsampling restores resolution
Upsampling increases the height and width of a feature map. Common methods include:
- Nearest-neighbor interpolation copies the closest value. It is fast and has no learned parameters, but it can produce blocky boundaries.
- Bilinear interpolation uses nearby values to create smoother results. It is also parameter-free.
- Transposed convolution learns filters that increase spatial resolution. It can learn useful patterns, but uneven overlap can create checkerboard artifacts.
- Sub-pixel or resize-and-convolution methods first enlarge the grid and then apply a normal convolution. This often gives more predictable behavior.
Upsampling alone cannot recover details that the encoder discarded. Many networks use skip connections, which copy earlier, higher-resolution features into the decoder. The decoder combines these local features with the bottleneck's semantic features. U-Net is a well-known example of this design.
import torch
from torch import nn
class SmallSegmentationNet(nn.Module):
def __init__(self, num_classes):
super().__init__()
self.encoder = nn.Sequential(
nn.Conv2d(3, 16, 3, padding=1), nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(16, 32, 3, padding=1), nn.ReLU(),
nn.MaxPool2d(2),
)
self.decoder = nn.Sequential(
nn.ConvTranspose2d(32, 16, 2, stride=2), nn.ReLU(),
nn.ConvTranspose2d(16, num_classes, 2, stride=2),
)
def forward(self, image):
return self.decoder(self.encoder(image))
model = SmallSegmentationNet(num_classes=3)
x = torch.randn(2, 3, 128, 128)
logits = model(x)
print(logits.shape) # torch.Size([2, 3, 128, 128])Common errors
Check tensor shapes after every downsampling and upsampling stage. A stride of 2 usually halves each spatial dimension, while two such stages reduce each dimension by four. Input dimensions that are not divisible by the total scale factor can cause mismatched shapes.
The output contains logits, not class IDs. During training, pass logits and integer target maps to cross-entropy loss. Convert logits to labels only for inspection or evaluation.
Exercise
An encoder reduces a 256 × 256 image by a factor of 8. What spatial size must the decoder restore before producing the final label map?
Show answer
It must restore the feature map from 32 × 32 back to 256 × 256.