CSC3104 Β· Module 1 of 9

Images as Data

Represent, inspect & transform image inputs

~56 min to study properly

To a computer, an image is not a picture β€” it is a grid of numbers. A colour image is a 3D tensor of shape height Γ— width Γ— channels, and each value is usually an integer from 0 (dark) to 255 (bright).

Checkpoint Β· the examiner asks

Why do we normalise image pixels using the ImageNet mean and standard deviation before feeding them into a pretrained CNN?

In one minute

An image is a tensor of numbers (H Γ— W Γ— C, values 0–255). Before training we scale to 0–1 and normalise per channel with the mean/std the backbone was trained on, so inputs live where the pretrained weights expect them. Kernels like Sobel are tiny weight grids that respond to intensity change, and augmentation creates new views of the same image so the model learns invariances instead of memorising.

Traps examiners look for
  • Normalising with your own dataset's statistics when using a pretrained ImageNet backbone β€” use the backbone's mean/std.
  • Forgetting the ToTensor() step: Normalize expects 0–1 floats, not 0–255 integers.
  • Augmenting the validation/test set β€” augmentation is for training only.
Read

Study notes

01An image is a tensor

To a computer an image is a grid of numbers. A colour image of height H and width W has three channels (red, green, blue), so it's a 3-D array of shape H Γ— W Γ— 3, usually with 8-bit integers from 0 (dark) to 255 (bright). A 224 Γ— 224 RGB image holds 224 Γ— 224 Γ— 3 = 150,528 numbers.

Conventions matter in code:

  • NumPy, OpenCV and PIL use HWC (height, width, channels). PyTorch uses CHW, and a batch is NCHW.
  • OpenCV loads images as BGR, not RGB β€” a classic source of "the model works in training but not on camera images".
  • transforms.ToTensor() converts HWC uint8 [0, 255] into CHW float [0, 1].

02Colour spaces

RGB channels are strongly correlated: a shadow lowers all three together, so "brightness" is smeared across every channel. Other colour spaces separate it out:

  • HSV β€” Hue (which colour), Saturation (how vivid), Value (how bright). Thresholding on hue finds "red objects" regardless of lighting far better than RGB thresholds.
  • Grayscale β€” Y = 0.299R + 0.587G + 0.114B. The weights follow human sensitivity (we're most sensitive to green). One channel instead of three: less compute, but colour information is gone.
  • LAB β€” perceptually uniform; distances roughly match how different colours look.

Deep networks usually take RGB and learn whatever colour features they need, but colour spaces still matter for classical pipelines and augmentation.

03Normalisation

Networks train best when inputs are centred near zero with similar scales. The standard recipe:

  1. Scale to [0, 1]: x = pixel / 255.
  2. Standardise per channel: xΜ‚ = (x βˆ’ ΞΌ_c) / Οƒ_c.

When fine-tuning an ImageNet-pretrained model, use ImageNet's statistics: ΞΌ = (0.485, 0.456, 0.406), Οƒ = (0.229, 0.224, 0.225). The pretrained filters and BatchNorm layers were tuned to inputs distributed that way; feeding differently scaled data shifts every activation and wastes the pretraining.

tf = transforms.Compose([transforms.ToTensor(),
                         transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])

Normalised values are z-scores: a pure white red channel becomes (1 βˆ’ 0.485)/0.229 β‰ˆ 2.25, and black becomes about βˆ’2.12.

04Filters, augmentation and event cameras

A classic filter is a small grid of weights slid over the image (convolution). The Sobel-x kernel [[-1,0,1],[-2,0,2],[-1,0,1]] subtracts left neighbours from right ones, so it responds strongly where intensity changes horizontally β€” vertical edges. A box blur averages; a sharpen kernel boosts the centre against its neighbours. CNNs learn kernels like these.

Data augmentation creates new training views of each image: random crops and flips, small rotations, colour jitter, blur. It acts as a regulariser and teaches invariances β€” a flipped cat is still a cat. Augment only the training set, and only with transforms that keep the label true (flipping a "left turn" road sign changes its meaning!).

Not all vision sensors produce frames: event cameras report per-pixel brightness changes asynchronously with microsecond timing β€” no motion blur and enormous dynamic range, useful for fast robotics.

Watch it done

Worked examples

Worked example 1

Normalise a green pixel

A green-channel value is 200. Compute the value after ToTensor() and ImageNet Normalize (ΞΌ = 0.456, Οƒ = 0.224).

    Worked example 2

    Memory for a batch

    A batch of 32 RGB images at 224 Γ— 224 is stored as float32. How many MB is the input tensor?

      Vocabulary

      Key terms

      Event cameras see in microseconds

      Neuromorphic sensors (e.g. Prophesee, Sony IMX636) have no frame rate. Each pixel independently reports brightness changes with microsecond latency and >120 dB dynamic range β€” useful for drones, robotics and high-speed inspection where normal cameras blur.

      Practise

      Quick check

      0 of 8 correct so far. Wrong answers come with an explanation β€” and the buddy can unpack them.

      Q1A 224Γ—224 RGB image is stored as uint8. How many numbers is that?

      Q2Which augmentation is a BAD idea for digit recognition (MNIST)?

      Q3What shape does a batch of RGB images have in PyTorch?

      Q4OpenCV's cv2.imread returns channels in which order?

      Q5Why convert to HSV for colour thresholding?

      Q6A Sobel-x kernel responds most strongly to…

      Q7Which augmentation could change a label?

      Q8What does ToTensor() do to a uint8 image?

      Exam-style

      Written practice

      Short answer5 marks

      Describe how an RGB image is represented as a tensor and two common format pitfalls.

      Short answer5 marks

      Why do we normalise images with the ImageNet mean and standard deviation when fine-tuning a pretrained model?

      Long answer10 marks

      You are building a classifier for photos of cassava leaves taken on farmers' phones under very different lighting. Design the input pipeline β€” loading, colour handling, resizing, normalisation and augmentation β€” and justify each choice.

      Watch

      Videos

      Fresh picks are chosen by AI every day.
      Hands-on

      Pixels & normalisation

      4 guided tasks: predict, experiment, explain β€” then challenge yourself. Plus 2 coding exercises you run right in the browser.

      Start the lab
      Convolutional Neural Networks