Skip to content
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

Absent class miou fix [WIP] #2892

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Isalia20
Copy link
Contributor

@Isalia20 Isalia20 commented Jan 3, 2025

What does this PR do?

Fixes #2866

This is not the final version. I wanted to discuss the expected outputs for different edge cases and how to deal with them. I haven't included tests for these edge cases because monAI deals with such cases differently than what is proposed in this PR/issue, therefore adding a test and checking it against the monAI value would lead to failing tests. I'll post different inputs and what kind of outputs we should expect in the followup comment.

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? See above
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? partly 😄

Make sure you had fun coding 🙃


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

@Isalia20
Copy link
Contributor Author

Isalia20 commented Jan 3, 2025

Case 1: Perfect prediction

import torch
from torchmetrics.segmentation import MeanIoU
from torchmetrics.functional.segmentation.mean_iou import mean_iou

num_classes = 3
metric_one = MeanIoU(num_classes=num_classes, per_class=False, input_format='index')
metric_two = MeanIoU(num_classes=num_classes, per_class=True, input_format='index')
target = torch.tensor([
    [0, 1],  # Ground truth: class 0, class 1
    [1, 0],  # Ground truth: class 1, class 0
    [2, 2],  # Ground truth: class 2, class 2
])
preds = torch.tensor([
    [0, 1],  # Predictions: class 0, class 1
    [1, 0],  # Predictions: class 1, class 0
    [2, 2],  # Predictions: class 2, class 2
])
metric_one.update(preds, target)
miou_per_class_one = metric_one.compute()
metric_two.update(preds, target)
miou_per_class_two = metric_two.compute()

print(miou_per_class_one)
print(miou_per_class_two)

Returns:

tensor(1.)
tensor([1., 1., 1.])

Case 2 Perfect prediction but one completely absent class

import torch
from torchmetrics.segmentation import MeanIoU
from torchmetrics.functional.segmentation.mean_iou import mean_iou

num_classes = 4
metric_one = MeanIoU(num_classes=num_classes, per_class=False, input_format='index')
metric_two = MeanIoU(num_classes=num_classes, per_class=True, input_format='index')
target = torch.tensor([
    [0, 1],  # Ground truth: class 0, class 1
    [1, 0],  # Ground truth: class 1, class 0
    [2, 2],  # Ground truth: class 2, class 2
])
preds = torch.tensor([
    [0, 1],  # Predictions: class 0, class 1
    [1, 0],  # Predictions: class 1, class 0
    [2, 2],  # Predictions: class 2, class 2
])
metric_one.update(preds, target)
miou_per_class_one = metric_one.compute()
metric_two.update(preds, target)
miou_per_class_two = metric_two.compute()

print(miou_per_class_one)
print(miou_per_class_two)

Returns:

tensor(nan)
tensor([1., 1., 1., nan])

Case 3: Completely wrong predictions(Probably same as the old one)

import torch
from torchmetrics.segmentation import MeanIoU

num_classes = 3
metric_one = MeanIoU(num_classes=num_classes, per_class=False, input_format='index')
metric_two = MeanIoU(num_classes=num_classes, per_class=True, input_format='index')
target = torch.tensor([
    [0, 1],  # Ground truth: class 0, class 1
    [1, 0],  # Ground truth: class 1, class 0
    [2, 2],  # Ground truth: class 2, class 2
])
preds = torch.tensor([
    [1, 2],  # Predictions: all wrong
    [2, 1],  # Predictions: all wrong
    [0, 1],  # Predictions: all wrong
])
metric_one.update(preds, target)
miou_per_class_one = metric_one.compute()
metric_two.update(preds, target)
miou_per_class_two = metric_two.compute()

print(miou_per_class_one)
print(miou_per_class_two)

Returns

tensor(0.)
tensor([0., 0., 0.])

Copy link

codecov bot commented Jan 3, 2025

Codecov Report

Attention: Patch coverage is 66.66667% with 4 lines in your changes missing coverage. Please review.

Project coverage is 35%. Comparing base (7bda8f7) to head (b67a5b6).
Report is 3 commits behind head on master.

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

HEAD has 72 uploads less than BASE
Flag BASE (7bda8f7) HEAD (b67a5b6)
macOS 8 4
cpu 34 16
python3.12 6 3
torch2.5.0 2 1
python3.10 18 8
torch2.0.1 4 2
python3.11 8 4
torch2.6.0 2 1
torch2.6.0+cpu 4 2
Windows 6 3
torch2.0.1+cpu 6 3
torch2.5.0+cpu 2 1
Linux 20 9
torch2.4.1+cu121 4 2
torch2.2.2+cpu 2 1
torch2.5.0+cu124 4 2
torch2.3.1+cpu 2 0
python3.9 2 1
torch2.1.2+cpu 2 1
Additional details and impacted files
@@           Coverage Diff            @@
##           master   #2892     +/-   ##
========================================
- Coverage      69%     35%    -34%     
========================================
  Files         332     332             
  Lines       18966   18969      +3     
========================================
- Hits        13055    6598   -6457     
- Misses       5911   12371   +6460     

@Borda Borda changed the title Absent class miou fix Absent class miou fix [WIP] Jan 6, 2025
@Isalia20
Copy link
Contributor Author

Isalia20 commented Jan 6, 2025

While it's marked WIP I still need comments from you @Borda . I posted some cases and wonder how I should proceed

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.

Zero value MeanIoU for Absent Classes in Ground Truth
1 participant