patrickvonplaten
commited on
Commit
•
e8cc4d8
1
Parent(s):
38e8348
Update README.md
Browse files
README.md
CHANGED
@@ -1,26 +1,124 @@
|
|
1 |
-
|
2 |
---
|
3 |
-
|
4 |
language: es
|
5 |
-
|
6 |
datasets:
|
7 |
-
|
8 |
- common_voice
|
9 |
-
|
10 |
tags:
|
11 |
-
|
|
|
12 |
- speech
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
-
|
|
|
|
|
15 |
|
16 |
-
|
17 |
|
18 |
-
|
19 |
|
20 |
-
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
|
23 |
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
|
|
|
|
1 |
---
|
|
|
2 |
language: es
|
|
|
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 Spanish Manuel Romero
|
13 |
+
results:
|
14 |
+
- task:
|
15 |
+
name: Speech Recognition
|
16 |
+
type: automatic-speech-recognition
|
17 |
+
dataset:
|
18 |
+
name: Common Voice es
|
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-Spanish
|
28 |
+
Fine-tuned [facebook/wav2vec2-large-xlsr-53](https://huggingface.co/facebook/wav2vec2-large-xlsr-53) in Spanish 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 |
+
import torch
|
37 |
+
import torchaudio
|
38 |
+
from datasets import load_dataset
|
39 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
40 |
|
41 |
+
test_dataset = load_dataset("common_voice", "es, split="test[:2%]").
|
42 |
|
43 |
+
processor = Wav2Vec2Processor.from_pretrained("mrm8488/wav2vec2-large-xlsr-53-spanish")
|
44 |
+
model = Wav2Vec2ForCTC.from_pretrained("mrm8488/wav2vec2-large-xlsr-53-spanish")
|
45 |
+
|
46 |
+
resampler = torchaudio.transforms.Resample(48_000, 16_000)
|
47 |
+
|
48 |
+
# Preprocessing the datasets.
|
49 |
+
# We need to read the aduio files as arrays
|
50 |
+
def speech_file_to_array_fn(batch):
|
51 |
+
speech_array, sampling_rate = torchaudio.load(batch["path"])
|
52 |
+
batch["speech"] = resampler(speech_array).squeeze().numpy()
|
53 |
+
return batch
|
54 |
+
|
55 |
+
test_dataset = test_dataset.map(speech_file_to_array_fn)
|
56 |
+
inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
|
57 |
+
|
58 |
+
with torch.no_grad():
|
59 |
+
logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
|
60 |
+
|
61 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
62 |
+
|
63 |
+
print("Prediction:", processor.batch_decode(predicted_ids))
|
64 |
+
print("Reference:", test_dataset["sentence"][:2])
|
65 |
+
```
|
66 |
+
|
67 |
+
|
68 |
+
## Evaluation
|
69 |
+
|
70 |
+
The model can be evaluated as follows on the Spanish test data of Common Voice.
|
71 |
+
|
72 |
+
|
73 |
+
```python
|
74 |
+
import torch
|
75 |
+
import torchaudio
|
76 |
+
from datasets import load_dataset, load_metric
|
77 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
78 |
+
import re
|
79 |
+
|
80 |
+
test_dataset = load_dataset("common_voice", "es", split="test")
|
81 |
+
wer = load_metric("wer")
|
82 |
+
|
83 |
+
processor = Wav2Vec2Processor.from_pretrained("mrm8488/wav2vec2-large-xlsr-53-spanish")
|
84 |
+
model = Wav2Vec2ForCTC.from_pretrained("mrm8488/wav2vec2-large-xlsr-53-spanish")
|
85 |
+
model.to("cuda")
|
86 |
+
|
87 |
+
chars_to_ignore_regex = '[\,\?\.\!\-\;\:\"\“]'
|
88 |
+
resampler = torchaudio.transforms.Resample(48_000, 16_000)
|
89 |
+
|
90 |
+
# Preprocessing the datasets.
|
91 |
+
# We need to read the aduio files as arrays
|
92 |
+
def speech_file_to_array_fn(batch):
|
93 |
+
batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
|
94 |
+
speech_array, sampling_rate = torchaudio.load(batch["path"])
|
95 |
+
batch["speech"] = resampler(speech_array).squeeze().numpy()
|
96 |
+
return batch
|
97 |
+
|
98 |
+
test_dataset = test_dataset.map(speech_file_to_array_fn)
|
99 |
+
|
100 |
+
# Preprocessing the datasets.
|
101 |
+
# We need to read the aduio files as arrays
|
102 |
+
def evaluate(batch):
|
103 |
+
inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
|
104 |
+
|
105 |
+
with torch.no_grad():
|
106 |
+
logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
|
107 |
+
|
108 |
+
pred_ids = torch.argmax(logits, dim=-1)
|
109 |
+
batch["pred_strings"] = processor.batch_decode(pred_ids)
|
110 |
+
return batch
|
111 |
+
|
112 |
+
result = test_dataset.map(evaluate, batched=True, batch_size=8)
|
113 |
+
|
114 |
+
print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"], references=result["sentence"])))
|
115 |
+
```
|
116 |
+
|
117 |
+
**Test Result**: ??? %
|
118 |
+
|
119 |
+
|
120 |
+
## Training
|
121 |
+
|
122 |
+
The Common Voice `train`, `validation` datasets were used for training.
|
123 |
|
124 |
+
The script used for training can be found ???
|