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

Error for index tensor verification in GeneralizedDice if target is zeros #2563

Closed
juliendenize opened this issue May 27, 2024 · 1 comment · Fixed by #2572
Closed

Error for index tensor verification in GeneralizedDice if target is zeros #2563

juliendenize opened this issue May 27, 2024 · 1 comment · Fixed by #2572
Labels
bug / fix Something isn't working help wanted Extra attention is needed v1.4.x

Comments

@juliendenize
Copy link

🐛 Bug

To compute segmentation mask GeneralizedDice metrics one can use index tensors as stated by the documentation.

However, if the mask is filled by zeros the conversion to one_hot label will not work in the following lines:

if (target.bool() != target).any():  # target is an index tensor
    target = torch.nn.functional.one_hot(target, num_classes=num_classes).movedim(-1, 1)

To Reproduce

Run the following code snippet:

import torch
from torchmetrics.segmentation import GeneralizedDiceScore

metric = GeneralizedDiceScore(num_classes=3)

target = torch.zeros((2, 3, 3), dtype=torch.int64)
predictions = torch.randint(0, 3, (2, 3, 3), dtype=torch.int64)

metric(predictions, target)

>>> RuntimeError: The size of tensor a (3) must match the size of tensor b (2) at non-singleton dimension 1

Expected behavior

The expected behavior is to have the metric computed after the conversion of the target to a one-hot label.

Environment

  • TorchMetrics version 1.4.0

Additional context

@juliendenize juliendenize added bug / fix Something isn't working help wanted Extra attention is needed labels May 27, 2024
Copy link

Hi! thanks for your contribution!, great first issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug / fix Something isn't working help wanted Extra attention is needed v1.4.x
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants