Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Test suite: separate metrics tests into individual files (#2805)

Lyngkhoi0883059ec6da
diff --git a/tests/metrics/test_aepe.py b/tests/metrics/test_aepe.py
new file mode 100644
index 000000000..cfb62b4f9
--- /dev/null
+++ b/tests/metrics/test_aepe.py
@@ -0,0 +1,64 @@
+import pytest
+import torch
+
+import kornia
+
+from testing.base import BaseTester
+
+
+class TestAepe(BaseTester):
+ def test_metric_mean_reduction(self, device, dtype):
+ sample = torch.ones(4, 4, 2, device=device, dtype=dtype)
+ expected = torch.tensor(0.565685424, device=device, dtype=dtype)
+ actual = kornia.metrics.aepe(sample, 1.4 * sample, reduction="mean")
+ self.assert_close(actual, expected)
+
+ def test_metric_sum_reduction(self, device, dtype):
+ sample = torch.ones(4, 4, 2, device=device, dtype=dtype)
+ expected = torch.tensor(1.4142, device=device, dtype=dtype) * 4**2
+ actual = kornia.metrics.aepe(sample, 2.0 * sample, reduction="sum")
+ self.assert_close(actual, expected)
+
+ def test_metric_no_reduction(self, device, dtype):
+ sample = torch.ones(4, 4, 2, device=device, dtype=dtype)
+ expected = torch.zeros(4, 4, device=device, dtype=dtype) + 1.4142
+ actual = kornia.metrics.aepe(sample, 2.0 * sample, reduction="none")
+ self.assert_close(actual, expected)
+
+ def test_perfect_fit(self, device, dtype):
+ sample = torch.ones(4, 4, 2, device=device, dtype=dtype)
+ expected = torch.zeros(4, 4, device=device, dtype=dtype)
+ actual = kornia.metrics.aepe(sample, sample, reduction="none")
+ self.assert_close(actual, expected)
+
+ def test_aepe_alias(self, device, dtype):
+ sample = torch.ones(4, 4, 2, device=device, dtype=dtype)
+ expected = torch.zeros(4, 4, device=device, dtype=dtype)
+ actual_aepe = kornia.metrics.aepe(sample, sample, reduction="none")
+ actual_alias = kornia.metrics.average_endpoint_error(sample, sample, reduction="none")
+ self.assert_close(actual_aepe, expected)
+ self.assert_close(actual_alias, expected)
+ self.assert_close(actual_aepe, actual_alias)
+
+ def test_exception(self, device, dtype):
+ with pytest.raises(TypeError) as errinfo:
+ criterion = kornia.metrics.AEPE()
+ criterion(None, torch.ones(4, 4, 2, device=device, dtype=dtype))
+ assert "Not a Tensor type. Got" in str(errinfo)
+
+ with pytest.raises(NotImplementedError) as errinfo:
+ sample = torch.ones(4, 4, 2, device=device, dtype=dtype)
+ _ = kornia.metrics.aepe(sample, 2.0 * sample, reduction="foo")
+ assert "Invalid reduction option." in str(errinfo)
+
+ with pytest.raises(Exception) as errinfo:
+ sample = torch.ones(4, 4, 2, device=device, dtype=dtype)
+ _ = kornia.metrics.aepe(sample, 2.0 * sample[..., 0], reduction="mean")
+ assert "shape must be [['*', '2']]. Got" in str(errinfo)
+
+ def test_smoke(self, device, dtype):
+ input = torch.rand(3, 3, 2, device=device, dtype=dtype)
+ target = torch.rand(3, 3, 2, device=device, dtype=dtype)
+
+ criterion = kornia.metrics.AEPE()
+ assert criterion(input, target) is not None
diff --git a/tests/metrics/test_confusion.py b/tests/metrics/test_confusion.py
new file mode 100644
index 000000000..503c8dbf6
--- /dev/null
+++ b/tests/metrics/test_confusion.py
@@ -0,0 +1,122 @@
+import torch
+
+import kornia
+
+from testing.base import BaseTester
+
+
+class TestConfusionMatrix(BaseTester):
+ def test_two_classes(self, device, dtype):
+ num_classes = 2
+ actual = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 0]], device=device, dtype=torch.long)
+ predicted = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 1]], device=device, dtype=torch.long)
+
+ conf_mat = kornia.metrics.confusion_matrix(predicted, actual, num_classes)
+ conf_mat_real = torch.tensor([[[3, 1], [0, 4]]], device=device, dtype=torch.float32)
+ self.assert_close(conf_mat, conf_mat_real)
+
+ def test_two_classes_batch2(self, device, dtype):
+ batch_size = 2
+ num_classes = 2
+ actual = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 0]], device=device, dtype=torch.long).repeat(batch_size, 1)
+ predicted = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 1]], device=device, dtype=torch.long).repeat(batch_size, 1)
+
+ conf_mat = kornia.metrics.confusion_matrix(predicted, actual, num_classes)
+ conf_mat_real = torch.tensor([[[3, 1], [0, 4]], [[3, 1], [0, 4]]], device=device, dtype=torch.float32)
+ self.assert_close(conf_mat, conf_mat_real)
+
+ def test_three_classes(self, device, dtype):
+ num_classes = 3
+ actual = torch.tensor([[2, 2, 0, 0, 1, 0, 0, 2, 1, 1, 0, 0, 1, 2, 1, 0]], device=device, dtype=torch.long)
+ predicted = torch.tensor([[2, 1, 0, 0, 0, 0, 0, 1, 0, 2, 2, 1, 0, 0, 2, 2]], device=device, dtype=torch.long)
+
+ conf_mat = kornia.metrics.confusion_matrix(predicted, actual, num_classes)
+ conf_mat_real = torch.tensor([[[4, 1, 2], [3, 0, 2], [1, 2, 1]]], device=device, dtype=torch.float32)
+ self.assert_close(conf_mat, conf_mat_real)
+
+ def test_four_classes_one_missing(self, device, dtype):
+ num_classes = 4
+ actual = torch.tensor([[3, 3, 1, 1, 2, 1, 1, 3, 2, 2, 1, 1, 2, 3, 2, 1]], device=device, dtype=torch.long)
+ predicted = torch.tensor([[3, 2, 1, 1, 1, 1, 1, 2, 1, 3, 3, 2, 1, 1, 3, 3]], device=device, dtype=torch.long)
+
+ conf_mat = kornia.metrics.confusion_matrix(predicted, actual, num_classes)
+ conf_mat_real = torch.tensor(
+ [[[0, 0, 0, 0], [0, 4, 1, 2], [0, 3, 0, 2], [0, 1, 2, 1]]], device=device, dtype=torch.float32
+ )
+ self.assert_close(conf_mat, conf_mat_real)
+
+ def test_three_classes_normalized(self, device, dtype):
+ num_classes = 3
+ normalized = True
+ actual = torch.tensor([[2, 2, 0, 0, 1, 0, 0, 2, 1, 1, 0, 0, 1, 2, 1, 0]], device=device, dtype=torch.long)
+ predicted = torch.tensor([[2, 1, 0, 0, 0, 0, 0, 1, 0, 2, 2, 1, 0, 0, 2, 2]], device=device, dtype=torch.long)
+
+ 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]]],
+ device=device,
+ dtype=torch.float32,
+ )
+
+ self.assert_close(conf_mat, conf_mat_real)
+
+ def test_four_classes_2d_perfect(self, device, dtype):
+ num_classes = 4
+ actual = torch.tensor(
+ [[[0, 0, 1, 1], [0, 0, 1, 1], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
+ )
+ predicted = torch.tensor(
+ [[[0, 0, 1, 1], [0, 0, 1, 1], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
+ )
+
+ conf_mat = kornia.metrics.confusion_matrix(predicted, actual, num_classes)
+ conf_mat_real = torch.tensor(
+ [[[4, 0, 0, 0], [0, 4, 0, 0], [0, 0, 4, 0], [0, 0, 0, 4]]], device=device, dtype=torch.float32
+ )
+ self.assert_close(conf_mat, conf_mat_real)
+
+ def test_four_classes_2d_one_class_nonperfect(self, device, dtype):
+ num_classes = 4
+ actual = torch.tensor(
+ [[[0, 0, 1, 1], [0, 0, 1, 1], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
+ )
+ predicted = torch.tensor(
+ [[[0, 0, 1, 1], [0, 3, 0, 1], [2, 2, 1, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
+ )
+
+ conf_mat = kornia.metrics.confusion_matrix(predicted, actual, num_classes)
+ conf_mat_real = torch.tensor(
+ [[[3, 0, 0, 1], [1, 3, 0, 0], [0, 0, 4, 0], [0, 1, 0, 3]]], device=device, dtype=torch.float32
+ )
+ self.assert_close(conf_mat, conf_mat_real)
+
+ def test_four_classes_2d_one_class_missing(self, device, dtype):
+ num_classes = 4
+ actual = torch.tensor(
+ [[[0, 0, 1, 1], [0, 0, 1, 1], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
+ )
+ predicted = torch.tensor(
+ [[[3, 3, 1, 1], [3, 3, 1, 1], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
+ )
+
+ conf_mat = kornia.metrics.confusion_matrix(predicted, actual, num_classes)
+ conf_mat_real = torch.tensor(
+ [[[0, 0, 0, 4], [0, 4, 0, 0], [0, 0, 4, 0], [0, 0, 0, 4]]], device=device, dtype=torch.float32
+ )
+ self.assert_close(conf_mat, conf_mat_real)
+
+ def test_four_classes_2d_one_class_no_predicted(self, device, dtype):
+ num_classes = 4
+ actual = torch.tensor(
+ [[[0, 0, 0, 0], [0, 0, 0, 0], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
+ )
+ predicted = torch.tensor(
+ [[[3, 3, 2, 2], [3, 3, 2, 2], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
+ )
+
+ conf_mat = kornia.metrics.confusion_matrix(predicted, actual, num_classes)
+ conf_mat_real = torch.tensor(
+ [[[0, 0, 4, 4], [0, 0, 0, 0], [0, 0, 4, 0], [0, 0, 0, 4]]], device=device, dtype=torch.float32
+ )
+ self.assert_close(conf_mat, conf_mat_real)
diff --git a/tests/metrics/test_map.py b/tests/metrics/test_map.py
new file mode 100644
index 000000000..1d5f9d2a3
--- /dev/null
+++ b/tests/metrics/test_map.py
@@ -0,0 +1,32 @@
+import pytest
+import torch
+
+import kornia
+
+from testing.base import BaseTester
+
+
+class TestMeanAveragePrecision(BaseTester):
+ def test_smoke(self, device, dtype):
+ boxes = torch.tensor([[100, 50, 150, 100.0]], device=device, dtype=dtype)
+ labels = torch.tensor([1], device=device, dtype=torch.long)
+ scores = torch.tensor([0.7], device=device, dtype=dtype)
+
+ gt_boxes = torch.tensor([[100, 50, 150, 100.0]], device=device, dtype=dtype)
+ gt_labels = torch.tensor([1], device=device, dtype=torch.long)
+
+ mean_ap = kornia.metrics.mean_average_precision([boxes], [labels], [scores], [gt_boxes], [gt_labels], 2)
+
+ self.assert_close(mean_ap[0], torch.tensor(1.0, device=device, dtype=dtype))
+ self.assert_close(mean_ap[1][1], 1.0)
+
+ def test_raise(self, device, dtype):
+ boxes = torch.tensor([[100, 50, 150, 100.0]], device=device, dtype=dtype)
+ labels = torch.tensor([1], device=device, dtype=torch.long)
+ scores = torch.tensor([0.7], device=device, dtype=dtype)
+
+ gt_boxes = torch.tensor([[100, 50, 150, 100.0]], device=device, dtype=dtype)
+ gt_labels = torch.tensor([1], device=device, dtype=torch.long)
+
+ with pytest.raises(AssertionError):
+ _ = kornia.metrics.mean_average_precision(boxes[0], [labels], [scores], [gt_boxes], [gt_labels], 2)
diff --git a/tests/metrics/test_mean_iou.py b/tests/metrics/test_mean_iou.py
new file mode 100644
index 000000000..e4d268e7f
--- /dev/null
+++ b/tests/metrics/test_mean_iou.py
@@ -0,0 +1,71 @@
+import torch
+
+import kornia
+
+from testing.base import BaseTester
+
+
+class TestMeanIoU(BaseTester):
+ def test_two_classes_perfect(self, device, dtype):
+ batch_size = 1
+ num_classes = 2
+ actual = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 0]], device=device, dtype=torch.long)
+ predicted = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 0]], device=device, dtype=torch.long)
+
+ mean_iou = kornia.metrics.mean_iou(predicted, actual, num_classes)
+ mean_iou_real = torch.tensor([[1.0, 1.0]], device=device, dtype=torch.float32)
+ assert mean_iou.shape == (batch_size, num_classes)
+ self.assert_close(mean_iou, mean_iou_real)
+
+ def test_two_classes_perfect_batch2(self, device, dtype):
+ batch_size = 2
+ num_classes = 2
+ actual = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 0]], device=device, dtype=torch.long).repeat(batch_size, 1)
+ predicted = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 0]], device=device, dtype=torch.long).repeat(batch_size, 1)
+
+ mean_iou = kornia.metrics.mean_iou(predicted, actual, num_classes)
+ mean_iou_real = torch.tensor([[1.0, 1.0], [1.0, 1.0]], device=device, dtype=torch.float32)
+ assert mean_iou.shape == (batch_size, num_classes)
+ self.assert_close(mean_iou, mean_iou_real)
+
+ def test_two_classes(self, device, dtype):
+ batch_size = 1
+ num_classes = 2
+ actual = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 0]], device=device, dtype=torch.long)
+ predicted = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 1]], device=device, dtype=torch.long)
+
+ mean_iou = kornia.metrics.mean_iou(predicted, actual, num_classes)
+ mean_iou = kornia.metrics.mean_iou(predicted, actual, num_classes)
+ mean_iou_real = torch.tensor([[0.75, 0.80]], device=device, dtype=torch.float32)
+ assert mean_iou.shape == (batch_size, num_classes)
+ self.assert_close(mean_iou, mean_iou_real)
+
+ def test_four_classes_2d_perfect(self, device, dtype):
+ batch_size = 1
+ num_classes = 4
+ actual = torch.tensor(
+ [[[0, 0, 1, 1], [0, 0, 1, 1], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
+ )
+ predicted = torch.tensor(
+ [[[0, 0, 1, 1], [0, 0, 1, 1], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
+ )
+
+ mean_iou = kornia.metrics.mean_iou(predicted, actual, num_classes)
+ mean_iou_real = torch.tensor([[1.0, 1.0, 1.0, 1.0]], device=device, dtype=torch.float32)
+ assert mean_iou.shape == (batch_size, num_classes)
+ self.assert_close(mean_iou, mean_iou_real)
+
+ def test_four_classes_one_missing(self, device, dtype):
+ batch_size = 1
+ num_classes = 4
+ actual = torch.tensor(
+ [[[0, 0, 0, 0], [0, 0, 0, 0], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
+ )
+ predicted = torch.tensor(
+ [[[3, 3, 2, 2], [3, 3, 2, 2], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
+ )
+
+ mean_iou = kornia.metrics.mean_iou(predicted, actual, num_classes)
+ mean_iou_real = torch.tensor([[0.0, 1.0, 0.5, 0.5]], device=device, dtype=torch.float32)
+ assert mean_iou.shape == (batch_size, num_classes)
+ self.assert_close(mean_iou, mean_iou_real)
diff --git a/tests/metrics/test_psnr_metric.py b/tests/metrics/test_psnr_metric.py
new file mode 100644
index 000000000..4d89796b4
--- /dev/null
+++ b/tests/metrics/test_psnr_metric.py
@@ -0,0 +1,13 @@
+import torch
+
+import kornia
+
+from testing.base import BaseTester
+
+
+class TestPsnr(BaseTester):
+ def test_metric(self, device, dtype):
+ sample = torch.ones(1, device=device, dtype=dtype)
+ expected = torch.tensor(20.0, device=device, dtype=dtype)
+ actual = kornia.metrics.psnr(sample, 1.2 * sample, 2.0)
+ self.assert_close(actual, expected)
diff --git a/tests/metrics/test_ssim3d.py b/tests/metrics/test_ssim3d.py
new file mode 100644
index 000000000..420a73228
--- /dev/null
+++ b/tests/metrics/test_ssim3d.py
@@ -0,0 +1,137 @@
+import pytest
+import torch
+
+import kornia
+
+from testing.base import BaseTester
+
+
+class TestSSIM3d(BaseTester):
+ @pytest.mark.parametrize(
+ "shape,padding,window_size,max_value",
+ [
+ ((1, 1, 3, 3, 3), "same", 5, 1.0),
+ ((1, 1, 3, 3, 3), "same", 3, 2.0),
+ ((1, 1, 3, 3, 3), "same", 3, 0.5),
+ ((1, 1, 3, 3, 3), "valid", 3, 1.0),
+ ((2, 4, 3, 3, 3), "same", 3, 1.0),
+ ],
+ )
+ def test_smoke(self, shape, padding, window_size, max_value, device, dtype):
+ img_a = (torch.ones(shape, device=device, dtype=dtype) * max_value).clamp(0.0, max_value)
+ img_b = torch.zeros(shape, device=device, dtype=dtype)
+
+ actual = kornia.metrics.ssim3d(img_a, img_b, window_size, max_value, padding=padding)
+ expected = torch.ones_like(actual, device=device, dtype=dtype)
+
+ self.assert_close(actual, expected * 0.0001)
+
+ actual = kornia.metrics.ssim3d(img_a, img_a, window_size, max_value, padding=padding)
+ self.assert_close(actual, expected)
+
+ @pytest.mark.parametrize(
+ "shape,padding,window_size,expected",
+ [
+ ((1, 1, 2, 2, 3), "same", 3, (1, 1, 2, 2, 3)),
+ ((1, 1, 3, 3, 3), "same", 5, (1, 1, 3, 3, 3)),
+ ((1, 1, 3, 3, 3), "valid", 3, (1, 1, 1, 1, 1)),
+ ((2, 4, 3, 3, 3), "same", 3, (2, 4, 3, 3, 3)),
+ ],
+ )
+ def test_cardinality(self, shape, padding, window_size, expected, device, dtype):
+ img = torch.rand(shape, device=device, dtype=dtype)
+
+ actual = kornia.metrics.ssim3d(img, img, window_size, padding=padding)
+
+ assert actual.shape == expected
+
+ def test_exception(self, device, dtype):
+ img = torch.rand(1, 1, 3, 3, 3, device=device, dtype=dtype)
+
+ # Check if both are tensors
+ with pytest.raises(TypeError) as errinfo:
+ kornia.metrics.ssim3d(1.0, img, 3)
+ assert "Not a Tensor type. Got:" in str(errinfo)
+
+ with pytest.raises(TypeError) as errinfo:
+ kornia.metrics.ssim3d(img, 1.0, 3)
+ assert "Not a Tensor type. Got:" in str(errinfo)
+
+ # Check both shapes
+ img_wrong_shape = torch.rand(3, 3, device=device, dtype=dtype)
+ with pytest.raises(TypeError) as errinfo:
+ kornia.metrics.ssim3d(img, img_wrong_shape, 3)
+ assert "shape must be [" in str(errinfo)
+
+ with pytest.raises(TypeError) as errinfo:
+ kornia.metrics.ssim3d(img_wrong_shape, img, 3)
+ assert "shape must be [" in str(errinfo)
+
+ # Check if same shape
+ img_b = torch.rand(1, 1, 3, 3, 4, device=device, dtype=dtype)
+ with pytest.raises(Exception) as errinfo:
+ kornia.metrics.ssim3d(img, img_b, 3)
+ assert "img1 and img2 shapes must be the same. Got:" in str(errinfo)
+
+ def test_unit(self, device, dtype):
+ img_a = torch.tensor(
+ [
+ [
+ [
+ [[0.7, 1.0, 0.5], [1.0, 0.3, 1.0], [0.2, 1.0, 0.1]],
+ [[0.2, 1.0, 0.1], [1.0, 0.3, 1.0], [0.7, 1.0, 0.5]],
+ [[1.0, 0.3, 1.0], [0.7, 1.0, 0.5], [0.2, 1.0, 0.1]],
+ ]
+ ]
+ ],
+ device=device,
+ dtype=dtype,
+ )
+
+ img_b = torch.ones(1, 1, 3, 3, 3, device=device, dtype=dtype) * 0.5
+
+ actual = kornia.metrics.ssim3d(img_a, img_b, 3, padding="same")
+
+ expected = torch.tensor(
+ [
+ [
+ [
+ [[0.0093, 0.0080, 0.0075], [0.0075, 0.0068, 0.0063], [0.0067, 0.0060, 0.0056]],
+ [[0.0077, 0.0070, 0.0065], [0.0077, 0.0069, 0.0064], [0.0075, 0.0066, 0.0062]],
+ [[0.0075, 0.0069, 0.0064], [0.0078, 0.0070, 0.0065], [0.0077, 0.0067, 0.0064]],
+ ]
+ ]
+ ],
+ device=device,
+ dtype=dtype,
+ )
+
+ self.assert_close(actual, expected, atol=1e-4, rtol=1e-4)
+
+ @pytest.mark.parametrize(
+ "shape,padding,window_size,max_value",
+ [
+ ((1, 1, 3, 3, 3), "same", 5, 1.0),
+ ((1, 1, 3, 3, 3), "same", 3, 2.0),
+ ((1, 1, 3, 3, 3), "same", 3, 0.5),
+ ((1, 1, 3, 3, 3), "valid", 3, 1.0),
+ ],
+ )
+ def test_module(self, shape, padding, window_size, max_value, device, dtype):
+ img_a = torch.rand(shape, device=device, dtype=dtype).clamp(0.0, max_value)
+ img_b = torch.rand(shape, device=device, dtype=dtype).clamp(0.0, max_value)
+
+ ops = kornia.metrics.ssim3d
+ mod = kornia.metrics.SSIM3D(window_size, max_value, padding=padding)
+
+ ops_out = ops(img_a, img_b, window_size, max_value, padding=padding)
+ mod_out = mod(img_a, img_b)
+
+ self.assert_close(ops_out, mod_out)
+
+ def test_gradcheck(self, device):
+ img = torch.rand(1, 1, 3, 3, 3, device=device, dtype=torch.float64)
+
+ op = kornia.metrics.ssim3d
+
+ self.gradcheck(op, (img, img, 3), nondet_tol=1e-8)
diff --git a/tests/test_metrics.py b/tests/test_metrics.py
deleted file mode 100644
index 3023bbb45..000000000
--- a/tests/test_metrics.py
+++ /dev/null
@@ -1,412 +0,0 @@
-import pytest
-import torch
-
-import kornia
-
-from testing.base import BaseTester
-
-
-class TestMeanIoU(BaseTester):
- def test_two_classes_perfect(self, device, dtype):
- batch_size = 1
- num_classes = 2
- actual = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 0]], device=device, dtype=torch.long)
- predicted = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 0]], device=device, dtype=torch.long)
-
- mean_iou = kornia.metrics.mean_iou(predicted, actual, num_classes)
- mean_iou_real = torch.tensor([[1.0, 1.0]], device=device, dtype=torch.float32)
- assert mean_iou.shape == (batch_size, num_classes)
- self.assert_close(mean_iou, mean_iou_real)
-
- def test_two_classes_perfect_batch2(self, device, dtype):
- batch_size = 2
- num_classes = 2
- actual = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 0]], device=device, dtype=torch.long).repeat(batch_size, 1)
- predicted = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 0]], device=device, dtype=torch.long).repeat(batch_size, 1)
-
- mean_iou = kornia.metrics.mean_iou(predicted, actual, num_classes)
- mean_iou_real = torch.tensor([[1.0, 1.0], [1.0, 1.0]], device=device, dtype=torch.float32)
- assert mean_iou.shape == (batch_size, num_classes)
- self.assert_close(mean_iou, mean_iou_real)
-
- def test_two_classes(self, device, dtype):
- batch_size = 1
- num_classes = 2
- actual = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 0]], device=device, dtype=torch.long)
- predicted = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 1]], device=device, dtype=torch.long)
-
- mean_iou = kornia.metrics.mean_iou(predicted, actual, num_classes)
- mean_iou = kornia.metrics.mean_iou(predicted, actual, num_classes)
- mean_iou_real = torch.tensor([[0.75, 0.80]], device=device, dtype=torch.float32)
- assert mean_iou.shape == (batch_size, num_classes)
- self.assert_close(mean_iou, mean_iou_real)
-
- def test_four_classes_2d_perfect(self, device, dtype):
- batch_size = 1
- num_classes = 4
- actual = torch.tensor(
- [[[0, 0, 1, 1], [0, 0, 1, 1], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
- )
- predicted = torch.tensor(
- [[[0, 0, 1, 1], [0, 0, 1, 1], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
- )
-
- mean_iou = kornia.metrics.mean_iou(predicted, actual, num_classes)
- mean_iou_real = torch.tensor([[1.0, 1.0, 1.0, 1.0]], device=device, dtype=torch.float32)
- assert mean_iou.shape == (batch_size, num_classes)
- self.assert_close(mean_iou, mean_iou_real)
-
- def test_four_classes_one_missing(self, device, dtype):
- batch_size = 1
- num_classes = 4
- actual = torch.tensor(
- [[[0, 0, 0, 0], [0, 0, 0, 0], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
- )
- predicted = torch.tensor(
- [[[3, 3, 2, 2], [3, 3, 2, 2], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
- )
-
- mean_iou = kornia.metrics.mean_iou(predicted, actual, num_classes)
- mean_iou_real = torch.tensor([[0.0, 1.0, 0.5, 0.5]], device=device, dtype=torch.float32)
- assert mean_iou.shape == (batch_size, num_classes)
- self.assert_close(mean_iou, mean_iou_real)
-
-
-class TestConfusionMatrix(BaseTester):
- def test_two_classes(self, device, dtype):
- num_classes = 2
- actual = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 0]], device=device, dtype=torch.long)
- predicted = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 1]], device=device, dtype=torch.long)
-
- conf_mat = kornia.metrics.confusion_matrix(predicted, actual, num_classes)
- conf_mat_real = torch.tensor([[[3, 1], [0, 4]]], device=device, dtype=torch.float32)
- self.assert_close(conf_mat, conf_mat_real)
-
- def test_two_classes_batch2(self, device, dtype):
- batch_size = 2
- num_classes = 2
- actual = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 0]], device=device, dtype=torch.long).repeat(batch_size, 1)
- predicted = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 1]], device=device, dtype=torch.long).repeat(batch_size, 1)
-
- conf_mat = kornia.metrics.confusion_matrix(predicted, actual, num_classes)
- conf_mat_real = torch.tensor([[[3, 1], [0, 4]], [[3, 1], [0, 4]]], device=device, dtype=torch.float32)
- self.assert_close(conf_mat, conf_mat_real)
-
- def test_three_classes(self, device, dtype):
- num_classes = 3
- actual = torch.tensor([[2, 2, 0, 0, 1, 0, 0, 2, 1, 1, 0, 0, 1, 2, 1, 0]], device=device, dtype=torch.long)
- predicted = torch.tensor([[2, 1, 0, 0, 0, 0, 0, 1, 0, 2, 2, 1, 0, 0, 2, 2]], device=device, dtype=torch.long)
-
- conf_mat = kornia.metrics.confusion_matrix(predicted, actual, num_classes)
- conf_mat_real = torch.tensor([[[4, 1, 2], [3, 0, 2], [1, 2, 1]]], device=device, dtype=torch.float32)
- self.assert_close(conf_mat, conf_mat_real)
-
- def test_four_classes_one_missing(self, device, dtype):
- num_classes = 4
- actual = torch.tensor([[3, 3, 1, 1, 2, 1, 1, 3, 2, 2, 1, 1, 2, 3, 2, 1]], device=device, dtype=torch.long)
- predicted = torch.tensor([[3, 2, 1, 1, 1, 1, 1, 2, 1, 3, 3, 2, 1, 1, 3, 3]], device=device, dtype=torch.long)
-
- conf_mat = kornia.metrics.confusion_matrix(predicted, actual, num_classes)
- conf_mat_real = torch.tensor(
- [[[0, 0, 0, 0], [0, 4, 1, 2], [0, 3, 0, 2], [0, 1, 2, 1]]], device=device, dtype=torch.float32
- )
- self.assert_close(conf_mat, conf_mat_real)
-
- def test_three_classes_normalized(self, device, dtype):
- num_classes = 3
- normalized = True
- actual = torch.tensor([[2, 2, 0, 0, 1, 0, 0, 2, 1, 1, 0, 0, 1, 2, 1, 0]], device=device, dtype=torch.long)
- predicted = torch.tensor([[2, 1, 0, 0, 0, 0, 0, 1, 0, 2, 2, 1, 0, 0, 2, 2]], device=device, dtype=torch.long)
-
- 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]]],
- device=device,
- dtype=torch.float32,
- )
-
- self.assert_close(conf_mat, conf_mat_real)
-
- def test_four_classes_2d_perfect(self, device, dtype):
- num_classes = 4
- actual = torch.tensor(
- [[[0, 0, 1, 1], [0, 0, 1, 1], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
- )
- predicted = torch.tensor(
- [[[0, 0, 1, 1], [0, 0, 1, 1], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
- )
-
- conf_mat = kornia.metrics.confusion_matrix(predicted, actual, num_classes)
- conf_mat_real = torch.tensor(
- [[[4, 0, 0, 0], [0, 4, 0, 0], [0, 0, 4, 0], [0, 0, 0, 4]]], device=device, dtype=torch.float32
- )
- self.assert_close(conf_mat, conf_mat_real)
-
- def test_four_classes_2d_one_class_nonperfect(self, device, dtype):
- num_classes = 4
- actual = torch.tensor(
- [[[0, 0, 1, 1], [0, 0, 1, 1], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
- )
- predicted = torch.tensor(
- [[[0, 0, 1, 1], [0, 3, 0, 1], [2, 2, 1, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
- )
-
- conf_mat = kornia.metrics.confusion_matrix(predicted, actual, num_classes)
- conf_mat_real = torch.tensor(
- [[[3, 0, 0, 1], [1, 3, 0, 0], [0, 0, 4, 0], [0, 1, 0, 3]]], device=device, dtype=torch.float32
- )
- self.assert_close(conf_mat, conf_mat_real)
-
- def test_four_classes_2d_one_class_missing(self, device, dtype):
- num_classes = 4
- actual = torch.tensor(
- [[[0, 0, 1, 1], [0, 0, 1, 1], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
- )
- predicted = torch.tensor(
- [[[3, 3, 1, 1], [3, 3, 1, 1], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
- )
-
- conf_mat = kornia.metrics.confusion_matrix(predicted, actual, num_classes)
- conf_mat_real = torch.tensor(
- [[[0, 0, 0, 4], [0, 4, 0, 0], [0, 0, 4, 0], [0, 0, 0, 4]]], device=device, dtype=torch.float32
- )
- self.assert_close(conf_mat, conf_mat_real)
-
- def test_four_classes_2d_one_class_no_predicted(self, device, dtype):
- num_classes = 4
- actual = torch.tensor(
- [[[0, 0, 0, 0], [0, 0, 0, 0], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
- )
- predicted = torch.tensor(
- [[[3, 3, 2, 2], [3, 3, 2, 2], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
- )
-
- conf_mat = kornia.metrics.confusion_matrix(predicted, actual, num_classes)
- conf_mat_real = torch.tensor(
- [[[0, 0, 4, 4], [0, 0, 0, 0], [0, 0, 4, 0], [0, 0, 0, 4]]], device=device, dtype=torch.float32
- )
- self.assert_close(conf_mat, conf_mat_real)
-
-
-class TestPsnr(BaseTester):
- def test_metric(self, device, dtype):
- sample = torch.ones(1, device=device, dtype=dtype)
- expected = torch.tensor(20.0, device=device, dtype=dtype)
- actual = kornia.metrics.psnr(sample, 1.2 * sample, 2.0)
- self.assert_close(actual, expected)
-
-
-class TestAepe(BaseTester):
- def test_metric_mean_reduction(self, device, dtype):
- sample = torch.ones(4, 4, 2, device=device, dtype=dtype)
- expected = torch.tensor(0.565685424, device=device, dtype=dtype)
- actual = kornia.metrics.aepe(sample, 1.4 * sample, reduction="mean")
- self.assert_close(actual, expected)
-
- def test_metric_sum_reduction(self, device, dtype):
- sample = torch.ones(4, 4, 2, device=device, dtype=dtype)
- expected = torch.tensor(1.4142, device=device, dtype=dtype) * 4**2
- actual = kornia.metrics.aepe(sample, 2.0 * sample, reduction="sum")
- self.assert_close(actual, expected)
-
- def test_metric_no_reduction(self, device, dtype):
- sample = torch.ones(4, 4, 2, device=device, dtype=dtype)
- expected = torch.zeros(4, 4, device=device, dtype=dtype) + 1.4142
- actual = kornia.metrics.aepe(sample, 2.0 * sample, reduction="none")
- self.assert_close(actual, expected)
-
- def test_perfect_fit(self, device, dtype):
- sample = torch.ones(4, 4, 2, device=device, dtype=dtype)
- expected = torch.zeros(4, 4, device=device, dtype=dtype)
- actual = kornia.metrics.aepe(sample, sample, reduction="none")
- self.assert_close(actual, expected)
-
- def test_aepe_alias(self, device, dtype):
- sample = torch.ones(4, 4, 2, device=device, dtype=dtype)
- expected = torch.zeros(4, 4, device=device, dtype=dtype)
- actual_aepe = kornia.metrics.aepe(sample, sample, reduction="none")
- actual_alias = kornia.metrics.average_endpoint_error(sample, sample, reduction="none")
- self.assert_close(actual_aepe, expected)
- self.assert_close(actual_alias, expected)
- self.assert_close(actual_aepe, actual_alias)
-
- def test_exception(self, device, dtype):
- with pytest.raises(TypeError) as errinfo:
- criterion = kornia.metrics.AEPE()
- criterion(None, torch.ones(4, 4, 2, device=device, dtype=dtype))
- assert "Not a Tensor type. Got" in str(errinfo)
-
- with pytest.raises(NotImplementedError) as errinfo:
- sample = torch.ones(4, 4, 2, device=device, dtype=dtype)
- _ = kornia.metrics.aepe(sample, 2.0 * sample, reduction="foo")
- assert "Invalid reduction option." in str(errinfo)
-
- with pytest.raises(Exception) as errinfo:
- sample = torch.ones(4, 4, 2, device=device, dtype=dtype)
- _ = kornia.metrics.aepe(sample, 2.0 * sample[..., 0], reduction="mean")
- assert "shape must be [['*', '2']]. Got" in str(errinfo)
-
- def test_smoke(self, device, dtype):
- input = torch.rand(3, 3, 2, device=device, dtype=dtype)
- target = torch.rand(3, 3, 2, device=device, dtype=dtype)
-
- criterion = kornia.metrics.AEPE()
- assert criterion(input, target) is not None
-
-
-class TestMeanAveragePrecision(BaseTester):
- def test_smoke(self, device, dtype):
- boxes = torch.tensor([[100, 50, 150, 100.0]], device=device, dtype=dtype)
- labels = torch.tensor([1], device=device, dtype=torch.long)
- scores = torch.tensor([0.7], device=device, dtype=dtype)
-
- gt_boxes = torch.tensor([[100, 50, 150, 100.0]], device=device, dtype=dtype)
- gt_labels = torch.tensor([1], device=device, dtype=torch.long)
-
- mean_ap = kornia.metrics.mean_average_precision([boxes], [labels], [scores], [gt_boxes], [gt_labels], 2)
-
- self.assert_close(mean_ap[0], torch.tensor(1.0, device=device, dtype=dtype))
- self.assert_close(mean_ap[1][1], 1.0)
-
- def test_raise(self, device, dtype):
- boxes = torch.tensor([[100, 50, 150, 100.0]], device=device, dtype=dtype)
- labels = torch.tensor([1], device=device, dtype=torch.long)
- scores = torch.tensor([0.7], device=device, dtype=dtype)
-
- gt_boxes = torch.tensor([[100, 50, 150, 100.0]], device=device, dtype=dtype)
- gt_labels = torch.tensor([1], device=device, dtype=torch.long)
-
- with pytest.raises(AssertionError):
- _ = kornia.metrics.mean_average_precision(boxes[0], [labels], [scores], [gt_boxes], [gt_labels], 2)
-
-
-class TestSSIM3d(BaseTester):
- @pytest.mark.parametrize(
- "shape,padding,window_size,max_value",
- [
- ((1, 1, 3, 3, 3), "same", 5, 1.0),
- ((1, 1, 3, 3, 3), "same", 3, 2.0),
- ((1, 1, 3, 3, 3), "same", 3, 0.5),
- ((1, 1, 3, 3, 3), "valid", 3, 1.0),
- ((2, 4, 3, 3, 3), "same", 3, 1.0),
- ],
- )
- def test_smoke(self, shape, padding, window_size, max_value, device, dtype):
- img_a = (torch.ones(shape, device=device, dtype=dtype) * max_value).clamp(0.0, max_value)
- img_b = torch.zeros(shape, device=device, dtype=dtype)
-
- actual = kornia.metrics.ssim3d(img_a, img_b, window_size, max_value, padding=padding)
- expected = torch.ones_like(actual, device=device, dtype=dtype)
-
- self.assert_close(actual, expected * 0.0001)
-
- actual = kornia.metrics.ssim3d(img_a, img_a, window_size, max_value, padding=padding)
- self.assert_close(actual, expected)
-
- @pytest.mark.parametrize(
- "shape,padding,window_size,expected",
- [
- ((1, 1, 2, 2, 3), "same", 3, (1, 1, 2, 2, 3)),
- ((1, 1, 3, 3, 3), "same", 5, (1, 1, 3, 3, 3)),
- ((1, 1, 3, 3, 3), "valid", 3, (1, 1, 1, 1, 1)),
- ((2, 4, 3, 3, 3), "same", 3, (2, 4, 3, 3, 3)),
- ],
- )
- def test_cardinality(self, shape, padding, window_size, expected, device, dtype):
- img = torch.rand(shape, device=device, dtype=dtype)
-
- actual = kornia.metrics.ssim3d(img, img, window_size, padding=padding)
-
- assert actual.shape == expected
-
- def test_exception(self, device, dtype):
- img = torch.rand(1, 1, 3, 3, 3, device=device, dtype=dtype)
-
- # Check if both are tensors
- with pytest.raises(TypeError) as errinfo:
- kornia.metrics.ssim3d(1.0, img, 3)
- assert "Not a Tensor type. Got:" in str(errinfo)
-
- with pytest.raises(TypeError) as errinfo:
- kornia.metrics.ssim3d(img, 1.0, 3)
- assert "Not a Tensor type. Got:" in str(errinfo)
-
- # Check both shapes
- img_wrong_shape = torch.rand(3, 3, device=device, dtype=dtype)
- with pytest.raises(TypeError) as errinfo:
- kornia.metrics.ssim3d(img, img_wrong_shape, 3)
- assert "shape must be [" in str(errinfo)
-
- with pytest.raises(TypeError) as errinfo:
- kornia.metrics.ssim3d(img_wrong_shape, img, 3)
- assert "shape must be [" in str(errinfo)
-
- # Check if same shape
- img_b = torch.rand(1, 1, 3, 3, 4, device=device, dtype=dtype)
- with pytest.raises(Exception) as errinfo:
- kornia.metrics.ssim3d(img, img_b, 3)
- assert "img1 and img2 shapes must be the same. Got:" in str(errinfo)
-
- def test_unit(self, device, dtype):
- img_a = torch.tensor(
- [
- [
- [
- [[0.7, 1.0, 0.5], [1.0, 0.3, 1.0], [0.2, 1.0, 0.1]],
- [[0.2, 1.0, 0.1], [1.0, 0.3, 1.0], [0.7, 1.0, 0.5]],
- [[1.0, 0.3, 1.0], [0.7, 1.0, 0.5], [0.2, 1.0, 0.1]],
- ]
- ]
- ],
- device=device,
- dtype=dtype,
- )
-
- img_b = torch.ones(1, 1, 3, 3, 3, device=device, dtype=dtype) * 0.5
-
- actual = kornia.metrics.ssim3d(img_a, img_b, 3, padding="same")
-
- expected = torch.tensor(
- [
- [
- [
- [[0.0093, 0.0080, 0.0075], [0.0075, 0.0068, 0.0063], [0.0067, 0.0060, 0.0056]],
- [[0.0077, 0.0070, 0.0065], [0.0077, 0.0069, 0.0064], [0.0075, 0.0066, 0.0062]],
- [[0.0075, 0.0069, 0.0064], [0.0078, 0.0070, 0.0065], [0.0077, 0.0067, 0.0064]],
- ]
- ]
- ],
- device=device,
- dtype=dtype,
- )
-
- self.assert_close(actual, expected, atol=1e-4, rtol=1e-4)
-
- @pytest.mark.parametrize(
- "shape,padding,window_size,max_value",
- [
- ((1, 1, 3, 3, 3), "same", 5, 1.0),
- ((1, 1, 3, 3, 3), "same", 3, 2.0),
- ((1, 1, 3, 3, 3), "same", 3, 0.5),
- ((1, 1, 3, 3, 3), "valid", 3, 1.0),
- ],
- )
- def test_module(self, shape, padding, window_size, max_value, device, dtype):
- img_a = torch.rand(shape, device=device, dtype=dtype).clamp(0.0, max_value)
- img_b = torch.rand(shape, device=device, dtype=dtype).clamp(0.0, max_value)
-
- ops = kornia.metrics.ssim3d
- mod = kornia.metrics.SSIM3D(window_size, max_value, padding=padding)
-
- ops_out = ops(img_a, img_b, window_size, max_value, padding=padding)
- mod_out = mod(img_a, img_b)
-
- self.assert_close(ops_out, mod_out)
-
- def test_gradcheck(self, device):
- img = torch.rand(1, 1, 3, 3, 3, device=device, dtype=torch.float64)
-
- op = kornia.metrics.ssim3d
-
- self.gradcheck(op, (img, img, 3), nondet_tol=1e-8)