Preprocessing images properly is a critical step before feeding them to a neural network. The goal of preprocessing is to convert raw image data into a format that makes it easier for the model to learn patterns. Here's a comprehensive checklist and some commonly used methods for image preprocessing for neural network training:
✅ 1. Resize / Rescale
Resize all images to a fixed dimension that matches the input size expected by the neural net.
python
from PIL import Image
img = Image.open("image.jpg")
img = img.resize((224, 224)) # Example for models like ResNet
✅ 2. Normalize / Standardize Pixel Values
Convert pixel values to a consistent range. Neural nets learn better with data in standard ranges such as:
[0, 1]: Divide by 255
[-1, 1]: (pixel / 127.5) - 1
Zero mean / unit variance: Standardization
python
import numpy as np
img = np.array(img) / 255.0
PyTorch example:
python
import torchvision.transforms as transforms
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(), # Converts to [0,1] and channels-first (C, H, W)
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
TensorFlow/Keras example:
python
from tensorflow.keras.applications.resnet50 import preprocess_input
x = preprocess_input(x) # Normalizes according to ResNet expectations
✅ 3. Data Augmentation (During Training)
Helps prevent overfitting by introducing randomness.