π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(losses): compute the documented DSSIM clamp((1 - SSIM) / 2, 0, 1) in ssim3d_loss (#5567)
εΊζ’η18b4d0b4cfb3
diff --git a/changelog.d/5567.breaking.md b/changelog.d/5567.breaking.mdnew file mode 100644index 000000000..a42c06cd7--- /dev/null+++ b/changelog.d/5567.breaking.md@@ -0,0 +1,4 @@+`ssim3d_loss` and `SSIM3DLoss` now compute the documented structural dissimilarity `clamp((1 - SSIM) / 2, 0, 1)`,+the form `ssim_loss` uses (#5533). Previously they returned the unclamped `1 - SSIM`, twice the 2-D loss on matched+content and above 1 on anti-correlated content. A caller who wants the old value can reduce+`1 - kornia.metrics.ssim3d(...)`.diff --git a/kornia/losses/ssim3d.py b/kornia/losses/ssim3d.pyindex 90db71e26..39310324d 100644--- a/kornia/losses/ssim3d.py+++ b/kornia/losses/ssim3d.py@@ -68,7 +68,7 @@ def ssim3d_loss(ssim_map: torch.Tensor = metrics.ssim3d(img1, img2, window_size, max_val, eps, padding)# compute and reduce the loss- loss = 1.0 - ssim_map+ loss = torch.clamp((1.0 - ssim_map) / 2, min=0, max=1)if reduction == "mean":loss = loss.mean()diff --git a/tests/losses/test_ssim.py b/tests/losses/test_ssim.pyindex 5ff2f6e92..74190e99e 100644--- a/tests/losses/test_ssim.py+++ b/tests/losses/test_ssim.py@@ -437,15 +437,45 @@ class TestSSIM3DLoss(BaseTester):actual = kornia.losses.ssim3d_loss(img1, img2, window_size, reduction=reduction_type)+ # DSSIM = (1 - SSIM) / 2 of ones against zeros, the value ssim_loss gives in 2-D (#5533).if reduction_type == "mean":- expected = torch.tensor(0.9999, device=device, dtype=dtype)+ expected = torch.tensor(0.49995, device=device, dtype=dtype)elif reduction_type == "sum":- expected = (torch.ones_like(img1, device=device, dtype=dtype) * 0.9999).sum()+ expected = (torch.ones_like(img1, device=device, dtype=dtype) * 0.49995).sum()elif reduction_type == "none":- expected = torch.ones_like(img1, device=device, dtype=dtype) * 0.9999+ expected = torch.ones_like(img1, device=device, dtype=dtype) * 0.49995self.assert_close(actual, expected)+ def test_documented_dssim_form(self, device, dtype):+ # The loss is clamp((1 - SSIM) / 2, 0, 1): anti-correlated volumes gave 1.97, outside [0, 1] (#5533).+ generator = torch.Generator().manual_seed(0)+ img1 = torch.rand(2, 1, 7, 9, 11, generator=generator).to(device=device, dtype=dtype)+ img2 = 1.0 - img1+ ssim_map = kornia.metrics.ssim3d(img1, img2, 5)+ expected = ((1.0 - ssim_map) / 2).clamp(0, 1)+ actual = kornia.losses.ssim3d_loss(img1, img2, 5, reduction="none")+ self.assert_close(actual, expected, rtol=0, atol=0)+ assert actual.min() >= 0 and actual.max() <= 1+ self.assert_close(kornia.losses.SSIM3DLoss(5)(img1, img2), expected.mean())++ def test_clamps_roundoff_into_the_unit_range(self, device, dtype):+ # The clamp is inert in exact arithmetic, where the SSIM map lies in [-1, 1]. Float32 volumes with a large+ # constant offset make the moments cancel and push the map far outside [-1, 1] on both sides; the loss stays+ # in [0, 1], as ssim_loss does (#5533).+ if dtype != torch.float32:+ pytest.skip("the clamp does not depend on the dtype; the offset fixture needs float32 roundoff")+ generator = torch.Generator().manual_seed(0)+ img1 = (1000.0 + 1e-2 * torch.rand(2, 2, 7, 23, 31, generator=generator, dtype=torch.float64)).to(device, dtype)+ img2 = (1000.0 + 1e-2 * torch.rand(2, 2, 7, 23, 31, generator=generator, dtype=torch.float64)).to(device, dtype)+ unclamped = (1.0 - kornia.metrics.ssim3d(img1, img2, 5)) / 2+ assert (unclamped < 0).any()+ assert (unclamped > 1).any()+ loss = kornia.losses.ssim3d_loss(img1, img2, 5, reduction="none")+ self.assert_close(loss, unclamped.clamp(0, 1))+ assert loss.min() >= 0+ assert loss.max() <= 1+def test_module(self, device, dtype):img1 = torch.rand(1, 2, 3, 4, 5, device=device, dtype=dtype)img2 = torch.rand(1, 2, 3, 4, 5, device=device, dtype=dtype)