|
12 | 12 |
|
13 | 13 | import unittest |
14 | 14 | from pathlib import Path |
| 15 | +from unittest import mock |
15 | 16 |
|
16 | 17 | import numpy as np |
17 | 18 | import torch |
@@ -138,18 +139,26 @@ def test_ill_shape(self): |
138 | 139 | with self.assertRaisesRegex(ValueError, ""): |
139 | 140 | warp_layer(image=torch.arange(4).reshape((1, 1, 2, 2)).to(dtype=torch.float), ddf=torch.zeros(1, 2, 3, 3)) |
140 | 141 |
|
| 142 | + @mock.patch("monai.networks.blocks.warp.USE_COMPILED", False) |
141 | 143 | def test_singleton_spatial_dim(self): |
142 | | - # a single-slice volume / single-row image has a spatial dim of size 1; the grid |
143 | | - # normalization must not divide by zero (regression for native grid_sample path). |
144 | | - # "zeros" padding maps an out-of-range (here NaN, pre-fix) coordinate to 0, so it exposes |
145 | | - # the bug; "border"/"reflection" would clamp onto the lone voxel and mask it. |
| 144 | + """ |
| 145 | + Regression test for a singleton spatial dimension (a single-slice volume or a |
| 146 | + single-row/column image), where the grid normalization ``* 2 / (dim - 1)`` previously |
| 147 | + divided by zero. |
| 148 | +
|
| 149 | + The native ``grid_sample`` path is forced via ``USE_COMPILED=False`` because only that |
| 150 | + branch normalizes the grid; the csrc ``grid_pull`` path is unaffected. ``padding_mode`` |
| 151 | + is ``"zeros"`` so an out-of-range (pre-fix ``nan``) coordinate maps to 0 and exposes the |
| 152 | + bug, whereas ``"border"``/``"reflection"`` would clamp onto the lone voxel and mask it. |
| 153 | + For a zero displacement field the warped output must contain no ``nan`` and must equal |
| 154 | + the input image. |
| 155 | + """ |
146 | 156 | for shape, ndim in [((1, 1, 1, 4, 4), 3), ((1, 1, 1, 5), 2), ((1, 1, 5, 1), 2)]: |
147 | 157 | image = torch.rand(*shape) |
148 | 158 | ddf = torch.zeros(shape[0], ndim, *shape[2:]) |
149 | 159 | warp_layer = Warp(mode="bilinear", padding_mode="zeros") |
150 | 160 | result = warp_layer(image, ddf) |
151 | 161 | self.assertFalse(torch.isnan(result).any(), f"NaN in warp output for shape {shape}") |
152 | | - # a zero displacement field must reproduce the input image |
153 | 162 | np.testing.assert_allclose(result.cpu().numpy(), image.cpu().numpy(), rtol=1e-4, atol=1e-4) |
154 | 163 |
|
155 | 164 | def test_grad(self): |
|
0 commit comments