-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcriterions.py
30 lines (22 loc) · 1015 Bytes
/
criterions.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
import torch
import torch.nn as nn
import torch.nn.functional as F
# https://www.kaggle.com/bigironsphere/loss-function-library-keras-pytorch
class DiceBCELoss(nn.Module):
"""Binary Cross Entropy Loss with Dice Loss.
:param weight: weight for dice_loss
"""
def __init__(self, weight: float = 1.):
super(DiceBCELoss, self).__init__()
self.weight = weight
def forward(self, out, targets, smooth=1):
#comment out if your model contains a sigmoid or equivalent activation layer
# out = F.sigmoid(out)
#flatten label and prediction tensors
out = out.view(-1)
targets = targets.view(-1)
intersection = (out * targets).sum()
dice_loss = 1 - (2.*intersection + smooth)/(out.sum() + targets.sum() + smooth)
bce = F.binary_cross_entropy_with_logits(out, targets, reduction='mean')
dice_bce = bce + self.weight*dice_loss
return dice_bce