IoU and NMS Utils¶
supervision.detection.utils.iou_and_nms.OverlapFilter
¶
Bases: Enum
Enum specifying the strategy for filtering overlapping detections.
Attributes:
| Name | Type | Description |
|---|---|---|
NONE |
Do not filter detections based on overlap. |
|
NON_MAX_SUPPRESSION |
Filter detections using non-max suppression. This means, detections that overlap by more than a set threshold will be discarded, except for the one with the highest confidence. |
|
NON_MAX_MERGE |
Merge detections with non-max merging. This means, detections that overlap by more than a set threshold will be merged into a single detection. |
Source code in supervision/detection/utils/iou_and_nms.py
supervision.detection.utils.iou_and_nms.OverlapMetric
¶
Bases: Enum
Enum specifying the metric for measuring overlap between detections.
Attributes:
| Name | Type | Description |
|---|---|---|
IOU |
Intersection over Union. A region-overlap metric that compares two shapes (usually bounding boxes or masks) by normalising the shared area with the area of their union. |
|
IOS |
Intersection over Smaller, a region-overlap metric that compares two shapes (usually bounding boxes or masks) by normalising the shared area with the smaller of the two shapes. |
Source code in supervision/detection/utils/iou_and_nms.py
supervision.detection.utils.iou_and_nms.box_iou(box_true, box_detection, overlap_metric=OverlapMetric.IOU)
¶
Compute overlap metric between two bounding boxes.
Supports standard IOU (intersection-over-union) and IOS
(intersection-over-smaller-area) metrics. Returns the overlap value in range
[0, 1].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
`list[float]` or `numpy.array`
|
Ground truth box in format
|
required |
|
`list[float]` or `numpy.array`
|
Detected box in format
|
required |
|
`OverlapMetric` or `str`
|
Overlap type.
Use |
IOU
|
Returns:
| Type | Description |
|---|---|
`float`
|
Overlap value between boxes in |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
import supervision as sv
box_true = [100, 100, 200, 200]
box_detection = [150, 150, 250, 250]
sv.box_iou(box_true, box_detection, overlap_metric=sv.OverlapMetric.IOU)
# 0.14285714285714285
sv.box_iou(box_true, box_detection, overlap_metric=sv.OverlapMetric.IOS)
# 0.25
Source code in supervision/detection/utils/iou_and_nms.py
supervision.detection.utils.iou_and_nms.box_iou_batch(boxes_true, boxes_detection, overlap_metric=OverlapMetric.IOU)
¶
Compute pairwise overlap scores between batches of bounding boxes.
Supports standard IOU (intersection-over-union) and IOS
(intersection-over-smaller-area) metrics for all boxes_true and
boxes_detection pairs. Returns a matrix of overlap values in range
[0, 1], matching each box from the first batch to each from the second.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
`numpy.array`
|
Array of reference boxes in
shape |
required |
|
`numpy.array`
|
Array of detected boxes in
shape |
required |
|
`OverlapMetric` or `str`
|
Overlap type.
Use |
IOU
|
Returns:
| Type | Description |
|---|---|
`numpy.array`
|
Overlap matrix of shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
import numpy as np
import supervision as sv
boxes_true = np.array([
[100, 100, 200, 200],
[300, 300, 400, 400]
])
boxes_detection = np.array([
[150, 150, 250, 250],
[320, 320, 420, 420]
])
sv.box_iou_batch(boxes_true, boxes_detection, overlap_metric=OverlapMetric.IOU)
# array([[0.14285715, 0. ],
# [0. , 0.47058824]])
sv.box_iou_batch(boxes_true, boxes_detection, overlap_metric=OverlapMetric.IOS)
# array([[0.25, 0. ],
# [0. , 0.64]])
Source code in supervision/detection/utils/iou_and_nms.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | |
supervision.detection.utils.iou_and_nms.box_iou_batch_with_jaccard(boxes_true, boxes_detection, is_crowd)
¶
Calculate the intersection over union (IoU) between detection bounding boxes (dt) and ground-truth bounding boxes (gt). Reference: https://github.com/rafaelpadilla/review_object_detection_metrics
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
List[List[float]]
|
List of ground-truth bounding boxes in the format [x, y, width, height]. |
required |
|
List[List[float]]
|
List of detection bounding boxes in the format [x, y, width, height]. |
required |
|
List[bool]
|
List indicating if each ground-truth bounding box is a crowd region or not. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Array of IoU values of shape (len(dt), len(gt)). |
Examples:
import numpy as np
import supervision as sv
boxes_true = [
[10, 20, 30, 40], # x, y, w, h
[15, 25, 35, 45]
]
boxes_detection = [
[12, 22, 28, 38],
[16, 26, 36, 46]
]
is_crowd = [False, False]
ious = sv.box_iou_batch_with_jaccard(
boxes_true=boxes_true,
boxes_detection=boxes_detection,
is_crowd=is_crowd
)
# array([
# [0.8866..., 0.4960...],
# [0.4000..., 0.8622...]
# ])
Source code in supervision/detection/utils/iou_and_nms.py
supervision.detection.utils.iou_and_nms.mask_iou_batch(masks_true, masks_detection, overlap_metric=OverlapMetric.IOU, memory_limit=1024 * 5)
¶
Compute Intersection over Union (IoU) of two sets of masks -
masks_true and masks_detection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ndarray
|
3D |
required |
|
ndarray
|
3D |
required |
|
OverlapMetric
|
Metric used to compute the degree of overlap between pairs of masks (e.g., IoU, IoS). |
IOU
|
|
int
|
memory limit in MB, default is 1024 * 5 MB (5GB). |
1024 * 5
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Pairwise IoU of masks from |
Source code in supervision/detection/utils/iou_and_nms.py
supervision.detection.utils.iou_and_nms.oriented_box_iou_batch(boxes_true, boxes_detection)
¶
Compute Intersection over Union (IoU) of two sets of oriented bounding boxes -
boxes_true and boxes_detection. Both sets of boxes are expected to be in
((x1, y1), (x2, y2), (x3, y3), (x4, y4)) format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ndarray
|
a |
required |
|
ndarray
|
a |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Pairwise IoU of boxes from |
Source code in supervision/detection/utils/iou_and_nms.py
supervision.detection.utils.iou_and_nms.box_non_max_suppression(predictions, iou_threshold=0.5, overlap_metric=OverlapMetric.IOU)
¶
Perform Non-Maximum Suppression (NMS) on object detection predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ndarray
|
An array of object detection predictions in
the format of |
required |
|
float
|
The intersection-over-union threshold to use for non-maximum suppression. |
0.5
|
|
OverlapMetric
|
Metric used to compute the degree of overlap between pairs of boxes (e.g., IoU, IoS). |
IOU
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: A boolean array indicating which predictions to keep after n on-maximum suppression. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If |
Source code in supervision/detection/utils/iou_and_nms.py
supervision.detection.utils.iou_and_nms.mask_non_max_suppression(predictions, masks, iou_threshold=0.5, overlap_metric=OverlapMetric.IOU, mask_dimension=640)
¶
Perform Non-Maximum Suppression (NMS) on segmentation predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ndarray
|
A 2D array of object detection predictions in
the format of |
required |
|
ndarray
|
A 3D array of binary masks corresponding to the predictions.
Shape: |
required |
|
float
|
The intersection-over-union threshold to use for non-maximum suppression. |
0.5
|
|
OverlapMetric
|
Metric used to compute the degree of overlap between pairs of masks (e.g., IoU, IoS). |
IOU
|
|
int
|
The dimension to which the masks should be resized before computing IOU values. Defaults to 640. |
640
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: A boolean array indicating which predictions to keep after non-maximum suppression. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If |
Source code in supervision/detection/utils/iou_and_nms.py
supervision.detection.utils.iou_and_nms.box_non_max_merge(predictions, iou_threshold=0.5, overlap_metric=OverlapMetric.IOU)
¶
Apply greedy version of non-maximum merging per category to avoid detecting too many overlapping bounding boxes for a given object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[float64]
|
An array of shape |
required |
|
float
|
The intersection-over-union threshold to use for non-maximum suppression. Defaults to 0.5. |
0.5
|
|
OverlapMetric
|
Metric used to compute the degree of overlap between pairs of boxes (e.g., IoU, IoS). |
IOU
|
Returns:
| Type | Description |
|---|---|
list[list[int]]
|
list[list[int]]: Groups of prediction indices be merged. Each group may have 1 or more elements. |
Source code in supervision/detection/utils/iou_and_nms.py
supervision.detection.utils.iou_and_nms.mask_non_max_merge(predictions, masks, iou_threshold=0.5, mask_dimension=640, overlap_metric=OverlapMetric.IOU)
¶
Perform Non-Maximum Merging (NMM) on segmentation predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ndarray
|
A 2D array of object detection predictions in
the format of |
required |
|
ndarray
|
A 3D array of binary masks corresponding to the predictions.
Shape: |
required |
|
float
|
The intersection-over-union threshold to use for non-maximum suppression. |
0.5
|
|
int
|
The dimension to which the masks should be resized before computing IOU values. Defaults to 640. |
640
|
|
OverlapMetric
|
Metric used to compute the degree of overlap between pairs of masks (e.g., IoU, IoS). |
IOU
|
Returns:
| Type | Description |
|---|---|
list[list[int]]
|
np.ndarray: A boolean array indicating which predictions to keep after non-maximum suppression. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If |