π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix mean_iou batch2 unit test
edgarriba78d961c9a9ea
diff --git a/test/test_metrics.py b/test/test_metrics.pyindex 0186accd2..9eb7587c2 100644--- a/test/test_metrics.py+++ b/test/test_metrics.py@@ -10,6 +10,7 @@ from common import device_typeclass TestMeanIoU:def test_two_classes_perfect(self):+ batch_size = 1num_classes = 2actual = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 0]])@@ -17,11 +18,27 @@ class TestMeanIoU:[[1, 1, 1, 1, 0, 0, 0, 0]])mean_iou = tgm.metrics.mean_iou(predicted, actual, num_classes)- assert mean_iou.shape == (1, num_classes)- assert pytest.approx(mean_iou[..., 0].item(), 1.00)- assert pytest.approx(mean_iou[..., 1].item(), 1.00)+ mean_iou_real = torch.tensor(+ [[1.0, 1.0]], dtype=torch.float32)+ assert mean_iou.shape == (batch_size, num_classes)+ assert utils.check_equal_torch(mean_iou, mean_iou_real)++ def test_two_classes_perfect_batch2(self):+ batch_size = 2+ num_classes = 2+ actual = torch.tensor(+ [[1, 1, 1, 1, 0, 0, 0, 0]]).repeat(batch_size, 1)+ predicted = torch.tensor(+ [[1, 1, 1, 1, 0, 0, 0, 0]]).repeat(batch_size, 1)++ mean_iou = tgm.metrics.mean_iou(predicted, actual, num_classes)+ mean_iou_real = torch.tensor(+ [[1.0, 1.0]], dtype=torch.float32)+ assert mean_iou.shape == (batch_size, num_classes)+ assert utils.check_equal_torch(mean_iou, mean_iou_real)def test_two_classes(self):+ batch_size = 1num_classes = 2actual = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 0]])@@ -29,11 +46,14 @@ class TestMeanIoU:[[1, 1, 1, 1, 0, 0, 0, 1]])mean_iou = tgm.metrics.mean_iou(predicted, actual, num_classes)- assert mean_iou.shape == (1, num_classes)- assert pytest.approx(mean_iou[..., 0].item(), 0.75)- assert pytest.approx(mean_iou[..., 1].item(), 0.80)+ mean_iou = tgm.metrics.mean_iou(predicted, actual, num_classes)+ mean_iou_real = torch.tensor(+ [[0.75, 0.80]], dtype=torch.float32)+ assert mean_iou.shape == (batch_size, num_classes)+ assert utils.check_equal_torch(mean_iou, mean_iou_real)def test_four_classes_2d_perfect(self):+ batch_size = 1num_classes = 4actual = torch.tensor([[[0, 0, 1, 1],@@ -47,13 +67,13 @@ class TestMeanIoU:[2, 2, 3, 3]]])mean_iou = tgm.metrics.mean_iou(predicted, actual, num_classes)- assert mean_iou.shape == (1, num_classes)- assert pytest.approx(mean_iou[..., 0].item(), 1.00)- assert pytest.approx(mean_iou[..., 1].item(), 1.00)- assert pytest.approx(mean_iou[..., 2].item(), 1.00)- assert pytest.approx(mean_iou[..., 3].item(), 1.00)+ mean_iou_real = torch.tensor(+ [[1.0, 1.0, 1.0, 1.0]], dtype=torch.float32)+ assert mean_iou.shape == (batch_size, num_classes)+ assert utils.check_equal_torch(mean_iou, mean_iou_real)def test_four_classes_2d_one_class_no_predicted(self):+ batch_size = 1num_classes = 4actual = torch.tensor([[[0, 0, 0, 0],@@ -67,11 +87,10 @@ class TestMeanIoU:[2, 2, 3, 3]]])mean_iou = tgm.metrics.mean_iou(predicted, actual, num_classes)- assert mean_iou.shape == (1, num_classes)- assert pytest.approx(mean_iou[..., 0].item(), 0.00)- assert pytest.approx(mean_iou[..., 1].item(), 0.00)- assert pytest.approx(mean_iou[..., 2].item(), 0.50)- assert pytest.approx(mean_iou[..., 3].item(), 0.50)+ mean_iou_real = torch.tensor(+ [[0.0, 0.0, 0.5, 0.5]], dtype=torch.float32)+ assert mean_iou.shape == (batch_size, num_classes)+ assert utils.check_equal_torch(mean_iou, mean_iou_real)class TestConfusionMatrix:@@ -88,6 +107,20 @@ class TestConfusionMatrix:[0, 4]]], dtype=torch.float32)assert utils.check_equal_torch(conf_mat, conf_mat_real)+ def test_two_classes_batch2(self):+ batch_size = 2+ num_classes = 2+ actual = torch.tensor(+ [[1, 1, 1, 1, 0, 0, 0, 0]]).repeat(batch_size, 1)+ predicted = torch.tensor(+ [[1, 1, 1, 1, 0, 0, 0, 1]]).repeat(batch_size, 1)++ conf_mat = tgm.metrics.confusion_matrix(predicted, actual, num_classes)+ conf_mat_real = torch.tensor(+ [[[3, 1],+ [0, 4]]], dtype=torch.float32)+ assert utils.check_equal_torch(conf_mat, conf_mat_real)+def test_three_classes(self):num_classes = 3actual = torch.tensor(diff --git a/torchgeometry/metrics/mean_iou.py b/torchgeometry/metrics/mean_iou.pyindex 4b5c8d268..2b089c78d 100644--- a/torchgeometry/metrics/mean_iou.py+++ b/torchgeometry/metrics/mean_iou.py@@ -54,7 +54,7 @@ def mean_iou(# TODO: is it possible to vectorize this ?# iterate over classesfor class_id in range(num_classes):- tp: torch.Tensor = conf_mat[..., class_id, class_id].float()+ tp: torch.Tensor = conf_mat[..., None, class_id, class_id]total = torch.sum(conf_mat[..., class_id, :], dim=-1, keepdim=True) + \torch.sum(conf_mat[..., :, class_id], dim=-1, keepdim=True)iou_val: torch.Tensor = tp / (total.float() - tp + 1e-6)