Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Merge branch 'master' into master

Edgar Ribae7497cadfb74
diff --cc kornia/augmentation/augmentation.py
index c3d015bd6,34422595a..715c141eb
--- a/kornia/augmentation/augmentation.py
+++ b/kornia/augmentation/augmentation.py
@@@ -426,128 -424,5 +426,128 @@@ class RandomRotation(AugmentationBase)
if params is None:
height, width = self.infer_image_shape(input)
batch_size: int = self.infer_batch_size(input)
- self.set_params(batch_size, self.degrees)
- return super().forward(input, self._params)
+ params = self.get_params(batch_size, self.degrees)
+ return super().forward(input, params)
+
+
+class RandomCrop(AugmentationBase):
+ r"""Random Crop on given size.
+ Args:
+ size (tuple): Desired output size of the crop, like (h, w).
+ padding (int or sequence, optional): Optional padding on each border
+ of the image. Default is None, i.e no padding. If a sequence of length
+ 4 is provided, it is used to pad left, top, right, bottom borders
+ respectively. If a sequence of length 2 is provided, it is used to
+ pad left/right, top/bottom borders, respectively.
+ pad_if_needed (boolean): It will pad the image if smaller than the
+ desired size to avoid raising an exception. Since cropping is done
+ after padding, the padding seems to be done at a random offset.
+ fill: Pixel fill value for constant fill. Default is 0. If a tuple of
+ length 3, it is used to fill R, G, B channels respectively.
+ This value is only used when the padding_mode is constant
+ padding_mode: Type of padding. Should be: constant, edge, reflect or symmetric. Default is constant.
+ return_transform (bool): if ``True`` return the matrix describing the transformation applied to each
+ input tensor. If ``False`` and the input is a tuple the applied transformation
+ wont be concatenated
+ """
+
+ def __init__(self, size: Tuple[int, int], padding: Optional[BoarderUnionType] = None,
+ pad_if_needed: Optional[bool] = False, fill=0, padding_mode='constant',
+ return_transform: bool = False) -> None:
+ super(RandomCrop, self).__init__(F.apply_crop, return_transform)
+ self.size = size
+ self.padding = padding
+ self.pad_if_needed = pad_if_needed
+ self.fill = fill
+ self.padding_mode = padding_mode
+
+ def __repr__(self) -> str:
+ repr = f"RandomCrop(crop_size={self.size}, padding={self.padding}, fill={self.fill},\
+ pad_if_needed={self.pad_if_needed}, return_transform={self.return_transform})"
+ return self.__class__.__name__ + repr
+
+ @staticmethod
+ def get_params(batch_size: int, input_size: Tuple[int, int], size: Tuple[int, int]) -> Dict[str, torch.Tensor]:
+ return pg._random_crop_gen(batch_size, input_size, size)
+
+ def precrop_padding(self, input: torch.Tensor) -> torch.Tensor:
+
+ if self.padding is not None:
+ if isinstance(self.padding, int):
+ padding = [self.padding, self.padding, self.padding, self.padding]
+ elif isinstance(self.padding, tuple) and len(self.padding) == 2:
+ padding = [self.padding[1], self.padding[1], self.padding[0], self.padding[0]]
+ elif isinstance(self.padding, tuple) and len(self.padding) == 4:
+ padding = [self.padding[3], self.padding[2], self.padding[1], self.padding[0]] # type:ignore
+ input = pad(input, padding, value=self.fill, mode=self.padding_mode)
+
+ if self.pad_if_needed and input.shape[-2] < self.size[0]:
+ padding = [0, 0, (self.size[0] - input.shape[-2]), self.size[0] - input.shape[-2]]
+ input = pad(input, padding, value=self.fill, mode=self.padding_mode)
+
+ if self.pad_if_needed and input.shape[-1] < self.size[1]:
+ padding = [self.size[1] - input.shape[-1], self.size[1] - input.shape[-1], 0, 0]
+ input = pad(input, padding, value=self.fill, mode=self.padding_mode)
+
+ return input
+
+ def forward(self, input: UnionType, params: Optional[Dict[str, torch.Tensor]] = None) -> UnionType: # type: ignore
+ if isinstance(input, tuple):
+ input = (self.precrop_padding(input[0]), self.precrop_padding(input[1]))
+ batch_shape = input[0].shape
+ else:
+ input = self.precrop_padding(input)
+ batch_shape = input.shape
+
+ if params is None:
+ batch_size = self.infer_batch_size(input)
+ print(batch_shape)
+ params = RandomCrop.get_params(batch_size, batch_shape[-2:], self.size) # type: ignore
+ return super().forward(input, params)
+
+
+class RandomResizedCrop(AugmentationBase):
+ r"""Random Crop on given size and resizing the cropped patch to another.
+ Args:
+ size (Tuple[int, int]): expected output size of each edge
+ scale: range of size of the origin size cropped
+ ratio: range of aspect ratio of the origin aspect ratio cropped
+ interpolation: Default: PIL.Image.BILINEAR
+ return_transform (bool): if ``True`` return the matrix describing the transformation applied to each
+ input tensor. If ``False`` and the input is a tuple the applied transformation
+ wont be concatenated
+ """
+
+ def __init__(self, size: Tuple[int, int], scale=(1.0, 1.0), ratio=(1.0, 1.0),
+ interpolation=None, return_transform: bool = False) -> None:
+ super(RandomResizedCrop, self).__init__(F.apply_crop, return_transform)
+ self.size = size
+ self.scale = scale
+ self.ratio = ratio
+ self.interpolation = interpolation
+ if interpolation is not None:
+ raise ValueError("Interpolation has not been implemented. Please set to None")
+
+ def __repr__(self) -> str:
+ repr = f"RandomResizedCrop(size={self.size}, resize_to={self.scale}, resize_to={self.ratio}\
+ , return_transform={self.return_transform})"
+ return self.__class__.__name__ + repr
+
+ @staticmethod
+ def get_params(batch_size: int, input_size: Tuple[int, int], size: Tuple[int, int],
+ scale: Tuple[float, float] = (0.08, 1.0), ratio: Tuple[float, float] = (3. / 4., 4. / 3.)
+ ) -> Dict[str, torch.Tensor]:
+ target_size = pg._random_crop_size_gen(size, scale, ratio)
+ # TODO: scale and aspect ratio were fixed for one batch for now. Need to be separated.
+ return pg._random_crop_gen(batch_size, input_size, target_size, resize_to=size)
+
+ def forward(self, input: UnionType, params: Optional[Dict[str, torch.Tensor]] = None) -> UnionType: # type: ignore
+ if params is None:
+ batch_size = self.infer_batch_size(input)
+ if isinstance(input, tuple):
+ batch_shape = input[0].shape
+ else:
+ batch_shape = input.shape
+ params = RandomResizedCrop.get_params(
+ batch_size, (batch_shape[-2], batch_shape[-1]), self.size, self.scale, self.ratio)
- return super().forward(input, params)
++ return super().forward(input, params)
diff --cc kornia/augmentation/functional.py
index 004147177,3345660c8..954fee11f
--- a/kornia/augmentation/functional.py
+++ b/kornia/augmentation/functional.py
@@@ -405,12 -439,14 +405,14 @@@ def _apply_perspective(input: torch.Ten
return out_data.view_as(input)
- def apply_affine(input: torch.Tensor,
- params: Dict[str, torch.Tensor],
- return_transform: bool = False) -> UnionType:
+ def _apply_affine(input: torch.Tensor,
+ params: Dict[str, torch.Tensor],
+ return_transform: bool = False) -> UnionType:
+ if not torch.is_tensor(input):
+ raise TypeError(f"Input type is not a torch.Tensor. Got {type(input)}")
r"""Random affine transformation of the image keeping center invariant
Args:
- input (torch.Tensor): Tensor to be transformed with shape (*, C, H, W).
+ input (torch.Tensor): Tensor to be transformed with shape (H, W), (C, H, W), (*, C, H, W).
degrees (float or tuple): Range of degrees to select from.
If degrees is a number instead of sequence like (min, max), the range of degrees
will be (-degrees, +degrees). Set to 0 to deactivate rotations.