Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Kornia Kornia AugmentationSequential

From Leeroopedia


Knowledge Sources
Domains Vision, Augmentation
Last Updated 2026-02-09 15:00 GMT

Overview

Concrete tool for composing augmentation transforms into a multi-data-key pipeline provided by Kornia.

Description

AugmentationSequential chains augmentation modules and applies them consistently across multiple data types. It supports the following DataKey types:

  • INPUT -- image tensors
  • MASK -- segmentation masks
  • BBOX -- bounding boxes (default format)
  • BBOX_XYXY -- bounding boxes in (x1, y1, x2, y2) format
  • BBOX_XYWH -- bounding boxes in (x, y, width, height) format
  • KEYPOINTS -- point annotations
  • CLASS -- class labels (passed through unchanged)

Key features include:

  • random_apply -- select N random transforms from the sequence instead of applying all
  • Transformation matrix tracking for inverse operations via .inverse()
  • VideoSequential integration for temporal augmentation

Usage

Use to compose augmentation transforms and apply them jointly to images with masks, boxes, or keypoints. This is the primary entry point for building augmentation pipelines in Kornia.

Code Reference

Source Location

  • Repository: kornia
  • File: kornia/augmentation/container/augment.py
  • Lines: L51-433

Signature

class AugmentationSequential:
    def __init__(
        self,
        *args: Union[_AugmentationBase, ImageSequential],
        data_keys: Optional[Union[Sequence[str], Sequence[int], Sequence[DataKey]]] = (DataKey.INPUT,),
        same_on_batch: Optional[bool] = None,
        keepdim: Optional[bool] = None,
        random_apply: Union[int, bool, Tuple[int, int]] = False,
        random_apply_weights: Optional[List[float]] = None,
        transformation_matrix_mode: str = "silent",
        extra_args: Optional[Dict[DataKey, Dict[str, Any]]] = None,
    ) -> None

Import

from kornia.augmentation import AugmentationSequential

I/O Contract

Inputs

Name Type Required Description
*args ImageSequential Yes Augmentation modules to compose
data_keys Sequence[DataKey] No Which data types to transform; default (DataKey.INPUT,)
same_on_batch bool No Apply same transform to all items in batch
keepdim bool No Preserve input dimensionality
random_apply bool | Tuple[int, int] No Randomly select N transforms instead of applying all; default False
random_apply_weights List[float] No Sampling weights for random_apply
transformation_matrix_mode str No How to track transform matrices; default "silent"
extra_args Dict[DataKey, Dict[str, Any]] No Extra arguments per data key

Outputs

Name Type Description
return Tensor or List[Tensor] Augmented tensor(s) matching input data_keys, with transformation matrices tracked for .inverse()

Usage Examples

Pipeline with Images and Masks

import torch
from kornia.augmentation import AugmentationSequential, RandomAffine, ColorJiggle

# Define pipeline with image and mask data keys
aug = AugmentationSequential(
    RandomAffine(degrees=(-30, 30), scale=(0.8, 1.2), p=1.0),
    ColorJiggle(brightness=0.2, contrast=0.2, p=1.0),
    data_keys=["input", "mask"],
)

images = torch.randn(4, 3, 256, 256)
masks = torch.randint(0, 2, (4, 1, 256, 256)).float()

# Apply consistent augmentation to both images and masks
aug_images, aug_masks = aug(images, masks)
print(aug_images.shape)  # torch.Size([4, 3, 256, 256])
print(aug_masks.shape)   # torch.Size([4, 1, 256, 256])

Using random_apply

import torch
from kornia.augmentation import (
    AugmentationSequential, RandomAffine, ColorJiggle,
    RandomPerspective, RandomBrightness,
)

# Randomly select 2 transforms per forward pass
aug = AugmentationSequential(
    RandomAffine(degrees=30, p=1.0),
    ColorJiggle(brightness=0.2, p=1.0),
    RandomPerspective(distortion_scale=0.3, p=1.0),
    RandomBrightness(brightness=(0.8, 1.2), p=1.0),
    data_keys=["input"],
    random_apply=2,
)

images = torch.randn(4, 3, 256, 256)
augmented = aug(images)

Calling .inverse()

import torch
from kornia.augmentation import AugmentationSequential, RandomAffine

aug = AugmentationSequential(
    RandomAffine(degrees=(-30, 30), p=1.0),
    data_keys=["input"],
    transformation_matrix_mode="silent",
)

images = torch.randn(4, 3, 256, 256)
augmented = aug(images)

# Invert the augmentation to recover approximately original images
recovered = aug.inverse(augmented)
print(recovered.shape)  # torch.Size([4, 3, 256, 256])

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment