Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to Deep Learning Notes
Topic #351

Image Representation

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

RepresentationTypical RangeCommon Use
Raw 8-bit integer0 to 255 per channelHow images are typically stored on disk (JPEG, PNG)
Normalized float0.0 to 1.0Standard input format for most deep learning models — simply raw values divided by 255
Standardized floatRoughly -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?

Related DL Notes

Want to go beyond the notes?

Join Coding Hubs School of AI's Deep Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →