Skip to content

Commit 6cd452e

Browse files
ytl0623coderabbitai[bot]ericspod
authored
Fix incorrect truncated parameter in make_gaussian_kernel causing corrupted LocalNormalizedCrossCorrelationLoss (#8783)
Fixes #8780 ### Description Divide the pixel radius by `sigma` to convert it into the correct sigma-unit ratio. ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. --------- Signed-off-by: ytl0623 <david89062388@gmail.com> Signed-off-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
1 parent 07990cc commit 6cd452e

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

tests/integration/test_reg_loss_integration.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
[LocalNormalizedCrossCorrelationLoss, {"kernel_size": 7, "kernel_type": "rectangular"}, ["pred", "target"]],
2727
[LocalNormalizedCrossCorrelationLoss, {"kernel_size": 5, "kernel_type": "triangular"}, ["pred", "target"]],
2828
[LocalNormalizedCrossCorrelationLoss, {"kernel_size": 3, "kernel_type": "gaussian"}, ["pred", "target"]],
29+
[LocalNormalizedCrossCorrelationLoss, {"kernel_size": 7, "kernel_type": "gaussian"}, ["pred", "target"]],
2930
[GlobalMutualInformationLoss, {"num_bins": 10}, ["pred", "target"]],
3031
[GlobalMutualInformationLoss, {"kernel_type": "b-spline", "num_bins": 10}, ["pred", "target"]],
3132
]
@@ -98,6 +99,24 @@ def forward(self, x):
9899
optimizer.step()
99100
self.assertGreater(init_loss, loss_val, "loss did not decrease")
100101

102+
def test_lncc_gaussian_kernel_gt3_identical_images(self):
103+
"""
104+
Regression test for make_gaussian_kernel truncated parameter bug.
105+
LNCC on identical inputs must be close to -1.0 for gaussian kernel_size > 3.
106+
"""
107+
for kernel_size in [5, 7]:
108+
with self.subTest(kernel_size=kernel_size):
109+
loss_fn = LocalNormalizedCrossCorrelationLoss(
110+
spatial_dims=2, kernel_size=kernel_size, kernel_type="gaussian"
111+
).to(self.device)
112+
x = torch.rand(2, 1, 32, 32, device=self.device)
113+
y = x.clone()
114+
loss = loss_fn(x, y)
115+
self.assertTrue(
116+
torch.allclose(loss, torch.tensor(-1.0, device=self.device, dtype=loss.dtype), atol=1e-3),
117+
f"LNCC of identical images should be -1.0, got {loss.item():.6f} (kernel_size={kernel_size})",
118+
)
119+
101120

102121
if __name__ == "__main__":
103122
unittest.main()

0 commit comments

Comments
 (0)