U-Net

A U-Net (Ronneberger et al., MICCAI 2015) is a convolutional encoder-decoder with skip connections. It is built for dense prediction, where the output holds one value or vector per pixel. It maps an input to an output. The encoder alternates convolutions with downsampling by max or average pooling or by stride-2 convolution. Each level halves the height and width and usually increases the number of channels. The decoder mirrors the encoder with upsampling by transpose convolution or unpooling, and each level doubles the height and width. Skip connections copy the encoder activations at each resolution and concatenate them along the channel axis with the decoder activations of the same size. The convolutions after each concatenation combine fine detail from the encoder with context from the decoder. Drawn with resolution decreasing downward, the network has the shape of a U, which gives it its name. The original U-Net used valid convolutions, which shrink each map by two pixels per layer. Its encoder maps were therefore cropped to the decoder’s size before concatenation. Later versions use zero padding and need no cropping. The original network has no dense layers, so after training it runs on images of any size.

The design meets two needs at once. Each output pixel needs a large receptive field, and the network should not waste computation. stacked convolutions with stride 1 have a receptive field of pixels, so seeing pixels takes layers. Running a CNN on a patch around every pixel recomputes the same features many times. Downsampling grows the receptive field quickly and makes deep layers cheap. It also discards high-frequency detail, so a decoder without skips produces blobby outputs that miss object boundaries. The skips restore that detail. For kernel size , stride and padding , a convolution maps size to , and a transpose convolution maps to .

U-Nets are used for semantic segmentation (the original use, on biomedical images), depth and surface-normal prediction, colorization, the generator of 8.4. Pix2Pix, and the denoiser in diffusion and flow matching models. Every U-Net is an encoder-decoder. An encoder-decoder without skip connections is not a U-Net.

Counting Overlapping Patches

The lecture asks how many times a per-patch CNN filters a single pixel and gives the answer . That count holds when is the patch half-width, so that each patch is pixels. For patches of side cut around every pixel, each pixel lies in patches. In both readings the repeated work grows with the square of the patch size.

Two Meanings of Encoder-Decoder

In a U-Net, the encoder is the downsampling path and the decoder is the upsampling path of one convolutional network. In transformer encoder-decoders, the same words name two stacks of attention layers joined by cross-attention. The shared idea is a compressed internal representation, but the mechanisms differ.

Intuition

A classification CNN shrinks an image to one vector. Per-pixel tasks, such as labeling which pixels belong to a cyclist, need an output as large as the input. Each output pixel still needs context from far away. A patch of shirt is much easier to label “person” when the network can see the rest of the person. Two naive designs fail. Stacking stride-1 convolutions keeps full resolution, but the receptive field grows by only two pixels per layer. Cropping a patch around every pixel and classifying it gives enough context. Neighboring patches overlap almost entirely, however, so the same features are computed again and again. Large receptive fields and low cost are in direct conflict for both designs.

The encoder-decoder resolves the conflict by downsampling toward the middle of the network and upsampling back out. Every halving doubles the reach of the following convolutions and divides the cost per layer by four. Downsampling also loses detail. Like a downsampled image in resampling, the bottleneck has lost the high frequencies, and the decoder cannot invent them. The U-Net copies the encoder’s high-resolution activations across to the decoder, as the dashed arrows in the figure show. The figure is the MNIST U-Net from Project 3, with hidden channels.

\usepackage{tikz}
\usepackage{amsmath}
\begin{document}
\definecolor{hot}{HTML}{D9483B}
\definecolor{cold}{HTML}{2F80ED}
\definecolor{mid}{HTML}{8A8A8A}
\begin{tikzpicture}[>=latex,
  enc/.style={draw,thick,cold,fill=cold!12,text=black,minimum width=1.7cm,align=center,font=\small},
  dec/.style={draw,thick,hot,fill=hot!12,text=black,minimum width=1.7cm,align=center,font=\small},
  lab/.style={font=\footnotesize\ttfamily}]
\node[draw=mid,minimum width=1.2cm,minimum height=1.6cm,align=center,font=\small] (in) at (-3.0,0) {input\\$1{\times}28{\times}28$};
\node[enc,minimum height=1.6cm] (e1) at (0,0) {$D$\\$28{\times}28$};
\node[enc,minimum height=1.1cm] (e2) at (0,-2.4) {$D$\\$14{\times}14$};
\node[enc,minimum height=0.8cm] (e3) at (0,-4.5) {$2D$\\$7{\times}7$};
\node[draw,thick,minimum width=1.7cm,minimum height=0.4cm,font=\small] (b) at (3.6,-6.2) {$2D$, $1{\times}1$};
\node[dec,minimum height=0.8cm] (d3) at (7.2,-4.5) {$2D{+}2D$\\$7{\times}7$};
\node[dec,minimum height=1.1cm] (d2) at (7.2,-2.4) {$D{+}D$\\$14{\times}14$};
\node[dec,minimum height=1.6cm] (d1) at (7.2,0) {$D{+}D$\\$28{\times}28$};
\node[draw=mid,minimum width=1.2cm,minimum height=1.6cm,align=center,font=\small] (out) at (10.3,0) {output\\$1{\times}28{\times}28$};
\draw[->,thick] (in) -- node[above,lab] {ConvBlock} (e1);
\draw[->,thick] (e1) -- node[left,lab] {DownBlock} (e2);
\draw[->,thick] (e2) -- node[left,lab] {DownBlock} (e3);
\draw[->,thick] (e3) |- node[pos=0.25,left,lab] {Flatten} (b);
\draw[->,thick] (b) -| node[pos=0.75,left,lab] {Unflatten} (d3);
\draw[->,thick] (d3) -- node[left,lab] {UpBlock} (d2);
\draw[->,thick] (d2) -- node[right,lab] {UpBlock} (d1);
\draw[->,thick] (d1) -- node[above,lab] {ConvBlock} node[below,lab] {+Conv} (out);
\draw[->,dashed,thick] (e1) -- node[above,font=\small] {copy and concatenate} (d1);
\draw[->,dashed,thick] (e2) -- node[above,font=\small] {copy and concatenate} (d2);
\draw[->,dashed,thick] (e3) -- node[above,font=\small] {copy and concatenate} (d3);
\node[font=\small,align=center] (t) at (9.6,-4.4) {$t$, $c$ enter\\via FCBlocks};
\draw[->,mid,thick] (t) -- (7.25,-3.45);
\draw[->,mid,thick] (t) -- (7.25,-5.6);
\node[cold,font=\small] at (0,1.2) {encoder};
\node[hot,font=\small] at (7.2,1.2) {decoder};
\end{tikzpicture}
\end{document}

Properties

(Receptive Field Arithmetic) Each downsampling doubles the number of input pixels that every later convolution adds to the receptive field.

Track two numbers through the network: the receptive field , and the jump , the distance in input pixels between neighboring outputs of a layer. Both start at . A layer with kernel size and stride updates them as

A stride-1 convolution adds to . Without downsampling stays , which gives after layers. After halvings , and each convolution adds pixels. Each halving also divides the area of the feature map by four, so deep layers stay cheap even with more channels. The receptive field is a window, and padding can cover part of it. Features near the border see less of the image than features near the center.

(Where Detail Is Lost) The high-frequency detail sits in the early, high-resolution encoder layers, and skip connections carry it to the output.

Halving the resolution halves the highest frequency that a feature map can represent, the same limit that motivates anti-aliasing. Fine edges and exact object boundaries are high-frequency content. A decoder that sees only the bottleneck must guess them, so segmentation masks come out blobby and slightly misaligned with the image. Copying the early encoder activations forward gives the decoder that detail at the right resolution. The convolutions after each skip learn how to fuse the copied detail with the decoded context.

(Concatenating Skips) U-Net skips stack channels side by side, so the next convolution can weigh detail and context separately.

Concatenating a -channel tensor with a -channel tensor of the same height and width gives channels. The next convolution has separate weights for each group. A residual block in a ResNet instead outputs , where is a few convolution layers. Addition keeps the channel count, and residual blocks let networks with hundreds of layers train. A U-Net skip spans the whole encoder-decoder at one scale, while a residual skip spans a few layers. Many denoising U-Nets use residual blocks inside each level and concatenating skips across levels.

(Skips Carry What Smoothing Removes) An encoder that averages away noise also averages away sharp edges, and the skip connection brings part of the edge back.

Consider a 1D signal that steps from to , observed with independent noise. An encoder level that convolves with divides the noise variance by three, and it also spreads the step over three samples. A pooling then merges neighboring samples, and duplication upsampling gives each merged pair identical values. The two outputs on either side of a jump that falls inside a pooling window become equal, so the decoder cannot place the edge more precisely than the pooling grid. Combining the upsampled signal with the encoder’s first-level output restores part of the difference between neighbors. The skip also carries that level’s noise, which is less smoothed than the bottleneck’s. A skip therefore trades some noise for spatial precision, and the convolutions after it learn how to weight the two. Max pooling in such an encoder adds a further upward bias on noisy flat regions, as 5.2.2. Pooling shows.

(Per-Patch Waste) Classifying a patch around every pixel recomputes shared features once per overlapping patch.

With patches cut around every pixel, each pixel lies in patches, and the first layer processes it again in each one. For patches that is times. A U-Net computes each feature map once over the whole image and shares it among all output pixels. The encoder computes the features that a patch classifier would compute, and the decoder turns its coarse output back into a full-resolution map.

(Conditioning the Network) Project 3 injects the time and the class by scaling and shifting whole channels.

An FCBlock is a linear layer, a GELU, and a second linear layer. It maps the scalar , or the one-hot class vector , to one number per channel. Each number multiplies or shifts its entire channel, the same at every pixel. The time-only model replaces the output of Unflatten by and the output of the first UpBlock by . The class-conditioned model uses and . Here come from FCBlocks applied to and from FCBlocks applied to . During training the class vector is zeroed 10% of the time, which enables classifier-free guidance. One set of weights then serves every time step and every class.

(Block Sizes Vary) The number of levels, the channel counts and the bottleneck size are design choices.

Some networks stop downsampling while the bottleneck still has spatial extent. Others encode all the way to a single vector and decode from it. Project 3 does the second. Its Flatten layer averages the map to , and its Unflatten layer expands it back with a transpose convolution. Diagrams of U-Nets should not be read as fixed block sizes.

Examples

(Easy) Tensor Shapes in the Project 3 U-Net

Project 3 builds a U-Net for MNIST digits with hidden channels. Written as (kernel, stride, padding), DownConv is a convolution , UpConv is a transpose convolution , Flatten is a average pool, and Unflatten is a transpose convolution .

(Medium) Receptive Field of the Project 3 Encoder

The encoder of the same network applies, in order, two stride-1 convolutions (a ConvBlock), a DownConv and two stride-1 convolutions (a DownBlock), and a second DownBlock. All convolutions use padding .

See Also