Spaces:
Build error
Build error
Upload box_ops.py
Browse files- box_ops.py +88 -0
box_ops.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
|
2 |
+
"""
|
3 |
+
Utilities for bounding box manipulation and GIoU.
|
4 |
+
"""
|
5 |
+
import torch
|
6 |
+
from torchvision.ops.boxes import box_area
|
7 |
+
|
8 |
+
|
9 |
+
def box_cxcywh_to_xyxy(x):
|
10 |
+
x_c, y_c, w, h = x.unbind(-1)
|
11 |
+
b = [(x_c - 0.5 * w), (y_c - 0.5 * h),
|
12 |
+
(x_c + 0.5 * w), (y_c + 0.5 * h)]
|
13 |
+
return torch.stack(b, dim=-1)
|
14 |
+
|
15 |
+
|
16 |
+
def box_xyxy_to_cxcywh(x):
|
17 |
+
x0, y0, x1, y1 = x.unbind(-1)
|
18 |
+
b = [(x0 + x1) / 2, (y0 + y1) / 2,
|
19 |
+
(x1 - x0), (y1 - y0)]
|
20 |
+
return torch.stack(b, dim=-1)
|
21 |
+
|
22 |
+
|
23 |
+
# modified from torchvision to also return the union
|
24 |
+
def box_iou(boxes1, boxes2):
|
25 |
+
area1 = box_area(boxes1)
|
26 |
+
area2 = box_area(boxes2)
|
27 |
+
|
28 |
+
lt = torch.max(boxes1[:, None, :2], boxes2[:, :2]) # [N,M,2]
|
29 |
+
rb = torch.min(boxes1[:, None, 2:], boxes2[:, 2:]) # [N,M,2]
|
30 |
+
|
31 |
+
wh = (rb - lt).clamp(min=0) # [N,M,2]
|
32 |
+
inter = wh[:, :, 0] * wh[:, :, 1] # [N,M]
|
33 |
+
|
34 |
+
union = area1[:, None] + area2 - inter
|
35 |
+
|
36 |
+
iou = inter / union
|
37 |
+
return iou, union
|
38 |
+
|
39 |
+
|
40 |
+
def generalized_box_iou(boxes1, boxes2):
|
41 |
+
"""
|
42 |
+
Generalized IoU from https://giou.stanford.edu/
|
43 |
+
|
44 |
+
The boxes should be in [x0, y0, x1, y1] format
|
45 |
+
|
46 |
+
Returns a [N, M] pairwise matrix, where N = len(boxes1)
|
47 |
+
and M = len(boxes2)
|
48 |
+
"""
|
49 |
+
# degenerate boxes gives inf / nan results
|
50 |
+
# so do an early check
|
51 |
+
assert (boxes1[:, 2:] >= boxes1[:, :2]).all()
|
52 |
+
assert (boxes2[:, 2:] >= boxes2[:, :2]).all()
|
53 |
+
iou, union = box_iou(boxes1, boxes2)
|
54 |
+
|
55 |
+
lt = torch.min(boxes1[:, None, :2], boxes2[:, :2])
|
56 |
+
rb = torch.max(boxes1[:, None, 2:], boxes2[:, 2:])
|
57 |
+
|
58 |
+
wh = (rb - lt).clamp(min=0) # [N,M,2]
|
59 |
+
area = wh[:, :, 0] * wh[:, :, 1]
|
60 |
+
|
61 |
+
return iou - (area - union) / area
|
62 |
+
|
63 |
+
|
64 |
+
def masks_to_boxes(masks):
|
65 |
+
"""Compute the bounding boxes around the provided masks
|
66 |
+
|
67 |
+
The masks should be in format [N, H, W] where N is the number of masks, (H, W) are the spatial dimensions.
|
68 |
+
|
69 |
+
Returns a [N, 4] tensors, with the boxes in xyxy format
|
70 |
+
"""
|
71 |
+
if masks.numel() == 0:
|
72 |
+
return torch.zeros((0, 4), device=masks.device)
|
73 |
+
|
74 |
+
h, w = masks.shape[-2:]
|
75 |
+
|
76 |
+
y = torch.arange(0, h, dtype=torch.float)
|
77 |
+
x = torch.arange(0, w, dtype=torch.float)
|
78 |
+
y, x = torch.meshgrid(y, x)
|
79 |
+
|
80 |
+
x_mask = (masks * x.unsqueeze(0))
|
81 |
+
x_max = x_mask.flatten(1).max(-1)[0]
|
82 |
+
x_min = x_mask.masked_fill(~(masks.bool()), 1e8).flatten(1).min(-1)[0]
|
83 |
+
|
84 |
+
y_mask = (masks * y.unsqueeze(0))
|
85 |
+
y_max = y_mask.flatten(1).max(-1)[0]
|
86 |
+
y_min = y_mask.masked_fill(~(masks.bool()), 1e8).flatten(1).min(-1)[0]
|
87 |
+
|
88 |
+
return torch.stack([x_min, y_min, x_max, y_max], 1)
|