We resize both tensors to [A,B,2] without new malloc: [A,2] -> [A,1,2] -> [A,B,2] [B,2] -> [1,B,2] -> [A,B,2] Then we compute the area of intersect between box_a and box_b. Args: box_a: (tensor) bounding boxes, Shape: [A,4]. box_b: (tensor) bounding boxes, Shape: [B,4].
(box_a, box_b)
| 22 | |
| 23 | |
| 24 | def intersect(box_a, box_b): |
| 25 | """ We resize both tensors to [A,B,2] without new malloc: |
| 26 | [A,2] -> [A,1,2] -> [A,B,2] |
| 27 | [B,2] -> [1,B,2] -> [A,B,2] |
| 28 | Then we compute the area of intersect between box_a and box_b. |
| 29 | Args: |
| 30 | box_a: (tensor) bounding boxes, Shape: [A,4]. |
| 31 | box_b: (tensor) bounding boxes, Shape: [B,4]. |
| 32 | Return: |
| 33 | (tensor) intersection area, Shape: [A,B]. |
| 34 | """ |
| 35 | A = box_a.size(0) |
| 36 | B = box_b.size(0) |
| 37 | max_xy = torch.min(box_a[:, 2:].unsqueeze(1).expand(A, B, 2), |
| 38 | box_b[:, 2:].unsqueeze(0).expand(A, B, 2)) |
| 39 | min_xy = torch.max(box_a[:, :2].unsqueeze(1).expand(A, B, 2), |
| 40 | box_b[:, :2].unsqueeze(0).expand(A, B, 2)) |
| 41 | inter = torch.clamp((max_xy - min_xy), min=0) |
| 42 | return inter[:, :, 0] * inter[:, :, 1] |
| 43 | |
| 44 | |
| 45 | def jaccard(box_a, box_b, iscrowd=False): |