Question 11
Consider the following code snippet that calculates the Intersection over Union (IoU) for two bounding boxes:
import torch
def calculate_iou(box1, box2): # Calculate intersection x1 = torch.max(box1[0], box2[0]) y1 = torch.max(box1[1], box2[1]) x2 = torch.min(box1[2], box2[2]) y2 = torch.min(box1[3], box2[3]) intersection = torch.clamp(x2 - x1, min=0) * torch.clamp(y2 - y1, min=0)
# Calculate union area1 = (box1[2] - box1[0]) * (box1[3] - box1[1]) area2 = (box2[2] - box2[0]) * (box2[3] - box2[1]) union = area1 + area2 - intersection
# Compute IoU iou = intersection / union if union > 0 else 0 return iou
# Bounding boxes: [x1, y1, x2, y2]box1 = torch.tensor([0, 0, 2, 2])box2 = torch.tensor([1, 1, 3, 3])
print(calculate_iou(box1, box2))What will the function output for the given bounding boxes?
0.142
0.250
0.333
0.500