Skip to content
ml-recall

Topic notes

Segmentation

Labelling every pixel; semantic, instance and panoptic segmentation, FCN, U-Net, DeepLab and Mask R-CNN, transposed and dilated convolutions, and the IoU, Dice and PQ metrics.

4-min read AI-checked

Practise 39 questions Take a test

Key ideas

Formulas

With nijn_{ij} = pixels of true class ii predicted as jj and ti=∑jnijt_i = \sum_j n_{ij}, as in the FCN paper7:

pixel acc=∑inii∑iti,IoUi=niiti+∑jnji−nii,mIoU=1C∑iIoUi\text{pixel acc} = \frac{\sum_i n_{ii}}{\sum_i t_i},\qquad \mathrm{IoU}_i = \frac{n_{ii}}{t_i + \sum_j n_{ji} - n_{ii}},\qquad \text{mIoU} = \frac{1}{C}\sum_i \mathrm{IoU}_i D=2∣X∩Y∣∣X∣+∣Y∣=2J1+J,Dsoft=2∑ipigi∑ipi2+∑igi2,PQ=∑(p,g)∈TPIoU(p,g)∣TP∣+12∣FP∣+12∣FN∣D = \frac{2\lvert X \cap Y \rvert}{\lvert X \rvert + \lvert Y \rvert} = \frac{2J}{1 + J},\qquad D_{\text{soft}} = \frac{2\sum_i p_i g_i}{\sum_i p_i^2 + \sum_i g_i^2},\qquad \mathrm{PQ} = \frac{\sum_{(p, g) \in TP}\mathrm{IoU}(p, g)}{\lvert TP \rvert + \tfrac12\lvert FP \rvert + \tfrac12\lvert FN \rvert}

DsoftD_{\text{soft}} is V-Net's Dice for predicted probabilities pip_i and labels gig_i8. PQ is computed per class and averaged; it matches a predicted and a ground-truth segment only if their IoU is strictly above 0.5, which makes the matching unique because panoptic segments do not overlap1. Transposed convolution output size (dilation 1): (n−1)s−2p+k(n - 1)s - 2p + k, plus PyTorch's output_padding, which follows from the stride spacing and the removed padding2.

Worked example

Binary segmentation of 100 pixels; rows are the true class, columns the prediction:

true \ pred background object
background 70 10
object 5 15

In code

import numpy as np
import torch
from torch import nn
from torchvision.transforms import ToTensor
up = nn.ConvTranspose2d(21, 21, kernel_size=64, stride=32, padding=16)
print(up(torch.zeros(1, 21, 15, 20)).shape)   # torch.Size([1, 21, 480, 640])  scores back at input size
up2 = nn.ConvTranspose2d(16, 16, kernel_size=3, stride=2, padding=1)
print(up2(torch.zeros(1, 16, 8, 8)).shape)    # torch.Size([1, 16, 15, 15])  not 16
up3 = nn.ConvTranspose2d(16, 16, kernel_size=3, stride=2, padding=1, output_padding=1)
print(up3(torch.zeros(1, 16, 8, 8)).shape)    # torch.Size([1, 16, 16, 16])
mask = np.full((4, 4, 1), 15, dtype=np.uint8)  # label map: class 15 everywhere
print(round(ToTensor()(mask)[0, 0, 0].item(), 4))  # 0.0588  = 15/255, no longer a class index

Common pitfalls

References

  1. Kirillov et al. (2018), Panoptic Segmentation — Sec. 1 Introduction; 3 Panoptic Segmentation Format; 4.1 Segment Matching; 4.2 PQ Computation
  2. Dive into Deep Learning — Sec. 14.10.1 Basic Operation; 14.10.2 Padding, Strides, and Multiple Channels; 14.10.3 Connection to Matrix Transposition
  3. Dive into Deep Learning — Sec. 14.11 (introduction); 14.11.1 The Model; 14.11.2 Initializing Transposed Convolutional Layers; 14.11.4 Training; 14.11.5 Prediction
  4. Ronneberger et al. (2015), U-Net: Convolutional Networks for Biomedical Image Segmentation — Sec. 2 Network Architecture; 3 Training
  5. He et al. (2017), Mask R-CNN — Sec. 3 Mask R-CNN (Mask Representation; RoIAlign)
  6. Chen et al. (2016), DeepLab: Semantic Image Segmentation with Deep Convolutional Nets, Atrous Convolution, and Fully Connected CRFs — Sec. 3.1 Atrous Convolution for Dense Feature Extraction and Field-of-View Enlargement; 3.2 Multiscale Image Representations using Atrous Spatial Pyramid Pooling
  7. Long et al. (2015), Fully Convolutional Networks for Semantic Segmentation — Sec. 4 Segmentation Architecture (introduction; 4.1 From classifier to dense FCN; 4.2 Combining what and where); 5 Results (Metrics)
  8. Milletari et al. (2016), V-Net: Fully Convolutional Neural Networks for Volumetric Medical Image Segmentation — Sec. 3 Dice loss layer