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

Method __call__

monai/transforms/spatial/array.py:2940–2987  ·  view source on GitHub ↗

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

(
        self,
        img: torch.Tensor,
        spatial_size: tuple[int, 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

2938 self.rand_affine_grid.randomize()
2939
2940 def __call__(
2941 self,
2942 img: torch.Tensor,
2943 spatial_size: tuple[int, int, int] | int | None = None,
2944 mode: str | int | None = None,
2945 padding_mode: str | None = None,
2946 randomize: bool = True,
2947 ) -> torch.Tensor:
2948 """
2949 Args:
2950 img: shape must be (num_channels, H, W, D),
2951 spatial_size: specifying spatial 3D output image spatial size [h, w, d].
2952 if `spatial_size` and `self.spatial_size` are not defined, or smaller than 1,
2953 the transform will use the spatial size of `img`.
2954 mode: {``"bilinear"``, ``"nearest"``} or spline interpolation order 0-5 (integers).
2955 Interpolation mode to calculate output values. Defaults to ``self.mode``.
2956 See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
2957 When it's an integer, the numpy (cpu tensor)/cupy (cuda tensor) backends will be used
2958 and the value represents the order of the spline interpolation.
2959 See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html
2960 padding_mode: {``"zeros"``, ``"border"``, ``"reflection"``}
2961 Padding mode for outside grid values. Defaults to ``self.padding_mode``.
2962 See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
2963 When `mode` is an integer, using numpy/cupy backends, this argument accepts
2964 {'reflect', 'grid-mirror', 'constant', 'grid-constant', 'nearest', 'mirror', 'grid-wrap', 'wrap'}.
2965 See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html
2966 randomize: whether to execute `randomize()` function first, default to True.
2967 """
2968 sp_size = fall_back_tuple(self.spatial_size if spatial_size is None else spatial_size, img.shape[1:])
2969 if randomize:
2970 self.randomize(grid_size=sp_size)
2971
2972 _device = img.device if isinstance(img, torch.Tensor) else self.device
2973 grid = create_grid(spatial_size=sp_size, device=_device, backend="torch")
2974 if self._do_transform:
2975 if self.rand_offset is None:
2976 raise RuntimeError("rand_offset is not initialized.")
2977 gaussian = GaussianFilter(3, self.sigma, 3.0).to(device=_device)
2978 offset = torch.as_tensor(self.rand_offset, device=_device).unsqueeze(0)
2979 grid[:3] += gaussian(offset)[0] * self.magnitude
2980 grid = self.rand_affine_grid(grid=grid)
2981 out: torch.Tensor = self.resampler(
2982 img,
2983 grid, # type: ignore
2984 mode=mode if mode is not None else self.mode,
2985 padding_mode=padding_mode if padding_mode is not None else self.padding_mode,
2986 )
2987 return out
2988
2989
2990class GridDistortion(Transform):

Callers

nothing calls this directly

Calls 5

randomizeMethod · 0.95
fall_back_tupleFunction · 0.90
create_gridFunction · 0.90
GaussianFilterClass · 0.90
as_tensorMethod · 0.80

Tested by

no test coverage detected