/
mkqa_eval.py
364 lines (304 loc) · 12.3 KB
/
mkqa_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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import argparse
import collections
import json
import logging
import os
import sys
from gzip import GzipFile
from typing import Dict, Optional, Any
import numpy as np
import mkqa_eval_util as eval_util
MKQA_LANGUAGES = [
"ar",
"da",
"de",
"en",
"es",
"fi",
"fr",
"he",
"hu",
"it",
"ja",
"km",
"ko",
"ms",
"nl",
"no",
"pl",
"pt",
"ru",
"sv",
"th",
"tr",
"vi",
"zh_cn",
"zh_hk",
"zh_tw",
]
# A data structure for storing annotations
MKQAAnnotation = collections.namedtuple(
"MKQAAnnotation",
[
"example_id", # The unique ID for each example.
"types", # All answer types selected by inter-grader agreement
"answers", # All valid answer strings
],
)
# A data structure for storing predictions
MKQAPrediction = collections.namedtuple(
"MKQAPrediction",
[
"example_id", # The unique ID for each example.
"prediction", # The predicted answer text ("" serves as No Answer)
"binary_answer", # Is answer {"yes", "no"} (case insensitive), or `None` (indicating neither)
"no_answer_prob",
# (Optional) Score/probability that the answer is No Answer. Used to select the best threshold that maximizes F1.
],
)
def parse_args():
parser = argparse.ArgumentParser("Official evaluation script for single language MKQA.")
parser.add_argument(
"-a",
"--annotation_file",
metavar="mkqa.jsonl.gz",
required=True,
help="Input annotations MKQA JSON Lines gzip file.",
)
parser.add_argument(
"-p",
"--predictions_file",
metavar="preds.json",
required=True,
help="Model predictions json line file",
)
parser.add_argument(
"-l",
"--language",
required=True,
choices=MKQA_LANGUAGES,
help=f"Language code. Accepts any of: {MKQA_LANGUAGES}",
)
parser.add_argument(
"-o",
"--out-dir",
metavar="results/",
help="Write accuracy metrics to file (default is stdout).",
)
parser.add_argument("-v", "--verbose", action="store_true")
parser.add_argument("--print_metrics", "-m", action="store_true", default=True)
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
return parser.parse_args()
def read_annotations(gold_path: str) -> Dict[str, Any]:
"""Read mkqa gold annotation for all languages
Args:
gold_path: path to mkqa annotation file, such as mkqa.jsonl.gz
Returns:
A mapping from example id to MKQAAnnotation for all languages
"""
assert os.path.exists(gold_path)
all_gold_annotations = collections.defaultdict(dict)
gzipped_input_file = open(gold_path, "rb")
with GzipFile(fileobj=gzipped_input_file) as input_file:
for line in input_file:
example = json.loads(line)
for language in MKQA_LANGUAGES:
valid_answers, answer_types = [], []
for answer in example["answers"][language]:
# Binary (Yes/No) answer text is always "yes" / "no"
# If answer['text'] is None then it `""` represents No Answer
valid_answers.append(answer["text"] or "")
valid_answers.extend(answer.get("aliases", []))
answer_types.append(answer["type"])
annotation = MKQAAnnotation(
example_id=str(example["example_id"]),
types=list(set(answer_types)),
answers=list(set(valid_answers)),
)
all_gold_annotations[language][annotation.example_id] = annotation
for lang, annotations in all_gold_annotations.items():
if len(annotations) != 10000:
logging.warning(
f"The annotations file you've provided contains {len(all_gold_annotations)} for language {lang} examples, where 10000 are expected."
)
return all_gold_annotations
def read_predictions(predictions_path: str) -> Dict[str, MKQAPrediction]:
"""Read model prediction json file
Args:
predictions_path: path to prediction json line file
{
"example_id": <example id>,
"prediction": <prediction text>,
"binary_answer": <"yes", "no", "">,
"no_answer_prob": <prob>
}
Returns:
A mapping from example id to MKQAPrediction
"""
assert os.path.exists(predictions_path)
logging.info(f"Reading predictions from file: {predictions_path}")
with open(predictions_path, "r") as f:
predictions = [json.loads(l) for l in f.readlines()]
predict_labels = {}
for prediction in predictions:
binary_answer = prediction["binary_answer"].lower() if prediction["binary_answer"] else None
assert binary_answer in {
"yes",
"no",
None,
}, f"Binary prediction can only be yes, no, or None. Provided answer was {binary_answer}"
predict_label = MKQAPrediction(
example_id=str(prediction["example_id"]),
prediction=prediction["prediction"] or "",
binary_answer=binary_answer,
no_answer_prob=prediction.get("no_answer_prob", 0),
)
predict_labels[predict_label.example_id] = predict_label
return predict_labels
def compute_mkqa_scores_for_language(
predictions: Dict[str, MKQAPrediction],
gold_annotations: Dict[str, MKQAAnnotation],
language: str,
) -> (Dict[str, float], Dict[str, float]):
"""
Compute Exact Match and token overlap F1 scores per answer, similar to SQuAD
Args:
predictions: mapping from example id to MKQAPrediction
gold_annotations: mapping from example id to MKQAAnnotation
language: evaluation language in MKQA_LANGUAGES
Returns:
Squad like em and f1
"""
predict_texts, answer_texts = [], []
for example_id, gold_annotation in gold_annotations.items():
assert example_id in predictions, f"{example_id} is missing from prediction"
predict_texts.append(
predictions[example_id].binary_answer or predictions[example_id].prediction
)
answer_texts.append(gold_annotation.answers)
text_metrics = eval_util.get_text_metrics(predict_texts, answer_texts, language)
f1_scores = {example_id: text_metrics["f1"][i] for i, example_id in enumerate(gold_annotations)}
em_scores = {
example_id: text_metrics["exact_match"][i] for i, example_id in enumerate(gold_annotations)
}
return em_scores, f1_scores
def compute_best_threshold(
predictions: Dict[str, MKQAPrediction],
raw_em: Dict[str, float],
raw_f1: Dict[str, float],
no_answer_probs: Dict[str, float],
qid_is_answerable: Dict[str, bool],
) -> Dict[str, float]:
"""Compute the averaged text metrics at the best threshold chosen to maximize F1.
The threshold is varied over No Answer probabilities as provided in the predictions file.
Args:
predictions: mapping from example id to MKQAPrediction
raw_em: mapping from example id to em
raw_f1: mapping from example id to f1
no_answer_probs: mapping from example id to no_answer_probs
qid_is_answerable: mapping from example id to boolean indicator for whether the example is answerable
Returns:
text metrics at the best threshold of f1
"""
id_to_predtext = {exid: p.binary_answer or p.prediction for exid, p in predictions.items()}
ans_em_scores = {qid: raw_em[qid] for qid in raw_em if qid_is_answerable[qid]}
ans_f1_scores = {qid: raw_f1[qid] for qid in raw_f1 if qid_is_answerable[qid]}
unans_em_scores = {qid: raw_em[qid] for qid in raw_em if not qid_is_answerable[qid]}
best_scores = eval_util.compute_best_score_and_threshold(
id_to_predtext, raw_f1, no_answer_probs, qid_is_answerable
)
best_f1, f1_threshold = best_scores["best_score"], best_scores["best_threshold"]
best_em_by_id = eval_util.apply_no_answer_threshold(
raw_em, no_answer_probs, qid_is_answerable, f1_threshold
)
best_answerable_em_by_id = eval_util.apply_no_answer_threshold(
ans_em_scores, no_answer_probs, qid_is_answerable, f1_threshold
)
best_answerable_f1_by_id = eval_util.apply_no_answer_threshold(
ans_f1_scores, no_answer_probs, qid_is_answerable, f1_threshold
)
best_unanswerable_em_by_id = eval_util.apply_no_answer_threshold(
unans_em_scores, no_answer_probs, qid_is_answerable, f1_threshold
)
return {
"best_em": round(100.0 * np.mean(list(best_em_by_id.values())), 2),
"best_f1": round(best_f1, 2),
"best_answerable_em": round(100.0 * np.mean(list(best_answerable_em_by_id.values())), 2),
"best_answerable_f1": round(100.0 * np.mean(list(best_answerable_f1_by_id.values())), 2),
"best_unanswerable_em": round(
100.0 * np.mean(list(best_unanswerable_em_by_id.values())), 2
),
"best_f1_threshold": round(f1_threshold, 2),
}
def evaluate(
annotations: Dict[str, MKQAAnnotation],
predictions: Dict[str, MKQAPrediction],
language: str,
out_dir: Optional[str] = None,
verbose: bool = False,
print_metrics: bool = True,
) -> Dict[str, Any]:
"""Evaluates predictions on the gold answers for the specified `language`.
Args:
annotations: a mapping from example id to corresponding MKQAAnnotation
predictions: a mapping from example id to corresponding MKQAPrediction
language: language code in MKQA_LANGUAGES
out_dir: (Optional) Saves evaluation results into this directory.
f1_plot.png: comparing answerable, unanswerable, and overall f1 across all thresholds
metrics.json: reports best_em, best_f1, best_answerable_em, best_answerable_f1, best_unanswerable_em, and best_f1_threshold
na_prob_hist_hasAns.png: histgram of no answer probability for answerable questions
na_prob_hist_noAns.png: histgram of no answer probability for unanswerable questions
print_metrics: (Optional) Print metrics to console
Returns:
A dictionary of metrics and individual f1 and em scores
"""
# Argument validation
assert language in MKQA_LANGUAGES
raw_em_scores, raw_f1_scores = compute_mkqa_scores_for_language(
predictions, annotations, language=language
)
qid_is_answerable = {
ex_id: bool(annotation.answers != [""]) for ex_id, annotation in annotations.items()
}
# Find best thresholds
no_answer_probs = {ex_id: p.no_answer_prob for ex_id, p in predictions.items()}
metrics = compute_best_threshold(
predictions, raw_em_scores, raw_f1_scores, no_answer_probs, qid_is_answerable
)
if out_dir:
if not os.path.exists(out_dir):
os.makedirs(out_dir, exist_ok=True)
has_ans_qids = [ex_id for ex_id in qid_is_answerable if qid_is_answerable[ex_id]]
no_ans_qids = [ex_id for ex_id in qid_is_answerable if not qid_is_answerable[ex_id]]
if has_ans_qids:
eval_util.plot_na_prob_histogram(no_answer_probs, has_ans_qids, out_dir, "hasAns")
if no_ans_qids:
eval_util.plot_na_prob_histogram(no_answer_probs, no_ans_qids, out_dir, "noAns")
# plot the answerable, unanswerable and overall f1 curve
ans_f1_scores = {qid: raw_f1_scores[qid] for qid in raw_f1_scores if qid_is_answerable[qid]}
unans_em_scores = {
qid: raw_em_scores[qid] for qid in raw_em_scores if not qid_is_answerable[qid]
}
eval_util.plot_f1(
ans_f1_scores, unans_em_scores, no_answer_probs, qid_is_answerable, out_dir
)
if verbose:
default_metrics = eval_util.summarize_default_metrics(
raw_em_scores, raw_f1_scores, qid_is_answerable, metrics,
)
metrics.update(default_metrics)
if print_metrics:
print(json.dumps(metrics, indent=4))
if out_dir:
with open(os.path.join(out_dir, "metrics.json"), "w") as f:
f.write(json.dumps(metrics, indent=4))
return metrics
if __name__ == "__main__":
args = parse_args()
annotations = read_annotations(args.annotation_file)[args.language]
predictions = read_predictions(args.predictions_file)
evaluate(
annotations, predictions, args.language, args.out_dir, args.verbose,
)