Skip to content

Commit faaa212

Browse files
Kheil-Zericspod
andauthored
docs: add differentiability warning for Warp with mode='nearest' (#8629)
Fixes #8540 . ### Description This PR updates the docstring of monai.networks.blocks.Warp to clarify that mode='nearest' results in zero gradients almost everywhere, effectively blocking gradient flow in registration training. ### Changes: Added a note to the Warp docstring about the gradient behavior of mode='nearest'. Added a reference to the semi-supervised registration tutorial: https://github.com/Project-MONAI/tutorials/blob/main/3d_registration/learn2reg_oasis_unpaired_brain_mr.ipynb ### Next Steps (future PR): Extend VoxelMorph.forward to handle optional label and keypoint arguments for consistent warping behavior. ### 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`. - [x] In-line docstrings updated. - [x] Documentation updated, tested `make html` command in the `docs/` folder. --------- Signed-off-by: Kheil-Z <ziadishappy@gmail.com> Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
1 parent 61849bf commit faaa212

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

monai/networks/blocks/warp.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ def __init__(self, mode=GridSampleMode.BILINEAR.value, padding_mode=GridSamplePa
5252
Define reference grid on non-integer values
5353
Reference: B. Likar and F. Pernus. A heirarchical approach to elastic registration
5454
based on mutual information. Image and Vision Computing, 19:33-44, 2001.
55+
56+
Note that using ``mode="nearest"`` makes the warping operation effectively non-differentiable:
57+
gradients are zero almost everywhere, which can block gradient flow during training.
58+
For learning-based registration, use ``"bilinear"`` (2D) or ``"trilinear"`` (3D) interpolation instead.
59+
60+
See https://github.com/Project-MONAI/tutorials/blob/main/3d_registration/learn2reg_oasis_unpaired_brain_mr.ipynb
61+
for examples of semi-supervised registration using segmentations.
5562
"""
5663
super().__init__()
5764
# resolves _interp_mode for different methods

0 commit comments

Comments
 (0)