Skip to content

Commit 57b049e

Browse files
Register GlobalMutualInformationLoss bin_centers as buffer
Signed-off-by: Anthonyushie <anthonytwan75official@gmail.com>
1 parent 0a8d945 commit 57b049e

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

monai/losses/image_dissimilarity.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,10 @@ def __init__(
233233
self.kernel_type = look_up_option(kernel_type, ["gaussian", "b-spline"])
234234
self.num_bins = num_bins
235235
self.kernel_type = kernel_type
236+
self.bin_centers: torch.Tensor
236237
if self.kernel_type == "gaussian":
237238
self.preterm = 1 / (2 * sigma**2)
238-
self.bin_centers = bin_centers[None, None, ...]
239+
self.register_buffer("bin_centers", bin_centers[None, None, ...], persistent=False)
239240
self.smooth_nr = float(smooth_nr)
240241
self.smooth_dr = float(smooth_dr)
241242

tests/losses/image_dissimilarity/test_global_mutual_information_loss.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ def transformation(translate_params=(0.0, 0.0, 0.0), rotate_params=(0.0, 0.0, 0.
116116

117117

118118
class TestGlobalMutualInformationLossIll(unittest.TestCase):
119+
def test_gaussian_bin_centers_registered_buffer(self):
120+
loss = GlobalMutualInformationLoss(kernel_type="gaussian", num_bins=16)
121+
122+
self.assertIn("bin_centers", dict(loss.named_buffers()))
123+
self.assertFalse(loss.bin_centers.requires_grad)
124+
125+
loss = loss.to(dtype=torch.float64)
126+
self.assertEqual(loss.bin_centers.dtype, torch.float64)
127+
119128
@parameterized.expand(
120129
[
121130
(torch.ones((1, 2), dtype=torch.float), torch.ones((1, 3), dtype=torch.float)), # mismatched_simple_dims

0 commit comments

Comments
 (0)