Resize an image to a target spatial size by either centrally cropping the image or padding it evenly with a user-specified mode. When the dimension is smaller than the target size, do symmetric padding along that dim. When the dimension is larger than the target size, do central cro
| 1395 | |
| 1396 | |
| 1397 | class ResizeWithPadOrCrop(InvertibleTransform, LazyTransform): |
| 1398 | """ |
| 1399 | Resize an image to a target spatial size by either centrally cropping the image or |
| 1400 | padding it evenly with a user-specified mode. |
| 1401 | When the dimension is smaller than the target size, do symmetric padding along that dim. |
| 1402 | When the dimension is larger than the target size, do central cropping along that dim. |
| 1403 | |
| 1404 | This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>` |
| 1405 | for more information. |
| 1406 | |
| 1407 | Args: |
| 1408 | spatial_size: the spatial size of output data after padding or crop. |
| 1409 | If has non-positive values, the corresponding size of input image will be used (no padding). |
| 1410 | method: {``"symmetric"``, ``"end"``} |
| 1411 | Pad image symmetrically on every side or only pad at the end sides. Defaults to ``"symmetric"``. |
| 1412 | mode: available modes for numpy array:{``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``, |
| 1413 | ``"mean"``, ``"median"``, ``"minimum"``, ``"reflect"``, ``"symmetric"``, ``"wrap"``, ``"empty"``} |
| 1414 | available modes for PyTorch Tensor: {``"constant"``, ``"reflect"``, ``"replicate"``, ``"circular"``}. |
| 1415 | One of the listed string values or a user supplied function. Defaults to ``"constant"``. |
| 1416 | See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html |
| 1417 | https://pytorch.org/docs/stable/generated/torch.nn.functional.pad.html |
| 1418 | pad_kwargs: other arguments for the `np.pad` or `torch.pad` function. |
| 1419 | note that `np.pad` treats channel dimension as the first dimension. |
| 1420 | lazy: a flag to indicate whether this transform should execute lazily or not. Defaults to False. |
| 1421 | |
| 1422 | """ |
| 1423 | |
| 1424 | backend = list(set(SpatialPad.backend) & set(CenterSpatialCrop.backend)) |
| 1425 | |
| 1426 | def __init__( |
| 1427 | self, |
| 1428 | spatial_size: Sequence[int] | int, |
| 1429 | method: str = Method.SYMMETRIC, |
| 1430 | mode: str = PytorchPadMode.CONSTANT, |
| 1431 | lazy: bool = False, |
| 1432 | **pad_kwargs, |
| 1433 | ): |
| 1434 | LazyTransform.__init__(self, lazy) |
| 1435 | self.padder = SpatialPad(spatial_size=spatial_size, method=method, mode=mode, lazy=lazy, **pad_kwargs) |
| 1436 | self.cropper = CenterSpatialCrop(roi_size=spatial_size, lazy=lazy) |
| 1437 | |
| 1438 | @LazyTransform.lazy.setter # type: ignore |
| 1439 | def lazy(self, val: bool): |
| 1440 | self.padder.lazy = val |
| 1441 | self.cropper.lazy = val |
| 1442 | self._lazy = val |
| 1443 | |
| 1444 | def __call__( # type: ignore[override] |
| 1445 | self, img: torch.Tensor, mode: str | None = None, lazy: bool | None = None, **pad_kwargs |
| 1446 | ) -> torch.Tensor: |
| 1447 | """ |
| 1448 | Args: |
| 1449 | img: data to pad or crop, assuming `img` is channel-first and |
| 1450 | padding or cropping doesn't apply to the channel dim. |
| 1451 | mode: available modes for numpy array:{``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``, |
| 1452 | ``"mean"``, ``"median"``, ``"minimum"``, ``"reflect"``, ``"symmetric"``, ``"wrap"``, ``"empty"``} |
| 1453 | available modes for PyTorch Tensor: {``"constant"``, ``"reflect"``, ``"replicate"``, ``"circular"``}. |
| 1454 | One of the listed string values or a user supplied function. Defaults to ``"constant"``. |
no outgoing calls
searching dependent graphs…