Data augmentation for images
3.4.15· AI-ML › Convolutional Neural Networks
Overview
Data augmentation artificially training datasets ko expand karta hai existing images pe label-preserving transformations apply karke, model ki generalization improve karta hai aur overfitting reduce karta hai bina naya data collect kiye.

Socho jaise apne dost ko recognize karna seekhna: tum ek photo memorize nahi karte, tum unhe alag angles, lighting, distances se dekhte ho. Yahi kaam augmentation neural networks ke liye karta hai.
Core Concept: Label-Preserving Transformations
Label-preserving kyun matter karta hai: Agar hum ek "dog" image ko horizontally flip karein, toh woh abhi bhi dog hai. Lekin agar hum text ki image flip karein, toh woh unreadable ho jaati hai – woh transformation OCR tasks ke liye label-preserving NAHI hogi. Yeh constraint domain-specific hai.
Ek acchi augmentation kya hoti hai: Isse realistic variations create honi chahiye jo model test time pe dekh sakta hai, bina ground truth badlaaye.
Choose kaise karein: Poochho "Kya ek insaan abhi bhi isko same label dega?" aur "Kya yeh variation naturally ho sakti hai?"
Common Augmentation Techniques
1. Geometric Transformations
First principles se derivation:
- Problem se shuru karein: Hum point ko angle se origin ke around rotate karna chahte hain.
- Polar mein convert karein: Koi bhi point likha ja sakta hai jahan origin se distance hai, current angle hai.
- Rotation ke baad: Naya angle hai, toh naye coordinates hain:
- Substitute back karein: Kyunki aur :
- Arbitrary center ke liye: Origin pe translate karein, rotate karein, wapas translate karein (upar wala formula).
Yeh step kyun: Trigonometric addition formulas rotation ko ek linear transformation (matrix multiplication) mein convert karte hain, jo computationally efficient hai.
Typical range: Domain ke hisaab se se tak. Zyada rotation se objects frame se bahar ja sakte hain.
2. Flipping
jahan image width hai (0-indexed pixels).
Yeh kyun kaam karta hai: x-axis reverse hoti hai. Vertical flip ke liye: .
Domain considerations:
- Natural images: horizontal flip almost hamesha label preserve karta hai
- Vertical flip: aksar label preserve karta hai (ulta dog abhi bhi dog hai)
- Text/medical imaging: label preserve NAHI ho sakta
3. Scaling aur Cropping
Yeh kyun help karta hai ki derivation:
Model objects ko alag scales (close-up vs door se) aur positions (hamesha centered nahi) pe dekhta hai.
- Scale invariance: 100×100 pixel cat aur 200×200 pixel cat dono recognize hone chahiye. Crop size vary karke, hum model ko alag fractions mein objects pe train karte hain.
- Translation invariance: Alag positions se crop karne ka matlab hai model "cats hamesha center mein hote hain" pe rely nahi kar sakta.
- Aspect ratio variation: Real-world images hamesha perfectly square ya training aspect ratio mein nahi hoti.
Crop ke baad resize kyun: Neural networks fixed input size expect karte hain. Hum variation ke liye crop karte hain, phir architectural requirements meet karne ke liye resize karte hain.
4. Color Augmentations
jahan contrast factor hai (typically 0.8–1.2) aur brightness offset hai (typically -30 se +30).
Yeh formula kyun:
- : values ko spread karta hai (contrast badhata hai)
- : values compress karta hai (contrast ghatata hai)
- : saari values upar shift karti hain (brighter)
- : saari values neeche shift karti hain (darker)
Yeh intensity space ka linear transformation hai.
Derivation:
- Hue circular hai (0° = 360° = red), toh hum offset add karte hain aur modulo se wrap karte hain.
- Saturation color ki intensity hai (0 = gray, 1 = pure color), factor se multiply hoti hai.
- Yeh image structure preserve karta hai alag lighting conditions simulate karte hue.
Yeh kyun help karta hai: Indoor vs outdoor lighting, shadows, camera white balance differences.
Advanced Augmentations
5. Cutout / Random Erasing
- Rectangle position aur size sample karein
- set karein (ya random values)
Yeh kyun help karta hai – first principles se:
Real world mein, objects aksar partially occluded hote hain. Ek car kisi tree ke peeche partly ho sakti hai, kisi insaan ka chehra partially covered ho sakta hai. Occlusion training ke bina, model:
- Ek specific part pe bahut zyada rely kar sakta hai (jaise hamesha full face dekhna zaroori ho)
- Jab key features missing hon toh fail ho sakta hai
Cutout network ko multiple distributed features use karne par majboor karta hai instead of ek single discriminative region. Yeh regularization ka ek form hai kyunki yeh model ko shortcuts lene se rokta hai.
6. MixUp
jahan , typically ya .
Derivation ki yeh kyun kaam karta hai:
Standard training: model classes ke beech decision boundaries seekhta hai. MixUp ke saath: model blended inputs ke liye soft probabilities output karna seekhta hai.
Yeh encourage karta hai:
- Smoother decision boundaries: Sharp transitions ki jagah, gradual confidence changes
- Better calibrated predictions: Probabilities sahi likelihood better reflect karti hain
- Reduced memorization: Specific training examples memorize nahi ho sakte jab woh constantly blend hote hain
Beta distribution kyun: 0.5 ke around symmetric hai. Lower ko 0 ya 1 ki taraf push karta hai (kam mixing), higher zyada uniform mixing deta hai. empirically optimal hai.
Mathematical Framework
Augmentation ke saath:
Derivation ki yeh overfitting kyun reduce karta hai:
-
Original problem: ek finite sample hai, true distribution hai. Unke beech gap overfitting cause karta hai.
-
Augmentation ke saath: Har image bahut saari variations generate karti hai . Agar augmentations realistic hain, toh yeh variations ke samples approximate karti hain jo ke "near" hain.
-
Effective training set size: examples ki jagah, hamare paas hai jahan augmentation variations ki sankhya hai (agar randomly sample karein toh aksar infinite).
-
Regularization effect: Model ko features seekhne padte hain jo transformations ke liye invariant hain, yahi features generalize karte hain.
par expectation kyun: Har mini-batch alag augmentations sample karta hai, toh training mein hum poori distribution dekhte hain.
Practical Implementation Strategy
Conceptual structure – deriving the composition
Step 1: Random transformations (applied with probability p)
if random() < 0.5: image = horizontal_flip(image)
if random() < 0.3: angle = uniform(-15, 15) # degrees image = rotate(image, angle)
Step 2: Color jittering
brightness_factor = uniform(0.8, 1.2) contrast_factor = uniform(0.8, 1.2) image = adjust_brightness(image, brightness_factor) image = adjust_contrast(image, contrast_factor)
Step 3: Crop and resize (always applied)
scale = uniform(0.08, 1.0) aspect_ratio = uniform(3/4, 4/3) image = random_resized_crop(image, scale, aspect_ratio)
Step 4: Normalization (always applied last)
image = (image - mean) / std
**Yeh order kyun**:
1. Geometric transforms pehle (yeh pixel values pe depend nahi karte)
2. Color transforms baad mein (normalization se pehle jo unhe invalid kar de)
3. Crop/resize (final spatial dimensions establish karta hai)
4. Normalization sabse last (network input ke liye prepare karta hai)
> [!example] Domain-Specific Augmentation: Medical Imaging
> **Scenario**: Pneumonia detection ke liye Chest X-ray classification.
**Applicable augmentations**:
- Chhote rotations (±5°): Patient positioning vary karta hai
- Brightness/contrast: Alag X-ray machine settings
- Zoom/crop: Alag distances, body sizes
**Inappropriate augmentations**:
- Horizontal flip: Heart left side pe hota hai! Flipping anatomy change kar deta hai (situs inversus rare pathology hai)
- Vertical flip: Koi ulte X-rays nahi leta
- Strong color changes: X-rays grayscale hote hain, color meaningful nahi hai
- Critical regions pe Cutout: Exact pathology mask ho sakti hai
**Yeh kyun matter karta hai**: Galat augmentations model ko unrealistic patterns sikha sakti hain ya diagnostic information destroy kar sakti hain.
> [!example] Effective Dataset Size Calculate Karna
> **Given**:
> - Original dataset: 10,000 images
> - Augmentations per epoch:
> - Random crop: ~100 possible positions
> - Rotation: $[-15°, 15°]$ se uniform → continuous
> - Flip: 2 states (flipped/not)
> - Brightness/contrast: continuous
**Calculation**:
Agar hum augmentations randomly har epoch sample karein, toh exact same augmented image dobara dekhne ki probability near zero hai. 100 epochs mein:
$$N_{\text{effective}} \approx 10000 \times 100 = 1,000,000 \text{ unique augmented images}$$
**Yeh kyun help karta hai**: Model zero additional collection cost mein 100× zyada "training data" dekhta hai.
---
## Common Mistakes
> [!mistake] Mistake 1: Test Data Augment Karna
> **Galat approach**: Inference ke dauran random augmentation apply karna.
**Kyun sahi lagta hai**: "Agar augmentation training help karta hai, toh testing mein bhi karna chahiye!"
**Fix**:
- **Training**: Invariances seekhne ke liye random augmentations apply karein
- **Testing**: Original images use karein YA Test-Time Augmentation (TTA) systematically apply karein
**Kyun**: Single-image inference ke dauran random augmentation training ke averaging benefits ke bina noise add karta hai. TTA alag hai: multiple augmented versions pe predict karein aur predictions average karein (variance reduce hoti hai).
**Correct TTA**:
$$\hat{y} = \frac{1}{K} \sum_{i=1}^{K} f_\theta(t_i(x_{\text{test}}))$$
> [!mistake] Mistake 2: Non-Label-Preserving Transforms
> **Galat approach**: Digit recognition ke liye vertical flipping (6 ban jaata hai 9), ya anatomical asymmetry wali medical images ko horizontally flip karna.
**Kyun sahi lagta hai**: "Zyada data hamesha better hota hai!"
**Fix**:
- Domain experts se poochho kaunsi variations meaning preserve karti hain
- Chhote set pe test karein: Kya insaan augmented images ko sahi label dete hain?
**Mathematical consequence**: Agar transformation $t$ label preserve nahi karta, toh hum corrupted data pe train kar rahe hain:
$$\mathbb{E}_{t}[\ell(f_\theta(t(x)), y)] \text{ is undefined when } t(x) \not\rightarrow y$$
> [!mistake] Mistake 3: Over-Augmentation
> **Galat approach**: ±180° rotate karna, brightness 90% reduce karna, original area ka 1% tak crop karna.
**Kyun sahi lagta hai**: "Maximum variation matlab maximum performance!"
**Fix**: Augmentation **plausible variations** create karni chahiye, signal destroy nahi karna chahiye.
**Empirical test**: Agar augmented training set pe accuracy significantly drop kare, toh augmentation bahut aggressive hai. Rule of thumb: humans ki accuracy augmented examples pe >95% rehni chahiye.
---
## Connections
- [[3.4.1-Convolution-operation]]: CNNs mein kuch built-in translation invariance hoti hai, augmentation isse extend karta hai
- [[3.4.8-Dropout-in-neural-networks]]: Dono regularization techniques hain, aksar saath use hoti hain
- [[3.4.12-Transfer-learning-CNs]]: Pre-trained models augmentation ke saath train hue the, fine-tuning mein use karna consistency maintain karta hai
- [[5.2.3-Overfitting-vs-underfitting]]: Augmentation ka primary purpose overfitting reduce karna hai
- [[3.4.16-Batch-normalization]]: Normalization hamesha augmentation ke baad final step hoti hai
- [[6.14-Generalization-in-ML]]: Augmentation effective training distribution expand karke generalization improve karta hai
---
## Summary
Data augmentation ek **synthetic data generation** technique hai jo training images pe label-preserving transformations apply karti hai, effectively dataset size multiply karti hai aur models ko invariant features seekhne par majboor karti hai. Core principle hai test time pe hone wali realistic variations simulate karna, network ko training data memorize karne ki jagah robust, transferable patterns pe focus karne par majboor karna.
Key augmentation categories: geometric (rotation, flip, crop), photometric (brightness, contrast, color), aur modern techniques (cutout, mixup). Mathematical framework augmentation ko ek transformation distribution se sampling ke roop mein dikhata hai, training aur test distributions ke beech gap reduce karta hai.
Critical considerations: transformations labels preserve karni chahiye, domain-appropriate honi chahiye, aur realistic bounds ke andar rehni chahiye. Augmentation sirf training ke dauran apply hoti hai (Test-Time Augmentation ko chhodkar), aur pipeline mein normalization se pehle honi chahiye.
---
> [!recall]- Ek 12-saal ke bachche ko samjhao
> Socho tum ek robot ko cats recognize karna sikha rahe ho. Agar tum use sirf 100 cat photos dikhao, toh woh un specific photos ko memorize kar sakta hai instead of seekhne ke ki cat kya hoti hai.
Toh yeh trick hai: har photo lo aur naye versions banao! Thoda tilt karo, zoom in karo, brighter ya darker banao, horizontally flip karo. Ab 100 photos se thousands of versions ho gaye.
Robot ab memorize nahi kar sakta – use real patterns seekhne PADTE HAIN: "pointy ears, whiskers, fur texture." Jab woh real world mein cat dekhta hai (shayad alag angle ya alag lighting mein), woh phir bhi recognize karta hai kyunki usne actual concept seekha, sirf specific pictures nahi.
Yahi data augmentation hai! Hum naye photos nahi collect kar rahe; hum jinke paas hain unhe smart modifications kar rahe hain, taaki AI same amount of actual data se better seekhe.
---
> [!mnemonic] GRASP Augmentation
> **G**eometric (rotate, flip, crop)
> **R**andom (har epoch alag transforms sample karein)
> **A**ppropriate (domain-specific, label-preserving)
> **S**equential (transforms → normalize → network)
> **P**hotometric (brightness, contrast, color)
---
#flashcards/ai-ml
Data augmentation kya hai? :: Training datasets ko artificially expand karna existing images pe label-preserving transformations apply karke, naya data collect kiye bina generalization improve karna.
Data augmentation overfitting kyun reduce karta hai? ::: Yeh effective training set size badhata hai aur model ko invariant features seekhne par majboor karta hai jo transformations mein kaam karein, instead of specific training examples memorize karne ke.
Augmentation transformation ke liye key requirement kya hai? ::: Yeh label-preserving honi chahiye – transformed image ka ground-truth label original jaisa hona chahiye.
Angle θ ke liye rotation matrix likhein ::: $\begin{bmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{bmatrix}$
Brightness/contrast adjustment ka formula kya hai? ::: $p' = \alpha \cdot p + \beta$ jahan α contrast factor hai aur β brightness offset hai.
MixUp kaise kaam karta hai? ::: Linear interpolation se synthetic examples banata hai: $\tilde{x} = \lambda x_i + (1-\lambda) x_j$ aur $\tilde{y} = \lambda y_i + (1-\lambda) y_j$ jahan λ ~ Beta(α,α).
Chest X-rays ke liye horizontal flip appropriate kyun NAHI hai? ::: Heart anatomically left side pe hota hai; flipping ek unrealistic reversed anatomy (situs inversus) create karta, jo normal variation nahi balki rare pathology hai.
Augmentation pipeline ka correct order kya hai? ::: 1) Geometric transforms 2) Color transforms 3) Crop/resize 4) Normalization (hamesha last).
Cutout augmentation kya hai? ::: Random rectangular regions ko zeros ya noise se mask karna, network ko multiple distributed features use karne aur occlusion handle karne par majboor karta hai.
Kya test data augment karna chahiye? ::: Nahi, single predictions ke liye original images use karein. Test-Time Augmentation (TTA) alag hai: multiple augmented versions pe predict karein aur average karein.
Augmentation ka mathematical framework kya hai? ::: $\mathcal{L}_{\text{aug}}(\theta) = \mathbb{E}_{(x,y) \sim P_{\text{train}}} \mathbb{E}_{t \sim T}[\ell(f_\theta(t(x)), y)]$ – data aur transformations dono par expectation.
Random resized crop kyun help karta hai? ::: Scale invariance (alag sizes mein objects) aur translation invariance (alag positions mein objects) provide karta hai, real-world variation in object scale aur location simulate karta hai.
MixUp λ parameter ke liye kaunsi distribution use hoti hai? ::: Beta(α, α) distribution, typically α = 0.2 ya 0.4 ke saath, jo 0.5 ke around symmetric distribution create karta hai.
Augmentation ke saath effective dataset size calculate karein ::: Original N images × alag random augmentations wale epochs ≈ N × epochs of effectively unique training samples (typically 10-100× increase).
## 🖼️ Concept Map
```mermaid
flowchart TD
DA[Data Augmentation] -->|applies| LPT[Label-Preserving Transforms]
DA -->|improves| GEN[Generalization]
DA -->|reduces| OF[Overfitting]
DA -->|forces learning of| INV[Invariances]
INV -->|learn concept not| MEM[Memorized Pixels]
LPT -->|must preserve| LABEL[Semantic Label y]
LPT -->|domain-specific| CONSTRAINT[Realistic Variations]
LPT -->|example type| GEO[Geometric Transforms]
GEO -->|includes| ROT[Rotation by theta]
ROT -->|expressed as| MAT[Rotation Matrix]
MAT -->|derived from| POLAR[Polar Coordinates]
GEN -->|without| NEWDATA[New Data Collection]