Skip to content

Fix distance_transform indexing for non-square inputs#3338

Open
omkar-334 wants to merge 5 commits intoLightning-AI:masterfrom
omkar-334:fix-hausdorff
Open

Fix distance_transform indexing for non-square inputs#3338
omkar-334 wants to merge 5 commits intoLightning-AI:masterfrom
omkar-334:fix-hausdorff

Conversation

@omkar-334
Copy link
Copy Markdown

@omkar-334 omkar-334 commented Mar 17, 2026

What does this PR do?

Fixes #3300

Before submitting
  • Was this discussed/agreed via a Github issue? (no need for typos and docs improvements)
  • Did you read the contributor guideline, Pull Request section?
  • [] Did you make sure to update the docs?
  • Did you write any new necessary tests?

Fixes an indexing bug in the PyTorch implementation of distance_transform that affected non-square 2D inputs, and caused hausdorff_distance to fail for shapes where height is greater than width.

Tests

Added 2 tests for non-square inputs:

  • distance_transform on an 11x10 tensor, compared against SciPy for:
    • euclidean
    • chessboard
    • taxicab
  • hausdorff_distance on shape (1, 1, 11, 10), compared against MONAI

📚 Documentation preview 📚: https://torchmetrics--3338.org.readthedocs.build/en/3338/

@omkar-334
Copy link
Copy Markdown
Author

Before:
Screenshot 2026-03-18 at 2 10 31 AM

After:
Screenshot 2026-03-18 at 2 10 52 AM

@omkar-334
Copy link
Copy Markdown
Author

New tests passing:
Screenshot 2026-03-18 at 2 14 41 AM

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 18, 2026

Codecov Report

❌ Patch coverage is 0% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 32%. Comparing base (c781c99) to head (7853799).
⚠️ Report is 1 commits behind head on master.

❗ There is a different number of reports uploaded between BASE (c781c99) and HEAD (7853799). Click for more details.

HEAD has 2 uploads less than BASE
Flag BASE (c781c99) HEAD (7853799)
gpu 1 0
unittest 1 0
Additional details and impacted files
@@           Coverage Diff            @@
##           master   #3338     +/-   ##
========================================
- Coverage      37%     32%     -5%     
========================================
  Files         364     349     -15     
  Lines       20096   19901    -195     
========================================
- Hits         7521    6377   -1144     
- Misses      12575   13524    +949     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes an indexing bug in the PyTorch distance_transform implementation that breaks for non-square 2D inputs (notably when height > width), which in turn caused hausdorff_distance to error for certain shapes (issue #3300).

Changes:

  • Corrected flatten indexing in distance_transform for non-square inputs.
  • Added a new unit test covering non-square distance_transform outputs vs SciPy across distance metrics.
  • Added a regression test for hausdorff_distance on (1, 1, 11, 10) vs MONAI.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/torchmetrics/functional/segmentation/utils.py Fixes distance_transform flatten indexing by using width for row-major indexing.
tests/unittests/segmentation/test_utils.py Adds SciPy-based regression test for non-square distance_transform inputs.
tests/unittests/segmentation/test_hausdorff_distance.py Adds MONAI-based regression test for non-square hausdorff_distance inputs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread src/torchmetrics/functional/segmentation/utils.py
Comment thread tests/unittests/segmentation/test_hausdorff_distance.py
@omkar-334
Copy link
Copy Markdown
Author

omkar-334 commented Apr 5, 2026

@justusschock all tests are passing.... can you review this?

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.

hausdorff_distance() throws IndexError for certain input shapes

3 participants