๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #162

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?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Image Representation โ€“ FAQs

Quick answers about learning Image Representation in Deep Learning.

This free note from CodingNow 2.0 explains Image Representation in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Image Representation, is 100% free with no signup required.
With focused practice, most students grasp Image Representation in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now