-
Notifications
You must be signed in to change notification settings - Fork 2
/
loss.py
28 lines (23 loc) · 798 Bytes
/
loss.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
import torch
import torch.nn as nn
import torch.nn.functional as F
class DICE_BCE_Loss(nn.Module):
def __init__(self, smooth=1):
super().__init__()
self.smooth = smooth
def forward(self, logits, targets):
logits= torch.sigmoid(logits)
intersection = 2*(logits * targets).sum() + self.smooth
union = (logits + targets).sum() + self.smooth
dice_loss = 1. - intersection / union
loss = nn.BCELoss()
bce_loss = loss(logits, targets)
return dice_loss + bce_loss
def dice_coeff(logits, targets):
logits=torch.sigmoid(logits)
intersection = 2*(logits * targets).sum()
union = (logits + targets).sum()
if union == 0:
return 1
dice_coeff = intersection / union
return dice_coeff.item()