-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathloss.py
141 lines (116 loc) · 5.07 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
import torch.nn as nn
import torch.nn.functional as F
################################################# Focal Loss #########################################
# Single class
class FocalLoss(nn.Module):
def __init__(self, alpha=1, gamma=2, logits=False, reduce=True):
super(FocalLoss, self).__init__()
self.alpha = alpha
self.gamma = gamma
self.logits = logits
self.reduce = reduce
def forward(self, inputs, targets):
BCE_loss = nn.CrossEntropyLoss(reduction='none')(inputs, targets)
pt = torch.exp(-BCE_loss)
F_loss = self.alpha * (1-pt)**self.gamma * BCE_loss
if self.reduce:
return torch.mean(F_loss)
else:
return F_loss
# Multiclass
class FocalLoss(nn.Module):
def __init__(self, alpha=0.85, gamma=2, logits=True, reduce=True):
super(FocalLoss, self).__init__()
self.alpha = alpha
self.gamma = gamma
self.logits = logits
self.reduce = reduce
def forward(self, inputs, targets):
if self.logits:
BCE_loss = F.binary_cross_entropy_with_logits(inputs, targets, reduce=False)
else:
BCE_loss = F.binary_cross_entropy(inputs, targets, reduce=False)
pt = torch.exp(-BCE_loss)
F_loss = self.alpha * (1-pt)**self.gamma * BCE_loss
if self.reduce:
return torch.mean(F_loss)
else:
return F_loss
########################################### Label Smoothening ##########################################
# smoothening is not exact, eps/num_classes is also added to the true label
class LabelSmoothingLoss(torch.nn.Module):
def __init__(self, smoothing: float = 0.1, reduction="mean", weight=None):
super(LabelSmoothingLoss, self).__init__()
self.epsilon = smoothing
self.reduction = reduction
self.weight = weight
def reduce_loss(self, loss):
if self.reduction == "mean":
return loss.mean()
elif self.reduction == "sum":
return loss.sum()
else:
return loss
def linear_combination(self, x, y):
return self.epsilon * x + (1 - self.epsilon) * y
def forward(self, preds, target):
if self.weight is not None:
self.weight = self.weight.to(preds.device)
if self.training: # provision for training and validation
n = preds.size(-1)
log_preds = F.log_softmax(preds, dim=-1)
loss = self.reduce_loss(-log_preds.sum(dim=-1))
nll = F.nll_loss(
log_preds, target, reduction=self.reduction, weight=self.weight
)
return self.linear_combination(loss / n, nll)
else:
return torch.nn.functional.cross_entropy(preds, target, weight=self.weight)
# exact
class LabelSmoothingLoss(nn.Module):
def __init__(self, classes, smoothing=0.0, dim=-1):
super(LabelSmoothingLoss, self).__init__()
self.confidence = 1.0 - smoothing
self.smoothing = smoothing
self.cls = classes
self.dim = dim
def forward(self, pred, target):
pred = pred.log_softmax(dim=self.dim)
with torch.no_grad():
true_dist = torch.zeros_like(pred)
true_dist.fill_(self.smoothing / (self.cls - 1))
true_dist.scatter_(1, target.data.unsqueeze(1), self.confidence)
return torch.mean(torch.sum(-true_dist * pred, dim=self.dim))
######################################## Focal Cosine Loss #########################################
class FocalCosineLoss(nn.Module):
def __init__(self, alpha=1, gamma=2, xent=.1):
super(FocalCosineLoss, self).__init__()
self.alpha = alpha
self.gamma = gamma
self.xent = xent
self.y = torch.Tensor([1]).cuda()
def forward(self, input, target, reduction="mean"):
cosine_loss = F.cosine_embedding_loss(input, F.one_hot(target, num_classes=input.size(-1)), self.y, reduction=reduction)
cent_loss = F.cross_entropy(F.normalize(input), target, reduce=False)
pt = torch.exp(-cent_loss)
focal_loss = self.alpha * (1-pt)**self.gamma * cent_loss
if reduction == "mean":
focal_loss = torch.mean(focal_loss)
return cosine_loss + self.xent * focal_loss
#################### Symmetric Cross Entropy Loss ##########################
# https://arxiv.org/abs/1908.06112
class SymmetricCrossEntropy(nn.Module):
def __init__(self, alpha=0.1, beta=1.0, num_classes= 5):
super(SymmetricCrossEntropy, self).__init__()
self.alpha = alpha
self.beta = beta
self.num_classes = num_classes
def forward(self, logits, targets, reduction='mean'):
onehot_targets = torch.eye(self.num_classes)[targets].cuda()
ce_loss = F.cross_entropy(logits, targets, reduction=reduction)
rce_loss = (-onehot_targets*logits.softmax(1).clamp(1e-7, 1.0).log()).sum(1)
if reduction == 'mean':
rce_loss = rce_loss.mean()
elif reduction == 'sum':
rce_loss = rce_loss.sum()
return self.alpha * ce_loss + self.beta * rce_loss