MCPcopy Create free account
hub / github.com/UX-Decoder/Semantic-SAM / generalized_box_iou

Function generalized_box_iou

semantic_sam/utils/box_ops.py:45–66  ·  view source on GitHub ↗

Generalized IoU from https://giou.stanford.edu/ The boxes should be in [x0, y0, x1, y1] format Returns a [N, M] pairwise matrix, where N = len(boxes1) and M = len(boxes2)

(boxes1, boxes2)

Source from the content-addressed store, hash-verified

43
44
45def generalized_box_iou(boxes1, boxes2):
46 """
47 Generalized IoU from https://giou.stanford.edu/
48
49 The boxes should be in [x0, y0, x1, y1] format
50
51 Returns a [N, M] pairwise matrix, where N = len(boxes1)
52 and M = len(boxes2)
53 """
54 # degenerate boxes gives inf / nan results
55 # so do an early check
56 assert (boxes1[:, 2:] >= boxes1[:, :2]).all()
57 assert (boxes2[:, 2:] >= boxes2[:, :2]).all()
58 iou, union = box_iou(boxes1, boxes2)
59
60 lt = torch.min(boxes1[:, None, :2], boxes2[:, :2])
61 rb = torch.max(boxes1[:, None, 2:], boxes2[:, 2:])
62
63 wh = (rb - lt).clamp(min=0) # [N,M,2]
64 area = wh[:, :, 0] * wh[:, :, 1]
65
66 return iou - (area - union) / (area+1e-6)
67
68def generalized_box_iou_padded(boxes1, boxes2):
69 """

Callers 2

Calls 1

box_iouFunction · 0.85

Tested by

no test coverage detected