-
Notifications
You must be signed in to change notification settings - Fork 0
/
losses.py
352 lines (317 loc) · 14.2 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
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from torch.autograd import Variable
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
class SupConLoss(nn.Module):
def __init__(self, temperature=0.1, scale_by_temperature=True):
super(SupConLoss, self).__init__()
self.temperature = temperature
self.scale_by_temperature = scale_by_temperature
def forward(self, features, labels=None, mask=None):
"""
输入:
features: 输入样本的特征,尺寸为 [batch_size, hidden_dim].
labels: 每个样本的ground truth标签,尺寸是[batch_size].
mask: 用于对比学习的mask,尺寸为 [batch_size, batch_size], 如果样本i和j属于同一个label,那么mask_{i,j}=1
输出:
loss值
"""
device = (torch.device('cuda')
if features.is_cuda
else torch.device('cpu'))
features = F.normalize(features, p=2, dim=1)
batch_size = features.shape[0]
# 关于labels参数
if labels is not None and mask is not None: # labels和mask不能同时定义值,因为如果有label,那么mask是需要根据Label得到的
raise ValueError('Cannot define both `labels` and `mask`')
elif labels is None and mask is None: # 如果没有labels,也没有mask,就是无监督学习,mask是对角线为1的矩阵,表示(i,i)属于同一类
mask = torch.eye(batch_size, dtype=torch.float32).to(device)
elif labels is not None: # 如果给出了labels, mask根据label得到,两个样本i,j的label相等时,mask_{i,j}=1
labels = labels.contiguous().view(-1, 1)
if labels.shape[0] != batch_size:
raise ValueError('Num of labels does not match num of features')
mask = torch.eq(labels, labels.T).float().to(device)
else:
mask = mask.float().to(device)
'''
示例:
labels:
tensor([[1.],
[2.],
[1.],
[1.]])
mask: # 两个样本i,j的label相等时,mask_{i,j}=1
tensor([[1., 0., 1., 1.],
[0., 1., 0., 0.],
[1., 0., 1., 1.],
[1., 0., 1., 1.]])
'''
# compute logits
anchor_dot_contrast = torch.div(
torch.matmul(features, features.T),
self.temperature) # 计算两两样本间点乘相似度
# for numerical stability
logits_max, _ = torch.max(anchor_dot_contrast, dim=1, keepdim=True)
logits = anchor_dot_contrast - logits_max.detach()
exp_logits = torch.exp(logits)
'''
logits是anchor_dot_contrast减去每一行的最大值得到的最终相似度
示例: logits: torch.size([4,4])
logits:
tensor([[ 0.0000, -0.0471, -0.3352, -0.2156],
[-1.2576, 0.0000, -0.3367, -0.0725],
[-1.3500, -0.1409, -0.1420, 0.0000],
[-1.4312, -0.0776, -0.2009, 0.0000]])
'''
# 构建mask
logits_mask = torch.ones_like(mask).to(device) - torch.eye(batch_size).to(device)
positives_mask = mask * logits_mask
negatives_mask = 1. - mask
'''
但是对于计算Loss而言,(i,i)位置表示样本本身的相似度,对Loss是没用的,所以要mask掉
# 第ind行第ind位置填充为0
得到logits_mask:
tensor([[0., 1., 1., 1.],
[1., 0., 1., 1.],
[1., 1., 0., 1.],
[1., 1., 1., 0.]])
positives_mask:
tensor([[0., 0., 1., 1.],
[0., 0., 0., 0.],
[1., 0., 0., 1.],
[1., 0., 1., 0.]])
negatives_mask:
tensor([[0., 1., 0., 0.],
[1., 0., 1., 1.],
[0., 1., 0., 0.],
[0., 1., 0., 0.]])
'''
num_positives_per_row = torch.sum(positives_mask , axis=1) # 除了自己之外,正样本的个数 [2 0 2 2]
denominator = torch.sum(
exp_logits * negatives_mask, axis=1, keepdims=True) + torch.sum(
exp_logits * positives_mask, axis=1, keepdims=True)
log_probs = logits - torch.log(denominator)
if torch.any(torch.isnan(log_probs)):
raise ValueError("Log_prob has nan!")
log_probs = torch.sum(
log_probs*positives_mask , axis=1)[num_positives_per_row > 0] / num_positives_per_row[num_positives_per_row > 0]
'''
计算正样本平均的log-likelihood
考虑到一个类别可能只有一个样本,就没有正样本了 比如我们labels的第二个类别 labels[1,2,1,1]
所以这里只计算正样本个数>0的
'''
# loss
loss = -log_probs
if self.scale_by_temperature:
loss *= self.temperature
loss = loss.mean()
return loss
###############################
class BinaryFocalLoss(nn.Module):
"""
This is a implementation of Focal Loss with smooth label cross entropy supported which is proposed in
'Focal Loss for Dense Object Detection. (https://arxiv.org/abs/1708.02002)'
Focal_Loss= -1*alpha*(1-pt)*log(pt)
:param alpha: (tensor) 3D or 4D the scalar factor for this criterion
:param gamma: (float,double) gamma > 0 reduces the relative loss for well-classified examples (p>0.5) putting more
focus on hard misclassified example
:param reduction: `none`|`mean`|`sum`
:param **kwargs
balance_index: (int) balance class index, should be specific when alpha is float
"""
def __init__(self, alpha=3, gamma=2, ignore_index=None, reduction='mean', **kwargs):
super(BinaryFocalLoss, self).__init__()
self.alpha = alpha
self.gamma = gamma
self.smooth = 1e-6 # set '1e-4' when train with FP16
self.ignore_index = ignore_index
self.reduction = reduction
assert self.reduction in ['none', 'mean', 'sum']
# if self.alpha is None:
# self.alpha = torch.ones(2)
# elif isinstance(self.alpha, (list, np.ndarray)):
# self.alpha = np.asarray(self.alpha)
# self.alpha = np.reshape(self.alpha, (2))
# assert self.alpha.shape[0] == 2, \
# 'the `alpha` shape is not match the number of class'
# elif isinstance(self.alpha, (float, int)):
# self.alpha = np.asarray([self.alpha, 1.0 - self.alpha], dtype=np.float).view(2)
# else:
# raise TypeError('{} not supported'.format(type(self.alpha)))
def forward(self, output, target):
prob = torch.sigmoid(output)
prob = torch.clamp(prob, self.smooth, 1.0 - self.smooth)
valid_mask = None
if self.ignore_index is not None:
valid_mask = (target != self.ignore_index).float()
pos_mask = (target == 1).float()
neg_mask = (target == 0).float()
if valid_mask is not None:
pos_mask = pos_mask * valid_mask
neg_mask = neg_mask * valid_mask
pos_weight = (pos_mask * torch.pow(1 - prob, self.gamma)).detach()
pos_loss = -pos_weight * torch.log(prob) # / (torch.sum(pos_weight) + 1e-4)
neg_weight = (neg_mask * torch.pow(prob, self.gamma)).detach()
neg_loss = -self.alpha * neg_weight * F.logsigmoid(-output) # / (torch.sum(neg_weight) + 1e-4)
loss = pos_loss + neg_loss
loss = loss.mean()
return loss
class FocalLoss_Ori(nn.Module):
"""
This is a implementation of Focal Loss with smooth label cross entropy supported which is proposed in
'Focal Loss for Dense Object Detection. (https://arxiv.org/abs/1708.02002)'
Focal_Loss= -1*alpha*((1-pt)**gamma)*log(pt)
Args:
num_class: number of classes
alpha: class balance factor
gamma:
ignore_index:
reduction:
"""
def __init__(self, num_class, alpha=None, gamma=2, ignore_index=None, reduction='mean'):
super(FocalLoss_Ori, self).__init__()
self.num_class = num_class
self.gamma = gamma
self.reduction = reduction
self.smooth = 1e-4
self.ignore_index = ignore_index
self.alpha = alpha
if alpha is None:
self.alpha = torch.ones(num_class, )
elif isinstance(alpha, (int, float)):
self.alpha = torch.as_tensor([alpha] * num_class)
elif isinstance(alpha, (list, np.ndarray)):
self.alpha = torch.as_tensor(alpha)
if self.alpha.shape[0] != num_class:
raise RuntimeError('the length not equal to number of class')
# if isinstance(self.alpha, (list, tuple, np.ndarray)):
# assert len(self.alpha) == self.num_class
# self.alpha = torch.Tensor(list(self.alpha))
# elif isinstance(self.alpha, (float, int)):
# assert 0 < self.alpha < 1.0, 'alpha should be in `(0,1)`)'
# assert balance_index > -1
# alpha = torch.ones((self.num_class))
# alpha *= 1 - self.alpha
# alpha[balance_index] = self.alpha
# self.alpha = alpha
# elif isinstance(self.alpha, torch.Tensor):
# self.alpha = self.alpha
# else:
# raise TypeError('Not support alpha type, expect `int|float|list|tuple|torch.Tensor`')
def forward(self, logit, target):
# assert isinstance(self.alpha,torch.Tensor)\
N, C = logit.shape[:2]
alpha = self.alpha.to(logit.device)
prob = F.softmax(logit, dim=1)
if prob.dim() > 2:
# N,C,d1,d2 -> N,C,m (m=d1*d2*...)
prob = prob.view(N, C, -1)
prob = prob.transpose(1, 2).contiguous() # [N,C,d1*d2..] -> [N,d1*d2..,C]
prob = prob.view(-1, prob.size(-1)) # [N,d1*d2..,C]-> [N*d1*d2..,C]
ori_shp = target.shape
target = target.view(-1, 1) # [N,d1,d2,...]->[N*d1*d2*...,1]
valid_mask = None
if self.ignore_index is not None:
valid_mask = target != self.ignore_index
target = target * valid_mask
# ----------memory saving way--------
prob = prob.gather(1, target).view(-1) + self.smooth # avoid nan
logpt = torch.log(prob)
# alpha_class = alpha.gather(0, target.view(-1))
alpha_class = alpha[target.squeeze().long()]
class_weight = -alpha_class * torch.pow(torch.sub(1.0, prob), self.gamma)
loss = class_weight * logpt
if valid_mask is not None:
loss = loss * valid_mask.squeeze()
if self.reduction == 'mean':
loss = loss.mean()
if valid_mask is not None:
loss = loss.sum() / valid_mask.sum()
elif self.reduction == 'none':
loss = loss.view(ori_shp)
return loss
###############################
def make_one_hot(input, num_classes):
"""Convert class index tensor to one hot encoding tensor.
Args:
input: A tensor of shape [N, 1, *]
num_classes: An int of number of class
Returns:
A tensor of shape [N, num_classes, *]
"""
input = torch.unsqueeze(input, dim=1)
shape = np.array(input.shape)
shape[1] = num_classes
shape = tuple(shape)
result = torch.zeros(shape).cuda()
result = result.scatter_(1, input, 1)
return result
class BinaryDiceLoss(nn.Module):
"""Dice loss of binary class
Args:
smooth: A float number to smooth loss, and avoid NaN error, default: 1
p: Denominator value: \sum{x^p} + \sum{y^p}, default: 2
predict: A tensor of shape [N, *]
target: A tensor of shape same with predict
reduction: Reduction method to apply, return mean over batch if 'mean',
return sum if 'sum', return a tensor of shape [N,] if 'none'
Returns:
Loss tensor according to arg reduction
Raise:
Exception if unexpected reduction
"""
def __init__(self, smooth=1, p=2, reduction='mean'):
super(BinaryDiceLoss, self).__init__()
self.smooth = smooth
self.p = p
self.reduction = reduction
def forward(self, predict, target):
assert predict.shape[0] == target.shape[0], "predict & target batch size don't match"
predict = predict.contiguous().view(predict.shape[0], -1)
target = target.contiguous().view(target.shape[0], -1)
num = torch.sum(torch.mul(predict, target), dim=1) + self.smooth
den = torch.sum(predict.pow(self.p) + target.pow(self.p), dim=1) + self.smooth
loss = 1 - num / den
if self.reduction == 'mean':
return loss.mean()
elif self.reduction == 'sum':
return loss.sum()
elif self.reduction == 'none':
return loss
else:
raise Exception('Unexpected reduction {}'.format(self.reduction))
class DiceLoss(nn.Module):
"""Dice loss, need one hot encode input
Args:
weight: An array of shape [num_classes,]
ignore_index: class index to ignore
predict: A tensor of shape [N, C, *]
target: A tensor of same shape with predict
other args pass to BinaryDiceLoss
Return:
same as BinaryDiceLoss
"""
def __init__(self, weight=None, ignore_index=None, **kwargs):
super(DiceLoss, self).__init__()
self.kwargs = kwargs
self.weight = weight
self.ignore_index = ignore_index
def forward(self, predict, target):
target = make_one_hot(target, num_classes=5)
# print(predict.shape)
# print(target.shape)
assert predict.shape == target.shape, 'predict & target shape do not match'
dice = BinaryDiceLoss(**self.kwargs)
total_loss = 0
predict = F.softmax(predict, dim=1)
for i in range(target.shape[1]):
if i != self.ignore_index:
dice_loss = dice(predict[:, i], target[:, i])
if self.weight is not None:
assert self.weight.shape[0] == target.shape[1], \
'Expect weight shape [{}], get[{}]'.format(target.shape[1], self.weight.shape[0])
dice_loss *= self.weight[i]
total_loss += dice_loss
return total_loss/target.shape[1]