Before diving into convolution itself, it's worth being precise about what an image actually is, numerically โ the tensor structure every CNN operation in this category operates on.
An Image as a Tensor
As already established in Tensors, a single image is a rank-3 tensor: channels ร height ร width, written \((C, H, W)\) โ PyTorch's standard convention. A grayscale image has \(C=1\); a standard color (RGB) image has \(C=3\), one channel each for red, green and blue intensity.
Pixel Value Ranges
| Representation | Typical Range | Common Use |
|---|---|---|
| Raw 8-bit integer | 0 to 255 per channel | How images are typically stored on disk (JPEG, PNG) |
| Normalized float | 0.0 to 1.0 | Standard input format for most deep learning models โ simply raw values divided by 255 |
| Standardized float | Roughly -2 to 2 (dataset-dependent) | Normalized further using dataset mean/std, as in Variance & Standard Deviation โ often used for models pretrained on a specific dataset like ImageNet |
Code โ Loading and Inspecting an Image Tensor
import torch
from torchvision import transforms
from PIL import Image
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(), # converts to (C, H, W) float tensor, scaled to [0, 1]
])
# image = Image.open("photo.jpg")
# tensor = transform(image)
# print(tensor.shape) # torch.Size([3, 224, 224])
# print(tensor.min(), tensor.max()) # roughly 0.0 to 1.0
# A batch of such images adds a leading batch dimension: (N, C, H, W)
batch = torch.randn(32, 3, 224, 224)
print(batch.shape)
Why Channel Order Conventions Matter
PyTorch's convention is channel-first: \((C, H, W)\), or \((N, C, H, W)\) for a batch. Some other tools and libraries (including raw image-loading libraries and TensorFlow by default) use channel-last: \((H, W, C)\). Feeding a tensor in the wrong convention into a model expecting the other silently produces garbage results without necessarily raising an error โ always verify which convention a specific model or library expects.
Common Mistakes
- Forgetting to normalize pixel values from the raw 0โ255 range to 0โ1 (or a dataset-specific standardized range) before feeding them into a network โ as established in Variance & Standard Deviation, unnormalized inputs can meaningfully hurt training stability.
- Mixing up channel-first and channel-last conventions when moving data between different libraries or a custom data-loading pipeline and a pretrained model.
Interview Relevance
Q: "What is the shape of a batch of 16 color images, each resized to 128ร128, in PyTorch's convention?" \((16, 3, 128, 128)\) โ batch size, then channels (3 for RGB), then height, then width, following PyTorch's channel-first convention.
Practice Question
An image loaded as a NumPy array has shape \((256, 256, 3)\). Is this channel-first or channel-last, and what shape would it need to be reordered into for a standard PyTorch model?