π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(metrics): filter a float32/float64 pair in float64 in ssim, with the window built in that dtype (#5578)
εΊζ’η08c5ad956479
diff --git a/changelog.d/5578.breaking.md b/changelog.d/5578.breaking.mdnew file mode 100644index 000000000..fe5ee9daf--- /dev/null+++ b/changelog.d/5578.breaking.md@@ -0,0 +1,4 @@+`ssim` and `SSIM` now raise torch's error for building the Gaussian window in a complex dtype when one image is real+and the other complex, in either order (#5574): a `NotImplementedError`, or a plain `RuntimeError` on torch 2.5.1.+Previously a real `img1` with a complex `img2` returned a complex map, while a complex `img1` or two complex images+raised that error. `ssim_loss` and `SSIMLoss` already raised for such pairs.diff --git a/changelog.d/5578.fixed.md b/changelog.d/5578.fixed.mdnew file mode 100644index 000000000..c923fcd25--- /dev/null+++ b/changelog.d/5578.fixed.md@@ -0,0 +1,6 @@+`ssim` now computes a pair of a float64 image and a float32, half-precision or integer image in float64, with the+Gaussian window built in float64 (#5574). Previously the window was built in the dtype of `img1` and each image was+filtered in its own dtype, so the float64 result depended on the argument order: on the low-contrast pair of #5574 it+missed the all-float64 map by 1.1e-4 in one order and 5.7e-5 in the other, and swapping the arguments changed it by+1.3e-4. Both orders now equal the all-float64 map. Pairs without a float64 image, same-dtype pairs included, are+unchanged, and `SSIM`, `ssim_loss` and `SSIMLoss` inherit the change.diff --git a/kornia/metrics/ssim.py b/kornia/metrics/ssim.pyindex 83126eb3c..96ecff5cc 100644--- a/kornia/metrics/ssim.py+++ b/kornia/metrics/ssim.py@@ -72,6 +72,7 @@ def ssim(Note:Integer images are converted to float32 before computing the local moments.Half-precision inputs are evaluated in float32 for numerical stability.+ A float32/float64 pair is filtered in float64, with the Gaussian window built in that dtype.Filtering runs with autocast disabled; the result uses the promoted input dtype.Examples:@@ -117,9 +118,12 @@ def ssim(img1 = img1.float()if img2.dtype in (torch.float16, torch.bfloat16):img2 = img2.float()+ # Mixed inputs are filtered in their common dtype, which is also the dtype of the window.+ compute_dtype = torch.promote_types(img1.dtype, img2.dtype)+ img1, img2 = img1.to(compute_dtype), img2.to(compute_dtype)# prepare kernel- kernel: torch.Tensor = get_gaussian_kernel1d(window_size, 1.5, device=img1.device, dtype=img1.dtype)+ kernel: torch.Tensor = get_gaussian_kernel1d(window_size, 1.5, device=img1.device, dtype=compute_dtype)# compute coefficientsC1: float = (0.01 * max_val) ** 2diff --git a/tests/metrics/test_ssim_metric.py b/tests/metrics/test_ssim_metric.pyindex d2ffd41cb..e94a44777 100644--- a/tests/metrics/test_ssim_metric.py+++ b/tests/metrics/test_ssim_metric.py@@ -70,6 +70,18 @@ class TestSsim(BaseTester):expected = torch.full_like(actual, (2 * 128 * 64 + c1) / (128**2 + 64**2 + c1))self.assert_close(actual, expected)+ def test_mixed_float32_float64_pair_is_computed_in_float64(self, device, dtype):+ # A float32/float64 pair is filtered in float64, the result dtype, with a float64 window, whichever argument+ # is the float32 one, so it equals the all-float64 result to roundoff (#5574).+ if dtype != torch.float64:+ pytest.skip("the pair needs a float64 argument")+ generator = torch.Generator().manual_seed(0)+ img1 = (0.9 + 0.1 * torch.rand(1, 1, 16, 16, generator=generator, dtype=torch.float64)).float().to(device)+ img2 = (0.9 + 0.1 * torch.rand(1, 1, 16, 16, generator=generator, dtype=torch.float64)).to(device)+ expected = ssim(img1.double(), img2, 5)+ self.assert_close(ssim(img1, img2, 5), expected, rtol=1e-12, atol=1e-12)+ self.assert_close(ssim(img2, img1, 5), expected, rtol=1e-12, atol=1e-12)+def test_pixel_range_gradients(self, device, dtype):if device.type == "mps":pytest.skip("Float64 reference is unsupported on MPS")@@ -181,6 +193,18 @@ class TestSsim(BaseTester):with pytest.raises(ValueError, match="img1 and img2 shapes must be the same"):ssim(img1, img2, window_size=3)+ def test_exception_real_complex_pair(self, device):+ # A real/complex pair builds its Gaussian window in the complex promoted dtype, which raises in either order;+ # a real img1 with a complex img2 used to return a complex map (#5574). torch raises NotImplementedError, a+ # RuntimeError subclass, and a plain RuntimeError on 2.5.1.+ real = torch.rand(1, 1, 8, 8, device=device)+ complex_ = real.to(torch.complex64)+ for img1, img2 in ((real, complex_), (complex_, real)):+ with pytest.raises(RuntimeError, match="not implemented for 'ComplexFloat'"):+ ssim(img1, img2, window_size=3)+ with pytest.raises(RuntimeError, match="not implemented for 'ComplexFloat'"):+ SSIM(window_size=3)(img1, img2)+def test_ssim_module(self, device, dtype):img1 = torch.rand(2, 3, 16, 16, device=device, dtype=dtype)img2 = torch.rand(2, 3, 16, 16, device=device, dtype=dtype)