-
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
base: develop
Are you sure you want to change the base?
feat: speed up box iou batch using 2d in-place ops instead of 3d #2001
Conversation
|
Hi @AnonymDevOSS 👋🏻 thanks a lot for the contribution! Please accept the CLA license so we can move forward. |
without creating intermediary (N, M, 2) arrays.
6aa194f to
35ff87b
Compare
|
Oops. Thanks for the heads up! Fixed. |
| return ious | ||
|
|
||
|
|
||
| def box_iou_batch_alt( |
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_batch and remove old implementation of this function.
test/detection/utils/functions.py
Outdated
| import numpy as np | ||
|
|
||
|
|
||
| def generate_boxes( |
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.
In supervision/test/test_utils.py, there are already two functions, mock_detections and mock_key_points. It seems we are duplicating some logic from mock_detections. Try to unify them.
If that’s not possible, at least move generate_boxes to supervision/test/test_utils.py and make its arguments and naming conventions consistent with the existing functions.
One quick improvement would be to replace the separate W and H arguments with a single resolution_wh argument, which we use throughout the codebase.
| assert sorted_result == sorted_expected_result | ||
|
|
||
|
|
||
| def test_box_iou_batch_and_alt_equivalence(): |
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.
As we will rename box_iou_batch_alt to box_iou_batch that test no longer will be necessary.
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.
Added additional test cases rather than using the trivial approach of comparing against the original implementation.
|
Hi @AnonymDevOSS 👋🏻 Sorry for the delay, I’ve been a bit busy with other tasks at Roboflow. I left a few comments on the PR. The |


Description
I focused on improving the performance of the IoU calculation between bounding boxes.
This change adds a new function box_iou_batch_alt, which performs the same computation as box_iou_batch but runs faster and uses less memory.
The optimization uses in-place NumPy operations and avoids creating temporary arrays.
Results are identical to the original implementation within a small numerical tolerance.
No new dependencies were added.
Type of change
Performance improvement (non-breaking change)
How has this change been tested, please provide a testcase or example of how you tested the change?
I compared the outputs of both functions (box_iou_batch and box_iou_batch_alt) using random test boxes.
A simple pytest test checks that the results are equal within rtol=1e-6 and atol=1e-6.
Both functions return the same values for IoU and IoS modes.
Benchmarks show that the new version is around 2–5× faster on typical box sizes.
I also add a benchmark.