3.4.14 · D4Convolutional Neural Networks

Exercises — Semantic segmentation (U-Net, FCN)

2,618 words12 min readBack to topic

This page is a self-test ladder for Semantic segmentation (U-Net, FCN). Cover the solution, try the problem, then reveal. Each problem is labelled L1 → L5 by cognitive demand. Every symbol used below is defined the moment it appears — nothing is assumed.

Before we begin, one shared picture of the shapes we will juggle all page long:

Figure — Semantic segmentation (U-Net, FCN)

Level 1 — Recognition

L1.1 — Detection vs. segmentation output shape

An image is . You run (a) an object detector and (b) a semantic segmentation network with classes. What is the shape of the output each one produces?

Recall Solution
  • Detector: a short list of boxes, each box being — a handful of numbers, not tied to image size. It says "there is a thing around here."
  • Segmentation: a dense label map the same height and width as the input, , where every entry is a class ID in . Before the argmax it is logits.

The key recognition: segmentation output resolution matches the input; detection output does not. See Object Detection for the box-based alternative.

L1.2 — Add or concatenate?

Which architecture adds skip features and which concatenates them: FCN or U-Net?

Recall Solution
  • FCN uses element-wise addition: , both must have the same channel count .
  • U-Net uses concatenation along the channel axis: stacking (encoder, channels) on top of (decoder, channels) to make channels.

Mnemonic below.


Level 2 — Application

L2.1 — Transposed convolution output size

Why this formula and not the normal-conv one? A normal convolution shrinks a map: it slides a window and outputs fewer positions. Transposed convolution answers the opposite question — "what input size, run forward, would have produced this output?" — so we solve the forward-conv shape equation backwards. Geometrically it inserts zeros between every pair of input pixels (dilating the grid), then does an ordinary convolution over that inflated grid. The picture:

Figure — Semantic segmentation (U-Net, FCN)

Problem: input , kernel , stride , padding . Compute .

Recall Solution

Substitute directly: So : the map exactly doubled, which is the standard "upsample ×2" step of a decoder. That is why a transposed conv is the everyday choice.

L2.2 — FCN-32s one-shot upsample

FCN-32s takes a score map and upsamples it back to a input in one transposed convolution with stride . If padding , what kernel makes exactly?

Recall Solution

Set , , , in : A kernel of makes sense: to "smear" one coarse pixel over a block you need a filter that wide. This is why FCN-32s outputs are blurry — one decision is spread over the whole image.

L2.3 — Downsampling count

A U-Net encoder halves spatial size at each max-pool (see Pooling Layers). Starting from , after how many pool steps do you reach ?

Recall Solution

Each pool divides by . We need : Four pooling stages. (Check: , four arrows. ✓)


Level 3 — Analysis

L3.1 — Why crop in the original U-Net?

The original U-Net uses valid (no-padding) convolutions. At one skip level the encoder map is but the decoder's upsampled map is . Explain why they differ and compute the crop.

Recall Solution

A valid conv removes a -pixel border on each side (a window can't be centred on the outer ring), so each conv shrinks the side by . Two convs per block ⇒ shrink by . Because encoder and decoder go through different numbers of these shrinking convs before meeting, their sizes drift apart.

To concatenate they must match. Crop the encoder down to by removing pixels from every side (a centre crop). Now both are and can be stacked.

Modern U-Nets add padding to convs so nothing shrinks and no crop is needed — this keeps input and output the same size.

L3.2 — Channels after concatenation

At a decoder level, the encoder skip feature is and the decoder's upsampled feature is . What is the shape after the U-Net concatenation? Then a conv with output filters follows — what shape is its output (with padding)?

Recall Solution

Concatenation stacks channels: , giving . Using the concatenation rule with . The following conv (padded) keeps and sets channels to its filter count: output . So concatenation doubles channels and the very next conv compresses them back — that conv is where the two paths are actually fused.

L3.3 — Receptive field intuition

Why does the U-Net bottleneck (smallest, deepest map) carry the most semantic information despite having the fewest pixels?

Recall Solution

Each pooling stage doubles the region of the original image that one deep pixel "sees" — its receptive field. After 4 pools a single bottleneck pixel summarises a large patch, so it can answer "what object is here" (needs global context) but not "where exactly is the boundary" (needs local detail, long since pooled away). That's precisely why skip connections re-inject the shallow, high-resolution encoder features into the decoder — deep = what, shallow = where. Related idea in Residual Networks (ResNet), where skips instead help gradient flow.


Level 4 — Synthesis

L4.1 — Pixel-wise cross-entropy by hand

Problem: a tiny image (), classes. Logits at the single pixel are and the true class is . Compute the loss (natural log). Round to 3 decimals.

Recall Solution

Step 1 — softmax denominator (WHY: normalises scores into probabilities). Step 2 — probability of the true class . Step 3 — loss for the one pixel (WHY: of the correct-class prob; the here). The model is only sure of the right class, so it pays a moderate nats — not zero, not huge.

L4.2 — Weighted loss for class imbalance

Background is of pixels, cells are . To stop the network from lazily predicting "all background," we use weighted cross-entropy (see Loss Functions). A common choice sets each class weight inversely proportional to its frequency. Compute the weights for background and cell, then normalise so the smaller weight equals .

Recall Solution

Inverse-frequency weights (WHY: rare classes get amplified so each contributes equally overall). Normalise so the smallest = 1: divide both by : A misclassified cell pixel now costs a misclassified background pixel, forcing the network to care about the tiny structures. Pair this with heavy Data Augmentation since medical datasets are small.

L4.3 — Design an FCN-8s fusion path

You have VGG feature maps: pool3 = , pool4 = , conv7 = , all for a input with classes. Write the ordered sequence of upsample-and-add steps that yields a output.

Recall Solution
  1. conv on conv7 score map.
  2. Transposed conv ×2 → ; add to (-conv'd) pool4 ().
  3. Transposed conv ×2 → ; add to (-conv'd) pool3 ().
  4. Transposed conv ×8 → final prediction.

Check the total factor: . ✓ The three fusions inject progressively finer detail before each blow-up, so edges stay crisp. The convs exist only to force every skip source to exactly channels so addition is legal.


Level 5 — Mastery

L5.1 — Trace a full U-Net level end to end

A padded (modern) U-Net decoder receives a bottleneck. Trace one decoder level: (a) transposed conv ×2 halving channels, (b) concat with the matching encoder skip, (c) two padded convs each with filters. Give the shape after each sub-step, and the channel count just before the fusion conv.

Recall Solution
  • (a) Transposed conv : (spatial doubled, channels halved — the decoder's job).
  • (b) Concat with encoder : channels . This is the count just before the fusion conv: 1024.
  • (c) First padded conv, filters: . Second padded conv, filters: .

End of level: , ready to feed the next (higher-resolution) decoder level. Notice the symmetry — every encoder resolution has an exactly mirrored decoder resolution; that mirror is the "U."

L5.2 — Why concatenation beats addition for tiny structures (argue it)

In one paragraph, using the information-preservation idea, justify U-Net's concatenation over FCN's addition specifically for microscopy cell membranes.

Recall Solution

Addition computes : if the encoder channel encodes "thin bright membrane" and the decoder channel encodes "dark cytoplasm," their sum is a single blurred number and the membrane signal can be cancelled or drowned. Concatenation keeps and as separate channels, so the fusion conv can learn a weight that says "trust the encoder's membrane channel here, ignore the decoder's." No information is destroyed before the network gets to decide. For hair-thin membranes that occupy a couple of pixels, that non-destructive choice is the difference between detecting them and losing them. (This "let the network choose" theme also underlies Attention Mechanisms.)

L5.3 — Consistency check across the whole page

Verify that FCN-8s's fusion factors () and FCN-32s's single factor () both recover from , i.e. that both nets are internally consistent.

Recall Solution

FCN-32s: . ✓ FCN-8s: . ✓ Both reach the input resolution; FCN-8s just distributes the across three learnable steps with skip fusions in between, so its output is sharper for the same final size. This is the whole point of the FCN family: same total upsampling, more intermediate detail = better boundaries.


Recall Self-test summary (cover and recite)

Transposed-conv size formula ::: FCN combines skips by ::: element-wise addition (channels must match, so conv first) U-Net combines skips by ::: concatenation along channels (no channel match needed) Why the U-Net bottleneck is "semantic not spatial" ::: pooling grew the receptive field but destroyed fine location Weighted CE weight rule for imbalance ::: weight Cropping is needed only when ::: valid (no-padding) convolutions cause size drift