-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdice_losses.py
136 lines (106 loc) · 4.82 KB
/
dice_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
import torch
import torch.nn as nn
import torch.nn.functional as F
class DiceLoss(nn.Module):
def __init__(self, smooth=0, eps=1e-7):
super(DiceLoss, self).__init__()
self.smooth = smooth
self.eps = eps
def forward(self, output, target):
return 1 - (2 * torch.sum(output * target) + self.smooth) / (
torch.sum(output) + torch.sum(target) + self.smooth + self.eps)
def mixed_dice_cross_entropy_loss(output, target, dice_weight=0.5, dice_loss=None,
cross_entropy_weight=0.5, cross_entropy_loss=None, smooth=0,
dice_activation='softmax'):
num_classes_without_background = output.size(1) - 1
dice_output = output[:, 1:, :, :]
dice_target = target[:, :num_classes_without_background, :, :].long()
cross_entropy_target = torch.zeros_like(target[:, 0, :, :]).long()
for class_nr in range(num_classes_without_background):
cross_entropy_target = where(target[:, class_nr, :, :], class_nr + 1, cross_entropy_target)
if cross_entropy_loss is None:
cross_entropy_loss = nn.CrossEntropyLoss()
if dice_loss is None:
dice_loss = multiclass_dice_loss
return dice_weight * dice_loss(dice_output, dice_target, smooth,
dice_activation) + cross_entropy_weight * cross_entropy_loss(output,
cross_entropy_target)
def multiclass_dice_loss(output, target, smooth=0, activation='softmax'):
"""Calculate Dice Loss for multiple class output.
Args:
output (torch.Tensor): Model output of shape (N x C x H x W).
target (torch.Tensor): Target of shape (N x H x W).
smooth (float, optional): Smoothing factor. Defaults to 0.
activation (string, optional): Name of the activation function, softmax or sigmoid. Defaults to 'softmax'.
Returns:
torch.Tensor: Loss value.
"""
if activation == 'softmax':
activation_nn = torch.nn.Softmax2d()
elif activation == 'sigmoid':
activation_nn = torch.nn.Sigmoid()
else:
raise NotImplementedError('only sigmoid and softmax are implemented')
loss = 0
dice = DiceLoss(smooth=smooth)
output = activation_nn(output)
num_classes = output.size(1)
target.data = target.data.float()
for class_nr in range(num_classes):
loss += dice(output[:, class_nr, :, :], target[:, class_nr, :, :])
return loss / num_classes
def mixed_dice_bce_loss(output, target, dice_weight=0, dice_loss=multiclass_dice_loss,
bce_weight=1., bce_loss=nn.BCEWithLogitsLoss(),
smooth=0, dice_activation='sigmoid'):
#num_classes = output.size(1)
#target = target[:, :num_classes, :, :].long()
target = target.long()
d = dice_loss(output, target, smooth, dice_activation)
b = bce_loss(output, target)
#print('dice: {}, bce: {}'.format(d, b))
return dice_weight * d + bce_weight * b
def where(cond, x_1, x_2):
cond = cond.long()
return (cond * x_1) + ((1 - cond) * x_2)
class FocalLoss2d(nn.Module):
def __init__(self, gamma=2, size_average=True):
super(FocalLoss2d, self).__init__()
self.gamma = gamma
self.size_average = size_average
def forward(self, logit, target, class_weight=None, type='sigmoid'):
target = target.view(-1, 1).long()
if type=='sigmoid':
if class_weight is None:
class_weight = [1]*2 #[0.5, 0.5]
prob = torch.sigmoid(logit)
prob = prob.view(-1, 1)
prob = torch.cat((1-prob, prob), 1)
select = torch.FloatTensor(len(prob), 2).zero_().cuda()
select.scatter_(1, target, 1.)
elif type=='softmax':
B,C,H,W = logit.size()
if class_weight is None:
class_weight =[1]*C #[1/C]*C
logit = logit.permute(0, 2, 3, 1).contiguous().view(-1, C)
prob = F.softmax(logit,1)
select = torch.FloatTensor(len(prob), C).zero_().cuda()
select.scatter_(1, target, 1.)
class_weight = torch.FloatTensor(class_weight).cuda().view(-1,1)
class_weight = torch.gather(class_weight, 0, target)
prob = (prob*select).sum(1).view(-1,1)
prob = torch.clamp(prob,1e-8,1-1e-8)
batch_loss = - class_weight *(torch.pow((1-prob), self.gamma))*prob.log()
if self.size_average:
loss = batch_loss.mean()
else:
loss = batch_loss
return loss
if __name__ == '__main__':
L = FocalLoss2d()
out = torch.randn(2, 3, 3).cuda()
#target = torch.ones(2, 3, 3).cuda()
target = (torch.sigmoid(out) > 0.5).float()
#print(target, out)
loss = L(out, target)
print(loss)
#pass