-
Notifications
You must be signed in to change notification settings - Fork 0
/
loss.py
23 lines (17 loc) · 1.01 KB
/
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
import torch
def dice_loss(pred, target):
"""Cacluate dice loss
Parameters
----------
pred:
predictions from the model
target:
ground truth label
"""
smooth = 1.
p_flat = pred.contiguous().view(-1)
t_flat = target.contiguous().view(-1)
intersection = (p_flat * t_flat).sum()
a_sum = torch.sum(p_flat * p_flat)
b_sum = torch.sum(t_flat * t_flat)
return 1 - ((2. * intersection + smooth) / (a_sum + b_sum + smooth) )