Skip to content
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

⚡️ Speed up function select_top_confidence_detection by 188% #1092

Conversation

misrasaurabh1
Copy link
Contributor

📄 188% (1.88x) speedup for select_top_confidence_detection in inference/core/workflows/core_steps/common/query_language/operations/detections/base.py

⏱️ Runtime : 1.02 millisecond 355 microseconds (best of 491 runs)

📝 Explanation and details

Here is an optimized version of the program.

Correctness verification report:

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

import numpy as np
# imports
import pytest  # used for our unit tests
from inference.core.workflows.core_steps.common.query_language.operations.detections.base import \
    select_top_confidence_detection


# Mocking the sv.Detections class for testing purposes
class Detections:
    def __init__(self, confidence):
        self.confidence = np.array(confidence)
    
    def __len__(self):
        return len(self.confidence)
    
    def __getitem__(self, index):
        return Detections([self.confidence[index]])
from inference.core.workflows.core_steps.common.query_language.operations.detections.base import \
    select_top_confidence_detection

# unit tests

def test_single_detection():
    # Single detection
    detections = Detections([0.5])
    codeflash_output = select_top_confidence_detection(detections)

def test_multiple_unique_confidence_scores():
    # Multiple detections with unique confidence scores
    detections = Detections([0.1, 0.5, 0.9])
    codeflash_output = select_top_confidence_detection(detections)

def test_empty_detections():
    # Empty detections
    detections = Detections([])
    codeflash_output = select_top_confidence_detection(detections)

def test_all_zero_confidence_scores():
    # All zero confidence scores
    detections = Detections([0.0, 0.0, 0.0])
    codeflash_output = select_top_confidence_detection(detections)

def test_multiple_maximum_confidence_scores():
    # Multiple maximum confidence scores
    detections = Detections([0.9, 0.9, 0.8])
    codeflash_output = select_top_confidence_detection(detections)

def test_large_number_of_detections():
    # Large number of detections
    large_confidences = np.random.rand(1000)
    detections = Detections(large_confidences)
    codeflash_output = select_top_confidence_detection(detections)

def test_high_precision_confidence_scores():
    # High precision confidence scores
    detections = Detections([0.123456789, 0.987654321, 0.555555555])
    codeflash_output = select_top_confidence_detection(detections)

def test_negative_confidence_scores():
    # Negative confidence scores
    detections = Detections([-0.1, -0.5, -0.9])
    codeflash_output = select_top_confidence_detection(detections)


def test_minimum_and_maximum_float_values():
    # Minimum and maximum float values
    detections = Detections([np.finfo(float).min, np.finfo(float).max])
    codeflash_output = select_top_confidence_detection(detections)

def test_immutable_input():
    # Ensure the original detections object is not modified
    detections = Detections([0.5, 0.7, 0.9])
    original_confidences = deepcopy(detections.confidence)
    codeflash_output = select_top_confidence_detection(detections)

def test_non_numeric_confidence_scores():
    # Non-numeric confidence scores should raise an error
    detections = Detections([0.5, 'high', None])
    with pytest.raises(TypeError):
        select_top_confidence_detection(detections)

# Run the tests
if __name__ == "__main__":
    pytest.main()
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

from copy import deepcopy

import numpy as np
# imports
import pytest  # used for our unit tests
from inference.core.workflows.core_steps.common.query_language.operations.detections.base import \
    select_top_confidence_detection


# Mocking the sv.Detections class for testing purposes
class MockDetections:
    def __init__(self, confidence):
        self.confidence = np.array(confidence)
    
    def __len__(self):
        return len(self.confidence)
    
    def __getitem__(self, index):
        return MockDetections([self.confidence[index]])
from inference.core.workflows.core_steps.common.query_language.operations.detections.base import \
    select_top_confidence_detection


# unit tests
def test_empty_detections():
    # Test with empty detections
    detections = MockDetections([])
    codeflash_output = select_top_confidence_detection(detections)

def test_single_detection():
    # Test with a single detection
    detections = MockDetections([0.5])
    codeflash_output = select_top_confidence_detection(detections)

def test_multiple_unique_confidences():
    # Test with multiple detections with unique confidence scores
    detections = MockDetections([0.1, 0.5, 0.9])
    codeflash_output = select_top_confidence_detection(detections)

def test_multiple_duplicate_highest_confidences():
    # Test with multiple detections with duplicate highest confidence scores
    detections = MockDetections([0.5, 0.9, 0.9])
    codeflash_output = select_top_confidence_detection(detections)

def test_all_equal_confidences():
    # Test with multiple detections with all equal confidence scores
    detections = MockDetections([0.5, 0.5, 0.5])
    codeflash_output = select_top_confidence_detection(detections)

def test_non_numeric_confidences():
    # Test with non-numeric confidence scores (if applicable)
    detections = MockDetections(['high', 'medium', 'low'])
    with pytest.raises(TypeError):
        select_top_confidence_detection(detections)


def test_large_number_of_detections():
    # Test with a large number of detections
    detections = MockDetections(np.random.rand(10000))
    codeflash_output = select_top_confidence_detection(detections)

def test_performance_large_scale():
    # Performance test with large scale dataset
    detections = MockDetections(np.random.rand(1000000))
    codeflash_output = select_top_confidence_detection(detections)

# Run the tests
if __name__ == "__main__":
    pytest.main()
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

Codeflash

Here is an optimized version of the program.
@CLAassistant
Copy link

CLAassistant commented Mar 21, 2025

CLA assistant check
All committers have signed the CLA.

@PawelPeczek-Roboflow
Copy link
Collaborator

Could you please sign CLA

@PawelPeczek-Roboflow PawelPeczek-Roboflow merged commit 5b27747 into roboflow:main Mar 26, 2025
1 check passed
@codeflash-ai codeflash-ai bot deleted the codeflash/optimize-select_top_confidence_detection-m7drsd4o branch March 26, 2025 20:39
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