-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevaluate.py
126 lines (104 loc) · 4.32 KB
/
evaluate.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
from argparse import ArgumentParser
import torch
from torch.utils.data import DataLoader
from nltk.corpus import wordnet as wn
from wsd.data.dataset import WordSenseDisambiguationDataset
from wsd.data.processor import Processor
from wsd.models.model import SimpleModel
if __name__ == '__main__':
parser = ArgumentParser()
# Add data args.
parser.add_argument('--processor', type=str, required=True)
parser.add_argument('--model', type=str, required=True)
parser.add_argument('--model_input', type=str, required=True)
parser.add_argument('--model_output', type=str, required=True)
parser.add_argument('--evaluation_input', type=str, required=True)
# Add dataloader args.
parser.add_argument('--batch_size', type=int, default=32)
parser.add_argument('--num_workers', type=int, default=4)
# Other
parser.add_argument('--device', type=str, default='cuda')
# Store the arguments in hparams.
args = parser.parse_args()
processor = Processor.from_config(args.processor)
test_dataset = WordSenseDisambiguationDataset(args.model_input)
test_dataloader = DataLoader(
test_dataset,
batch_size=args.batch_size,
num_workers=args.num_workers,
collate_fn=processor.collate_sentences)
model = SimpleModel.load_from_checkpoint(args.model)
device = 'cuda' if torch.cuda.is_available() and args.device == 'cuda' else 'cpu'
model.to(device)
model.eval()
predictions = {}
with torch.no_grad():
for x, _ in test_dataloader:
x = {k: v.to(device) if not isinstance(v, list) else v for k, v in x.items()}
y = model(x)
batch_predictions = processor.decode(x, y)
predictions.update(batch_predictions)
predictions = sorted(list(predictions.items()), key=lambda kv: kv[0])
with open(args.model_output, 'w') as f:
for instance_id, synset_id in predictions:
f.write('{} {}\n'.format(instance_id, synset_id))
correct, total = 0, 0
gold = {}
pred = {}
with open(args.evaluation_input) as f_gold:
for line in f_gold:
instance_id, *gold_senses = line.strip().split()
gold_synsets = [wn.lemma_from_key(s).synset().name() for s in gold_senses]
gold[instance_id] = gold_synsets
with open(args.model_output) as f_pred:
for line in f_pred:
instance_id, pred_synset = line.strip().split()
pred[instance_id] = pred_synset
pos_correct = {
'NOUNs': 0,
'VERBs': 0,
'ADJs': 0,
'ADVs': 0,
}
pos_total = {
'NOUNs': 0,
'VERBs': 0,
'ADJs': 0,
'ADVs': 0,
}
for instance_id in gold:
if instance_id not in pred:
print('Warning: {} not in predictions.'.format(instance_id))
continue
total += 1
predicted_synset = pred[instance_id]
pos = predicted_synset.split('.')[1]
if pos == 'n':
pos_total['NOUNs'] += 1
elif pos == 'v':
pos_total['VERBs'] += 1
elif pos == 'r':
pos_total['ADVs'] += 1
elif pos == 'a' or pos == 's':
pos_total['ADJs'] += 1
if predicted_synset in gold[instance_id]:
correct += 1
if pos == 'n':
pos_correct['NOUNs'] += 1
elif pos == 'v':
pos_correct['VERBs'] += 1
elif pos == 'r':
pos_correct['ADVs'] += 1
elif pos == 'a' or pos == 's':
pos_correct['ADJs'] += 1
print()
print('Accuracy = {:0.3f}% ({}/{})'.format(100. * correct / total, correct, total))
if pos_total['NOUNs'] > 0:
print('NOUNs = {:0.3f}% ({}/{})'.format(100. * pos_correct['NOUNs'] / pos_total['NOUNs'], pos_correct['NOUNs'], pos_total['NOUNs']))
if pos_total['VERBs'] > 0:
print('VERBs = {:0.3f}% ({}/{})'.format(100. * pos_correct['VERBs'] / pos_total['VERBs'], pos_correct['VERBs'], pos_total['VERBs']))
if pos_total['ADJs'] > 0:
print('ADJs = {:0.3f}% ({}/{})'.format(100. * pos_correct['ADJs'] / pos_total['ADJs'], pos_correct['ADJs'], pos_total['ADJs']))
if pos_total['ADVs'] > 0:
print('ADVs = {:0.3f}% ({}/{})'.format(100. * pos_correct['ADVs'] / pos_total['ADVs'], pos_correct['ADVs'], pos_total['ADVs']))
print()