-
Notifications
You must be signed in to change notification settings - Fork 4
/
loss.py
399 lines (323 loc) · 12.9 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
# coding=utf-8
import torch
import torch.nn as nn
import numpy as np
import torch.nn.functional as F
def l2norm(X, eps=1e-13, dim=1):
"""L2-normalize columns of X
"""
norm = torch.pow(X, 2).sum(dim=dim, keepdim=True).sqrt() + eps + 1e-14
X = torch.div(X, norm)
return X
def l1norm(X, eps=1e-13, dim=1):
"""L2-normalize columns of X
"""
norm = torch.abs(X).sum(dim=dim, keepdim=True) + eps + 1e-14
X = torch.div(X, norm)
return X
def normalization(X, dim=1):
# 按行归一化
_range = np.max(X) - np.min(X)
return (X - np.min(X)) / _range
def cosine_sim(query, retrio):
"""Cosine similarity between all the query and retrio pairs
"""
query, retrio = l2norm(query), l2norm(retrio)
return query.mm(retrio.t())
def vector_cosine_sim(query, retrio):
"""Cosine similarity between the query and retrio pairs
"""
query, retrio = l2norm(query), l2norm(retrio)
return torch.sum(torch.mul(query, retrio),dim=1).unsqueeze(0)
def hist_sim(im, s, eps=1e-14):
bs = im.size(0)
im = im.unsqueeze(1).expand(-1, bs, -1)
s = s.unsqueeze(0).expand(bs, -1, -1)
intersection = torch.min(im, s).sum(-1)
union = torch.max(im, s).sum(-1) + eps
score = intersection / union
return score
def jaccard_sim(query, retrieval_base, eps=1e-8):
score = None
base_num = retrieval_base.size(0)
for each in query:
each = each.unsqueeze(0).repeat(base_num, 1)
intersection = torch.min(each, retrieval_base).sum(-1)
union = torch.max(each, retrieval_base).sum(-1) + eps
score_temp = (intersection / union).unsqueeze(0)
if score is None:
score = score_temp
else:
score = torch.cat((score, score_temp), dim=0)
return score
class MarginRankingLoss(nn.Module):
"""
Compute margin ranking loss
arg input: (batchsize, subspace) and (batchsize, subspace)
"""
def __init__(self, margin=0, measure='cosine', max_violation=False,
cost_style='sum', direction='bidir', device=torch.device('cpu')):
"""
:param margin:
:param measure: cosine 余弦相似度, hist_sim 扩展 jaccard 相似度
:param max_violation:
:param cost_style: 把所有误差相加 sum,还是取平均值 mean
:param direction: compare every diagonal score to scores in its column and row
"""
super(MarginRankingLoss, self).__init__()
self.margin = margin
self.cost_style = cost_style
self.direction = direction
if measure == 'cosine':
self.sim = cosine_sim
elif measure == 'hist':
self.sim = hist_sim
else:
raise Exception('Not implemented.')
self.max_violation = max_violation
def forward(self, s, im):
device = s.device
# compute image-sentence score matrix
scores = self.sim(im, s) #
diagonal = scores.diag().view(im.size(0), 1)
d1 = diagonal.expand_as(scores) # 扩展维度
d2 = diagonal.t().expand_as(scores)
# clear diagonals
I = torch.eye(scores.size(0)) > .5
I = I.to(device)
cost_s = None
cost_im = None
# compare every diagonal score to scores in its column
if self.direction in ['i2t', 'bidir']:
# caption retrieval
cost_s = (self.margin + scores - d1).clamp(min=0) # clamp 最大最小裁剪
cost_s = cost_s.masked_fill_(I, 0)
# compare every diagonal score to scores in its row
if self.direction in ['t2i', 'bidir']:
# image retrieval
cost_im = (self.margin + scores - d2).clamp(min=0)
cost_im = cost_im.masked_fill_(I, 0)
# keep the maximum violating negative for each query
if self.max_violation:
if cost_s is not None:
cost_s = cost_s.max(1)[0]
if cost_im is not None:
cost_im = cost_im.max(0)[0]
if cost_s is None:
cost_s = torch.zeros(1).to(device)
if cost_im is None:
cost_im = torch.zeros(1).to(device)
if self.cost_style == 'sum':
return cost_s.sum() + cost_im.sum()
else:
return cost_s.mean() + cost_im.mean()
class MarginRankingLossWithScore(nn.Module):
"""
Compute margin ranking loss
arg input: (batchsize, subspace) and (batchsize, subspace)
"""
def __init__(self, margin=0, max_violation=False,
cost_style='sum', direction='bidir', device=torch.device('cpu')):
"""
:param margin:
:param measure: cosine 余弦相似度, hist_sim 扩展 jaccard 相似度
:param max_violation:
:param cost_style: 把所有误差相加 sum,还是取平均值 mean
:param direction: compare every diagonal score to scores in its column and row
"""
super().__init__()
self.margin = margin
self.cost_style = cost_style
self.direction = direction
self.max_violation = max_violation
self.device = device
def forward(self, score):
device = self.device
diagonal = score.diag().view(score.size(0), 1)
d1 = diagonal.expand_as(score) # 扩展维度
d2 = diagonal.t().expand_as(score)
# clear diagonals
I = torch.eye(score.size(0)) > .5
I = I.to(device)
cost_s = None
cost_im = None
# compare every diagonal score to scores in its column
if self.direction in ['i2t', 'bidir']:
# caption retrieval
cost_s = (self.margin + score - d1).clamp(min=0) # clamp 最大最小裁剪
cost_s = cost_s.masked_fill_(I, 0)
# compare every diagonal score to scores in its row
if self.direction in ['t2i', 'bidir']:
# image retrieval
cost_im = (self.margin + score - d2).clamp(min=0)
cost_im = cost_im.masked_fill_(I, 0)
# keep the maximum violating negative for each query
if self.max_violation:
if cost_s is not None:
cost_s = cost_s.max(1)[0]
if cost_im is not None:
cost_im = cost_im.max(0)[0]
if cost_s is None:
cost_s = torch.zeros(1).to(device)
if cost_im is None:
cost_im = torch.zeros(1).to(device)
if self.cost_style == 'sum':
return cost_s.sum() + cost_im.sum()
else:
return cost_s.mean() + cost_im.mean()
class ImprovedBCELoss(nn.Module):
def __init__(self, lambda_):
super(ImprovedBCELoss, self).__init__()
self.L = lambda_
def forward(self, s, im):
astype = torch.float
im = im.type(astype)
s = s.type(astype)
weight_1 = self.L / torch.sum(im, dim=1, keepdim=True, dtype=astype) * im
weight_2 = (1 - self.L) / torch.sum(1-im, dim=1, keepdim=True, dtype=astype) * (1-im)
weight_1[weight_1 != weight_1] = 0 # NaN -> 0
weight_2[weight_2 != weight_2] = 0
res1 = torch.nn.functional.binary_cross_entropy_with_logits(s, im, weight=weight_1, reduction='sum')
res2 = torch.nn.functional.binary_cross_entropy_with_logits(s, im, weight=weight_2, reduction='sum')
return res1 + res2
class MarginLoss(nn.Module):
"""
Compute margin loss
arg input: (batchsize, subspace) and (batchsize, subspace)
"""
def __init__(self, neg_weight=1, margin=0, measure='cosine', cost_style='sum',
device=torch.device('cpu'), pos_weight=300):
"""
:param margin:
:param measure: cosine 余弦相似度, hist_sim 扩展 jaccard 相似度
:param max_violation:
:param cost_style: 把所有误差相加 sum,还是取平均值 mean
:param direction: compare every diagonal score to scores in its column and row
"""
super(MarginLoss, self).__init__()
self.margin = 0
self.cost_style = cost_style
if measure == 'cosine':
self.sim = vector_cosine_sim
elif measure == 'hist':
self.sim = hist_sim
else:
raise Exception('Not implemented.')
self.device = device
self.neg_weight = neg_weight
def forward(self, txt_embs, vis_embs, false_txt_embs, weight):
device = self.device
# compute image-sentence score matrix
scorest = self.sim(txt_embs, vis_embs)
weight = weight * (self.neg_weight - 1) + 1
scoresf = self.sim(false_txt_embs, vis_embs)
cost = (self.margin + scoresf - scorest).clamp(min=0)
cost = torch.mul(cost, weight).to(device)
if self.cost_style == 'sum':
return cost.sum()
else:
return cost.mean()
class CrossEntropyLoss(nn.Module):
def __init__(self, ):
super(CrossEntropyLoss, self).__init__()
def forward(self, s, im, temp=1000):
sim_matrix1 = cosine_sim(s, im)
sim_matrix2 = sim_matrix1.T
loss1 = self.cal_loss(sim_matrix1, temp)
loss2 = self.cal_loss(sim_matrix2, temp)
return (loss1 + loss2) / 2
def cal_loss(self, sim_matrix):
logpt = torch.diag(sim_matrix)
logpt = torch.diag(logpt)
loss = -logpt
loss = loss.sum()
return loss
class DualSoftmaxLoss(nn.Module):
def __init__(self, ):
super(DualSoftmaxLoss, self).__init__()
def forward(self, s, im, temp=1000):
sim_matrix1 = cosine_sim(s, im)
sim_matrix2 = sim_matrix1.T
loss1 = self.cal_loss(sim_matrix1, temp)
loss2 = self.cal_loss(sim_matrix2, temp)
return (loss1 + loss2) / 2
def cal_loss(self, sim_matrix, temp=1000):
sim_matrix = sim_matrix * F.softmax(sim_matrix / temp, dim=0) * len(
sim_matrix) # With an appropriate temperature parameter, the model achieves higher performance
logpt = F.log_softmax(sim_matrix, dim=-1)
logpt = torch.diag(logpt)
loss = -logpt
loss = loss.sum()
return loss
class KlLoss(nn.Module):
def __init__(self, cost_style='sum', direction='bidir', device=torch.device('cpu')):
super().__init__()
self.cost_style = cost_style
self.direction = direction
self.klloss= nn.KLDivLoss(reduction='none')
self.device = device
self.softmax = nn.Softmax(dim=1)
self.logsoftmax=nn.LogSoftmax(dim=1)
def forward(self,score,originscore):
losst2i = None
# compare every diagonal score to scores in its row
if self.direction in ['t2i', 'bidir']:
# image retrieval
originsimt2i = self.softmax(originscore)
simt2i=self.logsoftmax(score)
losst2i=self.klloss(simt2i,originsimt2i)
if self.cost_style == 'sum':
return losst2i.sum()
else:
return losst2i.mean()
class Margin2Loss(nn.Module):
"""
Compute margin loss
arg input: (batchsize, subspace) and (batchsize, subspace)
"""
def __init__(self, bottommargin,uppermargin, bottommargin_t2t,uppermargin_t2t, neg_weight=1, measure='cosine', cost_style='sum',
device=torch.device('cpu'), pos_weight=300):
"""
:param margin:
:param measure: cosine 余弦相似度, hist_sim 扩展 jaccard 相似度
:param max_violation:
:param cost_style: 把所有误差相加 sum,还是取平均值 mean
:param direction: compare every diagonal score to scores in its column and row
"""
super(Margin2Loss, self).__init__()
self.uppermargin = uppermargin
self.bottommargin=bottommargin
self.uppermargin_t2t = uppermargin_t2t
self.bottommargin_t2t=bottommargin_t2t
self.cost_style = cost_style
if measure == 'cosine':
self.sim = vector_cosine_sim
elif measure == 'hist':
self.sim = hist_sim
else:
raise Exception('Not implemented.')
self.device = device
self.neg_weight = neg_weight
def forward(self, txt_embs, vis_embs, false_txt_embs, weight):
device = self.device
# compute image-sentence score matrix
scorest = self.sim(txt_embs, vis_embs)
weight = weight * (self.neg_weight - 1) + 1
scoresf = self.sim(false_txt_embs, vis_embs)
scoresf2 = self.sim(false_txt_embs, txt_embs)
cost=0
if self.bottommargin is not None:
cost_b = (self.bottommargin + scoresf - scorest).clamp(min=0)
cost=cost+cost_b
if self.uppermargin is not None:
cost_u = (-self.uppermargin - scoresf +scorest).clamp(min=0)
cost=cost+cost_u
if self.bottommargin_t2t is not None:
cost += (self.bottommargin_t2t + scoresf2 - scorest).clamp(min=0)
if self.uppermargin_t2t is not None:
cost+= (-self.uppermargin_t2t- scoresf2 +scorest).clamp(min=0)
cost = torch.mul(cost, weight).to(device)
if self.cost_style == 'sum':
return cost.sum()
else:
return cost.mean()