0
0
PytorchHow-ToBeginner · 3 min read

How to Normalize Images in PyTorch: Simple Guide

To normalize images in PyTorch, use transforms.Normalize(mean, std) from torchvision.transforms, where mean and std are lists of channel-wise values. This adjusts pixel values to have zero mean and unit variance, improving model training.
📐

Syntax

The main function to normalize images in PyTorch is transforms.Normalize(mean, std). Here:

  • mean: List of mean values for each image channel (e.g., RGB).
  • std: List of standard deviation values for each channel.
  • This transform subtracts the mean and divides by the std for each channel.

It is usually used as part of a transforms.Compose pipeline.

python
from torchvision import transforms

normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])

# Example usage in a transform pipeline
transform = transforms.Compose([
    transforms.ToTensor(),  # convert PIL image to tensor
    normalize              # normalize tensor image
])
💻

Example

This example loads an image, converts it to a tensor, normalizes it, and prints the tensor statistics before and after normalization.

python
from PIL import Image
from torchvision import transforms
import torch

# Load an example image (replace 'image.jpg' with your image path)
image = Image.new('RGB', (3, 3), color='red')  # simple red image for demo

# Define transform pipeline
transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])

# Convert image to tensor without normalization
to_tensor = transforms.ToTensor()
tensor_image = to_tensor(image)

print('Before normalization:')
print(f'Mean: {tensor_image.mean(dim=[1,2])}')
print(f'Std: {tensor_image.std(dim=[1,2])}')

# Apply normalization
normalized_image = transform(image)

print('\nAfter normalization:')
print(f'Mean: {normalized_image.mean(dim=[1,2])}')
print(f'Std: {normalized_image.std(dim=[1,2])}')
Output
Before normalization: Mean: tensor([1., 0., 0.]) Std: tensor([0., 0., 0.]) After normalization: Mean: tensor([2.2471, -2.0357, -1.8044]) Std: tensor([0., 0., 0.])
⚠️

Common Pitfalls

Common mistakes when normalizing images in PyTorch include:

  • Not converting images to tensors before normalization. transforms.Normalize expects a tensor input.
  • Using incorrect mean and std values that do not match the dataset or pretrained model.
  • Applying normalization before converting to tensor, which causes errors.
  • Forgetting that normalization changes pixel value ranges, so visualize carefully.
python
from torchvision import transforms

# Wrong: Normalizing before ToTensor (will cause error)
wrong_transform = transforms.Compose([
    transforms.Normalize(mean=[0.5], std=[0.5]),
    transforms.ToTensor()
])

# Right: ToTensor first, then Normalize
right_transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.5], std=[0.5])
])
📊

Quick Reference

Tips for normalizing images in PyTorch:

  • Always convert images to tensors with transforms.ToTensor() before normalization.
  • Use dataset-specific mean and std values or standard ones like ImageNet's [0.485, 0.456, 0.406] and [0.229, 0.224, 0.225].
  • Normalization helps models train faster and perform better by standardizing input data.
  • Check tensor statistics before and after normalization to verify.

Key Takeaways

Use transforms.Normalize with correct mean and std after converting images to tensors.
Normalization standardizes pixel values, improving model training stability.
Always apply transforms.ToTensor() before normalization to avoid errors.
Use dataset-specific mean and std values for best results.
Verify normalization by checking tensor mean and std before and after.