-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloss.py
474 lines (397 loc) · 19.4 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
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
import torch
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd.function import Function
from torch.autograd import Variable
import pdb
class OriTripletLoss(nn.Module):
"""Triplet loss with hard positive/negative mining.
Reference:
Hermans et al. In Defense of the Triplet Loss for Person Re-Identification. arXiv:1703.07737.
Code imported from https://github.com/Cysu/open-reid/blob/master/reid/loss/triplet.py.
Args:
- margin (float): margin for triplet.
"""
def __init__(self, batch_size, margin=0.3):
super(OriTripletLoss, self).__init__()
self.margin = margin
self.ranking_loss = nn.MarginRankingLoss(margin=margin)
def forward(self, inputs, label_assign, targets, true_targets, prob, threshold=0.6, alpha=100):
"""
Args:
- inputs: feature matrix with shape (batch_size, feat_dim)
- targets: ground truth labels with shape (num_classes)
"""
n = inputs.size(0)
# Compute pairwise distance, replace by the official when merged
dist = torch.pow(inputs, 2).sum(dim=1, keepdim=True).expand(n, n)
dist = dist + dist.t()
dist.addmm_(1, -2, inputs, inputs.t())
dist = dist.clamp(min=1e-12).sqrt() # for numerical stability
# For each anchor, find the hardest positive and negative
mask = targets.expand(n, n).eq(targets.expand(n, n).t())
dist_ap, dist_an = [], []
for i in range(n):
dist_ap.append(dist[i][mask[i]].max().unsqueeze(0))
dist_an.append(dist[i][mask[i] == 0].min().unsqueeze(0))
dist_ap = torch.cat(dist_ap)
dist_an = torch.cat(dist_an)
# Compute ranking hinge loss
y = torch.ones_like(dist_an)
loss = self.ranking_loss(dist_an, dist_ap, y)
# compute accuracy
correct = torch.ge(dist_an, dist_ap).sum().item()
return loss, correct, dist_an.shape[0]
def softmax_weights(dist, mask):
max_v = torch.max(dist * mask, dim=1, keepdim=True)[0]
diff = dist - max_v
Z = torch.sum(torch.exp(diff) * mask, dim=1, keepdim=True) + 1e-6 # avoid division by zero
W = torch.exp(diff) * mask / Z
return W
def normalize(x, axis=-1):
"""Normalizing to unit length along the specified dimension.
Args:
x: pytorch Variable
Returns:
x: pytorch Variable, same shape as input
"""
x = 1. * x / (torch.norm(x, 2, axis, keepdim=True).expand_as(x) + 1e-12)
return x
class TripletLoss_WRT(nn.Module):
"""Weighted Regularized Triplet'."""
def __init__(self):
super(TripletLoss_WRT, self).__init__()
self.ranking_loss = nn.SoftMarginLoss()
def forward(self, inputs, label_assign, targets, true_targets, prob, threshold=0.6, alpha=100, normalize_feature=False):
if normalize_feature:
inputs = normalize(inputs, axis=-1)
dist_mat = pdist_torch(inputs, inputs)
N = dist_mat.size(0)
# shape [N, N]
is_pos = targets.expand(N, N).eq(targets.expand(N, N).t()).float()
is_neg = targets.expand(N, N).ne(targets.expand(N, N).t()).float()
# `dist_ap` means distance(anchor, positive)
# both `dist_ap` and `relative_p_inds` with shape [N, 1]
dist_ap = dist_mat * is_pos
dist_an = dist_mat * is_neg
weights_ap = softmax_weights(dist_ap, is_pos)
weights_an = softmax_weights(-dist_an, is_neg)
furthest_positive = torch.sum(dist_ap * weights_ap, dim=1)
closest_negative = torch.sum(dist_an * weights_an, dim=1)
y = furthest_positive.new().resize_as_(furthest_positive).fill_(1)
loss = self.ranking_loss(closest_negative - furthest_positive, y)
# compute accuracy
correct = torch.ge(closest_negative, furthest_positive).sum().item()
return loss, correct, closest_negative.shape[0]
class TripletLoss_ADP(nn.Module):
"""Weighted Regularized Triplet'."""
def __init__(self, alpha=1, gamma=1, square=0):
super(TripletLoss_ADP, self).__init__()
self.ranking_loss = nn.SoftMarginLoss()
self.alpha = alpha
self.gamma = gamma
self.square = square
def forward(self, inputs, label_assign, targets, true_targets, prob, threshold=0.6, alpha=100, normalize_feature=False):
if normalize_feature:
inputs = normalize(inputs, axis=-1)
dist_mat = pdist_torch(inputs, inputs)
N = dist_mat.size(0)
# shape [N, N]
is_pos = targets.expand(N, N).eq(targets.expand(N, N).t()).float()
is_neg = targets.expand(N, N).ne(targets.expand(N, N).t()).float()
# `dist_ap` means distance(anchor, positive)
# both `dist_ap` and `relative_p_inds` with shape [N, 1]
dist_ap = dist_mat * is_pos
dist_an = dist_mat * is_neg
weights_ap = softmax_weights(dist_ap * self.alpha, is_pos)
weights_an = softmax_weights(-dist_an * self.alpha, is_neg)
furthest_positive = torch.sum(dist_ap * weights_ap, dim=1)
closest_negative = torch.sum(dist_an * weights_an, dim=1)
# ranking_loss = nn.SoftMarginLoss(reduction = 'none')
# loss1 = ranking_loss(closest_negative - furthest_positive, y)
# squared difference
if self.square == 0:
y = furthest_positive.new().resize_as_(furthest_positive).fill_(1)
loss = self.ranking_loss(self.gamma * (closest_negative - furthest_positive), y)
else:
diff_pow = torch.pow(furthest_positive - closest_negative, 2) * self.gamma
diff_pow = torch.clamp_max(diff_pow, max=88)
# Compute ranking hinge loss
y1 = (furthest_positive > closest_negative).float()
y2 = y1 - 1
y = -(y1 + y2)
loss = self.ranking_loss(diff_pow, y)
# loss = self.ranking_loss(self.gamma*(closest_negative - furthest_positive), y)
# compute accuracy
correct = torch.ge(closest_negative, furthest_positive).sum().item()
return loss, correct, closest_negative.shape[0]
class KLDivLoss(nn.Module):
def __init__(self):
super(KLDivLoss, self).__init__()
def forward(self, pred, label):
# pred: 2D matrix (batch_size, num_classes)
# label: 1D vector indicating class number
T = 3
predict = F.log_softmax(pred / T, dim=1)
target_data = F.softmax(label / T, dim=1)
target_data = target_data + 10 ** (-7)
target = Variable(target_data.data.cuda(), requires_grad=False)
loss = T * T * ((target * (target.log() - predict)).sum(1).sum() / target.size()[0])
return loss
def pdist_torch(emb1, emb2):
'''
compute the eucilidean distance matrix between embeddings1 and embeddings2
using gpu
'''
m, n = emb1.shape[0], emb2.shape[0]
emb1_pow = torch.pow(emb1, 2).sum(dim=1, keepdim=True).expand(m, n)
emb2_pow = torch.pow(emb2, 2).sum(dim=1, keepdim=True).expand(n, m).t()
dist_mtx = emb1_pow + emb2_pow
dist_mtx = dist_mtx.addmm_(1, -2, emb1, emb2.t())
# dist_mtx = dist_mtx.clamp(min = 1e-12)
dist_mtx = dist_mtx.clamp(min=1e-12).sqrt()
return dist_mtx
def pdist_np(emb1, emb2):
'''
compute the eucilidean distance matrix between embeddings1 and embeddings2
using cpu
'''
m, n = emb1.shape[0], emb2.shape[0]
emb1_pow = np.square(emb1).sum(axis=1)[..., np.newaxis]
emb2_pow = np.square(emb2).sum(axis=1)[np.newaxis, ...]
dist_mtx = -2 * np.matmul(emb1, emb2.T) + emb1_pow + emb2_pow
# dist_mtx = np.sqrt(dist_mtx.clip(min = 1e-12))
return dist_mtx
class RobustTripletLoss_DART(nn.Module):
def __init__(self, batch_size, margin):
super(RobustTripletLoss_DART, self).__init__()
self.batch_size = batch_size
self.margin = margin
def forward(self, inputs, prediction, targets, true_targets, prob, threshold):
n = inputs.size(0)
# Compute pairwise distance, replace by the official when merged
dist = torch.pow(inputs, 2).sum(dim=1, keepdim=True).expand(n, n)
dist = dist + dist.t()
dist.addmm_(1, -2, inputs, inputs.t())
dist = dist.clamp(min=1e-12).sqrt() # for numerical stability
# For each anchor, find the positive and negative
is_pos = targets.expand(n, n).eq(targets.expand(n, n).t())
is_neg = targets.expand(n, n).ne(targets.expand(n, n).t())
is_confident = (prob >= threshold)
dist_ap, dist_an = [], []
cnt, loss = 0, 0
loss_inverse = False
for i in range(n):
if is_confident[i]:
pos_idx = (torch.nonzero(is_pos[i].long())).squeeze(1)
neg_idx = (torch.nonzero(is_neg[i].long())).squeeze(1)
random_pos_index = int(np.random.choice(pos_idx.cpu().numpy(), 1))
while random_pos_index == i:
random_pos_index = int(np.random.choice(pos_idx.cpu().numpy(), 1))
rank_neg_index = dist[i][neg_idx].argsort()
hard_neg_index = rank_neg_index[0]
hard_neg_index = neg_idx[hard_neg_index]
dist_ap.append(dist[i][random_pos_index].unsqueeze(0))
dist_an.append(dist[i][hard_neg_index].unsqueeze(0))
if prob[random_pos_index] >= threshold and prob[hard_neg_index] >= threshold:
# TP-TN
pass
elif prob[random_pos_index] >= threshold and prob[hard_neg_index] < threshold:
is_FN = (torch.argmax(prediction[hard_neg_index]) == targets[i])
# TP-FN
if is_FN:
tmp = rank_neg_index[1]
hard_neg_index_new = neg_idx[tmp]
j = 1
loop_cnt = 0
while prob[hard_neg_index_new] < threshold:
j += 1
tmp = rank_neg_index[j]
hard_neg_index_new = neg_idx[tmp]
loop_cnt += 1
if loop_cnt >= 10:
# print("------------warning, break the death loop---------------")
break
dist_ap[cnt] = (dist[i][random_pos_index].unsqueeze(0) +
dist[i][hard_neg_index].unsqueeze(0)) / 2
dist_an[cnt] = dist[i][hard_neg_index_new].unsqueeze(0)
# TP-TN
else:
pass
elif prob[random_pos_index] < threshold and prob[hard_neg_index] >= threshold:
# FP-TN
random_pos_index_new = int(np.random.choice(pos_idx.cpu().numpy(), 1))
loop_cnt = 0
while random_pos_index_new == i or prob[random_pos_index_new] < threshold:
random_pos_index_new = int(np.random.choice(pos_idx.cpu().numpy(), 1))
loop_cnt += 1
if loop_cnt >= 5:
# print("------------warning, break the death loop---------------")
break
dist_an[cnt] = (dist[i][random_pos_index].unsqueeze(0)
+ dist[i][hard_neg_index].unsqueeze(0)) / 2
dist_ap[cnt] = dist[i][random_pos_index_new].unsqueeze(0)
elif prob[random_pos_index] < threshold and prob[hard_neg_index] < threshold:
is_FN = (torch.argmax(prediction[hard_neg_index]) == targets[i])
# FP-FN
if is_FN:
loss_inverse = True
# FP-TN
else:
random_pos_index_new = int(np.random.choice(pos_idx.cpu().numpy(), 1))
loop_cnt = 0
while random_pos_index_new == i or prob[random_pos_index_new] < threshold:
random_pos_index_new = int(np.random.choice(pos_idx.cpu().numpy(), 1))
loop_cnt += 1
if loop_cnt >= 5:
# print("------------warning, break the death loop---------------")
break
dist_an[cnt] = (dist[i][random_pos_index].unsqueeze(0)
+ dist[i][hard_neg_index].unsqueeze(0)) / 2
dist_ap[cnt] = dist[i][random_pos_index_new].unsqueeze(0)
if loss_inverse:
loss += torch.clamp(dist_an[cnt] - dist_ap[cnt] + self.margin, 0)
else:
loss += torch.clamp(dist_ap[cnt] - dist_an[cnt] + self.margin, 0)
cnt += 1
loss_inverse = False
else:
continue
# compute accuracy
if cnt == 0:
return torch.Tensor([0.]).to(inputs.device), 0, cnt
else:
dist_ap = torch.cat(dist_ap)
dist_an = torch.cat(dist_an)
correct = torch.ge(dist_an, dist_ap).sum().item()
return loss / cnt, correct, cnt
class RobustTripletLoss_LCNL(nn.Module):
def __init__(self, batch_size, margin, op_type):
super(RobustTripletLoss_LCNL, self).__init__()
self.batch_size = batch_size
self.margin = margin
self.op_type = op_type
def forward(self, inputs, prediction, targets, true_targets, prob, threshold):
n = inputs.size(0)
# Compute pairwise distance, replace by the official when merged
dist = torch.pow(inputs, 2).sum(dim=1, keepdim=True).expand(n, n)
dist = dist + dist.t()
dist.addmm_(1, -2, inputs, inputs.t())
dist = dist.clamp(min=1e-12).sqrt() # for numerical stability
# For each anchor, find the positive and negative
is_pos = targets.expand(n, n).eq(targets.expand(n, n).t())
is_neg = targets.expand(n, n).ne(targets.expand(n, n).t())
is_confident = (prob >= threshold)
dist_ap, dist_an = [], []
cnt, loss = 0, 0
loss_inverse = False
for i in range(n):
if is_confident[i]:
pos_idx = (torch.nonzero(is_pos[i].long())).squeeze(1)
neg_idx = (torch.nonzero(is_neg[i].long())).squeeze(1)
rank_pos_index = dist[i][pos_idx].argsort()
hard_pos_index = rank_pos_index[-1]
hard_pos_index = pos_idx[hard_pos_index]
rank_neg_index = dist[i][neg_idx].argsort()
hard_neg_index = rank_neg_index[0]
hard_neg_index = neg_idx[hard_neg_index]
dist_ap.append(dist[i][hard_pos_index].unsqueeze(0))
dist_an.append(dist[i][hard_neg_index].unsqueeze(0))
if prob[hard_pos_index] >= threshold and prob[hard_neg_index] >= threshold:
# TP-TN
pass
elif prob[hard_pos_index] >= threshold and prob[hard_neg_index] < threshold:
is_FN = (torch.argmax(prediction[hard_neg_index]) == targets[i])
# TP-FN
if is_FN:
tmp = rank_neg_index[1]
hard_neg_index_new = neg_idx[tmp]
j = 1
loop_cnt = 0
while prob[hard_neg_index_new] < threshold:
j += 1
tmp = rank_neg_index[j]
hard_neg_index_new = neg_idx[tmp]
loop_cnt += 1
if loop_cnt >= 10:
# print("------------warning, break the death loop---------------")
break
dist_ap[cnt] = weighty_new(dist[i][hard_pos_index].unsqueeze(0), dist[i][hard_neg_index].unsqueeze(0), pair_type=1, op_type=self.op_type)
dist_an[cnt] = dist[i][hard_neg_index_new].unsqueeze(0)
# TP-TN
else:
pass
elif prob[hard_pos_index] < threshold and prob[hard_neg_index] >= threshold:
# FP-TN
tmp = rank_pos_index[-2]
hard_pos_index_new = pos_idx[tmp]
j = 2
loop_cnt = 0
while prob[hard_pos_index_new] < threshold:
j += 1
tmp = rank_pos_index[-j]
hard_pos_index_new = pos_idx[tmp]
loop_cnt += 1
if loop_cnt >= 5:
# print("------------warning, break the death loop---------------")
break
dist_an[cnt] = weighty_new(dist[i][hard_pos_index].unsqueeze(0), dist[i][hard_neg_index].unsqueeze(0), pair_type=0, op_type=self.op_type)
dist_ap[cnt] = dist[i][hard_pos_index_new].unsqueeze(0)
elif prob[hard_pos_index] < threshold and prob[hard_neg_index] < threshold:
is_FN = (torch.argmax(prediction[hard_neg_index]) == targets[i])
# FP-FN
if is_FN:
loss_inverse = True
# FP-TN
else:
tmp = rank_pos_index[-2]
hard_pos_index_new = pos_idx[tmp]
j = 2
loop_cnt = 0
while prob[hard_pos_index_new] < threshold:
j += 1
tmp = rank_pos_index[-j]
hard_pos_index_new = pos_idx[tmp]
loop_cnt += 1
if loop_cnt >= 5:
# print("------------warning, break the death loop---------------")
break
dist_an[cnt] = weighty_new(dist[i][hard_pos_index].unsqueeze(0), dist[i][hard_neg_index].unsqueeze(0), pair_type=0, op_type=self.op_type)
dist_ap[cnt] = dist[i][hard_pos_index_new].unsqueeze(0)
if loss_inverse:
loss += torch.clamp(dist_an[cnt] - dist_ap[cnt] + self.margin, 0)
else:
loss += torch.clamp(dist_ap[cnt] - dist_an[cnt] + self.margin, 0)
cnt += 1
loss_inverse = False
else:
continue
# compute accuracy
if cnt == 0:
return torch.Tensor([0.]).to(inputs.device), 0, cnt
else:
dist_ap = torch.cat(dist_ap)
dist_an = torch.cat(dist_an)
correct = torch.ge(dist_an, dist_ap).sum().item()
return loss / cnt, correct, cnt
def weighty_new(d1, d2, pair_type, op_type):
if op_type == 'mean':
return (d1 + d2) / 2.
elif op_type == 'max':
return max(d1, d2)
elif op_type == 'min':
return min(d1, d2)
elif op_type == 'max-min':
if pair_type == 1: # pos
return max(d1, d2)
elif pair_type == 0: # neg
return min(d1, d2)
elif op_type == 'weighty':
if pair_type == 1: # pos
exp_sum = torch.exp(d1) + torch.exp(d2)
d_sum = (torch.exp(d1) * d1 + torch.exp(d2) * d2) / exp_sum
elif pair_type == 0: # neg
exp_sum = torch.exp(-d1) + torch.exp(-d2) + 1e-6 # avoid division by zero
d_sum = (torch.exp(-d1) * d1 + torch.exp(-d2) * d2) / exp_sum
return d_sum