Data augmentation refers to techniques that artificially expand a training dataset by creating modified versions of existing samples. These methods help machine learning models generalize better, reduce overfitting, and perform well on new, unseen data. The process mimics real-world variations that might occur in the data, making models more resilient to different conditions and scenarios.
Core Concepts and Implementation
Image Augmentation Techniques
Geometric Transformations:
# Common geometric transforms
def geometric_augment(image):
# Rotation: -45 to +45 degrees
rotated = rotate(image, angle=random.uniform(-45, 45))
# Scale: 0.8x to 1.2x
scaled = resize(image, scale=random.uniform(0.8, 1.2))
# Flip: horizontal/vertical
flipped = np.fliplr(image) # horizontal flip
# Shear: -20 to +20 degrees
sheared = shear(image, angle=random.uniform(-20, 20))
return [rotated, scaled, flipped, sheared]
Color Space Modifications:
def color_augment(image):
# Brightness: ±30%
brightness = adjust_brightness(image, factor=random.uniform(0.7, 1.3))
# Contrast: ±30%
contrast = adjust_contrast(image, factor=random.uniform(0.7, 1.3))
# Saturation: ±30%
saturation = adjust_saturation(image, factor=random.uniform(0.7, 1.3))
# Hue: ±15 degrees
hue = adjust_hue(image, factor=random.uniform(-15, 15))
return [brightness, contrast, saturation, hue]
Advanced Image Augmentations:
- Mixing Methods:
def mixup(image1, image2, label1, label2, alpha=0.2):
# Create weighted combinations
lambda_val = np.random.beta(alpha, alpha)
mixed_image = lambda_val * image1 + (1 - lambda_val) * image2
mixed_label = lambda_val * label1 + (1 - lambda_val) * label2
return mixed_image, mixed_label
- Cutout/Random Erasing:
def cutout(image, size=50):
# Create random masks
h, w = image.shape[:2]
x = np.random.randint(w - size)
y = np.random.randint(h - size)
image[y:y+size, x:x+size] = 0
return image
Text Augmentation Techniques
Basic Text Operations:
def text_augment(text):
augmentations = []
# Synonym replacement
synonyms = wordnet.synsets(word)
new_text = replace_word(text, word, synonyms.lemmas()[0].name())
augmentations.append(new_text)
# Back translation
intermediate = translate(text, 'en', 'fr')
back_translated = translate(intermediate, 'fr', 'en')
augmentations.append(back_translated)
# Word insertion/deletion
words = text.split()
inserted = words.copy()
inserted.insert(random.randint(0, len(words)), random_word)
augmentations.append(' '.join(inserted))
return augmentations
Advanced Text Methods:
# Contextual augmentation using language models
def contextual_augment(text, model):
# Mask random words
masked_text = mask_random_words(text)
# Predict replacements
predictions = model(masked_text)
# Sample from top-k predictions
augmented = replace_masks(masked_text, predictions, k=5)
return augmented
Audio Augmentation
Time-Domain Transformations:
def audio_augment(waveform, sr=16000):
augmentations = []
# Time stretching
stretched = librosa.effects.time_stretch(waveform, rate=1.2)
# Pitch shifting
pitched = librosa.effects.pitch_shift(waveform, sr=sr, n_steps=2)
# Adding noise
noise = np.random.normal(0, 0.005, len(waveform))
noisy = waveform + noise
augmentations.extend([stretched, pitched, noisy])
return augmentations
Application-Specific Examples
Medical Imaging
def medical_image_augment(scan):
# Preserve diagnostic features
augmentations = []
# Intensity windowing
windowed = apply_window_level(scan, window=2000, level=0)
# Elastic deformation
deformed = elastic_transform(scan, alpha=100, sigma=10)
# Noise addition (within diagnostic limits)
noise_level = calculate_safe_noise_level(scan)
noisy = add_controlled_noise(scan, noise_level)
augmentations.extend([windowed, deformed, noisy])
return augmentations
Satellite Imagery
def satellite_augment(image):
# Weather and atmospheric effects
augmentations = []
# Cloud coverage simulation
cloudy = add_cloud_overlay(image, coverage=0.3)
# Shadow simulation
shadowed = add_shadow_mask(image, angle=45)
# Time of day simulation
time_shifted = adjust_lighting(image, time_of_day=14)
augmentations.extend([cloudy, shadowed, time_shifted])
return augmentations
Quality Control and Validation
Augmentation Verification:
def verify_augmentation(original, augmented, threshold=0.7):
# Check structural similarity
ssim_score = structural_similarity(original, augmented)
# Verify label preservation
label_preserved = verify_labels(original, augmented)
# Check feature consistency
feature_similarity = compare_features(original, augmented)
return all([
ssim_score > threshold,
label_preserved,
feature_similarity > threshold
])
Performance Impact Metrics
Common measurements for augmentation effectiveness:
def evaluate_augmentation(model, original_data, augmented_data):
metrics = {
'accuracy_improvement': 0,
'generalization_score': 0,
'overfitting_reduction': 0
}
# Train with and without augmentation
baseline = train_model(model, original_data)
augmented = train_model(model, augmented_data)
# Compare performance
metrics['accuracy_improvement'] = augmented.accuracy - baseline.accuracy
metrics['generalization_score'] = compare_validation_curves(baseline, augmented)
metrics['overfitting_reduction'] = compare_overfitting_metrics(baseline, augmented)
return metrics
These techniques continue to evolve as researchers develop new methods to create more effective and domain-specific data augmentations.
Comments are closed