voxelmorph.nn.functional
Functions containing the core operations and logic of for image registration for voxelmorph
written in PyTorch.
affine_to_disp
affine_to_disp(affine: Tensor, meshgrid: Tensor, rotate_around_center: Optional[bool] = True) -> Tensor
Convert an affine transformation matrix to a displacement field.
| PARAMETER | DESCRIPTION |
|---|---|
affine
|
Affine transformation matrix. It is expected to be a vox2vox target to source transformation.
TYPE:
|
meshgrid
|
The meshgrid tensor of shape
TYPE:
|
rotate_around_center
|
If True, the rotation will be around the center of the image, otherwise around the origin.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The generated displacement field of shape |
Source code in voxelmorph/nn/functional.py
angles_to_rotation_matrix
Compute a rotation matrix from the given rotation angles.
| PARAMETER | DESCRIPTION |
|---|---|
rotation
|
A tensor containing the rotation angles. If
TYPE:
|
degrees
|
Whether to interpret the rotation angles as degrees.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The computed |
Source code in voxelmorph/nn/functional.py
chance
Returns True with given probability.
| PARAMETER | DESCRIPTION |
|---|---|
prob
|
Probability of returning True. Must be in the range [0, 1].
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True with probability |
Source code in voxelmorph/nn/functional.py
compose_affine
compose_affine(ndim: int, translation: Tensor = None, rotation: Tensor = None, scale: Tensor = None, shear: Tensor = None, degrees: bool = True, device: device = None) -> Tensor
Composes an affine matrix from a set of translation, rotation, scale, and shear transform components.
| PARAMETER | DESCRIPTION |
|---|---|
ndim
|
The number of dimensions of the affine matrix. Must be 2 or 3.
TYPE:
|
translation
|
The translation vector. Must be a vector of size
TYPE:
|
rotation
|
The rotation angles. Must be a scalar value for 2D affine matrices, and a tensor of size 3 for 3D affine matrices.
TYPE:
|
scale
|
The scaling factor. Can be scalar or vector of size
TYPE:
|
shear
|
The shearing factor. Must be a scalar value for 2D affine matrices, and a tensor of size 3 for 3D affine matrices.
TYPE:
|
degrees
|
Whether to interpret the rotation angles as degrees.
TYPE:
|
device
|
The device of the returned matrix.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The composed affine matrix, as a tensor of shape |
Source code in voxelmorph/nn/functional.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | |
coords_to_disp
TODOC
Source code in voxelmorph/nn/functional.py
disp_to_coords
Convert the displacement crs to absolute crs scaled to range [-1, 1].
Parameters:
disp: torch.Tensor Displacement crs field meshgrid: torch.Tensor, optional crs grid for the image shape
Returns:
torch.Tensor: The absolute crs field scaled to range [-1, 1].
Source code in voxelmorph/nn/functional.py
gaussian_blur
gaussian_blur(image: Tensor, sigma: List[float], batched: bool = False, truncate: int = 3) -> Tensor
Apply Gaussian blurring to an image.
| PARAMETER | DESCRIPTION |
|---|---|
image
|
An input tensor of shape
TYPE:
|
sigma
|
Standard deviation(s) of the Gaussian filter along each dimension.
TYPE:
|
batched
|
Whether the input tensor includes a batch dimension.
TYPE:
|
truncate
|
The number of standard deviations to extend the kernel before truncating.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The blurred tensor with the same shape as the input tensor. |
Notes
The Gaussian filter is applied using convolution. The size of the filter kernel is determined by the standard deviation and the truncation factor.
Source code in voxelmorph/nn/functional.py
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 | |
gaussian_kernel_1d
Generate a 1D Gaussian kernel with the specified standard deviations.
| PARAMETER | DESCRIPTION |
|---|---|
sigma
|
A list of standard deviations for each dimension.
TYPE:
|
truncate
|
The number of standard deviations to extend the kernel before truncating.
TYPE:
|
device
|
The device on which to create the kernel.
TYPE:
|
dtype
|
Data type of the returned kernel.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
A kernel of shape |
Notes
The kernel is truncated when its values drop below 1e-5 of the maximum value.
Source code in voxelmorph/nn/functional.py
grid_coordinates
grid_coordinates(shape: Sequence[int], indexing: Optional[Literal['ij', 'xy']] = 'ij', dtype: Optional[dtype] = torch.float32, device: Optional[device] = None) -> Tensor
Generates a grid of coordinates with the specified spatial shape.
| PARAMETER | DESCRIPTION |
|---|---|
shape
|
The spatial shape of the grid to generate.
TYPE:
|
indexing
|
The indexing convention to use. 'ij' for matrix indexing, 'xy' for Cartesian indexing. Default is 'ij'.
TYPE:
|
dtype
|
The desired data type of the output tensor. Default is torch.float32.
TYPE:
|
device
|
The device on which to create the tensor. Default is None, which uses the current device.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
A tensor of shape (*shape, len(shape)) containing the grid coordinates. |
Examples:
>>> grid_coordinates((2, 3))
tensor([[[0., 0.],
[0., 1.],
[0., 2.]],
[[1., 0.],
[1., 1.],
[1., 2.]]])
>>> grid_coordinates((1, 2, 2), device=torch.device('cuda:0'))
tensor([[[[0., 0., 0.],
[0., 0., 1.]],
[[0., 1., 0.],
[0., 1., 1.]]]], device='cuda:0')
Source code in voxelmorph/nn/functional.py
integrate_disp
TODOC
Source code in voxelmorph/nn/functional.py
perlin
perlin(shape, smoothing: Union[float, List[float]] = None, magnitude: Union[float, List[float]] = 1.0, weights=None, device=None, method='blur')
Generates a perlin noise image.
| PARAMETER | DESCRIPTION |
|---|---|
shape
|
The desired shape of the output tensor. Can be 2D or 3D.
TYPE:
|
smoothing
|
The spatial smoothing sigma(s) in voxel coordinates.
TYPE:
|
magnitude
|
The standard deviation of the noise.
TYPE:
|
weights
|
The weights of the smoothing components (scales). If None, defaults to monotonically increasing weights.
TYPE:
|
device
|
The device on which the output tensor is allocated. If None, defaults to CPU.
TYPE:
|
method
|
Method for noise generation. Upsampling is much faster and more memory efficient for larger sigma values, but at the cost of quality.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
A Perlin noise image of shape |
Source code in voxelmorph/nn/functional.py
random_affine
random_affine(ndim: int, max_translation: float = 0, max_rotation: float = 0, max_scaling: float = 1, device: device = None, sampling: bool = True) -> Tensor
| PARAMETER | DESCRIPTION |
|---|---|
ndim
|
Dimensionality of target transform.
TYPE:
|
max_translation
|
Range to sample translation parameters from. Scalar values define the max deviation from 0.0 (-max_translation, max_translation).
TYPE:
|
max_rotation
|
Range to sample rotation parameters from. Scalar values define the max deviation from 0.0 (-max_rotation, max_rotation).
TYPE:
|
max_scaling
|
Max to sample scale parameters from. It is converted into a 2-element array defines the (min, max) deviation from 1.0.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
vox2vox affine matrix rotating around the image center |
Source code in voxelmorph/nn/functional.py
random_disp
random_disp(shape: List[int], smoothing: Union[float, List[float]] = 10, magnitude: Union[float, List[float]] = 10, integrations: int = 0, voxsize: float = 1, meshgrid: Tensor = None, device: device = None, perlin_method: str = 'upsample') -> Tensor
TODOC
Source code in voxelmorph/nn/functional.py
random_transform
random_transform(shape: List[int], affine_probability: float = 1.0, max_translation: float = 5.0, max_rotation: float = 5.0, max_scaling: float = 1.1, warp_probability: float = 1.0, warp_integrations: int = 5, warp_smoothing_range: List[int] = [10, 20], warp_magnitude_range: List[int] = [1, 2], voxsize: int = 1, device: device = None, isdisp: bool = True, perlin_method: str = 'upsample', sampling: bool = True) -> Tensor
generate a randomly sampled transform
Parameters:
disp: torch.Tensor Displacement crs field meshgrid: torch.Tensor, optional crs grid for the image shape
Returns:
torch.Tensor: displacement crs field, or absolute crs field scaled to range [-1, 1] if isdisp is False
Source code in voxelmorph/nn/functional.py
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 | |
resize
resize(image: Tensor, scale_factor: List[float] = None, shape: List[int] = None, nearest: bool = False) -> Tensor
Resize an image with the option of scaling and/or setting to a new shape.
Parameters:
image: torch.Tensor An input tensor with shape (C, H, W[, D]) to resize. scale_factor: float or List[float], optional Multiplicative factor(s) for scaling the input tensor. If a float, then the same scale factor is applied to all spatial dimensions. If a tuple, then the scaling factor for each dimension should be provided. shape: List[int], optional Target shape of the output tensor. nearest: bool, optional If True, use nearest neighbor interpolation. Otherwise, use linear interpolation.
Returns:
torch.Tensor:
The resized tensor with the shape specified by shape or scaled by scale_factor.
Source code in voxelmorph/nn/functional.py
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 | |
smooth_gaussian
Generates a smooth Gaussian noise image.
| PARAMETER | DESCRIPTION |
|---|---|
shape
|
The desired shape of the output tensor. Can be 2D or 3D.
TYPE:
|
sigma
|
The spatial smoothing sigma in voxel coordinates.
TYPE:
|
magnitude
|
The standard deviation of the noise.
TYPE:
|
device
|
The device on which the output tensor is allocated. If None, defaults to CPU.
TYPE:
|
method
|
Method for noise generation. Upsampling is much faster and more memory efficient for larger sigma values, but at the cost of quality.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
A smooth Gaussian noise image of shape |
Source code in voxelmorph/nn/functional.py
spatial_transform
spatial_transform(image: Tensor, trf: Tensor, method: str = 'linear', isdisp: bool = True, meshgrid: Tensor = None, rotate_around_center: bool = True) -> Tensor
TODOC