Given two bounding boxes, what does the Intersection over Union (IoU) metric measure?
Think about how much two boxes overlap compared to their combined size.
IoU measures how much two bounding boxes overlap by dividing the area of their intersection by the area of their union.
What is the output of the following Python code that calculates IoU for two bounding boxes?
def iou(box1, box2): x_left = max(box1[0], box2[0]) y_top = max(box1[1], box2[1]) x_right = min(box1[2], box2[2]) y_bottom = min(box1[3], box2[3]) if x_right < x_left or y_bottom < y_top: return 0.0 intersection_area = (x_right - x_left) * (y_bottom - y_top) box1_area = (box1[2] - box1[0]) * (box1[3] - box1[1]) box2_area = (box2[2] - box2[0]) * (box2[3] - box2[1]) iou_value = intersection_area / float(box1_area + box2_area - intersection_area) return round(iou_value, 2) box1 = [1, 1, 4, 4] box2 = [2, 2, 5, 5] print(iou(box1, box2))
Calculate intersection and union areas carefully.
The intersection area is 2x2=4, box1 area is 3x3=9, box2 area is 3x3=9, so IoU = 4 / (9+9-4) = 4/14 ≈ 0.2857 rounded to 0.29.
In object detection, which IoU threshold is commonly used to decide if a predicted bounding box is a true positive?
Think about a balance between strictness and leniency in overlap.
An IoU threshold of 0.5 is commonly used to consider a predicted box as a correct detection, balancing precision and recall.
What happens to precision and recall when the IoU threshold for true positive detection is increased from 0.5 to 0.75?
Higher IoU threshold means stricter matching.
Increasing IoU threshold makes it harder for predictions to count as true positives, so recall drops but precision improves because fewer false positives are counted.
What error does the following IoU calculation code produce when run, and why?
def iou(box1, box2): x_left = min(box1[0], box2[0]) y_top = min(box1[1], box2[1]) x_right = max(box1[2], box2[2]) y_bottom = max(box1[3], box2[3]) if x_right < x_left or y_bottom < y_top: return 0.0 intersection_area = (x_right - x_left) * (y_bottom - y_top) box1_area = (box1[2] - box1[0]) * (box1[3] - box1[1]) box2_area = (box2[2] - box2[0]) * (box2[3] - box2[1]) iou_value = intersection_area / float(box1_area + box2_area - intersection_area) return iou_value box1 = [1, 1, 4, 4] box2 = [2, 2, 5, 5] print(iou(box1, box2))
Check how intersection coordinates are calculated.
The code uses min for left and top and max for right and bottom, which calculates the union box, not the intersection. This causes intersection_area to be larger than actual intersection, leading to IoU > 1 which is invalid.