Computer Vision Pipelines

Follow an image all the way from raw photo to live prediction — collecting and labelling data, augmenting and preprocessing it, transfer-learning a pretrained backbone, running the train/validate loop, evaluating honestly, and deploying for inference.

Learn Computer Vision Pipelines in our free AI & Machine Learning course — a beginner-friendly interactive lesson with worked examples, a practice exercise…

Part of the free AI & Machine Learning course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

A CV pipeline is an assembly line that turns raw photos into predictions. Picture a factory floor with stations in a row:

If one station is mis-calibrated — say standardisation differs between the factory and the field — every later station produces junk. The whole point of a pipeline is that each stage is reliable and consistent from training through to deployment.

Everything starts with labelled data — images paired with the correct answer (the label ). Before any training, you split that data into a train set the model learns from and a validation set you hold back to check progress honestly. A common split is 80/20.

The split below is the simplest possible version — slice a list. Run it and watch which samples land where.

Augmentation creates safe, label-preserving copies of training images so the model sees more variety: horizontal flips , random crops , and colour jitter (brightness/contrast). A flipped cat is still a cat, so the label stays the same while the pixels change.

Here's the simplest augmentation — a horizontal flip — written in plain Python so you can see exactly what changes.

Preprocessing makes every image look the same to the model. Two steps dominate: resize (so all images share one width and height the model expects) and normalize (rescale pixels from 0..255 down to a small range like 0..1). Normalizing keeps training stable.

Run the example below to normalize a tiny "image" by hand — every pixel divided by 255.0.

In real projects you don't do this by hand — torchvision.transforms composes resize, crop, tensor-conversion and normalization into one reusable pipeline. The same recipe must run at training, validation, and inference time.

Training a vision model from scratch needs millions of images. Transfer learning avoids that: you take a backbone (like ResNet-50) that already learned generic features — edges, textures, shapes — from ImageNet, and you only replace its final layer (the head ) so it outputs your classes.

Below, Albumentations builds the augmentation pipelines (note: train augments, val does not) and a pretrained ResNet-50 has its head swapped for 5 classes.

Training runs in epochs — one full pass over the training data. Each epoch you let the model learn from the train set (compute loss, back-propagate, update weights), then validate on the held-back set without learning from it. Watching train loss fall while val accuracy rises tells you it's working; if val accuracy stalls or drops while train keeps improving, the model is overfitting .

Accuracy alone lies on imbalanced data. If 95% of images are "not cancer", a model that always says "not cancer" scores 95% yet catches nothing. So you also look at precision (of the things I flagged, how many were right?), recall (of the things I should have caught, how many did I?), and F1 (their balance). A confusion matrix shows exactly which classes get mixed up.

Deployment means running the trained model on one new image at a time. The golden rule: apply the exact same preprocessing you used for validation — never the training augmentation — switch to model.eval() , and run a single forward pass. A softmax turns the raw scores into probabilities so you can report a confidence.

Fill in the blank so every pixel is scaled to the 0..1 range. Use the expected output to check yourself.

Fill in the two slice indices so the first 25% becomes validation and the rest becomes training.

You normalize with one mean/std (or resize differently) at training but another at inference. The model sees inputs it was never trained on, so accuracy quietly collapses in production.

✅ Fix: define preprocessing once and reuse the identical transform everywhere — train, validate, and deploy.

Flips and crops on your val set make every run report a different, unrealistic score. You can no longer trust the number.

✅ Fix: keep a separate val transform with only Resize + Normalize — no random ops.

Near-duplicate frames of the same scene land in both train and val, or you compute normalization statistics over the whole dataset before splitting. Offline scores look amazing; real-world performance is poor.

✅ Fix: split first, then compute stats only on the train set; group related images so they never straddle the split.

Feeding raw 0..255 pixels makes loss spike to NaN or stall, because the gradients blow up.

✅ Fix: always scale pixels to a small range (0..1, then ImageNet mean/std) before the model.

Now with the support faded — only a comment outline is given. Write the augmentation yourself: mirror each row of a nested-list image left to right.

You can now walk an image down the whole assembly line: collect and split data, augment the training set, preprocess by resizing and normalizing, transfer-learn a pretrained backbone, run the train/validate loop, evaluate beyond plain accuracy, and deploy for inference — all while keeping preprocessing consistent and avoiding leakage.

🚀 Up next: Object Detection — go from "what is in this image?" to "what is where?", drawing labelled boxes around every object.

Practice quiz

To normalise an 8-bit pixel to the 0..1 range, you:

  • Multiply by 255
  • Subtract 128
  • Divide by 255.0
  • Take the square root

Answer: Divide by 255.0. Dividing each pixel by 255.0 maps 0 to 0.0 and 255 to 1.0, the small fixed range models train best on.

Which set should data augmentation (flips, crops, colour jitter) be applied to?

  • Training set only
  • Validation set only
  • Test set only
  • All sets equally

Answer: Training set only. Augmentation adds variety for training only; the validation and test sets must stay fixed for an honest score.

Why must validation and test sets NOT be augmented?

  • It is too slow
  • Augmentation deletes images
  • The model can't read flipped images
  • So the score you read reflects real-world performance honestly

Answer: So the score you read reflects real-world performance honestly. Augmenting eval sets gives a different, unrealistic number each run, so it no longer reflects real performance.

What does transfer learning with a pretrained backbone like ResNet-50 let you do?

  • Train from scratch with no data
  • Reuse learned generic features and retrain only a small head for your classes
  • Skip preprocessing entirely
  • Avoid the train/validate loop

Answer: Reuse learned generic features and retrain only a small head for your classes. The backbone already learned edges/textures from ImageNet; you swap its head and retrain on far less data.

A common train/validation split ratio is:

  • 80/20
  • 50/50
  • 99/1
  • 10/90

Answer: 80/20. An 80% train / 20% validation split is a standard default for checking progress on held-back data.

Before validating or deploying a PyTorch model you should call:

  • model.train()
  • model.reset()
  • model.eval()
  • model.augment()

Answer: model.eval(). model.eval() freezes dropout and batch-norm behaviour so validation and inference scores are consistent.

What is the golden rule for preprocessing at inference time?

  • Use the training augmentation
  • Apply the exact same preprocessing used for validation
  • Skip normalisation to go faster
  • Resize to a random size each call

Answer: Apply the exact same preprocessing used for validation. Inference must use the same resize+normalize as validation — never training augmentation — to avoid skew.

What is data leakage in a CV pipeline?

  • Running out of disk space
  • Using a GPU
  • Augmenting the training set
  • Information from val/test sneaking into training (e.g. computing stats over all data before splitting)

Answer: Information from val/test sneaking into training (e.g. computing stats over all data before splitting). Leakage is when val/test info influences training — e.g. near-duplicate frames in both splits, inflating offline scores.

Why is accuracy alone misleading on imbalanced data?

  • It is slow to compute
  • A model that always predicts the majority class scores high yet catches nothing of the rare class
  • It needs a GPU
  • It only works for detection

Answer: A model that always predicts the majority class scores high yet catches nothing of the rare class. If 95% of images are 'not cancer', always saying 'not cancer' scores 95% but catches no real cases — use precision/recall/F1.

Inside the training loop, what does a softmax over the final logits produce?

  • The raw weights
  • The loss value
  • Class probabilities that sum to 1
  • The learning rate

Answer: Class probabilities that sum to 1. Softmax turns the raw output scores into probabilities (each 0..1, summing to 1) so you can report a confidence.