MCPcopy Create free account
hub / github.com/MotrixLab/AiOS / diou

Function diou

util/box_loss.py:59–106  ·  view source on GitHub ↗
(bboxes1, bboxes2)

Source from the content-addressed store, hash-verified

57
58
59def diou(bboxes1, bboxes2):
60 bboxes1 = torch.sigmoid(bboxes1)
61 bboxes2 = torch.sigmoid(bboxes2)
62 rows = bboxes1.shape[0]
63 cols = bboxes2.shape[0]
64 cious = torch.zeros((rows, cols))
65 if rows * cols == 0:
66 return cious
67 exchange = False
68 if bboxes1.shape[0] > bboxes2.shape[0]:
69 bboxes1, bboxes2 = bboxes2, bboxes1
70 cious = torch.zeros((cols, rows))
71 exchange = True
72 w1 = torch.exp(bboxes1[:, 2])
73 h1 = torch.exp(bboxes1[:, 3])
74 w2 = torch.exp(bboxes2[:, 2])
75 h2 = torch.exp(bboxes2[:, 3])
76 area1 = w1 * h1
77 area2 = w2 * h2
78 center_x1 = bboxes1[:, 0]
79 center_y1 = bboxes1[:, 1]
80 center_x2 = bboxes2[:, 0]
81 center_y2 = bboxes2[:, 1]
82
83 inter_l = torch.max(center_x1 - w1 / 2, center_x2 - w2 / 2)
84 inter_r = torch.min(center_x1 + w1 / 2, center_x2 + w2 / 2)
85 inter_t = torch.max(center_y1 - h1 / 2, center_y2 - h2 / 2)
86 inter_b = torch.min(center_y1 + h1 / 2, center_y2 + h2 / 2)
87 inter_area = torch.clamp((inter_r - inter_l), min=0) * torch.clamp(
88 (inter_b - inter_t), min=0)
89
90 c_l = torch.min(center_x1 - w1 / 2, center_x2 - w2 / 2)
91 c_r = torch.max(center_x1 + w1 / 2, center_x2 + w2 / 2)
92 c_t = torch.min(center_y1 - h1 / 2, center_y2 - h2 / 2)
93 c_b = torch.max(center_y1 + h1 / 2, center_y2 + h2 / 2)
94
95 inter_diag = (center_x2 - center_x1)**2 + (center_y2 - center_y1)**2
96 c_diag = torch.clamp((c_r - c_l), min=0)**2 + torch.clamp(
97 (c_b - c_t), min=0)**2
98
99 union = area1 + area2 - inter_area
100 u = (inter_diag) / c_diag
101 iou = inter_area / union
102 dious = iou - u
103 dious = torch.clamp(dious, min=-1.0, max=1.0)
104 if exchange:
105 dious = dious.T
106 return 1 - dious
107
108
109if __name__ == '__main__':

Callers 1

box_loss.pyFile · 0.85

Calls 1

maxMethod · 0.80

Tested by

no test coverage detected