add evaluation script and results
Browse files
README.md
CHANGED
@@ -8,6 +8,37 @@ tags:
|
|
8 |
- robust-speech-event
|
9 |
datasets:
|
10 |
- mozilla-foundation/common_voice_7_0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
---
|
12 |
|
13 |
# wav2vec2-xls-r-300m-cv7-turkish
|
@@ -47,3 +78,22 @@ The following hypermaters were used for finetuning:
|
|
47 |
|
48 |
## Language Model
|
49 |
N-gram language model is trained on a Turkish Wikipedia articles using KenLM and [ngram-lm-wiki](https://github.com/mpoyraz/ngram-lm-wiki) repo was used to generate arpa LM and convert it into binary format.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
- robust-speech-event
|
9 |
datasets:
|
10 |
- mozilla-foundation/common_voice_7_0
|
11 |
+
model-index:
|
12 |
+
- name: mpoyraz/wav2vec2-xls-r-300m-cv7-turkish
|
13 |
+
results:
|
14 |
+
- task:
|
15 |
+
name: Automatic Speech Recognition
|
16 |
+
type: automatic-speech-recognition
|
17 |
+
dataset:
|
18 |
+
name: Common Voice 7
|
19 |
+
type: mozilla-foundation/common_voice_7_0
|
20 |
+
args: tr
|
21 |
+
metrics:
|
22 |
+
- name: Test WER
|
23 |
+
type: wer
|
24 |
+
value: 20.11
|
25 |
+
- name: Test CER
|
26 |
+
type: cer
|
27 |
+
value: 8.01
|
28 |
+
- task:
|
29 |
+
name: Automatic Speech Recognition
|
30 |
+
type: automatic-speech-recognition
|
31 |
+
dataset:
|
32 |
+
name: Robust Speech Event - Dev Data
|
33 |
+
type: speech-recognition-community-v2/dev_data
|
34 |
+
args: tr
|
35 |
+
metrics:
|
36 |
+
- name: Test WER
|
37 |
+
type: wer
|
38 |
+
value: 30.87
|
39 |
+
- name: Test CER
|
40 |
+
type: cer
|
41 |
+
value: 10.68
|
42 |
---
|
43 |
|
44 |
# wav2vec2-xls-r-300m-cv7-turkish
|
|
|
78 |
|
79 |
## Language Model
|
80 |
N-gram language model is trained on a Turkish Wikipedia articles using KenLM and [ngram-lm-wiki](https://github.com/mpoyraz/ngram-lm-wiki) repo was used to generate arpa LM and convert it into binary format.
|
81 |
+
|
82 |
+
## Evaluation Commands
|
83 |
+
Please install [unicode_tr](https://pypi.org/project/unicode_tr/) package before running evaluation. It is used for Turkish text processing.
|
84 |
+
1. To evaluate on `mozilla-foundation/common_voice_7_0` with split `test`
|
85 |
+
```bash
|
86 |
+
python eval.py --model_id mpoyraz/wav2vec2-xls-r-300m-cv7-turkish --dataset mozilla-foundation/common_voice_7_0 --config tr --split test
|
87 |
+
```
|
88 |
+
|
89 |
+
2. To evaluate on `speech-recognition-community-v2/dev_data`
|
90 |
+
|
91 |
+
```bash
|
92 |
+
python eval.py --model_id mpoyraz/wav2vec2-xls-r-300m-cv7-turkish --dataset speech-recognition-community-v2/dev_data --config tr --split validation --chunk_length_s 5.0 --stride_length_s 1.0
|
93 |
+
```
|
94 |
+
## Evaluation results:
|
95 |
+
|
96 |
+
| Dataset | WER | CER |
|
97 |
+
|---|---|---|
|
98 |
+
|Common Voice 7 TR test split| 20.11 | 8.01 |
|
99 |
+
|Speech Recognition Community dev data| 30.87 | 10.68 |
|
eval.py
ADDED
@@ -0,0 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
import argparse
|
3 |
+
import re
|
4 |
+
from typing import Dict
|
5 |
+
|
6 |
+
import torch
|
7 |
+
from datasets import Audio, Dataset, load_dataset, load_metric
|
8 |
+
|
9 |
+
from transformers import AutoFeatureExtractor, pipeline
|
10 |
+
|
11 |
+
from unicode_tr import unicode_tr
|
12 |
+
|
13 |
+
def log_results(result: Dataset, args: Dict[str, str]):
|
14 |
+
"""DO NOT CHANGE. This function computes and logs the result metrics."""
|
15 |
+
|
16 |
+
log_outputs = args.log_outputs
|
17 |
+
dataset_id = "_".join(args.dataset.split("/") + [args.config, args.split])
|
18 |
+
|
19 |
+
# load metric
|
20 |
+
wer = load_metric("wer")
|
21 |
+
cer = load_metric("cer")
|
22 |
+
|
23 |
+
# compute metrics
|
24 |
+
wer_result = wer.compute(references=result["target"], predictions=result["prediction"])
|
25 |
+
cer_result = cer.compute(references=result["target"], predictions=result["prediction"])
|
26 |
+
|
27 |
+
# print & log results
|
28 |
+
result_str = f"WER: {wer_result}\n" f"CER: {cer_result}"
|
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 |
+
chars_to_remove_regex = '[,?.!\-\;\:"“%”�—…–()]'
|
55 |
+
apostrophes = "[’‘`´ʹʻʼʽʿˈ]"
|
56 |
+
|
57 |
+
# Lower the text using 'unicode_tr'
|
58 |
+
# Regular lower() does not work well for Turkish Language
|
59 |
+
text_norm = unicode_tr(text).lower()
|
60 |
+
# Unify apostrophes
|
61 |
+
text_norm = re.sub(apostrophes, "'", text_norm)
|
62 |
+
# Remove pre-defined chars
|
63 |
+
text_norm = re.sub(chars_to_remove_regex, "", text_norm)
|
64 |
+
# Remove single quotes
|
65 |
+
text_norm = text_norm.replace(" '", " ")
|
66 |
+
text_norm = text_norm.replace("' ", " ")
|
67 |
+
# Handle hatted characters
|
68 |
+
text_norm = re.sub('[â]', 'a', text_norm)
|
69 |
+
text_norm = re.sub('[î]', 'i', text_norm)
|
70 |
+
text_norm = re.sub('[ô]', 'o', text_norm)
|
71 |
+
text_norm = re.sub('[û]', 'u', text_norm)
|
72 |
+
# Handle alternate characters
|
73 |
+
text_norm = re.sub('[é]', 'e', text_norm)
|
74 |
+
text_norm = re.sub('[ë]', 'e', text_norm)
|
75 |
+
# Remove multiple spaces
|
76 |
+
text_norm = re.sub(r"\s+", " ", text_norm)
|
77 |
+
|
78 |
+
return text_norm
|
79 |
+
|
80 |
+
|
81 |
+
def main(args):
|
82 |
+
# load dataset
|
83 |
+
dataset = load_dataset(args.dataset, args.config, split=args.split, use_auth_token=True)
|
84 |
+
|
85 |
+
# for testing: only process the first two examples as a test
|
86 |
+
# dataset = dataset.select(range(10))
|
87 |
+
|
88 |
+
# load processor
|
89 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(args.model_id)
|
90 |
+
sampling_rate = feature_extractor.sampling_rate
|
91 |
+
|
92 |
+
# resample audio
|
93 |
+
dataset = dataset.cast_column("audio", Audio(sampling_rate=sampling_rate))
|
94 |
+
|
95 |
+
# load eval pipeline
|
96 |
+
if args.device is None:
|
97 |
+
args.device = 0 if torch.cuda.is_available() else -1
|
98 |
+
asr = pipeline("automatic-speech-recognition", model=args.model_id, device=args.device)
|
99 |
+
|
100 |
+
# map function to decode audio
|
101 |
+
def map_to_pred(batch):
|
102 |
+
prediction = asr(
|
103 |
+
batch["audio"]["array"], chunk_length_s=args.chunk_length_s, stride_length_s=args.stride_length_s
|
104 |
+
)
|
105 |
+
|
106 |
+
batch["prediction"] = prediction["text"]
|
107 |
+
batch["target"] = normalize_text(batch["sentence"])
|
108 |
+
return batch
|
109 |
+
|
110 |
+
# run inference on all examples
|
111 |
+
result = dataset.map(map_to_pred, remove_columns=dataset.column_names)
|
112 |
+
|
113 |
+
# compute and log_results
|
114 |
+
# do not change function below
|
115 |
+
log_results(result, args)
|
116 |
+
|
117 |
+
|
118 |
+
if __name__ == "__main__":
|
119 |
+
parser = argparse.ArgumentParser()
|
120 |
+
|
121 |
+
parser.add_argument(
|
122 |
+
"--model_id", type=str, required=True, help="Model identifier. Should be loadable with 🤗 Transformers"
|
123 |
+
)
|
124 |
+
parser.add_argument(
|
125 |
+
"--dataset",
|
126 |
+
type=str,
|
127 |
+
required=True,
|
128 |
+
help="Dataset name to evaluate the `model_id`. Should be loadable with 🤗 Datasets",
|
129 |
+
)
|
130 |
+
parser.add_argument(
|
131 |
+
"--config", type=str, required=True, help="Config of the dataset. *E.g.* `'en'` for Common Voice"
|
132 |
+
)
|
133 |
+
parser.add_argument("--split", type=str, required=True, help="Split of the dataset. *E.g.* `'test'`")
|
134 |
+
parser.add_argument(
|
135 |
+
"--chunk_length_s", type=float, default=None, help="Chunk length in seconds. Defaults to 5 seconds."
|
136 |
+
)
|
137 |
+
parser.add_argument(
|
138 |
+
"--stride_length_s", type=float, default=None, help="Stride of the audio chunks. Defaults to 1 second."
|
139 |
+
)
|
140 |
+
parser.add_argument(
|
141 |
+
"--log_outputs", action="store_true", help="If defined, write outputs to log file for analysis."
|
142 |
+
)
|
143 |
+
parser.add_argument(
|
144 |
+
"--device",
|
145 |
+
type=int,
|
146 |
+
default=None,
|
147 |
+
help="The device to run the pipeline on. -1 for CPU (default), 0 for the first GPU and so on.",
|
148 |
+
)
|
149 |
+
args = parser.parse_args()
|
150 |
+
|
151 |
+
main(args)
|