forked from MELABIPCAS/DVSCL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LossFunctions.py
227 lines (195 loc) · 9.04 KB
/
LossFunctions.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
from torch.nn.modules import Module
import torch.nn._reduction as _Reduction
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import torch
class _Loss(Module):
def __init__(self, size_average=None, reduce=None, reduction='mean'):
super(_Loss, self).__init__()
if size_average is not None or reduce is not None:
self.reduction = _Reduction.legacy_get_string(size_average, reduce)
else:
self.reduction = reduction
class HHLoss(_Loss):
r"""Creates a criterion that measures the hierarchy label error (squared L2 norm) between
each element in the input :math:`x` and target :math:`y`.
"""
def __init__(self, size_average=None, reduce=None, reduction='mean'):
super(HHLoss, self).__init__(size_average, reduce, reduction)
self.norm_p = 2
self.param_lambda = 0.001
self.param_beta = 0.002
def forward(self, input, target):
input_s = input.mm(input.t())
target_s = target.mm(target.t())
uncorr_M = input_s.mm(input_s.t())/input_s.shape[0]
I = torch.eye(input_s.shape[1]).type_as(uncorr_M)
loss = torch.norm(input_s-target_s,p=self.norm_p)\
+ self.param_lambda*torch.norm(torch.sum(input_s,0),p=self.norm_p)\
+ self.param_beta*torch.norm(uncorr_M-I,p=self.norm_p)
return loss
# input_s = input.mm(input.t())
# target_s = target.mm(target.t())
# return F.mse_loss(input_s, target_s, reduction=self.reduction)
class HHLoss_bin(_Loss):
r"""Creates a criterion that measures the hierarchy label error (squared L2 norm) between
each element in the input :math:`x` and target :math:`y`.
"""
def __init__(self, size_average=None, reduce=None, reduction='mean'):
super(HHLoss_bin, self).__init__(size_average, reduce, reduction)
self.th = 0.0
self.norm_p = 2
self.param_lambda = 0.001
self.param_beta = 0.002
self.mu = 0.001
def forward(self, input, target):
input_b = torch.sign(input)
input_s = input.mm(input.t())
# input_s = input_b.mm(input_b.t())
target_s = target.mm(target.t())
uncorr_M = input_s.mm(input_s.t()) / input_s.shape[0]
I = torch.eye(input_s.shape[1]).type_as(uncorr_M)
loss = torch.norm(input_s - target_s, p=self.norm_p) \
+ self.param_lambda * torch.norm(torch.sum(input, 0), p=self.norm_p) \
+ self.param_beta * torch.norm(uncorr_M - I, p=self.norm_p) \
+ self.mu*torch.norm(torch.sum(input_b-input, 0), p=self.norm_p)
return loss
# input = (torch.sign(input)+1)/2
# input = torch.sign(input)
# input_s = input.mm(input.t())
# target_s = target.mm(target.t())
# uncorr_M = input_s.mm(input_s.t()) / input_s.shape[0]
# I = torch.eye(input_s.shape[1]).type_as(uncorr_M)
# loss = torch.norm(input_s - target_s, p=self.norm_p) \
# + self.param_lambda * torch.norm(torch.sum(input, 0), p=self.norm_p) \
# + self.param_beta * torch.norm(uncorr_M - I, p=self.norm_p) \
# + self.mu*torch.norm(torch.sum(input, 0)
# return loss
class MSELoss(_Loss):
r"""Creates a criterion that measures the mean squared error (squared L2 norm) between
each element in the input :math:`x` and target :math:`y`.
"""
__constants__ = ['reduction']
def __init__(self, size_average=None, reduce=None, reduction='mean'):
super(MSELoss, self).__init__(size_average, reduce, reduction)
def forward(self, input, target):
return F.mse_loss(input, target, reduction=self.reduction)
class FocalLoss(nn.Module):
r"""
This criterion is a implemenation of Focal Loss, which is proposed in
Focal Loss for Dense Object Detection.
Loss(x, class) = - \alpha (1-softmax(x)[class])^gamma \log(softmax(x)[class])
The losses are averaged across observations for each minibatch.
Args:
alpha(1D Tensor, Variable) : the scalar factor for this criterion
gamma(float, double) : gamma > 0; reduces the relative loss for well-classified examples (p > .5),
putting more focus on hard, misclassified examples
size_average(bool): By default, the losses are averaged over observations for each minibatch.
However, if the field size_average is set to False, the losses are
instead summed for each minibatch.
"""
def __init__(self, class_num, alpha=None, gamma=2, size_average=True, device='cuda'):
super(FocalLoss, self).__init__()
if alpha is None:
self.alpha = Variable(torch.ones(class_num, 1))
else:
if isinstance(alpha, Variable):
self.alpha = alpha
else:
self.alpha = Variable(alpha)
self.gamma = gamma
self.class_num = class_num
self.size_average = size_average
self.device = device
def forward(self, inputs, targets):
N = inputs.size(0)
C = inputs.size(1)
P = F.softmax(inputs,dim=1)
class_mask = inputs.data.new(N, C).fill_(0)
class_mask = Variable(class_mask)
ids = targets.view(-1, 1)
class_mask.scatter_(1, ids.data, 1.)
if inputs.is_cuda and not self.alpha.is_cuda:
self.alpha = self.alpha.cuda()
alpha = self.alpha[ids.data.view(-1)]
probs = (P*class_mask).sum(1).view(-1,1)
log_p = probs.log()
batch_loss = -alpha*(torch.pow((1-probs), self.gamma))*log_p
if self.size_average:
loss = batch_loss.mean()
else:
loss = batch_loss.sum()
return loss
class BalancedLoss(nn.Module):
r"""
This criterion is a implemenation of Focal Loss, which is proposed in
Focal Loss for Dense Object Detection.
Loss(x, class) = - \alpha (1-softmax(x)[class])^gamma \log(softmax(x)[class])
The losses are averaged across observations for each minibatch.
Args:
alpha(1D Tensor, Variable) : the scalar factor for this criterion
gamma(float, double) : gamma > 0; reduces the relative loss for well-classified examples (p > .5),
putting more focus on hard, misclassified examples
size_average(bool): By default, the losses are averaged over observations for each minibatch.
However, if the field size_average is set to False, the losses are
instead summed for each minibatch.
"""
def __init__(self, class_num, alpha=None, gamma=2, size_average=True, device='cpu'):
super(BalancedLoss, self).__init__()
if alpha is None:
self.alpha = Variable(torch.ones(class_num, 1))
else:
if isinstance(alpha, Variable):
self.alpha = alpha
else:
self.alpha = Variable(alpha)
self.gamma = gamma
self.class_num = class_num
self.size_average = size_average
self.device = device
def forward(self, inputs, targets):
N = inputs.size(0)
C = inputs.size(1)
P = F.softmax(inputs,dim=1)
class_mask = inputs.data.new(N, C).fill_(0)
class_mask = Variable(class_mask)
ids = targets.view(-1, 1)
class_mask.scatter_(1, ids.data, 1.)
self.alpha = torch.histc(ids, bins=self.class_num, min=0, max=self.class_num-1).float()/float(ids.shape[0])
# self.alpha = 1.0*self.alpha.reciprocal() # 10, 100
self.alpha = 1.0 - self.alpha/10.0
alpha_c = self.alpha[ids.data.view(-1)]
if inputs.is_cuda and not alpha_c.is_cuda:
alpha_c = alpha_c.to(self.device)
probs = (P*class_mask).sum(1).view(-1,1)
log_p = probs.log()
batch_loss = -alpha_c*(torch.pow((1-probs), self.gamma))*log_p
if self.size_average:
loss = batch_loss.mean()
else:
loss = batch_loss.sum()
return loss
class CosineLoss(nn.Module):
r"""
This criterion is a implemenation of Cosine Loss
"""
def __init__(self, size_average=True):
super(CosineLoss, self).__init__()
self.size_average = size_average
def forward(self, inputs, targets):
N = inputs.size(0)
C = inputs.size(1)
P = torch.div(inputs,inputs.norm(dim=1,keepdim=True))
# one-hot coding
class_mask = inputs.data.new(N, C).fill_(0)
class_mask = Variable(class_mask)
ids = targets.view(-1, 1)
class_mask.scatter_(1, ids.data, 1.)
probs = (P*class_mask).sum(1).view(-1,1)
log_p = 1.0-probs
if self.size_average:
loss = log_p.mean()
else:
loss = log_p.sum()
return loss