Very early results
Browse files- README.md +107 -0
- config.json +108 -0
- eval.py +161 -0
README.md
CHANGED
@@ -1,3 +1,110 @@
|
|
1 |
---
|
|
|
|
|
2 |
license: apache-2.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
language:
|
3 |
+
- sk
|
4 |
license: apache-2.0
|
5 |
+
tags:
|
6 |
+
- automatic-speech-recognition
|
7 |
+
- mozilla-foundation/common_voice_8_0
|
8 |
+
- robust-speech-event
|
9 |
+
- xlsr-fine-tuning-week
|
10 |
+
datasets:
|
11 |
+
- common_voice
|
12 |
+
model-index:
|
13 |
+
- name: Slovak comodoro Wav2Vec2 XLSR 300M CV8
|
14 |
+
results:
|
15 |
+
- task:
|
16 |
+
name: Automatic Speech Recognition
|
17 |
+
type: automatic-speech-recognition
|
18 |
+
dataset:
|
19 |
+
name: Common Voice 8
|
20 |
+
type: mozilla-foundation/common_voice_8_0
|
21 |
+
args: sk
|
22 |
+
metrics:
|
23 |
+
- name: Test WER
|
24 |
+
type: wer
|
25 |
+
value: 55.2
|
26 |
+
- name: Test CER
|
27 |
+
type: cer
|
28 |
+
value: 14.4
|
29 |
---
|
30 |
+
<!-- This model card has been generated automatically according to the information the Trainer had access to. You
|
31 |
+
should probably proofread and complete it, then remove this comment. -->
|
32 |
+
|
33 |
+
# wav2vec2-xls-r-300m-cs-cv8
|
34 |
+
|
35 |
+
This model is a fine-tuned version of [facebook/wav2vec2-xls-r-300m](https://huggingface.co/facebook/wav2vec2-xls-r-300m) on the common_voice 8.0 dataset.
|
36 |
+
It achieves the following results on the evaluation set:
|
37 |
+
- Wer: 55.2
|
38 |
+
- Cer: 14.4
|
39 |
+
|
40 |
+
## Usage
|
41 |
+
|
42 |
+
The model can be used directly (without a language model) as follows:
|
43 |
+
|
44 |
+
```python
|
45 |
+
import torch
|
46 |
+
import torchaudio
|
47 |
+
from datasets import load_dataset
|
48 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
49 |
+
|
50 |
+
test_dataset = load_dataset("mozilla-foundation/common_voice_8_0", "sk", split="test[:2%]")
|
51 |
+
|
52 |
+
processor = Wav2Vec2Processor.from_pretrained("comodoro/wav2vec2-xls-r-300m-sk-cv8")
|
53 |
+
model = Wav2Vec2ForCTC.from_pretrained("comodoro/wav2vec2-xls-r-300m-sk-cv8")
|
54 |
+
|
55 |
+
resampler = torchaudio.transforms.Resample(48_000, 16_000)
|
56 |
+
|
57 |
+
# Preprocessing the datasets.
|
58 |
+
# We need to read the aduio files as arrays
|
59 |
+
def speech_file_to_array_fn(batch):
|
60 |
+
speech_array, sampling_rate = torchaudio.load(batch["path"])
|
61 |
+
batch["speech"] = resampler(speech_array).squeeze().numpy()
|
62 |
+
return batch
|
63 |
+
|
64 |
+
test_dataset = test_dataset.map(speech_file_to_array_fn)
|
65 |
+
inputs = processor(test_dataset[:2]["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
|
66 |
+
|
67 |
+
with torch.no_grad():
|
68 |
+
logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
|
69 |
+
|
70 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
71 |
+
|
72 |
+
print("Prediction:", processor.batch_decode(predicted_ids))
|
73 |
+
print("Reference:", test_dataset[:2]["sentence"])
|
74 |
+
```
|
75 |
+
|
76 |
+
## Evaluation
|
77 |
+
|
78 |
+
The model can be evaluated using the attached `eval.py` script:
|
79 |
+
```
|
80 |
+
python eval.py --model_id comodoro/wav2vec2-xls-r-300m-sk-cv8 --dataset mozilla-foundation/common-voice_8_0 --split test --config sk
|
81 |
+
```
|
82 |
+
|
83 |
+
## Training and evaluation data
|
84 |
+
|
85 |
+
The Common Voice 8.0 `train` and `validation` datasets were used for training
|
86 |
+
|
87 |
+
## Training procedure
|
88 |
+
|
89 |
+
### Training hyperparameters
|
90 |
+
|
91 |
+
The following hyperparameters were used during training:
|
92 |
+
|
93 |
+
- learning_rate: 7e-4
|
94 |
+
- train_batch_size: 32
|
95 |
+
- eval_batch_size: 8
|
96 |
+
- seed: 42
|
97 |
+
- gradient_accumulation_steps: 20
|
98 |
+
- total_train_batch_size: 640
|
99 |
+
- optimizer: Adam with betas=(0.9,0.999) and epsilon=1e-08
|
100 |
+
- lr_scheduler_type: linear
|
101 |
+
- lr_scheduler_warmup_steps: 500
|
102 |
+
- num_epochs: 50
|
103 |
+
- mixed_precision_training: Native AMP
|
104 |
+
|
105 |
+
### Framework versions
|
106 |
+
|
107 |
+
- Transformers 4.16.0.dev0
|
108 |
+
- Pytorch 1.10.1+cu102
|
109 |
+
- Datasets 1.17.1.dev0
|
110 |
+
- Tokenizers 0.11.0
|
config.json
ADDED
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "facebook/wav2vec2-xls-r-300m",
|
3 |
+
"activation_dropout": 0.0,
|
4 |
+
"adapter_kernel_size": 3,
|
5 |
+
"adapter_stride": 2,
|
6 |
+
"add_adapter": false,
|
7 |
+
"apply_spec_augment": true,
|
8 |
+
"architectures": [
|
9 |
+
"Wav2Vec2ForCTC"
|
10 |
+
],
|
11 |
+
"attention_dropout": 0.1,
|
12 |
+
"bos_token_id": 1,
|
13 |
+
"classifier_proj_size": 256,
|
14 |
+
"codevector_dim": 768,
|
15 |
+
"contrastive_logits_temperature": 0.1,
|
16 |
+
"conv_bias": true,
|
17 |
+
"conv_dim": [
|
18 |
+
512,
|
19 |
+
512,
|
20 |
+
512,
|
21 |
+
512,
|
22 |
+
512,
|
23 |
+
512,
|
24 |
+
512
|
25 |
+
],
|
26 |
+
"conv_kernel": [
|
27 |
+
10,
|
28 |
+
3,
|
29 |
+
3,
|
30 |
+
3,
|
31 |
+
3,
|
32 |
+
2,
|
33 |
+
2
|
34 |
+
],
|
35 |
+
"conv_stride": [
|
36 |
+
5,
|
37 |
+
2,
|
38 |
+
2,
|
39 |
+
2,
|
40 |
+
2,
|
41 |
+
2,
|
42 |
+
2
|
43 |
+
],
|
44 |
+
"ctc_loss_reduction": "mean",
|
45 |
+
"ctc_zero_infinity": false,
|
46 |
+
"diversity_loss_weight": 0.1,
|
47 |
+
"do_stable_layer_norm": true,
|
48 |
+
"eos_token_id": 2,
|
49 |
+
"feat_extract_activation": "gelu",
|
50 |
+
"feat_extract_dropout": 0.0,
|
51 |
+
"feat_extract_norm": "layer",
|
52 |
+
"feat_proj_dropout": 0.0,
|
53 |
+
"feat_quantizer_dropout": 0.0,
|
54 |
+
"final_dropout": 0.0,
|
55 |
+
"gradient_checkpointing": false,
|
56 |
+
"hidden_act": "gelu",
|
57 |
+
"hidden_dropout": 0.1,
|
58 |
+
"hidden_size": 1024,
|
59 |
+
"initializer_range": 0.02,
|
60 |
+
"intermediate_size": 4096,
|
61 |
+
"layer_norm_eps": 1e-05,
|
62 |
+
"layerdrop": 0.1,
|
63 |
+
"mask_feature_length": 10,
|
64 |
+
"mask_feature_min_masks": 0,
|
65 |
+
"mask_feature_prob": 0.0,
|
66 |
+
"mask_time_length": 10,
|
67 |
+
"mask_time_min_masks": 2,
|
68 |
+
"mask_time_prob": 0.05,
|
69 |
+
"model_type": "wav2vec2",
|
70 |
+
"num_adapter_layers": 3,
|
71 |
+
"num_attention_heads": 16,
|
72 |
+
"num_codevector_groups": 2,
|
73 |
+
"num_codevectors_per_group": 320,
|
74 |
+
"num_conv_pos_embedding_groups": 16,
|
75 |
+
"num_conv_pos_embeddings": 128,
|
76 |
+
"num_feat_extract_layers": 7,
|
77 |
+
"num_hidden_layers": 24,
|
78 |
+
"num_negatives": 100,
|
79 |
+
"output_hidden_size": 1024,
|
80 |
+
"pad_token_id": 46,
|
81 |
+
"proj_codevector_dim": 768,
|
82 |
+
"tdnn_dilation": [
|
83 |
+
1,
|
84 |
+
2,
|
85 |
+
3,
|
86 |
+
1,
|
87 |
+
1
|
88 |
+
],
|
89 |
+
"tdnn_dim": [
|
90 |
+
512,
|
91 |
+
512,
|
92 |
+
512,
|
93 |
+
512,
|
94 |
+
1500
|
95 |
+
],
|
96 |
+
"tdnn_kernel": [
|
97 |
+
5,
|
98 |
+
3,
|
99 |
+
3,
|
100 |
+
1,
|
101 |
+
1
|
102 |
+
],
|
103 |
+
"torch_dtype": "float32",
|
104 |
+
"transformers_version": "4.16.0.dev0",
|
105 |
+
"use_weighted_layer_sum": false,
|
106 |
+
"vocab_size": 49,
|
107 |
+
"xvector_output_dim": 512
|
108 |
+
}
|
eval.py
ADDED
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
from datasets import load_dataset, load_metric, Audio, Dataset
|
3 |
+
from transformers import pipeline, AutoFeatureExtractor
|
4 |
+
import re
|
5 |
+
import argparse
|
6 |
+
import unicodedata
|
7 |
+
from typing import Dict
|
8 |
+
|
9 |
+
|
10 |
+
def log_results(result: Dataset, args: Dict[str, str]):
|
11 |
+
""" DO NOT CHANGE. This function computes and logs the result metrics. """
|
12 |
+
|
13 |
+
log_outputs = args.log_outputs
|
14 |
+
dataset_id = "_".join(args.dataset.split("/") + [args.config, args.split])
|
15 |
+
|
16 |
+
# load metric
|
17 |
+
wer = load_metric("wer")
|
18 |
+
cer = load_metric("cer")
|
19 |
+
|
20 |
+
# compute metrics
|
21 |
+
wer_result = wer.compute(references=result["target"], predictions=result["prediction"])
|
22 |
+
cer_result = cer.compute(references=result["target"], predictions=result["prediction"])
|
23 |
+
|
24 |
+
# print & log results
|
25 |
+
result_str = (
|
26 |
+
f"WER: {wer_result}\n"
|
27 |
+
f"CER: {cer_result}"
|
28 |
+
)
|
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 |
+
|
55 |
+
CHARS = {
|
56 |
+
'ü': 'ue',
|
57 |
+
'ö': 'oe',
|
58 |
+
'ï': 'i',
|
59 |
+
'ë': 'e',
|
60 |
+
'ã': 'a',
|
61 |
+
'à': 'á',
|
62 |
+
'ø': 'o',
|
63 |
+
'è': 'é',
|
64 |
+
'ê': 'é',
|
65 |
+
'å': 'ó',
|
66 |
+
'î': 'i',
|
67 |
+
'ñ': 'ň',
|
68 |
+
'ç': 's',
|
69 |
+
'ż': 'ž',
|
70 |
+
'ł': 'w',
|
71 |
+
'ć': 'č',
|
72 |
+
'þ': 't',
|
73 |
+
'ß': 'ss',
|
74 |
+
'ę': 'en',
|
75 |
+
'ą': 'an',
|
76 |
+
'æ': 'ae',
|
77 |
+
}
|
78 |
+
|
79 |
+
def replace_chars(sentence):
|
80 |
+
result = ''
|
81 |
+
for ch in sentence:
|
82 |
+
new = CHARS[ch] if ch in CHARS else ch
|
83 |
+
result += new
|
84 |
+
|
85 |
+
return result
|
86 |
+
|
87 |
+
chars_to_ignore_regex = '[\,\?\.\!\-\;\:\/\"\“\„\%\”\�\–\'\`\«\»\—\’\…\³]'
|
88 |
+
|
89 |
+
text = text.lower()
|
90 |
+
# normalize non-standard (stylized) unicode characters
|
91 |
+
text = unicodedata.normalize('NFKC', text)
|
92 |
+
# remove punctuation
|
93 |
+
text = re.sub(chars_to_ignore_regex, "", text)
|
94 |
+
batch["sentence"] = replace_chars(batch['sentence'])
|
95 |
+
|
96 |
+
# Let's also make sure we split on all kinds of newlines, spaces, etc...
|
97 |
+
text = " ".join(text.split())
|
98 |
+
|
99 |
+
return text
|
100 |
+
|
101 |
+
|
102 |
+
def main(args):
|
103 |
+
# load dataset
|
104 |
+
dataset = load_dataset(args.dataset, args.config, split=args.split, use_auth_token=True)
|
105 |
+
|
106 |
+
# for testing: only process the first two examples as a test
|
107 |
+
# dataset = dataset.select(range(10))
|
108 |
+
|
109 |
+
# load processor
|
110 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(args.model_id)
|
111 |
+
sampling_rate = feature_extractor.sampling_rate
|
112 |
+
|
113 |
+
# resample audio
|
114 |
+
dataset = dataset.cast_column("audio", Audio(sampling_rate=sampling_rate))
|
115 |
+
|
116 |
+
# load eval pipeline
|
117 |
+
asr = pipeline("automatic-speech-recognition", model=args.model_id)
|
118 |
+
|
119 |
+
# map function to decode audio
|
120 |
+
def map_to_pred(batch):
|
121 |
+
prediction = asr(batch["audio"]["array"], chunk_length_s=args.chunk_length_s, stride_length_s=args.stride_length_s)
|
122 |
+
|
123 |
+
batch["prediction"] = prediction["text"]
|
124 |
+
batch["target"] = normalize_text(batch["sentence"])
|
125 |
+
return batch
|
126 |
+
|
127 |
+
# run inference on all examples
|
128 |
+
result = dataset.map(map_to_pred, remove_columns=dataset.column_names)
|
129 |
+
|
130 |
+
# compute and log_results
|
131 |
+
# do not change function below
|
132 |
+
log_results(result, args)
|
133 |
+
|
134 |
+
|
135 |
+
if __name__ == "__main__":
|
136 |
+
parser = argparse.ArgumentParser()
|
137 |
+
|
138 |
+
parser.add_argument(
|
139 |
+
"--model_id", type=str, required=True, help="Model identifier. Should be loadable with 🤗 Transformers"
|
140 |
+
)
|
141 |
+
parser.add_argument(
|
142 |
+
"--dataset", type=str, required=True, help="Dataset name to evaluate the `model_id`. Should be loadable with 🤗 Datasets"
|
143 |
+
)
|
144 |
+
parser.add_argument(
|
145 |
+
"--config", type=str, required=True, help="Config of the dataset. *E.g.* `'en'` for Common Voice"
|
146 |
+
)
|
147 |
+
parser.add_argument(
|
148 |
+
"--split", type=str, required=True, help="Split of the dataset. *E.g.* `'test'`"
|
149 |
+
)
|
150 |
+
parser.add_argument(
|
151 |
+
"--chunk_length_s", type=float, default=None, help="Chunk length in seconds. Defaults to None. For long audio files a good value would be 5.0 seconds."
|
152 |
+
)
|
153 |
+
parser.add_argument(
|
154 |
+
"--stride_length_s", type=float, default=None, help="Stride of the audio chunks. Defaults to None. For long audio files a good value would be 1.0 seconds."
|
155 |
+
)
|
156 |
+
parser.add_argument(
|
157 |
+
"--log_outputs", action='store_true', help="If defined, write outputs to log file for analysis."
|
158 |
+
)
|
159 |
+
args = parser.parse_args()
|
160 |
+
|
161 |
+
main(args)
|