Fully Convolution Net FCN on Tensorflow
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
A Fully Convolutional Network, or FCN, is a neural network for dense prediction tasks such as semantic segmentation. Unlike a classification CNN, an FCN does not end with fully connected layers that collapse spatial structure into one label. Instead, it preserves spatial reasoning and produces a per-pixel output map. In TensorFlow, this is straightforward to build with convolution, pooling, and upsampling layers.
What Makes an FCN Different
A standard image classifier turns an image into one class prediction. An FCN turns an image into another image-shaped tensor, typically a segmentation map.
That leads to a few defining properties:
- no dense classifier head at the end
- output is spatial, not a single label
- the final tensor is often shaped like
height x width x classes
For segmentation, each pixel receives a class score instead of the whole image receiving one label.
A Small FCN in TensorFlow Keras
Here is a compact example:
This model downsamples to learn higher-level features and then upsamples back to the original spatial resolution.
Understand the Output Shape
If your input is 128 x 128 x 3 and num_classes=3, the output is:
That does not mean the model returns three separate images. It means each pixel location has three class scores, one for each segmentation class.
During training with sparse_categorical_crossentropy, the mask is usually shaped like:
with integer class IDs at each pixel.
Skip Connections and Better FCNs
The original FCN family and later architectures often improve segmentation quality with skip connections that combine low-level spatial detail from earlier encoder layers with high-level semantic features from deeper layers.
A simple idea is:
- encoder extracts semantic meaning
- decoder restores resolution
- skip connections recover fine detail
If you need stronger segmentation quality, that is usually the next improvement after a basic encoder-decoder FCN.
Train with the Right Masks
Your masks must align with the network output:
- same spatial resolution as the final output
- integer labels for sparse loss, or one-hot labels for categorical loss
- identical geometric preprocessing as the images
A mismatch here is one of the fastest ways to get a model that trains badly or appears to learn nothing.
Common Pitfalls
The most common mistake is using image-classification thinking for a segmentation problem. An FCN must output a dense spatial map, not a single class vector.
Another issue is choosing the wrong loss for the mask encoding. sparse_categorical_crossentropy expects integer class labels, while categorical_crossentropy expects one-hot labels.
Developers also often forget to check output shape against the mask shape. If upsampling does not restore the expected resolution, training will fail or silently misbehave.
Finally, plain pixel accuracy is often not enough for evaluating segmentation. Metrics such as IoU or Dice are usually more informative.
Summary
- An FCN is designed for dense prediction tasks such as semantic segmentation.
- In TensorFlow, you can build one with convolution, pooling, and upsampling layers.
- The output is a per-pixel class map, not a single image label.
- Mask shape and loss function must match the output format.
- Skip connections and stronger segmentation metrics are common next steps after a basic FCN.
Related reading
- Generating MNIST numbers using LSTM-CGAN in TensorFlow
- Generative adversarial networks tanh?
- generative models with tensorflow's tpu_estimator?
- Geometric representation of Perceptrons Artificial neural networks
- Function call stack keras_scratch_graph Error
- Future prediction using time series data set with Tensorflow
- Function that returns affinity between texts?
- Gaussian process multi-class classification
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.