-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.py
70 lines (59 loc) · 2.36 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
import torch
import torch.nn.functional as F
import matplotlib.pyplot as plt
import argparse
from sklearn.metrics import precision_recall_curve, accuracy_score
from pathlib import Path
from util import load_dataset
import pandas as pd
def pr_curve_coor(model, test_tokens, test_labels, device, batch_size=128):
model.to(device)
model.eval()
data_size = test_tokens.size(0)
y_probs = []
for i in range(0, data_size, batch_size):
token_ids = test_tokens[i:i + batch_size].to(device)
with torch.no_grad():
logits = model(token_ids)
logits = F.softmax(logits, dim=-1)
y_probs.append(logits)
y_probs = torch.cat(y_probs, 0).cpu()
test_labels = test_labels.cpu()
acc = accuracy_score(test_labels, y_probs.argmax(dim=-1))
y_true = F.one_hot(test_labels)
p, r, _ = precision_recall_curve(y_true.flatten(), y_probs.flatten())
return p, r, acc
def evaluate(filenames, test_tokens, test_labels, device, save_path="pr.png"):
names = []
accs = []
for filename in filenames:
model = torch.load(filename)
model_name = model.__class__.__name__
print(f"Evaling {model_name}...")
model_name = model_name[:-5]
p, r, acc = pr_curve_coor(model, test_tokens, test_labels, device)
plt.plot(r, p, lw=1, label=model_name)
names.append(model_name)
accs.append(acc)
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.legend()
plt.savefig(save_path)
df = pd.DataFrame(accs, index=names, columns=['Accuracy'])
df = df.sort_values('Accuracy')
print(df)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--data_dir", type=str, default='data')
parser.add_argument("--model_dir", type=str, default='output')
parser.add_argument("--name", type=str, default='prcurve.png',
help='Path to save PR-Curve, such as XXX/XXX.png, XXX.svg, XX.jpg')
parser.add_argument("--no_cuda", action='store_true')
args = parser.parse_args()
device = torch.device('cuda' if torch.cuda.is_available()
and not args.no_cuda else 'cpu')
test_tokens, test_labels = load_dataset(Path(args.data_dir), 'test')
filenames = Path(args.model_dir).glob('*.pkl')
evaluate(filenames, test_tokens, test_labels, device, args.name)
if __name__ == "__main__":
main()