How to Use Compose Transforms in PyTorch for Image Preprocessing
In PyTorch, use
transforms.Compose to chain multiple image transformations into one pipeline. This lets you apply several preprocessing steps like resizing, converting to tensor, and normalizing in sequence with a single call.Syntax
The transforms.Compose function takes a list of transformation functions and applies them in order to an input image or data. Each transform is called one after another.
transforms.Compose([transform1, transform2, ...]): Combines multiple transforms.transform1, transform2, ...: Individual transform functions likeResize,ToTensor,Normalize.
python
from torchvision import transforms transform_pipeline = transforms.Compose([ transforms.Resize((128, 128)), transforms.ToTensor(), transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]) ])
Example
This example shows how to create a Compose transform pipeline to resize an image, convert it to a tensor, and normalize it. Then it applies the pipeline to a sample image loaded with PIL.
python
from PIL import Image from torchvision import transforms import torch # Define the transform pipeline transform_pipeline = transforms.Compose([ transforms.Resize((128, 128)), transforms.ToTensor(), transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]) ]) # Load an example image (replace 'example.jpg' with a valid image path) image = Image.new('RGB', (256, 256), color='red') # Using a red image for demo # Apply the transform pipeline transformed_image = transform_pipeline(image) # Print the tensor shape and type print(f"Transformed image tensor shape: {transformed_image.shape}") print(f"Tensor type: {type(transformed_image)}")
Output
Transformed image tensor shape: torch.Size([3, 128, 128])
Tensor type: <class 'torch.Tensor'>
Common Pitfalls
Common mistakes when using Compose include:
- Not converting images to tensor before normalization, causing errors.
- Applying transforms in the wrong order, e.g., normalizing before converting to tensor.
- Using incompatible transforms for the data type (e.g., applying image transforms to non-image data).
Always ensure ToTensor() comes before Normalize() in the pipeline.
python
from torchvision import transforms # Wrong order: Normalize before ToTensor (will cause error) wrong_pipeline = transforms.Compose([ transforms.Normalize(mean=[0.5], std=[0.5]), transforms.ToTensor() ]) # Correct order correct_pipeline = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean=[0.5], std=[0.5]) ])
Quick Reference
| Transform | Purpose | Notes |
|---|---|---|
| Resize(size) | Change image size | Size can be int or tuple (height, width) |
| ToTensor() | Convert PIL image or numpy array to PyTorch tensor | Must be before Normalize |
| Normalize(mean, std) | Normalize tensor image with mean and std | Input tensor expected in [C, H, W] format |
| RandomHorizontalFlip() | Randomly flip image horizontally | Used for data augmentation |
| Compose([transforms]) | Chain multiple transforms | Transforms applied in order |
Key Takeaways
Use transforms.Compose to chain multiple image transformations in PyTorch.
Always convert images to tensor with ToTensor() before applying Normalize().
Order of transforms matters; Compose applies them sequentially.
Compose helps keep preprocessing code clean and reusable.
Test your transform pipeline on sample data to avoid common errors.