-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmetrics.py
170 lines (151 loc) · 6.6 KB
/
metrics.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
import re
import string
import numpy as np
import pandas as pd
import functools
import numbers
def _normalise(string_list,
lower_case_and_strip_punctuation,
allowed_punctuation="'"):
'''Convert a list of strings by converting to lower case and removing
punctuation. This helps when calculating metrics such as WER, as we're
interested in which words were predicted more than the capitalisation
or punctuation.'''
if isinstance(string_list, str):
raise ValueError(
'The _normalise() function is applied to lists of strings, not '
'strings directly.')
if not lower_case_and_strip_punctuation:
return string_list
result = []
for s in string_list:
s = s.lower()
punct = list(string.punctuation)
if allowed_punctuation:
for allowed in allowed_punctuation:
punct.remove(allowed)
result.append(''.join([c for c in s if c not in punct]))
return result
def multilingual_eval(eval_preds,
source_language,
target_language,
metrics,
metric_names,
tokenizer,
log_first_N_predictions,
speech_processor=None,
lower_case_and_strip_punctuation=True,
allowed_punctuation="'"):
'''Compute metric scores for each source and target language combination.'''
def _round_if_float(f, p):
if isinstance(f, float):
return round(f, p)
else:
return f
if speech_processor:
if len(eval_preds.predictions.shape) == 2:
# Predictions are token IDs
pred_ids = eval_preds.predictions
else:
# Predictions are logits
pred_logits = eval_preds.predictions
pred_ids = np.argmax(pred_logits, axis=-1)
eval_preds.label_ids[eval_preds.label_ids == -
100] = speech_processor.tokenizer.pad_token_id
decoded_predictions = speech_processor.batch_decode(pred_ids, skip_special_tokens=True)
# we do not want to group tokens when computing the metrics
decoded_labels = speech_processor.batch_decode(
eval_preds.label_ids, skip_special_tokens=True)
else:
predictions, labels = eval_preds
# Replace -100 values as we can't decode them.
predictions = np.where(
predictions != -100, predictions, tokenizer.pad_token_id)
decoded_predictions = tokenizer.batch_decode(
predictions, skip_special_tokens=True)
labels = np.where(labels != -100, labels, tokenizer.pad_token_id)
decoded_labels = tokenizer.batch_decode(
labels, skip_special_tokens=True)
if log_first_N_predictions:
print('First N predictions in eval set:')
for i in range(log_first_N_predictions):
print(f'Prediction ({source_language[i]} to {target_language[i]}):'
f' "{decoded_predictions[i]}", '
f'True label: "{decoded_labels[i]}"')
subsets = {}
for i in range(len(decoded_predictions)):
if speech_processor:
# For speech metrics, such as WER, we evaluate for separate target
# languages.
language_combination = target_language[i]
else:
# For translation metrics, such as BLEU, we want metrics for every
# source/target combination.
language_combination = source_language[i] + \
'_' + target_language[i]
if language_combination not in subsets:
subsets[language_combination] = {'predictions': [], 'labels': []}
subsets[language_combination]['predictions'].append(
decoded_predictions[i])
subsets[language_combination]['labels'].append(decoded_labels[i])
result = {}
for metric, metric_name in zip(metrics, metric_names):
for subset in list(subsets.keys()):
result_subset = metric.compute(
predictions=_normalise(subsets[subset]['predictions'],
lower_case_and_strip_punctuation,
allowed_punctuation),
references=_normalise(subsets[subset]['labels'],
lower_case_and_strip_punctuation,
allowed_punctuation),
)
if metric_name == 'BLEU':
# The sacrebleu and bleu implementations have different formats
r = result_subset.get('score') or result_subset.get('bleu')
else:
if not isinstance(result_subset, numbers.Number):
raise ValueError(
'Expected a metric that yields a single value, but the '
f'result from metric "{metric_name.lower()}" was '
f'{result_subset}. Supported metrics include '
'sacrebleu, wer, cer.')
r = result_subset
result[f'{metric_name}_{subset}'] = r
subset_values = [result[f'{metric_name}_{subset}']
for subset in list(subsets.keys())]
try:
result[f'{metric_name}_mean'] = np.mean(subset_values)
except TypeError:
result[f'{metric_name}_mean'] = np.nan
result = {k: _round_if_float(v, 3) for k, v in result.items()}
return result
def multilingual_eval_fn(eval_dataset,
metrics,
tokenizer,
log_first_N_predictions=0,
speech_processor=None,
lower_case_and_strip_punctuation=True,
allowed_punctuation="'"):
'''Return a function with the signature `eval_fn(preds)`.
If `speech_processor` is defined, then it is used to decode the predictions.
Otherwise `tokenizer` is used (e.g. for translation tasks).'''
df = pd.DataFrame(eval_dataset)
source_language = list(df['source.language'])
target_language = list(df['target.language'])
metric_names = []
for m in metrics:
if m.name == 'sacrebleu':
metric_names.append('BLEU')
else:
metric_names.append(m.name.upper())
return lambda x: multilingual_eval(
x,
source_language,
target_language,
metrics,
metric_names,
tokenizer,
log_first_N_predictions=log_first_N_predictions,
speech_processor=speech_processor,
lower_case_and_strip_punctuation=lower_case_and_strip_punctuation,
allowed_punctuation=allowed_punctuation)