forked from emma-sjwang/BEAL
-
Notifications
You must be signed in to change notification settings - Fork 1
/
metrics.py
executable file
·92 lines (74 loc) · 2.94 KB
/
metrics.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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import torch
import numpy as np
bce = torch.nn.BCEWithLogitsLoss(reduction='none')
def _upscan(f):
for i, fi in enumerate(f):
if fi == np.inf: continue
for j in range(1,i+1):
x = fi+j*j
if f[i-j] < x: break
f[i-j] = x
def dice_coefficient_numpy(binary_segmentation, binary_gt_label):
'''
Compute the Dice coefficient between two binary segmentation.
Dice coefficient is defined as here: https://en.wikipedia.org/wiki/S%C3%B8rensen%E2%80%93Dice_coefficient
Input:
binary_segmentation: binary 2D numpy array representing the region of interest as segmented by the algorithm
binary_gt_label: binary 2D numpy array representing the region of interest as provided in the database
Output:
dice_value: Dice coefficient between the segmentation and the ground truth
'''
# turn all variables to booleans, just in case
binary_segmentation = np.asarray(binary_segmentation, dtype=np.bool)
binary_gt_label = np.asarray(binary_gt_label, dtype=np.bool)
# compute the intersection
intersection = np.logical_and(binary_segmentation, binary_gt_label)
# count the number of True pixels in the binary segmentation
segmentation_pixels = float(np.sum(binary_segmentation.flatten()))
# same for the ground truth
gt_label_pixels = float(np.sum(binary_gt_label.flatten()))
# same for the intersection
intersection = float(np.sum(intersection.flatten()))
# compute the Dice coefficient
dice_value = (2 * intersection + 1.0) / (1.0 + segmentation_pixels + gt_label_pixels)
# return it
return dice_value
def dice_coeff(pred, target):
"""This definition generalize to real valued pred and target vector.
This should be differentiable.
pred: tensor with first dimension as batch
target: tensor with first dimension as batch
"""
target = target.data.cpu()
pred = torch.sigmoid(pred)
pred = pred.data.cpu()
pred[pred > 0.5] = 1
pred[pred <= 0.5] = 0
return dice_coefficient_numpy(pred, target)
def dice_coeff_2label(pred, target):
"""This definition generalize to real valued pred and target vector.
This should be differentiable.
pred: tensor with first dimension as batch
target: tensor with first dimension as batch
"""
target = target.data.cpu()
pred = torch.sigmoid(pred)
pred = pred.data.cpu()
pred[pred > 0.75] = 1
pred[pred <= 0.75] = 0
# print target.shape
# print pred.shape
return dice_coefficient_numpy(pred[:, 0, ...], target[:, 0, ...]), dice_coefficient_numpy(pred[:, 1, ...], target[:, 1, ...])
def DiceLoss(input, target):
'''
in tensor fomate
:param input:
:param target:
:return:
'''
smooth = 1.
iflat = input.contiguous().view(-1)
tflat = target.contiguous().view(-1)
intersection = (iflat * tflat).sum()
return 1 - ((2. * intersection + smooth) /
(iflat.sum() + tflat.sum() + smooth))