lgris commited on
Commit
c13a00b
1 Parent(s): 6e92117
README.md CHANGED
@@ -12,9 +12,36 @@ datasets:
12
  - common_voice
13
  model-index:
14
  - name: wav2vec2-xls-r-1b-cv8
15
- results: []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  ---
17
-
18
  <!-- This model card has been generated automatically according to the information the Trainer had access to. You
19
  should probably proofread and complete it, then remove this comment. -->
20
 
 
12
  - common_voice
13
  model-index:
14
  - name: wav2vec2-xls-r-1b-cv8
15
+ results:
16
+ - task:
17
+ name: Automatic Speech Recognition
18
+ type: automatic-speech-recognition
19
+ dataset:
20
+ name: Common Voice 8
21
+ type: mozilla-foundation/common_voice_8_0
22
+ args: pt
23
+ metrics:
24
+ - name: Test WER
25
+ type: wer
26
+ value: 17.70
27
+ - name: Test CER
28
+ type: cer
29
+ value: 5.21
30
+ - task:
31
+ name: Automatic Speech Recognition
32
+ type: automatic-speech-recognition
33
+ dataset:
34
+ name: Robust Speech Event - Dev Data
35
+ type: speech-recognition-community-v2/dev_data
36
+ args: sv
37
+ metrics:
38
+ - name: Test WER
39
+ type: wer
40
+ value: 45.68
41
+ - name: Test CER
42
+ type: cer
43
+ value: 18.67
44
  ---
 
45
  <!-- This model card has been generated automatically according to the information the Trainer had access to. You
46
  should probably proofread and complete it, then remove this comment. -->
47
 
eval.py ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
12
+ def log_results(result: Dataset, args: Dict[str, str]):
13
+ """DO NOT CHANGE. This function computes and logs the result metrics."""
14
+
15
+ log_outputs = args.log_outputs
16
+ dataset_id = "_".join(args.dataset.split("/") + [args.config, args.split])
17
+
18
+ # load metric
19
+ wer = load_metric("wer")
20
+ cer = load_metric("cer")
21
+
22
+ # compute metrics
23
+ wer_result = wer.compute(references=result["target"], predictions=result["prediction"])
24
+ cer_result = cer.compute(references=result["target"], predictions=result["prediction"])
25
+
26
+ # print & log results
27
+ result_str = f"WER: {wer_result}\n" f"CER: {cer_result}"
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
+ if args.device is None:
83
+ args.device = 0 if torch.cuda.is_available() else -1
84
+ asr = pipeline("automatic-speech-recognition", model=args.model_id, device=args.device)
85
+
86
+ # map function to decode audio
87
+ def map_to_pred(batch):
88
+ prediction = asr(
89
+ batch["audio"]["array"], chunk_length_s=args.chunk_length_s, stride_length_s=args.stride_length_s
90
+ )
91
+
92
+ batch["prediction"] = prediction["text"]
93
+ batch["target"] = normalize_text(batch["sentence"])
94
+
95
+ # print(batch["target"])
96
+ # print(batch["prediction"])
97
+ return batch
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",
114
+ type=str,
115
+ required=True,
116
+ help="Dataset name to evaluate the `model_id`. Should be loadable with 🤗 Datasets",
117
+ )
118
+ parser.add_argument(
119
+ "--config", type=str, required=True, help="Config of the dataset. *E.g.* `'en'` for Common Voice"
120
+ )
121
+ parser.add_argument("--split", type=str, required=True, help="Split of the dataset. *E.g.* `'test'`")
122
+ parser.add_argument(
123
+ "--chunk_length_s", type=float, default=None, help="Chunk length in seconds. Defaults to 5 seconds."
124
+ )
125
+ parser.add_argument(
126
+ "--stride_length_s", type=float, default=None, help="Stride of the audio chunks. Defaults to 1 second."
127
+ )
128
+ parser.add_argument(
129
+ "--log_outputs", action="store_true", help="If defined, write outputs to log file for analysis."
130
+ )
131
+ parser.add_argument(
132
+ "--device",
133
+ type=int,
134
+ default=None,
135
+ help="The device to run the pipeline on. -1 for CPU (default), 0 for the first GPU and so on.",
136
+ )
137
+ args = parser.parse_args()
138
+
139
+ main(args)
eval.sh ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ python3 eval.py --model_id ./ --dataset mozilla-foundation/common_voice_8_0 --config pt --split test --log_outputs
2
+ python3 eval.py --stride_length_s 1.0 --chunk_length_s 5.0 --model_id ./ --dataset speech-recognition-community-v2/dev_data --config pt --split validation --log_outputs
log_mozilla-foundation_common_voice_8_0_pt_test_predictions.txt ADDED
The diff for this file is too large to render. See raw diff
 
log_mozilla-foundation_common_voice_8_0_pt_test_targets.txt ADDED
The diff for this file is too large to render. See raw diff
 
log_speech-recognition-community-v2_dev_data_pt_validation_predictions.txt ADDED
The diff for this file is too large to render. See raw diff
 
log_speech-recognition-community-v2_dev_data_pt_validation_targets.txt ADDED
The diff for this file is too large to render. See raw diff
 
mozilla-foundation_common_voice_8_0_pt_test_eval_results.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ WER: 0.17704519823024203
2
+ CER: 0.05219412854391324
speech-recognition-community-v2_dev_data_pt_validation_eval_results.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ WER: 0.45682464020533503
2
+ CER: 0.18670771611948084
vocab.json CHANGED
@@ -1 +1 @@
1
- {"'": 1, "a": 2, "b": 3, "c": 4, "d": 5, "e": 6, "f": 7, "g": 8, "h": 9, "i": 10, "j": 11, "k": 12, "l": 13, "m": 14, "n": 15, "o": 16, "p": 17, "q": 18, "r": 19, "s": 20, "t": 21, "u": 22, "v": 23, "w": 24, "x": 25, "y": 26, "z": 27, "«": 28, "´": 29, "»": 30, "à": 31, "á": 32, "â": 33, "ã": 34, "ç": 35, "è": 36, "é": 37, "ê": 38, "í": 39, "ó": 40, "ô": 41, "õ": 42, "ú": 43, "ü": 44, "ž": 45, "|": 0, "[UNK]": 46, "[PAD]": 47}
 
1
+ {"a": 1, "b": 2, "c": 3, "d": 4, "e": 5, "f": 6, "g": 7, "h": 8, "i": 9, "j": 10, "k": 11, "l": 12, "m": 13, "n": 14, "o": 15, "p": 16, "q": 17, "r": 18, "s": 19, "t": 20, "u": 21, "v": 22, "w": 23, "x": 24, "y": 25, "z": 26, "27": 27, "á": 28, "â": 29, "ã": 30, "ç": 31, "32": 32, "é": 33, "ê": 34, "í": 35, "36": 36, "ó": 37, "ô": 38, "õ": 39, "ú": 40, "41": 41, "42": 42, "43": 43, "44": 44, "[PAD]": 45, "|": 0, "[UNK]": 46}