Skip to content

Conversation

VijayVignesh1
Copy link

@VijayVignesh1 VijayVignesh1 commented Sep 4, 2025

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
  • 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?
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/

@VijayVignesh1 VijayVignesh1 marked this pull request as ready for review September 4, 2025 11:20
Borda
Borda previously approved these changes Sep 4, 2025
Copy link

codecov bot commented Sep 4, 2025

Codecov Report

❌ Patch coverage is 35.71429% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 32%. Comparing base (e08e009) to head (ef5dade).
⚠️ Report is 1 commits behind head on master.

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

HEAD has 478 uploads less than BASE
Flag BASE (e08e009) HEAD (ef5dade)
torch2.0.1+cpu 24 3
python3.10 96 12
Windows 16 2
cpu 136 17
macOS 24 3
torch2.0.1 16 2
python3.12 32 4
torch2.8.0+cpu 24 3
Linux 96 12
torch2.7.1+cpu 16 2
torch2.8.0 8 1
torch2.2.2+cpu 8 1
torch2.3.1+cpu 8 1
torch2.4.1+cpu 8 1
torch2.1.2+cpu 8 1
torch2.6.0+cpu 8 1
torch2.5.1+cpu 8 1
python3.9 8 1
gpu 1 0
unittest 1 0
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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mergify mergify bot added the ready label Sep 4, 2025
@Borda Borda changed the title Bugfix for GeneralizedDiceScore to yield NaN for missing classes Bugfix for GeneralizedDiceScore to yield NaN for missing classes Sep 5, 2025
@Borda Borda enabled auto-merge (squash) September 5, 2025 07:02
@Borda Borda dismissed their stale review September 6, 2025 11:27

Let's revisit the range

@mergify mergify bot removed the ready label Sep 6, 2025
auto-merge was automatically disabled September 8, 2025 18:49

Head branch was pushed to by a user without write access

@VijayVignesh1
Copy link
Author

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].

@mergify mergify bot added the ready label Sep 12, 2025
@Borda Borda requested a review from Copilot September 19, 2025 19:49
Copy link
Contributor

@Copilot 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

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.

@Borda
Copy link
Contributor

Borda commented Sep 28, 2025

@VijayVignesh1 dont worry, Copilot is not always right but it may spot some overlooked issues

@mergify mergify bot removed the ready label Sep 28, 2025
@mergify mergify bot requested a review from a team September 29, 2025 19:02
@mergify mergify bot added the ready label Sep 30, 2025
[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]])
Copy link
Member

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!

Copy link
Author

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]])
Copy link
Member

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.

Copy link
Author

Choose a reason for hiding this comment

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

Done

@mergify mergify bot requested a review from a team September 30, 2025 10:18
@Borda Borda self-requested a review September 30, 2025 10:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GeneralizedDiceScore yields 0 scores when using per_class=True for samples where class is not present

4 participants