-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathloss_functions.py
318 lines (275 loc) · 16.1 KB
/
loss_functions.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import keras.backend as K
import tensorflow as tf
def sequence_loss_with_params(params):
def double_frame_sequence_loss(y_true, y_pred):
# hyperparameters
delta_var = params.DELTA_VAR
delta_d = params.DELTA_D
nC = params.NUM_CLASSES
nD = params.EMBEDDING_DIM
# unpack ground truth contents
class_mask_gt = y_true[:, :, :, 0]
occ_class_mask_gt = y_true[:, :, :, 1]
prev_class_mask_gt = y_true[:, :, :, 2]
occ_prev_class_mask_gt = y_true[:, :, :, 3]
id_mask = y_true[:, :, :, 4]
occ_id_mask = y_true[:, :, :, 5]
prev_id_mask = y_true[:, :, :, 6]
occ_prev_id_mask = y_true[:, :, :, 7]
optical_flow_gt = y_true[:, :, :, 8:10]
# y_pred
class_mask_pred = y_pred[:, :, :, (nC * 0):(nC * 1)]
occ_class_mask_pred = y_pred[:, :, :, (nC * 1):(nC * 2)]
prev_class_mask_pred = y_pred[:, :, :, (nC * 2):(nC * 3)]
occ_prev_class_mask_pred = y_pred[:, :, :, (nC * 3):(nC * 4)]
instance_emb = y_pred[:, :, :, (nC * 4 + nD * 0):(nC * 4 + nD * 1)]
occ_instance_emb = y_pred[:, :, :, (nC * 4 + nD * 1):(nC * 4 + nD * 2)]
prev_instance_emb = y_pred[:, :, :, (nC * 4 + nD * 2):(nC * 4 + nD * 3)]
occ_prev_instance_emb = y_pred[:, :, :, (nC * 4 + nD * 3):(nC * 4 + nD * 4)]
optical_flow_pred = y_pred[:, :, :, -2:]
# flatten and combine
# --identity mask gt
id_mask_flat = K.flatten(id_mask)
occ_id_mask_flat = K.flatten(occ_id_mask)
prev_id_mask_flat = K.flatten(prev_id_mask)
occ_prev_id_mask_flat = K.flatten(occ_prev_id_mask)
combined_id_mask_flat = tf.concat(
(id_mask_flat,
occ_id_mask_flat,
prev_id_mask_flat,
occ_prev_id_mask_flat),
axis=0)
# --instance embedding pred
instance_emb_flat = tf.reshape(instance_emb, shape=(-1, nD))
occ_instance_emb_flat = tf.reshape(occ_instance_emb, shape=(-1, nD))
prev_instance_emb_flat = tf.reshape(prev_instance_emb, shape=(-1, nD))
occ_prev_instance_emb_flat = tf.reshape(occ_prev_instance_emb, shape=(-1, nD))
combined_instance_emb_flat = tf.concat(
(instance_emb_flat,
occ_instance_emb_flat,
prev_instance_emb_flat,
occ_prev_instance_emb_flat),
axis=0)
# --class mask gt
class_mask_gt_flat = K.flatten(class_mask_gt)
occ_class_mask_gt_flat = K.flatten(occ_class_mask_gt)
prev_class_mask_gt_flat = K.flatten(prev_class_mask_gt)
occ_prev_class_mask_gt_flat = K.flatten(occ_prev_class_mask_gt)
combined_class_mask_gt_flat = tf.concat(
(class_mask_gt_flat,
occ_class_mask_gt_flat,
prev_class_mask_gt_flat,
occ_prev_class_mask_gt_flat),
axis=0)
# --class mask pred
class_mask_pred_flat = tf.reshape(class_mask_pred, shape=(-1, nC))
occ_class_mask_pred_flat = tf.reshape(occ_class_mask_pred, shape=(-1, nC))
prev_class_mask_pred_flat = tf.reshape(prev_class_mask_pred, shape=(-1, nC))
occ_prev_class_mask_pred_flat = tf.reshape(occ_prev_class_mask_pred, shape=(-1, nC))
combined_class_mask_pred_flat = tf.concat(
(class_mask_pred_flat,
occ_class_mask_pred_flat,
prev_class_mask_pred_flat,
occ_prev_class_mask_pred_flat),
axis=0)
# get number of pixels and clusters (without background)
num_cluster = tf.reduce_max(combined_id_mask_flat)
num_cluster = tf.cast(num_cluster, tf.int32)
# cast masks into tf.int32 for one-hot encoding
combined_id_mask_flat = tf.cast(combined_id_mask_flat, tf.int32)
combined_class_mask_gt_flat = tf.cast(combined_class_mask_gt_flat, tf.int32)
# one-hot encoding
combined_id_mask_flat -= 1
combined_id_mask_flat_one_hot = tf.one_hot(combined_id_mask_flat, num_cluster)
conbined_class_mask_gt_flat_one_hot = tf.one_hot(combined_class_mask_gt_flat, nC)
# ignore background pixels
non_background_idx = tf.greater(combined_id_mask_flat, -1)
combined_instance_emb_flat = tf.boolean_mask(combined_instance_emb_flat, non_background_idx)
combined_id_mask_flat = tf.boolean_mask(combined_id_mask_flat, non_background_idx)
combined_id_mask_flat_one_hot = tf.boolean_mask(combined_id_mask_flat_one_hot, non_background_idx)
combined_class_mask_gt_flat = tf.boolean_mask(combined_class_mask_gt_flat, non_background_idx)
# center count
combined_id_mask_flat_one_hot = tf.cast(combined_id_mask_flat_one_hot, tf.float32)
center_count = tf.reduce_sum(combined_id_mask_flat_one_hot, axis=0)
# add a small number to avoid division by zero
# variance term
embedding_sum_by_instance = tf.matmul(
tf.transpose(combined_instance_emb_flat), combined_id_mask_flat_one_hot)
centers = tf.math.divide_no_nan(embedding_sum_by_instance, center_count)
gathered_center = tf.gather(centers, combined_id_mask_flat, axis=1)
gathered_center_count = tf.gather(center_count, combined_id_mask_flat)
combined_emb_t = tf.transpose(combined_instance_emb_flat)
var_dist = tf.norm(combined_emb_t - gathered_center, ord=1, axis=0) - delta_var
# changed from soft hinge loss to hard cutoff
var_dist_pos = tf.square(tf.maximum(var_dist, 0))
var_dist_by_instance = tf.math.divide_no_nan(var_dist_pos, gathered_center_count)
num_cluster = tf.cast(num_cluster, tf.float32)
variance_term = tf.math.divide_no_nan(
tf.reduce_sum(var_dist_by_instance),
tf.cast(num_cluster, tf.float32))
# get instance to class mapping
class_mask_gt = tf.expand_dims(class_mask_gt, axis=-1)
# multiply classification with one hot flat identity mask
combined_class_mask_gt_flat = tf.cast(combined_class_mask_gt_flat, tf.float32)
combined_class_mask_gt_flat = tf.expand_dims(combined_class_mask_gt_flat, 1)
filtered_class = tf.multiply(combined_id_mask_flat_one_hot, combined_class_mask_gt_flat)
# shrink to a 1 by num_of_cluster vector to map instance to class;
# by reduce_max, any class other than 0 (background) stands out
instance_to_class = tf.reduce_max(filtered_class, axis = [0])
def distance_true_fn(num_cluster_by_class, centers_by_class):
centers_row_buffer = tf.ones((nD, num_cluster_by_class, num_cluster_by_class))
centers_by_class = tf.expand_dims(centers_by_class, axis=2)
centers_row = tf.multiply(centers_row_buffer, centers_by_class)
centers_col = tf.transpose(centers_row, perm=[0, 2, 1])
dist_matrix = centers_row - centers_col
idx2 = tf.ones((num_cluster_by_class, num_cluster_by_class))
diag = tf.ones((1, num_cluster_by_class))
diag = tf.reshape(diag, [-1])
idx2 = idx2 - tf.diag(diag)
idx2 = tf.cast(idx2, tf.bool)
idx2 = K.flatten(idx2)
dist_matrix = tf.reshape(dist_matrix, [nD, -1])
dist_matrix = tf.transpose(dist_matrix)
sampled_dist = tf.boolean_mask(dist_matrix, idx2)
distance_term = tf.square(tf.maximum(
2 * delta_d - tf.norm(sampled_dist, ord=1, axis=1), 0))
total_cluster_pair = num_cluster_by_class * (num_cluster_by_class - 1) + 1
total_cluster_pair = tf.cast(total_cluster_pair, tf.float32)
distance_term = tf.math.divide_no_nan(tf.reduce_sum(distance_term), total_cluster_pair)
return distance_term
def distance_false_fn():
return 0.0
distance_term_total = 0.0
# center distance term
for i in range(nC-1):
class_idx = tf.equal(instance_to_class, i+1)
centers_transpose = tf.transpose(centers)
centers_by_class_transpose = tf.boolean_mask(centers_transpose, class_idx)
centers_by_class = tf.transpose(centers_by_class_transpose)
num_cluster_by_class = tf.reduce_sum(tf.cast(class_idx, tf.float32))
num_cluster_by_class = tf.cast(num_cluster_by_class, tf.int32)
distance_term_subtotal = tf.cond(num_cluster_by_class > 0,
lambda: distance_true_fn(num_cluster_by_class, centers_by_class),
lambda: distance_false_fn())
distance_term_total += distance_term_subtotal
# regularization term
regularization_term = tf.reduce_mean(tf.norm(tf.squeeze(centers), ord=1, axis=0))
# sum up terms
instance_emb_sequence_loss = variance_term + distance_term_total + 0.01 * regularization_term
semseg_loss = K.mean(K.categorical_crossentropy(
tf.cast(conbined_class_mask_gt_flat_one_hot, tf.float32),
tf.cast(combined_class_mask_pred_flat, tf.float32)))
# masked mse for optical loss
flow_mask = tf.greater(prev_class_mask_gt, 0)
flow_mask = tf.cast(flow_mask, tf.float32)
flow_mask = tf.expand_dims(flow_mask, axis = -1)
masked_optical_flow_pred = tf.math.multiply(optical_flow_pred, flow_mask)
optical_flow_loss = tf.reduce_mean(tf.square(masked_optical_flow_pred - optical_flow_gt))
loss = instance_emb_sequence_loss + semseg_loss + params.OPTICAL_FLOW_WEIGHT * optical_flow_loss
loss = tf.reshape(loss, [-1])
return loss
return double_frame_sequence_loss
def single_frame_loss_with_params(params):
def multi_class_instance_embedding_loss(y_true, y_pred):
# hyperparameters
batch_size = params.BATCH_SIZE
delta_var = params.DELTA_VAR
delta_d = params.DELTA_D
nC = params.NUM_CLASSES
nD = params.EMBEDDING_DIM
total_loss = 0
# unpack ground truth contents
for j in range(batch_size):
class_mask = y_true[j:j+1, :, :, 0]
instance_mask = y_true[j:j+1, :, :, 1]
# y_pred
class_pred = y_pred[j:j+1, :, :, :nC]
instance_emb = y_pred[j:j+1, :, :, (nC):(nC + nD)]
# get number of pixels and clusters (without background)
num_cluster = tf.reduce_max(instance_mask)
num_cluster = tf.cast(num_cluster, tf.int32)
# one-hot encoding for mask
instance_mask = tf.cast(instance_mask, tf.int32)
instance_mask = instance_mask - 1
instance_mask_one_hot = tf.one_hot(instance_mask, num_cluster)
class_mask = tf.cast(class_mask, tf.int32)
class_mask_one_hot = tf.one_hot(class_mask, nC)
# flatten
instance_emb_flat = tf.reshape(instance_emb, shape=(-1, nD))
instance_mask_one_hot_flat = tf.reshape(instance_mask_one_hot, shape=(-1, num_cluster))
instance_mask_flat = K.flatten(instance_mask)
class_mask_flat = tf.reshape(class_mask_one_hot, shape=(-1, nC))
class_pred_flat = tf.reshape(class_pred, shape=(-1, nC))
# ignore background pixels
non_background_idx = tf.greater(instance_mask_flat, -1)
instance_emb_flat = tf.boolean_mask(instance_emb_flat, non_background_idx)
instance_mask_flat = tf.boolean_mask(instance_mask_flat, non_background_idx)
instance_mask_one_hot_flat = tf.boolean_mask(instance_mask_one_hot_flat, non_background_idx)
# center count
center_count = tf.reduce_sum(tf.cast(instance_mask_one_hot_flat, dtype=tf.float32), axis=0)
# variance term
embedding_sum_by_instance = tf.matmul(
tf.transpose(instance_emb_flat), tf.cast(instance_mask_one_hot_flat, dtype=tf.float32))
centers = tf.math.divide_no_nan(embedding_sum_by_instance, center_count)
gathered_center = tf.gather(centers, instance_mask_flat, axis=1)
gathered_center_count = tf.gather(center_count, instance_mask_flat)
combined_emb_t = tf.transpose(instance_emb_flat)
var_dist = tf.norm(combined_emb_t - gathered_center, ord=1, axis=0) - delta_var
# changed from soft hinge loss to hard cutoff
var_dist_pos = tf.square(tf.maximum(var_dist, 0))
var_dist_by_instance = tf.math.divide_no_nan(var_dist_pos, gathered_center_count)
variance_term = tf.math.divide_no_nan(
tf.reduce_sum(var_dist_by_instance),
tf.cast(num_cluster, tf.float32))
# get instance to class mapping
class_mask = tf.expand_dims(class_mask, axis=-1)
filtered_class = tf.multiply(tf.cast(instance_mask_one_hot, tf.float32), tf.cast(class_mask, tf.float32))
instance_to_class = tf.reduce_max(filtered_class, axis = [0, 1, 2])
def true_fn(num_cluster_by_class, centers_by_class):
centers_row_buffer = tf.ones((nD, num_cluster_by_class, num_cluster_by_class))
centers_by_class = tf.expand_dims(centers_by_class, axis=2)
centers_row = tf.multiply(centers_row_buffer, centers_by_class)
centers_col = tf.transpose(centers_row, perm=[0, 2, 1])
dist_matrix = centers_row - centers_col
idx2 = tf.ones((num_cluster_by_class, num_cluster_by_class))
diag = tf.ones((1, num_cluster_by_class))
diag = tf.reshape(diag, [-1])
idx2 = idx2 - tf.diag(diag)
idx2 = tf.cast(idx2, tf.bool)
idx2 = K.flatten(idx2)
dist_matrix = tf.reshape(dist_matrix, [nD, -1])
dist_matrix = tf.transpose(dist_matrix)
sampled_dist = tf.boolean_mask(dist_matrix, idx2)
distance_term = tf.square(tf.maximum(
2 * delta_d - tf.norm(sampled_dist, ord=1, axis=1), 0))
total_cluster_pair = num_cluster_by_class * (num_cluster_by_class - 1) + 1
total_cluster_pair = tf.cast(total_cluster_pair, tf.float32)
distance_term = tf.math.divide_no_nan(tf.reduce_sum(distance_term), total_cluster_pair)
return distance_term
def false_fn():
return 0.0
distance_term_total = 0.0
# center distance term
for i in range(nC-1):
class_idx = tf.equal(instance_to_class, i+1)
centers_transpose = tf.transpose(centers)
centers_by_class_transpose = tf.boolean_mask(centers_transpose, class_idx)
centers_by_class = tf.transpose(centers_by_class_transpose)
num_cluster_by_class = tf.reduce_sum(tf.cast(class_idx, tf.float32))
num_cluster_by_class = tf.cast(num_cluster_by_class, tf.int32)
distance_term_subtotal = tf.cond(num_cluster_by_class > 0,
lambda: true_fn(num_cluster_by_class, centers_by_class),
lambda: false_fn())
distance_term_total += distance_term_subtotal
# regularization term
regularization_term = tf.reduce_mean(tf.norm(tf.squeeze(centers), ord=1, axis=0))
# sum up terms
instance_emb_loss = variance_term + distance_term_total + 0.01 * regularization_term
semseg_loss = K.mean(K.categorical_crossentropy(
tf.cast(class_mask_flat, tf.float32), tf.cast(class_pred_flat, tf.float32)))
loss = instance_emb_loss + semseg_loss
loss = tf.reshape(loss, [-1])
total_loss += loss
total_loss = total_loss / batch_size
return total_loss
return multi_class_instance_embedding_loss