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

Bugfix so multilabel confusion matrix can plot for 2 or more labels #2858

Merged
merged 19 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Fixed plotting of multilabel confusion matrix ([#2858](https://github.com/PyTorchLightning/metrics/pull/2858))
Borda marked this conversation as resolved.
Show resolved Hide resolved


- Fixed issue with shared state in metric collection when using dice score ([#2848](https://github.com/PyTorchLightning/metrics/pull/2848))


Expand Down
2 changes: 1 addition & 1 deletion src/torchmetrics/utilities/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def plot_confusion_matrix(
fig, axs = plt.subplots(nrows=rows, ncols=cols, constrained_layout=True) if ax is None else (ax.get_figure(), ax)
axs = trim_axs(axs, nb)
for i in range(nb):
ax = axs[i] if rows != 1 and cols != 1 else axs
ax = axs[i] if (rows != 1 or cols != 1) else axs
if fig_label is not None:
ax.set_title(f"Label {fig_label[i]}", fontsize=15)
im = ax.imshow(confmat[i].cpu().detach() if confmat.ndim == 3 else confmat.cpu().detach(), cmap=cmap)
Expand Down
10 changes: 10 additions & 0 deletions tests/unittests/classification/test_confusion_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,16 @@ def test_multilabel_confusion_matrix_dtype_gpu(self, inputs, dtype):
dtype=dtype,
)

@pytest.mark.parametrize("num_labels", [2, NUM_CLASSES])
def test_multilabel_confusion_matrix_plot(self, num_labels):
"""Test multilabel cm plots."""
multi_label_confusion_matrix = MultilabelConfusionMatrix(num_labels=num_labels)
preds = target = torch.ones(1, num_labels).int()
multi_label_confusion_matrix.update(preds, target)
fig, ax = multi_label_confusion_matrix.plot()
assert fig is not None
assert ax is not None


def test_warning_on_nan():
"""Test that a warning is given if division by zero happens during normalization of confusion matrix."""
Expand Down
Loading