Compute pairwise intersection over union (IOU) of two sets of matched boxes that have the same number of boxes. Similar to :func:`pairwise_iou`, but computes only diagonal elements of the matrix. Args: boxes1 (Boxes): bounding boxes, sized [N,4]. boxes2 (Boxes): sam
(boxes1: Boxes, boxes2: Boxes)
| 396 | |
| 397 | |
| 398 | def matched_pairwise_iou(boxes1: Boxes, boxes2: Boxes) -> torch.Tensor: |
| 399 | """ |
| 400 | Compute pairwise intersection over union (IOU) of two sets of matched |
| 401 | boxes that have the same number of boxes. |
| 402 | Similar to :func:`pairwise_iou`, but computes only diagonal elements of the matrix. |
| 403 | |
| 404 | Args: |
| 405 | boxes1 (Boxes): bounding boxes, sized [N,4]. |
| 406 | boxes2 (Boxes): same length as boxes1 |
| 407 | Returns: |
| 408 | Tensor: iou, sized [N]. |
| 409 | """ |
| 410 | assert len(boxes1) == len( |
| 411 | boxes2 |
| 412 | ), "boxlists should have the same" "number of entries, got {}, {}".format( |
| 413 | len(boxes1), len(boxes2) |
| 414 | ) |
| 415 | area1 = boxes1.area() # [N] |
| 416 | area2 = boxes2.area() # [N] |
| 417 | box1, box2 = boxes1.tensor, boxes2.tensor |
| 418 | lt = torch.max(box1[:, :2], box2[:, :2]) # [N,2] |
| 419 | rb = torch.min(box1[:, 2:], box2[:, 2:]) # [N,2] |
| 420 | wh = (rb - lt).clamp(min=0) # [N,2] |
| 421 | inter = wh[:, 0] * wh[:, 1] # [N] |
| 422 | iou = inter / (area1 + area2 - inter) # [N] |
| 423 | return iou |