Why can't raw Intersection over Union be directly optimized as a loss function?

Quick Overview

Asks why Intersection over Union cannot be trained on directly as a loss for segmentation masks and detection boxes, and what is used in its place. Tests understanding of gradients through thresholded outputs, the zero-overlap problem for boxes, and differentiable surrogates such as soft IoU, Dice, Lovasz-Softmax and GIoU.

Why can't raw Intersection over Union be directly optimized as a loss function?

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Why can't raw Intersection over Union be directly optimized as a loss function? IoU is the standard evaluation metric for segmentation masks and detection boxes, so training on it directly looks attractive. Explain what goes wrong and what is used instead. ```hint Follow the computation Write out every step from the model's logits to the final IoU number and look for a step whose output does not change under small perturbations. ``` ```hint Boxes are a separate case For bounding boxes, compare two predictions that both miss the target box, one by a little and one by a lot. ``` ### What a Strong Answer Covers - Why thresholding or argmax makes mask IoU piecewise constant, with a gradient that is zero or undefined - The plateau for bounding boxes that do not overlap the target - Other practical issues, such as the lack of a per-pixel decomposition and empty masks - Differentiable surrogates for masks and for boxes, and how each one restores a useful gradient ### Follow-up Questions - How are soft Dice and soft IoU related, and when would you pick one over the other? - Why is a soft IoU loss often combined with cross-entropy during training? - How does GIoU behave when one box contains the other, and how do DIoU and CIoU deal with that? - Should the soft IoU be computed per image or over the whole batch, and why does it matter?

Overview: Asks why Intersection over Union cannot be trained on directly as a loss for segmentation masks and detection boxes, and what is used in its place. Tests understanding of gradients through thresholded outputs, the zero-overlap problem for boxes, and differentiable surrogates such as soft IoU, Dice, Lovasz-Softmax and GIoU.

General logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Why can't raw Intersection over Union be directly optimized as a loss function?

IoU is the standard evaluation metric for segmentation masks and detection boxes, so training on it directly looks attractive. Explain what goes wrong and what is used instead.

What a Strong Answer Covers Guidance

  • Why thresholding or argmax makes mask IoU piecewise constant, with a gradient that is zero or undefined
  • The plateau for bounding boxes that do not overlap the target
  • Other practical issues, such as the lack of a per-pixel decomposition and empty masks
  • Differentiable surrogates for masks and for boxes, and how each one restores a useful gradient

Follow-up Questions Guidance

  • How are soft Dice and soft IoU related, and when would you pick one over the other?
  • Why is a soft IoU loss often combined with cross-entropy during training?
  • How does GIoU behave when one box contains the other, and how do DIoU and CIoU deal with that?
  • Should the soft IoU be computed per image or over the whole batch, and why does it matter?