Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

refactor(tests/losses): Add @pytest.mark.grad() decorator and dtype parameter to test_gradcheck methods (#3414)

arc-wonders791c978ba30e
diff --git a/tests/losses/test_cauchy.py b/tests/losses/test_cauchy.py
index a49bf2bd9..8be556a44 100644
--- a/tests/losses/test_cauchy.py
+++ b/tests/losses/test_cauchy.py
@@ -68,7 +68,8 @@ class TestCauchyLoss(BaseTester):
actual = kornia.losses.cauchy_loss(img, img, reduction="mean")
assert actual.shape == ()
- def test_gradcheck(self, device):
+ @pytest.mark.grad()
+ def test_gradcheck(self, device, dtype):
img1 = torch.rand(2, 3, 3, 3, device=device, dtype=torch.float64)
img2 = torch.rand(2, 3, 3, 3, device=device, dtype=torch.float64)
diff --git a/tests/losses/test_charbonnier.py b/tests/losses/test_charbonnier.py
index 18a576990..e655b4233 100644
--- a/tests/losses/test_charbonnier.py
+++ b/tests/losses/test_charbonnier.py
@@ -68,7 +68,8 @@ class TestCharbonnierLoss(BaseTester):
actual = kornia.losses.charbonnier_loss(img, img, reduction="mean")
assert actual.shape == ()
- def test_gradcheck(self, device):
+ @pytest.mark.grad()
+ def test_gradcheck(self, device, dtype):
img1 = torch.rand(2, 3, 3, 3, device=device, dtype=torch.float64)
img2 = torch.rand(2, 3, 3, 3, device=device, dtype=torch.float64)
diff --git a/tests/losses/test_depth_smoothness.py b/tests/losses/test_depth_smoothness.py
index dac858d59..44ed66b1a 100644
--- a/tests/losses/test_depth_smoothness.py
+++ b/tests/losses/test_depth_smoothness.py
@@ -81,7 +81,8 @@ class TestDepthSmoothnessLoss(BaseTester):
self.assert_close(op(image, depth), op_module(image, depth))
- def test_gradcheck(self, device):
+ @pytest.mark.grad()
+ def test_gradcheck(self, device, dtype):
image = torch.rand(1, 2, 3, 4, device=device, dtype=torch.float64)
depth = torch.rand(1, 2, 3, 4, device=device, dtype=torch.float64)
self.gradcheck(kornia.losses.inverse_depth_smoothness_loss, (depth, image))
diff --git a/tests/losses/test_dice.py b/tests/losses/test_dice.py
index caa8273a7..50cd3eb77 100644
--- a/tests/losses/test_dice.py
+++ b/tests/losses/test_dice.py
@@ -194,7 +194,8 @@ class TestDiceLoss(BaseTester):
loss = criterion(logits, labels)
self.assert_close(loss, expected_loss, rtol=1e-3, atol=1e-3)
- def test_gradcheck(self, device):
+ @pytest.mark.grad()
+ def test_gradcheck(self, device, dtype):
num_classes = 3
logits = torch.rand(2, num_classes, 3, 2, device=device, dtype=torch.float64)
labels = torch.randint(0, num_classes, (2, 3, 2), device=device)
diff --git a/tests/losses/test_divergence.py b/tests/losses/test_divergence.py
index a16643089..a99d055a0 100644
--- a/tests/losses/test_divergence.py
+++ b/tests/losses/test_divergence.py
@@ -99,7 +99,8 @@ class TestDivergenceLoss(BaseTester):
expected = torch.tensor(expected).to(device, dtype)
self.assert_close(actual, expected)
- def test_gradcheck_kl(self, device):
+ @pytest.mark.grad()
+ def test_gradcheck_kl(self, device, dtype):
dtype = torch.float64
pred = torch.rand(1, 1, 10, 16, device=device, dtype=dtype)
target = torch.rand(1, 1, 10, 16, device=device, dtype=dtype)
@@ -107,7 +108,8 @@ class TestDivergenceLoss(BaseTester):
# evaluate function gradient
self.gradcheck(kornia.losses.kl_div_loss_2d, (pred, target))
- def test_gradcheck_js(self, device):
+ @pytest.mark.grad()
+ def test_gradcheck_js(self, device, dtype):
dtype = torch.float64
pred = torch.rand(1, 1, 10, 16, device=device, dtype=dtype)
target = torch.rand(1, 1, 10, 16, device=device, dtype=dtype)
diff --git a/tests/losses/test_focal_loss.py b/tests/losses/test_focal_loss.py
index 63cc3ceaf..9ccf1efd0 100644
--- a/tests/losses/test_focal_loss.py
+++ b/tests/losses/test_focal_loss.py
@@ -107,7 +107,8 @@ class TestBinaryFocalLossWithLogits(BaseTester):
expected = op(logits, labels, *args)
self.assert_close(actual, expected)
- def test_gradcheck(self, device):
+ @pytest.mark.grad()
+ def test_gradcheck(self, device, dtype):
logits = torch.rand(2, 3, 2, device=device, dtype=torch.float64)
labels = torch.rand(2, 3, 2, device=device, dtype=torch.float64)
@@ -115,7 +116,8 @@ class TestBinaryFocalLossWithLogits(BaseTester):
op = kornia.losses.binary_focal_loss_with_logits
self.gradcheck(op, (logits, labels, *args))
- def test_gradcheck_ignore_index(self, device):
+ @pytest.mark.grad()
+ def test_gradcheck_ignore_index(self, device, dtype):
logits = torch.rand(2, 3, 2, device=device, dtype=torch.float64)
labels = torch.rand(2, 3, 2, device=device, dtype=torch.float64)
ignore = torch.rand(2, 3, 2, device=device) > 0.8
@@ -230,7 +232,8 @@ class TestFocalLoss(BaseTester):
expected = op(logits, labels, *args)
self.assert_close(actual, expected)
- def test_gradcheck(self, device):
+ @pytest.mark.grad()
+ def test_gradcheck(self, device, dtype):
num_classes = 3
logits = torch.rand(2, num_classes, 3, 2, device=device, dtype=torch.float64)
labels = torch.randint(num_classes, (2, 3, 2), device=device).long()
diff --git a/tests/losses/test_geman_macclure.py b/tests/losses/test_geman_macclure.py
index 328305216..c341725a7 100644
--- a/tests/losses/test_geman_macclure.py
+++ b/tests/losses/test_geman_macclure.py
@@ -68,7 +68,8 @@ class TestGemanMcclureLossLoss(BaseTester):
actual = kornia.losses.geman_mcclure_loss(img, img, reduction="mean")
assert actual.shape == ()
- def test_gradcheck(self, device):
+ @pytest.mark.grad()
+ def test_gradcheck(self, device, dtype):
img1 = torch.rand(2, 3, 3, 3, device=device, dtype=torch.float64)
img2 = torch.rand(2, 3, 3, 3, device=device, dtype=torch.float64)
diff --git a/tests/losses/test_hd.py b/tests/losses/test_hd.py
index 4fa343316..3feac6ea2 100644
--- a/tests/losses/test_hd.py
+++ b/tests/losses/test_hd.py
@@ -92,7 +92,8 @@ class TestHausdorffLoss(BaseTester):
@pytest.mark.parametrize(
"hd,shape", [[kornia.losses.HausdorffERLoss, (5, 5)], [kornia.losses.HausdorffERLoss3D, (5, 5, 5)]]
)
- def test_gradcheck(self, hd, shape, device):
+ @pytest.mark.grad()
+ def test_gradcheck(self, hd, shape, device, dtype):
num_classes = 3
logits = torch.rand(2, num_classes, *shape, device=device)
labels = (torch.rand(2, 1, *shape, device=device) * (num_classes - 1)).long()
diff --git a/tests/losses/test_lovaz_hinge.py b/tests/losses/test_lovaz_hinge.py
index d75ef7073..90d71c3de 100644
--- a/tests/losses/test_lovaz_hinge.py
+++ b/tests/losses/test_lovaz_hinge.py
@@ -63,7 +63,8 @@ class TestLovaszHingeLoss(BaseTester):
loss = criterion(prediction, labels)
self.assert_close(loss, torch.zeros_like(loss), rtol=1e-3, atol=1e-3)
- def test_gradcheck(self, device):
+ @pytest.mark.grad()
+ def test_gradcheck(self, device, dtype):
dtype = torch.float64
num_classes = 1
logits = torch.rand(2, num_classes, 3, 2, device=device, dtype=dtype)
diff --git a/tests/losses/test_lovaz_softmax.py b/tests/losses/test_lovaz_softmax.py
index 93253a3e8..1a2b6f472 100644
--- a/tests/losses/test_lovaz_softmax.py
+++ b/tests/losses/test_lovaz_softmax.py
@@ -92,7 +92,8 @@ class TestLovaszSoftmaxLoss(BaseTester):
self.assert_close(loss, 0.5 * torch.ones_like(loss), rtol=1e-3, atol=1e-3)
- def test_gradcheck(self, device):
+ @pytest.mark.grad()
+ def test_gradcheck(self, device, dtype):
num_classes = 4
logits = torch.rand(2, num_classes, 3, 2, device=device, dtype=torch.float64)
labels = torch.randint(0, num_classes, (2, 3, 2), device=device)
diff --git a/tests/losses/test_psnr.py b/tests/losses/test_psnr.py
index 7342a7997..5a7520796 100644
--- a/tests/losses/test_psnr.py
+++ b/tests/losses/test_psnr.py
@@ -75,6 +75,7 @@ class TestPSNRLoss(BaseTester):
self.assert_close(op(*args), op_module(pred, target))
+ @pytest.mark.grad()
def test_gradcheck(self, device, dtype):
dtype = torch.float64
pred = torch.rand(2, 3, 3, 2, device=device, dtype=dtype)
diff --git a/tests/losses/test_ssim.py b/tests/losses/test_ssim.py
index c33b59ae7..f673f1d45 100644
--- a/tests/losses/test_ssim.py
+++ b/tests/losses/test_ssim.py
@@ -65,7 +65,8 @@ class TestSSIMLoss(BaseTester):
self.assert_close(op(*args), op_module(*args[:2]))
- def test_gradcheck(self, device):
+ @pytest.mark.grad()
+ def test_gradcheck(self, device, dtype):
# input data
window_size = 3
img1 = torch.rand(1, 1, 5, 4, device=device, dtype=torch.float64)
@@ -116,7 +117,8 @@ class TestMS_SSIMLoss(BaseTester):
self.assert_close(loss.sum().item(), 0.0)
- def test_gradcheck(self, device):
+ @pytest.mark.grad()
+ def test_gradcheck(self, device, dtype):
# input data
dtype = torch.float64
img1 = torch.rand(1, 1, 5, 5, device=device, dtype=dtype)
@@ -194,7 +196,8 @@ class TestSSIM3DLoss(BaseTester):
self.assert_close(op(*args), op_module(*args[:2]))
- def test_gradcheck(self, device):
+ @pytest.mark.grad()
+ def test_gradcheck(self, device, dtype):
# input data
img = torch.rand(1, 1, 5, 4, 3, device=device)
diff --git a/tests/losses/test_total_variation.py b/tests/losses/test_total_variation.py
index 931d9bc5e..e28ccc4a7 100644
--- a/tests/losses/test_total_variation.py
+++ b/tests/losses/test_total_variation.py
@@ -165,7 +165,8 @@ class TestTotalVariation(BaseTester):
self.assert_close(op(image), op_module(image))
- def test_gradcheck(self, device):
+ @pytest.mark.grad()
+ def test_gradcheck(self, device, dtype):
dtype = torch.float64
image = torch.rand(1, 2, 3, 4, device=device, dtype=dtype)
self.gradcheck(kornia.losses.total_variation, (image,))
diff --git a/tests/losses/test_tversky.py b/tests/losses/test_tversky.py
index 7ff9eb006..242a825b1 100644
--- a/tests/losses/test_tversky.py
+++ b/tests/losses/test_tversky.py
@@ -80,7 +80,8 @@ class TestTverskyLoss(BaseTester):
loss = criterion(logits, labels)
self.assert_close(loss, expected_loss, rtol=1e-3, atol=1e-3)
- def test_gradcheck(self, device):
+ @pytest.mark.grad()
+ def test_gradcheck(self, device, dtype):
num_classes = 3
alpha, beta = 0.5, 0.5 # for tversky loss
logits = torch.rand(2, num_classes, 3, 2, device=device, dtype=torch.float64)
diff --git a/tests/losses/test_welcsh.py b/tests/losses/test_welcsh.py
index 8858bdce3..9fdfb0c0f 100644
--- a/tests/losses/test_welcsh.py
+++ b/tests/losses/test_welcsh.py
@@ -42,7 +42,8 @@ class TestWelschLoss(BaseTester):
actual = kornia.losses.WelschLoss(reduction="mean")(img, img)
assert actual.shape == ()
- def test_gradcheck(self, device):
+ @pytest.mark.grad()
+ def test_gradcheck(self, device, dtype):
img1 = torch.rand(2, 3, 3, 3, device=device, dtype=torch.float64)
img2 = torch.rand(2, 3, 3, 3, device=device, dtype=torch.float64)