-
Notifications
You must be signed in to change notification settings - Fork 1
/
eval.py
138 lines (113 loc) · 4.41 KB
/
eval.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
import logging
logging.basicConfig(level='ERROR')
import numpy as np
from tqdm import tqdm
import json
from collections import defaultdict
import matplotlib.pyplot as plt
from sklearn.metrics import auc, roc_curve
import matplotlib
import random
import os
matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['ps.fonttype'] = 42
matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['ps.fonttype'] = 42
# plot data
def sweep(score, x):
"""
Compute a ROC curve and then return the FPR, TPR, AUC, and ACC.
"""
fpr, tpr, _ = roc_curve(x, -score)
acc = np.max(1-(fpr+(1-tpr))/2)
return fpr, tpr, auc(fpr, tpr), acc
def do_plot(prediction, answers, sweep_fn=sweep, metric='auc', legend="", output_dir=None):
"""
Generate the ROC curves by using ntest models as test models and the rest to train.
"""
fpr, tpr, auc, acc = sweep_fn(np.array(prediction), np.array(answers, dtype=bool))
low = tpr[np.where(fpr<.05)[0][-1]]
# bp()
print('Attack %s AUC %.4f, Accuracy %.4f, TPR@5%%FPR of %.4f\n'%(legend, auc,acc, low))
metric_text = ''
if metric == 'auc':
metric_text = 'auc=%.3f'%auc
elif metric == 'acc':
metric_text = 'acc=%.3f'%acc
plt.plot(fpr, tpr, label=legend+metric_text)
return legend, auc,acc, low
def fig_fpr_tpr(all_output, output_dir):
print("output_dir", output_dir)
answers = []
metric2predictions = defaultdict(list)
for ex in all_output:
answers.append(ex["label"])
for metric in ex["pred"].keys():
if ("raw" in metric) and ("clf" not in metric):
continue
metric2predictions[metric].append(ex["pred"][metric])
plt.figure(figsize=(4,3))
with open(f"{output_dir}/auc.txt", "w") as f:
for metric, predictions in metric2predictions.items():
legend, auc, acc, low = do_plot(predictions, answers, legend=metric, metric='auc', output_dir=output_dir)
f.write('%s AUC %.4f, Accuracy %.4f, TPR@5%%FPR of %.4f\n'%(legend, auc, acc, low))
plt.semilogx()
plt.semilogy()
plt.xlim(1e-5,1)
plt.ylim(1e-5,1)
plt.xlabel("False Positive Rate")
plt.ylabel("True Positive Rate")
plt.plot([0, 1], [0, 1], ls='--', color='gray')
plt.subplots_adjust(bottom=.18, left=.18, top=.96, right=.96)
plt.legend(fontsize=8)
plt.savefig(f"{output_dir}/auc.png")
def fig_fpr_tpr_img(all_output, output_dir):
print("output_dir", output_dir)
method_metrics = defaultdict(lambda: defaultdict(list))
for ex in all_output:
label = ex["label"]
for method, preds in ex["pred"].items():
for metric, prediction in preds.items():
if ("raw" in metric) and ("clf" not in metric):
continue
method_metrics[method][metric].append((prediction, label))
for method, metrics in method_metrics.items():
method_output_dir = f"{output_dir}/{method}"
os.makedirs(method_output_dir, exist_ok=True)
plt.figure(figsize=(4,3))
with open(f"{method_output_dir}/auc.txt", "w") as f:
for metric, data in metrics.items():
predictions, labels = zip(*data)
legend, auc, acc, low = do_plot(predictions, labels, legend=metric, metric='auc', output_dir=method_output_dir)
f.write(f'{legend} AUC {auc:.4f}, Accuracy {acc:.4f}, TPR@5% FPR of {low:.4f}\n')
plt.semilogx()
plt.semilogy()
plt.xlim(1e-5, 1)
plt.ylim(1e-5, 1)
plt.xlabel("False Positive Rate")
plt.ylabel("True Positive Rate")
plt.plot([0, 1], [0, 1], ls='--', color='gray')
plt.subplots_adjust(bottom=.18, left=.18, top=.96, right=.96)
plt.legend(fontsize=8)
plt.savefig(f"{method_output_dir}/auc.png")
plt.close()
def load_jsonl(input_path):
with open(input_path, 'r') as f:
data = [json.loads(line) for line in tqdm(f)]
random.seed(0)
random.shuffle(data)
return data
def dump_jsonl(data, path):
with open(path, 'w') as f:
for line in tqdm(data):
f.write(json.dumps(line) + "\n")
def read_jsonl(path):
with open(path, 'r') as f:
return [json.loads(line) for line in tqdm(f)]
def convert_huggingface_data_to_list_dic(dataset):
all_data = []
for i in range(len(dataset)):
ex = dataset[i]
all_data.append(ex)
random.shuffle(all_data)
return all_data