File size: 8,604 Bytes
e841b45 | 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 | import string
import json
import os
import argparse
import logging
from rouge import rouge_scorer
from transformers import AutoTokenizer
logger = logging.getLogger(__name__)
CURRENT_DIR = os.path.dirname(__file__)
GPT2TOKENIZER = os.path.join(CURRENT_DIR, "../data/gpt2tokenizer")
class GPTTokenizer:
gpt_tokenizer = AutoTokenizer.from_pretrained(GPT2TOKENIZER, max_length=1e5)
def tokenize(self, s):
tokens = self.gpt_tokenizer.tokenize(s)
# GPT2 uses Byte-level BPE, which will include space as part of the word.
# But for the first word of a sentence, there is no space before it.
# So, we remove all the added spaces ("Ġ").
tokens = [t.lstrip("Ġ") for t in tokens]
return tokens
xlingual_tokenizer = GPTTokenizer()
# adapted the flowing from Squad v1.1 evaluation, without removing the articles.
def normalize_answer(s):
"""Lower text and remove punctuation, and extra whitespace."""
def white_space_fix(text):
return ' '.join(text.split())
def remove_punc(text):
exclude = set(string.punctuation)
return ''.join(ch for ch in text if ch not in exclude)
def lower(text):
return text.lower()
return white_space_fix(remove_punc(lower(s)))
def exact_match_score(prediction, ground_truth, xlingual=False):
return (normalize_answer(prediction) == normalize_answer(ground_truth))
def rouge1_score(prediction, ground_truth, xlingual=False):
if xlingual:
scorer = rouge_scorer.RougeScorer(['rouge1'], tokenizer=xlingual_tokenizer)
else:
scorer = rouge_scorer.RougeScorer(['rouge1'], use_stemmer=True)
scores = scorer.score(prediction=prediction, target=ground_truth)
return scores["rouge1"].fmeasure
def rougeL_score(prediction, ground_truth, xlingual=False):
if xlingual:
scorer = rouge_scorer.RougeScorer(['rougeL'], tokenizer=xlingual_tokenizer)
else:
scorer = rouge_scorer.RougeScorer(['rougeL'], use_stemmer=True)
scores = scorer.score(prediction=prediction, target=ground_truth)
return scores["rougeL"].fmeasure
def metric_max_over_ground_truths(metric_fn, prediction, ground_truths, xlingual=False):
scores_for_ground_truths = []
for ground_truth in ground_truths:
score = metric_fn(prediction, ground_truth, xlingual=xlingual)
scores_for_ground_truths.append(score)
return max(scores_for_ground_truths)
def compute_metrics(predictions, references, xlingual=False):
assert len(predictions) == len(references), f"# of predictions {len(predictions)} doesn't match # of references {len(references)}."
exact_match, rouge1, rougeL = 0, 0, 0
for pred, gold in zip(predictions, references):
gold = [gold]
exact_match += metric_max_over_ground_truths(
exact_match_score, prediction=pred, ground_truths=gold, xlingual=xlingual
)
rouge1 += metric_max_over_ground_truths(
rouge1_score, prediction=pred, ground_truths=gold, xlingual=xlingual
)
rougeL += metric_max_over_ground_truths(
rougeL_score, prediction=pred, ground_truths=gold, xlingual=xlingual
)
exact_match = 100.0 * exact_match / len(references)
rouge1 = 100.0 * rouge1 / len(references)
rougeL = 100.0 * rougeL / len(references)
metrics = {"exact_match": exact_match, "rouge1": rouge1, "eval_rougeL": rougeL}
metrics = {k: round(v, 4) for k, v in metrics.items()}
return metrics
def compute_each_metrics(predictions, references, xlingual=False):
assert len(predictions) == len(references), f"# of predictions {len(predictions)} doesn't match # of references {len(references)}."
exact_match, rouge1, rougeL = [], [], []
for pred, gold in zip(predictions, references):
gold = [gold]
exact_match.append(metric_max_over_ground_truths(
exact_match_score, prediction=pred, ground_truths=gold, xlingual=xlingual
))
rouge1.append(metric_max_over_ground_truths(
rouge1_score, prediction=pred, ground_truths=gold, xlingual=xlingual
))
rougeL.append(metric_max_over_ground_truths(
rougeL_score, prediction=pred, ground_truths=gold, xlingual=xlingual
))
# exact_match = 100.0 * exact_match / len(references)
# rouge1 = 100.0 * rouge1 / len(references)
# rougeL = 100.0 * rougeL / len(references)
metrics = {"exact_match": exact_match, "rouge1": rouge1, "eval_rougeL": rougeL}
# metrics = {k: round(v, 4) for k, v in metrics.items()}
return metrics
def compute_grouped_metrics(predictions, references, groups, xlingual=False):
assert len(predictions) == len(references) == len(groups)
examples_by_group = {}
for pred, gold, group in zip(predictions, references, groups):
if group not in examples_by_group:
examples_by_group[group] = []
examples_by_group[group].append((pred, gold))
results = {}
for group, group_examples in examples_by_group.items():
task_predictions, task_references = zip(*group_examples)
group_metrics = compute_metrics(task_predictions, task_references, xlingual=xlingual)
for metric, value in group_metrics.items():
results[f"{metric}_for_{group}"] = value
return results
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--predictions", required=True, help="Path to predictions file.")
parser.add_argument("--track", choices=["default", "xlingual"], default="default",
help="default track or xlingual track. For xlingual, we need to use a different tokenizer."
)
parser.add_argument("--compute_per_category_metrics", action="store_true", help="Compute metrics on every evaluation category.")
parser.add_argument("--compute_per_task_metrics", action="store_true", help="Compute metrics on every evaluation task.")
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
with open(args.predictions) as fin:
examples = [json.loads(l) for l in fin]
predictions = [e["prediction"] for e in examples]
references = [e["instance"]["output"] for e in examples]
tasks = []
for e in examples:
if e["task"] == "task121_atomic_question_rewriting":
e["task"] = "task121_zest_question_rewriting"
tasks.append(e["Task"])
results = compute_metrics(predictions, references, xlingual=args.track == "xlingual")
print("======== Overall Metrics ========")
print("all_rougeL", results["rougeL"])
print("all_EM", results["exact_match"])
print()
category_metrics = [
("Textual Entailment", "exact_match"),
("Cause Effect Classification", "exact_match"),
("Coreference Resolution", "exact_match"),
("Dialogue Act Recognition", "exact_match"),
("Answerability Classification", "exact_match"),
("Word Analogy", "exact_match"),
("Overlap Extraction", "rougeL"),
("Keyword Tagging", "rougeL"),
("Question Rewriting", "rougeL"),
("Title Generation", "rougeL"),
("Data to Text", "rougeL"),
("Grammar Error Correction", "rougeL"),
]
category_metrics = {"_".join(category.lower().split()): metric for category, metric in category_metrics}
if args.compute_per_category_metrics:
print("======== Metrics per category ========")
task_category = {}
for task in set(tasks):
with open(os.path.join("./data/tasks/", task+".json")) as fin:
task_data = json.load(fin)
task_category[task] = "_".join(task_data["Categories"][0].lower().split())
categories = [task_category[e["Task"]] for e in examples]
results.update(compute_grouped_metrics(predictions, references, categories, xlingual=args.track=="xlingual"))
for category, metric in category_metrics.items():
# category = "_".join(category.lower().split())
if f"{metric}_for_{category}" in results:
print(f"{metric}_for_{category}", results[f"{metric}_for_{category}"])
print()
if args.compute_per_task_metrics:
print("======== Metrics per task ========")
results_by_task = compute_grouped_metrics(predictions, references, tasks, xlingual=args.track=="xlingual")
for task in sorted(list(set(tasks))):
category = task_category[task]
metric = category_metrics[category]
print(task, results_by_task[f"{metric}_for_{task}"])
print() |