YOLO algorithm
computer vision
bounding boxes
object detection
machine learning training

Yolo v1 bounding boxes during training step

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

In exploring the foundations of real-time object detection, the YOLO (You Only Look Once) algorithm stands as a pivotal advancement. YOLO v1, introduced by Joseph Redmon et al., is renowned for its efficiency and speed, particularly due to its unique method of handling bounding boxes during the training step. Here, we break down the technicalities involved with YOLO v1 bounding boxes.

YOLO v1 Bounding Boxes

YOLO v1 treats object detection as a single regression problem, directly predicting bounding boxes and class probabilities from full images in one evaluation. Unlike previous designs that repurpose classifiers or localizers to perform detection, YOLO v1 maintains a different approach by harnessing a single convolutional neural network (CNN) that simultaneously predicts multiple bounding boxes and their likelihoods.

Grid Division

The YOLO algorithm divides the input image into an S×SS \times S grid of cells. Each grid cell is responsible for predicting bounding boxes if the center of a ground-truth object lies within it. Typical grid sizes are 7×77 \times 7, where each cell predicts a fixed number of boxes.

Bounding Box Prediction

For each grid cell in the YOLO v1 architecture:

Bounding Boxes: Each cell predicts BB bounding boxes, with each box defined by five elements: (x,y,w,h,c)(x, y, w, h, c). • (x,y)(x, y): These are the coordinates corresponding to the center of the bounding box within the grid cell. The values are normalized between 0 and 1 relative to the grid cell. • (w,h)(w, h): Width and height are also predictions of the bounding box, normalized concerning the image dimensions. • cc: This stands for the confidence score, which represents the predicted confidence that the box contains an object and the accuracy of the bounding box itself. This is calculated as:

c=Pr(Object)×IOU(pred,truth)c = \text{Pr}(Object) \times \text{IOU}(pred, truth)

Class Predictions: Each grid cell has a set of class probabilities conditioned on the cell containing an object.

`Loss` Function

YOLO v1 uses a custom loss function that combines aspects of both localization and classification, focused on:

Localization `Loss` (bbox coordinates): Measures errors in bounding box predictions using sum-squared error, typically giving higher weight to coordinate accuracy. • Confidence Loss: Two parts evaluate this: • One part evaluates the squared error of the confidence score for boxes that contain objects. • The other evaluates confidence for boxes without objects, promoting low confidence predictions where no object exists. • Classification Loss: Evaluated only for grid cells containing an object.

The overall loss L\mathcal{L} can be expressed as:

L=Lcoord+Lsize+Lobj+Lnoobj+Lclass\mathcal{L} = L_{coord} + L_{size} + L_{obj} + L_{noobj} + L_{class}

Lcoord=λcoordi=0S2j=0B1ijobj[(xix^i)2+(yiy^i)2]L_{coord} = \lambda_{coord} \sum_{i=0}^{S^2} \sum_{j=0}^{B} \mathbf{1}_{ij}^{obj} \left[ (x_i - \hat{x}_i)^2 + (y_i - \hat{y}_i)^2 \right]

Lsize=λcoordi=0S2j=0B1ijobj[(wiw^i)2+(hih^i)2]L_{size} = \lambda_{coord} \sum_{i=0}^{S^2} \sum_{j=0}^{B} \mathbf{1}_{ij}^{obj} \left[ (\sqrt{w_i} - \sqrt{\hat{w}_i})^2 + (\sqrt{h_i} - \sqrt{\hat{h}_i})^2 \right]

Lobj=i=0S2j=0B1ijobj(CiC^i)2L_{obj} = \sum_{i=0}^{S^2} \sum_{j=0}^{B} \mathbf{1}_{ij}^{obj} (C_i - \hat{C}_i)^2

Lnoobj=λnoobji=0S2j=0B1ijnoobj(CiC^i)2L_{noobj} = \lambda_{noobj} \sum_{i=0}^{S^2} \sum_{j=0}^{B} \mathbf{1}_{ij}^{noobj} (C_i - \hat{C}_i)^2

Lclass=i=0S21iobjcclasses(pi(c)p^i(c))2L_{class} = \sum_{i=0}^{S^2} \mathbf{1}_{i}^{obj} \sum_{c \in classes} (p_i(c) - \hat{p}_i(c))^2

where 1ijobj\mathbf{1}_{ij}^{obj} is 1 if an object appears in cell ii and bounding box jj is responsible for the prediction, while 1ijnoobj\mathbf{1}_{ij}^{noobj} is 1 when that box is not responsible for an object.

Technical Summary

FeatureDescription
Grid SizeTypically 7×77 \times 7
Bounding Box Prediction(x,y,w,h,c)(x, y, w, h, c) for each of BB boxes per grid cell
Loss Function ComponentsLocalization, confidence, classification
NormalizationCoordinates and dimensions are relative to grid cell/image dimensions
Confidence ScoreCombines probability of object presence and IOU with ground truth
Class PredictionConditioned on object having presence in cell

Model Training and Optimization

Overlapping Detections

YOLO v1 does not focus on refining bounding boxes post-prediction. Instead, it trains an end-to-end model to predict bounding boxes directly. Non-Maximum Suppression (NMS) is typically used during post-processing to handle overlapping detections.

Limitations

YOLO v1 assumes only one object is present per grid cell, which can lead to performance bottlenecks with small objects. Bounding box aspect ratios are also restricted, as only a fixed number per grid cell are predicted.

Conclusion

YOLO v1 represents a significant step in object detection by integrating detection into a single neural network function. Its unique handling of bounding boxes during training and application of a multi-component loss function ensured efficient, real-time predictions that laid the groundwork for future YOLO iterations.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.