π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(metrics): accept non-contiguous label maps in confusion_matrix (#5625)
Abdulmalik Alquwayfilied3c7f83ad58
diff --git a/changelog.d/5625.fixed.md b/changelog.d/5625.fixed.mdnew file mode 100644index 000000000..f98db7df1--- /dev/null+++ b/changelog.d/5625.fixed.md@@ -0,0 +1 @@+Accept non-contiguous label tensors in `confusion_matrix` and `mean_iou`.diff --git a/kornia/metrics/confusion_matrix.py b/kornia/metrics/confusion_matrix.pyindex 33889c6a1..55ca219af 100644--- a/kornia/metrics/confusion_matrix.py+++ b/kornia/metrics/confusion_matrix.py@@ -109,7 +109,7 @@ def confusion_matrix(# NOTE: torch.bincount does not implement batched version# The cell index is formed in int64: in the labels' own dtype it wraps, for uint8 from num_classes = 17.pre_bincount: torch.Tensor = pred.long() + target.long() * num_classes- pre_bincount_vec: torch.Tensor = pre_bincount.view(batch_size, -1)+ pre_bincount_vec: torch.Tensor = pre_bincount.reshape(batch_size, -1)confusion_list = []for iter_id in range(batch_size):diff --git a/tests/metrics/test_confusion.py b/tests/metrics/test_confusion.pyindex 1a7748f5b..5ca98be28 100644--- a/tests/metrics/test_confusion.py+++ b/tests/metrics/test_confusion.py@@ -26,6 +26,28 @@ from testing.base import BaseTesterclass TestConfusionMatrix(BaseTester):+ @pytest.mark.parametrize("batch_size", [1, 2])+ @pytest.mark.parametrize("label_dtype", [torch.long, torch.uint8])+ @pytest.mark.parametrize("normalized", [False, True])+ def test_transposed_label_maps(self, device, batch_size, label_dtype, normalized):+ pred = torch.tensor([[[0, 1, 2], [2, 1, 0]], [[2, 0, 1], [1, 2, 0]]], device=device, dtype=label_dtype)[+ :batch_size+ ].transpose(1, 2)+ target = torch.tensor([[[0, 2, 2], [1, 1, 0]], [[2, 0, 0], [1, 2, 1]]], device=device, dtype=label_dtype)[+ :batch_size+ ].transpose(1, 2)+ expected = torch.tensor(+ [[[2, 0, 0], [0, 1, 1], [0, 1, 1]], [[1, 1, 0], [1, 1, 0], [0, 0, 2]]],+ device=device,+ dtype=torch.float32,+ )[:batch_size]+ if normalized:+ expected = expected / 2++ assert not pred.is_contiguous()+ assert not target.is_contiguous()+ self.assert_close(kornia.metrics.confusion_matrix(pred, target, 3, normalized), expected)+def test_two_classes(self, device, dtype):num_classes = 2actual = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 0]], device=device, dtype=torch.long)diff --git a/tests/metrics/test_mean_iou.py b/tests/metrics/test_mean_iou.pyindex 8ca5983f0..50cc46bec 100644--- a/tests/metrics/test_mean_iou.py+++ b/tests/metrics/test_mean_iou.py@@ -25,6 +25,15 @@ from testing.base import BaseTesterclass TestMeanIoU(BaseTester):+ def test_transposed_label_maps(self, device):+ pred = torch.tensor([[[0, 1, 2], [2, 1, 0]], [[2, 0, 1], [1, 2, 0]]], device=device).transpose(1, 2)+ target = torch.tensor([[[0, 2, 2], [1, 1, 0]], [[2, 0, 0], [1, 2, 1]]], device=device).transpose(1, 2)+ expected = torch.tensor([[1, 1 / 3, 1 / 3], [1 / 3, 1 / 3, 1]], device=device, dtype=torch.float32)++ assert not pred.is_contiguous()+ assert not target.is_contiguous()+ self.assert_close(kornia.metrics.mean_iou(pred, target, 3), expected)+def test_two_classes_perfect(self, device, dtype):batch_size = 1num_classes = 2