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

Incorrect split of columns and rows in a plot for multi label confusion matrix. #2059

Closed
DivineOblivion opened this issue Sep 5, 2023 · 1 comment · Fixed by #2060
Closed
Labels
bug / fix Something isn't working help wanted Extra attention is needed v1.1.x

Comments

@DivineOblivion
Copy link

🐛 Bug

The specific values of num_labels in MultilabelConfusionMatrix can throw IndexError in the plot function.

The _get_col_row_split function for num_labels=11 returns rows=3, cols=3, which is not enough for 11 axes.

To Reproduce

import torch
from torchmetrics.classification import MultilabelConfusionMatrix

num_labels = 11  # 17 21
example_preds = torch.rand((100, num_labels))
example_targets = torch.randint(0, 2, (100, num_labels))

conf_matrix = MultilabelConfusionMatrix(num_labels)
conf_matrix(example_preds, example_targets)
conf_matrix.plot()
Traceback
Traceback (most recent call last):
  File "D:\test\test.py", line 10, in <module>
    conf_matrix.plot()
  File "D:\test\.venv\lib\site-packages\torchmetrics\classification\confusion_matrix.py", line 466, in plot
    fig, ax = plot_confusion_matrix(val, ax=ax, add_text=add_text, labels=labels)
  File "C:\Users\Anton\AppData\Local\Programs\Python\Python310\lib\contextlib.py", line 79, in inner
    return func(*args, **kwds)
  File "D:\test\.venv\lib\site-packages\torchmetrics\utilities\plot.py", line 248, in plot_confusion_matrix
    ax = axs[i] if rows != 1 and cols != 1 else axs
IndexError: index 9 is out of bounds for axis 0 with size 9

Environment

  • TorchMetrics version (and how you installed TM, e.g. conda, pip, build from source): 1.1.1, pip
  • Python & PyTorch Version (e.g., 1.0): 3.10.9, 2.0.1
  • Any other relevant information such as OS (e.g., Linux): Windows
@DivineOblivion DivineOblivion added bug / fix Something isn't working help wanted Extra attention is needed labels Sep 5, 2023
@github-actions
Copy link

github-actions bot commented Sep 5, 2023

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.1.x
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants