Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Add PadTo to kornia.augmentation (#1286)

Edgar Riba6896a6eab5de
diff --git a/kornia/augmentation/__init__.py b/kornia/augmentation/__init__.py
index 6156114ff..5d48bced5 100644
--- a/kornia/augmentation/__init__.py
+++ b/kornia/augmentation/__init__.py
@@ -3,6 +3,7 @@ from .augmentation import (
ColorJitter,
Denormalize,
Normalize,
+ PadTo,
RandomAffine,
RandomBoxBlur,
RandomChannelShuffle,
diff --git a/kornia/augmentation/augmentation.py b/kornia/augmentation/augmentation.py
index 68d5f310a..f5bb62a39 100644
--- a/kornia/augmentation/augmentation.py
+++ b/kornia/augmentation/augmentation.py
@@ -105,6 +105,17 @@ class RandomHorizontalFlip(GeometricAugmentationBase2D):
) -> torch.Tensor:
return hflip(input)
+ def inverse_transform(
+ self,
+ input: torch.Tensor,
+ transform: Optional[torch.Tensor] = None,
+ size: Optional[Tuple[int, int]] = None,
+ **kwargs,
+ ) -> torch.Tensor:
+ return self.apply_transform(
+ input, params=self._params, transform=torch.as_tensor(transform, device=input.device, dtype=input.dtype)
+ )
+
class RandomVerticalFlip(GeometricAugmentationBase2D):
r"""Apply a random vertical flip to a tensor image or a batch of tensor images with a given probability.
@@ -161,6 +172,17 @@ class RandomVerticalFlip(GeometricAugmentationBase2D):
) -> torch.Tensor:
return vflip(input)
+ def inverse_transform(
+ self,
+ input: torch.Tensor,
+ transform: Optional[torch.Tensor] = None,
+ size: Optional[Tuple[int, int]] = None,
+ **kwargs,
+ ) -> torch.Tensor:
+ return self.apply_transform(
+ input, params=self._params, transform=torch.as_tensor(transform, device=input.device, dtype=input.dtype)
+ )
+
class ColorJitter(IntensityAugmentationBase2D):
r"""Apply a random transformation to the brightness, contrast, saturation and hue of a tensor image.
@@ -496,6 +518,17 @@ class RandomPerspective(GeometricAugmentationBase2D):
input, transform, (height, width), mode=self.resample.name.lower(), align_corners=self.align_corners
)
+ def inverse_transform(
+ self,
+ input: torch.Tensor,
+ transform: Optional[torch.Tensor] = None,
+ size: Optional[Tuple[int, int]] = None,
+ **kwargs,
+ ) -> torch.Tensor:
+ return self.apply_transform(
+ input, params=self._params, transform=torch.as_tensor(transform, device=input.device, dtype=input.dtype)
+ )
+
class RandomAffine(GeometricAugmentationBase2D):
r"""Apply a random 2D affine transformation to a tensor image.
@@ -686,6 +719,17 @@ class RandomAffine(GeometricAugmentationBase2D):
padding_mode=self.padding_mode.name.lower(),
)
+ def inverse_transform(
+ self,
+ input: torch.Tensor,
+ transform: Optional[torch.Tensor] = None,
+ size: Optional[Tuple[int, int]] = None,
+ **kwargs,
+ ) -> torch.Tensor:
+ return self.apply_transform(
+ input, params=self._params, transform=torch.as_tensor(transform, device=input.device, dtype=input.dtype)
+ )
+
class CenterCrop(GeometricAugmentationBase2D):
r"""Crop a given image tensor at the center.
@@ -911,6 +955,17 @@ class RandomRotation(GeometricAugmentationBase2D):
transform = cast(torch.Tensor, transform)
return affine(input, transform[..., :2, :3], self.resample.name.lower(), 'zeros', self.align_corners)
+ def inverse_transform(
+ self,
+ input: torch.Tensor,
+ transform: Optional[torch.Tensor] = None,
+ size: Optional[Tuple[int, int]] = None,
+ **kwargs,
+ ) -> torch.Tensor:
+ return self.apply_transform(
+ input, params=self._params, transform=torch.as_tensor(transform, device=input.device, dtype=input.dtype)
+ )
+
class RandomCrop(GeometricAugmentationBase2D):
r"""Crop random patches of a tensor image on a given size.
@@ -2052,6 +2107,7 @@ class RandomFisheye(GeometricAugmentationBase2D):
gamma = self.dist(self.gamma[:1], self.gamma[1:]).rsample(shape[:1])
return dict(center_x=center_x, center_y=center_y, gamma=gamma)
+ # TODO: It is incorrect to return identity
def compute_transformation(self, input: torch.Tensor, params: Dict[str, torch.Tensor]) -> torch.Tensor:
return self.identity_matrix(input)
@@ -2131,6 +2187,7 @@ class RandomElasticTransform(GeometricAugmentationBase2D):
noise = torch.rand(B, 2, H, W, device=self.device, dtype=self.dtype)
return dict(noise=noise * 2 - 1)
+ # TODO: It is incorrect to return identity
def compute_transformation(self, input: torch.Tensor, params: Dict[str, torch.Tensor]) -> torch.Tensor:
return self.identity_matrix(input)
@@ -2184,6 +2241,7 @@ class RandomThinPlateSpline(GeometricAugmentationBase2D):
dst = src + self.dist.rsample(src.shape)
return dict(src=src, dst=dst)
+ # TODO: It is incorrect to return identity
def compute_transformation(self, input: torch.Tensor, params: Dict[str, torch.Tensor]) -> torch.Tensor:
return self.identity_matrix(input)
@@ -2197,7 +2255,7 @@ class RandomThinPlateSpline(GeometricAugmentationBase2D):
return warp_image_tps(input, src, kernel, affine, self.align_corners)
-class RandomBoxBlur(GeometricAugmentationBase2D):
+class RandomBoxBlur(IntensityAugmentationBase2D):
"""Add random blur with a box filter to an image tensor.
.. image:: _static/img/RandomBoxBlur.png
@@ -2243,3 +2301,87 @@ class RandomBoxBlur(GeometricAugmentationBase2D):
self, input: torch.Tensor, params: Dict[str, torch.Tensor], transform: Optional[torch.Tensor] = None
) -> torch.Tensor:
return box_blur(input, self.kernel_size, self.border_type, self.normalized)
+
+
+class PadTo(GeometricAugmentationBase2D):
+ r"""Pad the given sample to a specific size.
+
+ Args:
+ size: a tuple of ints in the format (height, width) that give the spatial
+ dimensions to pad inputs to.
+ pad_mode: the type of padding to perform on the image (valid values
+ are those accepted by torch.nn.functional.pad)
+ pad_value: fill value for 'constant' padding applied to the image
+ p: probability of the image being flipped.
+ return_transform: if ``True`` return the matrix describing the transformation applied to each
+ input tensor. If ``False`` and the input is a tuple the applied transformation
+ won't be concatenated.
+ same_on_batch: apply the same transformation across the batch.
+ keepdim: whether to keep the output shape the same as input (True) or broadcast it
+ to the batch form (False).
+
+ Shape:
+ - Input: :math:`(C, H, W)` or :math:`(B, C, H, W)`, Optional: :math:`(B, 3, 3)`
+ - Output: :math:`(B, C, H, W)`
+
+ .. note::
+ This function internally uses :func:`torch.nn.functional.pad`.
+
+ Examples:
+ >>> img = torch.tensor([[[[0., 0., 0.],
+ ... [0., 0., 0.],
+ ... [0., 0., 0.]]]])
+ >>> pad = PadTo((4, 5), pad_value=1.)
+ >>> out = pad(img)
+ >>> out
+ tensor([[[[0., 0., 0., 1., 1.],
+ [0., 0., 0., 1., 1.],
+ [0., 0., 0., 1., 1.],
+ [1., 1., 1., 1., 1.]]]])
+ >>> pad.inverse(out)
+ tensor([[[[0., 0., 0.],
+ [0., 0., 0.],
+ [0., 0., 0.]]]])
+ """
+ def __init__(
+ self,
+ size: Tuple[int, int],
+ pad_mode: str = "constant",
+ pad_value: Union[int, float] = 0,
+ return_transform: bool = False,
+ ) -> None:
+ super().__init__(p=1., return_transform=return_transform, same_on_batch=True, p_batch=1.)
+ self.size = size
+ self.pad_mode = pad_mode
+ self.pad_value = pad_value
+
+ def __repr__(self) -> str:
+ return self.__class__.__name__ + f"({super().__repr__()})"
+
+ def generate_parameters(self, batch_shape: torch.Size) -> Dict[str, torch.Tensor]:
+ input_size = torch.tensor(batch_shape[-2:], dtype=torch.long).expand(batch_shape[0], -1)
+ return dict(input_size=input_size)
+
+ # TODO: It is incorrect to return identity
+ # TODO: Having a resampled version with ``warp_affine``
+ def compute_transformation(self, image: torch.Tensor, params: Dict[str, torch.Tensor]) -> torch.Tensor:
+ return self.identity_matrix(image)
+
+ def apply_transform(
+ self, input: torch.Tensor, params: Dict[str, torch.Tensor], transform: Optional[torch.Tensor] = None
+ ) -> torch.Tensor:
+ _, _, height, width = input.shape
+ height_pad: int = self.size[0] - height
+ width_pad: int = self.size[1] - width
+ return torch.nn.functional.pad(
+ input, (0, width_pad, 0, height_pad), mode=self.pad_mode, value=self.pad_value)
+
+ def inverse_transform(
+ self,
+ input: torch.Tensor,
+ transform: Optional[torch.Tensor] = None,
+ size: Optional[Tuple[int, int]] = None,
+ **kwargs
+ ) -> torch.Tensor:
+ size = cast(Tuple[int, int], size)
+ return input[..., :size[0], :size[1]]
diff --git a/kornia/augmentation/base.py b/kornia/augmentation/base.py
index 6f670ef9e..9b9f925bc 100644
--- a/kornia/augmentation/base.py
+++ b/kornia/augmentation/base.py
@@ -337,9 +337,7 @@ class GeometricAugmentationBase2D(AugmentationBase2D):
**kwargs,
) -> torch.Tensor:
"""By default, the exact transformation as ``apply_transform`` will be used."""
- return self.apply_transform(
- input, params=self._params, transform=torch.as_tensor(transform, device=input.device, dtype=input.dtype)
- )
+ raise NotImplementedError
def compute_inverse_transformation(self, transform: torch.Tensor):
"""Compute the inverse transform of given transformation matrices."""
diff --git a/test/augmentation/test_augmentation.py b/test/augmentation/test_augmentation.py
index 274e42d3f..a74b37244 100644
--- a/test/augmentation/test_augmentation.py
+++ b/test/augmentation/test_augmentation.py
@@ -12,6 +12,7 @@ from kornia.augmentation import (
ColorJitter,
Denormalize,
Normalize,
+ PadTo,
RandomBoxBlur,
RandomChannelShuffle,
RandomCrop,
@@ -2780,3 +2781,12 @@ class TestRandomBoxBlur:
img = torch.rand(1, 1, 2, 2, device=device, dtype=dtype)
aug = RandomBoxBlur(p=1.0)
assert img.shape == aug(img).shape
+
+
+class TestPadTo:
+ def test_smoke(self, device, dtype):
+ img = torch.rand(1, 1, 2, 2, device=device, dtype=dtype)
+ aug = PadTo(size=(4, 5))
+ out = aug(img)
+ assert out.shape == (1, 1, 4, 5)
+ assert (aug.inverse(out) == img).all()