Image Segmentation
Accuracy
Dice Loss
Performance Analysis
Machine Learning

Good performance with Accuracy but not with Dice loss in Image Segmentation

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In image segmentation, evaluating model performance requires choosing the right metrics. Two commonly used metrics are accuracy and Dice loss. Although both quantify how well a model segments an image, they measure fundamentally different things. It is entirely possible to achieve high accuracy while getting a poor Dice loss score. This article explains why this happens, walks through concrete scenarios, and offers strategies to address the gap.

Understanding Accuracy

Accuracy measures the proportion of correctly classified pixels over the total number of pixels:

Accuracy=TP+TNTP+TN+FP+FN\text{Accuracy} = \frac{TP + TN}{TP + TN + FP + FN}

Where:

  • TPTP (True Positive): pixels correctly classified as the object class
  • TNTN (True Negative): pixels correctly classified as background
  • FPFP (False Positive): background pixels incorrectly classified as object
  • FNFN (False Negative): object pixels incorrectly classified as background

Accuracy treats every pixel equally. When one class dominates the image (which is common in segmentation tasks), accuracy becomes heavily biased toward that dominant class.

Understanding Dice Loss

The Dice coefficient measures the overlap between the predicted segmentation and the ground truth. It is defined as:

Dice=2×ABA+B\text{Dice} = \frac{2 \times |A \cap B|}{|A| + |B|}

where AA is the set of predicted positive pixels and BB is the set of ground truth positive pixels. Dice loss is simply the complement:

Dice Loss=12×ABA+B\text{Dice Loss} = 1 - \frac{2 \times |A \cap B|}{|A| + |B|}

The Dice coefficient ranges from 0 (no overlap) to 1 (perfect overlap). Because it focuses exclusively on the positive class, it is far more sensitive to how well the model captures the target region.

Why Accuracy Can Be High While Dice Loss Is Poor

1. Class Imbalance

This is the most common cause. Consider a 512x512 image where the object of interest occupies only 1% of the pixels (about 2,621 pixels out of 262,144 total). A model that predicts every pixel as background achieves:

Accuracy=0+259,523262,14499%\text{Accuracy} = \frac{0 + 259{,}523}{262{,}144} \approx 99\%

Yet the Dice score would be exactly 0, because AB=0|A \cap B| = 0. The model completely failed to find the object, but accuracy cannot reveal this.

2. Missed Fine Details

In cases where small structures like blood vessels or thin boundaries matter, a model might correctly classify the large regions but miss these details. Since the missed pixels are few relative to the total, accuracy stays high. The Dice coefficient drops significantly because those missed pixels represent a large fraction of the target class.

3. Border Effects

Segmentation boundaries are inherently difficult. A model might identify the rough location of an object but fail to precisely delineate its edges. The boundary pixels contribute minimally to accuracy (there are far fewer boundary pixels than interior pixels), but they can substantially reduce the Dice score.

Practical Examples

Medical Imaging: Tumor Segmentation

In MRI brain scans, a tumor might occupy less than 5% of the total image area. A model that classifies everything as healthy tissue can achieve above 95% accuracy while having a Dice score near zero. This is why medical imaging papers almost always report Dice scores rather than accuracy.

Satellite Imagery: Urban Area Detection

In satellite imagery for land-cover classification, urban regions often represent a small fraction of the image compared to agricultural or natural landscapes. A model that correctly classifies the vast rural regions but misses small urban patches achieves high accuracy with poor Dice scores on the urban class.

Strategies to Improve Dice Loss

Use Dice Loss as a Training Objective

Instead of training with cross-entropy loss (which optimizes per-pixel accuracy), directly use Dice loss as the training objective. This forces the model to optimize for overlap with the target region.

Weighted Loss Functions

Apply class weights to penalize misclassification of the minority class more heavily. A common formula weights each class inversely proportional to its frequency:

wc=1frequency(c)w_c = \frac{1}{\text{frequency}(c)}

Data Augmentation

Augmentation techniques like cropping, rotation, flipping, and elastic deformations increase the diversity of training examples, helping the model learn better representations for minority classes.

Post-Processing

Conditional random fields (CRFs) and morphological operations can refine predicted boundaries after inference, improving spatial overlap with the ground truth.

Summary

MetricWhat It MeasuresSensitivity to Imbalance
AccuracyProportion of all correct pixelsLow (dominated by majority class)
Dice LossOverlap between prediction and ground truthHigh (focuses on target class)

Key Takeaways

  • High accuracy does not guarantee good segmentation quality, especially with class imbalance.
  • Dice loss directly measures spatial overlap, making it a more informative metric for segmentation tasks.
  • When the target region is small relative to the background, always evaluate with Dice, IoU, or similar overlap-based metrics.
  • Training with Dice loss or a combined loss function (e.g., Loss=αCE+(1α)Dice Loss\text{Loss} = \alpha \cdot \text{CE} + (1 - \alpha) \cdot \text{Dice Loss}) typically yields better segmentation quality than training with cross-entropy alone.

Course illustration
Course illustration

All Rights Reserved.