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)
| 43 | |
| 44 | |
| 45 | def 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 | |
| 68 | def generalized_box_iou_padded(boxes1, boxes2): |
| 69 | """ |
no test coverage detected