forked from XiangboYin/DPIS_SSVI-ReID
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloss.py
298 lines (255 loc) · 13 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
import torch
import torch.nn as nn
import numpy as np
def normalize(x, axis=-1):
"""
Normalizing to unit length along the specified dimension.
"""
x = 1. * x / (torch.norm(x, 2, axis, keepdim=True).expand_as(x) + 1e-12)
return x
class TripletLoss(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.
- inputs: feature matrix with shape (batch_size, feat_dim).
- targets: ground truth labels with shape (num_classes).
"""
def __init__(self, margin=0.3):
super(TripletLoss, self).__init__()
self.margin = margin
self.ranking_loss = nn.MarginRankingLoss(margin=margin)
def forward(self, inputs, targets):
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() # torch.eq: greater than or equal to >=
return loss, correct
class PredictionAlignmentLoss(nn.Module):
"""
Proposed loss for Prediction Alignment Learning (PAL).
"""
def __init__(self, lambda_vr=0.1, lambda_rv=0.5):
super(PredictionAlignmentLoss, self).__init__()
self.lambda_vr = lambda_vr
self.lambda_rv = lambda_rv
def forward(self, x_rgb, x_ir):
sim_rgbtoir = torch.mm(normalize(x_rgb), normalize(x_ir).t())
sim_irtorgb = torch.mm(normalize(x_ir), normalize(x_rgb).t())
sim_irtoir = torch.mm(normalize(x_ir), normalize(x_ir).t())
sim_rgbtoir = nn.Softmax(1)(sim_rgbtoir)
sim_irtorgb = nn.Softmax(1)(sim_irtorgb)
sim_irtoir = nn.Softmax(1)(sim_irtoir)
KL_criterion = nn.KLDivLoss(reduction="batchmean")
x_rgbtoir = torch.mm(sim_rgbtoir, x_ir)
x_irtorgb = torch.mm(sim_irtorgb, x_rgb)
x_irtoir = torch.mm(sim_irtoir, x_ir)
x_rgb_s = nn.Softmax(1)(x_rgb)
x_rgbtoir_ls = nn.LogSoftmax(1)(x_rgbtoir)
x_irtorgb_s = nn.Softmax(1)(x_irtorgb)
x_irtoir_ls = nn.LogSoftmax(1)(x_irtoir)
loss_rgbtoir = KL_criterion(x_rgbtoir_ls, x_rgb_s)
loss_irtorgb = KL_criterion(x_irtoir_ls, x_irtorgb_s)
loss = self.lambda_vr * loss_rgbtoir + self.lambda_rv * loss_irtorgb
return loss, sim_rgbtoir, sim_irtorgb
class RobustTripletLoss_final(nn.Module):
def __init__(self, batch_size, margin):
super(RobustTripletLoss_final, self).__init__()
self.batch_size = batch_size
self.margin = margin
# self.T=1#V4
# self.T=0.1#V6
self.T = 0.1 # V4
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
tnt=0
loss_inverse = False
K = 20
for i in range(n):
# print(i)
if is_confident[i]:
# if 0:
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))
endwhile=0
while random_pos_index == i:
endwhile+=1
random_pos_index = int(np.random.choice(pos_idx.cpu().numpy(), 1))
if endwhile>10:
break
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:
# try:
loss += torch.clamp(dist_ap[cnt] - dist_an[cnt] + self.margin, 0)
# except:
# continue
cnt += 1
tnt+=1
loss_inverse = False
# elif epoch<=0:
# continue
else:
cln=0.01
# continue
if i<=31:
ap_dis=dist[i][i+32]
# ap_dis=dist[i][i]
V_nagetive=dist[i][0:32]
I_nagetive = dist[i][64:]
V_an_dis, AV_indices = torch.sort(V_nagetive, dim=0, descending=True)
I_an_dis, AI_indices = torch.sort(I_nagetive, dim=0, descending=True)
V_an_dis = dist[i][AV_indices[0:K]]
I_an_dis = dist[i][AI_indices[0:K]]
V_an_dis=torch.sum(V_an_dis, dim=0)/K
I_an_dis=torch.sum(I_an_dis, dim=0)/K
loss = (torch.clamp(ap_dis - (I_an_dis)+ self.margin, 0)+torch.clamp(ap_dis - (V_an_dis)+ self.margin, 0))/2
elif i>=32 and i<64:
# continue
ap_dis = dist[i][i - 32]
# ap_dis=dist[i][i]
V_nagetive = dist[i][32:64]
I_nagetive = dist[i][64:]
V_an_dis, AV_indices = torch.sort(V_nagetive, dim=0, descending=True)
I_an_dis, AI_indices = torch.sort(I_nagetive, dim=0, descending=True)
V_an_dis = dist[i][AV_indices[0:K]]
I_an_dis = dist[i][AI_indices[0:K]]
V_an_dis = torch.sum(V_an_dis, dim=0)/K
I_an_dis = torch.sum(I_an_dis, dim=0)/K
# loss += torch.clamp(ap_dis - (V_an_dis+ I_an_dis)+ self.margin, 0)
loss = (torch.clamp(ap_dis - (I_an_dis)+ self.margin, 0)+torch.clamp(ap_dis - (V_an_dis)+ self.margin, 0))/2
else:
# continue
ap_dis = dist[i][i]
V_nagetive = dist[i][64:]
an_dis, AV_indices = torch.sort(V_nagetive, dim=0, descending=True)
an_dis = dist[i][AV_indices[0:K]]
an_dis = torch.sum(an_dis, dim=0)/K
loss += torch.clamp(ap_dis - an_dis + self.margin, 0)
# continue
# Anchor = inputs[i]
# Positive = Anchor
# V_nagetive = inputs[:64]
# I_nagetive = torch.cat((inputs[64:i], inputs[i:96]), 0)
# mat_sim_AV = torch.matmul(Anchor, V_nagetive.transpose(0, 1))
# mat_sim_AI = torch.matmul(Anchor, I_nagetive.transpose(0, 1))
# sorted_AV_distance, AV_indices = torch.sort(mat_sim_AV, dim=0, descending=False)
# sorted_AI_distance, AI_indices = torch.sort(mat_sim_AI, dim=0, descending=False)
# # Hard_AV_distance = sorted_AV_distance[0:5]
# # Hard_AI_distance = sorted_AI_distance[0:5]
# Hard_AV_distance = sorted_AV_distance[0:10]
# Hard_AI_distance = sorted_AI_distance[0:10]#6
# Positive_distance = torch.matmul(Anchor, Positive)
# loss_AV = -torch.log((torch.exp(Positive_distance / self.T)) / (
# torch.exp(Positive_distance / self.T) + torch.sum(torch.exp(Hard_AV_distance / self.T),
# dim=0)))
# loss_AI = -torch.log((torch.exp(Positive_distance / self.T)) / (
# torch.exp(Positive_distance / self.T) + torch.sum(torch.exp(Hard_AI_distance / self.T),
# dim=0)))
# loss += (loss_AV+loss_AI)*cln
# tnt += 1
# loss=loss
# print(loss)
tnt += 1
loss=loss.reshape(1,-1)
# cnt += 1
# print('loss_AV:', loss_AV)
# print('loss_AI:', loss_AI)
# 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 / tnt, correct, cnt