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

Method __call__

monai/transforms/spatial/array.py:3584–3628  ·  view source on GitHub ↗

Args: img: shape must be (num_channels, H, W[, D]), randomize: whether to execute `randomize()` function first, defaults to True.

(self, img: torch.Tensor, randomize: bool = True)

Source from the content-addressed store, hash-verified

3582 return None
3583
3584 def __call__(self, img: torch.Tensor, randomize: bool = True) -> torch.Tensor:
3585 """
3586 Args:
3587 img: shape must be (num_channels, H, W[, D]),
3588 randomize: whether to execute `randomize()` function first, defaults to True.
3589 """
3590 if randomize:
3591 self.randomize()
3592
3593 if self._do_transform:
3594 input_shape = img.shape[1:]
3595 # Clamp each axis to at least 1 so F.interpolate never sees a zero-sized dimension.
3596 target_shape = tuple(max(1, int(np.round(s * self.zoom_factor))) for s in input_shape)
3597
3598 # Use F.interpolate directly on a plain tensor to avoid mutating the global
3599 # set_track_meta flag, which is not thread-safe (see GitHub issue #8409).
3600 img_t = convert_to_tensor(img, track_meta=False)
3601 # F.interpolate requires float input and a batch dimension; cast matches
3602 # the default dtype=float32 that Resize uses internally.
3603 img_float = img_t.unsqueeze(0).to(dtype=torch.float32)
3604
3605 downsample_mode = str(self.downsample_mode)
3606 upsample_mode = str(self.upsample_mode)
3607 # align_corners is only valid for linear/bilinear/bicubic/trilinear modes
3608 _align_corners_modes = {"linear", "bilinear", "bicubic", "trilinear"}
3609 downsample_align_corners = self.align_corners if downsample_mode in _align_corners_modes else None
3610 upsample_align_corners = self.align_corners if upsample_mode in _align_corners_modes else None
3611
3612 img_downsampled = torch.nn.functional.interpolate(
3613 img_float, size=target_shape, mode=downsample_mode, align_corners=downsample_align_corners
3614 )
3615 img_upsampled_t = torch.nn.functional.interpolate(
3616 img_downsampled, size=input_shape, mode=upsample_mode, align_corners=upsample_align_corners
3617 ).squeeze(0)
3618
3619 # copy metadata from original image to down-and-upsampled image,
3620 # respecting the caller's get_track_meta() setting.
3621 img_upsampled = cast(torch.Tensor, convert_to_tensor(img_upsampled_t, track_meta=get_track_meta()))
3622 if isinstance(img_upsampled, MetaTensor):
3623 img_upsampled.copy_meta_from(img)
3624
3625 return img_upsampled
3626
3627 else:
3628 return img
3629
3630
3631class ConvertBoxToPoints(Transform):

Callers

nothing calls this directly

Calls 5

randomizeMethod · 0.95
convert_to_tensorFunction · 0.90
get_track_metaFunction · 0.90
maxFunction · 0.85
copy_meta_fromMethod · 0.80

Tested by

no test coverage detected