Question 13
Consider the following code snippet, which generates predictions and ground truth for an object detection task:
# Predicted bounding boxes and confidence scorespredictions = [ {'bbox': [40, 40, 90, 90], 'score': 0.85}, {'bbox': [20, 20, 60, 60], 'score': 0.80}, {'bbox': [150, 150, 200, 200], 'score': 0.65}, {'bbox': [250, 250, 300, 300], 'score': 0.50}]
# Ground truth bounding boxesground_truths = [ {'bbox': [40, 40, 90, 90]}, {'bbox': [20, 20, 60, 60]}, {'bbox': [150, 150, 200, 200]}]
# Function to calculate IoU (Intersection over Union)def calculate_iou(box1, box2): x1 = max(box1[0], box2[0]) y1 = max(box1[1], box2[1]) x2 = min(box1[2], box2[2]) y2 = min(box1[3], box2[3]) intersection = max(0, x2 - x1) * max(0, y2 - y1) area1 = (box1[2] - box1[0]) * (box1[3] - box1[1]) area2 = (box2[2] - box2[0]) * (box2[3] - box2[1]) union = area1 + area2 - intersection return intersection / union if union > 0 else 0
# Match predictions with ground truths (IoU > 0.5 considered correct)threshold = 0.5true_positives = 0false_positives = 0false_negatives = len(ground_truths)
for pred in predictions: matched = False for gt in ground_truths: iou = calculate_iou(pred['bbox'], gt['bbox']) if iou >= threshold: true_positives += 1 false_negatives -= 1 matched = True break if not matched: false_positives += 1
precision = true_positives / (true_positives + false_positives)recall = true_positives / (true_positives + false_negatives)
print(f"Precision: {precision:.2f}")print(f"Recall: {recall:.2f}")Given the predictions and ground truths, what are the precision and recall values?
Precision: 0.75, Recall: 1.00
Precision: 0.75, Recall: 0.75
Precision: 1.00, Recall: 0.75
Precision: 0.60, Recall: 1.00