voxelmorph.nn.models
Core VoxelMorph models for unsupervised and supervised learning.
VxmDeformable
VxmDeformable(ndim: int, in_channels: int, out_channels: int, nb_features: List[int] = (16, 16, 16, 16, 16), normalizations: Union[List[Union[Callable, str]], Callable, str, None] = None, activations: Union[List[Union[Callable, str]], Callable, str, None] = nn.ReLU, order: str = 'caca', final_activation: Union[str, Module, None] = None, flow_initializer: Union[float, Sampler] = ne.samplers.Normal(0, 1e-05), bidirectional_cost: bool = False, integration_steps: int = 0, resize_integrated_fields: bool = False, device: str = 'cpu')
Bases: Module
A network archetecture built on BasicUNet to perform nD image registration using a flow
field.
| PARAMETER | DESCRIPTION |
|---|---|
ndim
|
Number of spatial dimensions (e.g., 2 for 2D, 3 for 3D).
TYPE:
|
in_channels
|
Number of input channels in the source and target images.
TYPE:
|
out_channels
|
Number of output channels in the displacement field.
TYPE:
|
*args
|
Additional positional arguments for the
TYPE:
|
nb_features
|
List of integers specifying the number of features in each
level of the UNet architecture. Default is
TYPE:
|
normalizations
|
Normalization layers for the UNet. Can be a list of normalization
types or a single normalization type. Default is
TYPE:
|
activations
|
Activation functions for the UNet layers. Can be a list of
activation functions or a single function. Default is
TYPE:
|
order
|
The order of operations in each UNet block. Default is
TYPE:
|
final_activation
|
The activation applied to the final output of the network. Default is
TYPE:
|
flow_initializer
|
A custom sampler for initializing the weights of the flow layer.
If not provided, it defaults to a normal distribution
with mean 0 and standard deviation
TYPE:
|
integration_steps
|
Number of steps to take in integrating the flow field. Default is 1.
TYPE:
|
**kwargs
|
Additional keyword arguments passed to the
TYPE:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
flow_layer |
A custom convolutional block used to generate the flow field from the combined source and target features.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
forward |
Combines source and target images, processes them through the UNet and the flow layer, and returns the resulting flow field. |
Initialize the VxmDeformable.
| PARAMETER | DESCRIPTION |
|---|---|
ndim
|
Dimensionality of the input (1, 2, or 3).
TYPE:
|
in_channels
|
Number of input channels.
TYPE:
|
out_channels
|
Number of output channels.
TYPE:
|
expected_moving_shape
|
The expected shape of the
TYPE:
|
nb_features
|
Number of features at each level of the unet. Must be a list of positive integers.
TYPE:
|
normalizations
|
Normalization layers to use in each block. Can be a string or a list
of strings specifying normalizations for each layer, or
TYPE:
|
activations
|
Activation functions to use in each block. Can be a callable, a string, or a list of strings/callables.
TYPE:
|
order
|
The order of operations in each convolutional block. Default is 'cna'
(normalization -> convolution -> activation). Each character in the string represents
one of the following:
-
TYPE:
|
bidirectional_cost
|
Enable calculation of the cost-function bidirectionally. Default is False
TYPE:
|
integration_steps
|
Number of scaling and squaring steps for integrating the flow field. Default is 0 (no integration).
TYPE:
|
device
|
Device identifier (e.g., 'cpu' or 'cuda') to place/run the model on.
TYPE:
|
Source code in voxelmorph/nn/models.py
_init_flow_layer
_init_flow_layer(ndim: int, features: int, flow_initializer: Union[float, Sampler] = ne.samplers.Normal(0, 1e-05))
Initialize the flow layer with custom weight initialization (by sampling
flow_initializer).
This layer is a convolutional block that produces a displacement (flow) field. The weights of its initial convolution are sampled using the provided flow_initializer, and biases are set to zero.
| PARAMETER | DESCRIPTION |
|---|---|
ndim
|
Spatial dimensionality of the input (1, 2, or 3).
TYPE:
|
features
|
Number of input and output features for the flow layer.
TYPE:
|
flow_initializer
|
Sampler for initializing the weights of the flow layer. Default is
TYPE:
|
Source code in voxelmorph/nn/models.py
_integrate_velocity_fields
Integrate the velocity fields to obtain diffeomorphic warp (displacement) fields.
Derive a smooth and invertable displacement field by integrating a velocity field via the
scaling and squaring method. If no IntegrateVelocityField object exists in the model's
state dictionary, instantiate it with the correct size of the input and insert it. This will
only happen once upon initial call of this method.
| PARAMETER | DESCRIPTION |
|---|---|
pos_flow
|
Positive flow (velocity) field (source -> target).
TYPE:
|
neg_flow
|
Negative flow (velocity) field (target -> source).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Displacement field obtained by integrating the velocity field via scaling and squaring. |
Source code in voxelmorph/nn/models.py
_spatial_transform
Warp an image tensor using a deformation/displacement field.
This method applies a spatial transformation to the provided image tensor based on the
deformation field using a SpatialTransformer. If no IntegrateVelocityField object exists
in the model's state dictionary, instantiate one with the correct size of the input and
register it as a submodule. This will only happen once upon the initial call of this method.
| PARAMETER | DESCRIPTION |
|---|---|
moving_image
|
Image tensor to be warped, with shape (B, C, ...).
TYPE:
|
deformation_field
|
Displacement field used for warping, with shape matching the spatial dimensions of
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The warped image tensor. |