⚡️ Speed up function _scale_bounding_box_to_original_image_shape by 12%
#43
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.
📄 12% (0.12x) speedup for
_scale_bounding_box_to_original_image_shapeinultralytics/utils/callbacks/comet.py⏱️ Runtime :
237 milliseconds→211 milliseconds(best of36runs)📝 Explanation and details
The optimizations achieve a 12% speedup by improving memory access patterns and reducing computational overhead in tensor operations:
Key Optimizations:
xyxy2xywhfunction (35% faster): Replaced four individual tensor assignments with two vectorized operations using slice notation:y[..., :2] = (x[..., :2] + x[..., 2:]) / 2computes both center coordinates in one operationy[..., 2:] = x[..., 2:] - x[..., :2]computes both width/height in one operationxywhn2xyxyfunction (7% faster): Extracted repeated tensor indexing into intermediate variables:c_x = x[..., 0],c_y = x[..., 1], etc. avoid redundant indexing operationsWhy This Works:
Performance Impact:
The function is called from
_format_ground_truth_annotations_for_detectionin a loop over bounding boxes, making these micro-optimizations meaningful. Test results show consistent 5-12% improvements across various box configurations, with the largest gains (12.3%) occurring in batch processing scenarios with 1000+ boxes where the vectorization benefits compound.Best suited for: Workloads with multiple bounding boxes per image and batch processing scenarios common in object detection pipelines.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_scale_bounding_box_to_original_image_shape-mira3nwsand push.