TL;DR
Image segmentation gives every pixel in an image a label, such as road, person, sky or tumour. A plain image classifier answers "what is in this picture?" with one label for the whole image. A segmentation model answers "which pixels belong to what?", which is the question a robot, a medical scanner or a photo editor actually needs answered.
The standard approach is a fully convolutional network. It produces one score per class for every pixel, turns those scores into probabilities with a softmax at each pixel, and trains with cross-entropy averaged over all pixels.
Where you'll see it: in the background blur of a video call, in self-driving cars that must know where the drivable road ends, and in medical tools that outline organs or lesions on a scan.
Intuition
Labelling every pixel
Think of a colouring book in reverse. You are handed a photo and asked to paint over it with flat colours, one colour per kind of thing: green for grass, grey for road, blue for car. The result is a map with the same width and height as the photo, where each cell says what it is. That map is the output of segmentation.
The difference from classification matters. A classifier throws away position. It can tell you a cat is present, but not whether the cat is in the left corner or fills the frame. Segmentation keeps position, so its output has to be a full-resolution grid of labels.
Why does this need a new kind of network? A classifier ends in a few numbers that summarise the whole image. Segmentation needs a grid of numbers, one stack of class scores per pixel, so the network must keep spatial detail all the way to the output. The usual solution is an encoder-decoder: the encoder shrinks the image down to a compact description of what is there, and the decoder grows that description back into a map at the original size.
- loss = -ln p
The shape of the network
flowchart LR A["Input image"] --> B["Convolution blocks"] B --> C["Downsample"] C --> D["Bottleneck"] D --> E["Upsample"] B -.skip connection.-> E E --> F["1 by 1 convolution: K scores per pixel"] F --> G["Softmax per pixel"]
The dashed arrow is a skip connection, used in U-Net-style models. Downsampling throws away fine detail such as the exact edge of an object. Copying features from the encoder straight across to the decoder gives the decoder that detail back, so object boundaries come out sharp.
The math
Setup
Let the image have pixels and let there be classes. For each pixel the network produces a vector of raw scores , one score per class. The label of that pixel is , taken from the ground-truth mask.
Per-pixel probabilities
The Softmax turns each pixel's scores into probabilities that are positive and sum to 1:
Here is the probability the model gives class at pixel . The network is the same function applied everywhere, so it has one output head that is evaluated at every position.
The loss
Apply the cross-entropy loss to each pixel and average over all of them:
The inner term is the loss for one pixel: the negative log of the probability given to that pixel's true class. The outer sum runs over pixels, and dividing by gives the average, so images of different sizes are on the same scale. Everything else is the same as classification, applied independently at each pixel.
Measuring quality
Accuracy, the fraction of pixels labelled correctly, is easy to compute but misleading when one class is rare. The standard overlap score is the intersection over union, or IoU. For one class, with the set of predicted pixels and the set of true pixels:
It is 1 for a perfect match and 0 for no overlap. The mean IoU averages this over the classes. A related score, the Dice coefficient, is . Both reward getting the object's pixels right and penalise both missed pixels and false ones.
Worked examples
Take a strip of four pixels, with classes background (0) and object (1). For each pixel the network gives two logits, and the true label is known.
- Pixel 1: logits , true label background.
- Softmax: and , so .
- Loss: .
- Pixel 2: logits , true label object.
- Softmax: .
- Loss: .
- Pixel 3: logits , true label object.
- Softmax: both probabilities are .
- Loss: .
- Pixel 4: logits , true label background.
- Softmax: .
- Loss: .
- Average over the four pixels: .
Interpretation: pixels 1 and 2 are confident and right, so they cost little. Pixel 4 is confidently wrong, and it contributes more than the other three together. The loss is dominated by the pixels the model gets most wrong, which is what drives the training.
A 256 by 256 scan has pixels. A tumour covers of them, so it is of the image.
- A model that labels every pixel as healthy tissue gets pixels right.
- Its accuracy is .
- Its IoU for the tumour class is , because it predicts no tumour pixels at all.
Interpretation: 98 percent accuracy sounds excellent and the model is useless. This is why segmentation is judged by IoU or Dice per class, and why the loss is sometimes weighted so rare classes count for more.
Tradeoffs and failure modes
Segmentation needs a label for every pixel, and that is expensive. A mask for one image can take minutes of careful drawing, so datasets are small compared to classification datasets. Data augmentation helps, but only if the flips, crops and rotations are applied to the image and its mask together.
Downsampling buys context at the cost of detail. A deep encoder sees the whole scene but blurs small objects. Skip connections and high-resolution branches recover detail, at the cost of memory, since full-resolution feature maps are large.
| Situation | Likely problem | Better choice |
|---|---|---|
| Thin objects such as wires or blood vessels | Downsampling erases them | Skip connections, or a high-resolution branch |
| Rare class covers a few percent of pixels | Model ignores it, accuracy stays high | Report IoU or Dice, weight the loss for the rare class |
| Output edges are blocky | Upsampling is too coarse | Learned upsampling via Transposed Convolution, or a skip connection |
| Two touching objects of the same class | Merged into one blob | Instance segmentation, which labels each object separately |
| Little labelled data | Overfitting, poor generalisation | Pretrained encoder and heavy augmentation |
Segmentation labels are classes, not objects. A semantic model that marks all people as "person" cannot tell two people apart. Instance segmentation solves that, at additional cost.
History
Before deep learning, segmentation was done with hand-built features, graph cuts and conditional random fields. Those methods worked on textures and colours but struggled with the variety of real scenes.
In 2014 Jonathan Long, Evan Shelhamer and Trevor Darrell showed that a classifier trained on whole images could be converted into a fully convolutional network that produces a dense map directly. Their paper made per-pixel prediction with deep networks the standard approach.
In 2015 Olaf Ronneberger, Philipp Fischer and Thomas Brox published U-Net for biomedical images. It used a symmetric encoder-decoder with skip connections and trained well on very few annotated scans, which made it popular in medicine and beyond. Later models such as DeepLab added dilated convolutions and refinements, but the encoder-decoder idea remained central.
Check yourself
QuestionWhat is the difference between image classification and image segmentation in terms of the network's output?Show answer
A classifier outputs one vector of class scores for the whole image. Segmentation outputs a grid of class scores, one vector per pixel, with the same height and width as the input, so the output keeps spatial position.
QuestionA pixel has logits (0.0, 2.0) and its true class is 0. What is its cross-entropy loss, to two decimals?Show answer
The softmax probabilities are for class 0. The loss is , to two decimals.
QuestionWhy is the loss averaged over pixels instead of summed?Show answer
Averaging divides by , so the loss does not depend on the image size. Without it, a large image would produce a much larger loss and gradient than a small one, and the learning rate would have to be retuned for every input size.
QuestionA model segments tumours and reports 97 percent pixel accuracy. Why might that still be a bad model?Show answer
If tumours cover only a few percent of pixels, a model that predicts no tumour at all already scores around 97 percent. Per-class IoU or Dice would show the tumour class scoring near zero.
QuestionWhy do U-Net-style models copy encoder features across to the decoder?Show answer
Downsampling discards fine spatial detail, such as exact edges. Skip connections pass high-resolution features from the encoder to the decoder, so the decoder can place object boundaries precisely when it upsamples.