forked from rykov8/ssd_keras
-
Notifications
You must be signed in to change notification settings - Fork 86
/
sl_training.py
234 lines (176 loc) · 11.8 KB
/
sl_training.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
"""SegLink training utils."""
import tensorflow as tf
import keras.backend as K
from utils.training import smooth_l1_loss, softmax_loss
from ssd_training import compute_metrics
class SegLinkLoss(object):
def __init__(self, lambda_segments=1.0, lambda_offsets=1.0, lambda_links=1.0, neg_pos_ratio=3.0, first_map_size=(64,64)):
self.lambda_segments = lambda_segments
self.lambda_offsets = lambda_offsets
self.lambda_links = lambda_links
self.neg_pos_ratio = neg_pos_ratio
self.first_map_offset = first_map_size[0] * first_map_size[1] # TODO get it from model or prior_util object
self.metric_names = [ 'loss',
'seg_conf_loss', 'seg_loc_loss', 'link_conf_loss',
'num_pos_seg', 'num_neg_seg',
'pos_seg_conf_loss', 'neg_seg_conf_loss', 'pos_link_conf_loss', 'neg_link_conf_loss',
'seg_precision', 'seg_recall', 'seg_accuracy', 'seg_fmeasure',
'link_precision', 'link_recall', 'link_accuracy', 'link_fmeasure',
]
def compute(self, y_true, y_pred):
# y.shape (batches, segments, 2 x segment_label + 5 x segment_offset + 16 x inter_layer_links_label + 8 x cross_layer_links_label)
batch_size = tf.shape(y_true)[0]
eps = K.epsilon()
# segment confidence loss
seg_conf_true = tf.reshape(y_true[:,:,0:2], [-1, 2])
seg_conf_pred = tf.reshape(y_pred[:,:,0:2], [-1, 2])
seg_conf_loss = softmax_loss(seg_conf_true, seg_conf_pred)
seg_class_pred = tf.argmax(seg_conf_pred, axis=1)
neg_seg_mask_float = seg_conf_true[:,0]
neg_seg_mask = tf.cast(neg_seg_mask_float, tf.bool)
pos_seg_mask = tf.logical_not(neg_seg_mask)
pos_seg_mask_float = tf.cast(pos_seg_mask, tf.float32)
num_seg = tf.cast(tf.shape(seg_conf_true)[0], tf.float32)
num_pos_seg = tf.reduce_sum(pos_seg_mask_float)
num_neg_seg = num_seg - num_pos_seg
pos_seg_conf_loss = tf.reduce_sum(seg_conf_loss * pos_seg_mask_float)
#false_pos_seg_mask = tf.logical_and(neg_seg_mask, tf.not_equal(seg_class_pred, 0))
#num_false_pos_seg = tf.reduce_sum(tf.cast(false_pos_seg_mask, tf.float32))
#num_neg_seg = tf.minimum(self.neg_pos_ratio * num_pos_seg, num_false_pos_seg)
#neg_seg_conf_loss = tf.boolean_mask(seg_conf_loss, false_pos_seg_mask)
num_neg_seg = tf.minimum(self.neg_pos_ratio * num_pos_seg, num_neg_seg)
neg_seg_conf_loss = tf.boolean_mask(seg_conf_loss, neg_seg_mask)
vals, idxs = tf.nn.top_k(neg_seg_conf_loss, k=tf.cast(num_neg_seg, tf.int32))
neg_seg_conf_loss = tf.reduce_sum(vals)
pos_seg_conf_loss = pos_seg_conf_loss / (num_pos_seg + eps)
neg_seg_conf_loss = neg_seg_conf_loss / (num_neg_seg + eps)
seg_conf_loss = pos_seg_conf_loss + neg_seg_conf_loss
# segment offset loss
seg_loc_true = tf.reshape(y_true[:,:,2:7], [-1, 5])
seg_loc_pred = tf.reshape(y_pred[:,:,2:7], [-1, 5])
seg_loc_loss = smooth_l1_loss(seg_loc_true, seg_loc_pred)
pos_seg_loc_loss = tf.reduce_sum(seg_loc_loss * pos_seg_mask_float)
seg_loc_loss = pos_seg_loc_loss / (num_pos_seg + eps)
# link confidence loss
inter_link_conf_true = y_true[:,:,7:23]
cross_link_conf_true = y_true[:,self.first_map_offset:,23:31]
link_conf_true = tf.concat([tf.reshape(inter_link_conf_true, [-1, 2]),
tf.reshape(cross_link_conf_true, [-1, 2])], 0)
inter_link_conf_pred = y_pred[:,:,7:23]
cross_link_conf_pred = y_pred[:,self.first_map_offset:,23:31]
link_conf_pred = tf.concat([tf.reshape(inter_link_conf_pred, [-1, 2]),
tf.reshape(cross_link_conf_pred, [-1, 2])], 0)
link_conf_loss = softmax_loss(link_conf_true, link_conf_pred)
link_class_pred = tf.argmax(link_conf_pred, axis=1)
neg_link_mask_float = link_conf_true[:,0]
neg_link_mask = tf.cast(neg_link_mask_float, tf.bool)
pos_link_mask = tf.logical_not(neg_link_mask)
pos_link_mask_float = tf.cast(pos_link_mask, tf.float32)
num_link = tf.cast(tf.shape(link_conf_true)[0], tf.float32)
num_pos_link = tf.reduce_sum(pos_link_mask_float)
num_neg_link = num_link - num_pos_link
pos_link_conf_loss = tf.reduce_sum(link_conf_loss * pos_link_mask_float)
#false_pos_link_mask = tf.logical_and(neg_link_mask, tf.not_equal(link_class_pred, 0))
#num_false_pos_link = tf.reduce_sum(tf.cast(false_pos_link_mask, tf.float32))
#num_neg_link = tf.minimum(self.neg_pos_ratio * num_pos_link, num_false_pos_link)
#neg_link_conf_loss = tf.boolean_mask(link_conf_loss, false_pos_link_mask)
num_neg_link = tf.minimum(self.neg_pos_ratio * num_pos_link, num_neg_link)
neg_link_conf_loss = tf.boolean_mask(link_conf_loss, neg_link_mask)
vals, idxs = tf.nn.top_k(neg_link_conf_loss, k=tf.cast(num_neg_link, tf.int32))
neg_link_conf_loss = tf.reduce_sum(vals)
pos_link_conf_loss = pos_link_conf_loss / (num_pos_link + eps)
neg_link_conf_loss = neg_link_conf_loss / (num_neg_link + eps)
link_conf_loss = pos_link_conf_loss + neg_link_conf_loss
# total loss
loss = self.lambda_segments * seg_conf_loss + self.lambda_offsets * seg_loc_loss + self.lambda_links * link_conf_loss
seg_conf = tf.reduce_max(seg_conf_pred, axis=1)
seg_class_true = tf.argmax(seg_conf_true, axis=1)
seg_class_pred = tf.argmax(seg_conf_pred, axis=1)
seg_precision, seg_recall, seg_accuracy, seg_fmeasure = compute_metrics(seg_class_true, seg_class_pred, seg_conf, top_k=100*batch_size)
link_conf = tf.reduce_max(link_conf_pred, axis=1)
link_class_true = tf.argmax(link_conf_true, axis=1)
link_class_pred = tf.argmax(link_conf_pred, axis=1)
link_precision, link_recall, link_accuracy, link_fmeasure = compute_metrics(link_class_true, link_class_pred, link_conf, top_k=100*batch_size)
return eval('{'+' '.join(['"'+n+'": '+n+',' for n in self.metric_names])+'}')
class SegLinkFocalLoss(object):
def __init__(self, lambda_segments=100.0, lambda_offsets=1.0, lambda_links=100.0,
gamma_segments=2, gamma_links=2, first_map_size=(64,64), reduced_focal_loss=False):
self.lambda_segments = lambda_segments
self.lambda_offsets = lambda_offsets
self.lambda_links = lambda_links
self.gamma_segments = gamma_segments
self.gamma_links = gamma_links
self.first_map_offset = first_map_size[0] * first_map_size[1] # TODO get it from model or prior_util object
self.first_map_size = first_map_size
self.reduced_focal_loss = reduced_focal_loss
self.metric_names = [ 'loss',
'seg_conf_loss', 'seg_loc_loss', 'link_conf_loss',
'inter_link_conf_loss', 'cross_link_conf_loss',
'inter_link_precision', 'inter_link_recall', 'inter_link_accuracy', 'inter_link_fmeasure',
'cross_link_precision', 'cross_link_recall', 'cross_link_accuracy', 'cross_link_fmeasure',
'num_pos_seg', 'num_neg_seg',
'seg_precision', 'seg_recall', 'seg_accuracy', 'seg_fmeasure',
'link_precision', 'link_recall', 'link_accuracy', 'link_fmeasure',
]
def compute(self, y_true, y_pred):
# y.shape (batches, segments, 2 x segment_label + 5 x segment_offset + 16 x inter_layer_links_label + 8 x cross_layer_links_label)
if self.reduced_focal_loss:
from utils.training import reduced_focal_loss as focal_loss
else:
from utils.training import focal_loss
batch_size = tf.shape(y_true)[0]
eps = K.epsilon()
# segment confidence loss
seg_conf_true = tf.reshape(y_true[:,:,0:2], [-1, 2])
seg_conf_pred = tf.reshape(y_pred[:,:,0:2], [-1, 2])
pos_seg_mask = seg_conf_true[:,1]
pos_seg_mask_float = tf.cast(pos_seg_mask, tf.float32)
num_seg = tf.cast(tf.shape(seg_conf_true)[0], tf.float32)
num_pos_seg = tf.reduce_sum(pos_seg_mask_float)
num_neg_seg = num_seg - num_pos_seg
seg_conf_loss = focal_loss(seg_conf_true, seg_conf_pred, self.gamma_segments)
seg_conf_loss = tf.reduce_sum(seg_conf_loss)
seg_conf_loss = seg_conf_loss / (tf.cast(num_seg, tf.float32) + eps)
# segment offset loss
seg_loc_true = tf.reshape(y_true[:,:,2:7], [-1, 5])
seg_loc_pred = tf.reshape(y_pred[:,:,2:7], [-1, 5])
seg_loc_loss = smooth_l1_loss(seg_loc_true, seg_loc_pred)
pos_seg_loc_loss = tf.reduce_sum(seg_loc_loss * pos_seg_mask_float)
seg_loc_loss = pos_seg_loc_loss / (num_pos_seg + eps)
# link confidence loss
inter_link_conf_true = tf.reshape(y_true[:,:,7:23], [-1, 2])
inter_link_conf_pred = tf.reshape(y_pred[:,:,7:23], [-1, 2])
inter_link_conf_loss = focal_loss(inter_link_conf_true, inter_link_conf_pred, self.gamma_links)
inter_link_conf_loss = tf.reduce_sum(inter_link_conf_loss)
num_inter_links = tf.cast(tf.shape(inter_link_conf_true)[0], tf.float32)
inter_link_conf_loss = inter_link_conf_loss / (num_inter_links + eps)
cross_link_conf_true = tf.reshape(y_true[:,self.first_map_offset:,23:31], [-1, 2])
cross_link_conf_pred = tf.reshape(y_pred[:,self.first_map_offset:,23:31], [-1, 2])
cross_link_conf_loss = focal_loss(cross_link_conf_true, cross_link_conf_pred, self.gamma_links)
cross_link_conf_loss = tf.reduce_sum(cross_link_conf_loss)
num_cross_links = tf.cast(tf.shape(cross_link_conf_true)[0], tf.float32)
cross_link_conf_loss = cross_link_conf_loss / (num_cross_links + eps)
link_conf_loss = inter_link_conf_loss + cross_link_conf_loss
# total loss
loss = self.lambda_segments * seg_conf_loss + self.lambda_offsets * seg_loc_loss + self.lambda_links * link_conf_loss
seg_conf = tf.reduce_max(seg_conf_pred, axis=1)
seg_class_true = tf.argmax(seg_conf_true, axis=1)
seg_class_pred = tf.argmax(seg_conf_pred, axis=1)
seg_precision, seg_recall, seg_accuracy, seg_fmeasure = compute_metrics(
seg_class_true, seg_class_pred, seg_conf, top_k=100*batch_size)
inter_link_conf = tf.reduce_max(inter_link_conf_pred, axis=1)
inter_link_class_true = tf.argmax(inter_link_conf_true, axis=1)
inter_link_class_pred = tf.argmax(inter_link_conf_pred, axis=1)
inter_link_precision, inter_link_recall, inter_link_accuracy, inter_link_fmeasure = compute_metrics(
inter_link_class_true, inter_link_class_pred, inter_link_conf, top_k=100*batch_size)
cross_link_conf = tf.reduce_max(cross_link_conf_pred, axis=1)
cross_link_class_true = tf.argmax(cross_link_conf_true, axis=1)
cross_link_class_pred = tf.argmax(cross_link_conf_pred, axis=1)
cross_link_precision, cross_link_recall, cross_link_accuracy, cross_link_fmeasure = compute_metrics(
cross_link_class_true, cross_link_class_pred, cross_link_conf, top_k=100*batch_size)
link_precision, link_recall, link_accuracy, link_fmeasure = compute_metrics(
tf.concat([inter_link_class_true, cross_link_class_true], 0),
tf.concat([inter_link_class_pred, cross_link_class_pred], 0),
tf.concat([inter_link_conf, cross_link_conf], 0),
top_k=100*batch_size)
return eval('{'+' '.join(['"'+n+'": '+n+',' for n in self.metric_names])+'}')