-
Notifications
You must be signed in to change notification settings - Fork 464
Bugfix for GeneralizedDiceScore
to yield NaN
for missing classes
#3251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Bugfix for GeneralizedDiceScore
to yield NaN
for missing classes
#3251
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #3251 +/- ##
========================================
- Coverage 69% 32% -37%
========================================
Files 364 349 -15
Lines 20096 19905 -191
========================================
- Hits 13790 6377 -7413
- Misses 6306 13528 +7222 🚀 New features to boost your workflow:
|
GeneralizedDiceScore
to yield NaN
for missing classes
Head branch was pushed to by a user without write access
Done. Aggregating the numerator and denominator in the update function and finally dividing them in the compute function brings the range back to [0,1]. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a bug in GeneralizedDiceScore
where missing classes were not properly handled when per_class=True
, causing incorrect behavior instead of returning NaN
for those classes.
- Refactored the metric's state tracking to properly identify missing classes and return
NaN
for them - Updated the compute logic to handle both per-class and aggregate scoring correctly
- Added comprehensive test coverage for missing class scenarios
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
src/torchmetrics/segmentation/generalized_dice.py | Refactored state management and compute logic to track class presence and return NaN for missing classes |
src/torchmetrics/functional/segmentation/generalized_dice.py | Added NaN handling for non-per-class computation when denominator is zero |
tests/unittests/segmentation/test_generalized_dice_score.py | Added test cases for missing classes and zero denominator scenarios |
CHANGELOG.md | Updated changelog to document the bug fix |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
@VijayVignesh1 dont worry, Copilot is not always right but it may spot some overlooked issues |
[0.4571, 0.4980, 0.5191, 0.4380, 0.5649], | ||
[0.5428, 0.4904, 0.5358, 0.4830, 0.4724], | ||
[0.4715, 0.4925, 0.4797, 0.5267, 0.4788]]) | ||
tensor([[0.4845, 0.4997, 0.4993, 0.4864, 0.4912]]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the tensorshape shouldn't change here!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've made the changes.
[0.1837, 0.2162, 0.0962, 0.2692, 0.1895], | ||
[0.3866, 0.1348, 0.2526, 0.2301, 0.2083], | ||
[0.1978, 0.2804, 0.1714, 0.1915, 0.2783]]) | ||
tensor([[0.2234, 0.2170, 0.1597, 0.2399, 0.2204]]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also here the shape should not change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
What does this PR do?
Fixes GeneralizedDiceScore to yield NaN for missing classes when per_class=True.
Added testcases to verify the same.
Fixes #2846
Before submitting
PR review
Anyone in the community is free to review the PR once the tests have passed.
If we didn't discuss your PR in Github issues there's a high chance it will not be merged.
Did you have fun?
Make sure you had fun coding 🙃
📚 Documentation preview 📚: https://torchmetrics--3251.org.readthedocs.build/en/3251/