MCPcopy Create free account
hub / github.com/explainingai-code/FasterRCNN-PyTorch / get_iou

Function get_iou

model/faster_rcnn.py:9–31  ·  view source on GitHub ↗

r""" IOU between two sets of boxes :param boxes1: (Tensor of shape N x 4) :param boxes2: (Tensor of shape M x 4) :return: IOU matrix of shape N x M

(boxes1, boxes2)

Source from the content-addressed store, hash-verified

7
8
9def get_iou(boxes1, boxes2):
10 r"""
11 IOU between two sets of boxes
12 :param boxes1: (Tensor of shape N x 4)
13 :param boxes2: (Tensor of shape M x 4)
14 :return: IOU matrix of shape N x M
15 """
16 # Area of boxes (x2-x1)*(y2-y1)
17 area1 = (boxes1[:, 2] - boxes1[:, 0]) * (boxes1[:, 3] - boxes1[:, 1]) # (N,)
18 area2 = (boxes2[:, 2] - boxes2[:, 0]) * (boxes2[:, 3] - boxes2[:, 1]) # (M,)
19
20 # Get top left x1,y1 coordinate
21 x_left = torch.max(boxes1[:, None, 0], boxes2[:, 0]) # (N, M)
22 y_top = torch.max(boxes1[:, None, 1], boxes2[:, 1]) # (N, M)
23
24 # Get bottom right x2,y2 coordinate
25 x_right = torch.min(boxes1[:, None, 2], boxes2[:, 2]) # (N, M)
26 y_bottom = torch.min(boxes1[:, None, 3], boxes2[:, 3]) # (N, M)
27
28 intersection_area = (x_right - x_left).clamp(min=0) * (y_bottom - y_top).clamp(min=0) # (N, M)
29 union = area1[:, None] + area2 - intersection_area # (N, M)
30 iou = intersection_area / union # (N, M)
31 return iou
32
33
34def boxes_to_transformation_targets(ground_truth_boxes, anchors_or_proposals):

Callers 2

Calls

no outgoing calls

Tested by

no test coverage detected