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

Class RandDeformGrid

monai/transforms/spatial/array.py:1962–2003  ·  view source on GitHub ↗

Generate random deformation grid.

Source from the content-addressed store, hash-verified

1960
1961
1962class RandDeformGrid(Randomizable, Transform):
1963 """
1964 Generate random deformation grid.
1965 """
1966
1967 backend = [TransformBackends.TORCH]
1968
1969 def __init__(
1970 self, spacing: Sequence[float] | float, magnitude_range: tuple[float, float], device: torch.device | None = None
1971 ) -> None:
1972 """
1973 Args:
1974 spacing: spacing of the grid in 2D or 3D.
1975 e.g., spacing=(1, 1) indicates pixel-wise deformation in 2D,
1976 spacing=(1, 1, 1) indicates voxel-wise deformation in 3D,
1977 spacing=(2, 2) indicates deformation field defined on every other pixel in 2D.
1978 magnitude_range: the random offsets will be generated from
1979 `uniform[magnitude[0], magnitude[1])`.
1980 device: device to store the output grid data.
1981 """
1982 self.spacing = spacing
1983 self.magnitude = magnitude_range
1984
1985 self.rand_mag = 1.0
1986 self.random_offset: np.ndarray
1987 self.device = device
1988
1989 def randomize(self, grid_size: Sequence[int]) -> None:
1990 self.random_offset = self.R.normal(size=([len(grid_size)] + list(grid_size))).astype(np.float32, copy=False)
1991 self.rand_mag = self.R.uniform(self.magnitude[0], self.magnitude[1])
1992
1993 def __call__(self, spatial_size: Sequence[int]) -> torch.Tensor:
1994 """
1995 Args:
1996 spatial_size: spatial size of the grid.
1997 """
1998 self.spacing = fall_back_tuple(self.spacing, (1.0,) * len(spatial_size))
1999 control_grid = create_control_grid(spatial_size, self.spacing, device=self.device, backend="torch")
2000 self.randomize(control_grid.shape[1:])
2001 _offset, *_ = convert_to_dst_type(self.rand_mag * self.random_offset, control_grid)
2002 control_grid[: len(spatial_size)] += _offset
2003 return control_grid # type: ignore
2004
2005
2006class Resample(Transform):

Callers 2

test_rand_deform_gridMethod · 0.90
__init__Method · 0.85

Calls

no outgoing calls

Tested by 1

test_rand_deform_gridMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…