MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / AffineTransform

Class AffineTransform

monai/networks/layers/spatial_transforms.py:439–592  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

437
438
439class AffineTransform(nn.Module):
440
441 def __init__(
442 self,
443 spatial_size: Sequence[int] | int | None = None,
444 normalized: bool = False,
445 mode: str = GridSampleMode.BILINEAR,
446 padding_mode: str = GridSamplePadMode.ZEROS,
447 align_corners: bool = True,
448 reverse_indexing: bool = True,
449 zero_centered: bool | None = None,
450 ) -> None:
451 """
452 Apply affine transformations with a batch of affine matrices.
453
454 When `normalized=False` and `reverse_indexing=True`,
455 it does the commonly used resampling in the 'pull' direction
456 following the ``scipy.ndimage.affine_transform`` convention.
457 In this case `theta` is equivalent to (ndim+1, ndim+1) input ``matrix`` of ``scipy.ndimage.affine_transform``,
458 operates on homogeneous coordinates.
459 See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.affine_transform.html
460
461 When `normalized=True` and `reverse_indexing=False`,
462 it applies `theta` to the normalized coordinates (coords. in the range of [-1, 1]) directly.
463 This is often used with `align_corners=False` to achieve resolution-agnostic resampling,
464 thus useful as a part of trainable modules such as the spatial transformer networks.
465 See also: https://pytorch.org/tutorials/intermediate/spatial_transformer_tutorial.html
466
467 Args:
468 spatial_size: output spatial shape, the full output shape will be
469 `[N, C, *spatial_size]` where N and C are inferred from the `src` input of `self.forward`.
470 normalized: indicating whether the provided affine matrix `theta` is defined
471 for the normalized coordinates. If `normalized=False`, `theta` will be converted
472 to operate on normalized coordinates as pytorch affine_grid works with the normalized
473 coordinates.
474 mode: {``"bilinear"``, ``"nearest"``}
475 Interpolation mode to calculate output values. Defaults to ``"bilinear"``.
476 See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
477 padding_mode: {``"zeros"``, ``"border"``, ``"reflection"``}
478 Padding mode for outside grid values. Defaults to ``"zeros"``.
479 See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
480 align_corners: see also https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html.
481 reverse_indexing: whether to reverse the spatial indexing of image and coordinates.
482 set to `False` if `theta` follows pytorch's default "D, H, W" convention.
483 set to `True` if `theta` follows `scipy.ndimage` default "i, j, k" convention.
484 zero_centered: whether the affine is applied to coordinates in a zero-centered value range.
485 With `zero_centered=True`, for example, the center of rotation will be the
486 spatial center of the input; with `zero_centered=False`, the center of rotation will be the
487 origin of the input. This option is only available when `normalized=False`,
488 where the default behaviour is `False` if unspecified.
489 See also: :py:func:`monai.networks.utils.normalize_transform`.
490 """
491 super().__init__()
492 self.spatial_size = ensure_tuple(spatial_size) if spatial_size is not None else None
493 self.normalized = normalized
494 self.mode: str = look_up_option(mode, GridSampleMode)
495 self.padding_mode: str = look_up_option(padding_mode, GridSamplePadMode)
496 self.align_corners = align_corners

Callers 15

spatial_resampleFunction · 0.90
rotateFunction · 0.90
inverse_transformMethod · 0.90
__init__Method · 0.90
test_zoomMethod · 0.90
test_zoom_1Method · 0.90
test_zoom_2Method · 0.90
test_zoom_zero_centerMethod · 0.90

Calls

no outgoing calls

Tested by 14

__init__Method · 0.72
test_zoomMethod · 0.72
test_zoom_1Method · 0.72
test_zoom_2Method · 0.72
test_zoom_zero_centerMethod · 0.72
test_forward_2dMethod · 0.72
test_forward_3dMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…