Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

add normalize confusion matrix and return as torch.float32

edgarribab2d31819b00d
diff --git a/mypy_files.txt b/mypy_files.txt
index c194743bb..580f6b685 100644
--- a/mypy_files.txt
+++ b/mypy_files.txt
@@ -9,3 +9,5 @@ torchgeometry/losses/tversky.py
torchgeometry/losses/depth_smooth.py
torchgeometry/contrib/spatial_soft_argmax2d.py
torchgeometry/contrib/extract_patches.py
+torchgeometry/metrics/confusion_matrix.py
+torchgeometry/metrics/mean_iou.py
diff --git a/test/test_metrics.py b/test/test_metrics.py
index 2fda3d5d0..0186accd2 100644
--- a/test/test_metrics.py
+++ b/test/test_metrics.py
@@ -83,10 +83,10 @@ class TestConfusionMatrix:
[[1, 1, 1, 1, 0, 0, 0, 1]])
conf_mat = tgm.metrics.confusion_matrix(predicted, actual, num_classes)
- assert conf_mat[..., 0, 0].item() == 3
- assert conf_mat[..., 0, 1].item() == 1
- assert conf_mat[..., 1, 0].item() == 0
- assert conf_mat[..., 1, 1].item() == 4
+ 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 = 3
@@ -99,8 +99,25 @@ class TestConfusionMatrix:
conf_mat_real = torch.tensor(
[[[4, 1, 2],
[3, 0, 2],
- [1, 2, 1]]])
- assert torch.equal(conf_mat, conf_mat_real)
+ [1, 2, 1]]], dtype=torch.float32)
+ assert utils.check_equal_torch(conf_mat, conf_mat_real)
+
+ def test_three_classes_normalized(self):
+ num_classes = 3
+ normalized = True
+ actual = torch.tensor(
+ [[2, 2, 0, 0, 1, 0, 0, 2, 1, 1, 0, 0, 1, 2, 1, 0]])
+ predicted = torch.tensor(
+ [[2, 1, 0, 0, 0, 0, 0, 1, 0, 2, 2, 1, 0, 0, 2, 2]])
+
+ conf_mat = tgm.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]]], dtype=torch.float32)
+ assert utils.check_equal_torch(conf_mat, conf_mat_real)
def test_four_classes_2d_perfect(self):
num_classes = 4
@@ -120,8 +137,8 @@ class TestConfusionMatrix:
[[[4, 0, 0, 0],
[0, 4, 0, 0],
[0, 0, 4, 0],
- [0, 0, 0, 4]]])
- assert torch.equal(conf_mat, conf_mat_real)
+ [0, 0, 0, 4]]], dtype=torch.float32)
+ assert utils.check_equal_torch(conf_mat, conf_mat_real)
def test_four_classes_2d_one_class_nonperfect(self):
num_classes = 4
@@ -141,8 +158,8 @@ class TestConfusionMatrix:
[[[3, 0, 0, 1],
[1, 3, 0, 0],
[0, 0, 4, 0],
- [0, 1, 0, 3]]])
- assert torch.equal(conf_mat, conf_mat_real)
+ [0, 1, 0, 3]]], dtype=torch.float32)
+ assert utils.check_equal_torch(conf_mat, conf_mat_real)
def test_four_classes_2d_one_class_missing(self):
num_classes = 4
@@ -162,8 +179,8 @@ class TestConfusionMatrix:
[[[0, 0, 0, 4],
[0, 4, 0, 0],
[0, 0, 4, 0],
- [0, 0, 0, 4]]])
- assert torch.equal(conf_mat, conf_mat_real)
+ [0, 0, 0, 4]]], dtype=torch.float32)
+ assert utils.check_equal_torch(conf_mat, conf_mat_real)
def test_four_classes_2d_one_class_no_predicted(self):
num_classes = 4
@@ -183,5 +200,5 @@ class TestConfusionMatrix:
[[[0, 0, 4, 4],
[0, 0, 0, 0],
[0, 0, 4, 0],
- [0, 0, 0, 4]]])
- assert torch.equal(conf_mat, conf_mat_real)
+ [0, 0, 0, 4]]], dtype=torch.float32)
+ assert utils.check_equal_torch(conf_mat, conf_mat_real)
diff --git a/torchgeometry/metrics/confusion_matrix.py b/torchgeometry/metrics/confusion_matrix.py
index 195ecd61e..0d221f3dd 100644
--- a/torchgeometry/metrics/confusion_matrix.py
+++ b/torchgeometry/metrics/confusion_matrix.py
@@ -5,14 +5,14 @@ import torch
# Inspired by:
# https://github.com/pytorch/tnt/blob/master/torchnet/meter/confusionmeter.py#L68-L73 # noqa
+
def confusion_matrix(
input: torch.Tensor,
target: torch.Tensor,
- num_classes: int) -> torch.Tensor:
+ num_classes: int,
+ normalized: Optional[float] = False) -> torch.Tensor:
r"""Compute confusion matrix to evaluate the accuracy of a classification.
- Computes the confusion matrix of K x K size where K is no of classes.
-
Args:
input (torch.Tensor) : tensor with estimated targets returned by a
classifier. The shape can be :math:`(B, *)` and must contain integer
@@ -22,9 +22,11 @@ def confusion_matrix(
values between 0 and K-1, whete targets are assumed to be provided as
one-hot vectors.
num_classes (int): total possible number of classes in target.
+ normalized: (Optional[float]): wether to return the confusion matrix
+ normalized. Default: False.
Returns:
- torch.Tensor: a tensor with the confusion matrix with shape
+ torch.Tensor: a tensor containing the confusion matrix with shape
:math:`(B, K, K)` where K is the number of classes.
"""
if not isinstance(input, torch.LongTensor):
@@ -51,4 +53,12 @@ def confusion_matrix(
confusion_vec: torch.Tensor = torch.stack([
torch.bincount(pb, minlength=num_classes**2) for pb in pre_bincount_vec
])
- return confusion_vec.view(batch_size, num_classes, num_classes) # BxKxK
+
+ 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)
+ confusion_mat = confusion_mat / (norm_val + 1e-6)
+
+ return confusion_mat