readme: add initial version
Browse files
README.md
ADDED
@@ -0,0 +1,164 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
language: eu
|
3 |
+
datasets:
|
4 |
+
- common_voice
|
5 |
+
tags:
|
6 |
+
- audio
|
7 |
+
- automatic-speech-recognition
|
8 |
+
- speech
|
9 |
+
- xlsr-fine-tuning-week
|
10 |
+
license: apache-2.0
|
11 |
+
model-index:
|
12 |
+
- name: XLSR Wav2Vec2 Basque Stefan Schweter
|
13 |
+
results:
|
14 |
+
- task:
|
15 |
+
name: Speech Recognition
|
16 |
+
type: automatic-speech-recognition
|
17 |
+
dataset:
|
18 |
+
name: Common Voice eu
|
19 |
+
type: common_voice
|
20 |
+
args: es
|
21 |
+
metrics:
|
22 |
+
- name: Test WER
|
23 |
+
type: wer
|
24 |
+
value: ???
|
25 |
+
---
|
26 |
+
|
27 |
+
# Wav2Vec2-Large-XLSR-53-Basque
|
28 |
+
Fine-tuned [facebook/wav2vec2-large-xlsr-53](https://huggingface.co/facebook/wav2vec2-large-xlsr-53) in Basque using the [Common Voice](https://huggingface.co/datasets/common_voice).
|
29 |
+
When using this model, make sure that your speech input is sampled at 16kHz.
|
30 |
+
|
31 |
+
## Usage
|
32 |
+
|
33 |
+
The model can be used directly (without a language model) as follows:
|
34 |
+
|
35 |
+
```python
|
36 |
+
|
37 |
+
import torch
|
38 |
+
|
39 |
+
import torchaudio
|
40 |
+
|
41 |
+
from datasets import load_dataset
|
42 |
+
|
43 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
44 |
+
|
45 |
+
test_dataset = load_dataset("common_voice", "eu", split="test[:2%]").
|
46 |
+
|
47 |
+
processor = Wav2Vec2Processor.from_pretrained("stefan-it/wav2vec2-large-xlsr-53-basque")
|
48 |
+
|
49 |
+
model = Wav2Vec2ForCTC.from_pretrained("stefan-it/wav2vec2-large-xlsr-53-basque")
|
50 |
+
|
51 |
+
resampler = torchaudio.transforms.Resample(48_000, 16_000)
|
52 |
+
|
53 |
+
# Preprocessing the datasets.
|
54 |
+
|
55 |
+
# We need to read the aduio files as arrays
|
56 |
+
|
57 |
+
def speech_file_to_array_fn(batch):
|
58 |
+
|
59 |
+
speech_array, sampling_rate = torchaudio.load(batch["path"])
|
60 |
+
|
61 |
+
batch["speech"] = resampler(speech_array).squeeze().numpy()
|
62 |
+
|
63 |
+
return batch
|
64 |
+
|
65 |
+
test_dataset = test_dataset.map(speech_file_to_array_fn)
|
66 |
+
|
67 |
+
inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
|
68 |
+
|
69 |
+
with torch.no_grad():
|
70 |
+
|
71 |
+
logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
|
72 |
+
|
73 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
74 |
+
|
75 |
+
print("Prediction:", processor.batch_decode(predicted_ids))
|
76 |
+
|
77 |
+
print("Reference:", test_dataset["sentence"][:2])
|
78 |
+
|
79 |
+
```
|
80 |
+
|
81 |
+
## Evaluation
|
82 |
+
|
83 |
+
The model can be evaluated as follows on the Basque test data of Common Voice.
|
84 |
+
|
85 |
+
```python
|
86 |
+
|
87 |
+
import torch
|
88 |
+
|
89 |
+
import torchaudio
|
90 |
+
|
91 |
+
from datasets import load_dataset, load_metric
|
92 |
+
|
93 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
94 |
+
|
95 |
+
import re
|
96 |
+
|
97 |
+
test_dataset = load_dataset("common_voice", "es", split="test")
|
98 |
+
|
99 |
+
wer = load_metric("wer")
|
100 |
+
|
101 |
+
processor = Wav2Vec2Processor.from_pretrained("stefan-it/wav2vec2-large-xlsr-53-basque")
|
102 |
+
|
103 |
+
model = Wav2Vec2ForCTC.from_pretrained("stefan-it/wav2vec2-large-xlsr-53-basque")
|
104 |
+
|
105 |
+
model.to("cuda")
|
106 |
+
|
107 |
+
chars_to_ignore_regex = '[\,\?\.\!\-\;\:\"\“\%\‘\â€\�]'
|
108 |
+
|
109 |
+
resampler = torchaudio.transforms.Resample(48_000, 16_000)
|
110 |
+
|
111 |
+
# Preprocessing the datasets.
|
112 |
+
|
113 |
+
# We need to read the aduio files as arrays
|
114 |
+
|
115 |
+
def speech_file_to_array_fn(batch):
|
116 |
+
|
117 |
+
batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
|
118 |
+
|
119 |
+
speech_array, sampling_rate = torchaudio.load(batch["path"])
|
120 |
+
|
121 |
+
batch["speech"] = resampler(speech_array).squeeze().numpy()
|
122 |
+
|
123 |
+
return batch
|
124 |
+
|
125 |
+
test_dataset = test_dataset.map(speech_file_to_array_fn)
|
126 |
+
|
127 |
+
# Preprocessing the datasets.
|
128 |
+
|
129 |
+
# We need to read the aduio files as arrays
|
130 |
+
|
131 |
+
def evaluate(batch):
|
132 |
+
|
133 |
+
inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
|
134 |
+
|
135 |
+
with torch.no_grad():
|
136 |
+
|
137 |
+
logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
|
138 |
+
|
139 |
+
pred_ids = torch.argmax(logits, dim=-1)
|
140 |
+
|
141 |
+
batch["pred_strings"] = processor.batch_decode(pred_ids)
|
142 |
+
|
143 |
+
return batch
|
144 |
+
|
145 |
+
result = test_dataset.map(evaluate, batched=True, batch_size=8)
|
146 |
+
|
147 |
+
print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"], references=result["sentence"])))
|
148 |
+
|
149 |
+
```
|
150 |
+
|
151 |
+
**Test Result**: %
|
152 |
+
|
153 |
+
## Training
|
154 |
+
|
155 |
+
The Common Voice `train`, `validation` datasets were used for training.
|
156 |
+
|
157 |
+
The script used for training can be found ???
|
158 |
+
|
159 |
+
## Acknowledgements
|
160 |
+
|
161 |
+
Many thanks to the [OVH team](https://www.ovhcloud.com) for providing access to a V-100 instance. Without their help,
|
162 |
+
fine-tuning would not be possible!
|
163 |
+
|
164 |
+
I would also thank [Manuel Romero](https://github.com/mrm8488) (mrm8488) for helping with the fine-tuning script!
|