Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

implement psnr cuda tests

edgarriba0cda7011542f
diff --git a/kornia/losses/psnr.py b/kornia/losses/psnr.py
index ade19bed4..9f811ca84 100644
--- a/kornia/losses/psnr.py
+++ b/kornia/losses/psnr.py
@@ -26,10 +26,9 @@ class PSNRLoss(nn.Module):
def __init__(self, max_val: float) -> None:
super(PSNRLoss, self).__init__()
- self.max_val = max_val
+ self.max_val: float = max_val
- def forward( # type: ignore
- self, input: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
+ def forward(self, input: torch.Tensor, target: torch.Tensor) -> torch.Tensor: # type: ignore
return psnr_loss(input, target, self.max_val)
@@ -40,8 +39,9 @@ def psnr_loss(input: torch.Tensor, target: torch.Tensor, max_val: float) -> torc
"""
if not torch.is_tensor(input) or not torch.is_tensor(target):
raise TypeError(f"Expected 2 torch tensors but got {type(input)} and {type(target)}")
+
if input.shape != target.shape:
raise TypeError(f"Expected tensors of equal shapes, but got {input.shape} and {target.shape}")
mse_val = mse_loss(input, target, reduction='mean')
- max_val_tensor: torch.Tensor = torch.tensor(max_val)
+ max_val_tensor: torch.Tensor = torch.tensor(max_val).to(input.device).to(input.dtype)
return 10 * torch.log10(max_val_tensor * max_val_tensor / mse_val)
diff --git a/test/test_losses.py b/test/test_losses.py
index 17816e217..ef44d58f6 100644
--- a/test/test_losses.py
+++ b/test/test_losses.py
@@ -343,23 +343,23 @@ class TestTotalVariation:
class TestPSNRLoss:
- def test_smoke(self):
- input = torch.rand(2, 3, 3, 2)
- target = torch.rand(2, 3, 3, 2)
+ def test_smoke(self, device):
+ input = torch.rand(2, 3, 3, 2).to(device)
+ target = torch.rand(2, 3, 3, 2).to(device)
criterion = kornia.losses.PSNRLoss(1.0)
loss = criterion(input, target)
assert loss.shape == tuple()
- def test_same_input(self):
- input = torch.rand(2, 3, 3, 2)
- target = input
+ def test_same_input(self, device):
+ input = torch.rand(2, 3, 3, 2).to(device)
+ target = input.clone()
criterion = kornia.losses.PSNRLoss(1.0)
loss = criterion(input, target)
- assert_allclose(loss, torch.tensor(float('inf')))
+ assert_allclose(loss, torch.tensor(float('inf')).to(device))
def test_type(self):
# Expecting an exception
@@ -375,18 +375,24 @@ class TestPSNRLoss:
with pytest.raises(Exception) as e:
criterion(torch.rand(2, 3, 3, 2), torch.rand(2, 3, 3))
- def test_simple(self):
- assert_allclose(kornia.losses.psnr_loss(torch.ones(1), 1.2 * torch.ones(1), 2), torch.tensor(20.0))
+ def test_simple(self, device):
+ assert_allclose(
+ kornia.losses.psnr_loss(
+ torch.ones(1).to(device),
+ 1.2 * torch.ones(1).to(device),
+ 2),
+ torch.tensor(20.0).to(device))
@pytest.mark.skip(reason="TODO: implement me")
def test_jit(self):
pass
- def test_gradcheck(self):
- input = torch.rand(2, 3, 3, 2).double()
- target = torch.rand(2, 3, 3, 2).double()
+ def test_gradcheck(self, device):
+ input = torch.rand(2, 3, 3, 2).to(device)
+ target = torch.rand(2, 3, 3, 2).to(device)
input = utils.tensor_to_gradcheck_var(input) # to var
+ target = utils.tensor_to_gradcheck_var(target) # to var
assert gradcheck(
kornia.losses.psnr_loss, (input, target, 1.0), raise_exception=True
)