Skip to content

Fix HausdorffDistance raising for classes absent from both preds and target (#3682) - #3687

Open
debojyoti10CC wants to merge 1 commit into
Lightning-AI:masterfrom
debojyoti10CC:fix/hausdorff-absent-class
Open

debojyoti10CC wants to merge 1 commit into
Lightning-AI:masterfrom
debojyoti10CC:fix/hausdorff-absent-class

Conversation

@debojyoti10CC

Copy link
Copy Markdown

What does this PR do?

Fixes #3682.

hausdorff_distance / HausdorffDistance crashed with RuntimeError: max(): Expected reduction dim to be specified for input.numel() == 0 whenever a class was absent from both preds and target of a sample, because edge_surface_distance returns empty tensors there and .max() was called on them.

The issue leaves the value as a design question, so I followed what the sibling segmentation metrics already do for a class that is absent from both inputs:

  • functional/segmentation/dice.py computes per-class scores with _safe_divide(..., zero_division="nan"), and the DiceScore module aggregates with nanmean.
  • functional/segmentation/mean_iou.py computes per-class IoU with zero_division="nan", and the MeanIoU module only accumulates classes where union > 0 (valid_classes), so absent classes don't enter the running mean.

So this is option 3 from the issue (which is also what MONAI does):

  • the functional returns nan for a class absent from both (the one-sided case still gives inf, unchanged);
  • HausdorffDistance.update uses nansum and only counts non-nan entries, so the running mean stays meaningful.

Added nan behaviour to the functional docstring, a CHANGELOG entry, and regression tests (functional, directed and undirected, plus the module).

Testing

  • ruff format --check passes on the changed files; ruff check only reports two RUF059 warnings in an existing, untouched test (test_hausdorff_distance_raises_error).
  • I could not run pytest locally: this Windows machine's Application Control policy blocks loading torch_python.dll, so the new tests and the existing hausdorff tests are untested here. Please rely on CI for these.

Before submitting

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HausdorffDistance raises RuntimeError when a class is absent from both preds and target

1 participant