[903821]: / loss.py

Download this file

53 lines (40 with data), 1.6 kB

 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
import torch.nn.functional as F
import torch.nn as nn
import torch
from einops import rearrange
def cal_dice(output, target, eps=1e-3):
output = torch.argmax(output,dim=1)
inter = torch.sum(output * target) + eps
union = torch.sum(output) + torch.sum(target) + eps * 2
dice = 2 * inter / union
return dice
class Loss(nn.Module):
def __init__(self, n_classes, alpha=0.5):
"dice_loss_plus_cetr_weighted"
super(Loss, self).__init__()
self.n_classes = n_classes
self.alpha = alpha
def forward(self, input, target):
smooth = 0.01
# print(torch.unique(target))
input1 = F.softmax(input, dim=1)
target1 = F.one_hot(target,self.n_classes)
input1 = rearrange(input1,'b n h w s -> b n (h w s)')
target1 = rearrange(target1,'b h w s n -> b n (h w s)')
# 只取前景
input1 = input1[:, 1:, :]
target1 = target1[:, 1:, :].float()
# 以batch为单位计算dice_loss
inter = torch.sum(input1 * target1)
union = torch.sum(input1) + torch.sum(target1) + smooth
dice = 2.0 * inter / union
loss = F.cross_entropy(input,target)
total_loss = (1 - self.alpha) * loss + (1 - dice) * self.alpha
return total_loss
if __name__ == '__main__':
torch.manual_seed(3)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
losser = Loss(n_classes=2).to(device)
x = torch.randn((4, 2, 16, 16, 16)).to(device)
y = torch.randint(0, 2, (4, 16, 16, 16)).to(device)
print(losser(x, y))