← Explainer Library

Interactive Explainer

U-Net, Section by Section

Watch a synthetic image flow down an encoder, lose 16× of its spatial resolution at the bottleneck, and climb back up. Toggle skip connections at any scale and watch the predicted mask turn from a blurry blob to crisp segmentation. Then meet the same U inside diffusion models.

Prelude

The shape problem

Many vision tasks need a per-pixel output, not a single class per image: medical image segmentation, satellite land-cover parsing, depth estimation, and most recently denoising in diffusion models. The output has the same spatial size as the input.

A pure CNN classifier squashes spatial resolution down to a tiny feature map (good for "is this a cat?"). To produce a full-resolution mask you have to get the resolution back — and the only honest way to do that is to have remembered it on the way down.

U-Net's idea, in one line. Encoder $\to$ bottleneck $\to$ decoder, with a skip connection at every scale that hands the encoder's high-resolution feature map directly to the matching decoder block. The output mask gets coarse semantic context from deep in the U and sharp pixel-level structure from the early skips.
Step 1

The architecture, drawn to scale

A 4-level U-Net: 4 encoder blocks (each halves spatial size and doubles channels), one bottleneck, 4 decoder blocks (each doubles spatial size, halves channels, and concatenates the matching encoder feature). For a 64×64 input the bottleneck is 4×4. That's 16× spatial compression in two dimensions, or 256× total — enormous information loss without skips.

The U. Boxes are feature maps; numbers above are H × W; numbers below are channel count.
Step 2

Forward pass on a real synthetic image

We synthesise an input: two coloured ellipses on noise. The target mask labels which pixels belong to ellipse 1 vs ellipse 2 vs background. A small U-Net (random weights; we hand-craft the conv kernels for visibility) processes the image. Inspect the feature map at each scale:

resolution at bottleneck:
input 64×64
enc 1 · 32×32
enc 2 · 16×16
enc 3 · 8×8
bottleneck · 4×4
dec 3 · 8×8 (← bot)
dec 2 · 16×16
dec 1 · 32×32
output 64×64
target 64×64
Step 3

Toggle the skips, watch detail die

Each skip connection at level $\ell$ concatenates the encoder's level-$\ell$ feature map onto the decoder's input at the same level. Turn one off and the corresponding scale of detail goes missing. Turn them all off and you've built a plain encoder-decoder, which is exactly the architecture U-Net was invented to beat.

predicted mask
target mask
diff (RMSE)
What you should see. With all three skips on, the predicted mask hugs the ellipse boundaries. Disable level 1 (highest resolution) and the outline blurs. Disable level 3 (deepest skip) and the mask can still localise but loses semantic shape. All skips off → the mask collapses to a low-resolution rectangle.
Step 4

Why concatenate, not add?

ResNet adds: $y = F(x) + x$. U-Net concatenates: $y = [F(x); \text{skip}]$. The reason is information preservation. The encoder feature carries spatial detail the bottleneck has discarded. Adding a vector with very different statistics into the bottleneck output would interfere with the decoder's learning. Concatenation lets the decoder freely choose which channels to read.

This costs more channels in the decoder convolutions, but for dense prediction tasks it's worth it. Modern U-Nets (and diffusion U-Nets, see Step 5) sometimes mix — add for residuals within a block, concatenate for skips across the U.

Step 5

The U inside diffusion

Stable Diffusion, Imagen, every modern image-diffusion backbone is a U-Net. The denoiser takes a noisy image and predicts the noise it has to remove. That's a per-pixel regression problem — same shape, same downsampling and upsampling story, plus three additions:

The skips do the same job they always did: they preserve high-frequency detail that the encoder has thrown away. That's why diffusion outputs have crisp edges instead of looking like JPEG-compressed dreams.

Step 6

The variants worth knowing

Final takeaway. U-Net is the second piece of pre-2018 deep learning that refused to be obsoleted — ResNet's residual is the first. Whenever you need a per-pixel output and you don't have unlimited training data, the U is still the best default. The reason is the picture above: the encoder discards detail and the decoder can't invent it back, so you have to remember it via skips.
Step 7

Diffusion's U-Net — why an old architecture won the generative race

Every Stable Diffusion checkpoint is a U-Net, lightly modernised: the encoder + bottleneck + decoder structure is the original 2015 design, with three additions:

In 2024–2026 the field has moved partially toward DiT (Diffusion Transformers) — pure transformer backbones with no U-Net skips. DiTs scale better past ~2B parameters; U-Nets still dominate sub-1B image-generation models because they're much more parameter-efficient on small data.

Step 8

Practical implementation notes

Step 9

Reading list