⚡️ Speed up method ObjectDetectionEvalProcessor._box_iou by 99%
#12
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.
📄 99% (0.99x) speedup for
ObjectDetectionEvalProcessor._box_iouinunstructured/metrics/object_detection.py⏱️ Runtime :
3.20 milliseconds→1.61 milliseconds(best of250runs)📝 Explanation and details
The optimization replaces PyTorch's vectorized IoU calculation with a Numba-compiled implementation that provides significant speedup for this specific workload.
Key Changes:
@njit(fastmath=True, cache=True)decorator to create a compiled numpy-based IoU function that eliminates Python overheadWhy It's Faster:
[:, None, 2:]broadcasting which creates large intermediate tensors. The optimized version uses direct indexing in compiled loopsfastmath=Trueflag enables aggressive floating-point optimizations, whilecache=Trueensures compilation happens only oncePerformance Profile:
The optimization is particularly effective for object detection evaluation pipelines where IoU calculations are performed repeatedly on moderately-sized batches of bounding boxes, which is the typical use case for this metrics module.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-ObjectDetectionEvalProcessor._box_iou-mjcemef3and push.