The Origins of the YOLO architecture 🔎

YOLO v1 Architecture

What is Object Detection?

Given an input image, the function of a detection model would be to perform two actions, 1️⃣ Draw a bounding box around the detected object, 2️⃣ Return the class of the detected object in terms of a probability.

Consider this to be the input image

Sample input image

This would be the expected output of the object detection model

Sample output of an object detection model

RCNN’s, Faster-RCNN’s and their Downfall

RCNN’s and Faster-RCNN’s AKA Region based Convolution Neural Networks work in a 2-stage process which has 2 neural networks, The first stage draws Bounding Box around a region where an object is present, and the second stage predicts the Class of the object.

RCNN & FRCNN Architecture

As you can see above, the input of the image is preprocessed and sent to two different neural networks which have different purposes. Faster RCNN’s use a region proposal network (RPN) to make the process much more efficient.

“A Region Proposal Network, or RPN, is a fully convolutional network that simultaneously predicts object bounds and object-ness scores at each position. The RPN is trained end-to-end to generate high-quality region proposals. RPN and algorithms like Fast R-CNN can be merged into a single network by sharing their convolutional features - using the recently popular terminology of neural networks with attention mechanisms, the RPN component tells the unified network where to look.

RPNs are designed to efficiently predict region proposals with a wide range of scales and aspect ratios. RPNs use anchor boxes that serve as references at multiple scales and aspect ratios. The scheme can be thought of as a pyramid of regression references, which avoids enumerating images or filters of multiple scales or aspect ratios.” (Link to the paper about faster RCNN’s)

However, even with major developments to this field, RCNNs suffered from a lot of drawbacks, with the main list being Multistage Pipelines (making it take a lot of space and time for a single computation), Each component of the network being trained separately, making the overall setup of the network costly. Both of these attributes made the RCNN architecture pretty complex, ungeneralizable and unreliable for real time application. An RCNN running on a GPU was only giving about 5fps for reference.

The birth of YOLOv1 (You only look once)

The whole purpose of YOLO was to simplify the object detection problem and to run everything (Bounding and Classifying) through a neural network in a single pass as shown in the image below.

To reframe object detection as a single stage regression problem

The steps taken by a YOLOv1 model (what the official research paper says, simplified).

  1. Take Input image

  2. Resize any input image to 448×448 pixels (This varies between models, its 640×640 pixels for a YOLO v8 for reference)

  3. Then we split the image into S-by-S grids, the S in the case of YOLOv1 is 7×7 grids. So, each purple box above, will have 64 pixels inside it.

  4. It’s going to get a little complicated but understand that each cell (purple box) is responsible for predicting one object!

    Example grid cells and centers

  5. In the image above, the blue cell is responsible for predicting the person and the red cell is responsible for predicting the horse, the cells represent the centers of each object. In the case of object detection, it’s the center of the bounding box if you look closely.

  6. Let’s assume that position of the center for the person in this image is (200,311,142,250) which represent the coordinates (x1, y1, x2 or w, y2 or h). It’s pretty noticeable that we have some quite large numbers.

  7. When looking at machine learning, having large numbers makes the problem much more complicated so we can attempt to normalize this position and make it “relative” to the grid that it is inside of.

    Center point

  8. We take the top left point of the “green” box in this case and transform the position of our center using these equations.

    Formulas for center position transform

  9. I understand that it might be getting a little complicated here, so let’s look at an example calculation.

    Example transformation of a center point (green)

  10. After performing that, our center point transforms from (200, 311, 142, 250) [Relative to 448×448 image] → (0.13, 0.87, 0.31, 0.56) [Relative to its grid cell] which makes it much easier for the machine learning model.

  11. In terms of a training perspective, these new values now will be the “targets” for the YOLO model to achieve. This single step is done for all 49 grid cells for any input image. This is also popularly known as label encoding.

    Label encoding example

  12. In this case, we have two centers in two different grid cells, one for the horse and one for the person, these will show up as the values marked in green in the pic above. For grid cells with no center (object) inside then, the output will be 0. This process is done for all 49 cells.

  13. We can understand the first 5 values in the table. Additionally, p1 to p20 represent all the classes present in the pascal dataset preprocessed by using one-hot encoding.
    Example: If my classes were (Elephant, Person, Horse, TV, Computer) then the grid-cell which has the center of the person class would have the vector (0.0, 1.0, 0.0, 0.0, 0.0).

  14. At this point, pat yourself in the back if you’re able to understand all this (took me a while).

  15. After performing all of that computation, we have the transformed an input image into comprehendible training data for the YOLO model.

  16. You might have been wondering what the 5th column in the table represented (I did not clearly mention it anywhere). This represents the confidence of the model at its prediction, ranging from a 0 → 1.

    Let’s understand the output prediction vector so this makes a bit more sense.

    Output prediction vector


    The meaning of a “Prediction Vector” is essentially the output we get out of the model. It is important to understand that one grid cell will output two bounding boxes (importance of this will be explained shortly). Totally, with everything, a single prediction from a model will have 30 parameters, which will look something like this.

    Prediction vector visualized

    First 5 values belong to the first bounding box, second 5 belong to the second bounding box, and the last 20 belong to all the class probabilities.

  17. Now we need to understand how the output is “parsed” to get the final bounding box out of two predicted bounding boxes.

    Parsing the output.

    Each bounding box has its associated confidence (the 5th value). This is multiplied by the maximum value of the p vector to give us the class confidence. Class confidence (C_hat) = confidence * max(p_vector).

    The bounding box with the higher-class confidence is chosen.

  18. Take a breather and understand what we have done until now

    Overview so far

  19. YOLO v1 has a surprisingly simple architecture that was inspired by Google Net.

    Yolo V1 architecture

    Output of a model with all the information

Detailed Walk through of the Training process of the YOLOv1

Training process

The datasets chosen, pretraining and the actual training information is shown above, lets understand how the training actually takes place.

Sample training

An image is randomly selected from the dataset and is passed through the label encoding process (everything we saw at the start of the article). This same image is also passed through the YOLO network, and both of the outputs are taken in for loss computation.

Let’s now understand how the loss is computed for this network

Loss function calculation

The paper makes a pretty good point here in terms of taking the grid cells that actually have the object in them and prioritizing them when calculating the loss, instead of giving every cell equal weightage.

Hence, the formula for loss will be

Loss formula

The lambda no_obj here is the penalization factor for grid cells that have no object

Net loss including class and confidence

The paper has put more focus on the box parameters instead of the confidence and class values because they work with the actual object centers (absolute truth). Giving us this final formula for loss

Final loss formula

For all the math nerdssss, Here is detailed information about how exactly each loss is computed.

  1. BBox Loss

    BBox Loss

  2. Object-ness loss

    Obj Loss

  3. Classification Loss

    Class loss

Email me if you want further clarification on how the math works for this.

After the loss is computed, adjustments are made to the model and backpropagation as shown in this image happens.

Backpropagation

After a certain number of epochs, a very reliable object detection model is trained :D

YOLO Model Metrics

155 and 45 FPS

Ability of YOLO to be used in any domain

Limitations of YOLOv1

Thats all for today folks! If you have gotten this far, please feel free to leave me feedback on what I could have explained better or anything that you are still confused about. For a detailed video explanation, please check out this YouTube video, bro does an extremely good explanation of the same thing 😋

Keep reading