Skip to content

[Draft] add class-wise metrics #2800

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

robmarkcole
Copy link
Contributor

Replaces #2130

Adds support for labels to be used in ClasswiseWrapper

@github-actions github-actions bot added the trainers PyTorch Lightning trainers label May 26, 2025
@@ -180,33 +196,159 @@ def configure_losses(self) -> None:
def configure_metrics(self) -> None:
"""Initialize the performance metrics.

* :class:`~torchmetrics.Accuracy`: Overall accuracy
(OA) using 'micro' averaging. The number of true positives divided by the
Copy link
Collaborator

Choose a reason for hiding this comment

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

These need to be indented for Sphinx to parse the bulleted list correctly

dataset size. Higher values are better.
* :class:`~torchmetrics.JaccardIndex`: Intersection
over union (IoU). Uses 'micro' averaging. Higher valuers are better.
* :class:`~torchmetrics.classification.MulticlassAccuracy`: Overall accuracy
Copy link
Collaborator

Choose a reason for hiding this comment

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

This change seems like going in the wrong direction, shouldn't we keep the generic class links?

Comment on lines +221 to +227
expected_classes = 2 if task == 'binary' else num_classes
if len(labels) != expected_classes:
raise ValueError(
f"Length of labels ({len(labels)}) must equal number of classes "
f"({expected_classes}). Either provide {expected_classes} labels or "
f"set labels=None to use default integer names."
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is currently missing test coverage


# Validate labels length if provided
if labels is not None and task in ['binary', 'multiclass']:
expected_classes = 2 if task == 'binary' else num_classes
Copy link
Collaborator

Choose a reason for hiding this comment

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

In the above docs, we say that "labels" is only used for task=multiclass, this disagrees with that

self.test_metrics = metrics.clone(prefix='test_')

# For binary and multilabel tasks, we only need micro averaging
if task in ['binary', 'multilabel']:
Copy link
Collaborator

Choose a reason for hiding this comment

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

This whole if branch can be replaced with:

for average in ['micro', 'macro']:
    # For binary and multilabel tasks, we only need micro averaging
    if task in ['binary', 'multilabel'] and average == 'macro':
        continue
    ...

Then we don't have to duplicate.

)

# Add classwise metrics for binary and multiclass (not multilabel)
if task in ['binary', 'multiclass'] and labels is not None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we combine this too, something like:

for average in ['micro', 'macro', 'global']:
    metric = ...
    if average == 'global':
        metric = ClasswiseWrapper(metric)
    ...

@adamjstewart adamjstewart added this to the 0.8.0 milestone Jun 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
trainers PyTorch Lightning trainers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants