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)
| 58 | |
| 59 | |
| 60 | def generalized_box_iou(boxes1, boxes2): |
| 61 | """ |
| 62 | Generalized IoU from https://giou.stanford.edu/ |
| 63 | |
| 64 | The boxes should be in [x0, y0, x1, y1] format |
| 65 | |
| 66 | Returns a [N, M] pairwise matrix, where N = len(boxes1) |
| 67 | and M = len(boxes2) |
| 68 | """ |
| 69 | # degenerate boxes gives inf / nan results |
| 70 | # so do an early check |
| 71 | assert (boxes1[:, 2:] >= boxes1[:, :2]).all() |
| 72 | assert (boxes2[:, 2:] >= boxes2[:, :2]).all() |
| 73 | iou, union = box_iou(boxes1, boxes2) |
| 74 | |
| 75 | lt = torch.min(boxes1[:, None, :2], boxes2[:, :2]) |
| 76 | rb = torch.max(boxes1[:, None, 2:], boxes2[:, 2:]) |
| 77 | |
| 78 | wh = (rb - lt).clamp(min=0) # [N,M,2] |
| 79 | area = wh[:, :, 0] * wh[:, :, 1] |
| 80 | |
| 81 | return iou - ((area - union) + 1e-6) / (area + 1e-6) |
| 82 | |
| 83 | |
| 84 | def masks_to_boxes(masks): |