Implementation:Kornia Kornia Augmentation Transforms 2D
| Knowledge Sources | |
|---|---|
| Domains | Vision, Augmentation |
| Last Updated | 2026-02-09 15:00 GMT |
Overview
Concrete tool for defining differentiable 2D image augmentation transforms provided by Kornia's augmentation module.
Description
Kornia provides a rich set of 2D augmentation transforms inheriting from _BasicAugmentationBase. All transforms are nn.Module instances with probabilistic application, batch support, and transformation matrix tracking for invertibility.
Key transforms include:
- RandomAffine -- rotation, translation, scale, and shear in a single transform
- ColorJiggle -- random perturbation of brightness, contrast, saturation, and hue
- RandomPerspective -- perspective distortion with configurable distortion scale
- RandomBrightness -- dedicated brightness adjustment
Each transform accepts a probability parameter p that controls how often the transform is applied during training. When a transform is not applied (based on the probability check), the input is passed through unchanged.
Usage
Import specific transforms when building augmentation pipelines for training. Compose them using AugmentationSequential for multi-data-key support (images, masks, bounding boxes, keypoints).
Code Reference
Source Location
- Repository: kornia
- File: kornia/augmentation/_2d/ (individual transform files), base at kornia/augmentation/base.py
- Lines: base.py L51-571
Signature
# Base class
class _BasicAugmentationBase(nn.Module):
def __init__(
self,
p: float = 0.5,
p_batch: float = 1.0,
same_on_batch: bool = False,
keepdim: bool = False,
) -> None
# RandomAffine
RandomAffine(
degrees,
translate=None,
scale=None,
shear=None,
p=0.5,
same_on_batch=False,
keepdim=False,
)
# ColorJiggle
ColorJiggle(
brightness=0.,
contrast=0.,
saturation=0.,
hue=0.,
p=1.0,
same_on_batch=False,
keepdim=False,
)
Import
from kornia.augmentation import RandomAffine, ColorJiggle, RandomPerspective, RandomBrightness
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| degrees | float or tuple | Yes (RandomAffine) | Range of rotation degrees |
| translate | tuple | No | Range of horizontal and vertical translation as fraction of image size |
| scale | tuple | No | Range of scaling factor |
| shear | float or tuple | No | Range of shear degrees |
| brightness | float or tuple | No (ColorJiggle) | Brightness jitter range |
| contrast | float or tuple | No (ColorJiggle) | Contrast jitter range |
| saturation | float or tuple | No (ColorJiggle) | Saturation jitter range |
| hue | float or tuple | No (ColorJiggle) | Hue jitter range |
| p | float | No | Probability of applying the transform; default varies by transform |
Outputs
| Name | Type | Description |
|---|---|---|
| return | nn.Module | Instantiated transform module accepting (B, C, H, W) tensors, producing augmented (B, C, H, W) tensors |
Usage Examples
RandomAffine with Rotation and Scale
import torch
from kornia.augmentation import RandomAffine
# Create a RandomAffine transform with rotation [-30, 30] degrees and scale [0.8, 1.2]
aug = RandomAffine(degrees=(-30, 30), scale=(0.8, 1.2), p=1.0)
# Apply to a batch of images
images = torch.randn(4, 3, 256, 256) # (B, C, H, W)
augmented = aug(images)
print(augmented.shape) # torch.Size([4, 3, 256, 256])
ColorJiggle for Photometric Augmentation
import torch
from kornia.augmentation import ColorJiggle
# Create a ColorJiggle transform
aug = ColorJiggle(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.1, p=1.0)
# Apply to a batch of images
images = torch.randn(4, 3, 256, 256)
augmented = aug(images)
print(augmented.shape) # torch.Size([4, 3, 256, 256])