π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
perf(geometry): reduce per-axis before combining in bbox_to_mask (#3831)
Edgar Ribaf9184c5e2508
diff --git a/kornia/geometry/bbox.py b/kornia/geometry/bbox.pyindex 731db2baf..c0db0e3fa 100644--- a/kornia/geometry/bbox.py+++ b/kornia/geometry/bbox.py@@ -235,7 +235,12 @@ def bbox_to_mask(boxes: torch.Tensor, width: int, height: int) -> torch.Tensor:y_min = boxes[:, 0, 1].view(-1, 1, 1)x_max = boxes[:, 2, 0].view(-1, 1, 1)y_max = boxes[:, 2, 1].view(-1, 1, 1)- mask = (xx >= x_min) & (xx <= x_max) & (yy >= y_min) & (yy <= y_max)+ # Reduce along each axis first (cheap ``(B, 1, W)`` and ``(B, H, 1)`` ands), then combine once.+ # The previous ``a & b & c & d`` chained two full ``(B, H, W)`` ands; this does a single one β+ # byte-identical result, half the full-grid work (the dominant cost when masking large images).+ x_in = (xx >= x_min) & (xx <= x_max)+ y_in = (yy >= y_min) & (yy <= y_max)+ mask = x_in & y_inreturn mask.to(boxes.dtype)