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

Method __call__

monai/transforms/spatial/array.py:2552–2624  ·  view source on GitHub ↗

Args: img: shape must be (num_channels, H, W[, D]), spatial_size: output image spatial size. 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: Sequence[int] | int | None = None,
        mode: str | int | None = None,
        padding_mode: str | None = None,
        randomize: bool = True,
        grid=None,
        lazy: bool | None = None,
    )

Source from the content-addressed store, hash-verified

2550 self.rand_affine_grid.randomize()
2551
2552 def __call__(
2553 self,
2554 img: torch.Tensor,
2555 spatial_size: Sequence[int] | int | None = None,
2556 mode: str | int | None = None,
2557 padding_mode: str | None = None,
2558 randomize: bool = True,
2559 grid=None,
2560 lazy: bool | None = None,
2561 ) -> torch.Tensor:
2562 """
2563 Args:
2564 img: shape must be (num_channels, H, W[, D]),
2565 spatial_size: output image spatial size.
2566 if `spatial_size` and `self.spatial_size` are not defined, or smaller than 1,
2567 the transform will use the spatial size of `img`.
2568 if `img` has two spatial dimensions, `spatial_size` should have 2 elements [h, w].
2569 if `img` has three spatial dimensions, `spatial_size` should have 3 elements [h, w, d].
2570 mode: {``"bilinear"``, ``"nearest"``} or spline interpolation order 0-5 (integers).
2571 Interpolation mode to calculate output values. Defaults to ``self.mode``.
2572 See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
2573 When it's an integer, the numpy (cpu tensor)/cupy (cuda tensor) backends will be used
2574 and the value represents the order of the spline interpolation.
2575 See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html
2576 padding_mode: {``"zeros"``, ``"border"``, ``"reflection"``}
2577 Padding mode for outside grid values. Defaults to ``self.padding_mode``.
2578 See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
2579 When `mode` is an integer, using numpy/cupy backends, this argument accepts
2580 {'reflect', 'grid-mirror', 'constant', 'grid-constant', 'nearest', 'mirror', 'grid-wrap', 'wrap'}.
2581 See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html
2582 randomize: whether to execute `randomize()` function first, default to True.
2583 grid: precomputed grid to be used (mainly to accelerate `RandAffined`).
2584 lazy: a flag to indicate whether this transform should execute lazily or not
2585 during this call. Setting this to False or True overrides the ``lazy`` flag set
2586 during initialization for this call. Defaults to None.
2587 """
2588 if randomize:
2589 self.randomize()
2590 # if not doing transform and spatial size doesn't change, nothing to do
2591 # except convert to float and device
2592 ori_size = img.peek_pending_shape() if isinstance(img, MetaTensor) else img.shape[1:]
2593 sp_size = fall_back_tuple(self.spatial_size if spatial_size is None else spatial_size, ori_size)
2594 do_resampling = self._do_transform or (sp_size != ensure_tuple(ori_size))
2595 _mode = mode if mode is not None else self.mode
2596 _padding_mode = padding_mode if padding_mode is not None else self.padding_mode
2597 lazy_ = self.lazy if lazy is None else lazy
2598 img = convert_to_tensor(img, track_meta=get_track_meta())
2599 if lazy_:
2600 if self._do_transform:
2601 if grid is None:
2602 self.rand_affine_grid(sp_size, randomize=randomize, lazy=True)
2603 affine = self.rand_affine_grid.get_transformation_matrix()
2604 else:
2605 affine = convert_to_dst_type(torch.eye(len(sp_size) + 1), img, dtype=self.rand_affine_grid.dtype)[0]
2606 else:
2607 if grid is None:
2608 grid = self.get_identity_grid(sp_size, lazy_)
2609 if self._do_transform:

Callers

nothing calls this directly

Calls 11

randomizeMethod · 0.95
get_identity_gridMethod · 0.95
fall_back_tupleFunction · 0.90
ensure_tupleFunction · 0.90
convert_to_tensorFunction · 0.90
get_track_metaFunction · 0.90
convert_to_dst_typeFunction · 0.90
affine_funcFunction · 0.90
peek_pending_shapeMethod · 0.80
get_transform_infoMethod · 0.80

Tested by

no test coverage detected