Course contentsShow
← Machine Learning and Deep Learning
Lesson 821 of 3,53819. PyTorch FundamentalsFree lesson

Transforms and Data Preprocessing Pipelines

Build composable transform pipelines using torchvision.transforms for standardization and augmentation.

Transforms and Data Preprocessing Pipelines

What You'll Learn

You'll learn how to build composable transform pipelines using torchvision.transforms to standardize and augment your data before feeding it to your model.

The Problem: Raw Data Isn't Ready

When you load images from a dataset, they often come in different sizes, color ranges, and formats. Your neural network expects consistent, normalized tensors. Plus, you want to apply augmentations (like random flips or rotations) on-the-fly during training. Doing this manually for each sample would be messy and repetitive.

Composable Transform Pipelines

Think of transforms as a conveyor belt where each station performs one operation on your data. torchvision.transforms.Compose lets you chain these operations together into a single pipeline.

Common transforms include:

  • Resize: Make all images the same dimensions
  • ToTensor: Convert PIL images to PyTorch tensors
  • Normalize: Standardize pixel values (mean/std normalization)
  • RandomHorizontalFlip: Data augmentation for training
  • ColorJitter: Randomly adjust brightness, contrast, etc.

How It Works

from torchvision import transforms

train_transform = transforms.Compose([
    transforms.Resize((224, 224)),
    transforms.RandomHorizontalFlip(p=0.5),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406],
                        std=[0.229, 0.224, 0.225])
])

You pass this train_transform to your custom Dataset's __init__ or __getitem__ method. Each time you fetch a sample, it automatically flows through the entire pipeline—resize → augment → tensorize → normalize.

For validation/test data, you typically skip augmentations but keep standardization:

val_transform = transforms.Compose([
    transforms.Resize((224, 224)),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406],
                        std=[0.229, 0.224, 0.225])
])

Key Takeaway: Transform pipelines let you compose preprocessing and augmentation operations cleanly, ensuring consistent data preparation and easy-to-swap augmentation strategies between training and evaluation.