The idea: a class for every pixel
A classifier looks at an image and says "cat". Semantic segmentation says, for every pixel, which class it belongs to: this pixel is cat, that one is background. The answer is a mask with the same height and width as the image. U-Net (Ronneberger, Fischer and Brox, 2015) is the classic network for it. It first shrinks the image to a small, deep feature map, then grows it back to full size, and it hands sharp detail across from the way down to the way up.
The page follows one network both ways. The network has 4 levels down, a bottleneck and 4 levels up, with two 3×3 convs (each followed by ReLU) at every level. Press Next Block to compute one block at a time.
Down the U: the encoder
The left side of the U is an ordinary CNN. At each level, two 3×3 convs keep the size (padding 1), then a 2×2 max-pool with stride 2 halves it. The number of filters doubles at each level: 64, 128, 256, 512, then 1024 at the bottleneck. So 128×128×1 becomes 8×8×1024. Each value at the bottom sees a large part of the image and knows what is there, but only roughly where. How one conv layer slides over the image is on Convolution in a CNN; how to work out each layer's shape and weight count is on A CNN Layer by Layer (Demo 1).
Up the U: transposed convolution
The decoder must make the map bigger again. U-Net uses a transposed convolution (also called up-convolution) with a 2×2 kernel and stride 2. Think of it as stamping: each input value multiplies a copy of the kernel, and that copy is written into the output. The stamp for input row r, column c starts at output row 2r, column 2c. The output size is
out = (in − 1)·S − 2P + K
With K = 2, S = 2, P = 0 this is (in − 1)·2 + 2 = 2·in: 8 → 16, 16 → 32. In the up-conv panel, X = [1 2 / 3 1] and K = [1 0 / 2 1] give a 4×4 output whose four 2×2 blocks are 1·K, 2·K, 3·K and 1·K (Demo 3). The kernel's weights are learned like any conv filter. The up-conv also halves the channels: 1024 → 512.
If the kernel is bigger than the stride, stamps overlap and their values add up. With a 3×3 kernel and stride 2 the output is 5×5. The centre cell gets 4 stamps, the middle row and column get 2, the other cells 1 (Demo 4). That uneven count is the source of the checkerboard artifacts up-sampled images can show. A common fix is to resize first (nearest or bilinear) and then run an ordinary conv.
Skip connections: concatenate
The bottleneck has lost the fine detail, so up-sampling alone gives blurry, blocky edges. A skip connection copies the encoder map of the same size across the U. The decoder concatenates it with the up-sampled map along the channels, then runs two 3×3 convs on the result:
| level | up-conv output | copied from encoder | after concat | after two convs |
|---|---|---|---|---|
| dec4 | 16×16×512 | enc4 16×16×512 | 16×16×1024 | 16×16×512 |
| dec3 | 32×32×256 | enc3 32×32×256 | 32×32×512 | 32×32×256 |
| dec2 | 64×64×128 | enc2 64×64×128 | 64×64×256 | 64×64×128 |
| dec1 | 128×128×64 | enc1 128×128×64 | 128×128×128 | 128×128×64 |
Concatenation stacks the two maps; it does not add them. So the channel counts add: 512 + 512 = 1024. (A ResNet skip adds values instead and keeps the channel count.) Finally a 1×1 conv with one filter per class turns each pixel's 64 numbers into 2 scores: 128×128×2.
The original U-Net: no padding, so crop
The 2015 paper used no padding. Each 3×3 conv then loses 2 pixels, so a 572×572 tile shrinks to 568 at enc1, 280 at enc2, 136, 64 and 28 at the bottleneck. On the way up, 28 → 56, but enc4 is 64×64. The encoder map is cropped to its centre 56×56 before the concat (Demo 2):
| level | up-conv | crop of encoder map | concat | after two convs |
|---|---|---|---|---|
| dec4 | 28 → 56 | 64 → 56 | 56×56×1024 | 52×52×512 |
| dec3 | 52 → 104 | 136 → 104 | 104×104×512 | 100×100×256 |
| dec2 | 100 → 200 | 280 → 200 | 200×200×256 | 196×196×128 |
| dec1 | 196 → 392 | 568 → 392 | 392×392×128 | 388×388×64 |
So 572×572 in gives a 388×388×2 mask: the centre of the tile. The paper segments big images by sliding overlapping tiles and mirroring the border. Most code today uses padding 1, which keeps the sizes simple: the mask is as big as the image.
The mask and its score: IoU and Dice
Each pixel takes the class with the larger score (argmax). To grade a predicted mask, compare it with the true mask pixel by pixel and count TP (object found), FP (extra), FN (missed) and TN (background, correctly). Two scores look only at the object:
IoU = TP / (TP + FP + FN) Dice = 2·TP / (2·TP + FP + FN) = 2·IoU / (1 + IoU)
On the 8×8 toy image the true object has 26 pixels. The predictions are made by hand to show the effect; they are not the output of a trained net.
| prediction | TP | FP | FN | TN | IoU | Dice | pixel accuracy |
|---|---|---|---|---|---|---|---|
| with skips (Demo 5) | 25 | 1 | 1 | 37 | 0.926 | 0.962 | 96.9% |
| without skips, blocky (Demo 6) | 20 | 4 | 6 | 34 | 0.667 | 0.800 | 84.4% |
| all background (Demo 7) | 0 | 0 | 26 | 38 | 0 | 0 | 59.4% |
Dice is never below IoU, and both are 1 only for a perfect mask. Pixel accuracy is misleading: the mask that finds nothing still gets 59.4%, because most pixels are background. In medical images the object can be 1% of the pixels, and then "all background" scores 99%. That is why segmentation is scored with IoU or Dice, and why many nets are trained with a Dice loss next to the per-pixel cross-entropy. IoU for boxes and how detection uses it is on Object Detection: IoU and NMS; Dice is the F1 score of the object pixels (see Precision and Recall).
U-Net is not an autoencoder
An autoencoder also goes down to a small code and back up, but it must rebuild its own input through that code. A U-Net predicts something else (a mask), and the skip connections let detail bypass the bottleneck. The page on autoencoders covers the bottleneck itself.
See also the autoencoder, the same encoder–decoder shape without skip connections, trained to rebuild its input instead of predicting a mask.
See also the autoencoder, the same encoder–decoder shape without skip connections, trained to rebuild its input instead of predicting a mask.
What the page leaves out
Training: the per-pixel cross-entropy and Dice losses, and the paper's weight map that makes the thin borders between touching cells count more; data augmentation by elastic deformation; batch normalization and dropout; more than 2 classes (one score map per class, the same argmax); instance segmentation, which separates two touching objects of the same class (Mask R-CNN); later variants such as U-Net++, attention U-Net and 3D U-Net; and the overlap-tile strategy in detail.