Pad the input data by adding specified borders to every dimension. This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic ` for more information. Args: spatial_border: specified size for every spatial border. Any -ve values will be
| 238 | |
| 239 | |
| 240 | class BorderPad(Pad): |
| 241 | """ |
| 242 | Pad the input data by adding specified borders to every dimension. |
| 243 | |
| 244 | This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>` |
| 245 | for more information. |
| 246 | |
| 247 | Args: |
| 248 | spatial_border: specified size for every spatial border. Any -ve values will be set to 0. It can be 3 shapes: |
| 249 | |
| 250 | - single int number, pad all the borders with the same size. |
| 251 | - length equals the length of image shape, pad every spatial dimension separately. |
| 252 | for example, image shape(CHW) is [1, 4, 4], spatial_border is [2, 1], |
| 253 | pad every border of H dim with 2, pad every border of W dim with 1, result shape is [1, 8, 6]. |
| 254 | - length equals 2 x (length of image shape), pad every border of every dimension separately. |
| 255 | for example, image shape(CHW) is [1, 4, 4], spatial_border is [1, 2, 3, 4], pad top of H dim with 1, |
| 256 | pad bottom of H dim with 2, pad left of W dim with 3, pad right of W dim with 4. |
| 257 | the result shape is [1, 7, 11]. |
| 258 | mode: available modes for numpy array:{``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``, |
| 259 | ``"mean"``, ``"median"``, ``"minimum"``, ``"reflect"``, ``"symmetric"``, ``"wrap"``, ``"empty"``} |
| 260 | available modes for PyTorch Tensor: {``"constant"``, ``"reflect"``, ``"replicate"``, ``"circular"``}. |
| 261 | One of the listed string values or a user supplied function. Defaults to ``"constant"``. |
| 262 | See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html |
| 263 | https://pytorch.org/docs/stable/generated/torch.nn.functional.pad.html |
| 264 | lazy: a flag to indicate whether this transform should execute lazily or not. Defaults to False. |
| 265 | kwargs: other arguments for the `np.pad` or `torch.pad` function. |
| 266 | note that `np.pad` treats channel dimension as the first dimension. |
| 267 | |
| 268 | """ |
| 269 | |
| 270 | def __init__( |
| 271 | self, spatial_border: Sequence[int] | int, mode: str = PytorchPadMode.CONSTANT, lazy: bool = False, **kwargs |
| 272 | ) -> None: |
| 273 | self.spatial_border = spatial_border |
| 274 | super().__init__(mode=mode, lazy=lazy, **kwargs) |
| 275 | |
| 276 | def compute_pad_width(self, spatial_shape: Sequence[int]) -> tuple[tuple[int, int]]: |
| 277 | spatial_border = ensure_tuple(self.spatial_border) |
| 278 | if not all(isinstance(b, int) for b in spatial_border): |
| 279 | raise ValueError(f"self.spatial_border must contain only ints, got {spatial_border}.") |
| 280 | spatial_border = tuple(max(0, b) for b in spatial_border) |
| 281 | |
| 282 | if len(spatial_border) == 1: |
| 283 | data_pad_width = [(int(spatial_border[0]), int(spatial_border[0])) for _ in spatial_shape] |
| 284 | elif len(spatial_border) == len(spatial_shape): |
| 285 | data_pad_width = [(int(sp), int(sp)) for sp in spatial_border[: len(spatial_shape)]] |
| 286 | elif len(spatial_border) == len(spatial_shape) * 2: |
| 287 | data_pad_width = [ |
| 288 | (int(spatial_border[2 * i]), int(spatial_border[2 * i + 1])) for i in range(len(spatial_shape)) |
| 289 | ] |
| 290 | else: |
| 291 | raise ValueError( |
| 292 | f"Unsupported spatial_border length: {len(spatial_border)}, available options are " |
| 293 | f"[1, len(spatial_shape)={len(spatial_shape)}, 2*len(spatial_shape)={2 * len(spatial_shape)}]." |
| 294 | ) |
| 295 | return tuple([(0, 0)] + data_pad_width) # type: ignore |
| 296 | |
| 297 |