-
Notifications
You must be signed in to change notification settings - Fork 3k
feat: speed up box iou batch using 2d in-place ops instead of 3d #2001
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
35ff87b
ceefe0b
da7c5cd
bb87ab0
eee6a85
f716b7f
1e2dc8f
42d92ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import random | ||
|
|
||
| import numpy as np | ||
|
|
||
|
|
||
| def generate_boxes( | ||
|
||
| n: int, | ||
| W: int = 1920, | ||
| H: int = 1080, | ||
| min_size: int = 20, | ||
| max_size: int = 200, | ||
| seed: int | None = 1, | ||
| ): | ||
| """ | ||
| Generate N valid bounding boxes of format [x_min, y_min, x_max, y_max]. | ||
| Args: | ||
| n (int): Number of boexs to generate | ||
| W (int): Image width | ||
| H (int): Image height | ||
| min_size (int): Minimum box size (width/height) | ||
| max_size (int): Maximum box size (width/height) | ||
| seed (int | None): Random seed for reproducibility | ||
| Returns: | ||
| list[list[float]] | np.ndarray: List of boxes | ||
| """ | ||
| random.seed(seed) | ||
| boxes = [] | ||
| for _ in range(n): | ||
| w = random.uniform(min_size, max_size) | ||
| h = random.uniform(min_size, max_size) | ||
| x1 = random.uniform(0, W - w) | ||
| y1 = random.uniform(0, H - h) | ||
| x2 = x1 + w | ||
| y2 = y1 + h | ||
| boxes.append([x1, y1, x2, y2]) | ||
| return np.array(boxes, dtype=np.float32) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,10 +7,13 @@ | |
|
|
||
| from supervision.detection.utils.iou_and_nms import ( | ||
| _group_overlapping_boxes, | ||
| box_iou_batch, | ||
| box_iou_batch_alt, | ||
| box_non_max_suppression, | ||
| mask_non_max_merge, | ||
| mask_non_max_suppression, | ||
| ) | ||
| from test.detection.utils.functions import generate_boxes | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
|
|
@@ -631,3 +634,14 @@ def test_mask_non_max_merge( | |
| sorted_result = sorted([sorted(group) for group in result]) | ||
| sorted_expected_result = sorted([sorted(group) for group in expected_result]) | ||
| assert sorted_result == sorted_expected_result | ||
|
|
||
|
|
||
| def test_box_iou_batch_and_alt_equivalence(): | ||
|
||
| boxes_true = generate_boxes(20, seed=1) | ||
| boxes_detection = generate_boxes(30, seed=2) | ||
|
|
||
| iou_a = box_iou_batch(boxes_true, boxes_detection) | ||
| iou_b = box_iou_batch_alt(boxes_true, boxes_detection) | ||
|
|
||
| assert iou_a.shape == iou_b.shape | ||
| assert np.allclose(iou_a, iou_b, rtol=1e-6, atol=1e-6) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's rename it to
box_iou_batchand remove old implementation of this function.