Data transforms help prepare and change data so a machine learning model can learn better. They make data easier to use and improve model results.
0
0
Data transforms in PyTorch
Introduction
When images need to be resized to fit the model input size.
When you want to convert raw data into tensors for PyTorch models.
When you want to normalize data so all values are on a similar scale.
When you want to add random changes like flipping or rotating images to make the model more robust.
When you want to convert data types, like from PIL images to PyTorch tensors.
Syntax
PyTorch
transform = torchvision.transforms.Compose([
torchvision.transforms.Resize((height, width)),
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize(mean, std)
])Compose lets you combine many transforms in order.
Transforms like Resize, ToTensor, and Normalize are common for image data.
Examples
Convert an image to a PyTorch tensor with values between 0 and 1.
PyTorch
transform = torchvision.transforms.ToTensor()
Normalize tensor values to have mean 0 and standard deviation 1 after normalization.
PyTorch
transform = torchvision.transforms.Normalize(mean=[0.5], std=[0.5])
Resize images to 28x28, randomly flip horizontally, then convert to tensor.
PyTorch
transform = torchvision.transforms.Compose([
torchvision.transforms.Resize((28, 28)),
torchvision.transforms.RandomHorizontalFlip(),
torchvision.transforms.ToTensor()
])Sample Model
This code creates a white image, resizes it, converts it to a tensor, and normalizes it. Then it prints the tensor shape and pixel values.
PyTorch
import torch from torchvision import transforms from PIL import Image import numpy as np # Create a dummy image (100x100 pixels, white color) image = Image.fromarray(np.full((100, 100, 3), fill_value=255, dtype=np.uint8)) # Define transforms: resize to 64x64, convert to tensor, normalize transform = transforms.Compose([ transforms.Resize((64, 64)), transforms.ToTensor(), transforms.Normalize(mean=[1.0, 1.0, 1.0], std=[1.0, 1.0, 1.0]) ]) # Apply transform transformed_image = transform(image) # Print shape and some pixel values print(f"Transformed image shape: {transformed_image.shape}") print(f"Pixel value at (0,0): {transformed_image[:,0,0]}")
OutputSuccess
Important Notes
Normalization subtracts the mean and divides by std for each channel, so here mean=1 and std=1 makes pixel values zero after normalization.
Transforms are applied in order inside Compose.
Always convert images to tensors before feeding to PyTorch models.
Summary
Data transforms prepare raw data for machine learning models.
Use Compose to chain multiple transforms like resize, flip, tensor conversion, and normalization.
Transforms improve model training and prediction quality.