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

Fix MetricCollection dict keys when used with ClasswiseWrapper #2361

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

sup3rgiu
Copy link

@sup3rgiu sup3rgiu commented Feb 7, 2024

What does this PR do?

When a ClasswiseWrapper is inserted into a MetricCollection, the resulting output dictionary does not take into account the specified name.

from torchmetrics import MetricCollection
from torchmetrics.wrappers import ClasswiseWrapper
from torchmetrics.classification import MulticlassAccuracy, MulticlassRecall
labels = ["horse", "fish", "dog"]
metric = MetricCollection(
    {'my/name/': ClasswiseWrapper(MulticlassAccuracy(num_classes=3, average=None), labels, prefix='accuracy_'),
    'my/other_name/': ClasswiseWrapper(MulticlassRecall(num_classes=3, average=None), labels, prefix='recall_')}
)
preds = torch.randn(10, 3).softmax(dim=-1)
target = torch.randint(3, (10,))
metric(preds, target)  

The previous code produces the following output:

{'accuracy_horse': tensor(0.5000),
 'accuracy_fish': tensor(0.2500),
 'accuracy_dog': tensor(0.5000),
 'recall_horse': tensor(0.5000),
 'recall_fish': tensor(0.2500),
 'recall_dog': tensor(0.5000)}

While with proposed fix, the output is the following:

{'my/name/accuracy_horse': tensor(0.5000),
 'my/name/accuracy_fish': tensor(0.2500),
 'my/name/accuracy_dog': tensor(0.5000),
 'my/other_name/recall_horse': tensor(0.5000),
 'my/other_name/recall_fish': tensor(0.2500),
 'my/other_name/recall_dog': tensor(0.5000)}
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--2361.org.readthedocs.build/en/2361/

@Borda Borda changed the title Fix MetricCollection dict keys when used with ClasswiseWrapper Fix MetricCollection dict keys when used with ClasswiseWrapper Feb 7, 2024
@Borda
Copy link
Member

Borda commented Feb 7, 2024

@sup3rgiu please see the failing doctest, seems that chnege does not work as intended yet...

@Borda
Copy link
Member

Borda commented Feb 27, 2024

@sup3rgiu let's turn it to draft until failing tests are addressed 🦩

@Borda
Copy link
Member

Borda commented May 21, 2024

@SkafteNicki, what is missing here to make it land? 🐿️

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.

None yet

3 participants