IoU 和 NMS 工具¶
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)
¶
Compute the Intersection over Union (IoU) between two bounding boxes.
Note
Use box_iou
when computing IoU between two individual boxes.
For comparing multiple boxes (arrays of boxes), use box_iou_batch
for better
performance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Union[List[float], ndarray]
|
A single bounding box represented as [x_min, y_min, x_max, y_max]. |
required |
|
Union[List[float], ndarray]
|
A single bounding box represented as [x_min, y_min, x_max, y_max]. |
required |
Returns:
Name | Type | Description |
---|---|---|
IoU |
float
|
IoU score between the two boxes. Ranges from 0.0 (no overlap) to 1.0 (perfect overlap). |
Examples:
import numpy as np
import supervision as sv
box_true = np.array([100, 100, 200, 200])
box_detection = np.array([150, 150, 250, 250])
sv.box_iou(box_true=box_true, box_detection=box_detection)
# 0.14285814285714285
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 Intersection over Union (IoU) of two sets of bounding boxes -
boxes_true
and boxes_detection
. Both sets
of boxes are expected to be in (x_min, y_min, x_max, y_max)
format.
Note
Use box_iou
when computing IoU between two individual boxes.
For comparing multiple boxes (arrays of boxes), use box_iou_batch
for better
performance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
ndarray
|
2D |
required |
|
ndarray
|
2D |
required |
|
OverlapMetric
|
Metric used for matching detections in slices. |
IOU
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Pairwise IoU of boxes from |
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_true, boxes_detection=boxes_detection)
# array([
# [0.14285714, 0. ],
# [0. , 0.47058824]
# ])
Source code in supervision/detection/utils/iou_and_nms.py
147 148 149 150 151 152 153 154 155 156 157 158 159 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 |
|
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 for matching detections in slices. |
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 for matching detections in slices. |
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 for matching detections in slices. |
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 for matching detections in slices. |
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 for matching detections in slices. |
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 |