Sub topic 4
Semantic Segmentation: One Class per Pixel
Assume you know Python, NumPy, convolutional neural networks, and gradient-based training. Semantic segmentation predicts a class for every pixel in an image. It answers “what class is this pixel?” but does not separate two objects of the same class.
For an image with height \(H\), width \(W\), and \(C\) classes, a model usually produces logits with shape (C, H, W). A logit is an unnormalized score. Applying argmax over the class dimension gives a label map with shape (H, W).
| Output | Shape | Meaning |
|---|---|---|
| Input image | `(3, H, W)` | RGB values |
| Model logits | `(C, H, W)` | Score for each class at each pixel |
| Predicted labels | `(H, W)` | One integer class ID per pixel |
How the model produces a label map
Most semantic segmentation models use an encoder-decoder design.
- The encoder extracts features and gradually reduces spatial resolution. Deeper features contain context, such as whether a region is likely to be road or sky.
- The decoder restores the feature map to the input resolution. It uses upsampling and often combines features from earlier encoder layers to recover object boundaries.
- A final
1 × 1convolution converts each pixel's feature vector intoCclass logits.
A dilated convolution enlarges a convolution's receptive field by inserting gaps between kernel elements. It captures wider context without reducing the feature map as aggressively. This can help distinguish objects that need surrounding context, although large dilation rates can create gridding artifacts.
Training and inference
During training, the target is an integer label map. For C classes, each valid target pixel contains a value from 0 through C - 1. Cross-entropy loss compares the logits at each pixel with its target class. Pixels marked with an ignore value, such as 255, should be excluded from the loss.
import torch
torch.manual_seed(7)
num_classes, height, width = 3, 2, 3
logits = torch.randn(num_classes, height, width)
target = torch.tensor([[0, 1, 2], [1, 1, 0]])
predicted = logits.argmax(dim=0)
loss = torch.nn.functional.cross_entropy(
logits.unsqueeze(0), target.unsqueeze(0)
)
print("predicted labels:\n", predicted)
print("loss:", float(loss))Cross-entropy works well when classes have similar pixel frequencies. If a small foreground class is overwhelmed by background pixels, use class weights, balanced sampling, or a Dice-based term. Dice loss directly rewards foreground overlap, but it can be less stable when a class is absent from a training crop.
Common errors include resizing label maps with bilinear interpolation, which creates invalid fractional class IDs. Resize images with bilinear interpolation, but resize discrete labels with nearest-neighbor interpolation. Also verify that the model output and target use the same crop, orientation, and class-ID mapping.
Exercise
A three-class model outputs logits with shape (3, 512, 512). What operation produces one semantic class ID per pixel?
Show answer
Apply argmax over dimension 0, producing a (512, 512) integer label map.