Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Fix PSNR with integer tensor data ranges (#6444)

A Aswanth Rajefe1ceed801d
diff --git a/changelog.d/6444.fixed.md b/changelog.d/6444.fixed.md
new file mode 100644
index 000000000..20f70f28b
--- /dev/null
+++ b/changelog.d/6444.fixed.md
@@ -0,0 +1 @@
+`psnr`, `psnr_loss` and `PSNRLoss` no longer overflow an integer tensor data range: `max_val=torch.tensor(255, dtype=torch.uint8)` with an MSE of 1 returned 0 dB instead of 48.13 dB, and an `int16` range of 255 returned NaN. Python numbers and `int64` or floating tensor ranges are unchanged.
diff --git a/kornia/metrics/psnr.py b/kornia/metrics/psnr.py
index abd644994..4fa8929ea 100644
--- a/kornia/metrics/psnr.py
+++ b/kornia/metrics/psnr.py
@@ -50,6 +50,8 @@ def psnr(image: torch.Tensor, target: torch.Tensor, max_val: _PSNRMaxVal) -> tor
- ``image`` and ``target`` must have the same shape, without broadcasting; the value is symmetric in them.
Identical inputs give an MSE of 0 and ``inf``, while a batch with one identical pair stays finite.
- Integer and bool images are converted to float32 before computing the MSE; pixel values are not rescaled.
+ Uint8, int8, int16 and int32 tensor data ranges are widened to int64 before squaring to avoid overflow,
+ except when the promoted image dtype is float16 or bfloat16, which uses float32 range arithmetic.
- Float16 and bfloat16 inputs are evaluated in float32; the result retains their promoted input dtype,
also promoted with ``max_val.dtype`` when the data range is a tensor. A float32 tensor data range therefore
gives a float32 result for half-precision images.
@@ -106,6 +108,8 @@ def psnr(image: torch.Tensor, target: torch.Tensor, max_val: _PSNRMaxVal) -> tor
mse_value = mse(image, target, reduction="mean")
# TorchScript needs the union refined before selecting a pow overload.
if isinstance(max_val, torch.Tensor):
+ if max_val.dtype in (torch.uint8, torch.int8, torch.int16, torch.int32):
+ max_val = max_val.to(torch.int64)
ratio = torch.pow(max_val, 2) / mse_value
elif isinstance(max_val, int):
ratio = max_val**2 / mse_value
diff --git a/tests/metrics/test_psnr_metric.py b/tests/metrics/test_psnr_metric.py
index da40da159..0ac469978 100644
--- a/tests/metrics/test_psnr_metric.py
+++ b/tests/metrics/test_psnr_metric.py
@@ -179,6 +179,41 @@ class TestConventionsPsnr(BaseTester):
assert torch.isfinite(kornia.metrics.psnr(a.to(dtype), b.to(dtype), 255.0))
+class TestPSNRIntegerRange(BaseTester):
+ def test_int64_range_keeps_its_arithmetic(self, device, dtype):
+ """Keep int64 squares, or the existing float32 computation for half images."""
+ image = torch.zeros(3, device=device, dtype=dtype)
+ target = torch.ones_like(image)
+ max_val = torch.tensor([2**24 + 1, 2**30 + 1], device=device, dtype=torch.int64)
+ if dtype in (torch.float16, torch.bfloat16):
+ expected = (10 * max_val.float().square().log10()).to(dtype)
+ else:
+ expected = 10 * (max_val.square() / torch.ones((), device=device, dtype=dtype)).log10()
+ self.assert_close(kornia.metrics.psnr(image, target, max_val), expected, atol=0, rtol=0)
+
+ @pytest.mark.parametrize(
+ "range_dtype,range_value",
+ [(torch.uint8, 255), (torch.int8, 127), (torch.int16, 255), (torch.int32, 65535), (torch.int64, 65535)],
+ )
+ @pytest.mark.parametrize("scalar_range", [True, False])
+ @pytest.mark.parametrize("integer_image", [True, False])
+ def test_tensor_range_does_not_overflow(self, device, dtype, range_dtype, range_value, scalar_range, integer_image):
+ """An MSE of one gives 20 log10(range), including ordinary 8-bit and 16-bit image ranges."""
+ image = torch.zeros(1, 3, 4, 4, device=device, dtype=torch.uint8 if integer_image else dtype)
+ target = torch.ones_like(image)
+ values = range_value if scalar_range else [range_value, 1]
+ max_val = torch.tensor(values, device=device, dtype=range_dtype)
+ expected_dtype = torch.float32 if integer_image else dtype
+ expected = (20 * max_val.cpu().double().log10()).to(device=device, dtype=expected_dtype)
+ actual = kornia.metrics.psnr(image, target, max_val)
+ assert actual.shape == max_val.shape
+ assert actual.dtype == expected_dtype
+ assert torch.isfinite(actual).all()
+ self.assert_close(actual, expected)
+ self.assert_close(kornia.losses.psnr_loss(image, target, max_val), -expected)
+ self.assert_close(kornia.losses.PSNRLoss(max_val)(image, target), -expected)
+
+
class TestPSNRHalfPrecision(BaseTester):
@pytest.mark.parametrize("image_dtype", [torch.float16, torch.bfloat16])
@pytest.mark.parametrize("range_dtype", [torch.float16, torch.bfloat16, torch.float32, torch.float64])