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)
| 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 | |
| 3631 | class ConvertBoxToPoints(Transform): |
nothing calls this directly
no test coverage detected