1
"""Official evaluation script for SQuAD version 2.0.
3
In addition to basic functionality, we also compute additional statistics and
4
plot precision-recall curves if an additional na_prob.json file is provided.
5
This file is expected to map question ID's to the model's predicted probability
6
that a question is unanswerable.
20
ARTICLES_REGEX = re.compile(r"\b(a|an|the)\b", re.UNICODE)
26
parser = argparse.ArgumentParser("Official evaluation script for SQuAD version 2.0.")
27
parser.add_argument("data_file", metavar="data.json", help="Input data JSON file.")
28
parser.add_argument("pred_file", metavar="pred.json", help="Model predictions.")
30
"--out-file", "-o", metavar="eval.json", help="Write accuracy metrics to file (default is stdout)."
33
"--na-prob-file", "-n", metavar="na_prob.json", help="Model estimates of probability of no answer."
40
help='Predict "" if no-answer probability exceeds this (default = 1.0).',
43
"--out-image-dir", "-p", metavar="out_images", default=None, help="Save precision-recall curves to directory."
45
parser.add_argument("--verbose", "-v", action="store_true")
46
if len(sys.argv) == 1:
49
return parser.parse_args()
52
def make_qid_to_has_ans(dataset):
54
for article in dataset:
55
for p in article["paragraphs"]:
57
qid_to_has_ans[qa["id"]] = bool(qa["answers"]["text"])
61
def normalize_answer(s):
62
"""Lower text and remove punctuation, articles and extra whitespace."""
64
def remove_articles(text):
65
return ARTICLES_REGEX.sub(" ", text)
67
def white_space_fix(text):
68
return " ".join(text.split())
70
def remove_punc(text):
71
exclude = set(string.punctuation)
72
return "".join(ch for ch in text if ch not in exclude)
77
return white_space_fix(remove_articles(remove_punc(lower(s))))
83
return normalize_answer(s).split()
86
def compute_exact(a_gold, a_pred):
87
return int(normalize_answer(a_gold) == normalize_answer(a_pred))
90
def compute_f1(a_gold, a_pred):
91
gold_toks = get_tokens(a_gold)
92
pred_toks = get_tokens(a_pred)
93
common = collections.Counter(gold_toks) & collections.Counter(pred_toks)
94
num_same = sum(common.values())
95
if len(gold_toks) == 0 or len(pred_toks) == 0:
97
return int(gold_toks == pred_toks)
100
precision = 1.0 * num_same / len(pred_toks)
101
recall = 1.0 * num_same / len(gold_toks)
102
f1 = (2 * precision * recall) / (precision + recall)
106
def get_raw_scores(dataset, preds):
109
for article in dataset:
110
for p in article["paragraphs"]:
113
gold_answers = [t for t in qa["answers"]["text"] if normalize_answer(t)]
118
print(f"Missing prediction for {qid}")
122
exact_scores[qid] = max(compute_exact(a, a_pred) for a in gold_answers)
123
f1_scores[qid] = max(compute_f1(a, a_pred) for a in gold_answers)
124
return exact_scores, f1_scores
127
def apply_no_ans_threshold(scores, na_probs, qid_to_has_ans, na_prob_thresh):
129
for qid, s in scores.items():
130
pred_na = na_probs[qid] > na_prob_thresh
132
new_scores[qid] = float(not qid_to_has_ans[qid])
138
def make_eval_dict(exact_scores, f1_scores, qid_list=None):
140
total = len(exact_scores)
141
return collections.OrderedDict(
143
("exact", 100.0 * sum(exact_scores.values()) / total),
144
("f1", 100.0 * sum(f1_scores.values()) / total),
149
total = len(qid_list)
150
return collections.OrderedDict(
152
("exact", 100.0 * sum(exact_scores[k] for k in qid_list) / total),
153
("f1", 100.0 * sum(f1_scores[k] for k in qid_list) / total),
159
def merge_eval(main_eval, new_eval, prefix):
161
main_eval[f"{prefix}_{k}"] = new_eval[k]
164
def plot_pr_curve(precisions, recalls, out_image, title):
165
plt.step(recalls, precisions, color="b", alpha=0.2, where="post")
166
plt.fill_between(recalls, precisions, step="post", alpha=0.2, color="b")
168
plt.ylabel("Precision")
169
plt.xlim([0.0, 1.05])
170
plt.ylim([0.0, 1.05])
172
plt.savefig(out_image)
176
def make_precision_recall_eval(scores, na_probs, num_true_pos, qid_to_has_ans, out_image=None, title=None):
177
qid_list = sorted(na_probs, key=lambda k: na_probs[k])
184
for i, qid in enumerate(qid_list):
185
if qid_to_has_ans[qid]:
186
true_pos += scores[qid]
187
cur_p = true_pos / float(i + 1)
188
cur_r = true_pos / float(num_true_pos)
189
if i == len(qid_list) - 1 or na_probs[qid] != na_probs[qid_list[i + 1]]:
191
avg_prec += cur_p * (cur_r - recalls[-1])
192
precisions.append(cur_p)
193
recalls.append(cur_r)
195
plot_pr_curve(precisions, recalls, out_image, title)
196
return {"ap": 100.0 * avg_prec}
199
def run_precision_recall_analysis(main_eval, exact_raw, f1_raw, na_probs, qid_to_has_ans, out_image_dir):
200
if out_image_dir and not os.path.exists(out_image_dir):
201
os.makedirs(out_image_dir)
202
num_true_pos = sum(1 for v in qid_to_has_ans.values() if v)
203
if num_true_pos == 0:
205
pr_exact = make_precision_recall_eval(
210
out_image=os.path.join(out_image_dir, "pr_exact.png"),
211
title="Precision-Recall curve for Exact Match score",
213
pr_f1 = make_precision_recall_eval(
218
out_image=os.path.join(out_image_dir, "pr_f1.png"),
219
title="Precision-Recall curve for F1 score",
221
oracle_scores = {k: float(v) for k, v in qid_to_has_ans.items()}
222
pr_oracle = make_precision_recall_eval(
227
out_image=os.path.join(out_image_dir, "pr_oracle.png"),
228
title="Oracle Precision-Recall curve (binary task of HasAns vs. NoAns)",
230
merge_eval(main_eval, pr_exact, "pr_exact")
231
merge_eval(main_eval, pr_f1, "pr_f1")
232
merge_eval(main_eval, pr_oracle, "pr_oracle")
235
def histogram_na_prob(na_probs, qid_list, image_dir, name):
238
x = [na_probs[k] for k in qid_list]
239
weights = np.ones_like(x) / float(len(x))
240
plt.hist(x, weights=weights, bins=20, range=(0.0, 1.0))
241
plt.xlabel("Model probability of no-answer")
242
plt.ylabel("Proportion of dataset")
243
plt.title(f"Histogram of no-answer probability: {name}")
244
plt.savefig(os.path.join(image_dir, f"na_prob_hist_{name}.png"))
248
def find_best_thresh(preds, scores, na_probs, qid_to_has_ans):
249
num_no_ans = sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k])
250
cur_score = num_no_ans
251
best_score = cur_score
253
qid_list = sorted(na_probs, key=lambda k: na_probs[k])
254
for i, qid in enumerate(qid_list):
255
if qid not in scores:
257
if qid_to_has_ans[qid]:
265
if cur_score > best_score:
266
best_score = cur_score
267
best_thresh = na_probs[qid]
268
return 100.0 * best_score / len(scores), best_thresh
271
def find_all_best_thresh(main_eval, preds, exact_raw, f1_raw, na_probs, qid_to_has_ans):
272
best_exact, exact_thresh = find_best_thresh(preds, exact_raw, na_probs, qid_to_has_ans)
273
best_f1, f1_thresh = find_best_thresh(preds, f1_raw, na_probs, qid_to_has_ans)
274
main_eval["best_exact"] = best_exact
275
main_eval["best_exact_thresh"] = exact_thresh
276
main_eval["best_f1"] = best_f1
277
main_eval["best_f1_thresh"] = f1_thresh
281
with open(OPTS.data_file) as f:
282
dataset_json = json.load(f)
283
dataset = dataset_json["data"]
284
with open(OPTS.pred_file) as f:
286
if OPTS.na_prob_file:
287
with open(OPTS.na_prob_file) as f:
288
na_probs = json.load(f)
290
na_probs = {k: 0.0 for k in preds}
291
qid_to_has_ans = make_qid_to_has_ans(dataset)
292
has_ans_qids = [k for k, v in qid_to_has_ans.items() if v]
293
no_ans_qids = [k for k, v in qid_to_has_ans.items() if not v]
294
exact_raw, f1_raw = get_raw_scores(dataset, preds)
295
exact_thresh = apply_no_ans_threshold(exact_raw, na_probs, qid_to_has_ans, OPTS.na_prob_thresh)
296
f1_thresh = apply_no_ans_threshold(f1_raw, na_probs, qid_to_has_ans, OPTS.na_prob_thresh)
297
out_eval = make_eval_dict(exact_thresh, f1_thresh)
299
has_ans_eval = make_eval_dict(exact_thresh, f1_thresh, qid_list=has_ans_qids)
300
merge_eval(out_eval, has_ans_eval, "HasAns")
302
no_ans_eval = make_eval_dict(exact_thresh, f1_thresh, qid_list=no_ans_qids)
303
merge_eval(out_eval, no_ans_eval, "NoAns")
304
if OPTS.na_prob_file:
305
find_all_best_thresh(out_eval, preds, exact_raw, f1_raw, na_probs, qid_to_has_ans)
306
if OPTS.na_prob_file and OPTS.out_image_dir:
307
run_precision_recall_analysis(out_eval, exact_raw, f1_raw, na_probs, qid_to_has_ans, OPTS.out_image_dir)
308
histogram_na_prob(na_probs, has_ans_qids, OPTS.out_image_dir, "hasAns")
309
histogram_na_prob(na_probs, no_ans_qids, OPTS.out_image_dir, "noAns")
311
with open(OPTS.out_file, "w") as f:
312
json.dump(out_eval, f)
314
print(json.dumps(out_eval, indent=2))
317
if __name__ == "__main__":
319
if OPTS.out_image_dir:
322
matplotlib.use("Agg")
323
import matplotlib.pyplot as plt