Repository navigation
Commit c5bddb2
Fix compiled lazy resampling with align_corners=True (#9248)
Related to #9112.
### Description
Fixes lazy resampling with the compiled extension (`BUILD_MONAI=1`) when
`align_corners=True`, following @ericspod's confirmation that the native
`grid_sample` path is the reference.
In `spatial_resample`, the compiled branch builds `Affine(affine=xform,
normalized=True, align_corners=True)` from a matrix that is already in
voxel space. `AffineGrid` and the compiled `Resample` then apply the
`align_corners=True` scalings (`d_out/(d_out-1)` and `(d_in-1)/d_in`) on
top of it, distorting the grid passed to `grid_pull`. For non-integer
`mode`, the matrix is now pre-compensated so those scalings cancel.
Integer `mode` is unchanged.
Verification (`BUILD_MONAI=1` unless noted):
- `test_affine` + `test_affined`: 132 failed -> 0 failed.
- Wider set (spacing, resize, zoom, rotate, rand_* transforms): 10
failed -> 0 failed. Default (non-compiled) run: 0 failed, unchanged.
- Eager `Spacing` vs a plain `F.grid_sample(align_corners=True)`
reference on three shapes: compiled was 0.2 to 0.5 off before, ~2e-7
after (native ~3e-8).
- Reproduces on CPU too, so not GPU-specific.
Test changes:
- Added `test_lazy_rotate_align_corners_compiled` (skipped unless the
extension is compiled). It fails without the fix and passes with it.
- `_TEMPLATE_5_COMPILED` in `test_spacing.py` changed from 0.75 to 1.0.
All sampled input coordinates for that case lie inside the image and the
input is all ones, so 1.0 is the geometrically correct value. The native
template (0.9553) looks like a unit-size-axis artifact and is untouched.
Note: `BUILD_MONAI=1` must be set at runtime, not only at build time,
for these tests to exercise the compiled path. Tested locally on RTX
3050 / WSL2 / PyTorch 2.14.1+cu132 (outside CI's range); not run on
PyTorch 2.8 to 2.11 or through the full `./runtests.sh`.
### 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).
- [x] 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: Vikas Reddy <vikasreddy1090@gmail.com>
Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>1 parent 0e9fc83 commit c5bddb2
3 files changed
Lines changed: 61 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| |||
106 | 107 | | |
107 | 108 | | |
108 | 109 | | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
109 | 145 | | |
110 | 146 | | |
111 | 147 | | |
| |||
216 | 252 | | |
217 | 253 | | |
218 | 254 | | |
| 255 | + | |
| 256 | + | |
219 | 257 | | |
220 | 258 | | |
221 | 259 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
| 22 | + | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| |||
291 | 291 | | |
292 | 292 | | |
293 | 293 | | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
294 | 314 | | |
295 | 315 | | |
296 | 316 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | | - | |
| 30 | + | |
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
| |||
0 commit comments