Upload lm-boosted decoder
Browse files- .gitattributes +1 -0
- alphabet.json +1 -0
- eval.py +144 -0
- language_model/attrs.json +1 -0
- language_model/kenlm_finnish.bin +3 -0
- language_model/unigrams.txt +3 -0
- preprocessor_config.json +1 -0
- special_tokens_map.json +1 -1
- tokenizer_config.json +1 -1
.gitattributes
CHANGED
@@ -25,3 +25,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
25 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
26 |
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
27 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
25 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
26 |
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
27 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
language_model/unigrams.txt filter=lfs diff=lfs merge=lfs -text
|
alphabet.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"labels": [" ", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "\u00e4", "\u00e5", "\u00f6", "\u2047", "", "<s>", "</s>"], "is_bpe": false}
|
eval.py
ADDED
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 = [",", "?", "¿", ".", "!", "¡", ";", ";", ":", '""', "%", '"', "�", "ʿ", "·", "჻", "~", "՞",
|
54 |
+
"؟", "،", "।", "॥", "«", "»", "„", "“", "”", "「", "」", "‘", "’", "《", "》", "(", ")", "[", "]",
|
55 |
+
"{", "}", "=", "`", "_", "+", "<", ">", "…", "–", "°", "´", "ʾ", "‹", "›", "©", "®", "—", "→", "。",
|
56 |
+
"、", "﹂", "﹁", "‧", "~", "﹏", ",", "{", "}", "(", ")", "[", "]", "【", "】", "‥", "〽",
|
57 |
+
"『", "』", "〝", "〟", "⟨", "⟩", "〜", ":", "!", "?", "♪", "؛", "/", "\\", "º", "−", "^", "ʻ", "ˆ", "'"]
|
58 |
+
|
59 |
+
chars_to_remove_regex = f"[{re.escape(''.join(CHARS_TO_IGNORE))}]"
|
60 |
+
|
61 |
+
text = re.sub(chars_to_remove_regex, "", text.lower())
|
62 |
+
text = re.sub("[-]", " ", text)
|
63 |
+
|
64 |
+
# In addition, we can normalize the target text, e.g. removing new lines characters etc...
|
65 |
+
# note that order is important here!
|
66 |
+
token_sequences_to_ignore = ["\n\n", "\n", " ", " "]
|
67 |
+
|
68 |
+
for t in token_sequences_to_ignore:
|
69 |
+
text = " ".join(text.split(t))
|
70 |
+
|
71 |
+
return text
|
72 |
+
|
73 |
+
|
74 |
+
def main(args):
|
75 |
+
# load dataset
|
76 |
+
dataset = load_dataset(args.dataset, args.config, split=args.split, use_auth_token=True)
|
77 |
+
|
78 |
+
# for testing: only process the first two examples as a test
|
79 |
+
# dataset = dataset.select(range(10))
|
80 |
+
|
81 |
+
# load processor
|
82 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(args.model_id)
|
83 |
+
sampling_rate = feature_extractor.sampling_rate
|
84 |
+
|
85 |
+
# resample audio
|
86 |
+
dataset = dataset.cast_column("audio", Audio(sampling_rate=sampling_rate))
|
87 |
+
|
88 |
+
# load eval pipeline
|
89 |
+
if args.device is None:
|
90 |
+
args.device = 0 if torch.cuda.is_available() else -1
|
91 |
+
asr = pipeline("automatic-speech-recognition", model=args.model_id, device=args.device)
|
92 |
+
|
93 |
+
# map function to decode audio
|
94 |
+
def map_to_pred(batch):
|
95 |
+
prediction = asr(
|
96 |
+
batch["audio"]["array"], chunk_length_s=args.chunk_length_s, stride_length_s=args.stride_length_s
|
97 |
+
)
|
98 |
+
|
99 |
+
batch["prediction"] = prediction["text"]
|
100 |
+
batch["target"] = normalize_text(batch["sentence"])
|
101 |
+
return batch
|
102 |
+
|
103 |
+
# run inference on all examples
|
104 |
+
result = dataset.map(map_to_pred, remove_columns=dataset.column_names)
|
105 |
+
|
106 |
+
# compute and log_results
|
107 |
+
# do not change function below
|
108 |
+
log_results(result, args)
|
109 |
+
|
110 |
+
|
111 |
+
if __name__ == "__main__":
|
112 |
+
parser = argparse.ArgumentParser()
|
113 |
+
|
114 |
+
parser.add_argument(
|
115 |
+
"--model_id", type=str, required=True, help="Model identifier. Should be loadable with 🤗 Transformers"
|
116 |
+
)
|
117 |
+
parser.add_argument(
|
118 |
+
"--dataset",
|
119 |
+
type=str,
|
120 |
+
required=True,
|
121 |
+
help="Dataset name to evaluate the `model_id`. Should be loadable with 🤗 Datasets",
|
122 |
+
)
|
123 |
+
parser.add_argument(
|
124 |
+
"--config", type=str, required=True, help="Config of the dataset. *E.g.* `'en'` for Common Voice"
|
125 |
+
)
|
126 |
+
parser.add_argument("--split", type=str, required=True, help="Split of the dataset. *E.g.* `'test'`")
|
127 |
+
parser.add_argument(
|
128 |
+
"--chunk_length_s", type=float, default=None, help="Chunk length in seconds. Defaults to 5 seconds."
|
129 |
+
)
|
130 |
+
parser.add_argument(
|
131 |
+
"--stride_length_s", type=float, default=None, help="Stride of the audio chunks. Defaults to 1 second."
|
132 |
+
)
|
133 |
+
parser.add_argument(
|
134 |
+
"--log_outputs", action="store_true", help="If defined, write outputs to log file for analysis."
|
135 |
+
)
|
136 |
+
parser.add_argument(
|
137 |
+
"--device",
|
138 |
+
type=int,
|
139 |
+
default=None,
|
140 |
+
help="The device to run the pipeline on. -1 for CPU (default), 0 for the first GPU and so on.",
|
141 |
+
)
|
142 |
+
args = parser.parse_args()
|
143 |
+
|
144 |
+
main(args)
|
language_model/attrs.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"alpha": 0.5, "beta": 1.5, "unk_score_offset": -10.0, "score_boundary": true}
|
language_model/kenlm_finnish.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:79ca983c89af32b5c38560d822324bf2023c3aa2b32ed17fd1ea67aac68b5166
|
3 |
+
size 1021613027
|
language_model/unigrams.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c0f65403ece43fcc9eb95148060e402d7af46692f7a41a94b75ae23f40fa93d7
|
3 |
+
size 14815049
|
preprocessor_config.json
CHANGED
@@ -4,6 +4,7 @@
|
|
4 |
"feature_size": 1,
|
5 |
"padding_side": "right",
|
6 |
"padding_value": 0.0,
|
|
|
7 |
"return_attention_mask": true,
|
8 |
"sampling_rate": 16000
|
9 |
}
|
|
|
4 |
"feature_size": 1,
|
5 |
"padding_side": "right",
|
6 |
"padding_value": 0.0,
|
7 |
+
"processor_class": "Wav2Vec2ProcessorWithLM",
|
8 |
"return_attention_mask": true,
|
9 |
"sampling_rate": 16000
|
10 |
}
|
special_tokens_map.json
CHANGED
@@ -1 +1 @@
|
|
1 |
-
{"bos_token": "<s>", "eos_token": "</s>", "unk_token": "[UNK]", "pad_token": "[PAD]", "additional_special_tokens": [{"content": "<s>", "single_word": false, "lstrip": false, "rstrip": false, "normalized": true}, {"content": "</s>", "single_word": false, "lstrip": false, "rstrip": false, "normalized": true}]}
|
|
|
1 |
+
{"bos_token": "<s>", "eos_token": "</s>", "unk_token": "[UNK]", "pad_token": "[PAD]", "additional_special_tokens": [{"content": "<s>", "single_word": false, "lstrip": false, "rstrip": false, "normalized": true}, {"content": "</s>", "single_word": false, "lstrip": false, "rstrip": false, "normalized": true}, {"content": "<s>", "single_word": false, "lstrip": false, "rstrip": false, "normalized": true}, {"content": "</s>", "single_word": false, "lstrip": false, "rstrip": false, "normalized": true}]}
|
tokenizer_config.json
CHANGED
@@ -1 +1 @@
|
|
1 |
-
{"unk_token": "[UNK]", "bos_token": "<s>", "eos_token": "</s>", "pad_token": "[PAD]", "do_lower_case": false, "word_delimiter_token": "|", "replace_word_delimiter_char": " ", "special_tokens_map_file": null, "name_or_path": "
|
|
|
1 |
+
{"unk_token": "[UNK]", "bos_token": "<s>", "eos_token": "</s>", "pad_token": "[PAD]", "do_lower_case": false, "word_delimiter_token": "|", "replace_word_delimiter_char": " ", "special_tokens_map_file": null, "name_or_path": "/content/wav2vec2-base-fi-voxpopuli-v2-finetuned", "tokenizer_class": "Wav2Vec2CTCTokenizer", "processor_class": "Wav2Vec2ProcessorWithLM"}
|