-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriplet_loss.py
46 lines (37 loc) · 1.34 KB
/
triplet_loss.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
import logging
import keras.backend as K
import config as c
alpha = 0.2 # used in FaceNet https://arxiv.org/pdf/1503.03832.pdf
def batch_cosine_similarity(x1, x2):
# https://en.wikipedia.org/wiki/Cosine_similarity
# 1 = equal direction ; -1 = opposite direction
dot = K.squeeze(K.batch_dot(x1, x2, axes=1), axis=1)
return dot
def deep_speaker_loss(y_true, y_pred):
# y_true.shape = (batch_size, embedding_size)
# y_pred.shape = (batch_size, embedding_size)
# CONVENTION: Input is:
# concat(BATCH_SIZE * [ANCHOR, POSITIVE_EX, NEGATIVE_EX] * NUM_FRAMES)
# EXAMPLE:
# BATCH_NUM_TRIPLETS = 3, NUM_FRAMES = 2
# _____________________________________________________
# ANCHOR 1 (512,)
# ANCHOR 2 (512,)
# ANCHOR 3 (512,)
# POS EX 1 (512,)
# POS EX 2 (512,)
# POS EX 3 (512,)
# NEG EX 1 (512,)
# NEG EX 2 (512,)
# NEG EX 3 (512,)
# _____________________________________________________
#elements = int(y_pred.shape.as_list()[0] / 3)
elements = c.BATCH_SIZE
anchor = y_pred[0:elements]
positive_ex = y_pred[elements:2 * elements]
negative_ex = y_pred[2 * elements:]
sap = batch_cosine_similarity(anchor, positive_ex)
san = batch_cosine_similarity(anchor, negative_ex)
loss = K.maximum(san - sap + alpha, 0.0)
total_loss = K.sum(loss)
return total_loss