Skip to content

⚡️ Speed up function get_detection_size_category by 8% #44

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

codeflash-ai[bot]
Copy link

@codeflash-ai codeflash-ai bot commented Feb 3, 2025

📄 8% (0.08x) speedup for get_detection_size_category in supervision/metrics/utils/object_size.py

⏱️ Runtime : 1.16 millisecond 1.08 millisecond (best of 122 runs)

📝 Explanation and details

o3-mini
We can optimize the three size‐category functions by unifying the similar logic into a single helper function that converts an array of areas to its corresponding category, using a nested np.where instead of three separate boolean-indexing assignments. In the oriented bounding box function we can make the shoelace computation more concise and potentially faster by using np.roll. The overall behavior remains exactly the same.

Below is the optimized code:


Explanation of the changes made:

  1. Added a new helper function _get_size_category_from_areas that takes an array of areas and returns the corresponding category using nested np.where calls.
  2. Replaced the three boolean-index assignments in get_bbox_size_category, get_mask_size_category, and get_obb_size_category with a single call to _get_size_category_from_areas.
  3. For the oriented bounding boxes, used np.roll to compute the shoelace formula in a vectorized and concise manner.
  4. The rest of the behavior and function signatures remain unchanged.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 4 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests Details
from __future__ import annotations

from unittest.mock import MagicMock

import numpy as np
import numpy.typing as npt
# imports
import pytest  # used for our unit tests
from supervision.config import ORIENTED_BOX_COORDINATES
from supervision.detection.core import Detections
from supervision.metrics.core import MetricTarget
from supervision.metrics.utils.object_size import get_detection_size_category

# unit tests














from __future__ import annotations

from unittest.mock import patch

import numpy as np
# imports
import pytest  # used for our unit tests
from numpy.typing import NDArray
from supervision.config import ORIENTED_BOX_COORDINATES
from supervision.detection.core import Detections
from supervision.metrics.core import MetricTarget
from supervision.metrics.utils.object_size import get_detection_size_category


# unit tests
def test_single_bounding_box():
    detections = Detections(xyxy=np.array([[10, 10, 20, 20]]))
    codeflash_output = get_detection_size_category(detections, MetricTarget.BOXES)

def test_multiple_bounding_boxes():
    detections = Detections(xyxy=np.array([[10, 10, 20, 20], [30, 30, 40, 40]]))
    codeflash_output = get_detection_size_category(detections, MetricTarget.BOXES)










def test_large_number_of_bounding_boxes():
    detections = Detections(xyxy=np.random.randint(0, 100, size=(10000, 4)))
    codeflash_output = get_detection_size_category(detections, MetricTarget.BOXES)

Codeflash

o3-mini
We can optimize the three size‐category functions by unifying the similar logic into a single helper function that converts an array of areas to its corresponding category, using a nested np.where instead of three separate boolean-indexing assignments. In the oriented bounding box function we can make the shoelace computation more concise and potentially faster by using np.roll. The overall behavior remains exactly the same.

Below is the optimized code:

--------------------------------------------------
Explanation of the changes made:
1. Added a new helper function _get_size_category_from_areas that takes an array of areas and returns the corresponding category using nested np.where calls.
2. Replaced the three boolean-index assignments in get_bbox_size_category, get_mask_size_category, and get_obb_size_category with a single call to _get_size_category_from_areas.
3. For the oriented bounding boxes, used np.roll to compute the shoelace formula in a vectorized and concise manner.
4. The rest of the behavior and function signatures remain unchanged.
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Feb 3, 2025
@codeflash-ai codeflash-ai bot requested a review from misrasaurabh1 February 3, 2025 03:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⚡️ codeflash Optimization PR opened by Codeflash AI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants