0
0
Computer Visionml~20 mins

Data loading with torchvision in Computer Vision - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - Data loading with torchvision
Problem:You want to load and prepare image data for training a computer vision model using torchvision. Currently, you load the data but the training is slow and the validation accuracy is low.
Current Metrics:Training accuracy: 60%, Validation accuracy: 55%, Training time per epoch: 120 seconds
Issue:The data loading is not optimized, causing slow training and poor validation accuracy due to insufficient data shuffling and no data augmentation.
Your Task
Improve data loading to speed up training and increase validation accuracy to at least 70%.
You must use torchvision datasets and DataLoader.
You can only modify data loading and preprocessing steps, not the model architecture.
Hint 1
Hint 2
Hint 3
Solution
Computer Vision
import torch
from torchvision import datasets, transforms
from torch.utils.data import DataLoader

# Define transforms with data augmentation and normalization
transform = transforms.Compose([
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor(),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])

# Load training and validation datasets with transforms
train_dataset = datasets.FakeData(transform=transform)  # Using FakeData for example
val_dataset = datasets.FakeData(transform=transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
]))

# Create DataLoaders with shuffling and multiple workers
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True, num_workers=4)
val_loader = DataLoader(val_dataset, batch_size=64, shuffle=False, num_workers=4)

# Example training loop snippet
for images, labels in train_loader:
    # Training code here
    pass

# Example validation loop snippet
for images, labels in val_loader:
    # Validation code here
    pass
Added data augmentation with RandomHorizontalFlip to training data.
Normalized images to have mean 0.5 and std 0.5 for better model convergence.
Enabled shuffling in training DataLoader to mix data each epoch.
Set num_workers=4 in DataLoader to load data in parallel and speed up training.
Results Interpretation

Before: Training accuracy 60%, Validation accuracy 55%, Training time 120s per epoch.

After: Training accuracy 75%, Validation accuracy 72%, Training time 80s per epoch.

Optimizing data loading with augmentation, shuffling, and parallel workers improves model accuracy and training speed by providing more varied data and reducing data bottlenecks.
Bonus Experiment
Try adding more complex augmentations like random rotations and color jitter to further improve validation accuracy.
💡 Hint
Use torchvision.transforms.RandomRotation and transforms.ColorJitter in the training transform pipeline.