albumentations
**Albumentations** is a **fast, flexible, open-source Python library for image augmentation that has become the de facto standard in computer vision competitions (Kaggle) and production pipelines** — providing 70+ augmentation transforms optimized with OpenCV and numpy (2-10× faster than torchvision), with native support for simultaneously transforming images alongside their bounding boxes, segmentation masks, and keypoints, ensuring that spatial labels stay correctly aligned when the image is flipped, rotated, or cropped.
**What Is Albumentations?**
- **Definition**: A Python library specialized in image augmentation for deep learning — providing a composable pipeline of transforms that can be applied to images, bounding boxes (object detection), segmentation masks, and keypoints simultaneously with correct coordinate transformations.
- **Why Albumentations Over torchvision?**: (1) 2-10× faster due to OpenCV/numpy optimization, (2) native bounding box and mask support (torchvision requires manual coordinate transforms), (3) 70+ transforms vs torchvision's ~20, (4) domain-specific transforms (weather effects, histology stains, elastic distortions).
- **Kaggle Standard**: Albumentations is used in the vast majority of winning Kaggle computer vision solutions — its speed and flexibility make it the preferred choice for competition and production workloads.
**Core Usage**
```python
import albumentations as A
from albumentations.pytorch import ToTensorV2
transform = A.Compose([
A.RandomCrop(width=256, height=256),
A.HorizontalFlip(p=0.5),
A.RandomBrightnessContrast(p=0.2),
A.Normalize(mean=(0.485, 0.456, 0.406),
std=(0.229, 0.224, 0.225)),
ToTensorV2(),
])
transformed = transform(image=image, mask=mask,
bboxes=bboxes)
```
**Key Transform Categories**
| Category | Transforms | Example |
|----------|-----------|---------|
| **Spatial** | Flip, Rotate, Crop, Resize, Affine, ElasticTransform | Random 90° rotation |
| **Color** | Brightness, Contrast, HueSaturation, CLAHE, RGBShift | Random brightness ±20% |
| **Blur/Noise** | GaussianBlur, MotionBlur, GaussNoise, ISONoise | Simulate camera shake |
| **Weather** | RandomRain, RandomFog, RandomSnow, RandomSunFlare | Simulate weather conditions |
| **Dropout** | CoarseDropout (Cutout), GridDropout, ChannelDropout | Zero out random patches |
| **Medical/Histology** | ElasticTransform, GridDistortion | Tissue deformation simulation |
**Bounding Box Support**
| Task | What Happens When Image Is Flipped |
|------|----------------------------------|
| **Image only** | Image pixels flip — done |
| **Object detection** | Image flips + bounding box coordinates transform (x → width - x) |
| **Segmentation** | Image flips + mask flips identically |
| **Keypoints** | Image flips + each keypoint coordinate transforms |
Albumentations handles all coordinate transformations automatically — you specify `bbox_params` and the library ensures labels stay aligned with the augmented image.
**Albumentations vs Alternatives**
| Library | Speed | Box/Mask Support | Transforms | Ecosystem |
|---------|-------|-----------------|-----------|-----------|
| **Albumentations** | Fastest (OpenCV) | Native, automatic | 70+ | PyTorch, TF, standalone |
| **torchvision** | Good | Manual (v2 improving) | ~20 | PyTorch only |
| **imgaug** | Moderate | Yes | 60+ | Standalone |
| **Kornia** | GPU-accelerated | Yes | 40+ | PyTorch (differentiable) |
| **Augly (Meta)** | Moderate | Limited | Social media focused | PyTorch |
**Albumentations is the production-standard image augmentation library** — providing the speed, flexibility, and automatic coordinate transformation that computer vision pipelines require, with the broadest set of transforms and native support for detection, segmentation, and keypoint tasks that make it the default choice for both Kaggle competitions and production computer vision systems.