Perform padding for a given an amount of padding in each dimension. `torch.nn.functional.pad` is used unless the mode or kwargs are not available in torch, in which case `np.pad` will be used. This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_re
| 79 | |
| 80 | |
| 81 | class Pad(InvertibleTransform, LazyTransform): |
| 82 | """ |
| 83 | Perform padding for a given an amount of padding in each dimension. |
| 84 | |
| 85 | `torch.nn.functional.pad` is used unless the mode or kwargs are not available in torch, |
| 86 | in which case `np.pad` will be used. |
| 87 | |
| 88 | This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>` |
| 89 | for more information. |
| 90 | |
| 91 | Args: |
| 92 | to_pad: the amount to pad in each dimension (including the channel) [(low_H, high_H), (low_W, high_W), ...]. |
| 93 | if None, must provide in the `__call__` at runtime. |
| 94 | mode: available modes: (Numpy) {``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``, |
| 95 | ``"mean"``, ``"median"``, ``"minimum"``, ``"reflect"``, ``"symmetric"``, ``"wrap"``, ``"empty"``} |
| 96 | (PyTorch) {``"constant"``, ``"reflect"``, ``"replicate"``, ``"circular"``}. |
| 97 | One of the listed string values or a user supplied function. Defaults to ``"constant"``. |
| 98 | See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html |
| 99 | https://pytorch.org/docs/stable/generated/torch.nn.functional.pad.html |
| 100 | requires pytorch >= 1.10 for best compatibility. |
| 101 | lazy: a flag to indicate whether this transform should execute lazily or not. Defaults to False. |
| 102 | kwargs: other arguments for the `np.pad` or `torch.pad` function. |
| 103 | note that `np.pad` treats channel dimension as the first dimension. |
| 104 | |
| 105 | """ |
| 106 | |
| 107 | backend = [TransformBackends.TORCH, TransformBackends.NUMPY] |
| 108 | |
| 109 | def __init__( |
| 110 | self, |
| 111 | to_pad: tuple[tuple[int, int]] | None = None, |
| 112 | mode: str = PytorchPadMode.CONSTANT, |
| 113 | lazy: bool = False, |
| 114 | **kwargs, |
| 115 | ) -> None: |
| 116 | LazyTransform.__init__(self, lazy) |
| 117 | self.to_pad = to_pad |
| 118 | self.mode = mode |
| 119 | self.kwargs = kwargs |
| 120 | |
| 121 | def compute_pad_width(self, spatial_shape: Sequence[int]) -> tuple[tuple[int, int]]: |
| 122 | """ |
| 123 | dynamically compute the pad width according to the spatial shape. |
| 124 | the output is the amount of padding for all dimensions including the channel. |
| 125 | |
| 126 | Args: |
| 127 | spatial_shape: spatial shape of the original image. |
| 128 | |
| 129 | """ |
| 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, |