AndrewMcDowell
commited on
Commit
•
561be41
1
Parent(s):
44deea1
eval results.
Browse files
eval.py
ADDED
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
from datasets import load_dataset, load_metric, Audio, Dataset
|
3 |
+
from transformers import pipeline, AutoFeatureExtractor
|
4 |
+
import re
|
5 |
+
import argparse
|
6 |
+
import unicodedata
|
7 |
+
from typing import Dict
|
8 |
+
|
9 |
+
|
10 |
+
def log_results(result: Dataset, args: Dict[str, str]):
|
11 |
+
""" DO NOT CHANGE. This function computes and logs the result metrics. """
|
12 |
+
|
13 |
+
log_outputs = args.log_outputs
|
14 |
+
dataset_id = "_".join(args.dataset.split("/") + [args.config, args.split])
|
15 |
+
|
16 |
+
# load metric
|
17 |
+
wer = load_metric("wer")
|
18 |
+
cer = load_metric("cer")
|
19 |
+
|
20 |
+
# compute metrics
|
21 |
+
wer_result = wer.compute(references=result["target"], predictions=result["prediction"])
|
22 |
+
cer_result = cer.compute(references=result["target"], predictions=result["prediction"])
|
23 |
+
|
24 |
+
# print & log results
|
25 |
+
result_str = (
|
26 |
+
f"WER: {wer_result}\n"
|
27 |
+
f"CER: {cer_result}"
|
28 |
+
)
|
29 |
+
print(result_str)
|
30 |
+
|
31 |
+
with open(f"{dataset_id}_eval_results.txt", "w") as f:
|
32 |
+
f.write(result_str)
|
33 |
+
|
34 |
+
# log all results in text file. Possibly interesting for analysis
|
35 |
+
if log_outputs is not None:
|
36 |
+
pred_file = f"log_{dataset_id}_predictions.txt"
|
37 |
+
target_file = f"log_{dataset_id}_targets.txt"
|
38 |
+
|
39 |
+
with open(pred_file, "w") as p, open(target_file, "w") as t:
|
40 |
+
|
41 |
+
# mapping function to write output
|
42 |
+
def write_to_file(batch, i):
|
43 |
+
p.write(f"{i}" + "\n")
|
44 |
+
p.write(batch["prediction"] + "\n")
|
45 |
+
t.write(f"{i}" + "\n")
|
46 |
+
t.write(batch["target"] + "\n")
|
47 |
+
|
48 |
+
result.map(write_to_file, with_indices=True)
|
49 |
+
|
50 |
+
|
51 |
+
def normalize_text(text: str) -> str:
|
52 |
+
""" DO ADAPT FOR YOUR USE CASE. this function normalizes the target text. """
|
53 |
+
|
54 |
+
from pykakasi import kakasi
|
55 |
+
|
56 |
+
kakasi = kakasi()
|
57 |
+
kakasi.setMode('J', 'H') #Convert from kanji to hiragana
|
58 |
+
conv = kakasi.getConverter()
|
59 |
+
chars_to_ignore_regex = '[\,\?\!\-\;\:\"\“\%\‘\”\�\—\’\…\–\(\,\[\]\)\(\!]' # noqa: W605 IMPORTANT: this should correspond to the chars that were ignored during training
|
60 |
+
|
61 |
+
text = text.lower()
|
62 |
+
# normalize non-standard (stylized) unicode characters
|
63 |
+
text = unicodedata.normalize('NFKC', text)
|
64 |
+
# remove punctuation
|
65 |
+
text = conv.do(re.sub(chars_to_ignore_regex, "", text))
|
66 |
+
|
67 |
+
# Let's also make sure we split on all kinds of newlines, spaces, etc...
|
68 |
+
text = " ".join(text.split())
|
69 |
+
|
70 |
+
return text
|
71 |
+
|
72 |
+
|
73 |
+
def main(args):
|
74 |
+
# load dataset
|
75 |
+
dataset = load_dataset(args.dataset, args.config, split=args.split, use_auth_token=True)
|
76 |
+
|
77 |
+
# for testing: only process the first two examples as a test
|
78 |
+
# dataset = dataset.select(range(10))
|
79 |
+
|
80 |
+
# load processor
|
81 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(args.model_id)
|
82 |
+
sampling_rate = feature_extractor.sampling_rate
|
83 |
+
|
84 |
+
# resample audio
|
85 |
+
dataset = dataset.cast_column("audio", Audio(sampling_rate=sampling_rate))
|
86 |
+
|
87 |
+
# load eval pipeline
|
88 |
+
asr = pipeline("automatic-speech-recognition", model=args.model_id)
|
89 |
+
|
90 |
+
# map function to decode audio
|
91 |
+
def map_to_pred(batch):
|
92 |
+
prediction = asr(batch["audio"]["array"], chunk_length_s=args.chunk_length_s, stride_length_s=args.stride_length_s)
|
93 |
+
|
94 |
+
batch["prediction"] = prediction["text"]
|
95 |
+
batch["target"] = normalize_text(batch["sentence"])
|
96 |
+
return batch
|
97 |
+
|
98 |
+
# run inference on all examples
|
99 |
+
result = dataset.map(map_to_pred, remove_columns=dataset.column_names)
|
100 |
+
|
101 |
+
# compute and log_results
|
102 |
+
# do not change function below
|
103 |
+
log_results(result, args)
|
104 |
+
|
105 |
+
|
106 |
+
if __name__ == "__main__":
|
107 |
+
parser = argparse.ArgumentParser()
|
108 |
+
|
109 |
+
parser.add_argument(
|
110 |
+
"--model_id", type=str, required=True, help="Model identifier. Should be loadable with 🤗 Transformers"
|
111 |
+
)
|
112 |
+
parser.add_argument(
|
113 |
+
"--dataset", type=str, required=True, help="Dataset name to evaluate the `model_id`. Should be loadable with 🤗 Datasets"
|
114 |
+
)
|
115 |
+
parser.add_argument(
|
116 |
+
"--config", type=str, required=True, help="Config of the dataset. *E.g.* `'en'` for Common Voice"
|
117 |
+
)
|
118 |
+
parser.add_argument(
|
119 |
+
"--split", type=str, required=True, help="Split of the dataset. *E.g.* `'test'`"
|
120 |
+
)
|
121 |
+
parser.add_argument(
|
122 |
+
"--chunk_length_s", type=float, default=None, help="Chunk length in seconds. Defaults to None. For long audio files a good value would be 5.0 seconds."
|
123 |
+
)
|
124 |
+
parser.add_argument(
|
125 |
+
"--stride_length_s", type=float, default=None, help="Stride of the audio chunks. Defaults to None. For long audio files a good value would be 1.0 seconds."
|
126 |
+
)
|
127 |
+
parser.add_argument(
|
128 |
+
"--log_outputs", action='store_true', help="If defined, write outputs to log file for analysis."
|
129 |
+
)
|
130 |
+
args = parser.parse_args()
|
131 |
+
|
132 |
+
main(args)
|
log_mozilla-foundation_common_voice_8_0_ja_test_predictions.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|
log_mozilla-foundation_common_voice_8_0_ja_test_targets.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|
mozilla-foundation_common_voice_8_0_ja_test_eval_results.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
WER: 0.9855427046263345
|
2 |
+
CER: 0.328342726455934
|