From ee1404078d55c0e3600cb73fd4c4d19bc4850680 Mon Sep 17 00:00:00 2001 From: SexyERIC0723 Date: Tue, 14 Apr 2026 12:54:35 +0100 Subject: [PATCH] Fix SSIMLoss docstring examples: remove redundant 1- prefix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #8822. `SSIMLoss.forward()` already returns `1 - ssim_value`, so the docstring examples `print(1-SSIMLoss(...)(x,y))` compute `1 - (1 - ssim) = ssim` — the structural similarity, not the loss. A user following the example would build a "loss" that *increases* as images grow more similar and train in the wrong direction. The three examples in the `forward` docstring are corrected to `print(SSIMLoss(...)(x,y))`, which prints the actual loss value as intended by the function's return-type docstring ("1 minus the ssim index"). ### Types of changes - [x] Documentation update. ### Checks - [x] I've read the [contribution guidelines](https://github.com/Project-MONAI/MONAI/blob/dev/CONTRIBUTING.md). Signed-off-by: SexyERIC0723 Assisted-by: Claude Code --- monai/losses/ssim_loss.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/monai/losses/ssim_loss.py b/monai/losses/ssim_loss.py index 8ee1da7267..3fa578da29 100644 --- a/monai/losses/ssim_loss.py +++ b/monai/losses/ssim_loss.py @@ -111,17 +111,17 @@ def forward(self, input: torch.Tensor, target: torch.Tensor) -> torch.Tensor: # 2D data x = torch.ones([1,1,10,10])/2 y = torch.ones([1,1,10,10])/2 - print(1-SSIMLoss(spatial_dims=2)(x,y)) + print(SSIMLoss(spatial_dims=2)(x,y)) # pseudo-3D data x = torch.ones([1,5,10,10])/2 # 5 could represent number of slices y = torch.ones([1,5,10,10])/2 - print(1-SSIMLoss(spatial_dims=2)(x,y)) + print(SSIMLoss(spatial_dims=2)(x,y)) # 3D data x = torch.ones([1,1,10,10,10])/2 y = torch.ones([1,1,10,10,10])/2 - print(1-SSIMLoss(spatial_dims=3)(x,y)) + print(SSIMLoss(spatial_dims=3)(x,y)) """ ssim_value = self.ssim_metric._compute_tensor(input, target).view(-1, 1) loss: torch.Tensor = 1 - ssim_value