Compute pairwise intersection over union (IOU) of two sets of matched boxes. The box order must be (xmin, ymin, xmax, ymax). Similar to boxlist_iou, but computes only diagonal elements of the matrix Args: boxes1: (Boxes) bounding boxes, sized [N,4]. boxes2: (Boxes)
(boxes1: Boxes, boxes2: Boxes)
| 389 | |
| 390 | |
| 391 | def matched_boxlist_iou(boxes1: Boxes, boxes2: Boxes) -> torch.Tensor: |
| 392 | """ |
| 393 | Compute pairwise intersection over union (IOU) of two sets of matched |
| 394 | boxes. The box order must be (xmin, ymin, xmax, ymax). |
| 395 | Similar to boxlist_iou, but computes only diagonal elements of the matrix |
| 396 | |
| 397 | Args: |
| 398 | boxes1: (Boxes) bounding boxes, sized [N,4]. |
| 399 | boxes2: (Boxes) bounding boxes, sized [N,4]. |
| 400 | Returns: |
| 401 | Tensor: iou, sized [N]. |
| 402 | """ |
| 403 | assert len(boxes1) == len( |
| 404 | boxes2 |
| 405 | ), "boxlists should have the same" "number of entries, got {}, {}".format( |
| 406 | len(boxes1), len(boxes2) |
| 407 | ) |
| 408 | area1 = boxes1.area() # [N] |
| 409 | area2 = boxes2.area() # [N] |
| 410 | box1, box2 = boxes1.tensor, boxes2.tensor |
| 411 | lt = torch.max(box1[:, :2], box2[:, :2]) # [N,2] |
| 412 | rb = torch.min(box1[:, 2:], box2[:, 2:]) # [N,2] |
| 413 | wh = (rb - lt).clamp(min=0) # [N,2] |
| 414 | inter = wh[:, 0] * wh[:, 1] # [N] |
| 415 | iou = inter / (area1 + area2 - inter) # [N] |
| 416 | return iou |