Skip to content
Apixo
Blog
news· 3 min read· via Towards AI

Understanding Semantic Segmentation: Tensors, Logits, and Loss Functions

A detailed technical breakdown of semantic segmentation pipelines, tensor operations, class imbalance traps, and hybrid loss functions like Cross-Entropy and Dice loss.

Understanding Semantic Segmentation: Tensors, Logits, and Loss Functions

Semantic segmentation transforms raw image data into detailed, pixel-by-pixel class maps. Unlike standard image classification, which outputs a single label for an entire photo, or object detection, which returns bounding boxes that inevitably enclose background noise, semantic segmentation classifies every individual coordinate. Understanding how these models process input tensors, calculate internal probabilities, and measure loss reveals why high pixel accuracy can still mask significant model failures.

From Raw Pixels to Class Predictions

At its core, a semantic segmentation pipeline accepts an image array—typically normalized to floating-point numbers between zero and one—and converts it to a channels-first tensor with a batch dimension: (B, C, H, W). Here, C represents the number of target classes, while H and W represent the spatial dimensions of the input grid or feature map.

Instead of outputting final class decisions directly, the neural network computes raw scalar scores called logits for every pixel across all classes. Applying a softmax function across the class channel exponentiates and normalizes these logits into probability distributions that sum to one at each pixel location. To produce a final two-dimensional label map, an argmax operation extracts the class index holding the highest probability at each coordinate.

In practical real-time architectures like Ultralytics YOLO26n-sem—a 1.6-million-parameter model trained on the 19-class Cityscapes dataset—predictions are computed on a downsampled output grid (such as a 1/8th scale spatial patch). The model relies on bilinear interpolation to stretch those score grids back to the original image resolution before evaluating probabilities.

The Imbalance Trap: Cross-Entropy versus Dice Loss

Training a segmentation model requires comparing predicted pixel probabilities against ground-truth label maps. While predictions use argmax for final output, gradient descent cannot train on argmax directly because discrete class assignment lacks useful gradients. Instead, standard training relies on cross-entropy loss, calculating -ln(p) based on the ground-truth class probability p.

However, standard cross-entropy suffers from severe class imbalance. In images dominated by large background regions such as road surfaces or sky, millions of background pixels generate tiny individual losses that collectively flood the training signal. A model can achieve 99 percent pixel accuracy by simply predicting background classes everywhere, while remaining completely useless on small, critical target objects like traffic signs or pedestrians.

To address this issue, segmentation architectures incorporate Dice loss. Derived from region overlap metrics, soft Dice loss calculates 1 - (2 * overlap / (predicted_sum + ground_truth_sum)). Because Dice focuses exclusively on positive class presence and overlap rather than true background pixels, small objects receive equal weighting regardless of their total pixel count.

Because Dice loss can be volatile on tiny or empty classes and only evaluates aggregate area sums, production networks typically combine both approaches. YOLO26n-sem, for example, utilizes a multi-part loss formula combining main cross-entropy, class-averaged Dice loss, and an auxiliary cross-entropy term: L = CE + Dice + 0.4 * CE_aux.

What it means for developers

For AI developers building vision pipelines, understanding pixel-level loss mechanics helps prevent critical mistakes during model implementation and evaluation:

  • Avoid pixel accuracy as a sole metric: Never evaluate segmentation performance using pixel accuracy alone. Use class-averaged metrics such as Dice scores or Intersection over Union (IoU) to ensure low-frequency classes are properly evaluated.
  • Implement composite loss functions: When training custom segmentation models, combine per-pixel cross-entropy with soft Dice loss. Cross-entropy provides smooth, per-pixel gradients for accurate localization, while Dice loss prevents background regions from dominating network updates.
  • Account for downsampling artifacts: Be aware that real-time vision networks often perform inference on downscaled grids before upsampling with bilinear interpolation, which can create soft boundaries around small details.

As computer vision systems are increasingly embedded into multi-modal AI architectures and automated workflows, managing API infrastructure efficiently is vital. Developers looking to benchmark vision capabilities or build multi-modal AI tools can try top AI models cheaply through one API at https://apixoai.online.


Source: Semantic Segmentation, One Pixel at a Time — Towards AI. Written by the Apixo team from that report.

#ai-news#computer-vision#deep-learning#yolo#machine-learning#python
Try it with your own tools

One key for Claude, GPT, GLM, DeepSeek and more. Pay per token with crypto.

Get your API key

Keep reading