3.4.14 · D3Convolutional Neural Networks

Worked examples — Semantic segmentation (U-Net, FCN)

2,255 words10 min readBack to topic

This page is the "run the machine by hand" companion to Semantic segmentation (U-Net, FCN). We take every knob the topic exposes — output-size arithmetic, skip fusion, the pixel loss, degenerate inputs — and turn each into a fully worked, numerically checked example. Nothing here is hand-waved; every number reappears in the verification block.

Before we compute, a reminder of the two size laws we will lean on constantly. We only use symbols the parent note earned.

Why two different formulas? Because they answer opposite questions. The first asks "if I slide a -window with steps of , how many landing spots fit?" The second asks "if I first space the input pixels apart, then convolve, how big is the canvas?" Downsampling counts landing spots; upsampling counts a stretched canvas. Keep them mentally separate.


The scenario matrix

Segmentation math has a small, finite set of "shapes of problem." The table below lists every cell; the examples that follow are each tagged with the cell(s) they close.

# Cell class The tricky part it tests Covered by
A Clean downsample (padding, stride 1) "same" convolution keeps size Ex 1
B Downsample with stride 2, no padding floor division, size shrinks Ex 2
C Non-divisible input (odd size ÷ 2) floor drops a pixel — the degenerate case Ex 3
D Transposed conv upsample the other size law, ×2 growth Ex 4
E Skip connection mismatch → crop encoder bigger than decoder (U-Net) Ex 5
F Channel bookkeeping: add vs concat FCN addition vs U-Net concatenation Ex 6
G Pixel loss, single pixel softmax + cross-entropy at one location Ex 7
H Class imbalance limiting case why plain CE fails; weighted CE Ex 8
I Real-world word problem self-driving frame, end-to-end sizes Ex 9
J Exam twist / degenerate loss perfect prediction → loss ; wrong class → loss blows up Ex 10

Prerequisites used below: Pooling Layers (for the stride-2 shrink), Loss Functions (cross-entropy), Batch Normalization and Data Augmentation appear as side notes. If any symbol feels unfamiliar, it was built in the parent note.


Example 1 — Cell A: "same" convolution keeps the size

Forecast: guess before reading — does the size stay , drop to , or grow?

  1. Write the shrink law. . Why this step? Convolution is downsampling arithmetic; this is the correct law (not the transposed one).
  2. Substitute : . Why this step? Plugging numbers turns the abstract law into the concrete answer.
  3. Read the meaning. With padding a kernel, one row/column of zeros on each side exactly compensates for the two rows/columns the kernel would otherwise "eat." Output .

Verify: the standard rule "for a kernel, gives same output" holds. Modern padded U-Nets rely on this so decoder and encoder sizes align without cropping. ✔


Example 2 — Cell B: stride-2 pooling, no padding

Forecast: halved? off-by-one?

  1. Shrink law with : . Why this step? Pooling Layers is just a convolution-shaped scan; same landing-spot counting applies.
  2. Interpret. is even, so it halves cleanly: . No pixel is lost.

Verify: matches the parent note's U-Net table (" pool "). Even input ÷ stride-2 = exact halving. ✔


Example 3 — Cell C (degenerate): odd input, the floor bites

Forecast: ? ? ? A fraction is impossible — so which whole number?

  1. Shrink law: . Why this step? The floor is the whole point of this cell — landing spots is nonsense, so we round down.
  2. What just happened geometrically? The last column of the input had no room for a full window, so it is silently dropped. Output , not .
Figure — Semantic segmentation (U-Net, FCN)

Verify: . This is exactly why FCN needs its input divisible by 32 (five stride-2 stages, ) — otherwise dropped edge pixels make encoder/decoder sizes disagree. ✔


Example 4 — Cell D: transposed convolution upsamples ×2

Forecast: exactly ? more? less?

  1. Choose the correct law. We are growing, so use . Why this step? Using the shrink law here would give a wrong, smaller number — this cell exists to force the right choice.
  2. Substitute : . Why this step? Plug the bottleneck size to get the first decoder level.
  3. Picture it. Transposed conv spaces the 28 input pixels 2 apart (making a -wide gappy grid), then a -kernel sweep fills to . Clean doubling.

Verify: matches the parent U-Net table " upsample ." ✔


Example 5 — Cell E: skip mismatch, so we crop

Forecast: crop 8 total? 4 total? from where?

  1. Difference in size: pixels of extra encoder width. Why this step? Valid (no-pad) convs shrink each map slightly differently along the path, leaving the encoder larger — this cell tests that reality.
  2. Split symmetrically. Center-crop removes pixels from each side. Why this step? Cropping the center keeps the feature that best aligns spatially with the decoder pixel it will be fused with.
  3. Result: encoder , now matching the decoder for augmentation-robust concatenation.
Figure — Semantic segmentation (U-Net, FCN)

Verify: ✔. This is exactly the crop-and-concat step in the parent's decoder walkthrough. ✔


Example 6 — Cell F: channel arithmetic, add vs concat

Forecast: are they the same number? which is bigger?

  1. Addition (FCN): element-wise sum requires identical shapes; channels stay . Output . Why this step? Addition is a fold, not a stack — it cannot change channel count.
  2. Concatenation (U-Net): stack along channel axis: . Output . Why this step? Concat preserves all information from both paths, letting the next conv choose what to keep — the parent note's key U-Net argument.
  3. Cost note. The concat doubles channels, so the following conv has more input channels (more compute) — the price of keeping everything.

Verify: add , concat . ✔


Example 7 — Cell G: cross-entropy at a single pixel

Forecast: will it be near (confident-correct) or large (confused)?

  1. Softmax denominator . Why this step? Cross-entropy needs a probability; softmax turns raw logits into one (see Loss Functions).
  2. Numbers: , so .
  3. Probability of true class 0: .
  4. Loss . Why this step? The parent's pixel-wise CE is exactly of the true-class probability.

Verify: . Moderate loss — correct class but not fully confident. ✔


Example 8 — Cell H (limiting case): why plain CE fails under imbalance

Forecast: does plain CE "reward" the lazy model? does weighting punish it?

  1. Plain average CE. Background pixels contribute . Cell pixels () contribute each. Average . Why this step? The rare class is diluted by the majority — the loss looks small even though every cell is wrong.
  2. Weighted CE. Multiply each pixel's loss by its class weight, then divide by total weight. Weighted sum ; total weight . Weighted mean . Why this step? Up-weighting the rare class () re-inflates its influence so the optimizer can no longer ignore cells.
  3. Read the contrast. Plain loss vs weighted — nearly larger. The weighted objective forces the network to care about the .

Verify: plain , weighted . Weighted plain, confirming imbalance handling. ✔


Example 9 — Cell I: real-world self-driving pipeline

Forecast: does it land exactly on or overshoot?

  1. Encoder shrink: five halvings ⇒ divide by . . Encoder output . Why this step? Each stride-2 stage halves; five stages compose to (uses ResNet/VGG-style backbones via Transfer Learning).
  2. Upsample with the transposed law: . Why this step? We must recover the original resolution to label every road/pedestrian pixel.
  3. Interpret. Output — one class per original pixel, ready for the drivable-mask. Because is divisible by , no floor pixel was lost in step 1 (contrast Ex 3).

Verify: ; . Exact round-trip. ✔


Example 10 — Cell J (exam twist): the two loss extremes

Forecast: does perfect give exactly ? does wrong give exactly ?

  1. Case (i) probability of true class 1: . Loss . Why this step? A confident-correct prediction drives , so — small but never exactly zero (asymptote).
  2. Case (ii) probability of true class 1: . Loss . Why this step? A confident-wrong prediction drives , so . Here it's , and grows without bound as logits diverge.
  3. The limit picture. Cross-entropy is a funnel: at perfect, as the model confidently backs the wrong class. This unboundedness is why Batch Normalization and careful init matter — a single confidently-wrong pixel can dominate the gradient.

Verify: case (i) ; case (ii) . ✔


Recall Self-test

Why does an odd-sized map lose a pixel under stride-2 pooling? ::: The floor in discards the leftover edge that has no room for a full window. Add vs concat: which changes channel count? ::: Concatenation (); addition keeps channels fixed. What does cross-entropy loss approach for a confident-correct pixel? ::: from above (asymptotically, never exactly). Why crop encoder features in the original U-Net? ::: Valid (no-pad) convs leave the encoder map larger; center-crop aligns it to the decoder before concat.

Related tasks that reuse this size algebra: Object Detection (anchor grids) and Attention Mechanisms (token grids) both depend on the same downsampling bookkeeping.