Introduction
Data augmentation increases training data through random transformations to improve model generalization.
torchvision transforms
from torchvision import transforms
from PIL import Image
transform = transforms.Compose([
transforms.RandomHorizontalFlip(p=0.5),
transforms.RandomRotation(degrees=15),
transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2),
transforms.RandomResizedCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
img = Image.open('photo.jpg')
augmented = transform(img)
TensorFlow Augmentation
from tensorflow.keras.layers import RandomFlip, RandomRotation, RandomZoom
from tensorflow.keras import layers
data_augmentation = keras.Sequential([
RandomFlip("horizontal"),
RandomRotation(0.1),
RandomZoom(0.1),
])
albumentations
import albumentations as A
import cv2
transform = A.Compose([
A.HorizontalFlip(p=0.5),
A.RandomBrightnessContrast(p=0.3),
A.ShiftScaleRotate(shift_limit=0.1, scale_limit=0.1, rotate_limit=15),
A.GaussNoise(var_limit=(10.0, 50.0)),
A.CoarseDropout(max_holes=8, max_height=32, max_width=32)
])
image = cv2.imread('photo.jpg')
augmented = transform(image=image)
Keras ImageDataGenerator
from tensorflow.keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True,
zoom_range=0.2,
fill_mode='nearest'
)
datagen.fit(x_train)
Practice Problems
- Apply random flips and rotations
- Add color jitter
- Implement random cropping
- Use augmentation in training
- Create custom augmentation