π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(losses): keep MS_SSIMLoss's Gaussian window odd and centred (#5143)
εΊζ’η8f80e39d2a10
diff --git a/changelog.d/5143.breaking.md b/changelog.d/5143.breaking.mdnew file mode 100644index 000000000..b8b518da5--- /dev/null+++ b/changelog.d/5143.breaking.md@@ -0,0 +1,6 @@+`MS_SSIMLoss` now builds an odd, centred Gaussian window of `2 * int(2 * sigmas[-1]) + 1` pixels (#5126). Previously+the window was `int(4 * sigmas[-1] + 1)` wide, which is even for a largest sigma such as 1.3 (6 pixels) or 3.3 (14):+the window sat half a pixel off centre and the `reduction="none"` map came out `(N, H - 1, W - 1)` instead of the+documented `(N, H, W)`. Those sigmas now get a window one pixel narrower (5 and 13), the map keeps the input shape and+the loss changes by about 2% on random images. Every integer or half-integer sigma, the defaults included, keeps its+window size, and its outputs are bitwise unchanged.diff --git a/kornia/losses/ms_ssim.py b/kornia/losses/ms_ssim.pyindex 233305d8b..7fa7ad37f 100644--- a/kornia/losses/ms_ssim.py+++ b/kornia/losses/ms_ssim.py@@ -98,8 +98,9 @@ class MS_SSIMLoss(nn.Module):self.num_scales: int = len(sigmas)- # One mask per scale; forward repeats them for every channel of the input.- filter_size = int(4 * sigmas[-1] + 1)+ # One mask per scale; forward repeats them for every channel of the input. The window is odd, 2 * pad + 1+ # wide, so it is centred and the convolutions padded by ``pad`` keep the input's height and width.+ filter_size = 2 * self.pad + 1g_masks = torch.stack([self._fspecial_gauss_2d(filter_size, sigma) for sigma in sigmas]).unsqueeze(1)# The masks are derived from ``sigmas``, so they are not persisted; see ``_load_from_state_dict``.diff --git a/tests/losses/test_ssim.py b/tests/losses/test_ssim.pyindex 90313c1bb..00bb44ec0 100644--- a/tests/losses/test_ssim.py+++ b/tests/losses/test_ssim.py@@ -255,6 +255,19 @@ class TestMS_SSIMLoss(BaseTester):self.assert_close(model.criterion(img1, img2), expected)+ @pytest.mark.parametrize("sigmas", [(0.5, 1.3), (1.0, 3.3), (0.5, 1.0, 2.0)])+ def test_none_reduction_keeps_input_shape_5126(self, device, dtype, sigmas):+ # int(4 * sigma + 1) is even for sigma 1.3 (6) and 3.3 (14): the window sat half a pixel off centre and the+ # reduction="none" map came out (N, H - 1, W - 1) instead of (N, H, W). With an odd window the map keeps the+ # input shape and is mirror-symmetric: flipping both inputs flips the map, with no one-pixel shift.+ img1 = torch.rand(1, 3, 16, 20, device=device, dtype=dtype)+ img2 = torch.rand(1, 3, 16, 20, device=device, dtype=dtype)+ loss = kornia.losses.MS_SSIMLoss(sigmas=sigmas, reduction="none").to(device, dtype)+ assert loss._g_masks.shape[-1] % 2 == 1+ out = loss(img1, img2)+ assert out.shape == (1, 16, 20)+ self.assert_close(out, loss(img1.flip(-1), img2.flip(-1)).flip(-1))+def test_gradcheck(self, device, dtype):# input datadtype = torch.float64