How to apply non-maximum suppression (NMS) to model predictions

Remove duplicate, overlapping boxes from any object detection model with supervision, and learn which models need no NMS at all.

Overview

Object detection models often predict several overlapping boxes for the same object. Non-maximum suppression (NMS) keeps the most confident box and removes every other box that overlaps it by more than a threshold, measured as intersection over union (IoU).

Whether you need to apply it yourself depends on the model:

  • RF-DETR (opens in a new tab), YOLOv10 and YOLO26 are end-to-end models. They learn not to predict duplicate boxes and need no NMS.
  • Ultralytics YOLOv8 and YOLO11, YOLOv5 and YOLO-NAS apply NMS in their own post-processing, with a setting for the threshold.
  • Models deployed with Roboflow, and the Object Detection Model block in Workflows, apply NMS on the server.

You apply NMS yourself when you combine predictions from more than one source (two models, tiles of a large image, or several augmented copies of one image), when a model returns raw boxes, or when you want a stricter or class-agnostic pass than the model ran. supervision (opens in a new tab), Roboflow's open source computer vision library, does it in one call on any model's predictions.

Step 1: Install supervision

Shell
pip install supervision

Step 2: Load predictions into supervision

Convert your model's output to an sv.Detections object with the loader for its library:

  • Ultralytics YOLO (YOLOv8, YOLO11, YOLOv12): sv.Detections.from_ultralytics()
  • YOLOv5: sv.Detections.from_yolov5()
  • Hugging Face Transformers: sv.Detections.from_transformers()
  • Detectron2: sv.Detections.from_detectron2()
  • MMDetection: sv.Detections.from_mmdetection()
  • YOLO-NAS: sv.Detections.from_yolo_nas()
  • PaddleDetection: sv.Detections.from_paddledet()
  • TensorFlow Hub: sv.Detections.from_tensorflow()
  • DeepSparse: sv.Detections.from_deepsparse()
  • Roboflow Inference and the hosted API: sv.Detections.from_inference()

Step 3: Apply NMS

Call with_nms() on the detections. With an Ultralytics model:

apply_nms.pyPython
import supervision as sv
from ultralytics import YOLO

model = YOLO("yolo11n.pt")
result = model("image.jpg")[0]

detections = sv.Detections.from_ultralytics(result)
detections = detections.with_nms(threshold=0.5, class_agnostic=False)

To combine predictions from two models, merge them first, then apply NMS once to the result:

merge_and_nms.pyPython
detections = sv.Detections.merge([detections_a, detections_b])
detections = detections.with_nms(threshold=0.5, class_agnostic=True)

with_nms() returns a new sv.Detections object, so you can draw, count or save the result like any other detections.

Choose the threshold and class mode

  • threshold is the IoU above which the less confident of two boxes is removed. The default is 0.5. Lower it to remove more overlapping boxes; raise it for crowded scenes where real objects overlap.
  • class_agnostic=False, the default, only compares boxes of the same class. Set it to True to also remove overlapping boxes of different classes, for example when a model labels one object as two similar classes, or when you merge models whose class IDs do not match.
  • Segmentation predictions are compared by mask overlap and oriented boxes by their rotated IoU, with the same call.

with_nmm() takes the same arguments but merges overlapping boxes into one instead of discarding them. It suits tiled inference on large images, where an object cut by a tile edge is predicted as two partial boxes.

Set NMS on Roboflow models and Workflows

Models deployed with Roboflow apply NMS on the server, so you set it on the request rather than in your code. iou_threshold is the overlap above which boxes are removed (0.3 by default), and class_agnostic_nms compares boxes across classes (off by default). The Object Detection Model block in Workflows has the same two settings, and there is no separate NMS step to add.

Apply NMS with a coding agent

The Roboflow MCP server connects a coding agent, such as Claude Code, Cursor or Codex, to your Roboflow workspace. It can run your model, compare predictions before and after NMS, and tune the threshold for you. For example:

My model predicts two overlapping boxes on some objects. Run it on ./images, apply class-agnostic NMS at an IoU of 0.5, and tell me how many boxes it removed per image.
Start where you are

Speak with an AI expert

Our team will help you start solving business problems on the first call.

  • Solution architecting
  • Live demonstration
  • Pricing and specifications
  • Feasibility assessment

Over 80,000 organizations build with Roboflow.

Rivian
Pella
Chobani
USG Corporation
BNSF Railway
American Woodmark
Outcomes
Patrick Industries
Peer Robotics