forked from yuto3o/yolox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iou.py
149 lines (132 loc) · 6.29 KB
/
iou.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# -*- coding: utf-8 -*-
import tensorflow as tf
import math
EPS = 1e-8
def GIoU(y_pred_box, y_true_box):
"""
Calculate GIoU loss on anchor boxes
Reference Paper:
"Generalized Intersection over Union: A Metric and A Loss for Bounding Box Regression"
https://arxiv.org/abs/1902.09630
Parameters
----------
y_pred_box: tensor, shape=(batch, feat_w, feat_h, anchor_num, 4), x1y1x2y2
y_true_box: tensor, shape=(batch, feat_w, feat_h, anchor_num, 4), x1y1x2y2
Returns
-------
giou: tensor, shape=(batch, feat_w, feat_h, anchor_num, 1)
"""
y_pred_box_min = y_pred_box[..., :2]
y_pred_box_max = y_pred_box[..., 2:4]
y_pred_box_wh = y_pred_box_max - y_pred_box_min
y_true_box_min = y_true_box[..., :2]
y_true_box_max = y_true_box[..., 2:4]
y_true_box_wh = y_true_box_max - y_true_box_min
intersect_min = tf.maximum(y_true_box_min, y_pred_box_min)
intersect_max = tf.minimum(y_true_box_max, y_pred_box_max)
intersect_wh = tf.maximum(intersect_max - intersect_min, 0.)
intersect_area = intersect_wh[..., 0] * intersect_wh[..., 1]
y_pred_box_area = y_pred_box_wh[..., 0] * y_pred_box_wh[..., 1]
y_true_box_area = y_true_box_wh[..., 0] * y_true_box_wh[..., 1]
union_area = y_pred_box_area + y_true_box_area - intersect_area
# calculate IoU, add epsilon in denominator to avoid dividing by 0
iou = intersect_area / tf.maximum(union_area, EPS)
# get enclosed area
enclose_min = tf.minimum(y_true_box_min, y_pred_box_min)
enclose_max = tf.maximum(y_true_box_max, y_pred_box_max)
enclose_wh = tf.maximum(enclose_max - enclose_min, 0.0)
enclose_area = enclose_wh[..., 0] * enclose_wh[..., 1]
# calculate GIoU, add epsilon in denominator to avoid dividing by 0
giou = iou - (enclose_area - union_area) / tf.maximum(enclose_area, EPS)
return giou
def DIoU(y_pred_box, y_true_box):
"""
Calculate DIoU loss on anchor boxes
Reference Paper:
"Distance-IoU Loss: Faster and Better Learning for Bounding Box Regression"
https://arxiv.org/abs/1911.08287
Parameters
----------
y_pred_box: tensor, shape=(batch, feat_w, feat_h, anchor_num, 4), x1y1x2y2
y_true_box: tensor, shape=(batch, feat_w, feat_h, anchor_num, 4), x1y1x2y2
Returns
-------
diou: tensor, shape=(batch, feat_w, feat_h, anchor_num, 1)
"""
y_pred_box_min = y_pred_box[..., :2]
y_pred_box_max = y_pred_box[..., 2:4]
y_pred_box_wh = y_pred_box_max - y_pred_box_min
y_pred_box_center = (y_pred_box_min + y_pred_box_max) / 2.
y_true_box_min = y_true_box[..., :2]
y_true_box_max = y_true_box[..., 2:4]
y_true_box_wh = y_true_box_max - y_true_box_min
y_true_box_center = (y_true_box_min + y_true_box_max) / 2.
intersect_min = tf.maximum(y_pred_box_min, y_true_box_min)
intersect_max = tf.minimum(y_pred_box_max, y_true_box_max)
intersect_wh = tf.maximum(intersect_max - intersect_min, 0.)
intersect_area = intersect_wh[..., 0] * intersect_wh[..., 1]
y_true_box_area = y_true_box_wh[..., 0] * y_true_box_wh[..., 1]
y_pred_box_area = y_pred_box_wh[..., 0] * y_pred_box_wh[..., 1]
union_area = y_true_box_area + y_pred_box_area - intersect_area
# calculate IoU, add epsilon in denominator to avoid dividing by 0
iou = intersect_area / tf.maximum(union_area, EPS)
# box center distance
center_distance = tf.reduce_sum(tf.square(y_pred_box_center - y_true_box_center), axis=-1)
# get enclosed area
enclose_min = tf.minimum(y_pred_box_min, y_true_box_min)
enclose_max = tf.maximum(y_pred_box_max, y_true_box_max)
enclose_wh = tf.maximum(enclose_max - enclose_min, 0.0)
# get enclosed diagonal distance
enclose_diagonal = tf.reduce_sum(tf.square(enclose_wh), axis=-1)
# calculate DIoU, add epsilon in denominator to avoid dividing by 0
diou = iou - center_distance / tf.maximum(enclose_diagonal, EPS)
return diou
def CIoU(y_pred_box, y_true_box):
"""
Calculate DIoU loss on anchor boxes
Reference Paper:
"Distance-IoU Loss: Faster and Better Learning for Bounding Box Regression"
https://arxiv.org/abs/1911.08287
Parameters
----------
y_pred_box: tensor, shape=(batch, feat_w, feat_h, anchor_num, 4), x1y1x2y2
y_true_box: tensor, shape=(batch, feat_w, feat_h, anchor_num, 4), x1y1x2y2
Returns
-------
diou: tensor, shape=(batch, feat_w, feat_h, anchor_num, 1)
"""
y_pred_box_min = y_pred_box[..., :2]
y_pred_box_max = y_pred_box[..., 2:4]
y_pred_box_wh = y_pred_box_max - y_pred_box_min
y_pred_box_center = (y_pred_box_min + y_pred_box_max) / 2.
y_true_box_min = y_true_box[..., :2]
y_true_box_max = y_true_box[..., 2:4]
y_true_box_wh = y_true_box_max - y_true_box_min
y_true_box_center = (y_true_box_min + y_true_box_max) / 2.
intersect_min = tf.maximum(y_pred_box_min, y_true_box_min)
intersect_max = tf.minimum(y_pred_box_max, y_true_box_max)
intersect_wh = tf.maximum(intersect_max - intersect_min, 0.)
intersect_area = intersect_wh[..., 0] * intersect_wh[..., 1]
y_true_box_area = y_true_box_wh[..., 0] * y_true_box_wh[..., 1]
y_pred_box_area = y_pred_box_wh[..., 0] * y_pred_box_wh[..., 1]
union_area = y_true_box_area + y_pred_box_area - intersect_area
# calculate IoU, add epsilon in denominator to avoid dividing by 0
iou = intersect_area / tf.maximum(union_area, EPS)
# box center distance
center_distance = tf.reduce_sum(tf.square(y_pred_box_center - y_true_box_center), axis=-1)
# get enclosed area
enclose_min = tf.minimum(y_pred_box_min, y_true_box_min)
enclose_max = tf.maximum(y_pred_box_max, y_true_box_max)
enclose_wh = tf.maximum(enclose_max - enclose_min, 0.0)
# get enclosed diagonal distance
enclose_diagonal = tf.reduce_sum(tf.square(enclose_wh), axis=-1)
# calculate DIoU, add epsilon in denominator to avoid dividing by 0
diou = iou - center_distance / tf.maximum(enclose_diagonal, EPS)
# calculate param v and alpha to extend to CIoU
constant = 4. / (math.pi * math.pi)
v = constant * tf.square(
tf.math.atan2(y_true_box_wh[..., 0], tf.maximum(y_true_box_wh[..., 1], EPS)) - tf.math.atan2(
y_pred_box_wh[..., 0], tf.maximum(y_pred_box_wh[..., 1], EPS)))
alpha = v / tf.maximum(1.0 - iou + v, EPS)
ciou = diou - alpha * v
return ciou