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 RandAugment TrivialAugment

From Leeroopedia


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

Overview

Concrete tool for automatic augmentation policies provided by Kornia's augmentation module.

Description

RandAugment applies N randomly selected transforms with uniform magnitude M (0-30 scale). TrivialAugment applies one randomly selected transform with uniformly sampled magnitude. Both inherit from PolicyAugmentBase and use a default_policy containing standard transforms:

  • ShearX, ShearY
  • TranslateX, TranslateY
  • Rotate
  • Brightness, Contrast, Sharpness
  • Posterize, Solarize
  • AutoContrast, Equalize

Custom policies can be provided to override the default transform set.

Usage

Use RandAugment(n=2, m=10) for moderate augmentation. Use TrivialAugment() for zero-hyperparameter augmentation. Wrap in AugmentationSequential for pipeline integration with multi-data-key support.

Code Reference

Source Location

  • Repository: kornia
  • Files:
    • kornia/augmentation/auto/rand_augment/rand_augment.py (L50-132)
    • kornia/augmentation/auto/trivial_augment/trivial_augment.py (L47-92)

Signature

class RandAugment(PolicyAugmentBase):
    def __init__(
        self,
        n: int,
        m: int,
        policy: Optional[List[SUBPOLICY_CONFIG]] = None,
        transformation_matrix_mode: str = "silent",
    ) -> None

class TrivialAugment(PolicyAugmentBase):
    def __init__(
        self,
        policy: Optional[List[SUBPOLICY_CONFIG]] = None,
        transformation_matrix_mode: str = "silent",
    ) -> None

Import

from kornia.augmentation.auto import RandAugment, TrivialAugment

I/O Contract

Inputs (RandAugment)

Name Type Required Description
n int Yes Number of transforms to apply per image
m int Yes Magnitude of transforms on 0-30 scale
policy List[SUBPOLICY_CONFIG] No Custom policy; defaults to built-in standard policy
transformation_matrix_mode str No Transform matrix tracking mode; default "silent"

Inputs (TrivialAugment)

Name Type Required Description
policy List[SUBPOLICY_CONFIG] No Custom policy; defaults to built-in standard policy
transformation_matrix_mode str No Transform matrix tracking mode; default "silent"

Outputs

Name Type Description
return torch.Tensor Augmented tensor of shape (B, C, H, W); must be wrapped in AugmentationSequential for multi-data-key use

Usage Examples

RandAugment with n=2, m=10

import torch
from kornia.augmentation.auto import RandAugment

# Moderate augmentation: 2 transforms at magnitude 10
aug = RandAugment(n=2, m=10)

images = torch.randn(4, 3, 224, 224)
augmented = aug(images)
print(augmented.shape)  # torch.Size([4, 3, 224, 224])

TrivialAugment Basic

import torch
from kornia.augmentation.auto import TrivialAugment

# Zero-hyperparameter augmentation
aug = TrivialAugment()

images = torch.randn(4, 3, 224, 224)
augmented = aug(images)
print(augmented.shape)  # torch.Size([4, 3, 224, 224])

Custom Policy

import torch
from kornia.augmentation.auto import RandAugment

# Define a custom policy with only geometric transforms
custom_policy = [
    [("ShearX", -0.3, 0.3)],
    [("ShearY", -0.3, 0.3)],
    [("TranslateX", -0.1, 0.1)],
    [("TranslateY", -0.1, 0.1)],
    [("Rotate", -30.0, 30.0)],
]

aug = RandAugment(n=2, m=15, policy=custom_policy)

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

Related Pages

Implements Principle

Requires Environment

Page Connections

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