TensorFlow
Non-Maximum Suppression
Machine Learning
Computer Vision
Object Detection

Tensorflow Non-Maximum Suppression

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

Introduction

TensorFlow's Non-Maximum Suppression (NMS) is a crucial technique used primarily in object detection tasks. It serves to filter out overlapping bounding boxes by selecting only the most probable detections, thus refining the final output regions. This process dramatically increases the efficiency and accuracy of object detection models by eliminating redundant or less likely bounding box predictions.

Understanding Non-Maximum Suppression

The Need for NMS

In object detection models, such as Faster-RCNN, YOLO, or SSD, the network outputs multiple bounding boxes for the objects in an image. These boxes often overlap, leading to multiple detections for the same object. Non-Maximum Suppression helps in selecting the best bounding box from a set of overlapping boxes based on specific scoring criteria.

How NMS Works

  1. Score-Based Selection: Each bounding box is assigned a score, usually the class prediction probability. NMS starts by sorting all bounding boxes in descending order of their scores.
  2. IoU Calculation: For each box BiB_i, the method calculates Intersection over Union (IoU) with every other box. IoU is the ratio of the overlap area to the union area of two bounding boxes.
  3. Suppress Boxes: If the IoU of a box BjB_j with BiB_i is greater than a predefined threshold, BjB_j is suppressed (removed from consideration).
  4. Iterate and Repeat: The process repeats recursively, moving through the list sorted by scores, and suppresses boxes with high IoU until all boxes have been checked.

The result is a reduced list of bounding boxes with minimized overlap and high confidence scores for the detected objects.

Implementing NMS in TensorFlow

Basic Usage

TensorFlow provides a dedicated function for Non-Maximum Suppression, tf.image.non_max_suppression. Here is a simple example of implementing NMS:

  • boxes: A 2-D tensor of shape [num_boxes, 4] representing the coordinates of each box.
  • scores: A 1-D tensor representing the confidence score of each box.
  • max_output_size: The maximum number of boxes to be selected.
  • iou_threshold: A float representing the IoU threshold. Boxes with IoU greater than this value are suppressed.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free 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.