Commit af44576
authored
Resolves non-gradient bug of SSIM Loss (#5686)
Fixes #5668 .
### Description
The SSIM loss wasn't feeding into autograd - it wasn't allowing gradient
to be calculated. Now they can be calculated.
As per my previous PR (#5550), the loss function is now calculated using
the SSIMMetric (and not the other way around). The SSIMMetric in
inheriting from class `IterationMetric` that runs `.detach()` on all
inputs before running `._compute_tensor()`.
https://github.com/Project-MONAI/MONAI/blob/0737a33d62ce1e18023712a000828235b7758536/monai/metrics/metric.py#L70
I'm now calling the `SSIMMetric()._compute_tensor()` explicitly,
removing the step where it detaches from the current graph.
**To Reproduce**
```
import torch
from monai.losses.ssim_loss import SSIMLoss
x = torch.ones([1,1,10,10])/2
y = torch.ones([1,1,10,10])/2
y.requires_grad_(True)
data_range = x.max().unsqueeze(0)
loss = SSIMLoss(spatial_dims=2)(x,y,data_range)
print(loss.requires_grad)
```
**Expected behavior**
`loss.requires_grad` will now be True.
### Types of changes
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
Signed-off-by: PedroFerreiradaCosta <pedro.hpf.costa@gmail.com>1 parent 25130db commit af44576
2 files changed
Lines changed: 19 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
84 | | - | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
85 | 87 | | |
86 | 88 | | |
87 | 89 | | |
88 | | - | |
89 | | - | |
90 | | - | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
91 | 93 | | |
92 | 94 | | |
93 | 95 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
37 | 45 | | |
38 | 46 | | |
39 | 47 | | |
| |||
42 | 50 | | |
43 | 51 | | |
44 | 52 | | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
45 | 58 | | |
46 | 59 | | |
47 | 60 | | |
| |||
0 commit comments