-
Notifications
You must be signed in to change notification settings - Fork 16
/
loss.py
256 lines (214 loc) · 9.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
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
class KLLoss(nn.Module):
def __init__(self):
super(KLLoss, 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
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, targets):
"""
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
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.
"""
def __init__(self, batch_size, margin=0.5):
super(TripletLoss, self).__init__()
self.margin = margin
self.ranking_loss = nn.MarginRankingLoss(margin=margin)
self.batch_size = batch_size
self.mask = torch.eye(batch_size)
def forward(self, input, target):
"""
Args:
- input: feature matrix with shape (batch_size, feat_dim)
- target: ground truth labels with shape (num_classes)
"""
n = self.batch_size
input1 = input.narrow(0,0,n)
input2 = input.narrow(0,n,n)
# Compute pairwise distance, replace by the official when merged
dist = pdist_torch(input1, input2)
# For each anchor, find the hardest positive and negative
# mask = target1.expand(n, n).eq(target1.expand(n, n).t())
dist_ap, dist_an = [], []
for i in range(n):
dist_ap.append(dist[i,i].unsqueeze(0))
dist_an.append(dist[i][self.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*2
class BiTripletLoss(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.suffix
"""
def __init__(self, batch_size, margin=0.5):
super(BiTripletLoss, self).__init__()
self.margin = margin
self.ranking_loss = nn.MarginRankingLoss(margin=margin)
self.batch_size = batch_size
self.mask = torch.eye(batch_size)
def forward(self, input, target):
"""
Args:
- input: feature matrix with shape (batch_size, feat_dim)
- target: ground truth labels with shape (num_classes)
"""
n = self.batch_size
input1 = input.narrow(0,0,n)
input2 = input.narrow(0,n,n)
# Compute pairwise distance, replace by the official when merged
dist = pdist_torch(input1, input2)
# For each anchor, find the hardest positive and negative
# mask = target1.expand(n, n).eq(target1.expand(n, n).t())
dist_ap, dist_an = [], []
for i in range(n):
dist_ap.append(dist[i,i].unsqueeze(0))
dist_an.append(dist[i][self.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)
loss1 = self.ranking_loss(dist_an, dist_ap, y)
# compute accuracy
correct1 = torch.ge(dist_an, dist_ap).sum().item()
# Compute pairwise distance, replace by the official when merged
dist2 = pdist_torch(input2, input1)
# For each anchor, find the hardest positive and negative
dist_ap2, dist_an2 = [], []
for i in range(n):
dist_ap2.append(dist2[i,i].unsqueeze(0))
dist_an2.append(dist2[i][self.mask[i] == 0].min().unsqueeze(0))
dist_ap2 = torch.cat(dist_ap2)
dist_an2 = torch.cat(dist_an2)
# Compute ranking hinge loss
y2 = torch.ones_like(dist_an2)
# loss2 = self.ranking_loss(dist_an2, dist_ap2, y2)
loss2 = torch.sum(torch.nn.functional.relu(dist_ap2 + self.margin - dist_an2))
# compute accuracy
correct2 = torch.ge(dist_an2, dist_ap2).sum().item()
loss = torch.add(loss1, loss2)
return loss, correct1 + correct2
class BDTRLoss(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.suffix
"""
def __init__(self, batch_size, margin=0.5):
super(BDTRLoss, self).__init__()
self.margin = margin
self.ranking_loss = nn.MarginRankingLoss(margin=margin)
self.batch_size = batch_size
self.mask = torch.eye(batch_size)
def forward(self, inputs, targets):
"""
Args:
- input: feature matrix with shape (batch_size, feat_dim)
- target: 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)
correct = torch.ge(dist_an, dist_ap).sum().item()
return loss, correct
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