Skip to content

Conversation

@AnonymDevOSS
Copy link

@AnonymDevOSS AnonymDevOSS commented Oct 27, 2025

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.

(.venv) wednesday:benchmark rafelbennasar$ python benchmark_iou.py 
Benchmarking 2 functions on sizes=[(512, 1024)], 5 runs × 5 iterations each

- box_iou_batch  (512×1024)
[benchmark_iou.py](https://github.com/user-attachments/files/23167441/benchmark_iou.py)

  Run 01:   0.241 s
  Run 02:   0.232 s
  Run 03:   0.522 s
  Run 04:   0.246 s
  Run 05:   0.207 s
  Average:   0.290 s

- box_iou_batch_alt  (512×1024)
  Run 01:   0.060 s
  Run 02:   0.064 s
  Run 03:   0.066 s
  Run 04:   0.065 s
  Run 05:   0.066 s
  Average:   0.064 s


= Summary (5 runs × 5 iterations each):
box_iou_batch_alt           avg=  0.064s  stdev= 0.002s  min= 0.060s  max= 0.066s
box_iou_batch               avg=  0.290s  stdev= 0.131s  min= 0.207s  max= 0.522s


@CLAassistant
Copy link

CLAassistant commented Oct 27, 2025

CLA assistant check
All committers have signed the CLA.

@SkalskiP
Copy link
Collaborator

Hi @AnonymDevOSS 👋🏻 thanks a lot for the contribution!

Please accept the CLA license so we can move forward.

@AnonymDevOSS
Copy link
Author

I’m a little confused because I believe I’ve already accepted the license agreement.

image

@SkalskiP
Copy link
Collaborator

Looks like you used different account to commit the code.

Screenshot 2025-10-28 at 13 05 08

without creating intermediary (N, M, 2) arrays.
@AnonymDevOSS AnonymDevOSS force-pushed the feat/speed-up-box-iou-batch branch from 6aa194f to 35ff87b Compare October 28, 2025 12:51
@AnonymDevOSS
Copy link
Author

Oops. Thanks for the heads up! Fixed.

return ious


def box_iou_batch_alt(
Copy link
Collaborator

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.

import numpy as np


def generate_boxes(
Copy link
Collaborator

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():
Copy link
Collaborator

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.

Copy link
Author

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.

@SkalskiP
Copy link
Collaborator

SkalskiP commented Nov 5, 2025

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 supervision-0.27.0 release is coming up, so if you manage to apply the changes soon, this PR could be included in that release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants