baguette
Browse files- added_tokens.json +0 -1
- eval.py +153 -0
- run.sh +4 -4
- run_speech_recognition_ctc.py +3 -1
added_tokens.json
DELETED
@@ -1 +0,0 @@
|
|
1 |
-
{"<s>": 216, "</s>": 217}
|
|
|
|
eval.py
ADDED
@@ -0,0 +1,153 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
import argparse
|
3 |
+
import re
|
4 |
+
import unicodedata
|
5 |
+
from typing import Dict
|
6 |
+
|
7 |
+
import torch
|
8 |
+
from datasets import Audio, Dataset, load_dataset, load_metric
|
9 |
+
|
10 |
+
from transformers import AutoFeatureExtractor, pipeline
|
11 |
+
|
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 |
+
chars_to_remove_regex = r'[\,\?\.\!\-\_\;\:\"\“\%\‘\”\�\^]'
|
52 |
+
|
53 |
+
def remove_accents(text):
|
54 |
+
nfkd_form = unicodedata.normalize('NFKD', text)
|
55 |
+
return u"".join([c for c in nfkd_form if not unicodedata.combining(c)])
|
56 |
+
|
57 |
+
def remove_special_characters(text):
|
58 |
+
text = re.sub(chars_to_remove_regex, '', text).lower()
|
59 |
+
text = re.sub("ç", r'[cedille]', text)
|
60 |
+
text = re.sub("&", r'et', text)
|
61 |
+
text = re.sub("%", r' pourcents', text)
|
62 |
+
text = re.sub("([0-9]+)(,|.)([0-9+])", r'\1 virgule \3', text)
|
63 |
+
text = re.sub("\$", r'dollar', text)
|
64 |
+
text = re.sub("\£", r'livre', text)
|
65 |
+
text = re.sub("\€", r'euro', text)
|
66 |
+
text = remove_accents(text)
|
67 |
+
text = re.sub(r"\[cedille\]", 'ç', text) + " "
|
68 |
+
return text
|
69 |
+
|
70 |
+
def normalize_text(text: str) -> str:
|
71 |
+
text = remove_special_characters(text)
|
72 |
+
|
73 |
+
# In addition, we can normalize the target text, e.g. removing new lines characters etc...
|
74 |
+
# note that order is important here!
|
75 |
+
token_sequences_to_ignore = ["\n\n", "\n", " ", " "]
|
76 |
+
|
77 |
+
for t in token_sequences_to_ignore:
|
78 |
+
text = " ".join(text.split(t))
|
79 |
+
|
80 |
+
return text
|
81 |
+
|
82 |
+
|
83 |
+
def main(args):
|
84 |
+
# load dataset
|
85 |
+
dataset = load_dataset(args.dataset, args.config, split=args.split, use_auth_token=True)
|
86 |
+
|
87 |
+
# for testing: only process the first two examples as a test
|
88 |
+
dataset = dataset.select(range(20))
|
89 |
+
|
90 |
+
# load processor
|
91 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(args.model_id)
|
92 |
+
sampling_rate = feature_extractor.sampling_rate
|
93 |
+
|
94 |
+
# resample audio
|
95 |
+
dataset = dataset.cast_column("audio", Audio(sampling_rate=sampling_rate))
|
96 |
+
|
97 |
+
# load eval pipeline
|
98 |
+
if args.device is None:
|
99 |
+
args.device = 0 if torch.cuda.is_available() else -1
|
100 |
+
asr = pipeline("automatic-speech-recognition", model=args.model_id, device=args.device)
|
101 |
+
|
102 |
+
# map function to decode audio
|
103 |
+
def map_to_pred(batch):
|
104 |
+
prediction = asr(
|
105 |
+
batch["audio"]["array"], chunk_length_s=args.chunk_length_s, stride_length_s=args.stride_length_s,,skip_special_tokens=True
|
106 |
+
)
|
107 |
+
|
108 |
+
batch["prediction"] = prediction["text"]# "".join(prediction["text"].split("<s>"))
|
109 |
+
batch["target"] = normalize_text(batch["sentence"])
|
110 |
+
return batch
|
111 |
+
|
112 |
+
# run inference on all examples
|
113 |
+
result = dataset.map(map_to_pred, remove_columns=dataset.column_names)
|
114 |
+
|
115 |
+
# compute and log_results
|
116 |
+
# do not change function below
|
117 |
+
log_results(result, args)
|
118 |
+
|
119 |
+
|
120 |
+
if __name__ == "__main__":
|
121 |
+
parser = argparse.ArgumentParser()
|
122 |
+
|
123 |
+
parser.add_argument(
|
124 |
+
"--model_id", type=str, required=True, help="Model identifier. Should be loadable with 🤗 Transformers"
|
125 |
+
)
|
126 |
+
parser.add_argument(
|
127 |
+
"--dataset",
|
128 |
+
type=str,
|
129 |
+
required=True,
|
130 |
+
help="Dataset name to evaluate the `model_id`. Should be loadable with 🤗 Datasets",
|
131 |
+
)
|
132 |
+
parser.add_argument(
|
133 |
+
"--config", type=str, required=True, help="Config of the dataset. *E.g.* `'en'` for Common Voice"
|
134 |
+
)
|
135 |
+
parser.add_argument("--split", type=str, required=True, help="Split of the dataset. *E.g.* `'test'`")
|
136 |
+
parser.add_argument(
|
137 |
+
"--chunk_length_s", type=float, default=None, help="Chunk length in seconds. Defaults to 5 seconds."
|
138 |
+
)
|
139 |
+
parser.add_argument(
|
140 |
+
"--stride_length_s", type=float, default=None, help="Stride of the audio chunks. Defaults to 1 second."
|
141 |
+
)
|
142 |
+
parser.add_argument(
|
143 |
+
"--log_outputs", action="store_true", help="If defined, write outputs to log file for analysis."
|
144 |
+
)
|
145 |
+
parser.add_argument(
|
146 |
+
"--device",
|
147 |
+
type=int,
|
148 |
+
default=None,
|
149 |
+
help="The device to run the pipeline on. -1 for CPU (default), 0 for the first GPU and so on.",
|
150 |
+
)
|
151 |
+
args = parser.parse_args()
|
152 |
+
|
153 |
+
main(args)
|
run.sh
CHANGED
@@ -6,8 +6,8 @@ python run_speech_recognition_ctc.py \
|
|
6 |
--output_dir="./" \
|
7 |
--overwrite_output_dir \
|
8 |
--num_train_epochs="5" \
|
9 |
-
--per_device_train_batch_size="
|
10 |
-
--per_device_eval_batch_size="
|
11 |
--gradient_accumulation_steps="1" \
|
12 |
--learning_rate="7e-5" \
|
13 |
--warmup_steps="1500" \
|
@@ -22,9 +22,9 @@ python run_speech_recognition_ctc.py \
|
|
22 |
--save_total_limit="3" \
|
23 |
--freeze_feature_encoder \
|
24 |
--feat_proj_dropout="0.0" \
|
25 |
-
--mask_time_prob="0.
|
26 |
--mask_time_length="10" \
|
27 |
-
--mask_feature_prob="0.
|
28 |
--mask_feature_length="10" \
|
29 |
--gradient_checkpointing \
|
30 |
--report_to="wandb" \
|
|
|
6 |
--output_dir="./" \
|
7 |
--overwrite_output_dir \
|
8 |
--num_train_epochs="5" \
|
9 |
+
--per_device_train_batch_size="64" \
|
10 |
+
--per_device_eval_batch_size="64" \
|
11 |
--gradient_accumulation_steps="1" \
|
12 |
--learning_rate="7e-5" \
|
13 |
--warmup_steps="1500" \
|
|
|
22 |
--save_total_limit="3" \
|
23 |
--freeze_feature_encoder \
|
24 |
--feat_proj_dropout="0.0" \
|
25 |
+
--mask_time_prob="0.05" \
|
26 |
--mask_time_length="10" \
|
27 |
+
--mask_feature_prob="0.33" \
|
28 |
--mask_feature_length="10" \
|
29 |
--gradient_checkpointing \
|
30 |
--report_to="wandb" \
|
run_speech_recognition_ctc.py
CHANGED
@@ -511,6 +511,8 @@ def main():
|
|
511 |
tokenizer_kwargs = {
|
512 |
"config": config if config.tokenizer_class is not None else None,
|
513 |
"tokenizer_type": config.model_type if config.tokenizer_class is None else None,
|
|
|
|
|
514 |
"unk_token": unk_token,
|
515 |
"pad_token": pad_token,
|
516 |
"word_delimiter_token": word_delimiter_token,
|
@@ -641,7 +643,7 @@ def main():
|
|
641 |
|
642 |
pred.label_ids[pred.label_ids == -100] = tokenizer.pad_token_id
|
643 |
|
644 |
-
pred_str = tokenizer.batch_decode(pred_ids)
|
645 |
# we do not want to group tokens when computing the metrics
|
646 |
label_str = tokenizer.batch_decode(pred.label_ids, group_tokens=False)
|
647 |
|
|
|
511 |
tokenizer_kwargs = {
|
512 |
"config": config if config.tokenizer_class is not None else None,
|
513 |
"tokenizer_type": config.model_type if config.tokenizer_class is None else None,
|
514 |
+
"bos_token": None,
|
515 |
+
"eos_token": None,
|
516 |
"unk_token": unk_token,
|
517 |
"pad_token": pad_token,
|
518 |
"word_delimiter_token": word_delimiter_token,
|
|
|
643 |
|
644 |
pred.label_ids[pred.label_ids == -100] = tokenizer.pad_token_id
|
645 |
|
646 |
+
pred_str = tokenizer.batch_decode(pred_ids, skip_special_tokens=True)
|
647 |
# we do not want to group tokens when computing the metrics
|
648 |
label_str = tokenizer.batch_decode(pred.label_ids, group_tokens=False)
|
649 |
|