forked from damo-cv/TransReID
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriplet_loss.py
309 lines (265 loc) · 11.4 KB
/
triplet_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
import torch
from torch import nn
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
def euclidean_dist(x, y):
"""
Args:
x: pytorch Variable, with shape [m, d]
y: pytorch Variable, with shape [n, d]
Returns:
dist: pytorch Variable, with shape [m, n]
"""
m, n = x.size(0), y.size(0)
xx = torch.pow(x, 2).sum(1, keepdim=True).expand(m, n)
yy = torch.pow(y, 2).sum(1, keepdim=True).expand(n, m).t()
dist = xx + yy
dist = dist - 2 * torch.matmul(x, y.t())
# dist.addmm_(1, -2, x, y.t())
dist = dist.clamp(min=1e-12).sqrt() # for numerical stability
return dist
def cosine_dist(x, y):
"""
Args:
x: pytorch Variable, with shape [m, d]
y: pytorch Variable, with shape [n, d]
Returns:
dist: pytorch Variable, with shape [m, n]
"""
m, n = x.size(0), y.size(0)
# print(x.shape, y.shape)
x_norm = torch.pow(x, 2).sum(1, keepdim=True).sqrt().expand(m, n)
y_norm = torch.pow(y, 2).sum(1, keepdim=True).sqrt().expand(n, m).t()
xy_intersection = torch.mm(x, y.t())
dist = xy_intersection/(x_norm * y_norm)
dist = (1. - dist) / 2
return dist
def hard_example_mining(dist_mat, labels, return_inds=False):
"""For each anchor, find the hardest positive and negative sample.
Args:
dist_mat: pytorch Variable, pair wise distance between samples, shape [N, N]
labels: pytorch LongTensor, with shape [N]
return_inds: whether to return the indices. Save time if `False`(?)
Returns:
dist_ap: pytorch Variable, distance(anchor, positive); shape [N]
dist_an: pytorch Variable, distance(anchor, negative); shape [N]
p_inds: pytorch LongTensor, with shape [N];
indices of selected hard positive samples; 0 <= p_inds[i] <= N - 1
n_inds: pytorch LongTensor, with shape [N];
indices of selected hard negative samples; 0 <= n_inds[i] <= N - 1
NOTE: Only consider the case in which all labels have same num of samples,
thus we can cope with all anchors in parallel.
"""
assert len(dist_mat.size()) == 2
assert dist_mat.size(0) == dist_mat.size(1)
N = dist_mat.size(0)
# shape [N, N]
is_pos = labels.expand(N, N).eq(labels.expand(N, N).t())
is_neg = labels.expand(N, N).ne(labels.expand(N, N).t())
# print(dist_mat[is_pos].contiguous().shape)
# `dist_ap` means distance(anchor, positive)
# both `dist_ap` and `relative_p_inds` with shape [N, 1]
dist_ap, relative_p_inds = torch.max(
dist_mat[is_pos].contiguous().view(N, -1), 1, keepdim=True)
# print(dist_mat[is_pos].shape)
# `dist_an` means distance(anchor, negative)
# both `dist_an` and `relative_n_inds` with shape [N, 1]
dist_an, relative_n_inds = torch.min(
dist_mat[is_neg].contiguous().view(N, -1), 1, keepdim=True)
# shape [N]
dist_ap = dist_ap.squeeze(1)
dist_an = dist_an.squeeze(1)
if return_inds:
# shape [N, N]
ind = (labels.new().resize_as_(labels)
.copy_(torch.arange(0, N).long())
.unsqueeze(0).expand(N, N))
# shape [N, 1]
p_inds = torch.gather(
ind[is_pos].contiguous().view(N, -1), 1, relative_p_inds.data)
n_inds = torch.gather(
ind[is_neg].contiguous().view(N, -1), 1, relative_n_inds.data)
# shape [N]
p_inds = p_inds.squeeze(1)
n_inds = n_inds.squeeze(1)
return dist_ap, dist_an, p_inds, n_inds
return dist_ap, dist_an
def hard_example_mining_FCD(dist_mat, ref_dist_mat, return_inds=False):
"""For each anchor, find the hardest positive and negative sample.
Args:
dist_mat: pytorch Variable, pair wise distance between samples, shape [N, M]
ref_dist_mat: pytorch Variable, pair wise distance between samples, shape [N, M]
return_inds: whether to return the indices. Save time if `False`(?)
Returns:
dist_an: pytorch Variable, distance(anchor, negative); shape [N]
p_inds: pytorch LongTensor, with shape [N];
indices of selected hard positive samples; 0 <= p_inds[i] <= N - 1
n_inds: pytorch LongTensor, with shape [N];
indices of selected hard negative samples; 0 <= n_inds[i] <= N - 1
NOTE: Only consider the case in which all labels have same num of samples,
thus we can cope with all anchors in parallel.
"""
assert len(dist_mat.size()) == 2
assert dist_mat.size(0) == ref_dist_mat.size(0)
assert dist_mat.size(1) == ref_dist_mat.size(1)
N, M = dist_mat.size(0), dist_mat.size(1)
# shape [N, 1]
dist_an = [] #torch.ones(N)
for i in range(N):
# print(dist_mat[i].dtype)
# print(ref_dist_mat[i].dtype)
mat_tmp = dist_mat[i].clone()
# [N, M]
mat_tmp1 = mat_tmp.expand(N, M)
# [N]
delta_mat = torch.sqrt(torch.sum(torch.pow(mat_tmp1 - ref_dist_mat, 2), dim=1))
delta_mat[i] = 0.0
sorted, indices = torch.sort(delta_mat)
dist_an.append(sorted[1])
# `dist_ap` means distance(anchor, positive)
# both `dist_ap` and `relative_p_inds` with shape [N, 1]
# dist_ap, relative_p_inds = torch.max(
# dist_mat[is_pos].contiguous().view(N, -1), 1, keepdim=True)
# print(dist_mat[is_pos].shape)
# `dist_an` means distance(anchor, negative)
# both `dist_an` and `relative_n_inds` with shape [N, 1]
# dist_an, relative_n_inds = torch.min(
# dist_mat[is_neg].contiguous().view(N, -1), 1, keepdim=True)
# shape [N]
# dist_ap = dist_ap.squeeze(1)
dist_an = torch.Tensor(dist_an).unsqueeze(1)
# print("dist_an",dist_an.size())
# if return_inds:
# # shape [N, N]
# ind = (labels.new().resize_as_(labels)
# .copy_(torch.arange(0, N).long())
# .unsqueeze(0).expand(N, N))
# # shape [N, 1]
# p_inds = torch.gather(
# ind[is_pos].contiguous().view(N, -1), 1, relative_p_inds.data)
# n_inds = torch.gather(
# ind[is_neg].contiguous().view(N, -1), 1, relative_n_inds.data)
# # shape [N]
# p_inds = p_inds.squeeze(1)
# n_inds = n_inds.squeeze(1)
# return dist_ap, dist_an, p_inds, n_inds
return dist_an
class TripletLoss(object):
"""
Triplet loss using HARDER example mining,
modified based on original triplet loss using hard example mining
"""
def __init__(self, margin=None, hard_factor=0.0):
self.margin = margin
self.hard_factor = hard_factor
if margin is not None:
self.ranking_loss = nn.MarginRankingLoss(margin=margin)
else:
self.ranking_loss = nn.SoftMarginLoss()
def __call__(self, global_feat, labels, normalize_feature=False):
if normalize_feature:
global_feat = normalize(global_feat, axis=-1)
dist_mat = euclidean_dist(global_feat, global_feat)
# print("dist_mat", dist_mat.shape)
dist_ap, dist_an = hard_example_mining(dist_mat, labels)
dist_ap *= (1.0 + self.hard_factor)
dist_an *= (1.0 - self.hard_factor)
y = dist_an.new().resize_as_(dist_an).fill_(1)
if self.margin is not None:
loss = self.ranking_loss(dist_an, dist_ap, y)
else:
loss = self.ranking_loss(dist_an - dist_ap, y)
return loss, dist_ap, dist_an
class RSDLoss(object):
"""
Relation similarity distillation loss using similarity between two features
"""
def __init__(self, margin=None, hard_factor=0.0):
self.margin = margin
self.hard_factor = hard_factor
def __call__(self, global_feat, ref_global_feat, normalize_feature=False):
# print(global_feat.shape, ref_global_feat.shape)
# print(global_feat, ref_global_feat)
dist_mat = torch.matmul(global_feat, global_feat.t())
ref_dist_mat = torch.matmul(ref_global_feat, ref_global_feat.t())
if normalize_feature:
dist_mat = normalize(dist_mat, axis=-1)
ref_dist_mat = normalize(ref_dist_mat, axis=-1)
# dist_mat = cosine_dist(global_feat, global_feat)
# print("dist_mat", dist_mat.shape)
# ref_dist_mat = cosine_dist(ref_global_feat, ref_global_feat)
# print("ref_dist_mat", dist_mat.shape)
N = dist_mat.size(0)
loss = torch.pow(dist_mat - ref_dist_mat, 2).sum() / (N * N)
return loss
class SimLoss(object):
"""
Sim loss using cosine similarity between two features
"""
def __init__(self, margin=None, hard_factor=0.0):
self.margin = margin
self.hard_factor = hard_factor
# if margin is not None:
# self.ranking_loss = nn.MarginRankingLoss(margin=margin)
# else:
# self.ranking_loss = nn.SoftMarginLoss()
def __call__(self, global_feat, ref_global_feat, normalize_feature=False):
if normalize_feature:
global_feat = normalize(global_feat, axis=-1)
ref_global_feat = normalize(ref_global_feat, axis=-1)
dist_mat = cosine_dist(global_feat, global_feat)
# print("dist_mat", dist_mat.shape)
ref_dist_mat = cosine_dist(ref_global_feat, ref_global_feat)
# print("ref_dist_mat", dist_mat.shape)
N = dist_mat.size(0)
loss = ((dist_mat - ref_dist_mat) * (dist_mat - ref_dist_mat)).sum() / (N * N)
# dist_ap, dist_an = hard_example_mining(dist_mat, labels)
# dist_ap *= (1.0 + self.hard_factor)
# dist_an *= (1.0 - self.hard_factor)
# y = dist_an.new().resize_as_(dist_an).fill_(1)
# if self.margin is not None:
# loss = self.ranking_loss(dist_an, dist_ap, y)
# else:
# loss = self.ranking_loss(dist_an - dist_ap, y)
return loss
class FCDistLoss(object):
"""
Feature Consistent distillation loss using HARDER example mining,
modified based on original triplet loss using hard example mining
"""
def __init__(self, margin=0.3, hard_factor=0.0):
self.margin = margin
self.hard_factor = hard_factor
if margin is not None:
self.ranking_loss = nn.MarginRankingLoss(margin=margin)
else:
self.ranking_loss = nn.SoftMarginLoss()
def __call__(self, global_feat, ref_global_feat, normalize_feature=False):
if normalize_feature:
global_feat = normalize(global_feat, axis=-1)
ref_global_feat = normalize(ref_global_feat, axis=-1)
# dist_mat = euclidean_dist(global_feat, global_feat)
dist_an = hard_example_mining_FCD(global_feat, ref_global_feat)
N = global_feat.size(0)
M = global_feat.size(1)
# print(N, global_feat.size(), ref_global_feat.shape)
# print(global_feat.device, ref_global_feat.device, dist_an.device)
loss = self.margin + torch.sqrt(torch.pow(global_feat-ref_global_feat, 2)) - dist_an.cuda()
tmp = torch.zeros(N,M).cuda()
loss = torch.where(loss < 0, tmp, loss)
loss = loss.sum() / N
# dist_ap *= (1.0 + self.hard_factor)
# dist_an *= (1.0 - self.hard_factor)
# y = dist_an.new().resize_as_(dist_an).fill_(1)
# if self.margin is not None:
# loss = self.ranking_loss(dist_an, dist_ap, y)
# else:
# loss = self.ranking_loss(dist_an - dist_ap, y)
return loss, dist_an