Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Tests and style

ananyamukh60e308d202cdc
diff --git a/kornia/losses/psnr.py b/kornia/losses/psnr.py
index d48904018..042f9d930 100644
--- a/kornia/losses/psnr.py
+++ b/kornia/losses/psnr.py
@@ -7,7 +7,7 @@ class PSNR(nn.Module):
r"""Creates a criterion that calculates the PSNR between 2 images.
Arguments:
- max_val (float): Maximum value os signal
+ max_val (float): Maximum value of signal
Shape:
- signal: :math:`(*)`
@@ -22,15 +22,15 @@ class PSNR(nn.Module):
def __init__(self, max_val):
self.max_val = max_val
-
- def forward(self, signal: torch.tensor, approximation: torch.tensor):
+ def forward( # type: ignore
+ self, signal: torch.Tensor, approximation: torch.Tensor) -> torch.Tensor:
return psnr(signal, approximation, self.max_val)
-def psnr (signal: torch.tensor, approximation: torch.tensor, max_val):
+def psnr(signal: torch.Tensor, approximation: torch.Tensor, max_val) -> torch.Tensor:
if not torch.is_tensor(signal) or not torch.is_tensor(approximation):
raise TypeError(f"Expected 2 torch tensors but got {type(signal)} and {type(approximation)}")
if signal.shape != approximation.shape:
raise TypeError(f"Expected tensors of equal shapes, but got {signal.shape} and {approximation.shape}")
- mse_val = mse_loss(signal, approximation, reduction = 'mean')
- return 10 * torch.log(max_val*max_val/mse_val)/torch.log(10)
+ mse_val = mse_loss(signal, approximation, reduction='mean')
+ return 10 * torch.log(max_val * max_val / mse_val) / torch.log(torch.tensor(10.0))
diff --git a/kornia/losses/total_variation.py b/kornia/losses/total_variation.py
index 35ed8433e..6aa14416d 100644
--- a/kornia/losses/total_variation.py
+++ b/kornia/losses/total_variation.py
@@ -14,6 +14,7 @@ class TotalVariation(nn.Module):
>>> output = tv(torch.ones(2,3,4,4)) # tensor([0., 0.])
>>> output.backward()
"""
+
def __init__(self) -> None:
super(TotalVariation, self).__init__()
diff --git a/test/test_losses.py b/test/test_losses.py
index 36ba11dbd..1fc843eed 100644
--- a/test/test_losses.py
+++ b/test/test_losses.py
@@ -340,3 +340,28 @@ class TestTotalVariation:
def test_tv_on_invalid_types(self, input):
with pytest.raises(TypeError) as ex_info:
kornia.losses.total_variation(input)
+
+
+class TestPSNR:
+ def test_smoke(self):
+ signal = torch.rand(2, 3, 3, 2)
+ approximation = torch.rand(2, 3, 3, 2)
+
+ criterion = kornia.losses.PSNR(1.0)
+ loss = criterion(signal, approximation)
+
+ def test_same_signal(self):
+ signal = torch.rand(2, 3, 3, 2)
+ approximation = signal
+
+ criterion = kornia.losses.PSNR(1.0)
+ loss = criterion(signal, approximation)
+
+ assert pytest.approx(loss.item(), float('inf'))
+
+ @pytest.mark.skip(reason="TODO: implement me")
+ def test_jit(self):
+ pass
+
+ def test_gradcheck(self):
+ pass