Reference for ultralytics/utils/nms.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/utils/nms.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.utils.nms.TorchNMS
Ultralytics custom NMS implementation optimized for YOLO.
This class provides static methods for performing non-maximum suppression (NMS) operations on bounding boxes, including both standard NMS and batched NMS for multi-class scenarios.
Methods:
Name | Description |
---|---|
nms |
Optimized NMS with early termination that matches torchvision behavior exactly. |
batched_nms |
Batched NMS for class-aware suppression. |
Examples:
Perform standard NMS on boxes and scores
>>> boxes = torch.tensor([[0, 0, 10, 10], [5, 5, 15, 15]])
>>> scores = torch.tensor([0.9, 0.8])
>>> keep = TorchNMS.nms(boxes, scores, 0.5)
batched_nms
staticmethod
batched_nms(
boxes: Tensor,
scores: Tensor,
idxs: Tensor,
iou_threshold: float,
use_fast_nms: bool = False,
) -> torch.Tensor
Batched NMS for class-aware suppression.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
boxes
|
Tensor
|
Bounding boxes with shape (N, 4) in xyxy format. |
required |
scores
|
Tensor
|
Confidence scores with shape (N,). |
required |
idxs
|
Tensor
|
Class indices with shape (N,). |
required |
iou_threshold
|
float
|
IoU threshold for suppression. |
required |
use_fast_nms
|
bool
|
Whether to use the Fast-NMS implementation. |
False
|
Returns:
Type | Description |
---|---|
Tensor
|
Indices of boxes to keep after NMS. |
Examples:
Apply batched NMS across multiple classes
>>> boxes = torch.tensor([[0, 0, 10, 10], [5, 5, 15, 15]])
>>> scores = torch.tensor([0.9, 0.8])
>>> idxs = torch.tensor([0, 1])
>>> keep = TorchNMS.batched_nms(boxes, scores, idxs, 0.5)
Source code in ultralytics/utils/nms.py
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
|
fast_nms
staticmethod
fast_nms(
boxes: Tensor,
scores: Tensor,
iou_threshold: float,
use_triu: bool = True,
iou_func=box_iou,
) -> torch.Tensor
Fast-NMS implementation from https://arxiv.org/pdf/1904.02689 using upper triangular matrix operations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
boxes
|
Tensor
|
Bounding boxes with shape (N, 4) in xyxy format. |
required |
scores
|
Tensor
|
Confidence scores with shape (N,). |
required |
iou_threshold
|
float
|
IoU threshold for suppression. |
required |
use_triu
|
bool
|
Whether to use torch.triu operator for upper triangular matrix operations. |
True
|
iou_func
|
callable
|
Function to compute IoU between boxes. |
box_iou
|
Returns:
Type | Description |
---|---|
Tensor
|
Indices of boxes to keep after NMS. |
Examples:
Apply NMS to a set of boxes
>>> boxes = torch.tensor([[0, 0, 10, 10], [5, 5, 15, 15]])
>>> scores = torch.tensor([0.9, 0.8])
>>> keep = TorchNMS.nms(boxes, scores, 0.5)
Source code in ultralytics/utils/nms.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
|
nms
staticmethod
nms(boxes: Tensor, scores: Tensor, iou_threshold: float) -> torch.Tensor
Optimized NMS with early termination that matches torchvision behavior exactly.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
boxes
|
Tensor
|
Bounding boxes with shape (N, 4) in xyxy format. |
required |
scores
|
Tensor
|
Confidence scores with shape (N,). |
required |
iou_threshold
|
float
|
IoU threshold for suppression. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Indices of boxes to keep after NMS. |
Examples:
Apply NMS to a set of boxes
>>> boxes = torch.tensor([[0, 0, 10, 10], [5, 5, 15, 15]])
>>> scores = torch.tensor([0.9, 0.8])
>>> keep = TorchNMS.nms(boxes, scores, 0.5)
Source code in ultralytics/utils/nms.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
|
ultralytics.utils.nms.non_max_suppression
non_max_suppression(
prediction,
conf_thres: float = 0.25,
iou_thres: float = 0.45,
classes=None,
agnostic: bool = False,
multi_label: bool = False,
labels=(),
max_det: int = 300,
nc: int = 0,
max_time_img: float = 0.05,
max_nms: int = 30000,
max_wh: int = 7680,
rotated: bool = False,
end2end: bool = False,
return_idxs: bool = False,
)
Perform non-maximum suppression (NMS) on prediction results.
Applies NMS to filter overlapping bounding boxes based on confidence and IoU thresholds. Supports multiple detection formats including standard boxes, rotated boxes, and masks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prediction
|
Tensor
|
Predictions with shape (batch_size, num_classes + 4 + num_masks, num_boxes) containing boxes, classes, and optional masks. |
required |
conf_thres
|
float
|
Confidence threshold for filtering detections. Valid values are between 0.0 and 1.0. |
0.25
|
iou_thres
|
float
|
IoU threshold for NMS filtering. Valid values are between 0.0 and 1.0. |
0.45
|
classes
|
List[int]
|
List of class indices to consider. If None, all classes are considered. |
None
|
agnostic
|
bool
|
Whether to perform class-agnostic NMS. |
False
|
multi_label
|
bool
|
Whether each box can have multiple labels. |
False
|
labels
|
List[List[Union[int, float, Tensor]]]
|
A priori labels for each image. |
()
|
max_det
|
int
|
Maximum number of detections to keep per image. |
300
|
nc
|
int
|
Number of classes. Indices after this are considered masks. |
0
|
max_time_img
|
float
|
Maximum time in seconds for processing one image. |
0.05
|
max_nms
|
int
|
Maximum number of boxes for NMS. |
30000
|
max_wh
|
int
|
Maximum box width and height in pixels. |
7680
|
rotated
|
bool
|
Whether to handle Oriented Bounding Boxes (OBB). |
False
|
end2end
|
bool
|
Whether the model is end-to-end and doesn't require NMS. |
False
|
return_idxs
|
bool
|
Whether to return the indices of kept detections. |
False
|
Returns:
Name | Type | Description |
---|---|---|
output |
List[Tensor]
|
List of detections per image with shape (num_boxes, 6 + num_masks) containing (x1, y1, x2, y2, confidence, class, mask1, mask2, ...). |
keepi |
List[Tensor]
|
Indices of kept detections if return_idxs=True. |
Source code in ultralytics/utils/nms.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
|