-
Notifications
You must be signed in to change notification settings - Fork 9
/
evaluate.py
188 lines (154 loc) · 7.22 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
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
import os
import json
import random
from argparse import ArgumentParser
import numpy as np
import tqdm
import torch
from torch.utils.data import DataLoader
from transformers import AutoTokenizer
from model import GenerativeModel
from config import Config
from data import IEDataset
from constants import *
from util import *
import ree_eval
import scirex_eval
# configuration
parser = ArgumentParser()
parser.add_argument('--gpu', type=int, required=True)
parser.add_argument('--checkpoint', type=str, required=True)
args = parser.parse_args()
use_gpu = args.gpu > -1
checkpoint = torch.load(args.checkpoint, map_location=f'cuda:{args.gpu}' if use_gpu else 'cpu')
config = Config.from_dict(checkpoint['config'])
# set GPU device
config.gpu_device = args.gpu
config.use_gpu = use_gpu
# fix random seed
random.seed(config.seed)
np.random.seed(config.seed)
torch.manual_seed(config.seed)
torch.backends.cudnn.enabled = False
if use_gpu and config.gpu_device >= 0:
torch.cuda.set_device(config.gpu_device)
# datasets
model_name = config.bert_model_name
tokenizer = AutoTokenizer.from_pretrained(model_name,
cache_dir=config.bert_cache_dir)
tokenizer.add_tokens(SPECIAL_TOKENS)
# special_tokens_dict = {'additional_special_tokens': SPECIAL_TOKENS}
# num_added_toks = tokenizer.add_special_tokens(special_tokens_dict)
# print('==============Prepare Training Set=================')
# train_set = IEDataset(config.train_file, max_length=config.max_length, gpu=use_gpu)
print('==============Prepare Dev Set=================')
dev_set = IEDataset(config.dev_file, max_length=config.max_length, gpu=use_gpu)
print('==============Prepare Test Set=================')
test_set = IEDataset(config.test_file, max_length=config.max_length, gpu=use_gpu)
vocabs = {}
# print('==============Prepare Training Set=================')
# train_set.numberize(tokenizer, vocabs)
print('==============Prepare Dev Set=================')
dev_set.numberize(tokenizer, vocabs)
print('==============Prepare Test Set=================')
test_set.numberize(tokenizer, vocabs)
if config.task == ROLE_FILLER_ENTITY_EXTRACTION:
grit_dev = read_grit_gold_file(config.grit_dev_file)
grit_test = read_grit_gold_file(config.grit_test_file)
elif config.task in {BINARY_RELATION_EXTRACTION, FOUR_ARY_RELATION_EXTRACTION}:
scirex_dev = read_scirex_gold_file(config.scirex_dev_file)
scirex_test = read_scirex_gold_file(config.scirex_test_file)
dev_batch_num = len(dev_set) // config.eval_batch_size + \
(len(dev_set) % config.eval_batch_size != 0)
test_batch_num = len(test_set) // config.eval_batch_size + \
(len(test_set) % config.eval_batch_size != 0)
output_dir = '/'.join(args.checkpoint.split('/')[:-1])
dev_result_file = os.path.join(output_dir, 'dev.out.json')
test_result_file = os.path.join(output_dir, 'test.out.json')
# initialize the model
model = GenerativeModel(config, vocabs)
model.load_bert(model_name, cache_dir=config.bert_cache_dir, tokenizer=tokenizer)
if not model_name.startswith('roberta'):
model.bert.resize_token_embeddings(len(tokenizer))
model.load_state_dict(checkpoint['model'], strict=True)
if use_gpu:
model.cuda(device=config.gpu_device)
epoch = 1000
# dev set
progress = tqdm.tqdm(total=dev_batch_num, ncols=75,
desc='Dev {}'.format(epoch))
dev_gold_outputs, dev_pred_outputs, dev_input_tokens, dev_doc_ids, dev_documents = [], [], [], [], []
for batch in DataLoader(dev_set, batch_size=config.eval_batch_size,
shuffle=False, collate_fn=dev_set.collate_fn):
progress.update(1)
outputs = model.predict(batch, tokenizer,epoch=epoch)
decoder_inputs_outputs = generate_decoder_inputs_outputs(batch, tokenizer, model, use_gpu, config.max_position_embeddings, task=config.task)
dev_pred_outputs.extend(outputs['decoded_ids'].tolist())
dev_gold_outputs.extend(decoder_inputs_outputs['decoder_labels'].tolist())
dev_input_tokens.extend(batch.input_tokens)
dev_doc_ids.extend(batch.doc_ids)
dev_documents.extend(batch.document)
progress.close()
dev_result = {
'pred_outputs': dev_pred_outputs,
'gold_outputs': dev_gold_outputs,
'input_tokens': dev_input_tokens,
'doc_ids': dev_doc_ids,
'documents': dev_documents
}
with open(dev_result_file ,'w') as f:
f.write(json.dumps(dev_result))
if config.task == EVENT_TEMPLATE_EXTRACTION:
dev_scores = 0
elif config.task == ROLE_FILLER_ENTITY_EXTRACTION:
ree_preds = construct_outputs_for_ceaf(dev_pred_outputs, dev_input_tokens, dev_doc_ids, tokenizer)
dev_scores = ree_eval.ree_eval(ree_preds, grit_dev)
elif config.task == BINARY_RELATION_EXTRACTION:
bre_preds = construct_outputs_for_scirex(dev_pred_outputs, dev_documents, dev_doc_ids, tokenizer, task=BINARY_RELATION_EXTRACTION)
dev_scores = scirex_eval.scirex_eval(bre_preds, scirex_dev, cardinality=2)
elif config.task == FOUR_ARY_RELATION_EXTRACTION:
bre_preds = construct_outputs_for_scirex(dev_pred_outputs, dev_documents, dev_doc_ids, tokenizer, task=FOUR_ARY_RELATION_EXTRACTION)
dev_scores = scirex_eval.scirex_eval(bre_preds, scirex_dev, cardinality=4)
else:
raise NotImplementedError
save_model = False
# test set
progress = tqdm.tqdm(total=test_batch_num, ncols=75,
desc='Test {}'.format(epoch))
test_gold_outputs, test_pred_outputs, test_input_tokens, test_doc_ids, test_documents = [], [], [], [], []
test_loss = 0
for batch in DataLoader(test_set, batch_size=config.eval_batch_size, shuffle=False,
collate_fn=test_set.collate_fn):
progress.update(1)
outputs = model.predict(batch, tokenizer, epoch=epoch)
decoder_inputs_outputs = generate_decoder_inputs_outputs(batch, tokenizer, model, use_gpu, config.max_position_embeddings, task=config.task)
test_pred_outputs.extend(outputs['decoded_ids'].tolist())
test_gold_outputs.extend(decoder_inputs_outputs['decoder_labels'].tolist())
test_input_tokens.extend(batch.input_tokens)
test_doc_ids.extend(batch.doc_ids)
test_documents.extend(batch.document)
progress.close()
# currently use negative dev loss as validation criteria
if config.task == EVENT_TEMPLATE_EXTRACTION:
# TODO: call the official evaluator
test_scores = 0
elif config.task == ROLE_FILLER_ENTITY_EXTRACTION:
ree_preds = construct_outputs_for_ceaf(test_pred_outputs, test_input_tokens, test_doc_ids, tokenizer)
test_scores = ree_eval.ree_eval(ree_preds, grit_test)
elif config.task == BINARY_RELATION_EXTRACTION:
bre_preds = construct_outputs_for_scirex(test_pred_outputs, test_documents, test_doc_ids, tokenizer, task=BINARY_RELATION_EXTRACTION)
test_scores = scirex_eval.scirex_eval(bre_preds, scirex_test, cardinality=2)
elif config.task == FOUR_ARY_RELATION_EXTRACTION:
bre_preds = construct_outputs_for_scirex(test_pred_outputs, test_documents, test_doc_ids, tokenizer, task=FOUR_ARY_RELATION_EXTRACTION)
test_scores = scirex_eval.scirex_eval(bre_preds, scirex_test, cardinality=4)
else:
raise NotImplementedError
test_result = {
'pred_outputs': test_pred_outputs,
'gold_outputs': test_gold_outputs,
'input_tokens': test_input_tokens,
'doc_ids': test_doc_ids,
'documents': test_documents
}
with open(test_result_file,'w') as f:
f.write(json.dumps(test_result))