← Master Index
Vol. 04 Module 4.1 Lecture

Data Augmentation

Data Preparation

How This Lesson Fits the Module

After Feature Engineering shapes tabular signals, many AI projects still face a scarcer constraint: not enough labeled examples. Data augmentation synthesizes training variations—flipped images, paraphrased sentences—so models generalize without collecting millions of new labels.

Augmentation is standard in computer vision and increasingly essential in NLP. It complements (never replaces) better data collection and careful labeling.

Learning Objectives

By the end of this lesson, students should be able to:

  • Explain when augmentation helps versus when it introduces label noise.
  • Apply common image transforms: flip, crop, rotate, color jitter, and mixup.
  • Use library pipelines (torchvision, Albumentations) for on-the-fly augmentation.
  • Describe text augmentation: synonym replacement, back-translation, and paraphrase.
  • Distinguish offline augmentation (expand dataset) from online augmentation (each epoch).
  • Validate that augmented samples still match the original label semantics.

What Data Augmentation Is—and When to Use It

Data augmentation creates modified copies of existing training examples while preserving (approximately) the same label. The model sees plausible variation—lighting changes, word swaps—and learns invariances that improve test performance.

Use augmentation when…Avoid or limit augmentation when…
Training set is small relative to model capacityTransforms break label meaning (e.g., flip text, rotate digits that aren’t rotation-invariant)
Test distribution includes natural variationLabels are extremely fine-grained and sensitive to tiny changes
Collecting more data is expensiveYou can fix the problem with better features or more real data instead
Class imbalance can be balanced with oversampling + augAugmentation diversity is low (repeating the same warp)

Image Augmentation

Vision models benefit from geometric and photometric transforms. Apply them only during training; evaluation uses deterministic preprocessing (resize, center crop, normalize).

from torchvision import transforms train_transform = transforms.Compose([ transforms.RandomResizedCrop(224, scale=(0.8, 1.0)), transforms.RandomHorizontalFlip(p=0.5), transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ]) eval_transform = transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ])
TransformEffectTypical use
Random horizontal flipMirror imageNatural scenes, animals (not text/signs)
Random rotation (±15°)Small tilt invarianceDocument scans, product photos
Random crop / cutoutPartial occlusionRobustness to framing and clutter
Mixup / CutMixBlend images + soft labelsRegularization in classification
Albumentations pipelineFast CPU/GPU aug on numpyDetection, segmentation, medical imaging
Engineering Habit — Visual Sanity Check

Before a long training run, plot 16 augmented batches. If labels no longer match human judgment—upside-down road signs labeled “stop”—your policy is wrong for the task.

Text Augmentation

Text augmentation must preserve semantics. Aggressive synonym replacement can flip sentiment; back-translation (EN→DE→EN) often produces fluent paraphrases with the same meaning.

# Conceptual NLP augmentation pipeline original = "The battery life is excellent but the screen is dim." # Synonym replacement (spaCy / WordNet) — verify sentiment unchanged aug_synonym = "The battery duration is excellent but the display is dim." # Back-translation via MarianMT or cloud API aug_backtrans = "Battery life is great, though the display is a bit dark." # Random deletion / insertion (low rates) for robustness aug_noise = "The battery life excellent but screen is dim."
TechniqueProsCons
Synonym replacementFast, no extra modelsCan change negation or intensity
Back-translationNatural paraphrasesSlower; needs translation models
EDA (easy data augmentation)Simple random opsCan produce ungrammatical text
LLM paraphraseHigh qualityCost, consistency, policy review
Critical Mistake — Augmenting the Test Set

Augmentation is a training technique. Never augment validation or test data to inflate metrics. Report results on clean, representative evaluation sets.

Offline vs. Online Augmentation

Online (on-the-fly)

  • New random transform each epoch
  • Saves disk; infinite diversity
  • Standard in PyTorch DataLoader
  • Reproducibility needs seed control

Offline (pre-generated)

  • Write augmented files to storage
  • Auditable and shareable datasets
  • Useful for labeling QA workflows
  • Storage cost grows with multiplier

Augmentation and Class Balance

For imbalanced classification, oversample minority classes with heavier augmentation while undersampling or lightly augmenting majority classes. Track per-class metrics—overall accuracy can hide failure on rare labels.

Knowledge Check

  1. Short Answer: Why flip images horizontally for cats but not for “5 vs 2” digit classification? Answer: Cats are roughly left-right invariant; digit identity changes under flip.
  2. True/False: Validation transforms should include random crops. Answer: False—use deterministic eval transforms.
  3. Multiple Choice: Safer paraphrase method for sentiment: (a) random word deletion, (b) back-translation, (c) random character swap. Answer: (b).
  4. Short Answer: Online vs offline augmentation? Answer: Online applies random transforms each epoch; offline prewrites augmented files.
  5. Short Answer: Should you augment test data? Answer: No—only training (and optionally unlabeled pools for semi-supervised methods with care).
  6. True/False: Mixup interpolates both images and labels during training. Answer: True.
  7. Multiple Choice: RandomHorizontalFlip belongs in: (a) train transforms only, (b) test transforms only, (c) both equally. Answer: (a).
  8. Short Answer: What is the main risk of synonym replacement in NLP? Answer: It can change meaning or polarity if the synonym is not context-aware.
  9. True/False: Augmentation fully replaces the need for more labeled data. Answer: False—it multiplies diversity but cannot invent missing concepts.
  10. Multiple Choice: Color jitter is typically: (a) a geometric transform, (b) a photometric/color transform, (c) a label transform. Answer: (b).

Key Takeaways

  • Augmentation increases effective training diversity when labels stay semantically valid.
  • Image pipelines use geometric and color transforms; always separate train vs eval policies.
  • Text augmentation needs semantic checks—paraphrase beats random noise.
  • Apply augmentation during training only; never inflate test-set scores.
  • Next: Missing Values to handle incomplete records in tabular pipelines.
Trainer’s Guide

Hands-on idea: Train a small CNN on CIFAR-10 with and without augmentation. Students plot learning curves and explain the accuracy gap.

Discussion prompt: For a medical X-ray classifier, which transforms are ethically and clinically acceptable? Who must sign off?

Recap: Augmentation multiplies training diversity while keeping labels valid—use it on train only. Continue with Missing Values.