Given two lists of boxes of size N and M, compute the IoU (intersection over union) between __all__ N x M pairs of boxes. The box order must be (xmin, ymin, xmax, ymax). Args: boxes1,boxes2 (Boxes): two `Boxes`. Contains N & M boxes, respectively. Returns: T
(boxes1: Boxes, boxes2: Boxes)
| 344 | # implementation from https://github.com/kuangliu/torchcv/blob/master/torchcv/utils/box.py |
| 345 | # with slight modifications |
| 346 | def pairwise_iou(boxes1: Boxes, boxes2: Boxes) -> torch.Tensor: |
| 347 | """ |
| 348 | Given two lists of boxes of size N and M, |
| 349 | compute the IoU (intersection over union) |
| 350 | between __all__ N x M pairs of boxes. |
| 351 | The box order must be (xmin, ymin, xmax, ymax). |
| 352 | Args: |
| 353 | boxes1,boxes2 (Boxes): two `Boxes`. Contains N & M boxes, respectively. |
| 354 | |
| 355 | Returns: |
| 356 | Tensor: IoU, sized [N,M]. |
| 357 | """ |
| 358 | area1 = boxes1.area() # [N] |
| 359 | area2 = boxes2.area() # [M] |
| 360 | inter = pairwise_intersection(boxes1, boxes2) |
| 361 | |
| 362 | # handle empty boxes |
| 363 | iou = torch.where( |
| 364 | inter > 0, |
| 365 | inter / (area1[:, None] + area2 - inter), |
| 366 | torch.zeros(1, dtype=inter.dtype, device=inter.device), |
| 367 | ) |
| 368 | return iou |
| 369 | |
| 370 | |
| 371 | def pairwise_ioa(boxes1: Boxes, boxes2: Boxes) -> torch.Tensor: |