Before you start
Compute the IoU of two axis-aligned bounding boxes.
Each box is [x1, y1, x2, y2] with (x1, y1) the top-left corner and
(x2, y2) the bottom-right.
IoU = area of intersection / area of union
The intersection rectangle spans
x1 = max(a.x1, b.x1) y1 = max(a.y1, b.y1)
x2 = min(a.x2, b.x2) y2 = min(a.y2, b.y2)
and its width and height are max(0, x2 - x1) and max(0, y2 - y1). The clamp
at zero is the whole problem: without it, two boxes that miss each other produce
a negative width times a negative height, which is a positive "overlap" and a
detector that scores best when it is most wrong.
The union is area_a + area_b - intersection. If it is zero, return 0.0.
IoU is the threshold that decides whether a detection counts as a hit, usually
at 0.5, and it is what non-maximum suppression uses to drop duplicate boxes.
def iou(box_a, box_b):
Input
iou([0, 0, 2, 2], [1, 1, 3, 3])
Output
0.14285714285714285
Reasoning
The boxes overlap in the square from (1, 1) to (2, 2), so the intersection is 1. Each box has area 4, so the union is 4 + 4 - 1 = 7, and IoU is 1/7.
0.0.0 when the union is 0.Check runs the visible cases against a fresh kernel. Submit runs the hidden ones too.
⇧↵run cell