forked from squared9/Crime_Detection
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloss.py
More file actions
458 lines (372 loc) · 24.4 KB
/
loss.py
File metadata and controls
458 lines (372 loc) · 24.4 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
"""
Loss function definitions
"""
import numpy as np
import sys
import keras.backend as K
import tensorflow as tf
from coordinates import get_loss_weights
from coordinates import CoordinateType
from coordinates import get_coordinate_dimension
# NOTE: cordinate_encoding variable must be set to proper coordinate type
# Keras can't pass additional parameters to its loss function callbacks :-(
from configuration import IGNORE_MOVEMENT, coordinate_encoding, number_of_coordinates, number_of_frames
loss_weight_adjustments = get_loss_weights(coordinate_encoding,
IGNORE_MOVEMENT,
center_boost=1/100.0,
angular_boost=1.0,
bone_length_boost=1.0)
loss_weight_adjustments = np.repeat(loss_weight_adjustments, number_of_frames, axis=0)
loss_weight_adjustments = K.variable(value=loss_weight_adjustments)
def cumulative_point_distance_error(y_true, y_pred):
"""
Cumulative Euclidean distance of all (x, y) pairs between true and predicted vectors
Depending on coordinate encoding, it could be either distances of (x, y) pairs or
centers with distances of relative (dx, dy) pairs, or centers with angular
and bone length differences
Arguments:
y_true -- tensor of true values, [batch number, frame number, x/y as 0/1, coordinate]
y_pred -- tensor of predicted values, [batch number, frame number, x/y as 0/1, coordinate]
"""
# NOTE: Loss functions are defined in TensorFlow as Keras' backend doesn't expose many
# of the necessary functions :-(
if coordinate_encoding == CoordinateType.SAME:
delta_x = y_true[:, :, 0, :] - y_pred[:, :, 0, :]
delta_y = y_true[:, :, 1, :] - y_pred[:, :, 1, :]
weight_x = loss_weight_adjustments[:, :number_of_coordinates]
weight_y = loss_weight_adjustments[:, number_of_coordinates:]
# loss is sum of square roots of sums of squares per rows of weight-adjusted x and y differences
loss = tf.reduce_sum(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_x, weight_x)), axis=2))) +\
tf.reduce_sum(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_y, weight_y)), axis=2)))
elif coordinate_encoding == CoordinateType.OFFSET:
# (c_x, c_y, x_0...x_n, y_0...y_n)
delta_x = y_true[:, :, 0, 1:] - y_pred[:, :, 0, 1:]
delta_y = y_true[:, :, 1, 1:] - y_pred[:, :, 1, 1:]
c_delta_x = y_true[:, :, 0, 0] - y_pred[:, :, 0, 0]
c_delta_y = y_true[:, :, 1, 0] - y_pred[:, :, 1, 0]
weight_x = loss_weight_adjustments[:, 1:number_of_coordinates]
weight_y = loss_weight_adjustments[:, number_of_coordinates + 1:]
weight_c_x = loss_weight_adjustments[:, 0]
weight_c_y = loss_weight_adjustments[:, number_of_coordinates]
# relative coordinate loss + center loss
loss = tf.reduce_sum(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_x, weight_x)), axis=2))) +\
tf.reduce_sum(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_y, weight_y)), axis=2))) +\
tf.reduce_sum(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(c_delta_x, weight_c_x)), axis=1))) +\
tf.reduce_sum(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(c_delta_y, weight_c_y)), axis=1)))
elif coordinate_encoding == CoordinateType.ANGLE:
# WARNING: This loss function is extremely slow !!!
# Use only on Multi-GPU configurations (>= 8x) for a reasonable convergence time
# input is: [batch x frame x dimension x coordinate]
# angular distances, normalized to [-pi, pi]
angle_true = y_true[:, :, 0, 1:]
angle_pred = y_pred[:, :, 0, 1:]
delta_angle = tf.atan2(tf.sin(angle_true - angle_pred), tf.cos(angle_true - angle_pred))
# bone length differences
delta_bone_length = y_true[:, :, 1, 1:] - y_pred[:, :, 1, 1:]
# center position differences
c_delta_x = y_true[:, :, 0, 0] - y_pred[:, :, 0, 0]
c_delta_y = y_true[:, :, 1, 0] - y_pred[:, :, 1, 0]
# weights for angles
weight_angle = loss_weight_adjustments[:, 1: number_of_coordinates]
# weights for bone lengths
weight_bone_length = loss_weight_adjustments[:, number_of_coordinates + 1:]
# center weight
weight_c_x = loss_weight_adjustments[:, 0]
weight_c_y = loss_weight_adjustments[:, number_of_coordinates]
# loss is sum of squares of weight-adjusted angular differences + sum of squares of weight-adjusted
# bone length differences + sum of squares of weight-adjusted center differences;
# this is done per row (row = batch x frame), then summed together
loss = tf.reduce_sum(
tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_angle, weight_angle)), axis=2)) +\
tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_bone_length, weight_bone_length)), axis=2))
) +\
tf.reduce_sum(tf.sqrt(
tf.reduce_sum(tf.square(tf.multiply(c_delta_x, weight_c_x)), axis=1) +\
tf.reduce_sum(tf.square(tf.multiply(c_delta_y, weight_c_y)), axis=1)
)
)
return loss
def mean_point_distance_error(y_true, y_pred):
"""
Mean Euclidean distance of all (x, y) pairs between true and predicted vectors
Depending on coordinate encoding, it could be either distances of (x, y) pairs or
centers with distances of relative (dx, dy) pairs, or centers with angular
and bone length differences
Arguments:
y_true -- tensor of true values, [batch number, frame number, x/y as 0/1, coordinate]
y_pred -- tensor of predicted values, [batch number, frame number, x/y as 0/1, coordinate]
"""
if coordinate_encoding == CoordinateType.SAME:
delta_x = y_true[:, :, 0, :] - y_pred[:, :, 0, :]
delta_y = y_true[:, :, 1, :] - y_pred[:, :, 1, :]
weight_x = loss_weight_adjustments[:, :number_of_coordinates]
weight_y = loss_weight_adjustments[:, number_of_coordinates:]
# loss is sum of square roots of sums of squares per rows of weight-adjusted x and y differences
loss = tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_x, weight_x)), axis=2))) +\
tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_y, weight_y)), axis=2)))
elif coordinate_encoding == CoordinateType.OFFSET:
# (c_x, c_y, x_0...x_n, y_0...y_n)
delta_x = y_true[:, :, 0, 1:] - y_pred[:, :, 0, 1:]
delta_y = y_true[:, :, 1, 1:] - y_pred[:, :, 1, 1:]
c_delta_x = y_true[:, :, 0, 0] - y_pred[:, :, 0, 0]
c_delta_y = y_true[:, :, 1, 0] - y_pred[:, :, 1, 0]
weight_x = loss_weight_adjustments[:, 1: number_of_coordinates]
weight_y = loss_weight_adjustments[:, number_of_coordinates + 1:]
weight_c_x = loss_weight_adjustments[:, 0]
weight_c_y = loss_weight_adjustments[:, number_of_coordinates]
# relative coordinate loss + center loss
loss = tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_x, weight_x)), axis=2))) +\
tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_y, weight_y)), axis=2))) +\
tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(c_delta_x, weight_c_x)), axis=1))) +\
tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(c_delta_y, weight_c_y)), axis=1)))
elif coordinate_encoding == CoordinateType.ANGLE:
# WARNING: This loss function is extremely slow !!!
# Use only on Multi-GPU configurations (>= 8x) for a reasonable convergence time
# input is: [batch x frame x dimension x coordinate]
# angular distances, normalized to [-pi, pi]
angle_true = y_true[:, :, 0, 1:]
angle_pred = y_pred[:, :, 0, 1:]
delta_angle = tf.atan2(tf.sin(angle_true - angle_pred), tf.cos(angle_true - angle_pred))
# bone length differences
delta_bone_length = y_true[:, :, 1, 1:] - y_pred[:, :, 1, 1:]
# center position differences
c_delta_x = y_true[:, :, 0, 0] - y_pred[:, :, 0, 0]
c_delta_y = y_true[:, :, 1, 0] - y_pred[:, :, 1, 0]
# weights for angles
weight_angle = loss_weight_adjustments[:, 1: number_of_coordinates]
# weights for bone lengths
weight_bone_length = loss_weight_adjustments[:, number_of_coordinates + 1:]
# center weight
weight_c_x = loss_weight_adjustments[:, 0]
weight_c_y = loss_weight_adjustments[:, number_of_coordinates]
# loss is sum of squares of weight-adjusted angular differences + sum of squares of weight-adjusted
# bone length differences + sum of squares of weight-adjusted center differences;
# this is done per row (row = batch x frame), then summed together
loss = tf.reduce_mean(
tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_angle, weight_angle)), axis=2)) +\
tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_bone_length, weight_bone_length)), axis=2))
) * 0.5 +\
tf.reduce_mean(tf.sqrt(
tf.reduce_sum(tf.square(tf.multiply(c_delta_x, weight_c_x)), axis=1) +\
tf.reduce_sum(tf.square(tf.multiply(c_delta_y, weight_c_y)), axis=1)
)
) * 0.5
return loss
def pair_cumulative_point_distance_error(y_true, y_pred):
"""
Cumulative pair-wise Euclidean distance of all (x, y) pairs between true and predicted vectors
Depending on coordinate encoding, it could be either distances of (x, y) pairs or
centers with distances of relative (dx, dy) pairs, or centers with angular
and bone length differences
Arguments:
y_true -- tensor of true values, [batch number, frame number, x/y as 0/1, coordinate]
y_pred -- tensor of predicted values, [batch number, frame number, x/y as 0/1, coordinate]
"""
# NOTE: Loss functions are defined in TensorFlow as Keras' backend doesn't expose many
# of the necessary functions :-(
y_true_1 = y_true[:, :, :, :number_of_coordinates]
y_pred_1 = y_pred[:, :, :, :number_of_coordinates]
y_true_2 = y_true[:, :, :, number_of_coordinates:]
y_pred_2 = y_pred[:, :, :, number_of_coordinates:]
if coordinate_encoding == CoordinateType.SAME:
delta_x_1 = y_true_1[:, :, 0, :] - y_pred_1[:, :, 0, :]
delta_y_1 = y_true_1[:, :, 1, :] - y_pred_1[:, :, 1, :]
delta_x_2 = y_true_2[:, :, 0, :] - y_pred_2[:, :, 0, :]
delta_y_2 = y_true_2[:, :, 1, :] - y_pred_2[:, :, 1, :]
weight_x = loss_weight_adjustments[:, :number_of_coordinates]
weight_y = loss_weight_adjustments[:, number_of_coordinates:]
# loss is sum of square roots of sums of squares per rows of weight-adjusted x and y differences
loss = tf.reduce_sum(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_x_1, weight_x)), axis=2))) +\
tf.reduce_sum(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_y_1, weight_y)), axis=2))) +\
tf.reduce_sum(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_x_2, weight_x)), axis=2))) +\
tf.reduce_sum(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_y_2, weight_y)), axis=2)))
elif coordinate_encoding == CoordinateType.OFFSET:
# (c_x, c_y, x_0...x_n, y_0...y_n)
delta_x_1 = y_true_1[:, :, 0, 1:] - y_pred_1[:, :, 0, 1:]
delta_y_1 = y_true_1[:, :, 1, 1:] - y_pred_1[:, :, 1, 1:]
delta_x_2 = y_true_2[:, :, 0, 1:] - y_pred_2[:, :, 0, 1:]
delta_y_2 = y_true_2[:, :, 1, 1:] - y_pred_2[:, :, 1, 1:]
c_delta_x_1 = y_true_1[:, :, 0, 0] - y_pred_1[:, :, 0, 0]
c_delta_y_1 = y_true_1[:, :, 1, 0] - y_pred_1[:, :, 1, 0]
c_delta_x_2 = y_true_2[:, :, 0, 0] - y_pred_2[:, :, 0, 0]
c_delta_y_2 = y_true_2[:, :, 1, 0] - y_pred_2[:, :, 1, 0]
weight_x = loss_weight_adjustments[:, 1:number_of_coordinates]
weight_y = loss_weight_adjustments[:, number_of_coordinates + 1:]
weight_c_x = loss_weight_adjustments[:, 0]
weight_c_y = loss_weight_adjustments[:, number_of_coordinates]
# relative coordinate loss + center loss
loss = tf.reduce_sum(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_x_1, weight_x)), axis=2))) +\
tf.reduce_sum(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_y_1, weight_y)), axis=2))) +\
tf.reduce_sum(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(c_delta_x_1, weight_c_x)), axis=1))) +\
tf.reduce_sum(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(c_delta_y_1, weight_c_y)), axis=1))) +\
tf.reduce_sum(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_x_2, weight_x)), axis=2))) +\
tf.reduce_sum(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_y_2, weight_y)), axis=2))) +\
tf.reduce_sum(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(c_delta_x_2, weight_c_x)), axis=1))) +\
tf.reduce_sum(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(c_delta_y_2, weight_c_y)), axis=1)))
elif coordinate_encoding == CoordinateType.ANGLE:
# WARNING: This loss function is extremely slow !!!
# Use only on Multi-GPU configurations (>= 8x) for a reasonable convergence time
# input is: [batch x frame x dimension x coordinate]
# angular distances, normalized to [-pi, pi]
angle_true_1 = y_true_1[:, :, 0, 1:]
angle_pred_1 = y_pred_1[:, :, 0, 1:]
angle_true_2 = y_true_2[:, :, 0, 1:]
angle_pred_2 = y_pred_2[:, :, 0, 1:]
delta_angle_1 = tf.atan2(tf.sin(angle_true_1 - angle_pred_1), tf.cos(angle_true_1 - angle_pred_1))
delta_angle_2 = tf.atan2(tf.sin(angle_true_2 - angle_pred_2), tf.cos(angle_true_2 - angle_pred_2))
# bone length differences
delta_bone_length_1 = y_true_1[:, :, 1, 1:] - y_pred_1[:, :, 1, 1:]
delta_bone_length_2 = y_true_2[:, :, 1, 1:] - y_pred_2[:, :, 1, 1:]
# center position differences
c_delta_x_1 = y_true_1[:, :, 0, 0] - y_pred_1[:, :, 0, 0]
c_delta_y_1 = y_true_1[:, :, 1, 0] - y_pred_1[:, :, 1, 0]
c_delta_x_2 = y_true_2[:, :, 0, 0] - y_pred_2[:, :, 0, 0]
c_delta_y_2 = y_true_2[:, :, 1, 0] - y_pred_2[:, :, 1, 0]
# weights for angles
weight_angle = loss_weight_adjustments[:, 1: number_of_coordinates]
# weights for bone lengths
weight_bone_length = loss_weight_adjustments[:, number_of_coordinates + 1:]
# center weight
weight_c_x = loss_weight_adjustments[:, 0]
weight_c_y = loss_weight_adjustments[:, number_of_coordinates]
# loss is sum of squares of weight-adjusted angular differences + sum of squares of weight-adjusted
# bone length differences + sum of squares of weight-adjusted center differences;
# this is done per row (row = batch x frame), then summed together
loss = tf.reduce_sum(
tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_angle_1, weight_angle)), axis=2)) +\
tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_bone_length_1, weight_bone_length)), axis=2))
) +\
tf.reduce_sum(tf.sqrt(
tf.reduce_sum(tf.square(tf.multiply(c_delta_x_1, weight_c_x)), axis=1) +\
tf.reduce_sum(tf.square(tf.multiply(c_delta_y_1, weight_c_y)), axis=1)
)
) +\
tf.reduce_sum(
tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_angle_2, weight_angle)), axis=2)) +\
tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_bone_length_2, weight_bone_length)), axis=2))
) +\
tf.reduce_sum(tf.sqrt(
tf.reduce_sum(tf.square(tf.multiply(c_delta_x_2, weight_c_x)), axis=1) +\
tf.reduce_sum(tf.square(tf.multiply(c_delta_y_2, weight_c_y)), axis=1)
)
)
return loss
def pair_mean_point_distance_error(y_true, y_pred):
"""
Mean pair-wise Euclidean distance of all (x, y) pairs between true and predicted vectors
Depending on coordinate encoding, it could be either distances of (x, y) pairs or
centers with distances of relative (dx, dy) pairs, or centers with angular
and bone length differences
Arguments:
y_true -- tensor of true values, [batch number, frame number, x/y as 0/1, coordinate]
y_pred -- tensor of predicted values, [batch number, frame number, x/y as 0/1, coordinate]
"""
# NOTE: Loss functions are defined in TensorFlow as Keras' backend doesn't expose many
# of the necessary functions :-(
y_true_1 = y_true[:, :, :, :number_of_coordinates]
y_pred_1 = y_pred[:, :, :, :number_of_coordinates]
y_true_2 = y_true[:, :, :, number_of_coordinates:]
y_pred_2 = y_pred[:, :, :, number_of_coordinates:]
if coordinate_encoding == CoordinateType.SAME:
delta_x_1 = y_true_1[:, :, 0, :] - y_pred_1[:, :, 0, :]
delta_y_1 = y_true_1[:, :, 1, :] - y_pred_1[:, :, 1, :]
delta_x_2 = y_true_2[:, :, 0, :] - y_pred_2[:, :, 0, :]
delta_y_2 = y_true_2[:, :, 1, :] - y_pred_2[:, :, 1, :]
weight_x = loss_weight_adjustments[:, :number_of_coordinates]
weight_y = loss_weight_adjustments[:, number_of_coordinates:]
# loss is sum of square roots of sums of squares per rows of weight-adjusted x and y differences
loss = tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_x_1, weight_x)), axis=2))) +\
tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_y_1, weight_y)), axis=2))) +\
tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_x_2, weight_x)), axis=2))) +\
tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_y_2, weight_y)), axis=2)))
elif coordinate_encoding == CoordinateType.OFFSET:
# (c_x, c_y, x_0...x_n, y_0...y_n)
delta_x_1 = y_true_1[:, :, 0, 1:] - y_pred_1[:, :, 0, 1:]
delta_y_1 = y_true_1[:, :, 1, 1:] - y_pred_1[:, :, 1, 1:]
delta_x_2 = y_true_2[:, :, 0, 1:] - y_pred_2[:, :, 0, 1:]
delta_y_2 = y_true_2[:, :, 1, 1:] - y_pred_2[:, :, 1, 1:]
c_delta_x_1 = y_true_1[:, :, 0, 0] - y_pred_1[:, :, 0, 0]
c_delta_y_1 = y_true_1[:, :, 1, 0] - y_pred_1[:, :, 1, 0]
c_delta_x_2 = y_true_2[:, :, 0, 0] - y_pred_2[:, :, 0, 0]
c_delta_y_2 = y_true_2[:, :, 1, 0] - y_pred_2[:, :, 1, 0]
weight_x = loss_weight_adjustments[:, 1:number_of_coordinates]
weight_y = loss_weight_adjustments[:, number_of_coordinates + 1:]
weight_c_x = loss_weight_adjustments[:, 0]
weight_c_y = loss_weight_adjustments[:, number_of_coordinates]
# relative coordinate loss + center loss
loss = tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_x_1, weight_x)), axis=2))) +\
tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_y_1, weight_y)), axis=2))) +\
tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(c_delta_x_1, weight_c_x)), axis=1))) +\
tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(c_delta_y_1, weight_c_y)), axis=1))) +\
tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_x_2, weight_x)), axis=2))) +\
tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_y_2, weight_y)), axis=2))) +\
tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(c_delta_x_2, weight_c_x)), axis=1))) +\
tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(c_delta_y_2, weight_c_y)), axis=1)))
elif coordinate_encoding == CoordinateType.ANGLE:
# WARNING: This loss function is extremely slow !!!
# Use only on Multi-GPU configurations (>= 8x) for a reasonable convergence time
# input is: [batch x frame x dimension x coordinate]
# angular distances, normalized to [-pi, pi]
angle_true_1 = y_true_1[:, :, 0, 1:]
angle_pred_1 = y_pred_1[:, :, 0, 1:]
angle_true_2 = y_true_2[:, :, 0, 1:]
angle_pred_2 = y_pred_2[:, :, 0, 1:]
delta_angle_1 = tf.atan2(tf.sin(angle_true_1 - angle_pred_1), tf.cos(angle_true_1 - angle_pred_1))
delta_angle_2 = tf.atan2(tf.sin(angle_true_2 - angle_pred_2), tf.cos(angle_true_2 - angle_pred_2))
# bone length differences
delta_bone_length_1 = y_true_1[:, :, 1, 1:] - y_pred_1[:, :, 1, 1:]
delta_bone_length_2 = y_true_2[:, :, 1, 1:] - y_pred_2[:, :, 1, 1:]
# center position differences
c_delta_x_1 = y_true_1[:, :, 0, 0] - y_pred_1[:, :, 0, 0]
c_delta_y_1 = y_true_1[:, :, 1, 0] - y_pred_1[:, :, 1, 0]
c_delta_x_2 = y_true_2[:, :, 0, 0] - y_pred_2[:, :, 0, 0]
c_delta_y_2 = y_true_2[:, :, 1, 0] - y_pred_2[:, :, 1, 0]
# weights for angles
weight_angle = loss_weight_adjustments[:, 1: number_of_coordinates]
# weights for bone lengths
weight_bone_length = loss_weight_adjustments[:, number_of_coordinates + 1:]
# center weight
weight_c_x = loss_weight_adjustments[:, 0]
weight_c_y = loss_weight_adjustments[:, number_of_coordinates]
# loss is sum of squares of weight-adjusted angular differences + sum of squares of weight-adjusted
# bone length differences + sum of squares of weight-adjusted center differences;
# this is done per row (row = batch x frame), then summed together
loss = tf.reduce_mean(
tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_angle_1, weight_angle)), axis=2)) +\
tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_bone_length_1, weight_bone_length)), axis=2))
) +\
tf.reduce_mean(tf.sqrt(
tf.reduce_sum(tf.square(tf.multiply(c_delta_x_1, weight_c_x)), axis=1) +\
tf.reduce_sum(tf.square(tf.multiply(c_delta_y_1, weight_c_y)), axis=1)
)
) +\
tf.reduce_mean(
tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_angle_2, weight_angle)), axis=2)) +\
tf.sqrt(tf.reduce_sum(tf.square(tf.multiply(delta_bone_length_2, weight_bone_length)), axis=2))
) +\
tf.reduce_mean(tf.sqrt(
tf.reduce_sum(tf.square(tf.multiply(c_delta_x_2, weight_c_x)), axis=1) +\
tf.reduce_sum(tf.square(tf.multiply(c_delta_y_2, weight_c_y)), axis=1)
)
)
return loss
# NOTE: This needs to be set for a pair-wise loss function
# Keras can't pass additional parameters to its loss function callbacks :-(
base_loss_function = cumulative_point_distance_error
def pair_loss(y_true, y_pred):
"""
Computes a pair-wise loss function assuming two parameters
Loss function for a single track is defined by loss_function variable
It assumes coordinates as [batch x frame x dimension x 2 * number of coordinates],
where the last two dimensions have concatenated coordinates of both tracked poses
The resulting loss is just a simple sum of two individual losses on their corresponding
coordinate portion
Arguments:
y_true -- tensor of true values, [batch number, frame number, x/y as 0/1, coordinate]
y_pred -- tensor of predicted values, [batch number, frame number, x/y as 0/1, coordinate]
"""
loss_1 = base_loss_function(y_true[:, :, :, :number_of_coordinates],
y_pred[:, :, :, :number_of_coordinates])
loss_2 = base_loss_function(y_true[:, :, :, number_of_coordinates:],
y_pred[:, :, :, number_of_coordinates:])
return loss_1 + loss_2