forked from gofynd/mildnet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloss.py
191 lines (164 loc) · 7.19 KB
/
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
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
from tensorflow.keras import backend as K
import tensorflow as tf
_EPSILON = K.epsilon()
def hinge_loss_fn(batch_size):
def hinge_loss(y_true, y_pred):
y_pred = K.clip(y_pred, _EPSILON, 1.0-_EPSILON)
loss = tf.convert_to_tensor(0,dtype=tf.float32)
g = tf.constant(1.0, shape=[1], dtype=tf.float32)
for i in range(0, batch_size, 3):
try:
q_embedding = y_pred[i+0]
p_embedding = y_pred[i+1]
n_embedding = y_pred[i+2]
D_q_p = K.sqrt(K.sum((q_embedding - p_embedding)**2))
D_q_n = K.sqrt(K.sum((q_embedding - n_embedding)**2))
loss = (loss + g + D_q_p - D_q_n)
except:
continue
loss = loss/(batch_size/3)
zero = tf.constant(0.0, shape=[1], dtype=tf.float32)
return tf.maximum(loss,zero)
return hinge_loss
def hinge_new_loss_fn(batch_size):
def hinge_new_loss(y_true, y_pred):
y_pred = K.clip(y_pred, _EPSILON, 1.0-_EPSILON)
loss = tf.convert_to_tensor(0,dtype=tf.float32)
g = tf.constant(1.0, shape=[1], dtype=tf.float32)
for i in range(0, batch_size, 3):
try:
q_embedding = y_pred[i+0]
p_embedding = y_pred[i+1]
n_embedding = y_pred[i+2]
D_q_p = K.sqrt(K.sum((q_embedding - p_embedding)**2))
D_q_n = K.sqrt(K.sum((q_embedding - n_embedding)**2))
D_p_n = K.sqrt(K.sum((p_embedding - n_embedding)**2))
loss = (loss + g + D_q_p - D_q_n + D_q_p - D_p_n)
except:
continue
loss = loss/(batch_size/6)
zero = tf.constant(0.0, shape=[1], dtype=tf.float32)
return tf.maximum(loss,zero)
return hinge_new_loss
def hinge_twice_loss_fn(batch_size):
def hinge_twice_loss(y_true, y_pred):
y_pred = K.clip(y_pred, _EPSILON, 1.0-_EPSILON)
loss = tf.convert_to_tensor(0,dtype=tf.float32)
g = tf.constant(1.0, shape=[1], dtype=tf.float32)
for i in range(0, batch_size, 3):
try:
q_embedding = y_pred[i+0]
p_embedding = y_pred[i+1]
n_embedding = y_pred[i+2]
D_q_p = K.sqrt(K.sum((q_embedding - p_embedding)**2))
D_q_n = K.sqrt(K.sum((q_embedding - n_embedding)**2))
loss = (loss + g + D_q_p - D_q_n)
except:
continue
loss = loss/(batch_size/6)
zero = tf.constant(0.0, shape=[1], dtype=tf.float32)
return tf.maximum(loss,zero)
return hinge_twice_loss
def contrastive_loss_fn(batch_size):
def contrastive_loss(y_true, y_pred):
def _contrastive_loss(y1, D):
g = tf.constant(1.0, shape=[1], dtype=tf.float32)
return K.mean(y1 * K.square(D) +
(g - y1) * K.square(K.maximum(g - D, 0)))
y_pred = K.clip(y_pred, _EPSILON, 1.0-_EPSILON)
loss = tf.convert_to_tensor(0,dtype=tf.float32)
g = tf.constant(1.0, shape=[1], dtype=tf.float32)
h = tf.constant(0.0, shape=[1], dtype=tf.float32)
for i in range(0,batch_size,3):
try:
q_embedding = y_pred[i+0]
p_embedding = y_pred[i+1]
n_embedding = y_pred[i+2]
D_q_p = K.sqrt(K.sum((q_embedding - p_embedding)**2))
D_q_n = K.sqrt(K.sum((q_embedding - n_embedding)**2))
L_q_p = _contrastive_loss(g, D_q_p)
L_q_n = _contrastive_loss(h, D_q_n)
loss = (loss + L_q_p + L_q_n )
except:
continue
loss = loss/(batch_size*2/3)
zero = tf.constant(0.0, shape=[1], dtype=tf.float32)
return tf.maximum(loss,zero)
return contrastive_loss
#https://towardsdatascience.com/lossless-triplet-loss-7e932f990b24
def lossless_loss_fn(batch_size):
def lossless_loss(y_true, y_pred):
N = tf.constant(4096.0, shape=[1], dtype=tf.float32)
beta = tf.constant(4096.0, shape=[1], dtype=tf.float32)
y_pred = K.clip(y_pred, _EPSILON, 1.0-_EPSILON)
loss = tf.convert_to_tensor(0,dtype=tf.float32)
g = tf.constant(1.0, shape=[1], dtype=tf.float32)
const1 = tf.constant(1.0, shape=[1], dtype=tf.float32)
for i in range(0,batch_size,3):
try:
anchor = y_pred[i+0]
positive = y_pred[i+1]
negative = y_pred[i+2]
pos_dist = K.sum(K.square(anchor-positive),1)
neg_dist = K.sum(K.square(anchor,negative),1)
pos_dist = -tf.log(-tf.divide((pos_dist), beta)+const1+epsilon)
neg_dist = -tf.log(-tf.divide((N-neg_dist), beta)+const1+epsilon)
_loss = neg_dist + pos_dist
loss = (loss + g + _loss)
except:
continue
loss = loss/(batch_size/3)
zero = tf.constant(0.0, shape=[1], dtype=tf.float32)
return tf.maximum(loss,zero)
return lossless_loss
def angular_loss_1_fn(batch_size):
def angular_loss_1(y_true, y_pred):
y_pred = K.clip(y_pred, _EPSILON, 1.0-_EPSILON)
loss = tf.convert_to_tensor(0,dtype=tf.float32)
g = tf.constant(1.0, shape=[1], dtype=tf.float32)
c = tf.constant(4.0, shape=[1], dtype=tf.float32)
alpha = tf.constant(45.0, shape=[1], dtype=tf.float32)
for i in range(0,batch_size,3):
try:
xa = y_pred[i+0]
xp = y_pred[i+1]
xn = y_pred[i+2]
sq = K.square(xa-xp)
xc = (xa+xp)/2
_loss = sq - c*(tf.tan(alpha*K.square(xn-xc))**2)
zero = tf.constant(0.0, shape=[1], dtype=tf.float32)
_loss = tf.maximum(_loss,zero)
loss = (loss + g + _loss)
except:
continue
loss = loss/(batch_size/3)
return loss
return angular_loss_1
def angular_loss_2_fn(batch_size):
def angular_loss_2(y_true, y_pred):
y_pred = K.clip(y_pred, _EPSILON, 1.0-_EPSILON)
loss = tf.convert_to_tensor(0,dtype=tf.float32)
g = tf.constant(1.0, shape=[1], dtype=tf.float32)
c = tf.constant(4.0, shape=[1], dtype=tf.float32)
d = tf.constant(2.0, shape=[1], dtype=tf.float32)
alpha = tf.constant(45.0, shape=[1], dtype=tf.float32)
losses = []
losses2 = []
for i in range(0,batch_size,3):
try:
xa = y_pred[i+0]
xp = y_pred[i+1]
xn = y_pred[i+2]
fapn = c*(tf.tan(alpha*K.transpose(xa+xp)*xn)**2) - d*(g+tf.tan(alpha)**2)*K.transpose(xa)*xp
losses.append(fapn)
losses2.append(K.transpose(xa)*xn - K.transpose(xa)*xp)
loss = (loss + g + _loss)
except:
continue
loss = K.sum(K.log(1+2*K.sum([K.exp(v) for v in losses])))
loss2 = K.sum(K.log(1+2*K.sum([K.exp(v) for v in losses2])))
loss = loss + 2*loss2
loss = loss/(batch_size/3)
zero = tf.constant(0.0, shape=[1], dtype=tf.float32)
return tf.maximum(loss,zero)
return angular_loss_2