-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluator.py
152 lines (123 loc) · 4.42 KB
/
evaluator.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
import logging
import sys
import json
import numpy as np
from sklearn.metrics import precision_recall_curve, auc, confusion_matrix, f1_score, roc_curve
import matplotlib.pyplot as plt
def read_answers(filename):
answers={}
with open(filename) as f:
for line in f:
line=line.strip()
js=json.loads(line)
answers[js['idx']]=js['target']
return answers
def read_predictions(filename):
predictions={}
with open(filename) as f:
for line in f:
line=line.strip()
idx,label=line.split()
predictions[int(idx)]=float(label.strip('[]'))
return predictions
def read_binary_predictions(filename):
binary_predictions={}
with open(filename) as f:
for line in f:
line=line.strip()
idx,label=line.split()
binary_predictions[int(idx)]=int(label)
return binary_predictions
def calculate_scores(answers,binary_predictions):
Acc=[]
for key in answers:
if key not in binary_predictions:
logging.error("Missing prediction for index {}.".format(key))
sys.exit()
Acc.append(answers[key]==binary_predictions[key])
scores={}
scores['Acc']=np.mean(Acc)
return scores
#def calculate_PRAUC(answers, predictions):
# y_true=[]
# y_scores=[]
# answers_fit=list(answers.values())
# y_true = answers_fit
# predict_fit=list(predictions.values())
# y_scores=predict_fit
# y_true.append(answers[key]])
# y_true.append[predictions[key]])
# return y_true
def main():
import argparse
parser = argparse.ArgumentParser(description='Evaluate leaderboard predictions for Defect Detection dataset.')
parser.add_argument('--answers', '-a',help="filename of the labels, in txt format.")
parser.add_argument('--predictions_binary', '-b',help="filename of the leaderboard predictions, in txt format.")
parser.add_argument('--predictions', '-p',help="filename of the leaderboard predictions, in txt format.")
args = parser.parse_args()
answers=read_answers(args.answers)
predictions=read_predictions(args.predictions)
binary_predictions=read_binary_predictions(args.predictions_binary)
scores=calculate_scores(answers,binary_predictions)
# prauc=calculate_PRAUC(answers,predictions)
answers_fit=list(answers.values())
predict_fit=list(predictions.values())
# print(answers_fit)
#print(predict_fit)
print(scores)
#print(prauc)
# PR-AUC Curve
y_true=[]
y_scores=[]
y_true = answers_fit
y_scores=predict_fit
precision, recall, thresholds = precision_recall_curve(y_true, y_scores)
pr_auc = auc(recall, precision)
fig, ax = plt.subplots()
ax.plot(recall, precision, label=f'PR-AUC={pr_auc:.3f}')
ax.set_xlabel('Recall')
ax.set_ylabel('Precision')
ax.set_title('Precision-Recall Curve')
ax.legend(loc='lower left')
fig.savefig('pr_auc_curve.png')
plt.clf()
# F1 SCORE
predict_binary_fit=list(binary_predictions.values())
y_binary_scores = predict_binary_fit
f1 = f1_score(y_true, y_binary_scores)
print(f"F1-score: {f1:.2f}")
# Confusion Matrix
cm = confusion_matrix(y_true, y_binary_scores)
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
plt.imshow(cm, cmap=plt.cm.Blues, interpolation='nearest')
plt.colorbar()
tick_marks = np.arange(len(np.unique(y_true)))
plt.xticks(tick_marks, np.unique(y_true))
plt.yticks(tick_marks, np.unique(y_true))
plt.xlabel('Predicted')
plt.ylabel('True')
plt.title('Confusion Matrix')
thresh = cm.max() / 2.
for i, j in np.ndindex(cm.shape):
plt.text(j, i, format(cm[i, j], '.2f'),
ha="center", va="center",
color="white" if cm[i, j] > thresh else "black")
plt.savefig('confusion_matrix.png')
plt.clf()
# ROC Curve
fpr, tpr, thresholds = roc_curve(y_true, y_scores)
roc_auc = auc(fpr, tpr)
plt.figure()
plt.plot(fpr, tpr, color='darkorange', lw=2, label='ROC curve (area = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic (ROC)')
plt.legend(loc="lower right")
plt.savefig('roc_curve.png')
plt.clf()
if __name__ == '__main__':
predictions={}
main()