Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(metrics): normalize confusion matrix rows (#5568)

Puttala Jeevan Kumar6fa965c31949
diff --git a/changelog.d/5568.breaking.md b/changelog.d/5568.breaking.md
new file mode 100644
index 000000000..45637a299
--- /dev/null
+++ b/changelog.d/5568.breaking.md
@@ -0,0 +1,7 @@
+`kornia.metrics.confusion_matrix(normalized=True)` now divides each row (target class) by its sum, so every non-empty
+row sums to 1: the per-class recall, scikit-learn's `normalize='true'`, as in torchnet's `ConfusionMeter`, which the
+code cites (#5550). Previous releases divided each column (predicted class) by its sum, scikit-learn's
+`normalize='pred'`: for targets `[0, 0, 0, 1, 1, 2, 2, 2]` and predictions `[0, 0, 1, 1, 1, 2, 2, 0]` the rows summed
+to 1, 0.6667 and 1.3333, and now each sums to 1. Empty rows stay zero, and the `1e-6` added to each sum is unchanged.
+For the previous column normalization, compute `cm / (cm.sum(dim=1, keepdim=True) + 1e-6)` from the unnormalized
+`(B, K, K)` matrix `cm`.
diff --git a/kornia/metrics/confusion_matrix.py b/kornia/metrics/confusion_matrix.py
index b94cb231d..a9f00f676 100644
--- a/kornia/metrics/confusion_matrix.py
+++ b/kornia/metrics/confusion_matrix.py
@@ -35,11 +35,13 @@ def confusion_matrix(
values between 0 and K-1, where targets are assumed to be provided as
one-hot vectors.
num_classes: total possible number of classes in target.
- normalized: whether to return the confusion matrix normalized.
+ normalized: whether to normalize each target row by its sum plus ``1e-6``.
+ Non-empty rows sum approximately to one; empty rows remain zero.
Returns:
a tensor containing the confusion matrix with shape
- :math:`(B, K, K)` where K is the number of classes.
+ :math:`(B, K, K)` where K is the number of classes, rows represent targets,
+ and columns represent predictions.
Example:
>>> logits = torch.tensor([[0, 1, 0]])
@@ -84,7 +86,7 @@ def confusion_matrix(
confusion_mat: torch.Tensor = confusion_vec.view(batch_size, num_classes, num_classes).to(torch.float32) # BxKxK
if normalized:
- norm_val: torch.Tensor = torch.sum(confusion_mat, dim=1, keepdim=True)
+ norm_val: torch.Tensor = torch.sum(confusion_mat, dim=2, keepdim=True)
confusion_mat = confusion_mat / (norm_val + 1e-6)
return confusion_mat
diff --git a/tests/metrics/test_confusion.py b/tests/metrics/test_confusion.py
index bd167ee6c..4b5397cc3 100644
--- a/tests/metrics/test_confusion.py
+++ b/tests/metrics/test_confusion.py
@@ -72,13 +72,38 @@ class TestConfusionMatrix(BaseTester):
conf_mat = kornia.metrics.confusion_matrix(predicted, actual, num_classes, normalized)
conf_mat_real = torch.tensor(
- [[[0.5000, 0.3333, 0.4000], [0.3750, 0.0000, 0.4000], [0.1250, 0.6667, 0.2000]]],
+ [[[4 / 7, 1 / 7, 2 / 7], [3 / 5, 0, 2 / 5], [1 / 4, 2 / 4, 1 / 4]]],
device=device,
dtype=torch.float32,
)
self.assert_close(conf_mat, conf_mat_real)
+ def test_normalized_target_rows_batch(self, device, dtype):
+ pred = torch.tensor([[0, 0, 1, 1, 1, 2, 2, 0], [0, 1, 1, 2, 2, 2, 2, 2]], device=device, dtype=torch.long)
+ target = torch.tensor([[0, 0, 0, 1, 1, 2, 2, 2], [0, 0, 0, 0, 0, 2, 2, 2]], device=device, dtype=torch.long)
+ counts = torch.tensor(
+ [[[2, 1, 0], [0, 2, 0], [1, 0, 2]], [[1, 2, 2], [0, 0, 0], [0, 0, 3]]],
+ device=device,
+ dtype=torch.float32,
+ )
+ expected = torch.tensor(
+ [[[2 / 3, 1 / 3, 0], [0, 1, 0], [1 / 3, 0, 2 / 3]], [[1 / 5, 2 / 5, 2 / 5], [0, 0, 0], [0, 0, 1]]],
+ device=device,
+ dtype=torch.float32,
+ )
+
+ self.assert_close(kornia.metrics.confusion_matrix(pred, target, 3), counts)
+ normalized = kornia.metrics.confusion_matrix(pred, target, 3, normalized=True)
+ self.assert_close(normalized, expected)
+ self.assert_close(
+ normalized.sum(dim=2), torch.tensor([[1, 1, 1], [1, 0, 1]], device=device, dtype=torch.float32)
+ )
+ assert not torch.allclose(normalized, counts / (counts.sum(dim=1, keepdim=True) + 1e-6))
+ assert torch.count_nonzero(normalized[1, 1]) == 0
+ assert normalized.dtype == torch.float32
+ assert normalized.device == pred.device
+
def test_four_classes_2d_perfect(self, device, dtype):
num_classes = 4
actual = torch.tensor(