feat: speed up box iou batch using 2d in-place ops instead of 3d #2001
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.


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.