Sub topic 2
Panoptic segmentation: a unified output
This chapter assumes you know basic Python and NumPy, have used convolutional neural networks (CNNs) for image classification, and understand that a loss function is the number a model minimizes during training.
Panoptic segmentation: a unified output
Panoptic segmentation gives every pixel exactly one class label and, for countable objects, one instance ID. Countable objects such as cars are called things. Background regions such as sky or road are called stuff, and they share a class label without instance splits. The output is a single non-overlapping map.
Measuring overlap: IoU and mean IoU
Intersection over union (IoU) divides the overlap between a predicted mask and a ground-truth mask by the area of their union. Mean IoU (mIoU) averages the per-class IoU values. The code below computes both from a confusion matrix, where rows are ground truth and columns are predictions.
import numpy as np
def confusion_matrix(pred, target, num_classes):
valid = (target >= 0) & (target < num_classes)
idx = num_classes * target[valid].astype(np.int64) + pred[valid].astype(np.int64)
counts = np.bincount(idx, minlength=num_classes ** 2)
return counts.reshape(num_classes, num_classes)
def per_class_iou(cm):
tp = np.diag(cm).astype(np.float64)
union = cm.sum(axis=0) + cm.sum(axis=1) - np.diag(cm)
union = np.where(union == 0, np.nan, union)
return tp / union
pred = np.array([[0, 0, 1], [1, 1, 2]])
target = np.array([[0, 1, 1], [1, 2, 2]])
cm = confusion_matrix(pred, target, num_classes=3)
iou = per_class_iou(cm)
print(iou, np.nanmean(iou))Encoder-decoder networks and upsampling
An encoder shrinks the image into smaller feature maps. A decoder enlarges them back to full resolution so every pixel gets a label. Skip connections copy high-resolution encoder features into the decoder, which keeps object edges sharp.
Dilated convolutions and multi-scale context
A dilated convolution spreads its kernel taps apart. This widens the receptive field, the image area that influences one output, without reducing resolution or adding parameters. Stacking several dilation rates, such as 1, 2, and 4, lets the network use wide context while keeping fine detail.
Training losses: cross-entropy and Dice
Pixel-wise cross-entropy treats each pixel as a classification problem. It trains stably, but when the foreground covers a small part of the image, background pixels dominate the loss. Dice loss measures overlap directly and is less affected by this imbalance. Many training setups combine the two.
Exercise
A confusion matrix has diagonal values 4, 2, and 0, and column sums of 5, 3, and 1 with row sums of 4, 4, and 0. What is the IoU for class 2, and why does the code return NaN for a class with no pixels?
Show answer
Class 2 has true positives 0 and union 1 + 0 − 0 = 1, so IoU is 0. The code returns NaN only when the union is 0, which means the class appears in neither the predictions nor the ground truth. In this example the union for class 2 is 1, so it returns 0, not NaN.