maxidl
commited on
Commit
·
5765894
1
Parent(s):
1f39a71
update model card
Browse files
README.md
CHANGED
@@ -0,0 +1,197 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
language: de
|
3 |
+
datasets:
|
4 |
+
- common_voice
|
5 |
+
metrics:
|
6 |
+
- wer
|
7 |
+
tags:
|
8 |
+
- audio
|
9 |
+
- automatic-speech-recognition
|
10 |
+
- speech
|
11 |
+
- xlsr-fine-tuning-week
|
12 |
+
license: apache-2.0
|
13 |
+
model-index:
|
14 |
+
- name: {XLSR Wav2Vec2 Large 53 CV-de}
|
15 |
+
results:
|
16 |
+
- task:
|
17 |
+
name: Speech Recognition
|
18 |
+
type: automatic-speech-recognition
|
19 |
+
dataset:
|
20 |
+
name: Common Voice de
|
21 |
+
type: common_voice
|
22 |
+
args: de
|
23 |
+
metrics:
|
24 |
+
- name: Test WER
|
25 |
+
type: wer
|
26 |
+
value: 12.62
|
27 |
+
---
|
28 |
+
|
29 |
+
# Wav2Vec2-Large-XLSR-53-german #TODO: replace language with your {language}, *e.g.* French
|
30 |
+
|
31 |
+
Fine-tuned [facebook/wav2vec2-large-xlsr-53](https://huggingface.co/facebook/wav2vec2-large-xlsr-53) on German using the [Common Voice](https://huggingface.co/datasets/common_voice) dataset.
|
32 |
+
When using this model, make sure that your speech input is sampled at 16kHz.
|
33 |
+
|
34 |
+
## Usage
|
35 |
+
|
36 |
+
The model can be used directly (without a language model) as follows:
|
37 |
+
|
38 |
+
```python
|
39 |
+
import torch
|
40 |
+
import torchaudio
|
41 |
+
from datasets import load_dataset
|
42 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
43 |
+
|
44 |
+
test_dataset = load_dataset("common_voice", "de", split="test[:8]") # use a batch of 8 for demo purposes
|
45 |
+
|
46 |
+
processor = Wav2Vec2Processor.from_pretrained("maxidl/wav2vec2-large-xlsr-german")
|
47 |
+
model = Wav2Vec2ForCTC.from_pretrained("maxidl/wav2vec2-large-xlsr-german")
|
48 |
+
|
49 |
+
resampler = torchaudio.transforms.Resample(48_000, 16_000)
|
50 |
+
|
51 |
+
"""
|
52 |
+
Preprocessing the dataset by:
|
53 |
+
- loading audio files
|
54 |
+
- resampling to 16kHz
|
55 |
+
- converting to array
|
56 |
+
- prepare input tensor using the processor
|
57 |
+
"""
|
58 |
+
def speech_file_to_array_fn(batch):
|
59 |
+
speech_array, sampling_rate = torchaudio.load(batch["path"])
|
60 |
+
batch["speech"] = resampler(speech_array).squeeze().numpy()
|
61 |
+
return batch
|
62 |
+
|
63 |
+
test_dataset = test_dataset.map(speech_file_to_array_fn)
|
64 |
+
inputs = processor(test_dataset["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
|
65 |
+
|
66 |
+
# run forward
|
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["sentence"])
|
74 |
+
"""
|
75 |
+
Example Result:
|
76 |
+
|
77 |
+
Prediction: [
|
78 |
+
'zieh durch bittet draußen die schuhe aus',
|
79 |
+
'es kommt zugvorgebauten fo',
|
80 |
+
'ihre vorterstrecken erschienen it modemagazinen wie der voge karpes basar mariclair',
|
81 |
+
'fürliepert eine auch für manachen ungewöhnlich lange drittelliste',
|
82 |
+
'er wurde zu ehren des reichskanzlers otto von bismarck errichtet',
|
83 |
+
'was solls ich bin bereit',
|
84 |
+
'das internet besteht aus vielen computern die miteinander verbunden sind',
|
85 |
+
'der uranus ist der siebinteplanet in unserem sonnensystem s'
|
86 |
+
]
|
87 |
+
|
88 |
+
Reference: [
|
89 |
+
'Zieht euch bitte draußen die Schuhe aus.',
|
90 |
+
'Es kommt zum Showdown in Gstaad.',
|
91 |
+
'Ihre Fotostrecken erschienen in Modemagazinen wie der Vogue, Harper’s Bazaar und Marie Claire.',
|
92 |
+
'Felipe hat eine auch für Monarchen ungewöhnlich lange Titelliste.',
|
93 |
+
'Er wurde zu Ehren des Reichskanzlers Otto von Bismarck errichtet.',
|
94 |
+
'Was solls, ich bin bereit.',
|
95 |
+
'Das Internet besteht aus vielen Computern, die miteinander verbunden sind.',
|
96 |
+
'Der Uranus ist der siebente Planet in unserem Sonnensystem.'
|
97 |
+
]
|
98 |
+
"""
|
99 |
+
```
|
100 |
+
|
101 |
+
|
102 |
+
## Evaluation
|
103 |
+
|
104 |
+
The model can be evaluated as follows on the German test data of Common Voice:
|
105 |
+
|
106 |
+
|
107 |
+
```python
|
108 |
+
import re
|
109 |
+
import torch
|
110 |
+
import torchaudio
|
111 |
+
from datasets import load_dataset, load_metric
|
112 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
113 |
+
|
114 |
+
"""
|
115 |
+
Evaluation on the full test set:
|
116 |
+
- takes ~20mins (RTX 3090).
|
117 |
+
- requires ~170GB RAM to compute the WER. A potential solution to this is computing it in chunks.
|
118 |
+
See https://discuss.huggingface.co/t/spanish-asr-fine-tuning-wav2vec2/4586/5 on how to implement this.
|
119 |
+
"""
|
120 |
+
test_dataset = load_dataset("common_voice", "de", split="test") # use "test[:1%]" for 1% sample
|
121 |
+
wer = load_metric("wer")
|
122 |
+
|
123 |
+
processor = Wav2Vec2Processor.from_pretrained("maxidl/wav2vec2-large-xlsr-german")
|
124 |
+
model = Wav2Vec2ForCTC.from_pretrained("maxidl/wav2vec2-large-xlsr-german")
|
125 |
+
model.to("cuda")
|
126 |
+
|
127 |
+
chars_to_ignore_regex = '[\,\?\.\!\-\;\:\"\“]'
|
128 |
+
resampler = torchaudio.transforms.Resample(48_000, 16_000)
|
129 |
+
|
130 |
+
# Preprocessing the datasets.
|
131 |
+
# We need to read the aduio files as arrays
|
132 |
+
def speech_file_to_array_fn(batch):
|
133 |
+
batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
|
134 |
+
speech_array, sampling_rate = torchaudio.load(batch["path"])
|
135 |
+
batch["speech"] = resampler(speech_array).squeeze().numpy()
|
136 |
+
return batch
|
137 |
+
|
138 |
+
test_dataset = test_dataset.map(speech_file_to_array_fn)
|
139 |
+
|
140 |
+
# Preprocessing the datasets.
|
141 |
+
# We need to read the audio files as arrays
|
142 |
+
def evaluate(batch):
|
143 |
+
inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
|
144 |
+
|
145 |
+
with torch.no_grad():
|
146 |
+
logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
|
147 |
+
|
148 |
+
pred_ids = torch.argmax(logits, dim=-1)
|
149 |
+
batch["pred_strings"] = processor.batch_decode(pred_ids)
|
150 |
+
return batch
|
151 |
+
|
152 |
+
result = test_dataset.map(evaluate, batched=True, batch_size=8) # batch_size=8 -> requires ~14.5GB GPU memory
|
153 |
+
|
154 |
+
print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"], references=result["sentence"])))
|
155 |
+
# WER: 12.615308
|
156 |
+
```
|
157 |
+
|
158 |
+
**Test Result**: 12.62 %
|
159 |
+
|
160 |
+
|
161 |
+
## Training
|
162 |
+
|
163 |
+
The Common Voice German `train` and `validation` were used for training.
|
164 |
+
The script used for training can be found [here](https://github.com/maxidl/wav2vec2).
|
165 |
+
The model was trained for 50k steps, taking around 30 hours on a single A100.
|
166 |
+
|
167 |
+
The arguments used for training this model are:
|
168 |
+
```
|
169 |
+
python run_finetuning.py \
|
170 |
+
--model_name_or_path="facebook/wav2vec2-large-xlsr-53" \
|
171 |
+
--dataset_config_name="de" \
|
172 |
+
--output_dir=./wav2vec2-large-xlsr-german \
|
173 |
+
--preprocessing_num_workers="16" \
|
174 |
+
--overwrite_output_dir \
|
175 |
+
--num_train_epochs="20" \
|
176 |
+
--per_device_train_batch_size="64" \
|
177 |
+
--per_device_eval_batch_size="32" \
|
178 |
+
--learning_rate="1e-4" \
|
179 |
+
--warmup_steps="500" \
|
180 |
+
--evaluation_strategy="steps" \
|
181 |
+
--save_steps="5000" \
|
182 |
+
--eval_steps="5000" \
|
183 |
+
--logging_steps="1000" \
|
184 |
+
--save_total_limit="3" \
|
185 |
+
--freeze_feature_extractor \
|
186 |
+
--activation_dropout="0.055" \
|
187 |
+
--attention_dropout="0.094" \
|
188 |
+
--feat_proj_dropout="0.04" \
|
189 |
+
--layerdrop="0.04" \
|
190 |
+
--mask_time_prob="0.08" \
|
191 |
+
--gradient_checkpointing="1" \
|
192 |
+
--fp16 \
|
193 |
+
--do_train \
|
194 |
+
--do_eval \
|
195 |
+
--dataloader_num_workers="16" \
|
196 |
+
--group_by_length
|
197 |
+
```
|