I am using this intersection over union code to determine IOU from my predictions and targets:
def intersection_over_union(boxes_preds, boxes_labels):
"""
Calculates intersection over union
Parameters:
boxes_preds (tensor): Predictions of Bounding Boxes (BATCH_SIZE, 4)
boxes_labels (tensor): Correct labels of Bounding Boxes (BATCH_SIZE, 4)
box_format (str): midpoint/corners, if boxes (x,y,w,h) or (x1,y1,x2,y2)
Returns:
tensor: Intersection over union for all examples
"""
box1_x1 = boxes_preds[..., 0:1]
box1_y1 = boxes_preds[..., 1:2]
box1_x2 = boxes_preds[..., 2:3]
box1_y2 = boxes_preds[..., 3:4] # (N, 1)
box2_x1 = boxes_labels[..., 0:1]
box2_y1 = boxes_labels[..., 1:2]
box2_x2 = boxes_labels[..., 2:3]
box2_y2 = boxes_labels[..., 3:4]
x1 = torch.max(box1_x1, box2_x1)
y1 = torch.max(box1_y1, box2_y1)
x2 = torch.min(box1_x2, box2_x2)
y2 = torch.min(box1_y2, box2_y2)
# .clamp(0) is for the case when they do not intersect
intersection = (x2 - x1).clamp(0) * (y2 - y1).clamp(0)
box1_area = abs((box1_x2 - box1_x1) * (box1_y2 - box1_y1))
box2_area = abs((box2_x2 - box2_x1) * (box2_y2 - box2_y1))
return intersection / (box1_area + box2_area - intersection + 1e-6)
My inputs looks like this:
My target bounding boxes look like this:
print(targets[0]['boxes'])
tensor([[217., 481., 249., 511.],
[435., 191., 467., 223.],
[471., 86., 503., 118.]])
And my prediction bounding boxes look like this:
predictions['boxes']
tensor([[ 29.7859, 354.9666, 63.0900, 387.6363],
[469.1072, 85.6840, 503.1974, 119.7137],
[ 89.3957, 314.1584, 123.9789, 347.1621],
[432.2971, 188.4454, 468.4712, 227.3808],
[214.5407, 482.0136, 248.7030, 512.0000],
[329.1979, 340.8802, 366.3720, 375.8683],
[298.5089, 99.0098, 334.4280, 129.4205],
[ 0.0000, 347.7724, 17.3409, 384.5709],
[485.4312, 181.3882, 512.0000, 213.2009],
[144.5959, 356.5197, 183.4489, 387.4958]])
However, when I apply the IOU function:
iou = intersection_over_union(predictions['boxes'], targets[0]['boxes'])
I get this error:
RuntimeError: The size of tensor a (10) must match the size of tensor b (3) at non-singleton dimension 0
I'm not sure how I can fix the function as I'm guessing this means I have more predictions than targets...