-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmetrics.py
183 lines (142 loc) · 5.81 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
171
172
173
174
175
176
177
178
179
180
181
182
183
import tensorflow as tf
import numpy as np
def tpr(label, prediction):
"""calculate true positive rate between label and prediction
TPR = TP / (TP + FN)
Parameters
----------
label : tensor
tensor of shape [batch_size, height, width, depth] containing the ground truth label
prediction : tensor
tensor of shape [batch_size, height, width, depth] containing the class prediction made by the network
Returns
----------
coeff : float
calculated true negative rate
"""
with tf.variable_scope("calc_tpr"):
smoothing = 1e-5
axis = [1, 2]
tp = tf.reduce_sum(tf.multiply(prediction, label), axis=axis)
fn = tf.reduce_sum(tf.cast(tf.logical_and(tf.equal(label, tf.ones_like(label)),
tf.equal(prediction, tf.zeros_like(prediction))), "float"), axis=axis)
coeff = tf.reduce_mean((tp + smoothing) / (tp + fn + smoothing))
return coeff
def tnr(label, prediction):
"""calculate true negative rate between label and prediction
TNR = TN / (TN + FP)
Parameters
----------
label : tensor
tensor of shape [batch_size, height, width, depth] containing the ground truth label
prediction : tensor
tensor of shape [batch_size, height, width, depth] containing the class prediction made by the network
Returns
----------
coeff : float
calculated true negative rate
"""
with tf.variable_scope("calc_tnr"):
smoothing = 1e-5
axis = [1, 2]
tn = tf.reduce_sum(tf.cast(tf.logical_and(tf.equal(label, tf.zeros_like(label)),
tf.equal(prediction, tf.zeros_like(prediction))), "float"), axis=axis)
fp = tf.reduce_sum(tf.cast(tf.logical_and(tf.equal(label, tf.zeros_like(label)),
tf.equal(prediction, tf.ones_like(prediction))), "float"), axis=axis)
coeff = tf.reduce_mean((tn + smoothing) / (tn + fp + smoothing))
return coeff
def iou_coeff(label, prediction):
"""calculate intersection over union (jaccard index) between label and prediction
IOU = Intersection / Union = |Label AND Prediction| / |Label OR Prediction|
Parameters
----------
label : tensor
tensor of shape [batch_size, height, width, depth] containing the ground truth label
prediction : tensor
tensor of shape [batch_size, height, width, depth] containing the class prediction made by the network
Returns
----------
coeff : float
calculated intersection over union coefficient (jaccard index)
"""
smoothing = 1e-5
axis = [1, 2]
intersec = tf.reduce_sum(tf.multiply(prediction, label), axis=axis)
union = tf.reduce_sum(tf.cast(tf.add(prediction, label) >= 1, dtype=tf.float32), axis=axis)
batch_iou = (intersec + smoothing) / (union + smoothing)
coeff = tf.reduce_mean(batch_iou)
return coeff
def dsc_coeff(label, prediction):
"""calculate dice sorensen coefficient between label and prediction
DSC = Intersection / (number of label elements + number of prediction elements)
Parameters
----------
label : tensor
tensor of shape [batch_size, height, width, depth] containing the ground truth label
prediction : tensor
tensor of shape [batch_size, height, width, depth] containing the class prediction made by the network
Returns
----------
coeff : float
calculated dice sorensen coefficient
"""
with tf.variable_scope("calc_dsc"):
smoothing = 1e-5
axis = [1, 2]
intersec = tf.reduce_sum(tf.multiply(prediction, label), axis=axis)
x = tf.reduce_sum(prediction, axis=axis)
y = tf.reduce_sum(label, axis=axis)
batch_dice = (2.0 * intersec + smoothing) / (x + y + smoothing)
coeff = tf.reduce_mean(batch_dice)
return coeff
def directed_hausdorff(A, B):
"""calculate directed hausdorff distance h(A,B) between point sets A and B
h(A, B) = max(min(d(a, b)))
where d(a, b) is L2 Norm between points a and b.
Point sets A and B may have a different number of rows, but must have the same dimension (number of columns)
Parameters
----------
A : ndarray
array of shape [m, dim] where m denotes the number of points in point set A and dim the dimension of the point
set
B : ndarray
array of shape [n, dim] where n denotes the number of points in point set B and dim the dimension of the point
set
Returns
----------
distance : float
calculated directed hausdorff distance
"""
m = np.shape(A)[0]
n = np.shape(B)[0]
dim = np.shape(A)[1]
dist = []
for k in range(m):
C = np.ones([n, 1]) * A[k, :]
D = np.multiply((C - B), (C - B))
D = np.sqrt(D.dot(np.ones([dim, 1])))
dist.append(np.min(D))
distance = np.max(dist)
return distance
def hd_distance(label, prediction):
"""calculate hausdorff distance between label and prediction
HD = max(h(A,B), h(B,A))
where A and B are point sets containing the indices of positive class labels and predictions and h(A,B) beeing the
directed hausdorff distance calculated by function "directed_hausdorff(A, B)"
Parameters
----------
label : numpy array
array of shape [batch_size, height, width] containing the ground truth label
prediction : tensor
array of shape [batch_size, height, width] containing the class prediction made by the network
Returns
----------
coeff : float
calculated hausdorff distance
"""
A_indices = np.argwhere(label[0, :, :] > 0)
B_indices = np.argwhere(prediction[0, :, :] > 0)
AB = directed_hausdorff(A_indices, B_indices)
BA = directed_hausdorff(B_indices, A_indices)
coeff = max(AB, BA)
return coeff