forked from seanie12/SWEP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval_bio.py
152 lines (122 loc) · 5.37 KB
/
eval_bio.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 os
import argparse
import torch
from tqdm import tqdm
from transformers import (AutoModel, AutoModelForQuestionAnswering,
AutoTokenizer)
from models import VariationalBert, VariationalElectra
from mrqa_utils import evaluate, read_answers, read_predictions
from run_squad import get_dataloader, to_list
from squad_metrics import (SquadResult, compute_predictions_logits,
squad_evaluate)
from squad_utils import (convert_examples_to_features, read_mrqa_examples,
read_squad_examples)
def run(args):
ckpt = torch.load(args.ckpt_file, map_location="cpu")
state_dict = ckpt["state_dict"]
model_args = ckpt["args"]
device = torch.cuda.current_device()
if args.baseline:
model = AutoModelForQuestionAnswering.from_pretrained(model_args.bert_model)
else:
if model_args.electra:
model = VariationalElectra(model_args)
else:
model = VariationalBert(model_args)
model.load_state_dict(state_dict)
model.eval()
model = model.to(device)
prefix = ""
tokenizer = AutoTokenizer.from_pretrained(args.bert_model)
examples = read_mrqa_examples(args.test_file,
debug=args.debug,
is_training=False)
features = convert_examples_to_features(examples, tokenizer,
max_seq_length=args.max_seq_length,
max_query_length=args.max_query_length,
doc_stride=args.doc_stride,
is_training=False)
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
eval_dataloader = get_dataloader(args, features, is_training=False)
# Eval!
all_results = []
for batch in tqdm(eval_dataloader, desc="Evaluating"):
model.eval()
input_ids, mask, seg_ids, feature_indices = batch
length = torch.sum(mask, 1)
max_length = torch.max(length)
input_ids = input_ids[:, :max_length].to(device)
mask = mask[:, :max_length].to(device)
seg_ids = seg_ids[:, :max_length].to(device)
with torch.no_grad():
inputs = {
"input_ids": input_ids,
"attention_mask": mask,
"token_type_ids": seg_ids,
}
outputs = model(**inputs)
outputs = (outputs[0], outputs[1])
for i, feature_index in enumerate(feature_indices):
eval_feature = features[feature_index.item()]
unique_id = int(eval_feature.unique_id)
output = [to_list(output[i]) for output in outputs]
start_logits, end_logits = output
result = SquadResult(unique_id, start_logits, end_logits)
all_results.append(result)
# Compute predictions
output_prediction_file = os.path.join(
args.output_dir, "predictions_{}.json".format(prefix))
output_nbest_file = os.path.join(
args.output_dir, "nbest_predictions_{}.json".format(prefix))
if args.version_2_with_negative:
output_null_log_odds_file = os.path.join(
args.output_dir, "null_odds_{}.json".format(prefix))
else:
output_null_log_odds_file = None
predictions = compute_predictions_logits(
examples,
features,
all_results,
args.n_best_size,
args.max_answer_length,
args.do_lower_case,
output_prediction_file,
output_nbest_file,
output_null_log_odds_file,
args.verbose_logging,
args.version_2_with_negative,
args.null_score_diff_threshold,
tokenizer,
)
answers = read_answers(args.test_file)
metrics = evaluate(answers, predictions, args.skip_no_answer)
eval_file = os.path.join(args.output_dir, "bioasq_metrics.txt")
with open(eval_file, "w") as f:
for k, v in metrics.items():
str_format = "{}: {:.4f}".format(k, v)
print(str_format)
f.write(str_format + "\n")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--ckpt_file", required=True, type=str)
parser.add_argument("--bert_model", type=str, default="bert-base-uncased")
parser.add_argument("--batch_size", type=int, default=32)
parser.add_argument("--debug", action="store_true")
parser.add_argument("--output_dir", type=str, required=True)
parser.add_argument("--test_file", type=str,
default="./mrqa-data/BioASQ.jsonl.gz")
parser.add_argument("--skip_no_answer", action="store_true")
parser.add_argument("--version_2_with_negative", action="store_true")
parser.add_argument("--n_best_size", type=int, default=20)
parser.add_argument("--verbose_logging", action="store_true")
parser.add_argument("--max_answer_length", type=int, default=30)
parser.add_argument("--null_score_diff_threshold", type=float, default=0.0)
parser.add_argument("--doc_stride", type=int, default=128)
parser.add_argument("--max_seq_length", type=int, default=384)
parser.add_argument("--max_query_length", type=int, default=64)
parser.add_argument("--baseline", action="store_true")
parser.add_argument("--var", action="store_true")
args = parser.parse_args()
args.do_lower_case = True
run(args)