Repository navigation
Fix HausdorffDistance raising for classes absent from both preds and target (#3682) - #3687
Open
debojyoti10CC wants to merge 1 commit into
Open
debojyoti10CC wants to merge 1 commit into
debojyoti10CC wants to merge 1 commit into
Conversation
debojyoti10CC
requested review from
SkafteNicki,
ethanwharris,
justusperillieux and
tchaton
as code owners
November 30, 2026 01:16
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fixes #3682.
hausdorff_distance/HausdorffDistancecrashed withRuntimeError: max(): Expected reduction dim to be specified for input.numel() == 0whenever a class was absent from bothpredsandtargetof a sample, becauseedge_surface_distancereturns 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.pycomputes per-class scores with_safe_divide(..., zero_division="nan"), and theDiceScoremodule aggregates withnanmean.functional/segmentation/mean_iou.pycomputes per-class IoU withzero_division="nan", and theMeanIoUmodule only accumulates classes whereunion > 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):
nanfor a class absent from both (the one-sided case still givesinf, unchanged);HausdorffDistance.updateusesnansumand only counts non-nan entries, so the running mean stays meaningful.Added
nanbehaviour to the functional docstring, a CHANGELOG entry, and regression tests (functional, directed and undirected, plus the module).Testing
ruff format --checkpasses on the changed files;ruff checkonly reports two RUF059 warnings in an existing, untouched test (test_hausdorff_distance_raises_error).torch_python.dll, so the new tests and the existing hausdorff tests are untested here. Please rely on CI for these.Before submitting