MAI5063 Advances in Deep Learning · Lab 5b · Week 5 companion lab

One look: YOLO grids, anchors and speed

A single-stage detector skips the proposal step and predicts boxes straight from a grid. Build the grid encoding and decoding by hand, match objects to anchor shapes, then run YOLOv8 and an SSD and measure how much faster they are than the Faster R-CNN from Lab 5.

Platform Colab, PyTorch and TensorFlow Runtime GPU recommended Builds on Lab 5

By the end you can

  • Encode a box to its responsible grid cell and decode it back exactly.
  • Explain the output tensor S × S × (5B + C) with a worked example.
  • Match an object to the best anchor by shape IoU.
  • Run YOLOv8 and an SSD and measure frames per second.
  • Say when a single-stage detector is the right choice and when a two-stage one is.

Before the lab

Tasks 2 and 3 · The grid

The cell holding an object's centre is responsible for it

Divide the image into an S × S grid. For a box, find its centre, the cell that contains the centre, and the centre's offset inside that cell. Decoding reverses every step.

j = floor(cx · S) i = floor(cy · S) x_cell = cx · S − j y_cell = cy · S − i
Edit box
Question 3 · Output shape

What does the network output?

Every cell predicts B boxes and C class probabilities. Each box has five numbers: an objectness score c, the offsets x and y, and the width w and height h.

per box = [ c_obj , x , y , w , h ] YOLOv1: shape = S × S × (5B + C) YOLOv3 and later: shape = S × S × B(5 + C)
Class scores
Task 4 · Anchor boxes

Match the object's shape to the closest anchor

Anchors are preset box shapes. Only width and height are compared, as if both boxes shared a centre. This lets one cell specialise: tall objects to one anchor, wide objects to another.

IoU_wh = min(w₁,w₂)·min(h₁,h₂) / (w₁h₁ + w₂h₂ − min(w₁,w₂)·min(h₁,h₂))
Lecture link · The multi-part loss

Three questions, three terms

Training adds up errors on where the box is, whether anything is there and what it is. Choose a term to see what it penalises.

Question 6 · Multi-scale prediction

Small objects need a fine grid

A coarse grid cannot see an object smaller than a cell. YOLOv3, SSD and later models predict from several feature maps with different strides, so each object size has a suitable grid.

Input size
Task 7 · Accuracy against speed

Why single-stage models are faster

Compute per image is a guide to speed. Published figures for the detectors you run in this lab are below. Compare accuracy (COCO box AP) with compute (GFLOPs).

The time column is only compute divided by a sustained TFLOPS figure you choose. It ignores memory traffic, pre- and post-processing and NMS, and real hardware rarely sustains its peak. Measure your own FPS in Task 7. AP and GFLOPs are taken from the model documentation of each library and may differ between versions and input sizes.

Hands-on · Google Colab

The Colab tasks

Copy each cell into Colab and run them in order. Tasks 1 to 4 use NumPy only. Task 5 uses PyTorch (Ultralytics) and Task 6 uses TensorFlow, so you can do either in one runtime. Task 7 times everything on the same machine. Comments in the code explain each step.

Reflection

Write it in your own words

Answers are saved in this browser only. Open a hint after you have tried.