2D geometric transforms#
Transforms that move pixels. Each one exposes its (B, 3, 3) transformation matrix, can be inverted, and is
applied consistently to masks, boxes and keypoints by AugmentationSequential.
- class kornia.augmentation.CenterCrop(size, align_corners=True, resample=Resample.BILINEAR.name, p=1.0, keepdim=False, cropping_mode='slice')[source]#
Crop a given image torch.Tensor at the center.
- Parameters:
size (
Union[int,Tuple[int,int]]) – Desired output size (out_h, out_w) of the crop. If integer, out_h = out_w = size. If Tuple[int, int], out_h = size[0], out_w = size[1].align_corners (
bool, optional) – interpolation flag. Default:Trueresample (
Union[str,int,Resample], optional) – The interpolation mode. Default:Resample.BILINEAR.namep (
float, optional) – probability of applying the transformation for the whole batch. Default:1.0keepdim (
bool, optional) – whether to keep the output shape the same as input (True) or broadcast it to the batch form (False). Default:Falsecropping_mode (
str, optional) – The used algorithm to crop.slicewill use advanced slicing to extract the torch.Tensor based on the sampled indices.resamplewill use warp_affine using the affine transformation to extract and resize at once. Use slice for efficiency, or resample for proper differentiability. Default:"slice"
- Shape:
Input: \((C, H, W)\) or \((B, C, H, W)\), Optional: \((B, 3, 3)\)
Output: \((B, C, out_h, out_w)\)
Note
This function internally uses
kornia.geometry.transform.crop_by_boxes().Examples
>>> import torch >>> rng = torch.manual_seed(0) >>> inputs = torch.randn(1, 1, 4, 4) >>> inputs tensor([[[[-1.1258, -1.1524, -0.2506, -0.4339], [ 0.8487, 0.6920, -0.3160, -2.1152], [ 0.3223, -1.2633, 0.3500, 0.3081], [ 0.1198, 1.2377, 1.1168, -0.2473]]]]) >>> aug = CenterCrop(2, p=1., cropping_mode="resample") >>> out = aug(inputs) >>> out tensor([[[[ 0.6920, -0.3160], [-1.2633, 0.3500]]]]) >>> aug.inverse(out, padding_mode="border") tensor([[[[ 0.6920, 0.6920, -0.3160, -0.3160], [ 0.6920, 0.6920, -0.3160, -0.3160], [-1.2633, -1.2633, 0.3500, 0.3500], [-1.2633, -1.2633, 0.3500, 0.3500]]]])
- To apply the exact augmenation again, you may take the advantage of the previous parameter state:
>>> input = torch.randn(1, 3, 32, 32) >>> aug = CenterCrop(2, p=1., cropping_mode="resample") >>> (aug(input) == aug(input, params=aug._params)).all() tensor(True)
- class kornia.augmentation.PadTo(size, pad_mode='constant', pad_value=0, keepdim=False)[source]#
Pad the given sample to a specific size. Always occurs (p=1.0).
- Parameters:
size (
Tuple[int,int]) – a tuple of ints in the format (height, width) that give the spatial dimensions to pad inputs to.pad_mode (
str, optional) – the type of padding to perform on the image (valid values are those accepted by torch.nn.functional.pad) Default:"constant"pad_value (
float, optional) – fill value for ‘constant’ padding applied to the image Default:0keepdim (
bool, optional) – whether to keep the output shape the same as input (True) or broadcast it to the batch form (False). Default:False
- Shape:
Input: \((C, H, W)\) or \((B, C, H, W)\), Optional: \((B, 3, 3)\)
Output: \((B, C, H, W)\)
Note
This function internally uses
torch.nn.functional.pad().Examples
>>> import torch >>> 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.]]]])
- class kornia.augmentation.RandomAffine(degrees, translate=None, scale=None, shear=None, resample=Resample.BILINEAR.name, same_on_batch=False, align_corners=False, padding_mode=SamplePadding.ZEROS.name, fill_value=None, p=0.5, keepdim=False)[source]#
Apply a random 2D affine transformation to a torch.Tensor image.
The transformation is computed so that the image center is kept invariant.
- Parameters:
degrees (
Union[Tensor,float,Tuple[float,float]]) – 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.translate (
Union[Tensor,Tuple[float,float],None], optional) – tuple of maximum absolute fraction for horizontal and vertical translations. For example translate=(a, b), then horizontal shift is randomly sampled in the range -img_width * a < dx < img_width * a and vertical shift is randomly sampled in the range -img_height * b < dy < img_height * b. Will not translate by default. Default:Nonescale (
Union[Tensor,Tuple[float,float],Tuple[float,float,float,float],None], optional) – scaling factor interval. If (a, b) represents isotropic scaling, the scale is randomly sampled from the range a <= scale <= b. If (a, b, c, d), the scale is randomly sampled from the range a <= scale_x <= b, c <= scale_y <= d. Will keep original scale by default. Default:Noneshear (
Union[Tensor,float,Tuple[float,float],None], optional) – Range of degrees to select from. If float, a shear parallel to the x axis in the range (-shear, +shear) will be applied. If (a, b), a shear parallel to the x axis in the range (-shear, +shear) will be applied. If (a, b, c, d), then x-axis shear in (shear[0], shear[1]) and y-axis shear in (shear[2], shear[3]) will be applied. Will not apply shear by default. Default:Noneresample (
Union[str,int,Resample], optional) – resample mode from “nearest” (0) or “bilinear” (1). Default:Resample.BILINEAR.namepadding_mode (
Union[str,int,SamplePadding], optional) – padding mode from “torch.zeros” (0), “border” (1), “reflection” (2) or “fill” (3). Default:SamplePadding.ZEROS.namefill_value (
Union[float,int,Tensor,None], optional) – the value to be filled in the padding area when padding_mode=”fill”. Can be a float, int, or a torch.Tensor of shape (C) or (1). Default:Nonesame_on_batch (
bool, optional) – apply the same transformation across the batch. Default:Falsealign_corners (
bool, optional) – interpolation flag. Default:Falsep (
float, optional) – probability of applying the transformation. Default:0.5keepdim (
bool, optional) – whether to keep the output shape the same as input (True) or broadcast it to the batch form (False). Default:False
- Shape:
Input: \((C, H, W)\) or \((B, C, H, W)\), Optional: \((B, 3, 3)\)
Output: \((B, C, H, W)\)
Note
This function internally uses
kornia.geometry.transform.warp_affine().Examples
>>> import torch >>> rng = torch.manual_seed(0) >>> input = torch.rand(1, 1, 3, 3) >>> aug = RandomAffine((-15., 20.), p=1.) >>> out = aug(input) >>> out, aug.transform_matrix (tensor([[[[0.3961, 0.7310, 0.1574], [0.1781, 0.3074, 0.5648], [0.4804, 0.8379, 0.4234]]]]), tensor([[[ 0.9923, -0.1241, 0.1319], [ 0.1241, 0.9923, -0.1164], [ 0.0000, 0.0000, 1.0000]]])) >>> aug.inverse(out) tensor([[[[0.3890, 0.6573, 0.1865], [0.2063, 0.3074, 0.5459], [0.3892, 0.7896, 0.4224]]]]) >>> input tensor([[[[0.4963, 0.7682, 0.0885], [0.1320, 0.3074, 0.6341], [0.4901, 0.8964, 0.4556]]]])
- To apply the exact augmenation again, you may take the advantage of the previous parameter state:
>>> input = torch.randn(1, 3, 32, 32) >>> aug = RandomAffine((-15., 20.), p=1.) >>> (aug(input) == aug(input, params=aug._params)).all() tensor(True)
- class kornia.augmentation.RandomCrop(size, padding=None, pad_if_needed=False, fill=0, padding_mode='constant', resample=Resample.BILINEAR.name, same_on_batch=False, align_corners=True, p=1.0, keepdim=False, cropping_mode='slice')[source]#
Crop random patches of a torch.Tensor image on a given size.
- Parameters:
size (
Tuple[int,int]) – Desired output size (out_h, out_w) of the crop. Must be Tuple[int, int], then out_h = size[0], out_w = size[1].padding (
Union[int,Tuple[int,int],Tuple[int,int,int,int],None], 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 F.pad left, top, right, bottom borders respectively. If a sequence of length 2 is provided, it is used to F.pad left/right, top/bottom borders, respectively. Default:Nonepad_if_needed (
Optional[bool], optional) – It will F.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. Default:Falsefill (
int, optional) – 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. Default:0padding_mode (
str, optional) – Type of padding. Should be: constant, reflect, replicate. Default:"constant"resample (
Union[str,int,Resample], optional) – the interpolation mode. Default:Resample.BILINEAR.namesame_on_batch (
bool, optional) – apply the same transformation across the batch. Default:Falsealign_corners (
bool, optional) – interpolation flag. Default:Truep (
float, optional) – probability of applying the transformation for the whole batch. Default:1.0keepdim (
bool, optional) – whether to keep the output shape the same as input (True) or broadcast it to the batch form (False). Default:Falsecropping_mode (
str, optional) – The used algorithm to crop.slicewill use advanced slicing to extract the torch.Tensor based on the sampled indices.resamplewill use warp_affine using the affine transformation to extract and resize at once. Use slice for efficiency, or resample for proper differentiability. Default:"slice"
- Shape:
Input: \((C, H, W)\) or \((B, C, H, W)\), Optional: \((B, 3, 3)\)
Output: \((B, C, out_h, out_w)\)
Note
Input torch.Tensor must be float and normalized into [0, 1] for the best differentiability support. Additionally, this function accepts another transformation torch.Tensor (\((B, 3, 3)\)), then the applied transformation will be merged int to the input transformation torch.Tensor and returned.
Examples
>>> import torch >>> _ = torch.manual_seed(0) >>> inputs = torch.arange(1*1*3*3.).view(1, 1, 3, 3) >>> aug = RandomCrop((2, 2), p=1., cropping_mode="resample") >>> out = aug(inputs) >>> out tensor([[[[3., 4.], [6., 7.]]]]) >>> aug.inverse(out, padding_mode="replicate") tensor([[[[3., 4., 4.], [3., 4., 4.], [6., 7., 7.]]]])
- To apply the exact augmenation again, you may take the advantage of the previous parameter state:
>>> input = torch.randn(1, 3, 32, 32) >>> aug = RandomCrop((2, 2), p=1., cropping_mode="resample") >>> (aug(input) == aug(input, params=aug._params)).all() tensor(True)
- class kornia.augmentation.RandomElasticTransform(kernel_size=(63, 63), sigma=(32.0, 32.0), alpha=(1.0, 1.0), align_corners=False, resample=Resample.BILINEAR.name, padding_mode='zeros', same_on_batch=False, p=0.5, keepdim=False)[source]#
Add random elastic transformation to a torch.Tensor image.
- Parameters:
kernel_size (
Tuple[int,int], optional) – the size of the Gaussian kernel. Default:(63, 63)sigma (
Tuple[float,float], optional) – The standard deviation of the Gaussian in the y and x directions, respectively. Larger sigma results in smaller pixel displacements. Default:(32.0, 32.0)alpha (
Tuple[float,float], optional) – The scaling factor that controls the intensity of the deformation in the y and x directions, respectively. Default:(1.0, 1.0)align_corners (
bool, optional) – Interpolation flag used by grid_sample. Default:Falseresample (
Union[str,int,Resample], optional) – Interpolation mode used by grid_sample. Either ‘nearest’ (0) or ‘bilinear’ (1). Default:Resample.BILINEAR.namepadding_mode (
str, optional) – The padding used by`grid_sample`. Either ‘torch.zeros’, ‘border’ or ‘refection’. Default:"zeros"same_on_batch (
bool, optional) – apply the same transformation across the batch. Default:Falsep (
float, optional) – probability of applying the transformation. Default:0.5keepdim (
bool, optional) – whether to keep the output shape the same as input (True) or broadcast it to the batch form (False). Default:False
Note
This function internally uses
kornia.geometry.transform.elastic_transform2d().Examples
>>> import torch >>> img = torch.ones(1, 1, 2, 2) >>> out = RandomElasticTransform()(img) >>> out.shape torch.Size([1, 1, 2, 2])
- To apply the exact augmenation again, you may take the advantage of the previous parameter state:
>>> input = torch.randn(1, 3, 32, 32) >>> aug = RandomElasticTransform(p=1.) >>> (aug(input) == aug(input, params=aug._params)).all() tensor(True)
- class kornia.augmentation.RandomErasing(scale=(0.02, 0.33), ratio=(0.3, 3.3), value=0.0, same_on_batch=False, p=0.5, keepdim=False)[source]#
Erase a random rectangle of a torch.Tensor image according to a probability p value.
The operator removes image parts and fills them with zero values at a selected rectangle for each of the images in the batch.
The rectangle will have an area equal to the original image area multiplied by a value uniformly sampled between the range [scale[0], scale[1]) and an aspect ratio sampled between [ratio[0], ratio[1])
- Parameters:
scale (
Union[Tensor,Tuple[float,float]], optional) – range of proportion of erased area against input image. Default:(0.02, 0.33)ratio (
Union[Tensor,Tuple[float,float]], optional) – range of aspect ratio of erased area. Default:(0.3, 3.3)value (
float, optional) – the value to fill the erased area. Default:0.0same_on_batch (
bool, optional) – apply the same transformation across the batch. Default:Falsep (
float, optional) – probability that the random erasing operation will be performed. Default:0.5keepdim (
bool, optional) – whether to keep the output shape the same as input (True) or broadcast it to the batch form (False). Default:False
- Shape:
Input: \((C, H, W)\) or \((B, C, H, W)\), Optional: \((B, 3, 3)\)
Output: \((B, C, H, W)\)
Note
Input torch.Tensor must be float and normalized into [0, 1] for the best differentiability support. Additionally, this function accepts another transformation torch.Tensor (\((B, 3, 3)\)), then the applied transformation will be merged int to the input transformation torch.Tensor and returned.
Examples
>>> rng = torch.manual_seed(0) >>> inputs = torch.ones(1, 1, 3, 3) >>> aug = RandomErasing((.4, .8), (.3, 1/.3), p=0.5) >>> aug(inputs) tensor([[[[1., 0., 0.], [1., 0., 0.], [1., 0., 0.]]]])
- To apply the exact augmenation again, you may take the advantage of the previous parameter state:
>>> input = torch.randn(1, 3, 32, 32) >>> aug = RandomErasing((.4, .8), (.3, 1/.3), p=1.) >>> (aug(input) == aug(input, params=aug._params)).all() tensor(True)
- class kornia.augmentation.RandomFisheye(center_x, center_y, gamma, same_on_batch=False, p=0.5, keepdim=False)[source]#
Add random camera radial distortion.
- Parameters:
center_x (
Tensor) – Ranges to sample respect to x-coordinate center with shape (2,).center_y (
Tensor) – Ranges to sample respect to y-coordinate center with shape (2,).gamma (
Tensor) – Ranges to sample for the gamma values respect to optical center with shape (2,).same_on_batch (
bool, optional) – apply the same transformation across the batch. Default:Falsep (
float, optional) – probability of applying the transformation. Default:0.5keepdim (
bool, optional) – whether to keep the output shape the same as input (True) or broadcast it to the batch form (False). Default:False
Examples
>>> import torch >>> img = torch.ones(1, 1, 2, 2) >>> center_x = torch.tensor([-.3, .3]) >>> center_y = torch.tensor([-.3, .3]) >>> gamma = torch.tensor([.9, 1.]) >>> out = RandomFisheye(center_x, center_y, gamma)(img) >>> out.shape torch.Size([1, 1, 2, 2])
- To apply the exact augmenation again, you may take the advantage of the previous parameter state:
>>> input = torch.randn(1, 3, 32, 32) >>> aug = RandomFisheye(center_x, center_y, gamma, p=1.) >>> (aug(input) == aug(input, params=aug._params)).all() tensor(True)
- class kornia.augmentation.RandomHorizontalFlip(p=0.5, p_batch=1.0, same_on_batch=False, keepdim=False)[source]#
Apply a random horizontal flip to a torch.Tensor image or a batch of torch.Tensor images.
The flip is applied with a given probability.
Input should be a torch.Tensor of shape (C, H, W) or a batch of tensors \((B, C, H, W)\). If Input is a tuple it is assumed that the first element contains the aforementioned tensors and the second, the corresponding transformation matrix that has been applied to them. In this case the module will Horizontally flip the tensors and torch.cat the corresponding transformation matrix to the previous one. This is especially useful when using this functionality as part of an
nn.Sequentialmodule.- Parameters:
p (
float, optional) – probability of the image being flipped. Default:0.5same_on_batch (
bool, optional) – apply the same transformation across the batch. Default:Falsekeepdim (
bool, optional) – whether to keep the output shape the same as input (True) or broadcast it to the batch form (False). Default:False
- Shape:
Input: \((C, H, W)\) or \((B, C, H, W)\), Optional: \((B, 3, 3)\)
Output: \((B, C, H, W)\)
Note
This function internally uses
kornia.geometry.transform.hflip().Examples
>>> import torch >>> input = torch.tensor([[[[0., 0., 0.], ... [0., 0., 0.], ... [0., 1., 1.]]]]) >>> seq = RandomHorizontalFlip(p=1.0) >>> seq(input), seq.transform_matrix (tensor([[[[0., 0., 0.], [0., 0., 0.], [1., 1., 0.]]]]), tensor([[[-1., 0., 2.], [ 0., 1., 0.], [ 0., 0., 1.]]])) >>> seq.inverse(seq(input)).equal(input) True
- To apply the exact augmenation again, you may take the advantage of the previous parameter state:
>>> input = torch.randn(1, 3, 32, 32) >>> seq = RandomHorizontalFlip(p=1.0) >>> (seq(input) == seq(input, params=seq._params)).all() tensor(True)
- class kornia.augmentation.RandomPerspective(distortion_scale=0.5, resample=Resample.BILINEAR.name, same_on_batch=False, align_corners=False, p=0.5, keepdim=False, sampling_method='basic')[source]#
Apply a random perspective transformation to an image torch.Tensor with a given probability.
- Parameters:
distortion_scale (
Union[Tensor,float], optional) – the degree of distortion, ranged from 0 to 1. Default:0.5resample (
Union[str,int,Resample], optional) – the interpolation method to use. Default:Resample.BILINEAR.namesame_on_batch (
bool, optional) – apply the same transformation across the batch. Default: False.align_corners (
bool, optional) – interpolation flag. Default:Falsep (
float, optional) – probability of the image being perspectively transformed. Default:0.5keepdim (
bool, optional) – whether to keep the output shape the same as input (True) or broadcast it to the batch form (False). Default:Falsesampling_method (
str, optional) –'basic'|'area_preserving'. Default:'basic'If'basic', samples by translating the image corners randomly inwards. If'area_preserving', samples by randomly translating the image corners in any direction. Preserves area on average. See https://arxiv.org/abs/2104.03308 for further details.
- Shape:
Input: \((C, H, W)\) or \((B, C, H, W)\), Optional: \((B, 3, 3)\)
Output: \((B, C, H, W)\)
Note
This function internally uses
kornia.geometry.transform.warp_pespective().Examples
>>> rng = torch.manual_seed(0) >>> inputs= torch.tensor([[[[1., 0., 0.], ... [0., 1., 0.], ... [0., 0., 1.]]]]) >>> aug = RandomPerspective(0.5, p=0.5) >>> out = aug(inputs) >>> out tensor([[[[0.0000, 0.2289, 0.0000], [0.0000, 0.4800, 0.0000], [0.0000, 0.0000, 0.0000]]]]) >>> aug.inverse(out) tensor([[[[0.0500, 0.0961, 0.0000], [0.2011, 0.3144, 0.0000], [0.0031, 0.0130, 0.0053]]]])
- To apply the exact augmenation again, you may take the advantage of the previous parameter state:
>>> input = torch.randn(1, 3, 32, 32) >>> aug = RandomPerspective(0.5, p=1.) >>> (aug(input) == aug(input, params=aug._params)).all() tensor(True)
- class kornia.augmentation.RandomResizedCrop(size, scale=(0.08, 1.0), ratio=(3.0 / 4.0, 4.0 / 3.0), resample=Resample.BILINEAR.name, same_on_batch=False, align_corners=True, p=1.0, keepdim=False, cropping_mode='slice')[source]#
Crop random patches in an image torch.Tensor and resizes to a given size.
- Parameters:
size (
Tuple[int,int]) – Desired output size (out_h, out_w) of each edge. Must be Tuple[int, int], then out_h = size[0], out_w = size[1].scale (
Union[Tensor,Tuple[float,float]], optional) – range of size of the origin size cropped. Default:(0.08, 1.0)ratio (
Union[Tensor,Tuple[float,float]], optional) – range of aspect ratio of the origin aspect ratio cropped. Default:(3.0 / 4.0, 4.0 / 3.0)resample (
Union[str,int,Resample], optional) – the interpolation mode. Default:Resample.BILINEAR.namesame_on_batch (
bool, optional) – apply the same transformation across the batch. Default:Falsealign_corners (
bool, optional) – interpolation flag. Default:Truep (
float, optional) – probability of the augmentation been applied. Default:1.0keepdim (
bool, optional) – whether to keep the output shape the same as input (True) or broadcast it to the batch form (False). Default:Falsecropping_mode (
str, optional) – The used algorithm to crop.slicewill use advanced slicing to extract the torch.Tensor based on the sampled indices.resamplewill use warp_affine using the affine transformation to extract and resize at once. Use slice for efficiency, or resample for proper differentiability. Default:"slice"
- Shape:
Input: \((C, H, W)\) or \((B, C, H, W)\), Optional: \((B, 3, 3)\)
Output: \((B, C, out_h, out_w)\)
Note
Input torch.Tensor must be float and normalized into [0, 1] for the best differentiability support. Additionally, this function accepts another transformation torch.Tensor (\((B, 3, 3)\)), then the applied transformation will be merged int to the input transformation torch.Tensor and returned.
Example
>>> rng = torch.manual_seed(0) >>> inputs = torch.tensor([[[0., 1., 2.], ... [3., 4., 5.], ... [6., 7., 8.]]]) >>> aug = RandomResizedCrop(size=(3, 3), scale=(3., 3.), ratio=(2., 2.), p=1., cropping_mode="resample") >>> out = aug(inputs) >>> out tensor([[[[1.0000, 1.5000, 2.0000], [4.0000, 4.5000, 5.0000], [7.0000, 7.5000, 8.0000]]]]) >>> aug.inverse(out, padding_mode="border") tensor([[[[1., 1., 2.], [4., 4., 5.], [7., 7., 8.]]]])
- To apply the exact augmenation again, you may take the advantage of the previous parameter state:
>>> input = torch.randn(1, 3, 32, 32) >>> aug = RandomResizedCrop(size=(3, 3), scale=(3., 3.), ratio=(2., 2.), p=1., cropping_mode="resample") >>> (aug(input) == aug(input, params=aug._params)).all() tensor(True)
- class kornia.augmentation.RandomRotation90(times, resample=Resample.BILINEAR.name, same_on_batch=False, align_corners=True, p=0.5, keepdim=False)[source]#
Apply a random 90 * n degree rotation to a torch.Tensor image or a batch of torch.Tensor images.
- Parameters:
times (
tuple[int,int]) – the range of n times 90 degree rotation needs to be applied.resample (
Union[str,int,Resample], optional) – Default: the interpolation mode.same_on_batch (
bool, optional) – apply the same transformation across the batch. Default:Falsealign_corners (
bool, optional) – interpolation flag. Default:Truep (
float, optional) – probability of applying the transformation. Default:0.5keepdim (
bool, optional) – whether to keep the output shape the same as input (True) or broadcast it to the batch form (False). Default:False
- Shape:
Input: \((C, H, W)\) or \((B, C, H, W)\), Optional: \((B, 3, 3)\)
Output: \((B, C, H, W)\)
Note
This function internally uses
kornia.geometry.transform.affine(). This version is relatively slow as it operates based on affine transformations.Examples
>>> rng = torch.manual_seed(1) >>> input = torch.tensor([[1., 0., 0., 2.], ... [0., 0., 0., 0.], ... [0., 1., 2., 0.], ... [0., 0., 1., 2.]]) >>> aug = RandomRotation90(times=(1, 1), p=1.) >>> out = aug(input) >>> out.round() # rounded: the warp leaves float32 noise around 1e-7 tensor([[[[2., 0., 0., 2.], [0., 0., 2., 1.], [0., 0., 1., 0.], [1., 0., 0., 0.]]]]) >>> aug.transform_matrix.round(decimals=4) + 0. # + 0. turns -0. into 0. tensor([[[ 0., 1., 0.], [-1., 0., 3.], [ 0., 0., 1.]]]) >>> inv = aug.inverse(out)
- To apply the exact augmenation again, you may take the advantage of the previous parameter state:
>>> input = torch.randn(1, 3, 32, 32) >>> aug = RandomRotation90(times=(-1, 1), p=1.) >>> (aug(input) == aug(input, params=aug._params)).all() tensor(True)
- class kornia.augmentation.RandomRotation(degrees, resample=Resample.BILINEAR.name, same_on_batch=False, align_corners=True, p=0.5, keepdim=False)[source]#
Apply a random rotation to a torch.Tensor image or a batch of torch.Tensor images given an amount of degrees.
- Parameters:
degrees (
Union[Tensor,float,Tuple[float,float],List[float]]) – range of degrees to select from. If degrees is a number the range of degrees to select from will be (-degrees, +degrees).resample (
Union[str,int,Resample], optional) – Default: the interpolation mode.same_on_batch (
bool, optional) – apply the same transformation across the batch. Default:Falsealign_corners (
bool, optional) – interpolation flag. Default:Truep (
float, optional) – probability of applying the transformation. Default:0.5keepdim (
bool, optional) – whether to keep the output shape the same as input (True) or broadcast it to the batch form (False). Default:False
- Shape:
Input: \((C, H, W)\) or \((B, C, H, W)\), Optional: \((B, 3, 3)\)
Output: \((B, C, H, W)\)
Note
This function internally uses
kornia.geometry.transform.affine().Examples
>>> rng = torch.manual_seed(0) >>> input = torch.tensor([[1., 0., 0., 2.], ... [0., 0., 0., 0.], ... [0., 1., 2., 0.], ... [0., 0., 1., 2.]]) >>> aug = RandomRotation(degrees=45.0, p=1.) >>> out = aug(input) >>> out tensor([[[[0.9824, 0.0088, 0.0000, 1.9649], [0.0000, 0.0029, 0.0000, 0.0176], [0.0029, 1.0000, 1.9883, 0.0000], [0.0000, 0.0088, 1.0117, 1.9649]]]]) >>> aug.transform_matrix tensor([[[ 1.0000, -0.0059, 0.0088], [ 0.0059, 1.0000, -0.0088], [ 0.0000, 0.0000, 1.0000]]]) >>> inv = aug.inverse(out)
- To apply the exact augmenation again, you may take the advantage of the previous parameter state:
>>> input = torch.randn(1, 3, 32, 32) >>> aug = RandomRotation(degrees=45.0, p=1.) >>> (aug(input) == aug(input, params=aug._params)).all() tensor(True)
- class kornia.augmentation.RandomShear(shear, resample=Resample.BILINEAR.name, same_on_batch=False, align_corners=False, padding_mode=SamplePadding.ZEROS.name, p=0.5, keepdim=False)[source]#
Apply a random 2D shear transformation to a torch.Tensor image.
The transformation is computed so that the image center is kept invariant.
- Parameters:
shear (
Union[Tensor,float,Tuple[float,float],Tuple[float,float,float,float]]) – Range of degrees to select from. If float, a shear parallel to the x axis in the range (-shear, +shear) will be applied. If (a, b), a shear parallel to the x axis in the range (-shear, +shear) will be applied. If (a, b, c, d), then x-axis shear in (shear[0], shear[1]) and y-axis shear in (shear[2], shear[3]) will be applied. Will not apply shear by default.resample (
Union[str,int,Resample], optional) – resample mode from “nearest” (0) or “bilinear” (1). Default:Resample.BILINEAR.namepadding_mode (
Union[str,int,SamplePadding], optional) – padding mode from “torch.zeros” (0), “border” (1) or “reflection” (2). Default:SamplePadding.ZEROS.namesame_on_batch (
bool, optional) – apply the same transformation across the batch. Default:Falsealign_corners (
bool, optional) – interpolation flag. Default:Falsep (
float, optional) – probability of applying the transformation. Default:0.5keepdim (
bool, optional) – whether to keep the output shape the same as input (True) or broadcast it to the batch form (False). Default:False
- Shape:
Input: \((C, H, W)\) or \((B, C, H, W)\)
Output: \((B, C, H, W)\)
Note
This function internally uses
kornia.geometry.transform.warp_affine().Examples
>>> import torch >>> rng = torch.manual_seed(0) >>> input = torch.rand(1, 1, 3, 3) >>> aug = RandomShear((-5., 2., 5., 10.), p=1.) >>> out = aug(input) >>> out, aug.transform_matrix (tensor([[[[0.4403, 0.7614, 0.1516], [0.1753, 0.3074, 0.6127], [0.4438, 0.8924, 0.4061]]]]), tensor([[[ 1.0000, 0.0100, -0.0100], [-0.1183, 0.9988, 0.1194], [ 0.0000, 0.0000, 1.0000]]])) >>> aug.inverse(out) tensor([[[[0.4045, 0.7577, 0.1393], [0.2071, 0.3074, 0.5582], [0.3958, 0.8868, 0.4265]]]])
- To apply the exact augmenation again, you may take the advantage of the previous parameter state:
>>> input = torch.randn(1, 3, 32, 32) >>> aug = RandomShear((-15., 20.), p=1.) >>> (aug(input) == aug(input, params=aug._params)).all() tensor(True)
- class kornia.augmentation.RandomThinPlateSpline(scale=0.2, align_corners=False, padding_mode=SamplePadding.ZEROS.name, same_on_batch=False, p=0.5, keepdim=False)[source]#
Add random noise to the Thin Plate Spline algorithm.
- Parameters:
scale (
float, optional) – the scale factor to apply to the destination points. Default:0.2align_corners (
bool, optional) – Interpolation flag used bygrid_sample. Default:Falsemode – Interpolation mode used by grid_sample. Either ‘bilinear’ or ‘nearest’.
same_on_batch (
bool, optional) – apply the same transformation across the batch. Default:Falsep (
float, optional) – probability of applying the transformation. Default:0.5keepdim (
bool, optional) – whether to keep the output shape the same as input (True) or broadcast it to the batch form (False). Default:False
Note
This function internally uses
kornia.geometry.transform.warp_image_tps().Examples
>>> img = torch.ones(1, 1, 2, 2) >>> out = RandomThinPlateSpline()(img) >>> out.shape torch.Size([1, 1, 2, 2])
- To apply the exact augmenation again, you may take the advantage of the previous parameter state:
>>> input = torch.randn(1, 3, 32, 32) >>> aug = RandomThinPlateSpline(p=1.) >>> (aug(input) == aug(input, params=aug._params)).all() tensor(True)
- class kornia.augmentation.RandomVerticalFlip(p=0.5, p_batch=1.0, same_on_batch=False, keepdim=False)[source]#
Apply a random vertical flip to a torch.Tensor image or a batch of torch.Tensor images with a given probability.
- Parameters:
p (
float, optional) – probability of the image being flipped. Default:0.5same_on_batch (
bool, optional) – apply the same transformation across the batch. Default:Falsekeepdim (
bool, optional) – whether to keep the output shape the same as input (True) or broadcast it to the batch form (False). Default:False
- Shape:
Input: \((C, H, W)\) or \((B, C, H, W)\), Optional: \((B, 3, 3)\)
Output: \((B, C, H, W)\)
Note
This function internally uses
kornia.geometry.transform.vflip().Examples
>>> import torch >>> input = torch.tensor([[[[0., 0., 0.], ... [0., 0., 0.], ... [0., 1., 1.]]]]) >>> seq = RandomVerticalFlip(p=1.0) >>> seq(input), seq.transform_matrix (tensor([[[[0., 1., 1.], [0., 0., 0.], [0., 0., 0.]]]]), tensor([[[ 1., 0., 0.], [ 0., -1., 2.], [ 0., 0., 1.]]])) >>> seq.inverse(seq(input)).equal(input) True
- To apply the exact augmenation again, you may take the advantage of the previous parameter state:
>>> input = torch.randn(1, 3, 32, 32) >>> seq = RandomVerticalFlip(p=1.0) >>> (seq(input) == seq(input, params=seq._params)).all() tensor(True)
Resize#
Deterministic resizing operators, shape-agnostic for 2D and 3D tensors.
- class kornia.augmentation.LongestMaxSize(max_size, resample=Resample.BILINEAR.name, align_corners=True, p=1.0)[source]#
Rescale an image so that maximum side is equal to max_size, keeping the aspect ratio of the initial image.
- Parameters:
max_size (
int) – maximum size of the image after the transformation.
- class kornia.augmentation.Resize(size, side='short', resample=Resample.BILINEAR.name, align_corners=True, antialias=False, p=1.0, keepdim=False)[source]#
Resize to size.
- Parameters:
size (
Union[int,Tuple[int,int]]) – Size (h, w) in pixels of the resized region or just one side.side (
str, optional) – Which side to resize, if size is only of type int. Default:"short"resample (
Union[str,int,Resample], optional) – Resampling mode. Default:Resample.BILINEAR.namealign_corners (
bool, optional) – interpolation flag. Default:Trueantialias (
bool, optional) – if True, then image will be filtered with Gaussian before downscaling. No effect for upscaling. Default:Falsep (
float, optional) – probability of the augmentation been applied. Default:1.0keepdim (
bool, optional) – whether to keep the output shape the same as input (True) or broadcast it to the batch form (False). Default:False
- class kornia.augmentation.SmallestMaxSize(max_size, resample=Resample.BILINEAR.name, align_corners=True, p=1.0)[source]#
Rescale an image so that minimum side is equal to max_size, keeping the aspect ratio of the initial image.
- Parameters:
max_size (
int) – maximum size of the image after the transformation.