Helper Function to Display Images
We begin by defining a helper function to visualize the original image alongside its transformed version. This function is essential for comparing the effects of different transformations in real time.Loading an Image with Pillow and PyTorch Transforms
In this section, we load an image of a cat using the Pillow library while utilizing both version 2 and version 1 of the transform APIs.
Resizing an Image
Resizing ensures consistent image dimensions across your dataset. In the example below, we resize the image to 50×25 pixels using the PyTorch v2 API.Random Horizontal Flip
Random horizontal flips augment your dataset by mirroring images randomly. In this demonstration, we set the flip probability to 100% (p=1) for clarity.For real-world applications, consider using a probability less than 1 (e.g., p=0.5) to introduce randomness in augmentation.
Converting Images to Tensors
Before feeding images into a PyTorch model, they must be converted into tensors. This transformation scales pixel intensity values appropriately for model consumption.Normalizing Tensor Images
Normalization adjusts pixel intensity values to a standardized range, which is crucial for faster model convergence. Here, we normalize the tensor with a mean and standard deviation of (0.5, 0.5, 0.5).Normalization typically shifts the pixel values to a range between -1 and 1, promoting efficient model training.
Random Cropping
Random cropping extracts a fixed-size region from an image, which is useful for data augmentation. In this example, we extract a 100×100 pixel patch.
Random Photometric Distortion
Photometric distortion augments images by adjusting brightness, contrast, saturation, and hue. This increases the variation in lighting conditions, helping to improve model generalization.Try changing the parameter ranges to see how variations in brightness and saturation impact the overall image appearance.
Random Resize
Random resizing applies variable scaling to images, introducing additional diversity into the dataset. Here, the image is randomly resized to a pixel size between 100 and 200.
Building Transformation Pipelines with Compose
The Compose class enables you to chain multiple transformations together into a single, streamlined pipeline. This approach ensures that every image undergoes the same sequence of augmentations.Using a transformation pipeline streamlines preprocessing and ensures consistency across your training data.
Applying Transformations to a Dataset
Next, we integrate a transformation pipeline with a real dataset: the Fashion MNIST dataset from TorchVision.transform argument:
Conclusion
In this lesson, we explored a variety of image transformation techniques using PyTorch’s TorchVision library. We covered resizing, flipping, cropping, photometric adjustments, normalization, and composing pipelines—all critical steps for effective image preprocessing and data augmentation. These techniques not only standardize your dataset but also improve model robustness and performance. Experiment with these transformations to optimize the augmentation strategies for your projects.