Search

Search papers and lessons

Fully Convolutional Network

Level 3 · 12 min read

A fully convolutional network has no fully connected layers, so it accepts images of any size and outputs a map of predictions, one per location, instead of a single label.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Linear algebra
  • Neural network basics
  • Architectures
  • Probability
  • Sign in to see your progress

TL;DR

A fully convolutional network is a Convolutional Neural Network with no fully connected layers. Built only from convolutions, it accepts an image of any size and returns a grid of predictions, one for each region of the input, instead of one label for the whole picture. Upsampling layers and skip connections then bring that grid back to the input's resolution, so the network can label every pixel.

Where you'll see it: in segmentation models that outline roads, organs or crops, and in robot-manipulation networks that output a heatmap of where to grasp (see Transporter Network).

Intuition

The classifier ends in a vector

A standard image classifier has two halves. The convolutional layers turn the picture into a stack of feature maps. The fully connected layers then flatten those maps into one long vector and produce a single score per class. The fully connected half needs a fixed input size, so every photo must be cropped or resized to the same shape first. Its output also has no sense of position: the network says "cat", not "cat in the left corner".

Segmentation needs the opposite. It must keep a grid, with an answer at every position. The question is whether the fully connected half can be dropped without losing what it does.

The fully connected layer was a convolution all along

The key observation, made by Jonathan Long, Evan Shelhamer and Trevor Darrell, is that a fully connected layer applied to a feature map is the same computation as a convolution whose kernel covers the whole map. A convolution slides its kernel over a larger input and computes that same dot product at every position. Replace the fully connected layers with convolutions and the network no longer needs a fixed size. Feed it a bigger image and you get a bigger output grid, with the same weights reused at every position.

Each cell of that grid sees a large patch of the image and says what is in it. Cells near a cat score high for "cat", and cells over grass do not. That is a dense prediction: a map of scores that lives in the same spatial layout as the image.

flowchart LR
  A[Image of any size] --> B[Convolution and pooling stages]
  B --> C[Coarse grid of class scores]
  C --> D[Upsample to input size]
  D --> E[Score per pixel]
  B -.skip connection.-> D

Coarse first, then fine

Pooling halves the grid several times, so the score grid is coarse. In a VGG-style backbone the cells are 32 pixels apart. That is enough to say where a large object is, but the boundaries are blurry. The fix has two steps. First, upsample the coarse grid back to the input size. Second, add in scores from earlier, finer layers, which still remember where the edges were. The network is then a classifier at every location with a decoder that puts the result back in place.

The math

A fully connected layer is a convolution

Take a feature map with hh rows, ww columns and CC channels. A fully connected layer with DD outputs computes, for each output dd,

yd=∑c=1C∑i=1h∑j=1wWd,c,i,j xc,i,j+bd.y_d = \sum_{c=1}^{C} \sum_{i=1}^{h} \sum_{j=1}^{w} W_{d,c,i,j}\, x_{c,i,j} + b_d.

A convolution with DD filters, each of size h×w×Ch \times w \times C, computes the same weighted sum. If the input is exactly h×w×Ch \times w \times C, the convolution has only one position to slide to, so its output is 1×1×D1 \times 1 \times D, identical to the fully connected layer. The weights are literally the same numbers, reshaped.

Now give the convolution a larger input, H×W×CH \times W \times C with H≥hH \ge h and W≥wW \ge w. The filter slides over every valid position and produces

(H−h+1)×(W−w+1)×D(H - h + 1) \times (W - w + 1) \times D

outputs, one fully connected computation per window. This is the whole idea of the fully convolutional network. The layer is still the same layer; it has just been applied to more places.

Output size and upsampling

Suppose the coarse map has stride ss, meaning each cell is ss input pixels apart. An input of length nn gives a coarse map of about n/sn/s cells. To go back up, use a Transposed Convolution with stride ss, kernel length kk and padding pp at each end. Its output length is

n′=(n−1) s+k−2p.n' = (n - 1)\,s + k - 2p.

Choosing k=2sk = 2s and p=s/2p = s/2 gives n′=(n−1)s+s=nsn' = (n-1)s + s = ns, which is the exact factor-of-ss enlargement.

Fusing scores from earlier layers

Let SℓS_\ell be the score map produced by a 1×11 \times 1 convolution (a convolution with a single-pixel kernel, which mixes channels at each position and acts like a small dense layer applied to every location) on top of pooling stage ℓ\ell. Let upm\mathrm{up}_m denote upsampling by a factor mm. The version that fuses the stride-32 and stride-16 stages is

Sfused=up2(S5)+S4,thenup16(Sfused).S_{\text{fused}} = \mathrm{up}_2(S_{5}) + S_{4}, \qquad \text{then} \quad \mathrm{up}_{16}(S_{\text{fused}}).

The factor 2 aligns the coarse stage S5S_5 with the finer grid of S4S_4, so they can be added cell by cell. The final upsampling by 16 brings the sum to input resolution. Adding a stride-8 stage S3S_3 in the same way gives the finer version of the model.

Training uses the per-pixel cross-entropy from Image Segmentation, applied to the full-resolution output.

Worked examples

Example
Tiny: one dense layer, two ways

Take a single-channel 2×22 \times 2 input and a dense layer with one output. Flatten the input as (1,2,3,4)(1, 2, 3, 4) and use weights (1,0,0,−1)(1, 0, 0, -1). The output is 1⋅1+0⋅2+0⋅3+(−1)⋅4=−31 \cdot 1 + 0 \cdot 2 + 0 \cdot 3 + (-1) \cdot 4 = -3.

Now write the same weights as a 2×22 \times 2 kernel, K=(100−1)K = \begin{pmatrix} 1 & 0 \\ 0 & -1 \end{pmatrix}, and convolve it with the same input. The output is the same −3-3, because the kernel's elementwise products with the input are the dense layer's products.

  1. Apply the kernel to a larger 3×33 \times 3 input, X=(120341025)X = \begin{pmatrix} 1 & 2 & 0 \\ 3 & 4 & 1 \\ 0 & 2 & 5 \end{pmatrix}.
  2. Top-left window (1234)\begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}: 1−4=−31 - 4 = -3.
  3. Top-right window (2041)\begin{pmatrix} 2 & 0 \\ 4 & 1 \end{pmatrix}: 2−1=12 - 1 = 1.
  4. Bottom-left window (3402)\begin{pmatrix} 3 & 4 \\ 0 & 2 \end{pmatrix}: 3−2=13 - 2 = 1.
  5. Bottom-right window (4125)\begin{pmatrix} 4 & 1 \\ 2 & 5 \end{pmatrix}: 4−5=−14 - 5 = -1.

The output is the 2×22 \times 2 map (−311−1)\begin{pmatrix} -3 & 1 \\ 1 & -1 \end{pmatrix}.

Interpretation: the dense layer would reject the 3×33 \times 3 input, because it expects exactly four values. The convolution takes it in stride and returns one answer per window, with the same weights. Those four answers are the dense layer's answers on four different patches.

Example
Standard: output sizes on a 320 by 320 image

A VGG-style backbone halves the grid five times, with stride 2 each time, so its coarse output has stride 32.

  1. Coarse grid: 320/32=10320 / 32 = 10. The coarse map is 10×1010 \times 10, and each cell covers a 32×3232 \times 32 patch of the input.
  2. Upsample with a transposed convolution of kernel k=64k = 64, stride s=32s = 32 and padding p=16p = 16: n′=(10−1)⋅32+64−2⋅16=288+32=320n' = (10 - 1) \cdot 32 + 64 - 2 \cdot 16 = 288 + 32 = 320.
  3. The output is 320×320320 \times 320, back at the input size.
  4. Now reuse the same network on a 640×480640 \times 480 image. The coarse map becomes 20×1520 \times 15, and the upsampled output is again 640×480640 \times 480, with no change to the weights.

Interpretation: the network adapts its output to the input size automatically. Each coarse cell is responsible for a 32×3232 \times 32 block, which is why the boundaries are blurry before fusion.

Example
Standard: fusing a coarse score with a finer one

Take one class, "cat", and a 2×22 \times 2 block of stride-16 cells from pool4 whose cat scores are (0.5, 1.0, −0.5, 0.0)(0.5,\ 1.0,\ -0.5,\ 0.0). The stride-32 score from pool5 for the same region is 2.02.0, and upsampling by 2 copies it to all four cells.

  1. Upsampled pool5 score: (2.0, 2.0, 2.0, 2.0)(2.0,\ 2.0,\ 2.0,\ 2.0).
  2. Add the pool4 scores: (2.0+0.5, 2.0+1.0, 2.0−0.5, 2.0+0.0)=(2.5, 3.0, 1.5, 2.0)(2.0 + 0.5,\ 2.0 + 1.0,\ 2.0 - 0.5,\ 2.0 + 0.0) = (2.5,\ 3.0,\ 1.5,\ 2.0).
  3. The fused map keeps the variation from pool4, so the cell with score 3.0 is the most likely cat location, while pool5 sets the overall level.

Interpretation: the coarse score says that a cat is probably here. The fine score says where within the block the cat's evidence is strongest. Adding them gives a map with both.

Example
Realistic: the classifier head of a 21-class segmenter

Take a VGG-style backbone whose last pooling stage gives a 7×7×5127 \times 7 \times 512 feature map on 224×224224 \times 224 inputs. The original classifier has a fully connected layer with 4096 outputs.

  1. Parameters in that fully connected layer: 7⋅7⋅512=25,0887 \cdot 7 \cdot 512 = 25{,}088 inputs, and 25,088×4096=102,760,44825{,}088 \times 4096 = 102{,}760{,}448 weights. Written as a convolution, it is 4096 filters of size 7×7×5127 \times 7 \times 512, the same 102,760,448102{,}760{,}448 weights.
  2. Replace the 4096-way classifier with a 1×11 \times 1 convolution to 21 classes (PASCAL VOC's 20 object classes plus background). It has 4096×21=86,0164096 \times 21 = 86{,}016 weights and 21 biases, 86,03786{,}037 parameters in total.
  3. On a 320×320320 \times 320 input the backbone gives a 10×1010 \times 10 grid of 4096 features, and the head gives a 10×10×2110 \times 10 \times 21 score map.
  4. Upsampling that map to 320×320×21320 \times 320 \times 21 gives one score per class per pixel. Taking the arg-max over the 21 classes gives the segmentation.

Interpretation: the head that dominated the original network's size is still there, unchanged, as a convolution. What changed is that it now runs at every location, so the same weights answer "what is here?" everywhere in the image.

Tradeoffs and failure modes

The fully convolutional design works well when the important information is local and the output needs to match the input's layout. It struggles when the network has to see the whole scene at once, or when fine detail is lost on the way down.

SituationProblemBetter choice
Output taken straight from the stride-32 mapBoundaries are blurry and small objects disappearFuse finer stages, as in FCN-16s and FCN-8s, or use U-Net-style skip connections
Large objects that exceed the Receptive FieldA cell cannot see the whole object, so its label is unreliableDilated convolutions, pyramid pooling, or global context
Relations between distant regions matterLocal filters connect far-apart cells only after many layersTransformer attention over the feature map
Learned upsampling gives a grid patternUneven overlap of the transposed kernelKernel width divisible by the stride (see Transposed Convolution)
Training crops differ in size from test imagesResults depend on scale, and objects may look different at a new sizeTrain on crops close to the test scale, or test on the sizes used in training
Very large inputs, such as satellite tilesFull-resolution feature maps exhaust memoryRun on overlapping tiles and stitch the predictions

The most common mistake is to judge the model on the coarse grid alone. The grid looks reasonable at a glance while its edges are off by tens of pixels, which only shows up when the output is upsampled and compared with a precise mask.

History

Before 2014, some detection systems had already run a trained classifier convolutionally over larger images. OverFeat, in 2013, is one example. Long, Shelhamer and Darrell made the idea general and central. They took classification networks, including AlexNet and VGG, converted their fully connected layers into convolutions, and fine-tuned them for dense prediction. The paper was posted in late 2014 and presented at CVPR in 2015. It set new results on PASCAL VOC segmentation at the time.

The approach caught on for two reasons. First, it let segmentation reuse the large pretrained weights of classification networks, so a model could start from features learned on ImageNet rather than from scratch. Second, it turned an architecture that had been designed for a single label into one that produced a map, so one network could serve many tasks. The decoder ideas came next: U-Net in 2015 added skip connections from the encoder, and later models refined the upsampling and the context. The fully convolutional pattern is still the default for per-pixel prediction.

Recap

A fully convolutional network replaces the fully connected layers of a classifier with convolutions, which compute the same weighted sums at every position. It therefore takes any input size and outputs a coarse grid of scores. Transposed convolutions upsample that grid, and skip connections from earlier stages restore edges. Training uses the per-pixel loss from Image Segmentation. The same idea reaches beyond labelling: a Transporter Network uses it to output where a robot should grasp. The skip-connection variant that became standard for medical images is the U-Net.

Check yourself

QuestionWhat makes a network fully convolutional, and what does it output for a 320 by 320 image?Show answer

A fully convolutional network has no fully connected layers, so every layer is a convolution, pooling or upsampling. For a 320 by 320 image the coarse output from a stride-32 backbone is a 10×1010 \times 10 grid, and after upsampling it is a 320×320320 \times 320 map of per-pixel scores.

QuestionA dense layer takes a 5×5×85 \times 5 \times 8 feature map. How many weights does each output use, and what convolution gives the same layer?Show answer

Each output uses one weight for every input value, so 5⋅5⋅8=2005 \cdot 5 \cdot 8 = 200 weights. A convolution with a 5×55 \times 5 kernel over the 8 channels, with one filter per output, computes exactly the same sum at a single position, so it is the same layer.

QuestionWhy do FCN-style models add scores from earlier, finer layers instead of upsampling the final map alone?Show answer

The final map has a stride of 32, so each cell covers a large block and the edges within it are lost. Earlier layers have a finer stride and still hold where the edges were. Adding their scores restores fine boundaries that upsampling alone cannot recover.

QuestionYou train on 256×256256 \times 256 crops and then run the network on a 1000×6001000 \times 600 image. What size is the coarse map, and what should you check before trusting the output?Show answer

Halving with floor rounding gives 1000→311000 \to 31 and 600→18600 \to 18 over five stages, so the coarse map is about 31×1831 \times 18, and the output is upsampled back to 1000×6001000 \times 600. Before trusting it, check the scale: objects in the new image may be much larger than the crops the network was trained on, so they can fall outside the receptive field the model learned. Check the outputs on a few images at the test scale.

Sources