Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
32 changes: 32 additions & 0 deletions tests/metrics/classification/test_confusion_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@


class TestBinaryConfusionMatrix(MetricClassTester):
def test_binary_confusion_matrix_float_target(self) -> None:
input = torch.tensor([0, 0, 0, 1, 1, 0, 1, 1, 1, 1])
target = torch.tensor([0, 0, 0, 0, 0, 1, 1, 1, 1, 1])
for dtype in (torch.float32, torch.float64):
for normalize in (None, "pred", "true", "all"):
metric = BinaryConfusionMatrix(normalize=normalize)
other = BinaryConfusionMatrix(normalize=normalize)
metric.update(input[:5], target[:5].to(dtype))
other.update(input[5:], target[5:].to(dtype))
metric.merge_state([other])
expected = torch.tensor(
skcm(target, input, labels=[0, 1], normalize=normalize),
dtype=metric.compute().dtype,
)
torch.testing.assert_close(metric.compute(), expected)

def _test_binary_confusion_matrix_with_input(
self,
input: torch.Tensor,
Expand Down Expand Up @@ -178,6 +194,22 @@ def test_binary_confusion_matrix_invalid_input(self) -> None:


class TestMulticlassConfusionMatrix(MetricClassTester):
def test_multiclass_confusion_matrix_float_target(self) -> None:
input = torch.tensor([0, 1, 2, 1, 2, 0])
target = torch.tensor([0, 1, 1, 2, 0, 2])
for dtype in (torch.float32, torch.float64):
for normalize in (None, "pred", "true", "all"):
metric = MulticlassConfusionMatrix(3, normalize=normalize)
other = MulticlassConfusionMatrix(3, normalize=normalize)
metric.update(input[:3], target[:3].to(dtype))
other.update(input[3:], target[3:].to(dtype))
metric.merge_state([other])
expected = torch.tensor(
skcm(target, input, labels=[0, 1, 2], normalize=normalize),
dtype=metric.compute().dtype,
)
torch.testing.assert_close(metric.compute(), expected)

def _test_multiclass_confusion_matrix_with_input(
self,
input: torch.Tensor,
Expand Down
18 changes: 18 additions & 0 deletions tests/metrics/functional/classification/test_confusion_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@


class TestBinaryConfusionMatrix(unittest.TestCase):
def test_binary_confusion_matrix_float_target(self) -> None:
input = torch.tensor([0, 0, 0, 1, 1, 0, 1, 1, 1, 1])
target = torch.tensor([0, 0, 0, 0, 0, 1, 1, 1, 1, 1])
for dtype in (torch.float32, torch.float64):
for normalize in (None, "pred", "true", "all"):
self._test_binary_confusion_matrix_with_input(
input, target.to(dtype), normalize=normalize
)

def _test_binary_confusion_matrix_with_input(
self,
input: torch.Tensor,
Expand Down Expand Up @@ -105,6 +114,15 @@ def test_binary_confusion_matrix_invalid_input(self) -> None:


class TestMultiClassConfusionMatrix(unittest.TestCase):
def test_multiclass_confusion_matrix_float_target(self) -> None:
input = torch.tensor([0, 1, 2, 1, 2, 0])
target = torch.tensor([0, 1, 1, 2, 0, 2])
for dtype in (torch.float32, torch.float64):
for normalize in (None, "pred", "true", "all"):
self._test_multiclass_confusion_matrix_with_input(
input, target.to(dtype), num_classes=3, normalize=normalize
)

def _test_multiclass_confusion_matrix_with_input(
self,
input: torch.Tensor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def _update(
if input.ndim == 2:
input = torch.argmax(input, dim=1)

coordinates = torch.vstack((target, input))
coordinates = torch.vstack((target, input)).to(torch.int64)
cm_shape = torch.Size([num_classes, num_classes])

# Each prediction creates an entry at the position (true, pred)
Expand Down