-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlosses.py
255 lines (198 loc) · 9.47 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
##This code is from : https://github.com/HanxunH/Active-Passive-Losses
import torch
import torch.nn.functional as F
from torch import nn
import numpy as np
class ReverseCrossEntropy(torch.nn.Module):
def __init__(self, num_classes, scale=1.0):
super(ReverseCrossEntropy, self).__init__()
self.num_classes = num_classes
self.scale = scale
def forward(self, pred, labels):
pred = F.softmax(pred, dim=1)
pred = torch.clamp(pred, min=1e-7, max=1.0)
label_one_hot = torch.nn.functional.one_hot(labels, self.num_classes).float()
label_one_hot = torch.clamp(label_one_hot, min=1e-4, max=1.0)
rce = (-1*torch.sum(pred * torch.log(label_one_hot), dim=1))
return self.scale * rce.mean()
class NormalizedCrossEntropy(torch.nn.Module):
def __init__(self, num_classes, scale=1.0):
super(NormalizedCrossEntropy, self).__init__()
self.num_classes = num_classes
self.scale = scale
def forward(self, pred, labels):
pred = F.log_softmax(pred, dim=1)
label_one_hot = torch.nn.functional.one_hot(labels, self.num_classes).float()
nce = -1 * torch.sum(label_one_hot * pred, dim=1) / (- pred.sum(dim=1))
return self.scale * nce.mean()
class NormalizedReverseCrossEntropy(torch.nn.Module):
def __init__(self, num_classes, scale=1.0):
super(NormalizedReverseCrossEntropy, self).__init__()
self.num_classes = num_classes
self.scale = scale
def forward(self, pred, labels):
pred = F.softmax(pred, dim=1)
pred = torch.clamp(pred, min=1e-7, max=1.0)
label_one_hot = torch.nn.functional.one_hot(labels, self.num_classes).float()
label_one_hot = torch.clamp(label_one_hot, min=1e-4, max=1.0)
normalizor = 1 / 4 * (self.num_classes - 1)
rce = (-1*torch.sum(pred * torch.log(label_one_hot), dim=1))
return self.scale * normalizor * rce.mean()
class MeanAbsoluteError(torch.nn.Module):
def __init__(self, num_classes, scale=1.0):
super(MeanAbsoluteError, self).__init__()
self.num_classes = num_classes
self.scale = scale
return
def forward(self, pred, labels):
pred = F.softmax(pred, dim=1)
label_one_hot = torch.nn.functional.one_hot(labels, self.num_classes).float()
mae = 1. - torch.sum(label_one_hot * pred, dim=1)
# Note: Reduced MAE
# Original: torch.abs(pred - label_one_hot).sum(dim=1)
# $MAE = \sum_{k=1}^{K} |\bm{p}(k|\bm{x}) - \bm{q}(k|\bm{x})|$
# $MAE = \sum_{k=1}^{K}\bm{p}(k|\bm{x}) - p(y|\bm{x}) + (1 - p(y|\bm{x}))$
# $MAE = 2 - 2p(y|\bm{x})$
#
return self.scale * mae.mean()
class NormalizedMeanAbsoluteError(torch.nn.Module):
def __init__(self, num_classes, scale=1.0):
super(NormalizedMeanAbsoluteError, self).__init__()
self.num_classes = num_classes
self.scale = scale
return
def forward(self, pred, labels):
pred = F.softmax(pred, dim=1)
label_one_hot = torch.nn.functional.one_hot(labels, self.num_classes).float()
normalizor = 1 / (2 * (self.num_classes - 1))
mae = 1. - torch.sum(label_one_hot * pred, dim=1)
return self.scale * normalizor * mae.mean()
class BetaCrossEnropyError(torch.nn.Module):
def __init__(self, num_classes, scale=1.0, beta=0.1):
super(BetaCrossEnropyError, self).__init__()
self.num_classes = num_classes
self.scale = scale
self.beta = beta
return
def forward(self, pred, labels):
pred = F.softmax(pred, dim=1)
# ns = labels.shape[0]
C = -(self.beta + 1) / self.beta
label_one_hot = torch.nn.functional.one_hot(labels, self.num_classes).float()
single_prob = torch.sum(pred * label_one_hot, dim=1)
# print(single_prob)
term1 = C * (torch.pow(single_prob, self.beta) - 1) # This needs to be checked!!!!!!!!!
term2 = torch.sum(torch.pow(pred, self.beta + 1), dim=1)
# print(term1)
# print(term2)
bce = torch.mean(term1 + term2)
#
return self.scale * bce
class GeneralizedCrossEntropy(torch.nn.Module):
def __init__(self, num_classes, q=0.7):
super(GeneralizedCrossEntropy, self).__init__()
self.num_classes = num_classes
self.q = q
def forward(self, pred, labels):
pred = F.softmax(pred, dim=1)
pred = torch.clamp(pred, min=1e-7, max=1.0)
label_one_hot = torch.nn.functional.one_hot(labels, self.num_classes).float()
gce = (1. - torch.pow(torch.sum(label_one_hot * pred, dim=1), self.q)) / self.q
return gce.mean()
class SCELoss(torch.nn.Module):
def __init__(self, num_classes, alpha=0.01, beta=1):
super(SCELoss, self).__init__()
self.alpha = alpha
self.beta = beta
self.num_classes = num_classes
self.cross_entropy = torch.nn.CrossEntropyLoss()
def forward(self, pred, labels):
# CCE
ce = self.cross_entropy(pred, labels)
# RCE
pred = F.softmax(pred, dim=1)
pred = torch.clamp(pred, min=1e-7, max=1.0)
label_one_hot = torch.nn.functional.one_hot(labels, self.num_classes).float()
label_one_hot = torch.clamp(label_one_hot, min=1e-4, max=1.0)
rce = (-1*torch.sum(pred * torch.log(label_one_hot), dim=1))
# Loss
loss = self.alpha * ce + self.beta * rce.mean()
return loss
class NormalizedGeneralizedCrossEntropy(torch.nn.Module):
def __init__(self, num_classes, scale=1.0, q=0.7):
super(NormalizedGeneralizedCrossEntropy, self).__init__()
self.num_classes = num_classes
self.q = q
self.scale = scale
def forward(self, pred, labels):
pred = F.softmax(pred, dim=1)
pred = torch.clamp(pred, min=1e-7, max=1.0)
label_one_hot = torch.nn.functional.one_hot(labels, self.num_classes).float()
numerators = 1. - torch.pow(torch.sum(label_one_hot * pred, dim=1), self.q)
denominators = self.num_classes - pred.pow(self.q).sum(dim=1)
ngce = numerators / denominators
return self.scale * ngce.mean()
class NCEandRCE(torch.nn.Module):
def __init__(self, num_classes, alpha=50, beta=0.1):
super(NCEandRCE, self).__init__()
self.num_classes = num_classes
self.nce = NormalizedCrossEntropy(scale=alpha, num_classes=num_classes)
self.rce = ReverseCrossEntropy(scale=beta, num_classes=num_classes)
def forward(self, pred, labels):
return self.nce(pred, labels) + self.rce(pred, labels)
class NCEandMAE(torch.nn.Module):
def __init__(self, num_classes, alpha=50, beta=1.0):
super(NCEandMAE, self).__init__()
self.num_classes = num_classes
self.nce = NormalizedCrossEntropy(scale=alpha, num_classes=num_classes)
self.mae = MeanAbsoluteError(scale=beta, num_classes=num_classes)
def forward(self, pred, labels):
return self.nce(pred, labels) + self.mae(pred, labels)
class GCEandRCE(torch.nn.Module):
def __init__(self, alpha, beta, num_classes, q=0.7):
super(GCEandRCE, self).__init__()
self.num_classes = num_classes
self.gce = GeneralizedCrossEntropy(num_classes=num_classes, q=q)
self.rce = ReverseCrossEntropy(scale=beta, num_classes=num_classes)
def forward(self, pred, labels):
return self.gce(pred, labels) + self.rce(pred, labels)
class GCEandNCE(torch.nn.Module):
def __init__(self, alpha, beta, num_classes, q=0.7):
super(GCEandNCE, self).__init__()
self.num_classes = num_classes
self.gce = GeneralizedCrossEntropy(num_classes=num_classes, q=q)
self.nce = NormalizedCrossEntropy(num_classes=num_classes)
def forward(self, pred, labels):
return self.gce(pred, labels) + self.nce(pred, labels)
class NGCEandNCE(torch.nn.Module):
def __init__(self, alpha, beta, num_classes, q=0.7):
super(NGCEandNCE, self).__init__()
self.num_classes = num_classes
self.ngce = NormalizedGeneralizedCrossEntropy(scale=alpha, q=q, num_classes=num_classes)
self.nce = NormalizedCrossEntropy(scale=beta, num_classes=num_classes)
def forward(self, pred, labels):
return self.ngce(pred, labels) + self.nce(pred, labels)
class NGCEandMAE(torch.nn.Module):
def __init__(self, alpha, beta, num_classes, q=0.7):
super(NGCEandMAE, self).__init__()
self.num_classes = num_classes
self.ngce = NormalizedGeneralizedCrossEntropy(scale=alpha, q=q, num_classes=num_classes)
self.mae = MeanAbsoluteError(scale=beta, num_classes=num_classes)
def forward(self, pred, labels):
return self.ngce(pred, labels) + self.mae(pred, labels)
class NGCEandRCE(torch.nn.Module):
def __init__(self, alpha, beta, num_classes, q=0.7):
super(NGCEandRCE, self).__init__()
self.num_classes = num_classes
self.ngce = NormalizedGeneralizedCrossEntropy(scale=alpha, q=q, num_classes=num_classes)
self.rce = ReverseCrossEntropy(scale=beta, num_classes=num_classes)
def forward(self, pred, labels):
return self.ngce(pred, labels) + self.rce(pred, labels)
class MAEandRCE(torch.nn.Module):
def __init__(self, alpha, beta, num_classes):
super(MAEandRCE, self).__init__()
self.num_classes = num_classes
self.mae = MeanAbsoluteError(scale=alpha, num_classes=num_classes)
self.rce = ReverseCrossEntropy(scale=beta, num_classes=num_classes)
def forward(self, pred, labels):
return self.mae(pred, labels) + self.rce(pred, labels)