TL;DR
U-Net is a convolutional network shaped like the letter U. The left arm shrinks the image and builds up abstract features. The right arm grows the image back to full size. At each resolution, a skip connection copies the left arm's feature map across to the matching point on the right, so the output has both the high-level meaning and the sharp local detail it needs to draw boundaries.
It was built for segmentation, where the output must give every pixel a label, and it works well even when only a few dozen labelled images exist.
Where you'll see it: in medical scans that need an organ or tumour outlined, in cell microscopy, and as the denoising backbone of many diffusion models.
Intuition
Two paths and a bridge
Picture a map. Zoomed far out, you can tell a district is residential or industrial, but not where one house ends and the next begins. Zoomed in, you see every house but lose track of the district. A segmentation model needs both at once. Whether a pixel is a cell membrane depends on the local edge, and also on the wider context that says this is a membrane and not a shadow.
A U-Net gets both by running two paths. The contracting path on the left applies convolutions and then max pooling, which halves the resolution. Each deeper level sees a bigger patch of the original image, so the deep layers know what surrounds each pixel. The expanding path on the right doubles the resolution back up. A coarse map alone is too blurry to place an edge, so at every level the expanding path also receives the feature map from the left at the same resolution. That is the skip connection, drawn as a horizontal arrow across the U.
Why not just squeeze everything through the middle? A plain encoder-decoder pushes the whole image through a small bottleneck, and the exact position of a boundary is one of the things that gets lost on the way. Skip connections let the decoder look at the original detail directly instead of rebuilding it from the bottleneck alone.
flowchart LR E1[Encoder level 1, full size] --> E2[Encoder level 2, half size] --> E3[Encoder level 3, quarter size] E3 --> B[Bottom, one eighth size] B --> D3[Decoder level 3] --> D2[Decoder level 2] --> D1[Decoder level 1, full size] --> O[1x1 conv: class scores] E1 -. skip .-> D1 E2 -. skip .-> D2 E3 -. skip .-> D3
The math
The building blocks
The contracting path repeats one pattern. Two 3x3 convolutions, each followed by a ReLU, then a 2x2 max pool with stride 2. Each pool halves the height and width, and the number of channels doubles. The original network used 64, 128, 256, 512 and then 1024 channels at the bottom.
The expanding path also repeats a pattern. A 2x2 up-convolution, which is a transposed convolution with stride 2, doubles the height and width and halves the channels. It is joined to the matching encoder map, then two 3x3 convolutions reduce the channels again. The final layer is a 1x1 convolution that turns the 64 channels into one score per class for each pixel . A Softmax turns those scores into probabilities:
Here is the number of classes and is the probability that pixel belongs to class .
Skip connections as concatenation
The skip connection is a concatenation along the channel axis, not a sum. If the upsampled decoder map has channels and the encoder map has channels, the joined map has channels. The convolutions that follow learn how to combine the two sources.
The original network uses unpadded 3x3 convolutions, so each one trims two pixels from each dimension. The encoder maps are therefore larger than the decoder maps at the same level. Before concatenating, the encoder map is cropped from its centre to match. Modern implementations usually pad instead, so no cropping is needed.
The weighted loss
The network is trained with a cross-entropy loss summed over pixels, with each pixel weighted:
Here is the true label of pixel . The weight map has two parts. The first, , corrects for class frequency, so rare classes count for more. The second adds weight near the borders between touching objects:
Here and are the distances from to the nearest and second-nearest cell border. The paper uses and pixels. Without this term, two cells that touch would be learned as one blob, because the background gap between them is only a few pixels wide and costs almost nothing to miss.
The border term in depends on only, so it is easy to plot:
- w(s) with w_c = 1, w_0 = 10, sigma = 5
Data augmentation with elastic deformation
The paper was trained on only a few dozen annotated microscopy images, so it augmented them heavily. The augmentation warps the image with a smooth random deformation, produced by a coarse grid of random displacement vectors that is interpolated to full size. Tissue and cells change shape a little from slide to slide, so the warped images are realistic. See Data Augmentation for the general idea.
Worked examples
The paper feeds in a tile and gets a output. Trace the height along the left arm, then the right arm.
- Input: .
- Level 1, two unpadded 3x3 convolutions: . Pool: .
- Level 2, two convolutions: . Pool: .
- Level 3, two convolutions: . Pool: .
- Level 4, two convolutions: . Pool: .
- Bottom, two convolutions: .
- Up-convolution: . The level 4 map is , so crop it to , concatenate, then two convolutions: .
- Up-convolution: . Crop the level 3 map from to . Two convolutions: .
- Up-convolution: . Crop the level 2 map from to . Two convolutions: .
- Up-convolution: . Crop the level 1 map from to . Two convolutions: .
Interpretation: the unpadded design loses a margin at every convolution, so the output is smaller than the input by pixels in each dimension, which is pixels lost on every side. To label a full image, the authors tile it with overlapping windows and keep only the central pixels of each. The overlap supplies the context around each tile's output. At the edges of the whole image there is no neighbouring pixel to borrow, so the paper extrapolates the missing context by mirroring the image.
For a sense of size, one 3x3 convolution from to channels has parameters, counting the bias. The bottom level, with channels, is far heavier.
Take , and . A pixel sits pixels from one cell border and pixels from the other.
- Sum of distances: .
- Square it: .
- Divide by : .
- Exponentiate: .
- Multiply by : .
- Add : .
Interpretation: a pixel in the gap has about 3.8 times the weight of a background pixel far from any border. Now the loss for this pixel. Suppose the network gives the correct class probability . The per-pixel term is , and the weighted term is . The same confident prediction on a pixel far from any border would contribute only . The network is therefore pushed hardest to get the gap right.
A three-class pixel, background, cell and border, has scores . Find its probabilities.
- Exponentiate each score: , , .
- Sum: .
- Divide: .
These sum to 1, as they must. The background wins with probability . If the true label is border, the loss for this pixel is , which is large, so the gradient pushes the border score up. Only the score differences matter for the probabilities, so adding the same constant to all three scores would change nothing.
Tradeoffs and failure modes
| Situation | Problem | Better choice |
|---|---|---|
| Output must match input size and have sharp edges | A plain encoder-decoder blurs boundaries | U-Net skip connections |
| Images are much larger than GPU memory | Full-image inference does not fit | Tile with overlap and keep the tile centres |
| Very few labelled images | Overfitting | Heavy augmentation such as elastic deformation, or a pretrained encoder |
| Objects touch or are very small | Errors cluster at borders and rare classes vanish in the loss | Weight map or class-balanced loss |
| The answer depends on context far beyond the receptive field | Deep U-Nets still see only a local window at the bottom | Attention or a transformer backbone |
| 3D volumes such as CT or MRI | 2D slices lose depth context and 3D convolutions are costly | 3D U-Net with smaller channel counts |
The most common practical failure is shape bookkeeping. Unpadded convolutions silently shrink the output, and a mismatch in crop size breaks concatenation. Padding to keep sizes equal avoids most of this, at a small cost in border artifacts.
History
Olaf Ronneberger, Philipp Fischer and Thomas Brox, working at the University of Freiburg, published U-Net in 2015. Their target was biomedical microscopy, where annotated images are expensive to produce and a single scan can be very large. Earlier convolutional segmenters either needed sliding-window patches, which were slow because neighbouring windows repeated most of the work, or lacked precise boundaries. The U shape fixed both problems in one design. The network won several categories of the ISBI 2015 cell tracking challenge, which drew attention beyond medicine.
It caught on because the design was simple to adapt, and because the same encoder-decoder with skips reappeared in later work on dense prediction. The fully convolutional networks of Long, Shelhamer and Darrell, also from 2015, had already shown that learned upsampling could produce full-resolution maps. U-Net added the skip connections that made the boundaries sharp. When denoising diffusion models were introduced in 2020, they chose a U-Net as the network that predicts the noise at each step, and that choice has been carried into many later image generators.
Check yourself
QuestionRecall: what are the two paths of a U-Net, and what does each skip connection join?Show answer
The contracting path (left arm) applies convolutions and max pooling, halving the resolution at each level while building up context. The expanding path (right arm) applies up-convolutions to double the resolution back up. Each skip connection joins the encoder feature map at some level to the decoder map at the same resolution, by concatenating them along the channel axis. This gives the decoder the fine spatial detail that pooling discarded.
QuestionComputation: w_c = 1, w_0 = 10, sigma = 5. A pixel has d_1 = 2 and d_2 = 4. What is its weight?Show answer
Here , so and . The exponent is , and . Multiply by to get , then add . The weight is .
QuestionWhy: why does the loss need a weight map, when cross-entropy alone already penalises wrong pixels?Show answer
Plain cross-entropy treats every pixel equally. In a dense cell image, the background gap between two touching cells is a few pixels wide, so missing it costs very little in total. The network can then merge the two cells into one blob and still score well. The border term gives those gap pixels a large weight, so merging them is expensive. The class term does the same for rare classes that would otherwise be drowned out by the background.
QuestionApplied: you need to segment 4096 x 4096 microscope images with a GPU that can only hold a 572 x 572 input at once. How do you run the model?Show answer
Run the network on overlapping tiles. Use an input window of 572 by 572 and a stride of 388 pixels, so that the 388 by 388 output centres tile the full image with no gaps. Since , you need tiles, and you first extend the image by mirroring its edges (by 92 pixels per side, plus a little extra for the last tile) so that edge tiles have full context. The output of each tile is trusted only in its central region, where the context is complete, and the overlapping margin supplies that context at the image's interior edges. Stitch the central outputs together to get the full mask. A padded modern variant would make the tiling simpler, but the overlap-and-crop logic is the same idea.
Sources
- Ronneberger, Fischer, Brox (2015), U-Net: Convolutional Networks for Biomedical Image Segmentation: arxiv.org/abs/1505.04597
- Long, Shelhamer, Darrell (2015), Fully Convolutional Networks for Semantic Segmentation: arxiv.org/abs/1411.4038
- U-Net (Wikipedia)