-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlosses.py
189 lines (138 loc) · 5.46 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
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import pdb
class DiceLoss(nn.Module):
def __init__(self, alpha=0.5, beta=0.5, size_average=True, reduce=True):
super(DiceLoss, self).__init__()
self.alpha = alpha
self.beta = beta
self.size_average = size_average
self.reduce = reduce
def forward(self, preds, targets, weight=False):
N = preds.size(0)
C = preds.size(1)
preds = preds.permute(0, 2, 3, 4, 1).contiguous().view(-1, C)
targets = targets.view(-1, 1)
P = F.softmax(preds, dim=1)
smooth = torch.zeros(C, dtype=torch.float32).fill_(0.00001)
class_mask = torch.zeros(preds.shape).to(preds.device)
class_mask.scatter_(1, targets, 1.)
ones = torch.ones(preds.shape).to(preds.device)
P_ = ones - P
class_mask_ = ones - class_mask
TP = P * class_mask
FP = P * class_mask_
FN = P_ * class_mask
smooth = smooth.to(preds.device)
self.alpha = FP.sum(dim=(0)) / ((FP.sum(dim=(0)) + FN.sum(dim=(0))) + smooth)
self.alpha = torch.clamp(self.alpha, min=0.2, max=0.8)
#print('alpha:', self.alpha)
self.beta = 1 - self.alpha
num = torch.sum(TP, dim=(0)).float()
den = num + self.alpha * torch.sum(FP, dim=(0)).float() + self.beta * torch.sum(FN, dim=(0)).float()
dice = num / (den + smooth)
if not self.reduce:
loss = torch.ones(C).to(dice.device) - dice
return loss
loss = 1 - dice
if weight is not False:
loss *= weight.squeeze(0)
loss = loss.sum()
if self.size_average:
if weight is not False:
loss /= weight.squeeze(0).sum()
else:
loss /= C
return loss
class BinaryDiceLoss(nn.Module):
def __init__(self, alpha=0.5, beta=0.5, size_average=True, reduce=True):
super(BinaryDiceLoss, self).__init__()
self.alpha = alpha
self.beta = beta
self.size_average = size_average
self.reduce = reduce
def forward(self, preds, targets):
N = preds.size(0)
preds = F.sigmoid(preds)
preds = preds.permute(0, 2, 3, 4, 1).contiguous().view(-1, 1)
P = preds
targets = targets.view(-1, 1)
smooth = torch.zeros(1, dtype=torch.float32).fill_(0.00001)
ones = torch.ones(preds.shape).to(preds.device)
P_ = ones - P
class_mask_ = ones - targets
class_mask = targets
TP = P * class_mask
FP = P * class_mask_
FN = P_ * class_mask
smooth = smooth.to(preds.device)
self.alpha = FP.sum(dim=(0)) / ((FP.sum(dim=(0)) + FN.sum(dim=(0))) + smooth)
self.alpha = torch.clamp(self.alpha, min=0.2, max=0.8)
#print('alpha:', self.alpha)
self.beta = 1 - self.alpha
num = torch.sum(TP, dim=(0)).float()
den = num + self.alpha * torch.sum(FP, dim=(0)).float() + self.beta * torch.sum(FN, dim=(0)).float()
dice = num / (den + smooth)
if not self.reduce:
loss = torch.ones(C).to(dice.device) - dice
return loss
dice = dice.sum()
loss = 1. - dice
return loss
class BinaryFocalLoss(nn.Module):
def __init__(self, alpha=None, gamma=2, size_average=True):
super(BinaryFocalLoss, self).__init__()
if alpha is None:
self.alpha = torch.Tensor([0.5]).cuda()
else:
self.alpha = torch.Tensor([alpha]).cuda()
self.gamma = gamma
self.size_average = size_average
def forward(self, preds, targets):
N = preds.size(0)
targets = targets.long()
preds = preds.permute(0, 2, 3, 4, 1).contiguous().view(-1)
targets = targets.view(-1)
P = F.sigmoid(preds)
log_P = F.logsigmoid(preds)
log_P_ = F.logsigmoid(1 - preds)
targets = targets.float()
batch_loss = -self.alpha * (1-P).pow(self.gamma)*log_P * targets \
-(1-self.alpha) * P.pow(self.gamma)*log_P_ * (1-targets)
if self.size_average:
loss = batch_loss.mean()
else:
loss = batch_loss.sum()
return loss
class FocalLoss(nn.Module):
def __init__(self, class_num, alpha=None, gamma=2, size_average=True):
super(FocalLoss, self).__init__()
if alpha is None:
self.alpha = torch.ones(class_num, 1).cuda()
else:
self.alpha = alpha
self.gamma = gamma
self.size_average = size_average
def forward(self, preds, targets, weight=False):
N = preds.size(0)
C = preds.size(1)
preds = preds.permute(0, 2, 3, 4, 1).contiguous().view(-1, C)
targets = targets.view(-1, 1)
P = F.softmax(preds, dim=1)
log_P = F.log_softmax(preds, dim=1)
class_mask = torch.zeros(preds.shape).to(preds.device)
class_mask.scatter_(1, targets, 1.)
alpha = self.alpha[targets.data.view(-1)]
probs = (P * class_mask).sum(1).view(-1, 1)
log_probs = (log_P * class_mask).sum(1).view(-1, 1)
batch_loss = -alpha * (1-probs).pow(self.gamma)*log_probs
if weight is not False:
element_weight = weight.squeeze(0)[targets.squeeze(0)]
batch_loss = batch_loss * element_weight
if self.size_average:
loss = batch_loss.mean()
else:
loss = batch_loss.sum()
return loss