-
Notifications
You must be signed in to change notification settings - Fork 4
/
metrics.py
170 lines (118 loc) · 4.14 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import numpy as np
from scipy import spatial
import torch
import torch.nn as nn
def dice_coeff(im1, im2, empty_score=1.0):
"""Calculates the dice coefficient for the images"""
im1 = np.asarray(im1).astype(np.bool)
im2 = np.asarray(im2).astype(np.bool)
if im1.shape != im2.shape:
raise ValueError("Shape mismatch: im1 and im2 must have the same shape.")
im1 = im1 > 0.5
im2 = im2 > 0.5
im_sum = im1.sum() + im2.sum()
if im_sum == 0:
return empty_score
# Compute Dice coefficient
intersection = np.logical_and(im1, im2)
#print(im_sum)
return 2. * intersection.sum() / im_sum
def numeric_score(prediction, groundtruth):
"""Computes scores:
FP = False Positives
FN = False Negatives
TP = True Positives
TN = True Negatives
return: FP, FN, TP, TN"""
FP = np.float(np.sum((prediction == 1) & (groundtruth == 0)))
FN = np.float(np.sum((prediction == 0) & (groundtruth == 1)))
TP = np.float(np.sum((prediction == 1) & (groundtruth == 1)))
TN = np.float(np.sum((prediction == 0) & (groundtruth == 0)))
return FP, FN, TP, TN
def accuracy_score(prediction, groundtruth):
"""Getting the accuracy of the model"""
FP, FN, TP, TN = numeric_score(prediction, groundtruth)
N = FP + FN + TP + TN
accuracy = np.divide(TP + TN, N)
return accuracy * 100.0
def get_accuracy(SR, GT, threshold=0.5):
SR = SR.view(-1)
GT = GT.view(-1)
SR = SR > threshold
GT = GT == torch.max(GT)
corr = torch.sum(SR == GT)
# tensor_size = SR.size(0)*SR.size(1)*SR.size(2)*SR.size(3)
acc = float(corr) / float(SR.size(0))
return acc
def get_sensitivity(SR, GT, threshold=0.5):
# Sensitivity == Recall
SR = SR.view(-1)
GT = GT.view(-1)
SR = SR > threshold
GT = GT == torch.max(GT)
# TP : True Positive
# FN : False Negative
TP = ((SR == True) & (GT == True)) == True
FN = ((SR == False) & (GT == True)) == True
SE = float(torch.sum(TP)) / (float(torch.sum(TP + FN)) + 1e-6)
return SE
def get_specificity(SR, GT, threshold=0.5):
SR = SR.view(-1)
GT = GT.view(-1)
SR = SR > threshold
GT = GT == torch.max(GT)
# TN : True Negative
# FP : False Positive
TN = ((SR == False) & (GT == False)) == True
FP = ((SR == True) & (GT == False)) == True
SP = float(torch.sum(TN)) / (float(torch.sum(TN + FP)) + 1e-6)
return SP
def get_precision(SR, GT, threshold=0.5):
SR = SR.view(-1)
GT = GT.view(-1)
SR = SR > threshold
GT = GT == torch.max(GT)
# TP : True Positive
# FP : False Positive
TP = ((SR == True) & (GT == True)) == True
FP = ((SR == True) & (GT == False)) == True
PC = float(torch.sum(TP)) / (float(torch.sum(TP + FP)) + 1e-6)
return PC
def get_F1(SR, GT, threshold=0.5):
# Sensitivity == Recall
SE = get_sensitivity(SR, GT, threshold=threshold)
PC = get_precision(SR, GT, threshold=threshold)
F1 = 2 * SE * PC / (SE + PC + 1e-6)
return F1
def get_JS(SR, GT, threshold=0.5):
# JS : Jaccard similarity
SR = SR.view(-1)
GT = GT.view(-1)
SR = SR > threshold
GT = GT == torch.max(GT)
Inter = torch.sum((SR + GT) == 2)
Union = torch.sum((SR + GT) >= 1)
JS = float(Inter) / (float(Union) + 1e-6)
return JS
def get_DC(SR, GT, threshold=0.5):
# DC : Dice Coefficient
SR = SR.view(-1)
GT = GT.view(-1)
SR = SR > threshold
GT = GT == torch.max(GT)
Inter = torch.sum((SR & GT) == True)
DC = float(2 * Inter) / (float(torch.sum(SR) + torch.sum(GT)) + 1e-6)
return DC
def get_IOU(SR,GT,threshold=0.5):
SR = SR.view(-1)
GT = GT.view(-1)
SR = SR > threshold
GT = GT == torch.max(GT)
# TP : True Positive
# FP : False Positive
# FN : False Negative
TP = ((SR == True) & (GT == True)) == True
FN = ((SR == False) & (GT == True)) == True
FP = ((SR == True) & (GT == False)) == True
IOU = float(torch.sum(TP))/(float(torch.sum(TP+FP+FN)) + 1e-6)
return IOU