Skip to content

Commit e18d140

Browse files
hmeixxericspod
andauthored
Fix NLST keypoint warping coordinates (#2073)
Fixes #2021 ### Description This PR fixes the keypoint coordinate normalization used when sampling the displacement field in `learn2reg_nlst_paired_lung_ct.ipynb`. Previously, keypoint voxel coordinates were normalized using the image dimension: ```python offset = torch.as_tensor(fixed_image.shape[-3:]).to(fixed_keypoints.device) / 2 ``` This corresponds to `2 * x / dim - 1`, which does not map voxel indices `[0, dim - 1]` to `[-1, 1]`. The normalization is updated to use `(dim - 1) / 2`, corresponding to: ```text 2 * x / (dim - 1) - 1 ``` `align_corners=True` is also explicitly specified in `F.grid_sample` so that the sampling convention is consistent with this normalization and with MONAI `Warp`. A small local `grid_sample` check confirmed that the updated normalization maps voxel centers correctly. ### Checks <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Avoid including large-size files in the PR. - [x] Clean up long text outputs from code cells in the notebook. - [x] For security purposes, please check the contents and remove any sensitive info such as user names and private key. - [x] Ensure (1) hyperlinks and markdown anchors are working (2) use relative paths for tutorial repo files (3) put figure and graphs in the `./figure` folder - [ ] Notebook runs automatically `./runner.sh -t <path to .ipynb file>` Local PEP 8/format checks passed with runner.sh. Notebook execution was skipped by the existing runner skip pattern. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Corrected keypoint displacement sampling to align with PyTorch’s corner-aligned coordinate system, improving registration accuracy and consistency. <!-- end of auto-generated comment: release notes by coderabbit.ai --> Signed-off-by: mei <hmeixx@gmail.com> Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
1 parent 189543c commit e18d140

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

‎3d_registration/learn2reg_nlst_paired_lung_ct.ipynb‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,11 +660,15 @@
660660
" # warp vectors for keypoints (optional)\n",
661661
" if fixed_keypoints is not None:\n",
662662
" with torch.no_grad():\n",
663-
" offset = torch.as_tensor(fixed_image.shape[-3:]).to(fixed_keypoints.device) / 2\n",
663+
" offset = (torch.as_tensor(fixed_image.shape[-3:]).to(fixed_keypoints.device) - 1) / 2\n",
664664
" offset = offset[None][None]\n",
665665
" ddf_keypoints = torch.flip((fixed_keypoints - offset) / offset, (-1,))\n",
666666
" ddf_keypoints = (\n",
667-
" F.grid_sample(ddf_image, ddf_keypoints.view(batch_size, -1, 1, 1, 3))\n",
667+
" F.grid_sample(\n",
668+
" ddf_image,\n",
669+
" ddf_keypoints.view(batch_size, -1, 1, 1, 3),\n",
670+
" align_corners=True,\n",
671+
" )\n",
668672
" .view(batch_size, 3, -1)\n",
669673
" .permute((0, 2, 1))\n",
670674
" )\n",

0 commit comments

Comments
 (0)