2D geometric transforms#
Spatial transformations and erasing operations. Matrix, inverse and annotation support depend on the class
and configuration. Slice-mode crops reject inversion; non-rigid transforms do not carry boxes or keypoints
along with the image (#4420). RandomErasing changes image
and mask values without moving coordinates. See
AugmentationSequential for the supported data-key paths and their limitations.
- 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().- Convention:
See
GeometricAugmentationBase2Dfor coordinates, defaults and inverse.sizeaccepts an integer for a square crop or an(height, width)tuple, unlikeRandomCropandRandomResizedCrop(#4417).cropping_mode="slice"indexes the input and returns a copy, so writing to the result leaves the input unchanged.cropping_mode="resample"warps with the configuredresampleandalign_corners. Only resample mode supportsinverse(), which resamples onto the original canvas (zero padding unless the call passespadding_mode) and cannot restore discarded data.
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().- Convention:
See
GeometricAugmentationBase2Dfor coordinates, defaults and inverse. This operation always runs and pads on the right and bottom, usingpad_modeandpad_value. A target smaller than the input crops that axis instead, so a target can pad one axis while cropping the other (#4410).inverse()slices to the prior input size. It exactly removes right/bottom padding, but after a crop it returns the cropped result unchanged rather than restoring discarded pixels. The transformation matrix is identity, so keypoints and boxes are neither moved nor clipped when cropping puts them outside the canvas.
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)\)
- Convention:
See
GeometricAugmentationBase2Dfor coordinates, defaults and inverse.translate=(a, b)samples pixel offsets from[-a W, a W]and[-b H, b H]; positive offsets move content right and down.a positive
degreesrotates the displayed image clockwise, unlikeRandomRotation(Conventions & Pitfalls, #4408).sheardraws degrees, converted to radians and applied about the image centre with the sign and the x-then-y order ofget_shear_matrix2d(), asAffineapplies itsshear.
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 (
Union[float,Sequence[float]], optional) – Pixel fill value for constant fill. Default is 0. A sequence gives one value per channel, so it must be as long as the input’s channel dimension. This value is only used when the padding_mode is constant, and a sequence requires it. 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.
- Convention:
See
GeometricAugmentationBase2Dfor coordinates, defaults and inverse.sizeis an(height, width)tuple; a bare integer raises, unlikeCenterCrop(#4417).pselects or skips the whole batch together; a skipped batch is returned unchanged and unpadded.transform_matrixmaps original input coordinates to the crop, including the translation frompaddingorpad_if_needed.Explicit
paddingis applied before sampling, in(left, top, right, bottom)order after its scalar or two-value shorthand is expanded.pad_if_needed=Truepads each side by the maximum of that padding and the crop-minus-input size on its axis, so asymmetric explicit padding can stay asymmetric. InAugmentationSequential, a per-channelfillpads only the image and the mask is padded with zero unlessextra_args[DataKey.MASK]sets itsfill; a scalarfillapplies to both.With
pad_if_needed=False, an oversized request does not raise: slice mode resizes the available slice, and resample mode uses a mis-scaled warp that rescales both matrix axes and can blend in zero padding (#4414). Slice mode scales only the matrix axes along which the request exceeds the canvas, as its resize does, and on the resize’s half-pixel grid:x' = (x + 0.5) * W_out / W_in - 0.5along a stretched axis (likewise fory). The scale is computed against the padded canvas, so a crop that fits after explicit padding gets no scale correction.Slice mode uses
crop_by_indices()with its own defaults and ignores this class’sresampleandalign_corners. Resample mode uses the configured interpolation andalign_cornersand maps constant, replicate and reflect pre-padding to zero, border and reflection sampler padding. Only resample mode supportsinverse(), which removes the pre-crop padding but cannot restore cropped or interpolated content. For an empty batch,forward_input_shaperecords the original, unpadded canvas for inverse replay.
Note
Compiled slice-mode interpolation matches eager execution to floating-point tolerance, not bitwise. Eager execution retains native slicing and resizing for performance; the tensorized compiled path avoids recompilation as crop coordinates change.
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.
- Convention:
This is a dense backward sampling field with no
transform_matrixorinverse; spatial labels in containers have limitations (#4420). Noise has shape(B, 2, H, W)with channels(x, y);alphascales displacement in normalized sampling coordinates, whereaskernel_sizeandsigmause(y, x)order. The displaced grid is clamped to[-1, 1]before sampling. Zeroalphais an identity warp up to interpolation roundoff for eitheralign_corners. Its defaults and thealign_cornerssplit across classes (#4412) are listed onGeometricAugmentationBase2D.
- Parameters:
kernel_size (
Tuple[int,int], optional) – the size of the Gaussian kernel, in the y and x directions, respectively. 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 x and y directions, respectively – the opposite order tokernel_sizeandsigma. 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 bygrid_sample. Either ‘zeros’, ‘border’ or ‘reflection’. 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.
See the Convention block on
IntensityAugmentationBase2D.The operator removes image parts and fills them with
valueat a selected rectangle for each of the images in the batch.The rectangle targets an area equal to the original image area multiplied by a value sampled from
scale, with an aspect ratio sampled fromratio.- 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)\)
- Convention:
scaleis the fraction of the image area the rectangle targets andratiois its height over its width, so a ratio above1targets a tall box and below1a wide one. The box is rounded to whole pixels and then clamped to[1, H] x [1, W], so it is never empty and never larger than the image, and the erased area can miss the target in either direction:scale=(0.25, 0.25)withratio=(3.0, 3.0)on a10 x 20image erases10 x 4 = 40pixels of the 50 asked for, whilescale=(0.0, 0.0)still erases one pixel.when
ratiostraddles1the draw is not uniform over the interval: a fair coin picks between[ratio[0], 1]and[1, ratio[1]], so at the defaultratio=(0.3, 3.3)half the draws are above1.the erased region is the half-open rectangle
[ys, ys + h) x [xs, xs + w)in pixels, recorded as_params["ys"],["xs"],["heights"]and["widths"].the erased pixels carry the literal
value, which has to lie in[0, 1]– the parameter generator rejects anything else at construction. Every other pixel is carried through unclamped, so every pixel outside the box keeps the input’s range.inside
AugmentationSequentialamaskdata key is erased in the same rectangle, but the mask is filled with0whatevervalueis.
Note
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.
- Convention:
Coordinates are normalized to
[-1, 1]with corner pixel centres at the endpoints. At output coordinate(x, y), letrbe its distance from the sampled centre. The input sampling coordinate is(x * (1 + r**gamma), y * (1 + r**gamma)): the centre changes the distance, but scaling is about the coordinate origin. Sampling is bilinear with zero padding andalign_corners=True; these settings are fixed. There is notransform_matrixorinverse; spatial labels in containers have limitations (#4420).
- 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)\)
- Convention:
A selected flip maps
(x, y)to(W - 1 - x, y)with the exact discretehflip(), without resampling; seeGeometricAugmentationBase2Dfor the shared contract.
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)\)
- Convention:
See
GeometricAugmentationBase2Dfor coordinates, defaults and inverse. The source corners are(0, 0),(W - 1, 0),(W - 1, H - 1)and(0, H - 1);transform_matrixmaps them to the sampled destination corners.basicsamples each corner inward by at mostdistortion_scale * (W, H) / 2.area_preservingsamples each coordinate in both directions over the same extent.with
distortion_scale=0andH > 1,W > 1, the warp is an identity up to numerical precision for eitheralign_corners.a spatial dimension of 1 is not distorted: its source corners span a unit extent instead of
0and its offsets are zero, so the single row or column maps onto itself and only the other axis is warped. Atdistortion_scale=0this is the identity.
Note
This function internally uses
kornia.geometry.transform.warp_perspective().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.2795, 0.3852, 0.0000], [0.0000, 0.6243, 0.0000], [0.0000, 0.0000, 0.0000]]]]) >>> aug.inverse(out) tensor([[[[0.3417, 0.3022, 0.0436], [0.3767, 0.4769, 0.1879], [0.1434, 0.1958, 0.1091]]]])
- 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.
- Convention:
See
GeometricAugmentationBase2Dfor coordinates, defaults and inverse.sizeis an(height, width)tuple; a bare integer raises, unlikeCenterCrop(#4417).pselects or skips the whole batch together. Within a selected batch, the generator tries ten candidate crops per image, sampling area fractions fromscaleand width/height ratios fromratio(shared withsame_on_batch=True), and resizes the first that fits to the requested output size. As in torchvision’sget_params, a candidate may equal the input, and when none fits the fallback keeps an input whose width/height is withinratiowhole, or else its full width (input narrower thanmin(ratio)) or its full height (wider thanmax(ratio)). Soscale=(1.0, 1.0)keeps an input withinratiowhole, except that a candidate rounded a pixel or two short on the longer side also fits (8x6 gives 7x6 in about 18% of draws, as in torchvision). Unlike torchvision, which centres the fallback crop, it is placed at a random position like any other crop.Both cropping modes use the configured interpolation and
align_corners. Slice mode ignoresalign_cornersforresample="nearest". With bilinear or bicubic interpolation, each mode’s image follows itstransform_matrix, and atalign_corners=Falsethe two modes give different images: slice mode resizes the crop on the half-pixel grid,x' = (x - x0 + 0.5) * W_out / W_crop - 0.5for a crop starting at columnx0(likewise fory), while resample mode maps the crop’s corner pixel centres onto the output’s. Nearest sampling in slice mode uses the half-pixel grid and PyTorch’snearest-exactinterpolation. Only resample mode supportsinverse(), which resamples onto the original canvas and cannot recover discarded information.
Note
Compiled slice-mode interpolation matches eager execution to floating-point tolerance, not bitwise. Eager execution retains native slicing and resizing for performance; the tensorized compiled path avoids recompilation as crop coordinates change.
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([[[[3.0000, 4.0000, 5.0000], [4.5000, 5.5000, 6.5000], [6.0000, 7.0000, 8.0000]]]]) >>> aug.inverse(out, padding_mode="border") tensor([[[[3., 4., 5.], [3., 4., 5.], [6., 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)\)
- Convention:
See
GeometricAugmentationBase2Dfor coordinates, defaults and inverse.a positive rounded
timesrotates the displayed image counter-clockwise bytimes * 90degrees.timesis sampled from a continuous uniform range and rounded, and a non-square image keeps its(H, W)shape (#4409).
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)\)
- Convention:
See
GeometricAugmentationBase2Dfor coordinates, defaults and inverse.a positive sampled angle rotates the displayed image counter-clockwise, as
rotate()does and unlikeRandomAffine(Conventions & Pitfalls, #4408).
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)\)
- Convention:
See
GeometricAugmentationBase2Dfor coordinates, defaults and inverse.shearsupplies one or two degree ranges for the x and y shears; a scalar or pair supplies only x shear. The signs are those ofRandomAffine’sshear.
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.
- Convention:
Five control points use normalized
(x, y)coordinates:(-1, -1),(-1, 1),(1, -1),(1, 1), and(0, 0). Each destination coordinate receives uniform noise between-scaleandscale; zero scale leaves these points unchanged. Sampling is bilinear, with no interpolation-mode argument. Zero scale is an identity warp up to interpolation roundoff for eitheralign_corners. There is notransform_matrixorinverse; spatial labels in containers have limitations (#4420). Its defaults and thealign_cornerssplit across classes (#4412) are listed onGeometricAugmentationBase2D.
- Parameters:
scale (
float, optional) – the non-negative scale factor to apply to the destination points. Zero leaves the control points unchanged. Default:0.2align_corners (
bool, optional) – Interpolation flag used bygrid_sample. Default:Falsepadding_mode (
Union[str,int,SamplePadding], optional) – Padding used bygrid_sample: ‘zeros’, ‘border’ or ‘reflection’. Default:SamplePadding.ZEROS.namesame_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.RandomTranslate(translate_x=None, translate_y=None, 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 affine transformation to a torch.Tensor image.
- Parameters:
translate_x (
Union[Tensor,Tuple[float,float],None], optional) – tuple of maximum absolute fraction for horizontal translations. For example translate_x=(a, b), then horizontal shift is randomly sampled in the range img_width * a < dx < img_width * b Default:Nonetranslate_y (
Union[Tensor,Tuple[float,float],None], optional) – tuple of maximum absolute fraction for vertical translations. For example translate_y=(a, b), then vertical shift is randomly sampled in the range img_height * a < dy < img_height * b. 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) 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)\)
- Convention:
See
GeometricAugmentationBase2Dfor coordinates, defaults and inverse.translate_xandtranslate_yare independently sampled fractions ofWandH; positive values move content right and down. Omitting either range fixes that axis at zero.
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 = RandomTranslate((-0.2, 0.2), (-0.1, 0.1), p=1.) >>> out = aug(input) >>> out, aug.transform_matrix (tensor([[[[0.3897, 0.6847, 0.2314], [0.1384, 0.3297, 0.5771], [0.3749, 0.7565, 0.4780]]]]), tensor([[[ 1.0000, 0.0000, 0.1588], [ 0.0000, 1.0000, -0.0907], [ 0.0000, 0.0000, 1.0000]]])) >>> aug.inverse(out) tensor([[[[0.3969, 0.5571, 0.1770], [0.1930, 0.3910, 0.4591], [0.4113, 0.6812, 0.4096]]]])
- To apply the exact augmenation again, you may take the advantage of the previous parameter state:
>>> input = torch.randn(1, 3, 32, 32) >>> aug = RandomTranslate((-0.2, 0.2), (-0.1, 0.1), 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)\)
- Convention:
A selected flip maps
(x, y)to(x, H - 1 - y)with the exact discretevflip(), without resampling; seeGeometricAugmentationBase2Dfor the shared contract.
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.
- Convention:
See
Resizefor the common resize conventions. This behaves likeResize(max_size, side="long", antialias=False, keepdim=False);antialiasandkeepdimare not accepted constructor arguments. Its longest output side equalsmax_sizeand the other side is truncated while preserving aspect ratio.
- 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
- Convention:
See
GeometricAugmentationBase2Dfor coordinates, defaults and inverse. A tuplesizeis the exact(height, width)output. With an integer,sideselects which input side is set to that value while preserving aspect ratio:"short"(the default) selects the shortest side,"long"the longest,"vert"the height, and"horz"the width. The derived side is truncated toward zero; if it becomes zero, the resize raisesAssertionError(for example,Resize(4, side="long")orLongestMaxSize(4)on a 1-by-10 image). This class useskornia.geometry.transform.resize();align_cornersis forwarded for bilinear and bicubic sampling, andtransform_matrixfollows the grid they sample: atalign_corners=Falseit is the half-pixel mapx' = (x + 0.5) * W_out / W_in - 0.5(likewise fory), and atTrueit maps the corner pixel centres onto each other. Nearest sampling uses the half-pixel grid and PyTorch’snearest-exactinterpolation.inverse()resamples to the prior canvas and cannot recover values discarded by a resize.
- 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.
- Convention:
See
Resizefor the common resize conventions. This behaves likeResize(max_size, side="short", antialias=False, keepdim=False);antialiasandkeepdimare not accepted constructor arguments. Its shortest output side equalsmax_sizeand the other side is truncated while preserving aspect ratio.