Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Address 2 comments

ananyamukh68683fee2c8f3
diff --git a/kornia/__init__.py b/kornia/__init__.py
index 8ee6afa70..adaf998bd 100644
--- a/kornia/__init__.py
+++ b/kornia/__init__.py
@@ -59,7 +59,7 @@ from kornia.losses import (
tversky_loss,
inverse_depth_smoothness_loss,
total_variation,
- psnr
+ psnr_loss
)
from kornia.utils import (
one_hot,
diff --git a/kornia/losses/__init__.py b/kornia/losses/__init__.py
index 24212d0ff..3a55dcdee 100644
--- a/kornia/losses/__init__.py
+++ b/kornia/losses/__init__.py
@@ -7,4 +7,4 @@ from .depth_smooth import (
)
from .divergence import kl_div_loss_2d, js_div_loss_2d
from .total_variation import TotalVariation, total_variation
-from .psnr import PSNR, psnr
+from .psnr import PSNRLoss, psnr_loss
diff --git a/kornia/losses/psnr.py b/kornia/losses/psnr.py
index 6690a1802..f57d8098c 100644
--- a/kornia/losses/psnr.py
+++ b/kornia/losses/psnr.py
@@ -3,7 +3,7 @@ import torch.nn as nn
from torch.nn.functional import mse_loss
-class PSNR(nn.Module):
+class PSNRLoss(nn.Module):
r"""Creates a criterion that calculates the PSNR between 2 images.
Arguments:
@@ -23,15 +23,15 @@ class PSNR(nn.Module):
"""
def __init__(self, max_val) -> None:
- super(PSNR, self).__init__()
+ super(PSNRLoss, self).__init__()
self.max_val = max_val
def forward( # type: ignore
self, signal: torch.Tensor, approximation: torch.Tensor) -> torch.Tensor:
- return psnr(signal, approximation, self.max_val)
+ return psnr_loss(signal, approximation, self.max_val)
-def psnr(signal: torch.Tensor, approximation: torch.Tensor, max_val) -> torch.Tensor:
+def psnr_loss(signal: torch.Tensor, approximation: torch.Tensor, max_val) -> torch.Tensor:
r"""Function that computes PSNR
See :class:`~kornia.losses.PSNR` for details.
@@ -41,4 +41,4 @@ def psnr(signal: torch.Tensor, approximation: torch.Tensor, max_val) -> torch.Te
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(torch.tensor(10.0))
+ return 10 * torch.log10(max_val * max_val / mse_val)
diff --git a/test/test_losses.py b/test/test_losses.py
index 2f8614f48..e1121460d 100644
--- a/test/test_losses.py
+++ b/test/test_losses.py
@@ -342,12 +342,12 @@ class TestTotalVariation:
kornia.losses.total_variation(input)
-class TestPSNR:
+class TestPSNRLoss:
def test_smoke(self):
signal = torch.rand(2, 3, 3, 2)
approximation = torch.rand(2, 3, 3, 2)
- criterion = kornia.losses.PSNR(1.0)
+ criterion = kornia.losses.PSNRLoss(1.0)
loss = criterion(signal, approximation)
assert loss.shape == tuple()
@@ -356,7 +356,7 @@ class TestPSNR:
signal = torch.rand(2, 3, 3, 2)
approximation = signal
- criterion = kornia.losses.PSNR(1.0)
+ criterion = kornia.losses.PSNRLoss(1.0)
loss = criterion(signal, approximation)
assert pytest.approx(loss.item(), float('inf'))
@@ -364,19 +364,19 @@ class TestPSNR:
def test_type(self):
# Expecting an exception
# since we pass integers instead of torch tensors
- criterion = kornia.losses.PSNR(1.0)
+ criterion = kornia.losses.PSNRLoss(1.0)
with pytest.raises(Exception) as e:
criterion(1, 2)
def test_shape(self):
# Expecting an exception
# since we pass tensors of different shapes
- criterion = kornia.losses.PSNR(1.0)
+ criterion = kornia.losses.PSNRLoss(1.0)
with pytest.raises(Exception) as e:
criterion(torch.rand(2, 3, 3, 2), torch.rand(2, 3, 3))
def test_simple(self):
- assert pytest.approx(kornia.losses.psnr(torch.ones(1), 1.2 * torch.ones(1), 2).item(), 20.0)
+ assert pytest.approx(kornia.losses.psnr_loss(torch.ones(1), 1.2 * torch.ones(1), 2).item(), 20.0)
@pytest.mark.skip(reason="TODO: implement me")
def test_jit(self):
@@ -388,5 +388,5 @@ class TestPSNR:
signal = utils.tensor_to_gradcheck_var(signal) # to var
assert gradcheck(
- kornia.losses.psnr, (signal, approximation, 1.0), raise_exception=True
+ kornia.losses.psnr_loss, (signal, approximation, 1.0), raise_exception=True
)