tutorials icon indicating copy to clipboard operation
tutorials copied to clipboard

learn2reg_nlst_paired_lung_ct.ipynb keypoints warping

Open coolteemf opened this issue 5 months ago • 2 comments

Hi, I'm working on a new registration method, and decided to use learn2reg_nlst_paired_lung_ct.ipynb as a base. However, the conversion from keypoints from voxel coordinates to warping coordinates in the range [-1, 1] seems incorrect to me. It is done like this (in the forward function):

offset = torch.as_tensor(fixed_image.shape[-3:]).to(fixed_keypoints.device) / 2
offset = offset[None][None]
ddf_keypoints = torch.flip((fixed_keypoints - offset) / offset, (-1,))

But in the Warp class of Monai, the conversion is done like this:

for i, dim in enumerate(grid.shape[1:-1]):
    grid[..., i] = grid[..., i] * 2 / (dim - 1) - 1
index_ordering: list[int] = list(range(spatial_dims - 1, -1, -1))
grid = grid[..., index_ordering]  # z, y, x -> x, y, z

Which seems correct to me, since voxel coordinates should max out at dim - 1 for indexing.

The two are almost equivalent, except that in the notebook, image.shape is used and not image.shape - 1, so I think the notebook is wrong. Feel free to correct me if I'm wrong, because I'm lacking info on what exactly are those keypoints and how they were obtained.

coolteemf avatar Aug 20 '25 09:08 coolteemf

Hi @coolteemf it's possible the notebook is incorrect, could you provide a correcting code snippet? @nvahmadi could you have a look at this issue? Also the images the notebook used in the description are no longer available, would have these elsewhere? Thanks!

ericspod avatar Sep 05 '25 12:09 ericspod

Hi @ericspod correcting code snippet:

offset = (torch.as_tensor(fixed_image.shape[-3:]).to(fixed_keypoints.device) - 1) / 2
offset = offset[None][None]
ddf_keypoints = torch.flip((fixed_keypoints - offset) / offset, (-1,))

I added the -1 to the offset computation, which is now equivalent to the dim - 1 in the Warp function

flecomet avatar Sep 05 '25 12:09 flecomet