-
Notifications
You must be signed in to change notification settings - Fork 0
/
losses.py
455 lines (354 loc) · 17.8 KB
/
losses.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
import random
from pytorch_metric_learning import miners, losses
def binarize(T, nb_classes):
T = T.cpu().numpy()
import sklearn.preprocessing
T = sklearn.preprocessing.label_binarize(
T, classes = range(0, nb_classes)
)
T = torch.FloatTensor(T).cuda()
return T
def l2_norm(input):
input_size = input.size()
buffer = torch.pow(input, 2)
normp = torch.sum(buffer, 1).add_(1e-12)
norm = torch.sqrt(normp)
_output = torch.div(input, norm.view(-1, 1).expand_as(input))
output = _output.view(input_size)
return output
'''
# This is the official ProxyAnchor loss.
class Proxy_Anchor(torch.nn.Module):
def __init__(self, nb_classes, sz_embed, mrg = 0.1, alpha = 32):
torch.nn.Module.__init__(self)
# Proxy Anchor Initialization
self.proxies = torch.nn.Parameter(torch.randn(nb_classes, sz_embed).cuda())
nn.init.kaiming_normal_(self.proxies, mode='fan_out')
#self.A = torch.nn.Parameter(torch.randn(180, 180).cuda())
#nn.init.kaiming_normal_(self.A, mode='fan_out')
self.nb_classes = nb_classes
self.sz_embed = sz_embed
self.mrg = mrg
self.alpha = alpha
def forward(self, X, T):
P = self.proxies
# P.shape = torch.Size([100, 512]), because proxies=100 and embedding_size=512
# this is the same as doing l2_norm(X).matmul(l2_norm(P).T)
cos_batch_cnt = F.linear(l2_norm(X), l2_norm(P)) # Calcluate cosine similarity
#cos = F.linear(l2_norm(X), l2_norm(X))
#print(self.A)
#print(self.A[0])
#temp = cos_batch_cnt.T.matmul(self.A)
cos_cnt_cnt = cos_batch_cnt.T.matmul(cos_batch_cnt)
#import pdb
#pdb.set_trace()
#cos_cnt_cnt = F.linear(l2_norm(P), l2_norm(P))
#print(cos_cnt_cnt[0])
# cos.shape = torch.Size([180, 100]), because batch_size=180 and proxies=100
# the P_one_hot are the labels in one-hot encoding (so they are the positives)
# P_one_hot.shape = torch.Size([180, 100]), because batch_size=180 and labels=100
P_one_hot = binarize(T = T, nb_classes = self.nb_classes)
#P_one_hot = torch.eye(self.nb_classes, self.nb_classes).cuda()
#N_one_hot = torch.ones(self.nb_classes, self.nb_classes).cuda() - torch.eye(self.nb_classes, self.nb_classes).cuda()
N_one_hot_2 = 1 - P_one_hot
#pos_exp = torch.exp(-self.alpha * (cos_batch_cnt - self.mrg))
pos_exp = torch.exp(-self.alpha * (cos_batch_cnt - self.mrg))
#neg_exp = torch.exp(self.alpha * (cos_cnt_cnt + self.mrg))
neg_exp_2 = torch.exp(self.alpha * (cos_batch_cnt + self.mrg))
# this one finds the proxies that have positives in batch
with_pos_proxies = torch.nonzero(P_one_hot.sum(dim = 0) != 0).squeeze(dim = 1) # The set of positive proxies of data in the batch
num_valid_proxies = len(with_pos_proxies) # The number of positive proxies
P_sim_sum = torch.where(P_one_hot == 1, pos_exp, torch.zeros_like(pos_exp)).sum(dim=0)
#N_sim_sum = torch.where(N_one_hot == 1, neg_exp, torch.zeros_like(neg_exp)).sum(dim=0)
N_sim_sum_2 = torch.where(N_one_hot_2 == 1, neg_exp_2, torch.zeros_like(neg_exp_2)).sum(dim=0)
pos_term = torch.log(1 + P_sim_sum).sum() / num_valid_proxies
#neg_term = torch.log(1 + N_sim_sum).sum() / self.nb_classes
neg_term_2 = torch.log(1 + N_sim_sum_2).sum() / self.nb_classes
loss = pos_term + neg_term
#import pdb
#pdb.set_trace()
return loss
'''
#'''
class Proxy_Anchor(torch.nn.Module):
def __init__(self, nb_classes, sz_embed, mrg = 0.1, alpha = 32):
torch.nn.Module.__init__(self)
# Proxy Anchor Initialization
self.proxies = torch.nn.Parameter(torch.randn(nb_classes, sz_embed).cuda())
nn.init.kaiming_normal_(self.proxies, mode='fan_out')
#self.A = torch.nn.Parameter(torch.randn(180, 180).cuda())
#nn.init.kaiming_normal_(self.A, mode='fan_out')
self.nb_classes = nb_classes
self.sz_embed = sz_embed
self.mrg = mrg
self.alpha = alpha
def forward(self, X, T):
P = self.proxies
# P.shape = torch.Size([100, 512]), because proxies=100 and embedding_size=512
# this is the same as doing l2_norm(X).matmul(l2_norm(P).T)
cos_batch_cnt = F.linear(l2_norm(X), l2_norm(P)) # Calcluate cosine similarity
#cos = F.linear(l2_norm(X), l2_norm(X))
#print(self.A)
#print(self.A[0])
#temp = cos_batch_cnt.T.matmul(self.A)
cos_cnt_cnt = cos_batch_cnt.T.matmul(cos_batch_cnt)
#import pdb
#pdb.set_trace()
#cos_cnt_cnt = F.linear(l2_norm(P), l2_norm(P))
#print(cos_cnt_cnt[0])
# cos.shape = torch.Size([180, 100]), because batch_size=180 and proxies=100
# the P_one_hot are the labels in one-hot encoding (so they are the positives)
# P_one_hot.shape = torch.Size([180, 100]), because batch_size=180 and labels=100
P_one_hot = binarize(T = T, nb_classes = self.nb_classes)
#P_one_hot = torch.eye(self.nb_classes, self.nb_classes).cuda()
N_one_hot = torch.ones(self.nb_classes, self.nb_classes).cuda() - torch.eye(self.nb_classes, self.nb_classes).cuda()
#N_one_hot_2 = 1 - P_one_hot
#pos_exp = torch.exp(-self.alpha * (cos_batch_cnt - self.mrg))
pos_exp = torch.exp(-self.alpha * (cos_batch_cnt - self.mrg))
neg_exp = torch.exp(self.alpha * (cos_cnt_cnt + self.mrg))
#neg_exp_2 = torch.exp(self.alpha * (cos_batch_cnt + self.mrg))
# this one finds the proxies that have positives in batch
with_pos_proxies = torch.nonzero(P_one_hot.sum(dim = 0) != 0).squeeze(dim = 1) # The set of positive proxies of data in the batch
num_valid_proxies = len(with_pos_proxies) # The number of positive proxies
P_sim_sum = torch.where(P_one_hot == 1, pos_exp, torch.zeros_like(pos_exp)).sum(dim=0)
N_sim_sum = torch.where(N_one_hot == 1, neg_exp, torch.zeros_like(neg_exp)).sum(dim=0)
#N_sim_sum_2 = torch.where(N_one_hot_2 == 1, neg_exp_2, torch.zeros_like(neg_exp_2)).sum(dim=0)
pos_term = torch.log(1 + P_sim_sum).sum() / num_valid_proxies
neg_term = torch.log(1 + N_sim_sum).sum() / self.nb_classes
#neg_term_2 = torch.log(1 + N_sim_sum_2).sum() / self.nb_classes
loss = pos_term + neg_term
#import pdb
#pdb.set_trace()
return loss
#'''
'''
class Proxy_Anchor(torch.nn.Module):
def __init__(self, nb_classes, sz_embed, mrg = 0.1, alpha = 32):
torch.nn.Module.__init__(self)
# Proxy Anchor Initialization
self.proxies = torch.nn.Parameter(torch.randn(nb_classes, sz_embed).cuda())
nn.init.kaiming_normal_(self.proxies, mode='fan_out')
#with torch.no_grad():
# self.proxies = torch.load('proxies/proxies.pt')
self.nb_classes = nb_classes
self.sz_embed = sz_embed
self.mrg = mrg
self.alpha = torch.nn.Parameter(torch.tensor(1.0).cuda(), requires_grad=True)
self.beta = torch.nn.Parameter(torch.tensor(1.0).cuda(), requires_grad=True)
def forward(self, X, T):
P = self.proxies
with open('alphas.csv', 'a') as file:
file.write(str(float(self.alpha)) + '\n')
# P.shape = torch.Size([100, 512]), because proxies=100 and embedding_size=512
#import pdb
#pdb.set_trace()
# this is the same as doing l2_norm(X).matmul(l2_norm(P).T)
cos = F.linear(l2_norm(X), l2_norm(P)) # Calcluate cosine similarity
# cos.shape = torch.Size([180, 100]), because batch_size=180 and proxies=100
# the P_one_hot are the labels in one-hot encoding (so they are the positives)
# P_one_hot.shape = torch.Size([180, 100]), because batch_size=180 and labels=100
P_one_hot = binarize(T = T, nb_classes = self.nb_classes)
N_one_hot = 1 - P_one_hot
pos_exp = torch.exp(-self.alpha * (cos - self.mrg))
#neg_exp = torch.exp(self.alpha * (cos + self.mrg))
neg_exp = torch.exp(self.beta * (cos + self.mrg))
# this one finds the proxies that have positives in batch
with_pos_proxies = torch.nonzero(P_one_hot.sum(dim = 0) != 0).squeeze(dim = 1) # The set of positive proxies of data in the batch
num_valid_proxies = len(with_pos_proxies) # The number of positive proxies
P_sim_sum = torch.where(P_one_hot == 1, pos_exp, torch.zeros_like(pos_exp)).sum(dim=0)
N_sim_sum = torch.where(N_one_hot == 1, neg_exp, torch.zeros_like(neg_exp)).sum(dim=0)
pos_term = torch.log(1 + P_sim_sum).sum() / num_valid_proxies
neg_term = torch.log(1 + N_sim_sum).sum() / self.nb_classes
loss = pos_term + neg_term
#loss = neg_term
return loss
'''
# We use PyTorch Metric Learning library for the following codes.
# Please refer to "https://github.com/KevinMusgrave/pytorch-metric-learning" for details.
class Proxy_NCA(torch.nn.Module):
def __init__(self, nb_classes, sz_embed, scale=32):
super(Proxy_NCA, self).__init__()
self.nb_classes = nb_classes
self.sz_embed = sz_embed
self.scale = scale
self.loss_func = losses.ProxyNCALoss(num_classes = self.nb_classes, embedding_size = self.sz_embed, softmax_scale = self.scale).cuda()
def forward(self, embeddings, labels):
loss = self.loss_func(embeddings, labels)
return loss
class MultiSimilarityLoss(torch.nn.Module):
def __init__(self, thresh = 0.5, epsilon = 0.1, scale_pos = 2, scale_neg = 50):
super(MultiSimilarityLoss, self).__init__()
#self.thresh = 0.5
#self.epsilon = 0.1
#self.scale_pos = 2
#self.scale_neg = 50
self.thresh = 0.77
self.epsilon = 0.39
self.scale_pos = 17.97
self.scale_neg = 75.66
self.miner = miners.MultiSimilarityMiner(epsilon=self.epsilon)
self.loss_func = losses.MultiSimilarityLoss(self.scale_pos, self.scale_neg, self.thresh)
def forward(self, embeddings, labels):
hard_pairs = self.miner(embeddings, labels)
loss = self.loss_func(embeddings, labels, hard_pairs)
return loss
class ContrastiveLoss(nn.Module):
def __init__(self, margin=0.5, **kwargs):
super(ContrastiveLoss, self).__init__()
self.margin = margin
self.loss_func = losses.ContrastiveLoss(neg_margin=self.margin)
def forward(self, embeddings, labels):
loss = self.loss_func(embeddings, labels)
return loss
class TripletLoss(nn.Module):
def __init__(self, margin=0.1, **kwargs):
super(TripletLoss, self).__init__()
self.margin = margin
self.miner = miners.TripletMarginMiner(margin, type_of_triplets = 'semihard')
self.loss_func = losses.TripletMarginLoss(margin = self.margin)
def forward(self, embeddings, labels):
hard_pairs = self.miner(embeddings, labels)
loss = self.loss_func(embeddings, labels, hard_pairs)
return loss
class NPairLoss(nn.Module):
def __init__(self, l2_reg=0):
super(NPairLoss, self).__init__()
self.l2_reg = l2_reg
self.loss_func = losses.NPairsLoss(l2_reg_weight=self.l2_reg, normalize_embeddings = False)
def forward(self, embeddings, labels):
loss = self.loss_func(embeddings, labels)
return loss
class ArcFaceLoss(nn.Module):
def __init__(self, nb_classes, sz_embed, margin=28.6, scale=220, **kwargs):
super(ArcFaceLoss, self).__init__()
self.nb_classes = nb_classes
self.sz_embed = sz_embed
self.margin = margin
self.scale = scale
self.loss_func = losses.ArcFaceLoss(num_classes = self.nb_classes, embedding_size = self.sz_embed, margin = self.margin, scale = self.scale)
def forward(self, embeddings, labels):
loss = self.loss_func(embeddings, labels)
return loss
'''
class SoftTripleLoss(nn.Module):
def __init__(self, nb_classes, sz_embed, centers_per_class=10, la=20, gamma=0.1, margin=0.01, **kwargs):
super(SoftTripleLoss, self).__init__()
self.nb_classes = nb_classes
self.sz_embed = sz_embed
self.centers_per_class = centers_per_class
self.la = la
self.gamma = gamma
self.margin = margin
self.fc = torch.nn.Parameter(torch.Tensor(self.sz_embed, self.nb_classes * centers_per_class))
#self.weight_init_func(self.fc)
self.loss_func = losses.SoftTripleLoss(num_classes = self.nb_classes, embedding_size = self.sz_embed, centers_per_class = self.centers_per_class, la = self.la, gamma = self.gamma, margin = self.margin)
def forward(self, embeddings, labels):
loss = self.loss_func(embeddings, labels)
return loss
'''
class SoftTripleLoss(nn.Module):
def __init__(self, margin, lamda , gamma, tau, nb_classes, sz_embed):
super(SoftTripleLoss, self).__init__()
# Default values
self.lamda = lamda
self.gamma = gamma
self.tau = tau
self.margin = margin
# Values from searching
#self.margin = 0.4
#self.lamda = 78
#self.gamma = 58
#self.tau = 0.4
self.centers_per_class = 2
self.nb_classes = nb_classes
self.sz_embed = sz_embed
self.fc = torch.nn.parameter.Parameter(torch.Tensor(self.sz_embed, self.nb_classes * self.centers_per_class))
self.weight = torch.zeros(self.nb_classes*self.centers_per_class, self.nb_classes*self.centers_per_class, dtype=torch.bool).cuda()
for i in range(0, self.nb_classes):
for j in range(0, self.centers_per_class):
self.weight[i*self.centers_per_class+j, i*self.centers_per_class+j+1:(i+1)*self.centers_per_class] = 1
torch.nn.init.kaiming_uniform_(self.fc, a=math.sqrt(5))
return
def forward(self, embeddings, labels):
centers = torch.nn.functional.normalize(self.fc, p=2, dim=0)
centers = centers.cuda()
simInd = embeddings.matmul(centers)
simStruc = simInd.reshape(-1, self.nb_classes, self.centers_per_class)
prob = torch.nn.functional.softmax(simStruc*self.gamma, dim=2)
simClass = torch.sum(prob*simStruc, dim=2)
marginM = torch.zeros(simClass.shape).cuda()
marginM[torch.arange(0, marginM.shape[0]), labels] = self.margin
labels = labels.cuda()
loss = torch.nn.functional.cross_entropy(self.lamda*(simClass-marginM), labels)
if self.tau > 0 and self.centers_per_class > 1:
simCenter = centers.t().matmul(centers)
reg = torch.sum(torch.sqrt(2.0+1e-5-2.*simCenter[self.weight]))/(self.nb_classes*self.centers_per_class*(self.centers_per_class-1.))
return loss+self.tau*reg
else:
return loss
'''
class LiftedStructureLoss(nn.Module):
def __init__(self, neg_margin=1, pos_margin=0, **kwargs):
super(LiftedStructureLoss, self).__init__()
self.neg_margin = neg_margin
self.pos_margin = pos_margin
self.loss_func = losses.GeneralizedLiftedStructureLoss(neg_margin = self.neg_margin)
def forward(self, embeddings, labels):
loss = self.loss_func(embeddings, labels)
return loss
'''
class LiftedStructureLoss(nn.Module):
def __init__(self, alpha=40, beta=2, margin=0.5, hard_mining=True, **kwargs):
super(LiftedStructureLoss, self).__init__()
self.margin = margin
self.alpha = alpha
self.beta = beta
self.hard_mining = hard_mining
def forward(self, embeddings, labels):
n = embeddings.size(0)
sim_mat = torch.matmul(embeddings, embeddings.t())
labels = labels
loss = list()
c = 0
for i in range(n):
pos_pair_ = torch.masked_select(sim_mat[i], labels==labels[i])
# move itself
pos_pair_ = torch.masked_select(pos_pair_, pos_pair_ < 1)
neg_pair_ = torch.masked_select(sim_mat[i], labels!=labels[i])
pos_pair_ = torch.sort(pos_pair_)[0]
neg_pair_ = torch.sort(neg_pair_)[0]
if self.hard_mining is not None:
neg_pair = torch.masked_select(neg_pair_, neg_pair_ + 0.1 > pos_pair_[0])
pos_pair = torch.masked_select(pos_pair_, pos_pair_ - 0.1 < neg_pair_[-1])
if len(neg_pair) < 1 or len(pos_pair) < 1:
c += 1
continue
pos_loss = 2.0/self.beta * torch.log(torch.sum(torch.exp(-self.beta*pos_pair)))
neg_loss = 2.0/self.alpha * torch.log(torch.sum(torch.exp(self.alpha*neg_pair)))
else:
pos_pair = pos_pair_
neg_pair = neg_pair_
pos_loss = 2.0/self.beta * torch.log(torch.sum(torch.exp(-self.beta*pos_pair)))
neg_loss = 2.0/self.alpha * torch.log(torch.sum(torch.exp(self.alpha*neg_pair)))
if len(neg_pair) == 0:
c += 1
continue
loss.append(pos_loss + neg_loss)
loss = sum(loss)/n
return loss
class MarginLoss(nn.Module):
def __init__(self, margin=0.2, nu=0, beta=1.2, **kwargs):
super(MarginLoss, self).__init__()
self.margin = margin
self.nu = nu
self.beta = beta
self.miner = miners.DistanceWeightedMiner(cutoff=0.5, nonzero_loss_cutoff=1.4)
self.loss_func = losses.MarginLoss(margin = self.margin, nu = self.nu, beta = self.beta)
def forward(self, embeddings, labels):
distance_weighted_pairs = self.miner(embeddings, labels)
loss = self.loss_func(embeddings, labels, distance_weighted_pairs)
return loss