voxelmorph.nn.modules
Neural network building blocks for VoxelMorph.
IntegrateVelocityField
IntegrateVelocityField(shape: tuple, steps: int = 1, interpolation_mode: str = 'bilinear', align_corners: bool = False, device: str = 'cpu')
Bases: Module
Integrates a velocity field over multiple steps using the scaling and squaring method.
This module ensures that transformations caused by a velocity field is diffeomorphic by compounding small, intermediate transformations (by recursive scaling and squaring). This ensures the resultant is both smooth and invertable.
| ATTRIBUTE | DESCRIPTION |
|---|---|
steps |
The number of squaring steps used for integration.
TYPE:
|
scale |
Scaling factor for the initial velocity field, determined as
TYPE:
|
transformer |
A spatial transformer module used to iteratively warp the vector field.
TYPE:
|
Examples:
Integrate a 2D velocity field over multiple steps:
>>> shape = (128, 128) # 2D spatial grid
>>> integrator = IntegrateVelocityField(shape, steps=256)
>>> velocity_field = torch.randn(1, 2, 128, 128) # (B, C, H, W)
>>> disp = integrator(velocity_field)
>>> disp.shape
torch.Size([1, 2, 128, 128])
Perform integration on a 3D velocity field with a single scaling step:
>>> shape = (64, 64, 64) # 3D spatial grid
>>> integrator = IntegrateVelocityField(shape, steps=1)
>>> velocity_field = torch.randn(1, 3, 64, 64, 64) # (B, C, D, H, W)
>>> disp = integrator(velocity_field)
>>> disp.shape
torch.Size([1, 3, 64, 64, 64])
Initialize IntegrateVelocityField
| PARAMETER | DESCRIPTION |
|---|---|
shape
|
Shape of the input velocity field (excluding batch and channel dimensions).
TYPE:
|
steps
|
Number of integration steps. A higher value leads to a more smooth and accurate integration at the cost of higher/longer computation. Default is 1.
TYPE:
|
interpolation_mode
|
Algorithm used for interpolating the warped image. Default is 'bilinear'. Options are: 'bilinear' | 'nearest' | 'bicubic'.
TYPE:
|
align_corners
|
Map the corner points of the moving image to the corner points of the warped image.
TYPE:
|
device
|
Device to construct and hold the identity grid.
TYPE:
|
Source code in voxelmorph/nn/modules.py
ResizeDisplacementField
ResizeDisplacementField(scale_factor: Optional[Union[float, int, Sampler]] = 1.0, interpolation_mode: str = 'bilinear', align_corners: bool = True)
Bases: Module
Resize and rescale a displacement field.
Resizd a displacement field both spatially (via interpolation) and in magnitude (via scaling).
Examples:
Resize a 2D displacement field
>>> resize_field = ResizeDisplacementField(scale_factor=2.0, interpolation_mode="bilinear")
>>> disp = torch.rand(1, 2, 16, 16) # Example displacement field in 2d
>>> resized_disp = resize_field(disp)
>>> print(resized_disp.shape) # Should be larger if scale_factor > 1
torch.Size([1, 2, 32, 32])
Instantiate the ResizeDisplacementField module.
| PARAMETER | DESCRIPTION |
|---|---|
scale_factor
|
Factor by which to stretch or shrink the spatial dimensions of the displacement field.
Values of
TYPE:
|
interpolation_mode
|
Algorithm used for interpolating the warped image. Default is 'bilinear'. Options are: 'bilinear' | 'nearest' | 'bicubic', 'trilinear'.
TYPE:
|
align_corners
|
Map the corner points of the moving image to the corner points of the warped image.
TYPE:
|
Source code in voxelmorph/nn/modules.py
SpatialTransformer
SpatialTransformer(size: Tuple[int], interpolation_mode: str = 'bilinear', align_corners: bool = False, device: Union[str, device] = 'cpu')
Bases: Module
N-D Spatial transformation according to a deformation field.
Uses a deformation field to transform the moving image.
References
If you find this helpful, please cite the following paper:
VoxelMorph: A Learning Framework for Deformable Medical Image Registration G. Balakrishnan, A. Zhao, M. R. Sabuncu, J. Guttag, A.V. Dalca. IEEE TMI: Transactions on Medical Imaging. 38(8). pp 1788-1800. 2019.
Initialize SpatialTransformer.
| PARAMETER | DESCRIPTION |
|---|---|
size
|
Expected size of
TYPE:
|
interpolation_mode
|
Algorithm used for interpolating the warped image. Default is 'bilinear'. Options are: 'bilinear' | 'nearest' | 'bicubic'.
TYPE:
|
align_corners
|
Map the corner points of the moving image to the corner points of the warped image.
TYPE:
|
device
|
Device to construct and hold the identity grid.
TYPE:
|
Source code in voxelmorph/nn/modules.py
_normalize_warped_grid
Normalize a warped grid to make PyTorch grid_sample() happy!
PyTorch's grid_sample() requires coordinates in the range [-1, 1].
This function scales and shifts the warped grid accordingly.
| PARAMETER | DESCRIPTION |
|---|---|
warped_grid
|
The resultant of the identity grid and the deformation field.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The warped grid rescaled to the range [-1, 1] for each spatial axis |