Overview
Data transforms are used to preprocess and augment data before feeding it to models. While PyTorch core focuses on tensor operations, data transformations are commonly handled by:- torchvision.transforms - For image transformations (vision tasks)
- torchaudio.transforms - For audio transformations
- Custom transforms - User-defined transformations
This page covers the general transformation concepts. For vision-specific transforms, see the torchvision documentation.
Transform Composition
Compose multiple transforms together.Custom Transforms
Create custom transformation functions or classes.Function-Based Transform
Class-Based Transform
Common Transform Patterns
Training vs Validation Transforms
Transform with Dataset
Tensor Transforms
Transforms that work directly on PyTorch tensors.Normalization
Random Augmentation
Functional Transforms
Lower-level functional API for transforms.Multi-Modal Transforms
Transforms for multiple inputs (e.g., image + mask).Batched Transforms
Transforms that operate on batches.Advanced Patterns
Transform with State
Conditional Transforms
Common Transform Functions
Image Preprocessing
Data Augmentation
Best Practices
Performance
Performance
- Use
transforms.ToTensor()to convert to PyTorch tensors - Apply expensive transforms (e.g., resize) before cheap ones
- Consider caching transformed data for small datasets
- Use
num_workers > 0in DataLoader to parallelize transforms
Reproducibility
Reproducibility
- Set random seeds before creating transforms
- Use
torch.manual_seed()for deterministic augmentation - Document all transform parameters
- Save transform configuration with model checkpoints
Debugging
Debugging
- Visualize transformed samples to verify correctness
- Test edge cases (e.g., very small/large images)
- Check data ranges after normalization
- Verify transforms maintain label consistency
Augmentation Strategy
Augmentation Strategy
- Start with light augmentation, increase gradually
- Don’t augment validation/test data
- Use domain-specific augmentations
- Monitor if augmentation helps or hurts performance
Visualization Helper
See Also
- torchvision.transforms - Vision transforms
- torchaudio.transforms - Audio transforms
- Datasets - Dataset classes
- DataLoader - Data loading utilities