-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlosses.py
More file actions
66 lines (50 loc) · 1.9 KB
/
losses.py
File metadata and controls
66 lines (50 loc) · 1.9 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import warnings
warnings.filterwarnings("ignore", category=UserWarning, message=".*iCCP: known incorrect sRGB profile.*")
import torch
import torch.nn as nn
import torch.nn.functional as F
class MyLoss(nn.Module):
def __init__(self, alpha=0.5, gamma=2):
super(MyLoss, self).__init__()
self.alpha = alpha
self.gamma = gamma
def Softmax_Focal_Loss(self, pred, target):
"""
pred : [B, C, H, W]
target : [B, H, W]
"""
target = target.long()
target = target.squeeze()
b, c, h, w = pred.size()
bt, ht, wt = target.size()
if h != ht or w != wt:
pred = F.interpolate(pred, size=(ht, wt), mode="bilinear", align_corners=True)
p = F.softmax(pred, dim=1)
ce_loss = F.cross_entropy(pred, target, reduction="none")
p_t = p.gather(1, target.unsqueeze(1)).squeeze(1)
loss = ce_loss * ((1 - p_t) ** self.gamma)
if self.alpha >= 0:
alpha_t = self.alpha * target + (1 - self.alpha) * (1 - target)
loss = alpha_t * loss
loss = loss.mean()
return loss
#
def Dice_loss(self, pred, mask):
"""
pred:[b,2,h,w],
mask:[b,h,w]
"""
smooth = 1e-6
mask = mask.squeeze(dim=1)
b, c, h, w = pred.size()
bt, ht, wt = mask.size()
if h != ht or w != wt:
pred = F.interpolate(pred, size=(ht, wt), mode="bilinear", align_corners=True)
pred = torch.softmax(pred, dim=1)[:, 1]
intersection = torch.sum(pred * mask, dim=(1, 2))
union = torch.sum(pred, dim=(1, 2)) + torch.sum(mask, dim=(1, 2))
dice_coefficient = (2. * intersection + smooth) / (union + smooth)
dice_loss = 1 - dice_coefficient.mean()
return dice_loss
def forward(self, pred, mask):
return self.Dice_loss(pred, mask) + self.Softmax_Focal_Loss(pred, mask)