Skip to content

ClasswiseWrapper silently drops values for invalid labels #3472

Description

@tandede

Bug

ClasswiseWrapper converts a metric tensor into a dictionary with zip(self.labels, x). If the number of labels does not match the number of metric values, zip silently truncates the longer input. Duplicate labels also overwrite earlier dictionary entries. Both cases can make class metrics disappear without any error.

Reproduction

import torch

from torchmetrics.classification import MulticlassAccuracy
from torchmetrics.wrappers import ClasswiseWrapper

metric = ClasswiseWrapper(
    MulticlassAccuracy(num_classes=3, average=None),
    labels=["cat", "dog"],
)

result = metric(
    torch.tensor([0, 1, 2]),
    torch.tensor([0, 1, 2]),
)
print(result)

The wrapped metric produces three class values, but the result contains only two:

{"multiclassaccuracy_cat": tensor(1.), "multiclassaccuracy_dog": tensor(1.)}

Likewise, labels such as ["cat", "cat", "dog"] produce only two dictionary keys because the second cat overwrites the first.

Expected behavior

Custom labels should define a one-to-one mapping to the wrapped metric values. Mismatched label counts and duplicate labels should raise a clear ValueError instead of silently losing results.

Environment

  • TorchMetrics: current master at e63e92a6
  • PyTorch: 2.13.0
  • Python: 3.12.13
  • macOS

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions