Skip to content

⚡️ Speed up method STrack.to_xyah by 11% #49

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

📄 11% (0.11x) speedup for STrack.to_xyah in supervision/tracker/byte_tracker/single_object_track.py

⏱️ Runtime : 27.4 microseconds 24.7 microseconds (best of 5 runs)

📝 Explanation and details

o3-mini
We can avoid a full copy of tlwh and a later in‐place modification by directly constructing the target array. In the code below we first convert the input to a float32 array (or assume it already is) and then compute the center x,y and aspect ratio without touching the original data (and without doing extra slice operations). This should provide a small performance benefit, especially when this conversion is called frequently.

Note that the return values remain exactly the same.

Explanation of the changes made:
• Instead of copying tlwh and then modifying slices in place, we compute center_x, center_y and aspect_ratio individually.
• We then create a new numpy array from these computed values so that the output exactly matches the original.
• The function signature is maintained and no other logic is altered.

Correctness verification report:

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

import numpy as np
import numpy.typing as npt
# imports
import pytest  # used for our unit tests
from supervision.tracker.byte_tracker.kalman_filter import KalmanFilter
from supervision.tracker.byte_tracker.single_object_track import STrack
from supervision.tracker.byte_tracker.utils import IdCounter

# unit tests

# Basic Functionality

def test_zero_width_height():
    tlwh = np.array([0, 0, 0, 0], dtype=np.float32)
    expected = np.array([0, 0, np.inf, 0], dtype=np.float32)


def test_extremely_large_values():
    tlwh = np.array([1e10, 1e10, 1e10, 1e10], dtype=np.float32)
    expected = np.array([1.5e10, 1.5e10, 1, 1e10], dtype=np.float32)

# Boundary Values
def test_minimal_non_zero_dimensions():
    tlwh = np.array([0, 0, 1e-10, 1e-10], dtype=np.float32)
    expected = np.array([5e-11, 5e-11, 1, 1e-10], dtype=np.float32)

# Floating Point Precision
def test_high_precision_floats():
    tlwh = np.array([0.123456789, 0.987654321, 0.111111111, 0.222222222], dtype=np.float32)
    expected = np.array([0.179012344, 1.098765432, 0.5, 0.222222222], dtype=np.float32)

# Performance and Scalability
def test_large_scale():
    tlwhs = np.random.rand(1000000, 4).astype(np.float32) * 100
    for tlwh in tlwhs:
        STrack.tlwh_to_xyah(tlwh)

# Invalid Inputs
def test_non_numeric_values():
    tlwh = ['a', 'b', 'c', 'd']
    with pytest.raises(TypeError):
        STrack.tlwh_to_xyah(tlwh)


def test_integration_with_strack():
    strack = STrack(np.array([0, 0, 10, 20], dtype=np.float32), np.array([1], dtype=np.float32), 1, None, IdCounter(), IdCounter())
    expected = np.array([5, 10, 0.5, 20], dtype=np.float32)

# Consistency
def test_consistency():
    strack = STrack(np.array([0, 0, 10, 20], dtype=np.float32), np.array([1], dtype=np.float32), 1, None, IdCounter(), IdCounter())
    codeflash_output = strack.to_xyah()
    codeflash_output = strack.to_xyah()

# State Dependency
def test_state_dependency():
    strack = STrack(np.array([0, 0, 10, 20], dtype=np.float32), np.array([1], dtype=np.float32), 1, None, IdCounter(), IdCounter())
    strack.mean = None
    expected = np.array([5, 10, 0.5, 20], dtype=np.float32)

    strack.mean = np.array([0, 0, 10, 20, 0, 0, 0, 0], dtype=np.float32)
    expected = np.array([5, 10, 0.5, 20], dtype=np.float32)

# Aspect Ratio Calculation
def test_aspect_ratio_calculation():
    tlwh = np.array([0, 0, 10, 20], dtype=np.float32)
    expected = np.array([5, 10, 0.5, 20], dtype=np.float32)

# Invalid Kalman Filter State
def test_invalid_kalman_filter_state():
    strack = STrack(np.array([0, 0, 10, 20], dtype=np.float32), np.array([1], dtype=np.float32), 1, None, IdCounter(), IdCounter())
    strack.kalman_filter = None
    expected = np.array([5, 10, 0.5, 20], dtype=np.float32)

# Multiple Tracks
def test_multiple_tracks():
    strack1 = STrack(np.array([0, 0, 10, 20], dtype=np.float32), np.array([1], dtype=np.float32), 1, None, IdCounter(), IdCounter())
    strack2 = STrack(np.array([10, 10, 20, 30], dtype=np.float32), np.array([1], dtype=np.float32), 1, None, IdCounter(), IdCounter())
    expected1 = np.array([5, 10, 0.5, 20], dtype=np.float32)
    expected2 = np.array([20, 25, 0.6666667, 30], dtype=np.float32)

# Rare or Unexpected Edge Cases

# Non-Standard Aspect Ratios
def test_extremely_wide_bounding_box():
    tlwh = np.array([0, 0, 1000, 1], dtype=np.float32)
    expected = np.array([500, 0.5, 1000, 1], dtype=np.float32)

def test_extremely_tall_bounding_box():
    tlwh = np.array([0, 0, 1, 1000], dtype=np.float32)
    expected = np.array([0.5, 500, 0.001, 1000], dtype=np.float32)

# Bounding Box with Negative Coordinates
def test_negative_coordinates():
    tlwh = np.array([-10, -20, 30, 40], dtype=np.float32)
    expected = np.array([5, 0, 0.75, 40], dtype=np.float32)

# Bounding Box with Mixed Sign Dimensions


def test_non_contiguous_memory_layout():
    tlwh = np.array([0, 0, 10, 20], dtype=np.float32)[::2]
    with pytest.raises(ValueError):
        STrack.tlwh_to_xyah(tlwh)

# High-Dimensional Input
def test_high_dimensional_input():
    tlwh = np.array([0, 0, 10, 20, 30], dtype=np.float32)
    with pytest.raises(ValueError):
        STrack.tlwh_to_xyah(tlwh)

# Empty Array Input

def test_integer_data_type():
    tlwh = np.array([0, 0, 10, 20], dtype=np.int32)
    with pytest.raises(TypeError):
        STrack.tlwh_to_xyah(tlwh)




def test_very_small_floating_point_values():
    tlwh = np.array([1e-45, 1e-45, 1e-45, 1e-45], dtype=np.float32)
    expected = np.array([1.5e-45, 1.5e-45, 1, 1e-45], dtype=np.float32)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

from __future__ import annotations

import numpy as np
import numpy.typing as npt
# imports
import pytest  # used for our unit tests
from supervision.tracker.byte_tracker.kalman_filter import KalmanFilter
from supervision.tracker.byte_tracker.single_object_track import STrack
from supervision.tracker.byte_tracker.utils import IdCounter


class TrackState:
    New = 0


# unit tests

Codeflash

o3-mini
We can avoid a full copy of tlwh and a later in‐place modification by directly constructing the target array. In the code below we first convert the input to a float32 array (or assume it already is) and then compute the center x,y and aspect ratio without touching the original data (and without doing extra slice operations). This should provide a small performance benefit, especially when this conversion is called frequently.

Note that the return values remain exactly the same.

Explanation of the changes made:
• Instead of copying tlwh and then modifying slices in place, we compute center_x, center_y and aspect_ratio individually.
• We then create a new numpy array from these computed values so that the output exactly matches the original.
• The function signature is maintained and no other logic is altered.
@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 04:50
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