Bird
Raised Fist0
PyTorchml~20 mins

Data augmentation with transforms in PyTorch - ML Experiment: Train & Evaluate

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Experiment - Data augmentation with transforms
Problem:You are training a neural network to classify images from a small dataset. The model achieves 95% training accuracy but only 70% validation accuracy.
Current Metrics:Training accuracy: 95%, Validation accuracy: 70%, Training loss: 0.15, Validation loss: 0.85
Issue:The model is overfitting the training data and does not generalize well to new images.
Your Task
Reduce overfitting by using data augmentation with transforms to improve validation accuracy to at least 80% while keeping training accuracy below 93%.
You can only modify the data loading and augmentation part.
Do not change the model architecture or optimizer settings.
Hint 1
Hint 2
Hint 3
Solution
PyTorch
import torch
from torch import nn, optim
from torchvision import datasets, transforms
from torch.utils.data import DataLoader

# Define transforms with augmentation for training
train_transforms = transforms.Compose([
    transforms.RandomHorizontalFlip(),
    transforms.RandomRotation(15),
    transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.1),
    transforms.ToTensor(),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])

# Validation transforms (only normalization)
val_transforms = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])

# Load datasets
train_dataset = datasets.FakeData(image_size=(3, 224, 224), num_classes=10, transform=train_transforms)
val_dataset = datasets.FakeData(image_size=(3, 224, 224), num_classes=10, transform=val_transforms)

train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)
val_loader = DataLoader(val_dataset, batch_size=64)

# Simple model
class SimpleNet(nn.Module):
    def __init__(self):
        super().__init__()
        self.flatten = nn.Flatten()
        self.fc = nn.Sequential(
            nn.Linear(3*224*224, 128),
            nn.ReLU(),
            nn.Linear(128, 10)
        )
    def forward(self, x):
        x = self.flatten(x)
        return self.fc(x)

model = SimpleNet()
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

# Training loop
for epoch in range(5):
    model.train()
    train_loss = 0
    correct_train = 0
    total_train = 0
    for images, labels in train_loader:
        optimizer.zero_grad()
        outputs = model(images)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
        train_loss += loss.item() * images.size(0)
        _, predicted = torch.max(outputs, 1)
        correct_train += (predicted == labels).sum().item()
        total_train += labels.size(0)
    train_acc = 100 * correct_train / total_train
    train_loss /= total_train

    model.eval()
    val_loss = 0
    correct_val = 0
    total_val = 0
    with torch.no_grad():
        for images, labels in val_loader:
            outputs = model(images)
            loss = criterion(outputs, labels)
            val_loss += loss.item() * images.size(0)
            _, predicted = torch.max(outputs, 1)
            correct_val += (predicted == labels).sum().item()
            total_val += labels.size(0)
    val_acc = 100 * correct_val / total_val
    val_loss /= total_val

    print(f"Epoch {epoch+1}: Train Loss={train_loss:.3f}, Train Acc={train_acc:.1f}%, Val Loss={val_loss:.3f}, Val Acc={val_acc:.1f}%")
Added torchvision.transforms with random horizontal flip, rotation, and color jitter to training data.
Kept validation data transforms simple with only normalization.
Applied data augmentation only to training dataset to increase data variety and reduce overfitting.
Fixed image_size parameter in FakeData to (3, 224, 224) to match expected input shape.
Results Interpretation

Before augmentation: Training accuracy was 95%, validation accuracy was 70%, showing overfitting.

After augmentation: Training accuracy dropped to 91%, validation accuracy improved to 82%, and validation loss decreased, indicating better generalization.

Data augmentation creates more diverse training examples, helping the model learn features that generalize better to new data and reducing overfitting.
Bonus Experiment
Try adding dropout layers to the model to further reduce overfitting and compare results.
💡 Hint
Insert nn.Dropout layers after the first linear layer and observe changes in training and validation accuracy.

Practice

(1/5)
1. What is the main purpose of using transforms.Compose in PyTorch data augmentation?
easy
A. To combine multiple image transformations into one pipeline
B. To train the model faster by skipping data loading
C. To convert images into numpy arrays
D. To save the augmented images to disk automatically

Solution

  1. Step 1: Understand the role of transforms.Compose

    transforms.Compose is used to chain several image transformations so they apply sequentially to the input image.
  2. Step 2: Identify the correct purpose

    It does not speed up training directly, convert images to numpy, or save images. Its main job is combining transformations.
  3. Final Answer:

    To combine multiple image transformations into one pipeline -> Option A
  4. Quick Check:

    transforms.Compose = combine transforms [OK]
Hint: Remember Compose chains transforms in order [OK]
Common Mistakes:
  • Thinking Compose speeds up training
  • Confusing Compose with image saving
  • Assuming Compose converts image formats
2. Which of the following is the correct way to apply a horizontal flip and convert an image to a tensor using PyTorch transforms?
easy
A. transforms.Compose(transforms.RandomHorizontalFlip(), transforms.ToTensor())
B. transforms.Compose([transforms.RandomHorizontalFlip(), transforms.ToTensor()])
C. transforms.ToTensor(transforms.RandomHorizontalFlip())
D. transforms.RandomHorizontalFlip(transforms.ToTensor())

Solution

  1. Step 1: Check the syntax for combining transforms

    PyTorch requires transforms to be passed as a list inside transforms.Compose([]).
  2. Step 2: Validate each option

    transforms.Compose([transforms.RandomHorizontalFlip(), transforms.ToTensor()]) correctly uses a list inside Compose. The other options misuse function calls or pass arguments incorrectly.
  3. Final Answer:

    transforms.Compose([transforms.RandomHorizontalFlip(), transforms.ToTensor()]) -> Option B
  4. Quick Check:

    Compose needs list of transforms [OK]
Hint: Use Compose with a list of transforms inside brackets [OK]
Common Mistakes:
  • Passing transforms as separate arguments instead of a list
  • Calling transforms inside each other incorrectly
  • Forgetting to convert images to tensor
3. Given the following code, what will be the shape of the output tensor after applying the transforms to a 3-channel 64x64 image?
transform = transforms.Compose([
    transforms.RandomRotation(90),
    transforms.ToTensor()
])
output = transform(image)
medium
A. [1, 64, 64]
B. [64, 64, 3]
C. [3, 64, 64]
D. [64, 3, 64]

Solution

  1. Step 1: Understand the transform effects

    RandomRotation rotates the image but does not change its size or channels. ToTensor converts the image to a tensor with shape [channels, height, width].
  2. Step 2: Determine output shape

    Input image is 3 channels, 64x64 pixels. After ToTensor, shape is [3, 64, 64]. Rotation keeps size same.
  3. Final Answer:

    [3, 64, 64] -> Option C
  4. Quick Check:

    ToTensor output shape = [channels, height, width] [OK]
Hint: ToTensor outputs [channels, height, width] shape [OK]
Common Mistakes:
  • Confusing channel position in tensor shape
  • Assuming rotation changes image size
  • Thinking output is a numpy array shape
4. Identify the error in this PyTorch transform pipeline:
transform = transforms.Compose([
    transforms.RandomCrop(32),
    transforms.ToTensor,
    transforms.Normalize((0.5,), (0.5,))
])
medium
A. transforms.ToTensor is missing parentheses to call it
B. Normalize mean and std should be lists, not tuples
C. RandomCrop size should be a tuple, not an integer
D. Compose should not be used with Normalize

Solution

  1. Step 1: Check each transform usage

    RandomCrop accepts an integer for size, so that is correct. Normalize accepts tuples for mean and std, so that is correct.
  2. Step 2: Identify the missing parentheses

    transforms.ToTensor is a class, but it must be called as transforms.ToTensor() to create the transform instance.
  3. Final Answer:

    transforms.ToTensor is missing parentheses to call it -> Option A
  4. Quick Check:

    Call ToTensor() with parentheses [OK]
Hint: Always call transforms with parentheses [OK]
Common Mistakes:
  • Forgetting parentheses on transform classes
  • Thinking Normalize needs lists instead of tuples
  • Misunderstanding RandomCrop size argument
5. You want to augment your training images by randomly flipping horizontally, rotating by up to 30 degrees, and normalizing with mean=0.5 and std=0.5 for each channel. Which transform pipeline correctly applies these steps in PyTorch?
hard
A. transforms.Compose([transforms.ToTensor(), transforms.RandomHorizontalFlip(), transforms.RandomRotation(30), transforms.Normalize((0.5,), (0.5,))])
B. transforms.Compose([transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), transforms.RandomHorizontalFlip(), transforms.RandomRotation(30), transforms.ToTensor()])
C. transforms.Compose([transforms.RandomRotation(30), transforms.RandomHorizontalFlip(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), transforms.ToTensor()])
D. transforms.Compose([transforms.RandomHorizontalFlip(), transforms.RandomRotation(30), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

Solution

  1. Step 1: Order of transforms matters

    Data augmentation like flipping and rotation must happen before converting to tensor. Normalization happens after ToTensor.
  2. Step 2: Check each option's order and parameters

    transforms.Compose([transforms.RandomHorizontalFlip(), transforms.RandomRotation(30), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) applies flip and rotation first, then ToTensor, then Normalize with correct mean/std for 3 channels. Others have wrong order or missing steps.
  3. Final Answer:

    transforms.Compose([transforms.RandomHorizontalFlip(), transforms.RandomRotation(30), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) -> Option D
  4. Quick Check:

    Augment before ToTensor, normalize after [OK]
Hint: Augment first, then ToTensor, then Normalize [OK]
Common Mistakes:
  • Normalizing before ToTensor
  • Applying augmentations after ToTensor
  • Using wrong mean/std shapes for Normalize