Args: img: data to be transformed, assuming `img` is channel-first and padding doesn't apply to the channel dim. to_pad: the amount to be padded in each dimension [(low_H, high_H), (low_W, high_W), ...]. default to `self.to_pad`. mode: ava
( # type: ignore[override]
self,
img: torch.Tensor,
to_pad: tuple[tuple[int, int]] | None = None,
mode: str | None = None,
lazy: bool | None = None,
**kwargs,
)
| 130 | raise NotImplementedError(f"subclass {self.__class__.__name__} must implement this method.") |
| 131 | |
| 132 | def __call__( # type: ignore[override] |
| 133 | self, |
| 134 | img: torch.Tensor, |
| 135 | to_pad: tuple[tuple[int, int]] | None = None, |
| 136 | mode: str | None = None, |
| 137 | lazy: bool | None = None, |
| 138 | **kwargs, |
| 139 | ) -> torch.Tensor: |
| 140 | """ |
| 141 | Args: |
| 142 | img: data to be transformed, assuming `img` is channel-first and padding doesn't apply to the channel dim. |
| 143 | to_pad: the amount to be padded in each dimension [(low_H, high_H), (low_W, high_W), ...]. |
| 144 | default to `self.to_pad`. |
| 145 | mode: available modes: (Numpy) {``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``, |
| 146 | ``"mean"``, ``"median"``, ``"minimum"``, ``"reflect"``, ``"symmetric"``, ``"wrap"``, ``"empty"``} |
| 147 | (PyTorch) {``"constant"``, ``"reflect"``, ``"replicate"``, ``"circular"``}. |
| 148 | One of the listed string values or a user supplied function. Defaults to ``"constant"``. |
| 149 | See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html |
| 150 | https://pytorch.org/docs/stable/generated/torch.nn.functional.pad.html |
| 151 | lazy: a flag to override the lazy behaviour for this call, if set. Defaults to None. |
| 152 | kwargs: other arguments for the `np.pad` or `torch.pad` function. |
| 153 | note that `np.pad` treats channel dimension as the first dimension. |
| 154 | |
| 155 | """ |
| 156 | to_pad_ = self.to_pad if to_pad is None else to_pad |
| 157 | if to_pad_ is None: |
| 158 | spatial_shape = img.peek_pending_shape() if isinstance(img, MetaTensor) else img.shape[1:] |
| 159 | to_pad_ = self.compute_pad_width(spatial_shape) |
| 160 | mode_ = self.mode if mode is None else mode |
| 161 | kwargs_ = dict(self.kwargs) |
| 162 | kwargs_.update(kwargs) |
| 163 | |
| 164 | img_t = convert_to_tensor(data=img, track_meta=get_track_meta()) |
| 165 | lazy_ = self.lazy if lazy is None else lazy |
| 166 | return pad_func(img_t, to_pad_, self.get_transform_info(), mode_, lazy_, **kwargs_) |
| 167 | |
| 168 | def inverse(self, data: MetaTensor) -> MetaTensor: |
| 169 | transform = self.pop_transform(data) |
no test coverage detected