End of training
Browse files- .ipynb_checkpoints/custom-checkpoint.py +76 -0
- .ipynb_checkpoints/eval-checkpoint.py +210 -0
- all_results.json +7 -7
- custom.py +76 -0
- eval.py +210 -0
- eval_results.json +3 -3
- log_mozilla-foundation_common_voice_7_0_vi_test_predictions.txt +20 -0
- log_mozilla-foundation_common_voice_7_0_vi_test_targets.txt +20 -0
- log_vivos_vi_test_predictions.txt +20 -0
- log_vivos_vi_test_targets.txt +20 -0
- runs/Jan29_00-37-33_job-fa775f5b-8438-4c3d-95fa-ed70ddaee577/1643416720.7954614/events.out.tfevents.1643416720.job-fa775f5b-8438-4c3d-95fa-ed70ddaee577.1784910.1 +3 -0
- runs/Jan29_00-37-33_job-fa775f5b-8438-4c3d-95fa-ed70ddaee577/events.out.tfevents.1643416720.job-fa775f5b-8438-4c3d-95fa-ed70ddaee577.1784910.0 +3 -0
- runs/Jan29_00-45-45_job-fa775f5b-8438-4c3d-95fa-ed70ddaee577/1643417179.7920382/events.out.tfevents.1643417179.job-fa775f5b-8438-4c3d-95fa-ed70ddaee577.1788197.1 +3 -0
- runs/Jan29_00-45-45_job-fa775f5b-8438-4c3d-95fa-ed70ddaee577/events.out.tfevents.1643417179.job-fa775f5b-8438-4c3d-95fa-ed70ddaee577.1788197.0 +3 -0
- runs/Jan29_00-45-45_job-fa775f5b-8438-4c3d-95fa-ed70ddaee577/events.out.tfevents.1643417291.job-fa775f5b-8438-4c3d-95fa-ed70ddaee577.1788197.2 +3 -0
- train_results.json +4 -4
- trainer_state.json +6 -6
- training_args.bin +1 -1
.ipynb_checkpoints/custom-checkpoint.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import Wav2Vec2CTCTokenizer
|
2 |
+
|
3 |
+
class Wav2Vec2WordpieceTokenizer(Wav2Vec2CTCTokenizer):
|
4 |
+
def __init__(
|
5 |
+
self,
|
6 |
+
vocab_file,
|
7 |
+
bos_token="<s>",
|
8 |
+
eos_token="</s>",
|
9 |
+
unk_token="<unk>",
|
10 |
+
pad_token="<pad>",
|
11 |
+
word_delimiter_token="|",
|
12 |
+
do_lower_case=False,
|
13 |
+
**kwargs
|
14 |
+
):
|
15 |
+
super().__init__(
|
16 |
+
vocab_file=vocab_file,
|
17 |
+
unk_token=unk_token,
|
18 |
+
bos_token=bos_token,
|
19 |
+
eos_token=eos_token,
|
20 |
+
pad_token=pad_token,
|
21 |
+
do_lower_case=do_lower_case,
|
22 |
+
word_delimiter_token=word_delimiter_token,
|
23 |
+
**kwargs,
|
24 |
+
)
|
25 |
+
|
26 |
+
self._create_trie(self.all_special_tokens_extended)
|
27 |
+
|
28 |
+
def _tokenize(self, text, **kwargs):
|
29 |
+
"""
|
30 |
+
Converts a string in a sequence of tokens (string), using the tokenizer.
|
31 |
+
"""
|
32 |
+
special_cases = set(['gia', 'qui', 'quy', 'que', 'qua'])
|
33 |
+
output_tokens = []
|
34 |
+
for token_idx, token in enumerate(text.split()):
|
35 |
+
if token in special_cases:
|
36 |
+
sub_tokens = [token[:2], token[2:]]
|
37 |
+
else:
|
38 |
+
end = len(token)
|
39 |
+
sub_tokens = []
|
40 |
+
while end > 0:
|
41 |
+
start = 0
|
42 |
+
cur_substr = None
|
43 |
+
while start < end:
|
44 |
+
substr = token[start:end]
|
45 |
+
if substr in self.encoder:
|
46 |
+
cur_substr = substr
|
47 |
+
break
|
48 |
+
start += 1
|
49 |
+
if cur_substr is None:
|
50 |
+
sub_tokens.insert(0, self.unk_token)
|
51 |
+
end = start - 1
|
52 |
+
else:
|
53 |
+
sub_tokens.insert(0, cur_substr)
|
54 |
+
end = start
|
55 |
+
|
56 |
+
if token_idx > 0:
|
57 |
+
output_tokens.append(self.word_delimiter_token)
|
58 |
+
output_tokens.extend(sub_tokens)
|
59 |
+
return output_tokens
|
60 |
+
|
61 |
+
def decode_ids(
|
62 |
+
self,
|
63 |
+
token_ids,
|
64 |
+
skip_special_tokens = False,
|
65 |
+
clean_up_tokenization_spaces = True,
|
66 |
+
group_tokens: bool = True,
|
67 |
+
spaces_between_special_tokens: bool = False,
|
68 |
+
) -> str:
|
69 |
+
# For compatible with speechbrain interfaces
|
70 |
+
return self.decode(
|
71 |
+
token_ids,
|
72 |
+
skip_special_tokens=skip_special_tokens,
|
73 |
+
clean_up_tokenization_spaces=clean_up_tokenization_spaces,
|
74 |
+
group_tokens=group_tokens,
|
75 |
+
spaces_between_special_tokens=spaces_between_special_tokens
|
76 |
+
)
|
.ipynb_checkpoints/eval-checkpoint.py
ADDED
@@ -0,0 +1,210 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
import argparse
|
3 |
+
import re
|
4 |
+
from typing import Dict
|
5 |
+
|
6 |
+
from datasets import Audio, Dataset, load_dataset, load_metric
|
7 |
+
from transformers import AutoFeatureExtractor, pipeline
|
8 |
+
from transformers import Wav2Vec2CTCTokenizer
|
9 |
+
|
10 |
+
class Wav2Vec2WordpieceTokenizer(Wav2Vec2CTCTokenizer):
|
11 |
+
def __init__(
|
12 |
+
self,
|
13 |
+
vocab_file,
|
14 |
+
bos_token="<s>",
|
15 |
+
eos_token="</s>",
|
16 |
+
unk_token="<unk>",
|
17 |
+
pad_token="<pad>",
|
18 |
+
word_delimiter_token="|",
|
19 |
+
do_lower_case=False,
|
20 |
+
**kwargs
|
21 |
+
):
|
22 |
+
super().__init__(
|
23 |
+
vocab_file=vocab_file,
|
24 |
+
unk_token=unk_token,
|
25 |
+
bos_token=bos_token,
|
26 |
+
eos_token=eos_token,
|
27 |
+
pad_token=pad_token,
|
28 |
+
do_lower_case=do_lower_case,
|
29 |
+
word_delimiter_token=word_delimiter_token,
|
30 |
+
**kwargs,
|
31 |
+
)
|
32 |
+
|
33 |
+
self._create_trie(self.all_special_tokens_extended)
|
34 |
+
|
35 |
+
def _tokenize(self, text, **kwargs):
|
36 |
+
"""
|
37 |
+
Converts a string in a sequence of tokens (string), using the tokenizer.
|
38 |
+
"""
|
39 |
+
special_cases = set(['gia', 'qui', 'quy', 'que', 'qua'])
|
40 |
+
output_tokens = []
|
41 |
+
for token_idx, token in enumerate(text.split()):
|
42 |
+
if token in special_cases:
|
43 |
+
sub_tokens = [token[:2], token[2:]]
|
44 |
+
else:
|
45 |
+
end = len(token)
|
46 |
+
sub_tokens = []
|
47 |
+
while end > 0:
|
48 |
+
start = 0
|
49 |
+
cur_substr = None
|
50 |
+
while start < end:
|
51 |
+
substr = token[start:end]
|
52 |
+
if substr in self.encoder:
|
53 |
+
cur_substr = substr
|
54 |
+
break
|
55 |
+
start += 1
|
56 |
+
if cur_substr is None:
|
57 |
+
sub_tokens.insert(0, self.unk_token)
|
58 |
+
end = start - 1
|
59 |
+
else:
|
60 |
+
sub_tokens.insert(0, cur_substr)
|
61 |
+
end = start
|
62 |
+
|
63 |
+
if token_idx > 0:
|
64 |
+
output_tokens.append(self.word_delimiter_token)
|
65 |
+
output_tokens.extend(sub_tokens)
|
66 |
+
return output_tokens
|
67 |
+
|
68 |
+
def decode_ids(
|
69 |
+
self,
|
70 |
+
token_ids,
|
71 |
+
skip_special_tokens = False,
|
72 |
+
clean_up_tokenization_spaces = True,
|
73 |
+
group_tokens: bool = True,
|
74 |
+
spaces_between_special_tokens: bool = False,
|
75 |
+
) -> str:
|
76 |
+
# For compatible with speechbrain interfaces
|
77 |
+
return self.decode(
|
78 |
+
token_ids,
|
79 |
+
skip_special_tokens=skip_special_tokens,
|
80 |
+
clean_up_tokenization_spaces=clean_up_tokenization_spaces,
|
81 |
+
group_tokens=group_tokens,
|
82 |
+
spaces_between_special_tokens=spaces_between_special_tokens
|
83 |
+
)
|
84 |
+
|
85 |
+
def log_results(result: Dataset, args: Dict[str, str]):
|
86 |
+
"""DO NOT CHANGE. This function computes and logs the result metrics."""
|
87 |
+
|
88 |
+
log_outputs = args.log_outputs
|
89 |
+
dataset_id = "_".join(args.dataset.split("/") + [args.config, args.split])
|
90 |
+
|
91 |
+
# load metric
|
92 |
+
wer = load_metric("wer")
|
93 |
+
cer = load_metric("cer")
|
94 |
+
|
95 |
+
# compute metrics
|
96 |
+
wer_result = wer.compute(references=result["target"], predictions=result["prediction"])
|
97 |
+
cer_result = cer.compute(references=result["target"], predictions=result["prediction"])
|
98 |
+
|
99 |
+
# print & log results
|
100 |
+
result_str = f"WER: {wer_result}\n" f"CER: {cer_result}"
|
101 |
+
print(result_str)
|
102 |
+
|
103 |
+
with open(f"{dataset_id}_eval_results.txt", "w") as f:
|
104 |
+
f.write(result_str)
|
105 |
+
|
106 |
+
# log all results in text file. Possibly interesting for analysis
|
107 |
+
if log_outputs is not None:
|
108 |
+
pred_file = f"log_{dataset_id}_predictions.txt"
|
109 |
+
target_file = f"log_{dataset_id}_targets.txt"
|
110 |
+
|
111 |
+
with open(pred_file, "w") as p, open(target_file, "w") as t:
|
112 |
+
|
113 |
+
# mapping function to write output
|
114 |
+
def write_to_file(batch, i):
|
115 |
+
p.write(f"{i}" + "\n")
|
116 |
+
p.write(batch["prediction"] + "\n")
|
117 |
+
t.write(f"{i}" + "\n")
|
118 |
+
t.write(batch["target"] + "\n")
|
119 |
+
|
120 |
+
result.map(write_to_file, with_indices=True)
|
121 |
+
|
122 |
+
|
123 |
+
def normalize_text(text: str) -> str:
|
124 |
+
"""DO ADAPT FOR YOUR USE CASE. this function normalizes the target text."""
|
125 |
+
|
126 |
+
chars_to_ignore_regex = '[,?.!\-\;\:"“%‘”�—’…–|]' # noqa: W605 IMPORTANT: this should correspond to the chars that were ignored during training
|
127 |
+
|
128 |
+
text = re.sub(chars_to_ignore_regex, "", text.lower())
|
129 |
+
|
130 |
+
# In addition, we can normalize the target text, e.g. removing new lines characters etc...
|
131 |
+
# note that order is important here!
|
132 |
+
token_sequences_to_ignore = ["\n\n", "\n", " ", " "]
|
133 |
+
|
134 |
+
for t in token_sequences_to_ignore:
|
135 |
+
text = " ".join(text.split(t))
|
136 |
+
|
137 |
+
return text
|
138 |
+
|
139 |
+
|
140 |
+
def main(args):
|
141 |
+
# load dataset
|
142 |
+
dataset = load_dataset(args.dataset, args.config, split=args.split, use_auth_token=True)
|
143 |
+
|
144 |
+
# for testing: only process the first two examples as a test
|
145 |
+
dataset = dataset.select(range(10))
|
146 |
+
|
147 |
+
# load processor
|
148 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(args.model_id)
|
149 |
+
sampling_rate = feature_extractor.sampling_rate
|
150 |
+
|
151 |
+
# load tokenizer
|
152 |
+
tokenizer = Wav2Vec2WordpieceTokenizer(
|
153 |
+
vocab_file = args.model_id + 'vocab.json',
|
154 |
+
)
|
155 |
+
|
156 |
+
# resample audio
|
157 |
+
dataset = dataset.cast_column("audio", Audio(sampling_rate=sampling_rate))
|
158 |
+
|
159 |
+
# load eval pipeline
|
160 |
+
asr = pipeline(
|
161 |
+
"automatic-speech-recognition",
|
162 |
+
model=args.model_id,
|
163 |
+
tokenizer = tokenizer
|
164 |
+
)
|
165 |
+
# map function to decode audio
|
166 |
+
def map_to_pred(batch):
|
167 |
+
prediction = asr(
|
168 |
+
batch["audio"]["array"], chunk_length_s=args.chunk_length_s, stride_length_s=args.stride_length_s
|
169 |
+
)
|
170 |
+
|
171 |
+
batch["prediction"] = prediction["text"]
|
172 |
+
batch["target"] = normalize_text(batch["sentence"])
|
173 |
+
return batch
|
174 |
+
|
175 |
+
# run inference on all examples
|
176 |
+
result = dataset.map(map_to_pred, remove_columns=dataset.column_names)
|
177 |
+
|
178 |
+
# compute and log_results
|
179 |
+
# do not change function below
|
180 |
+
log_results(result, args)
|
181 |
+
|
182 |
+
|
183 |
+
if __name__ == "__main__":
|
184 |
+
parser = argparse.ArgumentParser()
|
185 |
+
|
186 |
+
parser.add_argument(
|
187 |
+
"--model_id", type=str, required=True, help="Model identifier. Should be loadable with 🤗 Transformers"
|
188 |
+
)
|
189 |
+
parser.add_argument(
|
190 |
+
"--dataset",
|
191 |
+
type=str,
|
192 |
+
required=True,
|
193 |
+
help="Dataset name to evaluate the `model_id`. Should be loadable with 🤗 Datasets",
|
194 |
+
)
|
195 |
+
parser.add_argument(
|
196 |
+
"--config", type=str, required=True, help="Config of the dataset. *E.g.* `'en'` for Common Voice"
|
197 |
+
)
|
198 |
+
parser.add_argument("--split", type=str, required=True, help="Split of the dataset. *E.g.* `'test'`")
|
199 |
+
parser.add_argument(
|
200 |
+
"--chunk_length_s", type=float, default=None, help="Chunk length in seconds. Defaults to 5 seconds."
|
201 |
+
)
|
202 |
+
parser.add_argument(
|
203 |
+
"--stride_length_s", type=float, default=None, help="Stride of the audio chunks. Defaults to 1 second."
|
204 |
+
)
|
205 |
+
parser.add_argument(
|
206 |
+
"--log_outputs", action="store_true", help="If defined, write outputs to log file for analysis."
|
207 |
+
)
|
208 |
+
args = parser.parse_args()
|
209 |
+
|
210 |
+
main(args)
|
all_results.json
CHANGED
@@ -1,14 +1,14 @@
|
|
1 |
{
|
2 |
"epoch": 50.0,
|
3 |
"eval_loss": 4.169058322906494,
|
4 |
-
"eval_runtime": 34.
|
5 |
"eval_samples": 761,
|
6 |
-
"eval_samples_per_second": 22.
|
7 |
-
"eval_steps_per_second": 1.
|
8 |
"eval_wer": 0.4132525828286427,
|
9 |
-
"train_loss": 0.
|
10 |
-
"train_runtime":
|
11 |
"train_samples": 25915,
|
12 |
-
"train_samples_per_second":
|
13 |
-
"train_steps_per_second":
|
14 |
}
|
|
|
1 |
{
|
2 |
"epoch": 50.0,
|
3 |
"eval_loss": 4.169058322906494,
|
4 |
+
"eval_runtime": 34.3912,
|
5 |
"eval_samples": 761,
|
6 |
+
"eval_samples_per_second": 22.128,
|
7 |
+
"eval_steps_per_second": 1.396,
|
8 |
"eval_wer": 0.4132525828286427,
|
9 |
+
"train_loss": 0.0,
|
10 |
+
"train_runtime": 69.3888,
|
11 |
"train_samples": 25915,
|
12 |
+
"train_samples_per_second": 3734.751,
|
13 |
+
"train_steps_per_second": 116.733
|
14 |
}
|
custom.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import Wav2Vec2CTCTokenizer
|
2 |
+
|
3 |
+
class Wav2Vec2WordpieceTokenizer(Wav2Vec2CTCTokenizer):
|
4 |
+
def __init__(
|
5 |
+
self,
|
6 |
+
vocab_file,
|
7 |
+
bos_token="<s>",
|
8 |
+
eos_token="</s>",
|
9 |
+
unk_token="<unk>",
|
10 |
+
pad_token="<pad>",
|
11 |
+
word_delimiter_token="|",
|
12 |
+
do_lower_case=False,
|
13 |
+
**kwargs
|
14 |
+
):
|
15 |
+
super().__init__(
|
16 |
+
vocab_file=vocab_file,
|
17 |
+
unk_token=unk_token,
|
18 |
+
bos_token=bos_token,
|
19 |
+
eos_token=eos_token,
|
20 |
+
pad_token=pad_token,
|
21 |
+
do_lower_case=do_lower_case,
|
22 |
+
word_delimiter_token=word_delimiter_token,
|
23 |
+
**kwargs,
|
24 |
+
)
|
25 |
+
|
26 |
+
self._create_trie(self.all_special_tokens_extended)
|
27 |
+
|
28 |
+
def _tokenize(self, text, **kwargs):
|
29 |
+
"""
|
30 |
+
Converts a string in a sequence of tokens (string), using the tokenizer.
|
31 |
+
"""
|
32 |
+
special_cases = set(['gia', 'qui', 'quy', 'que', 'qua'])
|
33 |
+
output_tokens = []
|
34 |
+
for token_idx, token in enumerate(text.split()):
|
35 |
+
if token in special_cases:
|
36 |
+
sub_tokens = [token[:2], token[2:]]
|
37 |
+
else:
|
38 |
+
end = len(token)
|
39 |
+
sub_tokens = []
|
40 |
+
while end > 0:
|
41 |
+
start = 0
|
42 |
+
cur_substr = None
|
43 |
+
while start < end:
|
44 |
+
substr = token[start:end]
|
45 |
+
if substr in self.encoder:
|
46 |
+
cur_substr = substr
|
47 |
+
break
|
48 |
+
start += 1
|
49 |
+
if cur_substr is None:
|
50 |
+
sub_tokens.insert(0, self.unk_token)
|
51 |
+
end = start - 1
|
52 |
+
else:
|
53 |
+
sub_tokens.insert(0, cur_substr)
|
54 |
+
end = start
|
55 |
+
|
56 |
+
if token_idx > 0:
|
57 |
+
output_tokens.append(self.word_delimiter_token)
|
58 |
+
output_tokens.extend(sub_tokens)
|
59 |
+
return output_tokens
|
60 |
+
|
61 |
+
def decode_ids(
|
62 |
+
self,
|
63 |
+
token_ids,
|
64 |
+
skip_special_tokens = False,
|
65 |
+
clean_up_tokenization_spaces = True,
|
66 |
+
group_tokens: bool = True,
|
67 |
+
spaces_between_special_tokens: bool = False,
|
68 |
+
) -> str:
|
69 |
+
# For compatible with speechbrain interfaces
|
70 |
+
return self.decode(
|
71 |
+
token_ids,
|
72 |
+
skip_special_tokens=skip_special_tokens,
|
73 |
+
clean_up_tokenization_spaces=clean_up_tokenization_spaces,
|
74 |
+
group_tokens=group_tokens,
|
75 |
+
spaces_between_special_tokens=spaces_between_special_tokens
|
76 |
+
)
|
eval.py
ADDED
@@ -0,0 +1,210 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
import argparse
|
3 |
+
import re
|
4 |
+
from typing import Dict
|
5 |
+
|
6 |
+
from datasets import Audio, Dataset, load_dataset, load_metric
|
7 |
+
from transformers import AutoFeatureExtractor, pipeline
|
8 |
+
from transformers import Wav2Vec2CTCTokenizer
|
9 |
+
|
10 |
+
class Wav2Vec2WordpieceTokenizer(Wav2Vec2CTCTokenizer):
|
11 |
+
def __init__(
|
12 |
+
self,
|
13 |
+
vocab_file,
|
14 |
+
bos_token="<s>",
|
15 |
+
eos_token="</s>",
|
16 |
+
unk_token="<unk>",
|
17 |
+
pad_token="<pad>",
|
18 |
+
word_delimiter_token="|",
|
19 |
+
do_lower_case=False,
|
20 |
+
**kwargs
|
21 |
+
):
|
22 |
+
super().__init__(
|
23 |
+
vocab_file=vocab_file,
|
24 |
+
unk_token=unk_token,
|
25 |
+
bos_token=bos_token,
|
26 |
+
eos_token=eos_token,
|
27 |
+
pad_token=pad_token,
|
28 |
+
do_lower_case=do_lower_case,
|
29 |
+
word_delimiter_token=word_delimiter_token,
|
30 |
+
**kwargs,
|
31 |
+
)
|
32 |
+
|
33 |
+
self._create_trie(self.all_special_tokens_extended)
|
34 |
+
|
35 |
+
def _tokenize(self, text, **kwargs):
|
36 |
+
"""
|
37 |
+
Converts a string in a sequence of tokens (string), using the tokenizer.
|
38 |
+
"""
|
39 |
+
special_cases = set(['gia', 'qui', 'quy', 'que', 'qua'])
|
40 |
+
output_tokens = []
|
41 |
+
for token_idx, token in enumerate(text.split()):
|
42 |
+
if token in special_cases:
|
43 |
+
sub_tokens = [token[:2], token[2:]]
|
44 |
+
else:
|
45 |
+
end = len(token)
|
46 |
+
sub_tokens = []
|
47 |
+
while end > 0:
|
48 |
+
start = 0
|
49 |
+
cur_substr = None
|
50 |
+
while start < end:
|
51 |
+
substr = token[start:end]
|
52 |
+
if substr in self.encoder:
|
53 |
+
cur_substr = substr
|
54 |
+
break
|
55 |
+
start += 1
|
56 |
+
if cur_substr is None:
|
57 |
+
sub_tokens.insert(0, self.unk_token)
|
58 |
+
end = start - 1
|
59 |
+
else:
|
60 |
+
sub_tokens.insert(0, cur_substr)
|
61 |
+
end = start
|
62 |
+
|
63 |
+
if token_idx > 0:
|
64 |
+
output_tokens.append(self.word_delimiter_token)
|
65 |
+
output_tokens.extend(sub_tokens)
|
66 |
+
return output_tokens
|
67 |
+
|
68 |
+
def decode_ids(
|
69 |
+
self,
|
70 |
+
token_ids,
|
71 |
+
skip_special_tokens = False,
|
72 |
+
clean_up_tokenization_spaces = True,
|
73 |
+
group_tokens: bool = True,
|
74 |
+
spaces_between_special_tokens: bool = False,
|
75 |
+
) -> str:
|
76 |
+
# For compatible with speechbrain interfaces
|
77 |
+
return self.decode(
|
78 |
+
token_ids,
|
79 |
+
skip_special_tokens=skip_special_tokens,
|
80 |
+
clean_up_tokenization_spaces=clean_up_tokenization_spaces,
|
81 |
+
group_tokens=group_tokens,
|
82 |
+
spaces_between_special_tokens=spaces_between_special_tokens
|
83 |
+
)
|
84 |
+
|
85 |
+
def log_results(result: Dataset, args: Dict[str, str]):
|
86 |
+
"""DO NOT CHANGE. This function computes and logs the result metrics."""
|
87 |
+
|
88 |
+
log_outputs = args.log_outputs
|
89 |
+
dataset_id = "_".join(args.dataset.split("/") + [args.config, args.split])
|
90 |
+
|
91 |
+
# load metric
|
92 |
+
wer = load_metric("wer")
|
93 |
+
cer = load_metric("cer")
|
94 |
+
|
95 |
+
# compute metrics
|
96 |
+
wer_result = wer.compute(references=result["target"], predictions=result["prediction"])
|
97 |
+
cer_result = cer.compute(references=result["target"], predictions=result["prediction"])
|
98 |
+
|
99 |
+
# print & log results
|
100 |
+
result_str = f"WER: {wer_result}\n" f"CER: {cer_result}"
|
101 |
+
print(result_str)
|
102 |
+
|
103 |
+
with open(f"{dataset_id}_eval_results.txt", "w") as f:
|
104 |
+
f.write(result_str)
|
105 |
+
|
106 |
+
# log all results in text file. Possibly interesting for analysis
|
107 |
+
if log_outputs is not None:
|
108 |
+
pred_file = f"log_{dataset_id}_predictions.txt"
|
109 |
+
target_file = f"log_{dataset_id}_targets.txt"
|
110 |
+
|
111 |
+
with open(pred_file, "w") as p, open(target_file, "w") as t:
|
112 |
+
|
113 |
+
# mapping function to write output
|
114 |
+
def write_to_file(batch, i):
|
115 |
+
p.write(f"{i}" + "\n")
|
116 |
+
p.write(batch["prediction"] + "\n")
|
117 |
+
t.write(f"{i}" + "\n")
|
118 |
+
t.write(batch["target"] + "\n")
|
119 |
+
|
120 |
+
result.map(write_to_file, with_indices=True)
|
121 |
+
|
122 |
+
|
123 |
+
def normalize_text(text: str) -> str:
|
124 |
+
"""DO ADAPT FOR YOUR USE CASE. this function normalizes the target text."""
|
125 |
+
|
126 |
+
chars_to_ignore_regex = '[,?.!\-\;\:"“%‘”�—’…–|]' # noqa: W605 IMPORTANT: this should correspond to the chars that were ignored during training
|
127 |
+
|
128 |
+
text = re.sub(chars_to_ignore_regex, "", text.lower())
|
129 |
+
|
130 |
+
# In addition, we can normalize the target text, e.g. removing new lines characters etc...
|
131 |
+
# note that order is important here!
|
132 |
+
token_sequences_to_ignore = ["\n\n", "\n", " ", " "]
|
133 |
+
|
134 |
+
for t in token_sequences_to_ignore:
|
135 |
+
text = " ".join(text.split(t))
|
136 |
+
|
137 |
+
return text
|
138 |
+
|
139 |
+
|
140 |
+
def main(args):
|
141 |
+
# load dataset
|
142 |
+
dataset = load_dataset(args.dataset, args.config, split=args.split, use_auth_token=True)
|
143 |
+
|
144 |
+
# for testing: only process the first two examples as a test
|
145 |
+
dataset = dataset.select(range(10))
|
146 |
+
|
147 |
+
# load processor
|
148 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(args.model_id)
|
149 |
+
sampling_rate = feature_extractor.sampling_rate
|
150 |
+
|
151 |
+
# load tokenizer
|
152 |
+
tokenizer = Wav2Vec2WordpieceTokenizer(
|
153 |
+
vocab_file = args.model_id + 'vocab.json',
|
154 |
+
)
|
155 |
+
|
156 |
+
# resample audio
|
157 |
+
dataset = dataset.cast_column("audio", Audio(sampling_rate=sampling_rate))
|
158 |
+
|
159 |
+
# load eval pipeline
|
160 |
+
asr = pipeline(
|
161 |
+
"automatic-speech-recognition",
|
162 |
+
model=args.model_id,
|
163 |
+
tokenizer = tokenizer
|
164 |
+
)
|
165 |
+
# map function to decode audio
|
166 |
+
def map_to_pred(batch):
|
167 |
+
prediction = asr(
|
168 |
+
batch["audio"]["array"], chunk_length_s=args.chunk_length_s, stride_length_s=args.stride_length_s
|
169 |
+
)
|
170 |
+
|
171 |
+
batch["prediction"] = prediction["text"]
|
172 |
+
batch["target"] = normalize_text(batch["sentence"])
|
173 |
+
return batch
|
174 |
+
|
175 |
+
# run inference on all examples
|
176 |
+
result = dataset.map(map_to_pred, remove_columns=dataset.column_names)
|
177 |
+
|
178 |
+
# compute and log_results
|
179 |
+
# do not change function below
|
180 |
+
log_results(result, args)
|
181 |
+
|
182 |
+
|
183 |
+
if __name__ == "__main__":
|
184 |
+
parser = argparse.ArgumentParser()
|
185 |
+
|
186 |
+
parser.add_argument(
|
187 |
+
"--model_id", type=str, required=True, help="Model identifier. Should be loadable with 🤗 Transformers"
|
188 |
+
)
|
189 |
+
parser.add_argument(
|
190 |
+
"--dataset",
|
191 |
+
type=str,
|
192 |
+
required=True,
|
193 |
+
help="Dataset name to evaluate the `model_id`. Should be loadable with 🤗 Datasets",
|
194 |
+
)
|
195 |
+
parser.add_argument(
|
196 |
+
"--config", type=str, required=True, help="Config of the dataset. *E.g.* `'en'` for Common Voice"
|
197 |
+
)
|
198 |
+
parser.add_argument("--split", type=str, required=True, help="Split of the dataset. *E.g.* `'test'`")
|
199 |
+
parser.add_argument(
|
200 |
+
"--chunk_length_s", type=float, default=None, help="Chunk length in seconds. Defaults to 5 seconds."
|
201 |
+
)
|
202 |
+
parser.add_argument(
|
203 |
+
"--stride_length_s", type=float, default=None, help="Stride of the audio chunks. Defaults to 1 second."
|
204 |
+
)
|
205 |
+
parser.add_argument(
|
206 |
+
"--log_outputs", action="store_true", help="If defined, write outputs to log file for analysis."
|
207 |
+
)
|
208 |
+
args = parser.parse_args()
|
209 |
+
|
210 |
+
main(args)
|
eval_results.json
CHANGED
@@ -1,9 +1,9 @@
|
|
1 |
{
|
2 |
"epoch": 50.0,
|
3 |
"eval_loss": 4.169058322906494,
|
4 |
-
"eval_runtime": 34.
|
5 |
"eval_samples": 761,
|
6 |
-
"eval_samples_per_second": 22.
|
7 |
-
"eval_steps_per_second": 1.
|
8 |
"eval_wer": 0.4132525828286427
|
9 |
}
|
|
|
1 |
{
|
2 |
"epoch": 50.0,
|
3 |
"eval_loss": 4.169058322906494,
|
4 |
+
"eval_runtime": 34.3912,
|
5 |
"eval_samples": 761,
|
6 |
+
"eval_samples_per_second": 22.128,
|
7 |
+
"eval_steps_per_second": 1.396,
|
8 |
"eval_wer": 0.4132525828286427
|
9 |
}
|
log_mozilla-foundation_common_voice_7_0_vi_test_predictions.txt
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
0
|
2 |
+
nông mấy người giữ thụ độc này n
|
3 |
+
1
|
4 |
+
thoặc là có thể lái nhiệng khu mái manh
|
5 |
+
2
|
6 |
+
n không sồng không săn n
|
7 |
+
3
|
8 |
+
buồn thế n
|
9 |
+
4
|
10 |
+
dứt lời chinh vội bỏ ra ngoài quên lấy cải tiền
|
11 |
+
5
|
12 |
+
chịt nghẹ
|
13 |
+
6
|
14 |
+
ở bên trong chinh tự hỏn
|
15 |
+
7
|
16 |
+
nhưng đáng tiếc là thảo không hề suất
|
17 |
+
8
|
18 |
+
nó bắt hồn anh chàng ấy định biến anh ta thành một cặp đôi
|
19 |
+
9
|
20 |
+
mấy người cứ tin thầy cúng rồi mất mạng lại chác nhau
|
log_mozilla-foundation_common_voice_7_0_vi_test_targets.txt
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
0
|
2 |
+
ở cái nơi rừng thiêng nước độc này
|
3 |
+
1
|
4 |
+
hoặc là có thể là nhờ kuman
|
5 |
+
2
|
6 |
+
không không không sao đâu
|
7 |
+
3
|
8 |
+
buồn thế chứ
|
9 |
+
4
|
10 |
+
dứt lời trinh vội bỏ ra ngoài quên lấy cả tiền
|
11 |
+
5
|
12 |
+
kịch nghệ
|
13 |
+
6
|
14 |
+
ở bên trong trinh tự hỏi
|
15 |
+
7
|
16 |
+
nhưng đáng tiếc là thảo không hề xuất
|
17 |
+
8
|
18 |
+
nó bắt hồn anh chàng ấy định biến anh ta thành một cặp đôi
|
19 |
+
9
|
20 |
+
mấy người cứ tin thầy cúng rồi mất mạng lại trách nhau
|
log_vivos_vi_test_predictions.txt
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
0
|
2 |
+
ửa vòng trái đất hơn bảy năm
|
3 |
+
1
|
4 |
+
ốn chiếc trong đái phèn la
|
5 |
+
2
|
6 |
+
ố những thay lật khồng mằn
|
7 |
+
3
|
8 |
+
ông nhạc nó lên
|
9 |
+
4
|
10 |
+
ế nhưng khi giá phôi thép thế giới cao dần
|
11 |
+
5
|
12 |
+
ơn gọi tài địa phương là trắc thối
|
13 |
+
6
|
14 |
+
oanh nghiệp tính dụng lao động nước ngoài
|
15 |
+
7
|
16 |
+
ức khắc ập về kéo mọi người thứ trở lại thời hoang sơ
|
17 |
+
8
|
18 |
+
ới đây nhất là các tiểu vương quốc á rập thống nhất
|
19 |
+
9
|
20 |
+
ần nữa mãi mới gặp được tuyên
|
log_vivos_vi_test_targets.txt
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
0
|
2 |
+
nửa vòng trái đất hơn bảy năm
|
3 |
+
1
|
4 |
+
bốn chiếc trống đại phèng la
|
5 |
+
2
|
6 |
+
của những thế lực hùng mạnh
|
7 |
+
3
|
8 |
+
ông nhặt nó lên
|
9 |
+
4
|
10 |
+
thế nhưng khi giá phôi thép thế giới cao dần
|
11 |
+
5
|
12 |
+
tên gọi tại địa phương là trắc thối
|
13 |
+
6
|
14 |
+
doanh nghiệp tuyển dụng lao động nước ngoài
|
15 |
+
7
|
16 |
+
tức khắc ập về kéo mọi người thứ trở lại thời hoang sơ
|
17 |
+
8
|
18 |
+
mới đây nhất là các tiểu vương quốc ả rập thống nhất
|
19 |
+
9
|
20 |
+
lần lữa mãi mới gặp được tuyên
|
runs/Jan29_00-37-33_job-fa775f5b-8438-4c3d-95fa-ed70ddaee577/1643416720.7954614/events.out.tfevents.1643416720.job-fa775f5b-8438-4c3d-95fa-ed70ddaee577.1784910.1
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:7cae50cf7b5f569ace47fc42cf37c65b52adf354c0a74b8c3b3cb3c35d6e9411
|
3 |
+
size 4811
|
runs/Jan29_00-37-33_job-fa775f5b-8438-4c3d-95fa-ed70ddaee577/events.out.tfevents.1643416720.job-fa775f5b-8438-4c3d-95fa-ed70ddaee577.1784910.0
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:7d0ffd09dddff490466e0231f686ace1683c87954d3215505f8cccee81d0e4a9
|
3 |
+
size 4747
|
runs/Jan29_00-45-45_job-fa775f5b-8438-4c3d-95fa-ed70ddaee577/1643417179.7920382/events.out.tfevents.1643417179.job-fa775f5b-8438-4c3d-95fa-ed70ddaee577.1788197.1
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:45c66466692334d96c03c708dede3130ca4e3504e1aeb66510cbe60f0d9ec6a0
|
3 |
+
size 4811
|
runs/Jan29_00-45-45_job-fa775f5b-8438-4c3d-95fa-ed70ddaee577/events.out.tfevents.1643417179.job-fa775f5b-8438-4c3d-95fa-ed70ddaee577.1788197.0
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:860e59bfd681a686b6ac406314bc35097fe304791d59c3a846bede3300c40c4a
|
3 |
+
size 5107
|
runs/Jan29_00-45-45_job-fa775f5b-8438-4c3d-95fa-ed70ddaee577/events.out.tfevents.1643417291.job-fa775f5b-8438-4c3d-95fa-ed70ddaee577.1788197.2
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:fae2e456fa77d50de4c31bf4c4e2794821ccc799e503b83bebe265198d4d8d22
|
3 |
+
size 364
|
train_results.json
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
{
|
2 |
"epoch": 50.0,
|
3 |
-
"train_loss": 0.
|
4 |
-
"train_runtime":
|
5 |
"train_samples": 25915,
|
6 |
-
"train_samples_per_second":
|
7 |
-
"train_steps_per_second":
|
8 |
}
|
|
|
1 |
{
|
2 |
"epoch": 50.0,
|
3 |
+
"train_loss": 0.0,
|
4 |
+
"train_runtime": 69.3888,
|
5 |
"train_samples": 25915,
|
6 |
+
"train_samples_per_second": 3734.751,
|
7 |
+
"train_steps_per_second": 116.733
|
8 |
}
|
trainer_state.json
CHANGED
@@ -416,14 +416,14 @@
|
|
416 |
"epoch": 50.0,
|
417 |
"step": 40500,
|
418 |
"total_flos": 5.1003805267852526e+20,
|
419 |
-
"train_loss": 0.
|
420 |
-
"train_runtime":
|
421 |
-
"train_samples_per_second":
|
422 |
-
"train_steps_per_second":
|
423 |
}
|
424 |
],
|
425 |
-
"max_steps":
|
426 |
-
"num_train_epochs":
|
427 |
"total_flos": 5.1003805267852526e+20,
|
428 |
"trial_name": null,
|
429 |
"trial_params": null
|
|
|
416 |
"epoch": 50.0,
|
417 |
"step": 40500,
|
418 |
"total_flos": 5.1003805267852526e+20,
|
419 |
+
"train_loss": 0.0,
|
420 |
+
"train_runtime": 69.3888,
|
421 |
+
"train_samples_per_second": 3734.751,
|
422 |
+
"train_steps_per_second": 116.733
|
423 |
}
|
424 |
],
|
425 |
+
"max_steps": 8100,
|
426 |
+
"num_train_epochs": 10,
|
427 |
"total_flos": 5.1003805267852526e+20,
|
428 |
"trial_name": null,
|
429 |
"trial_params": null
|
training_args.bin
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
size 3055
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:9f7b935061dd3476a8cedb212f63c7b7aff005629bf2fdf5f3d75ed71568b22d
|
3 |
size 3055
|