Fix distance_transform indexing for non-square inputs#3338
Fix distance_transform indexing for non-square inputs#3338omkar-334 wants to merge 5 commits intoLightning-AI:masterfrom
distance_transform indexing for non-square inputs#3338Conversation
Codecov Report❌ Patch coverage is
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:
|
There was a problem hiding this comment.
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_transformfor non-square inputs. - Added a new unit test covering non-square
distance_transformoutputs vs SciPy across distance metrics. - Added a regression test for
hausdorff_distanceon(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.
|
@justusschock all tests are passing.... can you review this? |



What does this PR do?
Fixes #3300
Before submitting
Fixes an indexing bug in the PyTorch implementation of
distance_transformthat affected non-square 2D inputs, and causedhausdorff_distanceto fail for shapes where height is greater than width.Tests
Added 2 tests for non-square inputs:
distance_transformon an11x10tensor, compared against SciPy for:euclideanchessboardtaxicabhausdorff_distanceon shape(1, 1, 11, 10), compared against MONAI📚 Documentation preview 📚: https://torchmetrics--3338.org.readthedocs.build/en/3338/