Start studying
Module 3 · Computer Vision Module contents
Module 3  ·  Card 20 / 22intermediate

U-Net

U-Net is a segmentation architecture shaped like the letter U.

The left side downsamples to capture context.

The right side upsamples back to full resolution.

Skip connections carry fine detail across from left to right.

It was designed for medical images and works well with small datasets.

Learn in depth ↓
Module 03 · Computer Vision · 22 concepts
In depth  /  15 lines

U-Net, in more detail

U-Net is a segmentation architecture named after the shape of its diagram.

The left arm downsamples, halving resolution and doubling channels at each step.

That builds up context, so the network understands what it is looking at.

The right arm upsamples back to the original resolution to produce the mask.

The problem is that downsampling has already destroyed the fine detail the mask needs.

Skip connections solve it by carrying feature maps straight across from left to right.

Each upsampling step is handed the matching high-resolution map from the way down.

So the network combines what it is with where exactly it is.

Those horizontal links are what make the output sharp instead of blobby.

It was designed in 2015 for biomedical images, where datasets are tiny.

It works well with a few dozen annotated images plus heavy augmentation.

That efficiency is why it spread far beyond medicine.

It is still a strong baseline for any dense prediction task.

The same shape reappears inside diffusion models, which denoise images step by step.

Ten years on, a U-Net remains a sensible first thing to try on a new segmentation problem.

Want the full technical article? Wikipedia ↗
Deep dive

What is underneath U-Net

Why segmentation needs a different shape

A classifier collapses an image to one label, so throwing away spatial detail is free. Segmentation has to return a label per pixel, so the detail has to come back.

(H,W,3)⟶(H,W,C)(H, W, 3) \longrightarrow (H, W, C)

The output is the same size as the input, with one channel per class rather than three colours.

That forces an architecture that goes down and then comes back up, which is where the U in the name comes from.

The two halves

down:(H,W,C)→(H2,W2,2C)\text{down} : (H, W, C) \to \left(\frac{H}{2}, \frac{W}{2}, 2C\right)

The encoder is an ordinary convolutional stack. Each stage convolves twice and pools once, halving the resolution and doubling the channels.

up:(H,W,C)→(2H,2W,C/2)\text{up} : (H, W, C) \to (2H, 2W, C/2)

The decoder mirrors it. Each stage upsamples, halves the channels, and convolves twice.

adec[l]=f([ u[l]  ;  aenc[l] ])\mathbf{a}_{\mathrm{dec}}^{[l]} = f\big( [\, \mathbf{u}^{[l]} \; ; \; \mathbf{a}_{\mathrm{enc}}^{[l]} \,] \big)

The skip connection is the part that matters: before convolving, the decoder concatenates the encoder's map from the same resolution onto its own.

What the skip connections carry

The encoder's deep layers know what is in the image and have lost where, because five poolings reduced 256 pixels to 8.

Its early layers know exactly where every edge is and almost nothing about what they belong to.

The decoder needs both at once, and concatenation gives it both rather than forcing one path to carry them.

[ u  ;  a ]∈RH×W×2C[\, \mathbf{u} \; ; \; \mathbf{a} \,] \in \mathbb{R}^{H \times W \times 2C}

Concatenation rather than addition is deliberate. Adding would force the two to share a representation, while concatenating lets the following convolution learn how to weigh them.

One pass, in order

  1. Two 3 by 3 convolutions with ReLU, then store the result for the skip.
  2. Pool, and repeat for each encoder stage.
  3. At the bottom, two convolutions on the smallest map.
  4. Upsample, concatenate the stored map from that resolution, then two convolutions.
  5. Repeat for each decoder stage.
  6. A final 1 by 1 convolution mapping channels to classes, then softmax over the class axis.

The loss, and the imbalance behind it

LCE=−1HW∑i,j∑cyijclog⁡y^ijc\mathcal{L}_{\mathrm{CE}} = -\frac{1}{HW} \sum_{i,j} \sum_{c} y_{ijc} \log \hat{y}_{ijc}

Pixelwise cross-entropy treats every pixel as its own classification problem, which works and is badly imbalanced when the object is small.

LDice=1−2∑ipigi+ϵ∑ipi+∑igi+ϵ\mathcal{L}_{\mathrm{Dice}} = 1 - \frac{2 \sum_{i} p_i g_i + \epsilon}{\sum_{i} p_i + \sum_{i} g_i + \epsilon}

Dice loss measures overlap instead, so it is insensitive to how much background there is, and it is the standard choice in medical imaging for that reason.

The two are usually summed, because Dice alone has an unstable gradient when the prediction is almost empty and cross-entropy alone ignores the shape.

Why it survived

U-Net was published in 2015 for cell images with about 30 training examples, and it still wins on small datasets.

The reason is the skip connections and heavy augmentation rather than depth.

The same shape now sits inside every diffusion model, where the noise predictor is a U-Net with attention added.

Test yourself

One question on this card. Every option explains itself once you pick, right or wrong.

Question 1
What do U-Net's skip connections carry across the U?
Computer Vision

22 concepts in this module, then a graded test with every answer explained.

Study this module →
↑↓ move↵ openesc close