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

Method __call__

monai/transforms/spatial/array.py:2766–2818  ·  view source on GitHub ↗

Args: img: shape must be (num_channels, H, W), spatial_size: specifying output image spatial size [h, w]. if `spatial_size` and `self.spatial_size` are not defined, or smaller than 1, the transform will use the spatial size of `img`.

(
        self,
        img: torch.Tensor,
        spatial_size: tuple[int, int] | int | None = None,
        mode: str | int | None = None,
        padding_mode: str | None = None,
        randomize: bool = True,
    )

Source from the content-addressed store, hash-verified

2764 self.rand_affine_grid.randomize()
2765
2766 def __call__(
2767 self,
2768 img: torch.Tensor,
2769 spatial_size: tuple[int, int] | int | None = None,
2770 mode: str | int | None = None,
2771 padding_mode: str | None = None,
2772 randomize: bool = True,
2773 ) -> torch.Tensor:
2774 """
2775 Args:
2776 img: shape must be (num_channels, H, W),
2777 spatial_size: specifying output image spatial size [h, w].
2778 if `spatial_size` and `self.spatial_size` are not defined, or smaller than 1,
2779 the transform will use the spatial size of `img`.
2780 mode: {``"bilinear"``, ``"nearest"``} or spline interpolation order 0-5 (integers).
2781 Interpolation mode to calculate output values. Defaults to ``self.mode``.
2782 See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
2783 When it's an integer, the numpy (cpu tensor)/cupy (cuda tensor) backends will be used
2784 and the value represents the order of the spline interpolation.
2785 See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html
2786 padding_mode: {``"zeros"``, ``"border"``, ``"reflection"``}
2787 Padding mode for outside grid values. Defaults to ``self.padding_mode``.
2788 See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
2789 When `mode` is an integer, using numpy/cupy backends, this argument accepts
2790 {'reflect', 'grid-mirror', 'constant', 'grid-constant', 'nearest', 'mirror', 'grid-wrap', 'wrap'}.
2791 See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html
2792 randomize: whether to execute `randomize()` function first, default to True.
2793 """
2794 sp_size = fall_back_tuple(self.spatial_size if spatial_size is None else spatial_size, img.shape[1:])
2795 if randomize:
2796 self.randomize(spatial_size=sp_size)
2797
2798 if self._do_transform:
2799 grid = self.deform_grid(spatial_size=sp_size)
2800 grid = self.rand_affine_grid(grid=grid)
2801 grid = torch.nn.functional.interpolate(
2802 recompute_scale_factor=True,
2803 input=grid.unsqueeze(0),
2804 scale_factor=list(ensure_tuple(self.deform_grid.spacing)),
2805 mode=InterpolateMode.BICUBIC.value,
2806 align_corners=False,
2807 )
2808 grid = CenterSpatialCrop(roi_size=sp_size)(grid[0])
2809 else:
2810 _device = img.device if isinstance(img, torch.Tensor) else self.device
2811 grid = cast(torch.Tensor, create_grid(spatial_size=sp_size, device=_device, backend="torch"))
2812 out: torch.Tensor = self.resampler(
2813 img,
2814 grid,
2815 mode=mode if mode is not None else self.mode,
2816 padding_mode=padding_mode if padding_mode is not None else self.padding_mode,
2817 )
2818 return out
2819
2820
2821class Rand3DElastic(RandomizableTransform):

Callers

nothing calls this directly

Calls 5

randomizeMethod · 0.95
fall_back_tupleFunction · 0.90
ensure_tupleFunction · 0.90
CenterSpatialCropClass · 0.90
create_gridFunction · 0.90

Tested by

no test coverage detected