Object Detection: YOLO

YOLO (You Only Look Once) is a family of real-time object detection models that reframe detection as a single regression problem — predicting bounding boxes and class probabilities directly from full images in one forward pass. It matters because it broke the speed-accuracy tradeoff that plagued two-stage detectors like R-CNN, enabling detection at 45–155 FPS on standard hardware. The counterintuitive fact: YOLO is often more accurate on small-batch real-time inference than slower two-stage methods, because its global image context prevents the false positives on backgrounds that plague region-proposal pipelines.

The core insight

Instead of asking "where might objects be?" (region proposals) and then classifying each candidate, YOLO asks a single question of the entire image at once: "For every cell in this grid, what objects are centered here, how big are they, and what class are they?" The network divides the image into an S×S grid, and each cell simultaneously predicts B bounding boxes with confidence scores and C class probabilities — all in a single forward pass. This unified architecture means gradients flow through the entire detection pipeline end-to-end, and the model learns a richer global representation. The "aha moment" is realizing that object detection is just a structured regression problem dressed up in grid clothing.

How It Works

Grid Division and Prediction Tensor

Given an input image of size W×HW \times H, YOLO divides it into an S×SS \times S grid. Each grid cell predicts BB bounding boxes and CC conditional class probabilities. The full output tensor has shape:

1
Output∈RS×S×(B⋅5+C)\quad \text{Output} \in \mathbb{R}^{S \times S \times (B \cdot 5 + C)}

Explanation: Each of the S² grid cells independently emits B boxes (each parameterized by 5 values: x, y, w, h, confidence) plus C class scores, so the entire detection problem collapses to predicting this single dense tensor. Where:

  • SS — grid size (e.g., 7 in YOLOv1, 13/26/52 in YOLOv3)
  • BB — number of anchor boxes per cell (e.g., 2 in YOLOv1, 3 in YOLOv3)
  • CC — number of object classes
  • The 5 values per box: (tx,ty,tw,th,conf)(t_x, t_y, t_w, t_h, \text{conf})

Bounding Box Encoding (YOLOv2+)

Rather than predicting absolute coordinates, YOLO predicts offsets relative to anchor priors. Given an anchor of width pwp_w and height php_h, and the cell's top-left corner at (cx,cy)(c_x, c_y):

2
bx=σ(tx)+cx,by=σ(ty)+cy,bw=pwetw,bh=pheth\quad b_x = \sigma(t_x) + c_x, \quad b_y = \sigma(t_y) + c_y, \quad b_w = p_w e^{t_w}, \quad b_h = p_h e^{t_h}

Explanation: The sigmoid constrains the predicted center offset to stay within the responsible grid cell (preventing training instability), while the exponential maps unbounded network outputs to positive box dimensions scaled by the anchor prior. Where:

  • σ\sigma — sigmoid function, squashes tx,ty∈(−∞,∞)t_x, t_y \in (-\infty, \infty) to (0,1)(0, 1)
  • tx,tyt_x, t_y — raw network outputs for center offset
  • tw,tht_w, t_h — raw network outputs for width/height log-scale adjustment
  • cx,cyc_x, c_y — integer grid cell offsets (column, row)
  • pw,php_w, p_h — anchor prior dimensions (from k-means clustering on training boxes)

Objectness Confidence

Each box prediction includes an objectness score — the probability that an object exists there, weighted by how well the box fits:

3
Conf=Pr⁡(Object)×IoU(pred,gt)\quad \text{Conf} = \Pr(\text{Object}) \times \text{IoU}(\text{pred}, \text{gt})

Explanation: During training the confidence target is set to the IoU between the predicted box and the ground-truth box, so the network learns to predict not just "is something here" but also "how well am I boxing it." Where:

  • Pr⁡(Object)\Pr(\text{Object}) — 1 if any ground-truth box's center falls in this cell, 0 otherwise
  • IoU(pred,gt)\text{IoU}(\text{pred}, \text{gt}) — Intersection over Union between predicted and ground-truth boxes

Multi-Part Loss Function (YOLOv1)

The full YOLO training loss is a weighted sum over localization, confidence, and classification terms:

4
L=λcoord∑i,j1ijobj ⁣[(xi−x^i)2+(yi−y^i)2+(wi−w^i)2+(hi−h^i)2]+∑i,j1ijobj(Ci−C^i)2+λnoobj∑i,j1ijnoobj(Ci−C^i)2+∑i1iobj∑c∈classes(pi(c)−p^i(c))2\quad \mathcal{L} = \lambda_\text{coord} \sum_{i,j} \mathbf{1}_{ij}^\text{obj}\!\left[(x_i - \hat{x}_i)^2 + (y_i - \hat{y}_i)^2 + (\sqrt{w_i} - \sqrt{\hat{w}_i})^2 + (\sqrt{h_i} - \sqrt{\hat{h}_i})^2\right] + \sum_{i,j} \mathbf{1}_{ij}^\text{obj}(C_i - \hat{C}_i)^2 + \lambda_\text{noobj}\sum_{i,j}\mathbf{1}_{ij}^\text{noobj}(C_i - \hat{C}_i)^2 + \sum_{i} \mathbf{1}_i^\text{obj}\sum_{c \in \text{classes}}(p_i(c) - \hat{p}_i(c))^2

Explanation: The square root on width and height deemphasizes large errors in big boxes (a 10px error on a 200px box matters less than on a 20px box), while λ_noobj downweights the overwhelming majority of empty cells so they don't swamp the gradient signal from real detections. Where:

  • λcoord=5\lambda_\text{coord} = 5 — upweights localization loss to prioritize box accuracy
  • λnoobj=0.5\lambda_\text{noobj} = 0.5 — downweights confidence loss for empty cells
  • 1ijobj\mathbf{1}_{ij}^\text{obj} — indicator: 1 if object jj in cell ii is the "responsible" predictor
  • x^,y^,w^,h^\hat{x}, \hat{y}, \hat{w}, \hat{h} — ground-truth box coordinates
  • Ci,C^iC_i, \hat{C}_i — predicted and target confidence scores
  • pi(c),p^i(c)p_i(c), \hat{p}_i(c) — predicted and target class probabilities

IoU (Intersection over Union)

5
IoU(A,B)=∣A∩B∣∣A∪B∣=∣A∩B∣∣A∣+∣B∣−∣A∩B∣\quad \text{IoU}(A, B) = \frac{|A \cap B|}{|A \cup B|} = \frac{|A \cap B|}{|A| + |B| - |A \cap B|}

Explanation: IoU is scale-invariant — a 0.5 IoU means the overlap is half the combined area regardless of whether the boxes are 10px or 1000px, making it a consistent measure of localization quality across object sizes. Where:

  • AA — predicted bounding box region
  • BB — ground-truth bounding box region
  • ∣A∩B∣|A \cap B| — pixel area of overlap
  • ∣A∪B∣|A \cup B| — pixel area of union

PyTorch: YOLO Head Forward Pass

```python import torch import torch.nn as nn class YOLOHead(nn.Module): """Single YOLO detection head for one scale.""" def __init__(self, in_channels: int, num_anchors: int, num_classes: int): super().__init__() self.num_anchors = num_anchors self.num_classes = num_classes out_channels = num_anchors (5 + num_classes) self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=1) def forward(self, x: torch.Tensor, anchors: torch.Tensor): """ x: (B, C, S, S) — feature map from backbone anchors: (num_anchors, 2) — prior (pw, ph) for this scale returns: (B, S, S, num_anchors, 5 + num_classes) decoded predictions """ B, _, S, _ = x.shape raw = self.conv(x) # (B, num_anchors(5+C), S, S)

# Reshape to (B, S, S, num_anchors, 5+C) raw = raw.permute(0, 2, 3, 1).contiguous() raw = raw.view(B, S, S, self.num_anchors, 5 + self.num_classes)

# Build grid offsets cx, cy shape: (1, S, S, 1) grid_y, grid_x = torch.meshgrid( torch.arange(S, dtype=torch.float32), torch.arange(S, dtype=torch.float32), indexing='ij' ) cx = grid_x.unsqueeze(0).unsqueeze(-1) # (1, S, S, 1) cy = grid_y.unsqueeze(0).unsqueeze(-1)

# Decode tx,ty → bx,by via sigmoid + offset (Eq. [2]) bx = (torch.sigmoid(raw[..., 0]) + cx) / S by = (torch.sigmoid(raw[..., 1]) + cy) / S

# Decode tw,th → bw,bh via exp anchor prior pw = anchors[:, 0].view(1, 1, 1, -1) ph = anchors[:, 1].view(1, 1, 1, -1) bw = pw torch.exp(raw[..., 2]) bh = ph * torch.exp(raw[..., 3]) conf = torch.sigmoid(raw[..., 4]) # objectness cls = torch.sigmoid(raw[..., 5:]) # class probs (BCE targets) return torch.stack([bx, by, bw, bh], dim=-1), conf, cls ```

researcher · Joseph Redmon | b. 1987 | Inventor of YOLO

Joseph Redmon, a computer science PhD graduate from the University of Washington, introduced YOLO in 2015 with his advisor Ali Farhadi, framing object detection as a single regression problem for the first time. His contributions spanned YOLOv1 through YOLOv3, each dramatically improving speed and accuracy over prior two-stage methods like Faster R-CNN. In a surprising turn, Redmon publicly quit computer vision research in 2020 citing concerns about military applications and surveillance uses of his work — one of the few prominent researchers to step away from a field he largely defined.

researcher · Ali Farhadi | b. 1978 | Co-inventor of YOLO and Darknet

Ali Farhadi is a professor at the University of Washington and co-creator of YOLO and the Darknet framework, which provided the lightweight C/CUDA backbone that made real-time detection feasible on consumer GPUs. He led the team that demonstrated YOLO could process video at 155 FPS with the Fast YOLO variant — an order of magnitude faster than contemporaneous detectors. Farhadi later co-founded the Allen Institute for AI's vision group and Apple's AI research arm hired him, illustrating how foundational the YOLO work became to industrial computer vision.

Figure
YOLOv3 Multi-Scale Detection Architecture
YOLOv3 Multi-Scale Detection Pipeline Input 416×416 ×3 Darknet-53 Backbone Scale 1 52×52×256 Scale 2 26×26×512 Scale 3 13×13×1024 Detection Head 13×13 — Large Obj Detection Head 26×26 — Med Obj Detection Head 52×52 — Small Obj 13×13×3×(5+C) 3 large anchors 26×26×3×(5+C) 3 medium anchors 52×52×3×(5+C) 3 small anchors NMS IoU threshold conf filter FPN skip connection main data flow
Figure
YOLO Grid Cell Prediction Detail

YOLO Grid Cell: Bounding Box Encoding cell σ(tx) σ(ty) cx,cy GT box 5×5 grid (S=5) Cell output vector (per anchor) tx, ty center offset tw, th size log-scale conf P×IoU p(c₁)…p(cC) class probs