Object detectors output boxes, and almost everything about them depends on one question: how much do two boxes overlap? Intersection over union answers it with a number between 0 and 1. The intersection is the overlapping rectangle (whose width and height must be clamped at zero when the boxes do not touch), and the union is the sum of both areas minus the intersection so it is not counted twice. IoU is used to decide whether a prediction matches a ground-truth box, to suppress duplicate detections and to assign training targets to anchors, so it is computed between every pair of two sets of boxes at once, which makes broadcasting the heart of this exercise.
Implement box_iou.
(x1, y1, x2, y2) with x2 >= x1 and y2 >= y1. a has shape (N, 4) and b has shape (M, 4).box_iou(a, b) returns the (N, M) matrix of pairwise IoUs. Intersection width and height are clamped at 0.0.0.Broadcast a[:, None, :] against b[None, :, :] to get all pairs at once.
Intersection corners: max of the top-left corners and min of the bottom-right corners.
No results yet
Run your code to check it against the examples, or submit to run every test.