Pipelines: Composing Multiple Augmentations
On this page
- Quick Reference
- Defining a Simple Pipeline
- Compose Parameters
- Dynamic Pipeline Modification
- How Probabilities Work in Pipelines
- Advanced Composition Utilities
- Nested Compositions
- Where to Go Next?
While individual Transforms are useful, data augmentation often involves applying a sequence of different operations. Albumentations makes this easy using Pipelines.
A pipeline, defined using albumentations.Compose, chains multiple transforms together. When the pipeline is called, it applies the contained transforms sequentially to the input data.
Quick Reference
For experienced users, here are the key concepts covered in this guide:
- Basic Pipeline Creation:
A.Compose([transform1, transform2, ...]) - Dynamic Modification:
pipeline + transform,transform + pipeline,pipeline - transform - Composition Utilities:
OneOf,SomeOf,Sequential,RandomOrder, etc. - Parameter Configuration:
bbox_params,keypoint_params,additional_targets,seed, etc.
Defining a Simple Pipeline
You define a pipeline by passing a list of instantiated transforms to A.Compose.
import albumentations as A
import cv2
import numpy as np
# Assume 'image' is loaded as a NumPy array (e.g., 100x100x3)
image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8) # Dummy image
# 1. Define the pipeline
pipeline = A.Compose([
A.HorizontalFlip(p=0.5), # 50% chance to flip
A.RandomBrightnessContrast(p=0.8), # 80% chance to adjust brightness/contrast
A.GaussianBlur(p=0.3), # 30% chance to blur
])
# 2. Apply the pipeline
transformed_data = pipeline(image=image)
transformed_image = transformed_data['image']
print(f"Original shape: {image.shape}, Transformed shape: {transformed_image.shape}")
# Note: Shape usually remains the same unless a spatial transform like Resize is used.
Compose Parameters
While the transforms list is the only mandatory argument, A.Compose accepts several other parameters to configure its behavior, especially when dealing with multiple data targets or needing fine-grained control:
compose = A.Compose(
transforms,
bbox_params=None,
keypoint_params=None,
additional_targets=None,
p=1.0,
is_check_shapes=True,
strict=False,
mask_interpolation=None,
seed=None,
save_applied_params=False,
)
Core Parameters
-
transforms(list): (Required) The list of augmentation instances to apply sequentially. -
p(float): The probability that the entire composition of transforms is applied. If a random check (< p) fails, the input data is returned unchanged. Note this differs from the individualpof transforms inside the list. Default:1.0(always apply the sequence).
Target-Specific Parameters
-
bbox_params(A.BboxParams| dict | None): Configuration for handling bounding boxes. If provided, the pipeline can accept abboxesargument during the call. See the Object Detection Guide for details. Default:None. -
keypoint_params(A.KeypointParams| dict | None): Configuration for handling keypoints. If provided, the pipeline can accept akeypointsargument during the call. See the Keypoint Augmentation Guide for details. Default:None. -
additional_targets(dict[str, str] | None): Defines how to handle additional input arrays beyond the standardimage,mask, etc. Maps a custom input name (e.g.,'image2') to a standard target type ('image'or'mask'). See the Using Additional Targets Guide for details. Default:None.
Validation and Quality Control
-
is_check_shapes(bool): IfTrue, performs shape consistency checks for all input arrays (image,mask,masks,volume,mask3d, etc.) before applying transforms.Use
Falseonly when differently sized targets are intentional. Container and rank validation still applies, and a transform may require aligned targets to sample shared geometry. Default:True. -
strict(bool): WithTrue, rejects unknown sample keys and unknown transform constructor arguments. WithFalse, unrelated sample keys can pass through and unknown transform keywords warn. Invalid declared constructor values still raise in either mode. Default:False.Since 2.6.1,
Composevalidates root containers, ranks, aligned shapes, annotation coordinates, and label counts before drawing the pipeline probability, including withp=0. Root validation failures do not advance the random stream. See the validation contract.
Advanced Configuration
-
mask_interpolation(int | None): If set to an OpenCV interpolation flag (e.g.,cv2.INTER_LINEAR), this value overrides the interpolation method used for masks in all applicable geometric transforms within the pipeline. IfNone, each transform uses its default mask interpolation (usuallycv2.INTER_NEAREST). Default:None. -
seed(int | None): Controls the reproducibility of random augmentations within this specificComposeinstance. Two fresh pipelines with the same graph, seed, inputs, environment, and ordered calls produce the same sequence. Calls on one pipeline advance its stream. Default:None(no fixed seed).When seed is set (int):
- Creates a fixed internal random state
- Two
Composeinstances with the same seed and transforms will produce identical sequences of augmentations - Each call to the same
Composeinstance still produces random augmentations, but these sequences are reproducible
When seed is None (default):
- Generates a new internal random state on each
Composecreation - Different
Composeinstances will produce different sequences of augmentations
Important: Albumentations uses call-local random generators, completely independent from global seeds (
random.seed(),np.random.seed()). Setting global seeds has no effect on augmentations. Custom transforms receive them assampling.py_randomandsampling.random_generatorinsample_parameters. For comprehensive information on reproducibility, see the Reproducibility Guide. For custom transforms, also see Creating Custom Transforms Guide. Default:None. -
save_applied_params(bool): IfTrue, after processing each transform, Compose saves its JSON-compatible applied constructor configuration in the result dictionary under the keyapplied_transforms. The record identifies the transforms that ran and includes sampled constructor-level overrides when a transform exposes them. It does not contain every runtime-only field, such as crop coordinates, dropout masks, or transformation matrices. For a detailed workflow, see Replay and Applied-Parameter Debugging. Default:False.
Grayscale Image Preprocessing
Compose includes automatic preprocessing for grayscale images to ensure compatibility with all transforms:
- NumPy layouts: Accepts
(H, W)and(H, W, 1)grayscale images - CPU Tensor layouts: Accepts
(H, W)and(1, H, W)grayscale images - Channel dimension handling: If you pass a grayscale image without a channel dimension (e.g., shape
(H, W)),Composeautomatically adds one during preprocessing, making it(H, W, 1)for NumPy or(1, H, W)for a Tensor - Postprocessing cleanup: Removes an added channel axis only if the result still has one channel
- Transparent to users: This happens automatically - you can pass grayscale images in either format
Direct NumPy transforms such as HorizontalFlip also accept (H, W). Compose owns the common layout and dtype boundary for full policies; see target layouts and FP16 support.
Dynamic Pipeline Modification
Albumentations supports dynamic pipeline modification after initialization using mathematical operators. This allows you to add or remove transforms from existing pipelines without recreating them from scratch. All operations preserve the original pipeline's parameters (such as bbox_params, keypoint_params, additional_targets, etc.) and return new instances without modifying the original pipeline.
Adding Transforms to Pipelines
Use the + operator to add transforms to the end of a pipeline, or use a transform before + to add to the beginning:
import albumentations as A
# Create a base pipeline
base_pipeline = A.Compose([
A.RandomCrop(height=256, width=256),
A.HorizontalFlip(p=0.5),
], bbox_params=A.BboxParams(coord_format='pascal_voc', label_fields=['labels']))
# Add a single transform to the end
extended_pipeline = base_pipeline + A.RandomBrightnessContrast(p=0.3)
# Result: [RandomCrop, HorizontalFlip, RandomBrightnessContrast]
# Add multiple transforms to the end
extended_pipeline = base_pipeline + [A.GaussianBlur(p=0.2), A.Rotate(angle_range=(-15, 15))]
# Result: [RandomCrop, HorizontalFlip, GaussianBlur, Rotate]
# Add transforms to the beginning
extended_pipeline = A.Resize(height=512, width=512) + base_pipeline
# Result: [Resize, RandomCrop, HorizontalFlip]
# Add multiple transforms to the beginning
extended_pipeline = [A.Resize(height=512, width=512), A.Normalize()] + base_pipeline
# Result: [Resize, Normalize, RandomCrop, HorizontalFlip]
# All bbox_params and other settings are preserved in the new pipeline
print(f"bbox_params preserved: {extended_pipeline.processors.get('bboxes') is not None}")
Removing Transforms from Pipelines
Use the - operator with a transform class to remove its first occurrence from a pipeline:
import albumentations as A
# Create transform instances
flip_transform = A.HorizontalFlip(p=0.5)
brightness_transform = A.RandomBrightnessContrast(p=0.3)
blur_transform = A.GaussianBlur(p=0.2)
# Create pipeline with these specific instances
pipeline = A.Compose([
A.RandomCrop(height=256, width=256),
flip_transform,
brightness_transform,
blur_transform,
])
# Remove transforms by class type
reduced_pipeline = pipeline - A.HorizontalFlip
# Result: [RandomCrop, RandomBrightnessContrast, GaussianBlur]
# Note: Removal works by class type, parameters don't matter
another_flip = A.HorizontalFlip(p=0.8) # Different parameters
pipeline_with_another = A.Compose([A.RandomCrop(224, 224), another_flip])
reduced_again = pipeline_with_another - A.HorizontalFlip # Works! Class type matches
# Chain removal operations
further_reduced = reduced_pipeline - A.RandomBrightnessContrast - A.GaussianBlur
⚠️ Important: Class-Based Removal
The
-operator removes transforms by class type, not by instance object. This means:
- ✅ Pass the transform class (e.g.,
A.HorizontalFlip) to remove any instance of that class- ✅ No need to store transform instances - just use the class name
- ⚠️ Only the first occurrence of that class type is removed if multiple exist
# ✅ CORRECT: Remove by class type pipeline = A.Compose([A.Resize(224, 224), A.HorizontalFlip(p=0.5)]) modified = pipeline - A.HorizontalFlip # Works! Removes the HorizontalFlip transform # ✅ WORKS FROM COMPLEX PIPELINES: Remove from anywhere in the pipeline complex_pipeline = A.Compose([ A.RandomCrop(256, 256), A.HorizontalFlip(p=0.5), A.RandomBrightnessContrast(p=0.3), A.GaussianBlur(p=0.2) ]) without_flip = complex_pipeline - A.HorizontalFlip # Removes HorizontalFlip from middle # Result: [RandomCrop, RandomBrightnessContrast, GaussianBlur] # ⚠️ Multiple instances: Only first occurrence removed pipeline = A.Compose([ A.HorizontalFlip(p=0.3), A.VerticalFlip(p=0.5), A.HorizontalFlip(p=0.8) ]) modified = pipeline - A.HorizontalFlip # Removes first HorizontalFlip (p=0.3) # Result: [VerticalFlip(p=0.5), HorizontalFlip(p=0.8)]
Important Notes about Pipeline Modification
-
Class-Based Removal: The
-operator removes transforms based on class type usingtype(transform) is TargetClass. The specific parameters or instance identity don't matter. -
Parameter Preservation: All pipeline modification operations preserve the original configuration including
bbox_params,keypoint_params,additional_targets,seed, and other settings. -
New Instance Creation: Operators return a new composition with a changed transform list. Existing transform objects and random-state objects can be shared; this is not a deep copy for independent experiments.
-
Type Restrictions: Addition accepts
BasicTransforminstances or sequences of them. Subtraction accepts aBasicTransformclass. Composition objects such asOneOfcannot be added or removed with these operators. -
Error Handling:
- Adding non-
BasicTransforminstances raisesTypeError - Removing a transform not found in the pipeline raises a
ValueErrorsubclass - Removing non-
BasicTransforminstances raisesTypeError
- Adding non-
-
Random State Inheritance: New pipeline instances inherit the random state from the original pipeline, ensuring consistent behavior.
Practical Examples
Building Pipelines Incrementally:
import albumentations as A
# Start with basic geometric transforms
geometric_pipeline = A.Compose([
A.RandomCrop(height=256, width=256),
A.HorizontalFlip(p=0.5),
])
# Add color augmentations for training
training_pipeline = geometric_pipeline + [
A.RandomBrightnessContrast(p=0.3),
A.HueSaturationValue(p=0.3),
]
# Add noise and blur for robustness testing
robust_pipeline = training_pipeline + [
A.GaussNoise(p=0.2),
A.GaussianBlur(p=0.2),
]
Conditional Pipeline Construction:
import albumentations as A
# Base pipeline
pipeline = A.Compose([A.RandomCrop(height=256, width=256)])
# Add transforms based on conditions
if use_geometric_augmentations:
pipeline = pipeline + [A.HorizontalFlip(p=0.5), A.Rotate(angle_range=(-15, 15))]
if use_color_augmentations:
pipeline = pipeline + A.RandomBrightnessContrast(p=0.3)
if add_noise:
pipeline = pipeline + A.GaussNoise(p=0.1)
Transform Replacement:
import albumentations as A
# Original pipeline with specific transform instance
old_blur = A.GaussianBlur(blur_range=(3, 3), p=0.2)
pipeline = A.Compose([
A.RandomCrop(height=256, width=256),
A.HorizontalFlip(p=0.5),
old_blur,
])
# To replace a configured transform, rebuild the pipeline with the new instance.
new_blur = A.GaussianBlur(blur_range=(7, 7), p=0.4)
updated_pipeline = A.Compose([
A.RandomCrop(height=256, width=256),
A.HorizontalFlip(p=0.5),
new_blur,
])
How Probabilities Work in Pipelines
When a pipeline created with A.Compose is called:
- It iterates through the transforms in the list in the order they were provided.
- For each transform in the list, its individual
pvalue is checked. - If the random check for a specific transform passes (based on its
p), that transform is applied to the current state of the data (which might have been modified by previous transforms in the pipeline). - If the check fails, that specific transform is skipped, and the pipeline moves to the next one.
This means that on any given call to the pipeline, a different subset of the defined transforms might actually be applied, controlled by their individual probabilities.
Compose(p=...) first decides whether the whole policy runs. If it runs, each child applies its own probability rule. Sequential(p=...) can place another probability gate around a subgroup.
Advanced Composition Utilities
Beyond the basic sequential application in Compose, Albumentations offers several utilities to build more complex pipelines with conditional or altered execution flow.
OneOf: Apply Exactly One Transform
A.OneOf takes a list of transforms and applies exactly one of them, chosen randomly based on their individual p values (which are normalized within the OneOf block). The OneOf block itself also has a p parameter determining if any transform within it gets applied.
import albumentations as A
pipeline = A.Compose([
# Apply either GaussianBlur or MotionBlur, but not both.
# The choice between them depends on their p values (here, equal chance).
# This entire OneOf block has a 90% chance of executing.
A.OneOf([
A.GaussianBlur(p=1.0), # p=1.0 within OneOf means it's considered
A.MotionBlur(p=1.0), # p=1.0 within OneOf means it's considered
], p=0.9), # 90% chance to apply *one* of the above
A.HorizontalFlip(p=0.5)
])
# When pipeline(image=...) is called:
# - 90% chance: Either GaussianBlur OR MotionBlur is applied (50/50 chance each).
# - 10% chance: Neither GaussianBlur nor MotionBlur is applied.
# - Independently, 50% chance HorizontalFlip is applied.
SomeOf: Apply a Random Subset of Transforms
A.SomeOf takes a list of transforms and a number n. If the SomeOf block itself is activated (based on its main p value), it randomly selects n transforms from the list and attempts to apply them sequentially.
Key Features:
- Number of transforms (
n): A fixed integer.SomeOfselects exactly that many transforms on each call. - Selection Method (
replace): By default (replace=False), transforms are selected without replacement. Ifreplace=True, transforms are selected with replacement. - Uniform Selection: Transforms are selected uniformly at random from the list - each transform has an equal chance of being chosen, regardless of their individual
pvalues. - Main Probability (
p): Controls the probability that the SomeOf block executes at all. If this check fails, none of the transforms inside are considered. - Individual Probabilities: Crucially, after SomeOf selects the transforms, it attempts to apply each one. Each selected transform is only applied if its own individual
pvalue passes. Thepvalues inside the list are not ignored after selection.
import albumentations as A
pipeline = A.Compose([
# Select exactly two of the following transforms.
# Selection is without replacement (default).
# This SomeOf block has a 90% chance of being entered.
A.SomeOf([
A.GaussianBlur(p=0.5), # If selected, 50% chance to apply
A.RandomBrightnessContrast(p=1.0), # If selected, 100% chance to apply
A.HueSaturationValue(p=0.8), # If selected, 80% chance to apply
A.Sharpen(p=0.3), # If selected, 30% chance to apply
], n=2, p=0.9), # Select two transforms, 90% of the time
A.HorizontalFlip(p=0.5)
])
# When pipeline(image=...) is called:
# - 90% chance:
# 1. SomeOf selects two transforms randomly from the list.
# 2. It then iterates through those selected transforms.
# 3. For each selected transform, it applies it *only if* that transform's individual `p` value passes its random check.
# - 10% chance: None of the transforms in SomeOf are considered.
# - Independently, 50% chance HorizontalFlip is applied.
OneOrOther: Apply One of Two Transforms/Blocks
A.OneOrOther applies either its first transform (or composed block) or its second transform (or composed block), chosen randomly based on their p values. It provides a clearer structure for simple A/B choices compared to OneOf with two elements.
import albumentations as A
pipeline = A.Compose([
# 50% chance apply first (Flip), 50% chance apply second (Rotate)
A.OneOrOther(
first=A.HorizontalFlip(p=1.0), # p=1.0 means always apply if chosen
second=A.Rotate(angle_range=(-30, 30), p=1.0), # p=1.0 means always apply if chosen
p=0.5 # Probability of applying the `first` transform
),
A.RandomBrightnessContrast(p=0.8)
])
# When pipeline(image=...) is called:
# - 50% chance HorizontalFlip is applied.
# - 50% chance Rotate is applied.
# - Independently, 80% chance RandomBrightnessContrast is applied.
RandomOrder: Apply Transforms in Random Sequence
A.RandomOrder selects n transforms uniformly and visits them in sampled order. Its p controls whether the block runs; each selected child still checks its own probability. The default n=1 selects one child. Set n to the pool size with replace=False to shuffle the whole pool.
import albumentations as A
pipeline = A.Compose([
# Apply Flip and BrightnessContrast, but in a random order each time.
A.RandomOrder([
A.HorizontalFlip(p=0.5),
A.RandomBrightnessContrast(p=0.8),
], n=2, replace=False, p=1.0),
A.GaussianBlur(p=0.3)
])
# When pipeline(image=...) is called:
# - Order 1: Flip (if p=0.5 passes) -> BrightnessContrast (if p=0.8 passes)
# - Order 2: BrightnessContrast (if p=0.8 passes) -> Flip (if p=0.5 passes)
# The order is chosen randomly.
# - Independently, 30% chance GaussianBlur is applied *after* the RandomOrder block.
SelectiveChannelTransform: Apply Transforms to Specific Channels
A.SelectiveChannelTransform applies its contained transforms only to specific channels of the input image. You specify the channels (by index) to affect.
A common use case is working with multi-channel data like RGBA images where you might want to apply certain augmentations only to the RGB channels (indices 0, 1, 2) and leave the Alpha channel (index 3) untouched.
import albumentations as A
import numpy as np
# Assume a 4-channel image (RGBA)
image_rgba = np.random.randint(0, 256, (100, 100, 4), dtype=np.uint8)
# Apply GaussianBlur only to the first three channels (RGB)
pipeline = A.Compose([
A.SelectiveChannelTransform(
[A.GaussianBlur(p=1.0)], # Transform to apply
channels=[0, 1, 2], # Apply only to RGB channels
p=1.0 # Probability of applying this selective transform
),
A.HorizontalFlip(p=0.5) # Applied to all channels
])
transformed_data = pipeline(image=image_rgba)
transformed_image = transformed_data['image']
# When pipeline(image=...) is called:
# - GaussianBlur is applied *only* to the R, G, B channels.
# - Independently, 50% chance the *entire* RGBA image is flipped.
Sequential: Group Transforms with a Single Probability
A.Sequential groups transforms under a probability p. If its check fails, none of its children run. If it passes, the children run in order and still check their own probabilities.
Compose also has a top-level p and owns target validation, annotation processors, and public input/output layouts. Use Sequential inside it to gate a subgroup without creating another data boundary.
Use Sequential when you want to treat a whole sequence of operations as a single augmentation block that either runs entirely or not at all, based on one probability value.
import albumentations as A
pipeline = A.Compose([
# This whole sequence runs only 40% of the time.
A.Sequential([
A.HorizontalFlip(p=1.0), # Always apply *if* Sequential runs
A.RandomBrightnessContrast(p=1.0) # Always apply *if* Sequential runs
], p=0.4), # 40% chance to run the Flip -> BrightnessContrast sequence
A.GaussNoise(p=0.5)
])
# When pipeline(image=...) is called:
# - 40% chance: HorizontalFlip is applied, then RandomBrightnessContrast is applied.
# - 60% chance: Neither Flip nor BrightnessContrast is applied.
# - Independently, 50% chance GaussNoise is applied.
Nested Compositions
The real power of these composition utilities (OneOf, SomeOf, Sequential, etc.) comes from nesting them within each other or within a main A.Compose block. This allows you to build highly customized and complex augmentation pipelines.
Here's an example combining several concepts:
import albumentations as A
import cv2
import numpy as np
image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
complex_pipeline = A.Compose([
# --- Block 1: Basic Transform ---
A.HorizontalFlip(p=0.5), # Applied 50% of the time
# --- Block 2: Choose ONE of two sequences ---
A.OneOf([
# Sequence A: Blur then Sharpen (runs as a unit if chosen)
A.Sequential([
A.GaussianBlur(p=1.0), # Always applies if Sequence A is chosen
A.Sharpen(p=1.0) # Always applies if Sequence A is chosen
], p=1.0), # p=1.0 ensures it's considered by OneOf
# Sequence B: Emboss (runs if chosen)
A.Emboss(p=1.0) # Always applies if Sequence B is chosen
], p=0.8), # 80% chance to run EITHER Sequence A OR Sequence B (50/50 split)
# --- Block 3: Apply SOME of these color transforms ---
A.SomeOf([
A.RandomBrightnessContrast(p=0.7), # If selected, 70% chance to apply
A.HueSaturationValue(p=0.6), # If selected, 60% chance to apply
A.ColorJitter(p=0.5) # If selected, 50% chance to apply
], n=2, p=0.9), # 90% chance to select two, then apply based on their p
# --- Block 4: Noise, maybe ---
A.GaussNoise(p=0.2) # Applied 20% of the time, independently
])
# Applying the complex pipeline:
transformed_data = complex_pipeline(image=image)
transformed_image = transformed_data['image']
# --- Execution Flow Example ---
# When complex_pipeline(image=...) is called:
# 1. HorizontalFlip runs based on its p=0.5.
# 2. The OneOf block runs based on its p=0.8.
# - If it runs, it randomly chooses either Sequential or Emboss (50/50).
# - If Sequential is chosen, GaussianBlur and Sharpen are both applied.
# - If Emboss is chosen, Emboss is applied.
# 3. The SomeOf block runs based on its p=0.9.
# - If it runs, it selects two transforms randomly from its list.
# - For each selected transform, it runs based on *that transform's* p value.
# 4. GaussNoise runs based on its p=0.2.
This demonstrates how you can chain and nest different composition logic blocks to control the probability and flow of your augmentations precisely.
Where to Go Next?
After learning about pipelines, you now understand how to:
- Create basic and complex augmentation pipelines using A.Compose
- Dynamically modify pipelines using
+and-operators - Control probability and execution flow with composition utilities
- Configure pipelines for different data types (images, masks, bboxes, keypoints)
- Handle advanced scenarios like nested compositions
Next steps to explore:
- Transforms: Explore the individual augmentation operations you can include in your pipelines.
- Setting Probabilities: Get a deeper understanding of how the
pparameter works for both individual transforms and composition blocks. - Working with Targets: Learn how pipelines consistently apply augmentations to images, masks, bounding boxes, and keypoints.
- Basic Usage Examples: See complete code examples of pipelines applied to common computer vision tasks.
- Advanced Guides: Discover techniques like serialization or creating custom transforms to integrate into your pipelines.
- Visually Explore Transforms: Experiment with combining transforms in the interactive tool.