Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 62

YOLO Architecture: Grid Cells, Bounding Boxes, and NMS

Recommended resources

Hello! Welcome to the next lesson in our Computer Vision module.

In our last session, you learned how to fine-tune pre-trained models for image classification. You took a model like ResNet, which was great at classifying thousands of different objects, and adapted it to become an expert on a specific, custom task. This involved telling the model, "Here is an image, tell me if it contains an ant or a bee."

Today, we move from classification to a more complex and powerful task: object detection. We're no longer just asking "what" is in the image, but also "where" it is. Our goal is to answer questions like, "This image contains a dog at these coordinates and a bicycle at these other coordinates."

To do this, we'll explore the core concepts of one of the most famous object detection architectures: YOLO (You Only Look Once). Your learning outcome for this lesson is to explain YOLO architecture concepts: grid cells, bounding boxes, and Non-Maximum Suppression (NMS). By the end, you'll understand the clever ideas that allow YOLO to perform object detection so quickly and effectively.

Let's start with a high-level look at the process.

YOLO Object Detection Architecture Overview
This diagram illustrates the YOLO object detection pipeline. An input image is divided into a grid. For each grid cell, the model predicts bounding boxes, confidence scores, and class probabilities. These raw predictions are then filtered and refined to produce the final, clean detections.

This image summarizes the entire workflow we're about to dissect. Let's break it down, step by step.

1. The Core Idea: One Look Through a Grid

The name "You Only Look Once" highlights YOLO's main advantage: efficiency. Unlike older methods that would scan an image with a sliding window, YOLO processes the entire image in a single pass through a neural network.

The key innovation is to reframe object detection as a single regression problem. This is achieved by dividing the input image into an S x S grid of cells. Each cell in this grid is then responsible for detecting objects whose centers fall within it.

To see how this works, let's watch a video that explains this foundational concept.

What is YOLO algorithm? | Deep Learning Tutorial 31 (Tensorflow, Keras & Python)

This video from codebasics provides an excellent, intuitive explanation of how YOLO works. We'll start by focusing on how it uses a grid system to handle multiple objects.

Please watch from the beginning until 10:02. The video will cover: Object Localization (0:00 - 3:51): How a neural network can output not just a class label, but also a bounding box for a single object. Grid Cells for Multiple Objects (3:51 - 10:02): The core YOLO concept of dividing an image into a grid and assigning object detection responsibility to each cell.

As the video explained, each grid cell that contains the center of an object is tasked with predicting a vector of numbers. This vector contains all the information needed to describe the object:

  • Bounding Box (bx, by, bh, bw): Four values that define the rectangle around the object.
    • bx, by: The x and y coordinates of the bounding box's center, relative to the grid cell.
    • bh, bw: The height and width of the box, relative to the entire image.
  • Objectness Score (pc): A confidence score that represents the probability that there is any object in the bounding box. This is also sometimes called the confidence score.
  • Class Probabilities (c1, c2, ...): A set of probabilities, one for each class (e.g., dog, person, car). This tells us the probability that the detected object belongs to a specific class.

So, for an image divided into a 4x4 grid with 2 possible classes (like "player" and "ball"), the network outputs a 4 x 4 x (5 + 2) tensor. That's 4x4 cells, and for each cell, a vector of 7 numbers (bx, by, bh, bw, pc + c1, c2). This is how YOLO gets all its predictions in one forward pass.


2. The Cleanup: Intersection over Union (IoU) and Non-Maximum Suppression (NMS)

The grid-based approach is powerful, but it often produces messy results. An object, especially a large one, might trigger detections in several adjacent grid cells. This results in multiple, overlapping bounding boxes for the same object. We need a way to clean this up and keep only the best prediction.

This is where Non-Maximum Suppression (NMS) comes in. But before we can perform NMS, we first need a way to quantify how much two bounding boxes overlap. This metric is called Intersection over Union (IoU).

Intersection over Union (IoU)
IoU calculates the ratio of the overlapping area to the combined area of two bounding boxes.

The value ranges from 0 (no overlap) to 1 (perfect overlap).

With IoU as our tool, we can now implement NMS. The algorithm is as follows:

  1. Discard all predicted boxes with a low objectness score (pc).
  2. For each class, take the remaining box with the highest score and select it as a final prediction.
  3. Calculate the IoU of this selected box with all other boxes for that same class.
  4. Discard any box that has an IoU above a certain threshold (e.g., 0.5) with the selected box, as it is likely a duplicate detection.
  5. Repeat this process until no boxes are left.

Let's return to the codebasics video to see a visual explanation of this cleanup process.

What is YOLO algorithm? | Deep Learning Tutorial 31 (Tensorflow, Keras & Python)

This section of the video explains how IoU and NMS work together to refine YOLO's raw output.

Please watch from 10:02 to 13:33. Focus on how the algorithm uses IoU to identify redundant boxes and how NMS systematically removes them, leaving only the most confident, non-overlapping predictions.

To solidify your understanding, let's review a more formal, step-by-step description of the NMS algorithm.

Non-Maximum Suppression (NMS) Explained

The article 'Non-Maximum Suppression (NMS) Explained' from Ultralytics provides a very clear and concise summary of the NMS algorithm, along with a practical code example.

Please read the sections 'How Non-Maximum Suppression Works' and 'NMS Implementation with PyTorch'. The first section gives you a clear, 6-step algorithmic process. The second shows you how this is implemented in just one line using PyTorch's torchvision.ops.nms function, which is useful to see from a practical coding perspective.

By combining the grid predictions with the NMS post-processing step, YOLO can produce clean and accurate object detections.


3. An Edge Case: Multiple Objects in One Cell

There's one final problem to consider. What happens if the centers of two or more objects fall into the exact same grid cell? With the structure we've discussed so far, each cell can only predict one object.

To solve this, YOLO introduced the concept of Anchor Boxes. Instead of predicting just one bounding box, each grid cell is allowed to predict multiple boxes (e.g., B=2 or B=5). Each of these predictions is associated with a pre-defined "anchor box" that has a specific shape and aspect ratio (e.g., a tall, thin box and a short, wide box).

During training, the model learns to associate an object with the anchor box shape that best fits it. This allows a single grid cell to detect multiple objects, as long as they have different shapes.

The final part of the codebasics video provides a quick overview of this idea.

What is YOLO algorithm? | Deep Learning Tutorial 31 (Tensorflow, Keras & Python)

Let's see how anchor boxes solve the problem of multiple objects sharing a grid cell.

Watch from 13:33 to 15:21. This short segment explains how the output vector is expanded to accommodate predictions for multiple anchor boxes per grid cell.

With anchor boxes, the output tensor's shape becomes S x S x (B * (5 + C)), where B is the number of anchor boxes, and C is the number of classes. This makes the YOLO architecture even more robust.


Conclusion

Great job! You have now unpacked the fundamental concepts behind the YOLO object detection architecture. This is a significant step up from simple image classification.

Key Takeaways:

  • YOLO treats object detection as a regression problem by dividing an image into an S x S grid.
  • Each grid cell is responsible for predicting bounding boxes (center coordinates, width, height), an objectness score, and class probabilities for objects centered within it.
  • Intersection over Union (IoU) is the standard metric for measuring the overlap between two bounding boxes.
  • Non-Maximum Suppression (NMS) is a critical post-processing step that uses IoU to filter out redundant, overlapping detections, keeping only the best one for each object.
  • Anchor Boxes allow a single grid cell to detect multiple objects by predicting several boxes with pre-defined shapes.

Preview of the Next Lesson:
Theory is essential, but seeing it in action is where the learning clicks. In our next lesson, you will apply a pre-trained YOLO model for object detection. You'll take the concepts you've learned today and use them to find and identify objects in real images, bringing the theory to life with practical code.