How to beginner · 3 min read

How to preprocess data with transforms in PyTorch

Quick answer
Use torchvision.transforms to compose preprocessing steps like resizing, normalization, and tensor conversion. Apply these transforms to datasets by passing them to torchvision.datasets or custom datasets for efficient data loading and augmentation.

PREREQUISITES

  • Python 3.8+
  • pip install torch torchvision

Setup

Install PyTorch and torchvision to access dataset utilities and transforms.

bash
pip install torch torchvision

Step by step

This example shows how to preprocess images by resizing, converting to tensor, and normalizing using torchvision.transforms. Then it loads the CIFAR10 dataset with these transforms applied.

python
import torch
from torchvision import datasets, transforms
from torch.utils.data import DataLoader

# Define preprocessing transforms
transform = transforms.Compose([
    transforms.Resize((32, 32)),          # Resize images to 32x32
    transforms.ToTensor(),                  # Convert PIL image to tensor
    transforms.Normalize((0.5, 0.5, 0.5),  # Normalize RGB channels
                         (0.5, 0.5, 0.5))
])

# Load CIFAR10 dataset with transforms
train_dataset = datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)

# Create DataLoader
train_loader = DataLoader(train_dataset, batch_size=4, shuffle=True)

# Iterate over one batch
images, labels = next(iter(train_loader))
print(f'Batch image tensor shape: {images.shape}')
print(f'Batch labels: {labels}')
output
Files already downloaded and verified
Batch image tensor shape: torch.Size([4, 3, 32, 32])
Batch labels: tensor([3, 8, 8, 0])

Common variations

You can customize transforms for different tasks:

  • Use transforms.RandomHorizontalFlip() for data augmentation.
  • Apply transforms.ColorJitter() to vary brightness and contrast.
  • Chain transforms differently for validation (usually no augmentation, just normalization).
  • Use transforms.Lambda() for custom preprocessing functions.
python
transform_augmented = transforms.Compose([
    transforms.Resize((32, 32)),
    transforms.RandomHorizontalFlip(p=0.5),
    transforms.ColorJitter(brightness=0.2, contrast=0.2),
    transforms.ToTensor(),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])

Troubleshooting

If images are not loading correctly, verify the dataset path and that download=True is set for first-time downloads. If normalization causes unexpected results, check the mean and std values match your dataset. For shape mismatches, ensure Resize or CenterCrop is applied before ToTensor.

Key Takeaways

  • Use transforms.Compose to chain multiple preprocessing steps cleanly.
  • Apply transforms directly in dataset loading for efficient on-the-fly preprocessing.
  • Customize transforms for training and validation to improve model generalization.
  • Always normalize images with dataset-specific mean and std for stable training.
Verified 2026-04
Verify ↗