patrickvonplaten
commited on
Commit
·
30d7398
1
Parent(s):
1c82004
up
Browse files
eval.py
CHANGED
@@ -1,45 +1,126 @@
|
|
1 |
#!/usr/bin/env python3
|
2 |
-
from datasets import load_dataset, load_metric, Audio
|
3 |
-
from transformers import
|
4 |
-
import torch
|
5 |
import re
|
|
|
|
|
6 |
|
7 |
-
lang = "sv-SE"
|
8 |
-
model_id = "./xls-r-300m-sv"
|
9 |
|
10 |
-
|
|
|
11 |
|
12 |
-
|
13 |
-
|
14 |
|
15 |
-
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
|
|
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
|
|
22 |
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
input_values = processor(batch["audio"]["array"], return_tensors="pt", padding="longest", sampling_rate=16_000).input_values
|
26 |
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
predicted_ids = torch.argmax(logits, dim=-1)
|
32 |
-
transcription = processor.batch_decode(predicted_ids)[0]
|
33 |
-
else:
|
34 |
-
transcription = processor.batch_decode(logits.cpu().numpy()).text[0]
|
35 |
|
36 |
-
batch["transcription"] = transcription
|
37 |
-
batch["text"] = re.sub(chars_to_ignore_regex, "", batch["sentence"].lower())
|
38 |
-
return batch
|
39 |
|
|
|
|
|
40 |
|
41 |
-
|
42 |
|
43 |
-
|
44 |
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
from typing import Dict
|
7 |
|
|
|
|
|
8 |
|
9 |
+
def log_results(result: Dataset, args: Dict[str, str]):
|
10 |
+
""" DO NOT CHANGE. This function computes and logs the result metrics. """
|
11 |
|
12 |
+
log_outputs = args.log_outputs
|
13 |
+
dataset_id = "_".join(args.dataset.split("/") + [args.config, args.split])
|
14 |
|
15 |
+
# load metric
|
16 |
+
wer = load_metric("wer")
|
17 |
+
cer = load_metric("cer")
|
18 |
|
19 |
+
# compute metrics
|
20 |
+
wer_result = wer.compute(references=result["target"], predictions=result["prediction"])
|
21 |
+
cer_result = cer.compute(references=result["target"], predictions=result["prediction"])
|
22 |
|
23 |
+
# print & log results
|
24 |
+
result_str = (
|
25 |
+
f"WER: {wer_result}\n"
|
26 |
+
f"CER: {cer_result}"
|
27 |
+
)
|
28 |
+
print(result_str)
|
29 |
|
30 |
+
with open(f"{dataset_id}_eval_results.txt", "w") as f:
|
31 |
+
f.write(result_str)
|
32 |
|
33 |
+
# log all results in text file. Possibly interesting for analysis
|
34 |
+
if log_outputs is not None:
|
35 |
+
pred_file = f"log_{dataset_id}_predictions.txt"
|
36 |
+
target_file = f"log_{dataset_id}_targets.txt"
|
37 |
|
38 |
+
with open(pred_file, "w") as p, open(target_file, "w") as t:
|
|
|
39 |
|
40 |
+
# mapping function to write output
|
41 |
+
def write_to_file(batch, i):
|
42 |
+
p.write(f"{i}" + "\n")
|
43 |
+
p.write(batch["prediction"] + "\n")
|
44 |
+
t.write(f"{i}" + "\n")
|
45 |
+
t.write(batch["target"] + "\n")
|
46 |
|
47 |
+
result.map(write_to_file, with_indices=True)
|
|
|
|
|
|
|
|
|
48 |
|
|
|
|
|
|
|
49 |
|
50 |
+
def normalize_text(text: str) -> str:
|
51 |
+
""" DO ADAPT FOR YOUR USE CASE. this function normalizes the target text. """
|
52 |
|
53 |
+
chars_to_ignore_regex = '[,?.!\-\;\:\"“%‘”�—’…–]' # noqa: W605 IMPORTANT: this should correspond to the chars that were ignored during training
|
54 |
|
55 |
+
text = re.sub(chars_to_ignore_regex, "", text.lower())
|
56 |
|
57 |
+
# In addition, we can normalize the target text, e.g. removing new lines characters etc...
|
58 |
+
# note that order is important here!
|
59 |
+
token_sequences_to_ignore = ["\n\n", "\n", " ", " "]
|
60 |
+
|
61 |
+
for t in token_sequences_to_ignore:
|
62 |
+
text = " ".join(text.split(t))
|
63 |
+
|
64 |
+
return text
|
65 |
+
|
66 |
+
|
67 |
+
def main(args):
|
68 |
+
# load dataset
|
69 |
+
dataset = load_dataset(args.dataset, args.config, split=args.split, use_auth_token=True)
|
70 |
+
|
71 |
+
# for testing: only process the first two examples as a test
|
72 |
+
dataset = dataset.select(range(10))
|
73 |
+
|
74 |
+
# load processor
|
75 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(args.model_id)
|
76 |
+
sampling_rate = feature_extractor.sampling_rate
|
77 |
+
|
78 |
+
# resample audio
|
79 |
+
dataset = dataset.cast_column("audio", Audio(sampling_rate=sampling_rate))
|
80 |
+
|
81 |
+
# load eval pipeline
|
82 |
+
asr = pipeline("automatic-speech-recognition", model=args.model_id)
|
83 |
+
|
84 |
+
# map function to decode audio
|
85 |
+
def map_to_pred(batch):
|
86 |
+
prediction = asr(batch["audio"]["array"], chunk_length_s=args.chunk_length_s, stride_length_s=args.stride_length_s)
|
87 |
+
|
88 |
+
batch["prediction"] = prediction["text"]
|
89 |
+
batch["target"] = normalize_text(batch["sentence"])
|
90 |
+
return batch
|
91 |
+
|
92 |
+
# run inference on all examples
|
93 |
+
result = dataset.map(map_to_pred, remove_columns=dataset.column_names)
|
94 |
+
|
95 |
+
# compute and log_results
|
96 |
+
# do not change function below
|
97 |
+
log_results(result, args)
|
98 |
+
|
99 |
+
|
100 |
+
if __name__ == "__main__":
|
101 |
+
parser = argparse.ArgumentParser()
|
102 |
+
|
103 |
+
parser.add_argument(
|
104 |
+
"--model_id", type=str, required=True, help="Model identifier. Should be loadable with 🤗 Transformers"
|
105 |
+
)
|
106 |
+
parser.add_argument(
|
107 |
+
"--dataset", type=str, required=True, help="Dataset name to evaluate the `model_id`. Should be loadable with 🤗 Datasets"
|
108 |
+
)
|
109 |
+
parser.add_argument(
|
110 |
+
"--config", type=str, required=True, help="Config of the dataset. *E.g.* `'en'` for Common Voice"
|
111 |
+
)
|
112 |
+
parser.add_argument(
|
113 |
+
"--split", type=str, required=True, help="Split of the dataset. *E.g.* `'test'`"
|
114 |
+
)
|
115 |
+
parser.add_argument(
|
116 |
+
"--chunk_length_s", type=float, default=None, help="Chunk length in seconds. Defaults to 5 seconds."
|
117 |
+
)
|
118 |
+
parser.add_argument(
|
119 |
+
"--stride_length_s", type=float, default=None, help="Stride of the audio chunks. Defaults to 1 second."
|
120 |
+
)
|
121 |
+
parser.add_argument(
|
122 |
+
"--log_outputs", action='store_true', help="If defined, write outputs to log file for analysis."
|
123 |
+
)
|
124 |
+
args = parser.parse_args()
|
125 |
+
|
126 |
+
main(args)
|
log_mozilla-foundation_common_voice_7_0_sv-SE_test_predictions.txt
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
0
|
2 |
+
jag lämnade grovjobbet åt honom
|
3 |
+
1
|
4 |
+
ja för att åter få ett stulet föremål
|
5 |
+
2
|
6 |
+
har du fortfarande samma nummer
|
7 |
+
3
|
8 |
+
det räcker inte
|
9 |
+
4
|
10 |
+
där är om
|
11 |
+
5
|
12 |
+
vill jag se dig död skulle du var det
|
13 |
+
6
|
14 |
+
vi är oemottagliga för din utstrålning
|
15 |
+
7
|
16 |
+
jag vet att du pratar med honom
|
17 |
+
8
|
18 |
+
dra åt helvete här u
|
19 |
+
9
|
20 |
+
hon fick bra betyg för att hon pluggade på kvällen
|
log_mozilla-foundation_common_voice_7_0_sv-SE_test_targets.txt
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
0
|
2 |
+
jag lämnade grovjobbet åt honom
|
3 |
+
1
|
4 |
+
ja för att återfå ett stulet föremål
|
5 |
+
2
|
6 |
+
har du fortfarande samma nummer
|
7 |
+
3
|
8 |
+
det räcker inte
|
9 |
+
4
|
10 |
+
där är de
|
11 |
+
5
|
12 |
+
ville jag se dig död skulle du vara det
|
13 |
+
6
|
14 |
+
vi är oemottagliga för din utstrålning
|
15 |
+
7
|
16 |
+
jag vet att du pratar med honom
|
17 |
+
8
|
18 |
+
dra åt helvete harry
|
19 |
+
9
|
20 |
+
hon fick bra betyg för att hon pluggade på kvällen
|
mozilla-foundation_common_voice_7_0_sv-SE_test_eval_results.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
WER: 0.11864406779661017
|
2 |
+
CER: 0.02666666666666667
|
run_cv_eval.sh
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
./eval.py --model_id hf-test/xls-r-300m-sv --dataset mozilla-foundation/common_voice_7_0 --config sv-SE --split test --log_outputs
|
run_real_eval.sh
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
./eval.py --model_id hf-test/xls-r-300m-sv --dataset speech-recognition-community-internal/tedx_manual_dev_test --config sv --split validation --chunk_length_s 5.0 --stride_length_s 1.0 --log_outputs
|