Machine Learning Academy · درس

زيادة البيانات: تحويلات لتعزيز المتانة

سيطبّق المتعلمون القلب الأفقي والقص وتغيير الألوان عشوائيًا عبر torchvision.transforms، ويقيسون تحسّن الدقة الناتج عن زيادة البيانات.

الدرس 4 من 413 خطوة

زيادة البيانات: تحويلات لتعزيز المتانة درس مجاني في Machine Learning Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Machine Learning Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Machine Learning Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

What Is Data Augmentation?

Data augmentation artificially increases the size and diversity of the training dataset by applying random transformations to existing images. Instead of collecting new data (expensive), augmentation creates new training examples on the fly. The key insight is that for many tasks, the label should remain the same under the transformation — a horizontally flipped image of a cat is still a cat. Augmentation reduces overfitting and teaches the model to be invariant to irrelevant variations.

import torchvision.transforms as transforms
from PIL import Image
import torch

# A simple augmentation pipeline
transform = transforms.Compose([
    transforms.RandomHorizontalFlip(p=0.5),
    transforms.RandomRotation(degrees=15),
    transforms.ColorJitter(brightness=0.3),
    transforms.ToTensor()
])

# Apply to same image -> different result each time
# img = Image.open('cat.jpg')
# aug1 = transform(img)  # one augmented version
# aug2 = transform(img)  # different augmented version
print('Augmentation pipeline defined')

torchvision.transforms: The Augmentation Toolkit

torchvision.transforms provides a rich library of image transforms. Key categories: geometric (flip, rotate, crop, resize, perspective); color (jitter, grayscale, solarize, equalize); pixel-level (Gaussian blur, random erasing); and tensor operations (ToTensor, Normalize). They chain together with transforms.Compose. From PyTorch 2.0, transforms.v2 provides improved speed and additional augmentation primitives.

import torchvision.transforms as transforms

# Catalog of key transforms
train_tf = transforms.Compose([
    transforms.Resize(36),                    # scale
    transforms.RandomCrop(32, padding=4),     # spatial
    transforms.RandomHorizontalFlip(),        # mirror
    transforms.RandomRotation(10),            # rotate
    transforms.RandomGrayscale(p=0.1),        # de-color
    transforms.ColorJitter(
        brightness=0.3, contrast=0.3,
        saturation=0.3, hue=0.1),             # color
    transforms.GaussianBlur(kernel_size=3,    # blur
                            sigma=(0.1, 1.0)),
    transforms.ToTensor(),
    transforms.Normalize([0.5]*3, [0.5]*3)
])
print('Full augmentation pipeline ready')

RandomHorizontalFlip and RandomVerticalFlip

RandomHorizontalFlip(p=0.5) mirrors the image left-to-right with probability p. This is one of the most effective augmentations for natural images — most objects look equally valid when mirrored. RandomVerticalFlip flips top-to-bottom; use it only when vertical flipping makes semantic sense (e.g., satellite imagery, microscopy). Never use vertical flip for upright objects like faces or digits — it would create unrealistic examples that confuse the model.

import torchvision.transforms as transforms
import torch

hflip = transforms.RandomHorizontalFlip(p=0.5)
vflip = transforms.RandomVerticalFlip(p=0.5)

# Both input and output are PIL images or tensors
# On a tensor: shape (C, H, W)

t = torch.arange(12.).reshape(1, 3, 4)  # 1ch, 3x4
flipped = transforms.functional.hflip(t)
print('Original:', t)
print('H-flipped:', flipped)  # columns reversed

RandomCrop for Spatial Invariance

RandomCrop(size, padding) pads the image (typically with zeros) then crops a random sub-region. For CIFAR-10 (32x32), padding=4 and crop size 32 is standard — it shifts the image content by up to 4 pixels in any direction. This teaches the model that the object label is independent of its exact position, providing translation invariance. CenterCrop is used at test time for deterministic central cropping from a slightly larger image.

import torchvision.transforms as transforms
import torch

# Training: random crop -> trains on shifted versions
train_crop = transforms.Compose([
    transforms.Pad(4, padding_mode='reflect'),  # pad 4 pixels
    transforms.RandomCrop(32)                  # random 32x32 region
])

# Test: deterministic center crop (no padding needed if same size)
test_crop = transforms.CenterCrop(32)

# RandomResizedCrop: zoom + crop (ImageNet-style)
rrc = transforms.RandomResizedCrop(
    224, scale=(0.08, 1.0), ratio=(0.75, 1.33)
)
print('Random crop transforms ready')

ColorJitter: Colour Augmentation

ColorJitter randomly perturbs brightness (how light/dark), contrast (difference between darks and lights), saturation (colour intensity), and hue (colour shift). These simulate variations in lighting, camera settings, and environmental conditions. Strong colour augmentation (jitter with 0.4 on each parameter) is part of the SimCLR self-supervised learning recipe. For standard supervised training, moderate values (0.2-0.3) are recommended.

import torchvision.transforms as transforms

# Moderate color augmentation for supervised learning
color_aug = transforms.ColorJitter(
    brightness=0.2,   # multiply by U[0.8, 1.2]
    contrast=0.2,     # multiply by U[0.8, 1.2]
    saturation=0.2,   # multiply by U[0.8, 1.2]
    hue=0.05          # shift hue by U[-0.05, 0.05]
)

# Strong color augmentation for self-supervised learning
strong_color = transforms.ColorJitter(
    brightness=0.8, contrast=0.8,
    saturation=0.8, hue=0.2
)

print('Color augmentation configured')

RandomRotation and RandomPerspective

RandomRotation(degrees) rotates the image by a random angle within [-degrees, +degrees]. Use small angles (10-15 degrees) for natural images; larger for medical imaging or scientific data where arbitrary orientations are valid. RandomPerspective applies a random projective transformation that simulates viewing the object from a different angle — as if the camera had tilted. Both add robustness to viewpoint changes at the cost of distorting rectangular features.

import torchvision.transforms as transforms

# Mild rotation for natural images
rotate = transforms.RandomRotation(
    degrees=15,
    fill=0            # fill empty corners with black
)

# Perspective distortion
perspective = transforms.RandomPerspective(
    distortion_scale=0.3,
    p=0.3             # apply 30% of the time
)

# Affine: combines rotation, scale, shear, translate
affine = transforms.RandomAffine(
    degrees=10,
    translate=(0.1, 0.1),
    scale=(0.9, 1.1)
)
print('Geometric augmentations defined')

Random Erasing: Occlusion Robustness

RandomErasing randomly masks out a rectangular region of the image by replacing it with zeros, random noise, or the mean pixel value. This simulates occlusion — when part of the object is blocked by another object. Models trained with random erasing learn to recognise objects from partial views, making them more robust in real-world scenes where occlusion is common. It is typically applied to the tensor (after ToTensor), not the PIL image.

import torchvision.transforms as transforms
import torch

# Apply to tensor after normalization
erasing = transforms.RandomErasing(
    p=0.5,           # probability of applying
    scale=(0.02, 0.2),  # fraction of image area to erase
    ratio=(0.3, 3.3),   # aspect ratio of erased region
    value=0           # fill with zeros
)

tf = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize([0.5]*3, [0.5]*3),
    erasing           # applied last on tensor
])
print('Random erasing added to pipeline')

AutoAugment and RandAugment

AutoAugment learns the best augmentation policy for a dataset using reinforcement learning — it was discovered that CIFAR-10 benefits from specific policies (shear X, rotate, equalize). RandAugment is simpler: randomly select N of K available operations and apply them at a magnitude M. Both consistently outperform hand-designed augmentation pipelines. PyTorch provides pre-trained policies via transforms.AutoAugment and transforms.RandAugment.

import torchvision.transforms as transforms

# AutoAugment with CIFAR10 policy
auto_aug = transforms.AutoAugment(
    policy=transforms.AutoAugmentPolicy.CIFAR10
)

# RandAugment: N ops, magnitude M
rand_aug = transforms.RandAugment(
    num_ops=2,         # apply 2 random operations
    magnitude=9        # strength of each operation (0-30)
)

train_tf = transforms.Compose([
    transforms.RandomCrop(32, padding=4),
    transforms.RandomHorizontalFlip(),
    rand_aug,          # or auto_aug
    transforms.ToTensor(),
    transforms.Normalize([0.4914, 0.4822, 0.4465],
                        [0.2023, 0.1994, 0.2010])
])

Online vs Offline Augmentation

There are two approaches to augmentation. Online augmentation applies transforms during training on-the-fly using the DataLoader — new augmented versions are created each epoch, providing effectively unlimited diversity. Offline augmentation pre-generates augmented images and saves them to disk — faster data loading but fixed diversity. Online augmentation (via transforms) is almost always preferred for deep learning because it creates new variety every epoch and requires no additional storage.

# Online augmentation: new random transform every epoch
# -> effectively infinite training data from finite images

train_dataset = torchvision.datasets.CIFAR10(
    root='./data', train=True,
    transform=train_transform,    # applied at load time
    download=True
)

# The SAME image gets a DIFFERENT augmented version each epoch!
# epoch 1: image 0 -> flipped, dark
# epoch 2: image 0 -> not flipped, brighter
# epoch 3: image 0 -> rotated, saturated
# This diversity is the core benefit of online augmentation
print('Online: new augmentation every epoch automatically')

Augmentation for Non-Image Data

Augmentation is not limited to images. For time-series: add Gaussian noise, scale amplitude, time-warp, or random crop sequences. For audio: pitch shift, time stretch, add background noise, random masking (SpecAugment for speech). For text: synonym replacement, back-translation, random insertion/deletion. For tabular data: add Gaussian noise to features, randomly permute rows in a batch. The principle is always the same: label-preserving variation that increases diversity.

import torch

# Tabular augmentation: Gaussian noise
def tabular_augment(X, noise_std=0.05):
    noise = torch.randn_like(X) * noise_std
    return X + noise  # label unchanged

# Time-series augmentation: amplitude scale
def ts_augment(series, scale_range=(0.8, 1.2)):
    scale = torch.empty(1).uniform_(*scale_range)
    return series * scale  # label unchanged

X = torch.randn(32, 10)   # 32 tabular samples, 10 features
X_aug = tabular_augment(X)
print('Augmented tabular shape:', X_aug.shape)

Measuring Augmentation Impact

Always measure the impact of augmentation quantitatively. Train the same model with and without augmentation for the same number of epochs and compare final validation accuracy. Also compare the training vs validation accuracy gap (generalisation gap) — good augmentation closes this gap. Too aggressive augmentation can actually hurt performance by making training too hard. Ablation studies (testing one augmentation type at a time) reveal which transforms contribute most to your specific task.

# Typical CIFAR-10 accuracy improvements from augmentation
results = {
    'No augmentation':               '73% val acc',
    '+ Random crop':                 '79% val acc (+6%)',
    '+ Horizontal flip':             '82% val acc (+3%)',
    '+ Color jitter':                '84% val acc (+2%)',
    '+ Random erasing':              '85% val acc (+1%)',
    '+ RandAugment':                 '87% val acc (+2%)',
    '+ Cutmix/Mixup':                '89% val acc (+2%)'
}

for aug, result in results.items():
    print(f'{aug}: {result}')

Quick Check

Test your understanding of Machine Learning with Python concepts from this lesson.

Lesson Recap

In this lesson you learned: data augmentation applies label-preserving random transforms during training to increase dataset diversity and reduce overfitting, key transforms include RandomCrop, RandomHorizontalFlip, ColorJitter, and RandomErasing, and AutoAugment and RandAugment automatically find effective augmentation policies, adding 2-6% accuracy over hand-designed pipelines. Next up we explore vanilla RNNs for sequential data processing.

البدء مجانًا

تعلم Python مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
30
الدروس
120

الأسئلة الشائعة

هل درس «زيادة البيانات: تحويلات لتعزيز المتانة» مجاني؟

نعم — نص درس «زيادة البيانات: تحويلات لتعزيز المتانة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Machine Learning Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Machine Learning Academy 4 دروس في المجموع.

ماذا ستتعلم في «زيادة البيانات: تحويلات لتعزيز المتانة»؟

سيطبّق المتعلمون القلب الأفقي والقص وتغيير الألوان عشوائيًا عبر torchvision.transforms، ويقيسون تحسّن الدقة الناتج عن زيادة البيانات. تتمرن على Machine Learning Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Machine Learning Academy؟

لا تُشترط خبرة سابقة. Machine Learning Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «زيادة البيانات: تحويلات لتعزيز المتانة»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Machine Learning Academy هذا؟

نعم. كل درس في Machine Learning Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. الالتفاف والمرشحات: اكتشاف الحواف والأنماط
  2. طبقات التجميع: خفض الأبعاد المكانية والثبات
  3. بناء وتدريب شبكة CNN على CIFAR-10
  4. زيادة البيانات: تحويلات لتعزيز المتانة
← العودة إلى Machine Learning Academy