-
Notifications
You must be signed in to change notification settings - Fork 225
/
Metrics.py
321 lines (256 loc) · 15.7 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Import useful packages
import tensorflow as tf
def evaluation(y, prediction):
'''
This is the evaluation metrics.
Here, we provide a four-class classification evaluation codes
The files will be updated to adapt multiply classes soon.
Notice: All the evaluations during training will be saved in the TensorBoard.
Use
tensorboard --logdir="Your_Checkpoint_File_Address" --host=127.0.0.1
to view and download the evaluation files.
BTW, you can definitely edit the following codes to adapt your own classes,
e.g., if Two classes, u can just delete the three and four class.
Args:
y: The True Labels (with One-hot representation)
prediction: The predicted output probability
Returns:
Single class evaluation: T1_accuracy, T1_Precision, T1_Recall, T1_F_Score,
T2_accuracy, T2_Precision, T2_Recall, T2_F_Score,
T3_accuracy, T3_Precision, T3_Recall, T3_F_Score,
T4_accuracy, T4_Precision, T4_Recall, T4_F_Score,
Global model evaluation: Global_Average_Accuracy,
Kappa_Metric,
Macro_Global_Precision,
Macro_Global_Recall,
Macro_Global_F1_Score
'''
# Calculate Accuracy
# Add metrics to TensorBoard.
with tf.name_scope('Evalution'):
# Calculate Each Task Accuracy
with tf.name_scope('Each_Class_accuracy'):
# Task 1 Accuracy
with tf.name_scope('T1_accuracy'):
# Number of Classified Correctly
y_T1 = tf.equal(tf.argmax(y, 1), 0)
prediction_T1 = tf.equal(tf.argmax(prediction, 1), 0)
T1_Corrected_Num = tf.reduce_sum(tf.cast(tf.math.logical_and(y_T1, prediction_T1), tf.float32))
# Number of All the Test Samples
T1_all_Num = tf.reduce_sum(tf.cast(y_T1, tf.float32))
# Task 1 Accuracy
T1_accuracy = tf.divide(T1_Corrected_Num, T1_all_Num)
tf.summary.scalar('T1_accuracy', T1_accuracy)
T1_TP = T1_Corrected_Num
T1_TN = tf.reduce_sum(
tf.cast(tf.math.logical_and(tf.math.logical_not(y_T1), tf.math.logical_not(prediction_T1)),
tf.float32))
T1_FP = tf.reduce_sum(
tf.cast(tf.math.logical_and(tf.math.logical_not(y_T1), prediction_T1), tf.float32))
T1_FN = tf.reduce_sum(
tf.cast(tf.math.logical_and(y_T1, tf.math.logical_not(prediction_T1)), tf.float32))
with tf.name_scope("T1_Precision"):
T1_Precision = T1_TP / (T1_TP + T1_FP)
tf.summary.scalar('T1_Precision', T1_Precision)
with tf.name_scope("T1_Recall"):
T1_Recall = T1_TP / (T1_TP + T1_FN)
tf.summary.scalar('T1_Recall', T1_Recall)
with tf.name_scope("T1_F_Score"):
T1_F_Score = (2 * T1_Precision * T1_Recall) / (T1_Precision + T1_Recall)
tf.summary.scalar('T1_F_Score', T1_F_Score)
# Task 2 Accuracy
with tf.name_scope('T2_accuracy'):
# Number of Classified Correctly
y_T2 = tf.equal(tf.argmax(y, 1), 1)
prediction_T2 = tf.equal(tf.argmax(prediction, 1), 1)
T2_Corrected_Num = tf.reduce_sum(tf.cast(tf.math.logical_and(y_T2, prediction_T2), tf.float32))
# Number of All the Test Samples
T2_all_Num = tf.reduce_sum(tf.cast(y_T2, tf.float32))
# Task 2 Accuracy
T2_accuracy = tf.divide(T2_Corrected_Num, T2_all_Num)
tf.summary.scalar('T2_accuracy', T2_accuracy)
T2_TP = T2_Corrected_Num
T2_TN = tf.reduce_sum(
tf.cast(tf.math.logical_and(tf.math.logical_not(y_T2), tf.math.logical_not(prediction_T2)),
tf.float32))
T2_FP = tf.reduce_sum(
tf.cast(tf.math.logical_and(tf.math.logical_not(y_T2), prediction_T2), tf.float32))
T2_FN = tf.reduce_sum(
tf.cast(tf.math.logical_and(y_T2, tf.math.logical_not(prediction_T2)), tf.float32))
with tf.name_scope("T2_Precision"):
T2_Precision = T2_TP / (T2_TP + T2_FP)
tf.summary.scalar('T2_Precision', T2_Precision)
with tf.name_scope("T2_Recall"):
T2_Recall = T2_TP / (T2_TP + T2_FN)
tf.summary.scalar('T2_Recall', T2_Recall)
with tf.name_scope("T2_F_Score"):
T2_F_Score = (2 * T2_Precision * T2_Recall) / (T2_Precision + T2_Recall)
tf.summary.scalar('T2_F_Score', T2_F_Score)
# Task 3 Accuracy
with tf.name_scope('T3_accuracy'):
# Number of Classified Correctly
y_T3 = tf.equal(tf.argmax(y, 1), 2)
prediction_T3 = tf.equal(tf.argmax(prediction, 1), 2)
T3_Corrected_Num = tf.reduce_sum(tf.cast(tf.math.logical_and(y_T3, prediction_T3), tf.float32))
# Number of All the Test Samples
T3_all_Num = tf.reduce_sum(tf.cast(y_T3, tf.float32))
# Task 3 Accuracy
T3_accuracy = tf.divide(T3_Corrected_Num, T3_all_Num)
tf.summary.scalar('T3_accuracy', T3_accuracy)
T3_TP = T3_Corrected_Num
T3_TN = tf.reduce_sum(
tf.cast(tf.math.logical_and(tf.math.logical_not(y_T3), tf.math.logical_not(prediction_T3)),
tf.float32))
T3_FP = tf.reduce_sum(
tf.cast(tf.math.logical_and(tf.math.logical_not(y_T3), prediction_T3), tf.float32))
T3_FN = tf.reduce_sum(
tf.cast(tf.math.logical_and(y_T3, tf.math.logical_not(prediction_T3)), tf.float32))
with tf.name_scope("T3_Precision"):
T3_Precision = T3_TP / (T3_TP + T3_FP)
tf.summary.scalar('T3_Precision', T3_Precision)
with tf.name_scope("T3_Recall"):
T3_Recall = T3_TP / (T3_TP + T3_FN)
tf.summary.scalar('T3_Recall', T3_Recall)
with tf.name_scope("T3_F_Score"):
T3_F_Score = (2 * T3_Precision * T3_Recall) / (T3_Precision + T3_Recall)
tf.summary.scalar('T3_F_Score', T3_F_Score)
# Task 4 Accuracy
with tf.name_scope('T4_accuracy'):
# Number of Classified Correctly
y_T4 = tf.equal(tf.argmax(y, 1), 3)
prediction_T4 = tf.equal(tf.argmax(prediction, 1), 3)
T4_Corrected_Num = tf.reduce_sum(tf.cast(tf.math.logical_and(y_T4, prediction_T4), tf.float32))
# Number of All the Test Samples
T4_all_Num = tf.reduce_sum(tf.cast(y_T4, tf.float32))
# Task 4 Accuracy
T4_accuracy = tf.divide(T4_Corrected_Num, T4_all_Num)
tf.summary.scalar('T4_accuracy', T4_accuracy)
T4_TP = T4_Corrected_Num
T4_TN = tf.reduce_sum(
tf.cast(tf.math.logical_and(tf.math.logical_not(y_T4), tf.math.logical_not(prediction_T4)),
tf.float32))
T4_FP = tf.reduce_sum(
tf.cast(tf.math.logical_and(tf.math.logical_not(y_T4), prediction_T4), tf.float32))
T4_FN = tf.reduce_sum(
tf.cast(tf.math.logical_and(y_T4, tf.math.logical_not(prediction_T4)), tf.float32))
with tf.name_scope("T4_Precision"):
T4_Precision = T4_TP / (T4_TP + T4_FP)
tf.summary.scalar('T4_Precision', T4_Precision)
with tf.name_scope("T4_Recall"):
T4_Recall = T4_TP / (T4_TP + T4_FN)
tf.summary.scalar('T4_Recall', T4_Recall)
with tf.name_scope("T4_F_Score"):
T4_F_Score = (2 * T4_Precision * T4_Recall) / (T4_Precision + T4_Recall)
tf.summary.scalar('T4_F_Score', T4_F_Score)
# Calculate the Confusion Matrix
with tf.name_scope("Confusion_Matrix"):
with tf.name_scope("T1_Label"):
T1_T1 = T1_Corrected_Num
T1_T2 = tf.reduce_sum(tf.cast(tf.math.logical_and(y_T1, prediction_T2), tf.float32))
T1_T3 = tf.reduce_sum(tf.cast(tf.math.logical_and(y_T1, prediction_T3), tf.float32))
T1_T4 = tf.reduce_sum(tf.cast(tf.math.logical_and(y_T1, prediction_T4), tf.float32))
T1_T1_percent = tf.divide(T1_T1, T1_all_Num)
T1_T2_percent = tf.divide(T1_T2, T1_all_Num)
T1_T3_percent = tf.divide(T1_T3, T1_all_Num)
T1_T4_percent = tf.divide(T1_T4, T1_all_Num)
tf.summary.scalar('T1_T1_percent', T1_T1_percent)
tf.summary.scalar('T1_T2_percent', T1_T2_percent)
tf.summary.scalar('T1_T3_percent', T1_T3_percent)
tf.summary.scalar('T1_T4_percent', T1_T4_percent)
with tf.name_scope("T2_Label"):
T2_T1 = tf.reduce_sum(tf.cast(tf.math.logical_and(y_T2, prediction_T1), tf.float32))
T2_T2 = T2_Corrected_Num
T2_T3 = tf.reduce_sum(tf.cast(tf.math.logical_and(y_T2, prediction_T3), tf.float32))
T2_T4 = tf.reduce_sum(tf.cast(tf.math.logical_and(y_T2, prediction_T4), tf.float32))
T2_T1_percent = tf.divide(T2_T1, T2_all_Num)
T2_T2_percent = tf.divide(T2_T2, T2_all_Num)
T2_T3_percent = tf.divide(T2_T3, T2_all_Num)
T2_T4_percent = tf.divide(T2_T4, T2_all_Num)
tf.summary.scalar('T2_T1_percent', T2_T1_percent)
tf.summary.scalar('T2_T2_percent', T2_T2_percent)
tf.summary.scalar('T2_T3_percent', T2_T3_percent)
tf.summary.scalar('T2_T4_percent', T2_T4_percent)
with tf.name_scope("T3_Label"):
T3_T1 = tf.reduce_sum(tf.cast(tf.math.logical_and(y_T3, prediction_T1), tf.float32))
T3_T2 = tf.reduce_sum(tf.cast(tf.math.logical_and(y_T3, prediction_T2), tf.float32))
T3_T3 = T3_Corrected_Num
T3_T4 = tf.reduce_sum(tf.cast(tf.math.logical_and(y_T3, prediction_T4), tf.float32))
T3_T1_percent = tf.divide(T3_T1, T3_all_Num)
T3_T2_percent = tf.divide(T3_T2, T3_all_Num)
T3_T3_percent = tf.divide(T3_T3, T3_all_Num)
T3_T4_percent = tf.divide(T3_T4, T3_all_Num)
tf.summary.scalar('T3_T1_percent', T3_T1_percent)
tf.summary.scalar('T3_T2_percent', T3_T2_percent)
tf.summary.scalar('T3_T3_percent', T3_T3_percent)
tf.summary.scalar('T3_T4_percent', T3_T4_percent)
with tf.name_scope("T4_Label"):
T4_T1 = tf.reduce_sum(tf.cast(tf.math.logical_and(y_T4, prediction_T1), tf.float32))
T4_T2 = tf.reduce_sum(tf.cast(tf.math.logical_and(y_T4, prediction_T2), tf.float32))
T4_T3 = tf.reduce_sum(tf.cast(tf.math.logical_and(y_T4, prediction_T3), tf.float32))
T4_T4 = T4_Corrected_Num
T4_T1_percent = tf.divide(T4_T1, T4_all_Num)
T4_T2_percent = tf.divide(T4_T2, T4_all_Num)
T4_T3_percent = tf.divide(T4_T3, T4_all_Num)
T4_T4_percent = tf.divide(T4_T4, T4_all_Num)
tf.summary.scalar('T4_T1_percent', T4_T1_percent)
tf.summary.scalar('T4_T2_percent', T4_T2_percent)
tf.summary.scalar('T4_T3_percent', T4_T3_percent)
tf.summary.scalar('T4_T4_percent', T4_T4_percent)
with tf.name_scope('Global_Evalution_Metrics'):
# Global Average Accuracy - Simple Algorithm
with tf.name_scope('Global_Average_Accuracy'):
correct_prediction = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
Global_Average_Accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
tf.summary.scalar('Global_Average_Accuracy', Global_Average_Accuracy)
with tf.name_scope('Kappa_Metric'):
Test_Set_Num = T1_all_Num + T2_all_Num + T3_all_Num + T4_all_Num
Actual_T1 = T1_all_Num
Actual_T2 = T2_all_Num
Actual_T3 = T3_all_Num
Actual_T4 = T4_all_Num
Prediction_T1 = T1_T1 + T2_T1 + T3_T1 + T4_T1
Prediction_T2 = T1_T2 + T2_T2 + T3_T2 + T4_T2
Prediction_T3 = T1_T3 + T2_T3 + T3_T3 + T4_T3
Prediction_T4 = T1_T4 + T2_T4 + T3_T4 + T4_T4
p0 = (T1_T1 + T2_T2 + T3_T3 + T4_T4) / Test_Set_Num
pe = (Actual_T1 * Prediction_T1 + Actual_T2 * Prediction_T2 + Actual_T3 * Prediction_T3 + Actual_T4 * Prediction_T4) / \
(Test_Set_Num * Test_Set_Num)
Kappa_Metric = (p0 - pe) / (1 - pe)
tf.summary.scalar('Kappa_Metric', Kappa_Metric)
with tf.name_scope('Micro_Averaged_Evalution'):
with tf.name_scope("Micro_Averaged_Confusion_Matrix"):
TP_all = T1_TP + T2_TP + T3_TP + T4_TP
TN_all = T1_TN + T2_TN + T3_TN + T4_TN
FP_all = T1_FP + T2_FP + T3_FP + T4_FP
FN_all = T1_FN + T2_FN + T3_FN + T4_FN
with tf.name_scope("Micro_Global_Precision"):
Micro_Global_Precision = TP_all / (TP_all + FP_all)
tf.summary.scalar('Micro_Global_Precision', Micro_Global_Precision)
with tf.name_scope("Micro_Global_Recall"):
Micro_Global_Recall = TP_all / (TP_all + FN_all)
tf.summary.scalar('Micro_Global_Recall', Micro_Global_Recall)
with tf.name_scope("Micro_Global_F1_Score"):
Micro_Global_F1_Score = (2 * Micro_Global_Precision * Micro_Global_Recall) / (
Micro_Global_Precision + Micro_Global_Recall)
tf.summary.scalar('Micro_Global_F1_Score', Micro_Global_F1_Score)
with tf.name_scope('Macro_Averaged_Evalution'):
with tf.name_scope("Macro_Global_Precision"):
Macro_Global_Precision = (T1_Precision + T2_Precision + T3_Precision + T4_Precision) / 4
tf.summary.scalar('Macro_Global_Precision', Macro_Global_Precision)
with tf.name_scope("Macro_Global_Recall"):
Macro_Global_Recall = (T1_Recall + T2_Recall + T3_Recall + T4_Recall) / 4
tf.summary.scalar('Macro_Global_Recall', Macro_Global_Recall)
with tf.name_scope("Macro_Global_F1_Score"):
Macro_Global_F1_Score = (T1_F_Score + T2_F_Score + T3_F_Score + T4_F_Score) / 4
tf.summary.scalar('Macro_Global_F1_Score', Macro_Global_F1_Score)
# You DON'T have to return these because all the criterias have been saved in the TensorBoard
# return T1_accuracy, T1_Precision, T1_Recall, T1_F_Score, \
# T2_accuracy, T2_Precision, T2_Recall, T2_F_Score, \
# T3_accuracy, T3_Precision, T3_Recall, T3_F_Score, \
# T4_accuracy, T4_Precision, T4_Recall, T4_F_Score, \
# Global_Average_Accuracy, Kappa_Metric, \
# Macro_Global_Precision, Macro_Global_Recall, Macro_Global_F1_Score
# Instead, you can only return Accuracy Criteria to compare the capacity of your Model
return Global_Average_Accuracy