Non-Max Suppression In Object Detection

Non-Max Suppression In Object Detection

Non-Max Suppression (NMS) is a crucial post-processing step in object detection algorithms like YOLO (You Only Look Once). It helps in refining the detection results by removing redundant or overlapping bounding boxes, ensuring that each object is represented by a single, precise bounding box. Here’s a detailed explanation of how NMS works in the context of YOLO:

Steps of Non-Max Suppression

  1. Initial Detection: YOLO outputs multiple bounding boxes for each object, each with a confidence score and class probabilities.
  2. Filter by Confidence Threshold: First, filter out bounding boxes with confidence scores below a certain threshold. This step reduces the number of boxes to consider in the subsequent steps.
  3. Sort by Confidence: Sort the remaining bounding boxes by their confidence scores in descending order.
  4. Iterate through Boxes: Start with the highest confidence box and compare it with the rest of the boxes.
  5. Compute Intersection over Union (IoU): For each pair of boxes, calculate the Intersection over Union (IoU). IoU is a metric that measures the overlap between two bounding boxes. It is defined as the area of the intersection divided by the area of the union of the two boxes.
  6. Suppress Boxes: If the IoU of two boxes is higher than a predefined threshold (typically 0.5), suppress the box with the lower confidence score. This means it will be discarded from the list of detections.
  7. Repeat: Repeat the process for the next highest confidence box and continue until all boxes have been processed.

Example

Assume YOLO detects the following boxes for an object:

  • Box A: Confidence 0.9
  • Box B: Confidence 0.85
  • Box C: Confidence 0.75
  • Box D: Confidence 0.6

Let's say our IoU threshold is 0.5.

  1. Filter by Confidence Threshold: Suppose our threshold is 0.5, so all boxes remain.
  2. Sort by Confidence: A (0.9), B (0.85), C (0.75), D (0.6).
  3. Iterate and Suppress:Compare A with B, C, and D. If IoU(A, B) > 0.5, suppress B. Suppose IoU(A, B) < 0.5, then B remains. Similarly, if IoU(A, C) < 0.5 and IoU(A, D) < 0.5, C and D remain.now compare B with C and D. If IoU(B, C) > 0.5, suppress C. Suppose IoU(B, C) > 0.5, then C is suppressed. Similarly, compare B with D.Finally, compare C with D.

Python Implementation Example

Here's a simple Python implementation of NMS for YOLO:

https://github.com/maryamsoftdev/non_max_suppression

import numpy as np

def iou(box1, box2):
    # Compute the intersection over union of two bounding boxes
    x1, y1, x2, y2 = box1
    x1_, y1_, x2_, y2_ = box2
    
    xi1 = max(x1, x1_)
    yi1 = max(y1, y1_)
    xi2 = min(x2, x2_)
    yi2 = min(y2, y2_)
    
    inter_area = max(0, xi2 - xi1 + 1) * max(0, yi2 - yi1 + 1)
    
    box1_area = (x2 - x1 + 1) * (y2 - y1 + 1)
    box2_area = (x2_ - x1_ + 1) * (y2_ - y1_ + 1)
    
    union_area = box1_area + box2_area - inter_area
    
    return inter_area / union_area

def non_max_suppression(boxes, scores, threshold=0.5):
    indices = np.argsort(scores)[::-1]
    keep = []
    
    while indices.size > 0:
        i = indices[0]
        keep.append(i)
        rest_indices = indices[1:]
        
        suppressed_indices = []
        for j in rest_indices:
            if iou(boxes[i], boxes[j]) > threshold:
                suppressed_indices.append(j)
        
        indices = np.delete(indices, np.where(np.isin(indices, suppressed_indices)))
        indices = indices[1:]
    
    return keep

# Example usage
boxes = np.array([[100, 100, 210, 210], [105, 105, 215, 215], [200, 200, 310, 310]])
scores = np.array([0.9, 0.85, 0.75])

kept_indices = non_max_suppression(boxes, scores, threshold=0.5)
kept_boxes = boxes[kept_indices]

print("Kept boxes:", kept_boxes)
        

要查看或添加评论,请登录

Nabeelah Maryam的更多文章

社区洞察

其他会员也浏览了