Skip to main content
Welcome to this technical lesson on PyTorch image transformations. In this guide, you’ll learn how to utilize PyTorch transformations for data preprocessing and augmentation to boost model performance and efficiency. PyTorch’s TorchVision library offers a comprehensive set of transformation classes that convert raw image data into formats that are optimized for model training and can augment your dataset by adding variability. Below, we demonstrate various transformation techniques—including resizing, random horizontal flips, tensor conversion, normalization, random cropping, photometric distortions, random resizing, and building transformation pipelines with Compose—each explained with its corresponding code snippet.

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.
You can now use this function to visually compare the before and after images for every transformation applied.

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.
The image shows a Jupyter Notebook interface with text discussing improving model accuracy and pipelines using PyTorch transformations. It includes a conclusion and a note about using transformations for image classification models.

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.
For those who prefer the v1 API, the same operation can be implemented as follows:

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.
Running the transformation multiple times yields crops from different parts of the image.
The image shows two side-by-side pictures of a cat on a black background. The left is labeled "Original Image," and the right is labeled "Random Crop," showing a slightly different framing of the cat.

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.
The image shows two side-by-side pictures of a cat against a black background. The left is labeled "Original Image," and the right is labeled "Random Resize."

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.
After applying the pipeline, you can view the transformed image as shown below:
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.
Now, we define a transformation pipeline tailored to the smaller dimensions of Fashion MNIST images:
You can integrate these transformations into the dataset by passing the pipeline as the 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.
The image shows two side-by-side visualizations labeled "Original Image" and "Pipeline Image," depicting a transformation process with color-coded pixel data.
Thank you for following along, and happy coding!

Watch Video

Practice Lab