Commit
·
8060b51
1
Parent(s):
366f001
Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
language: eo
|
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: Wav2Vec2 Large 53 Esperanto by Gunjan Chhablani
|
15 |
+
results:
|
16 |
+
- task:
|
17 |
+
name: Speech Recognition
|
18 |
+
type: automatic-speech-recognition
|
19 |
+
dataset:
|
20 |
+
name: Common Voice eo
|
21 |
+
type: common_voice
|
22 |
+
args: eo
|
23 |
+
metrics:
|
24 |
+
- name: Test WER
|
25 |
+
type: wer
|
26 |
+
value: 9.92
|
27 |
+
---
|
28 |
+
|
29 |
+
# Wav2Vec2-Large-XLSR-53-Esperanto
|
30 |
+
|
31 |
+
Fine-tuned [facebook/wav2vec2-large-xlsr-53](https://huggingface.co/facebook/wav2vec2-large-xlsr-53) on Esperanto 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", "eo", split="test[:2%]")
|
45 |
+
|
46 |
+
processor = Wav2Vec2Processor.from_pretrained('gchhablani/wav2vec2-large-xlsr-eo')
|
47 |
+
model = Wav2Vec2ForCTC.from_pretrained('gchhablani/wav2vec2-large-xlsr-eo')
|
48 |
+
|
49 |
+
|
50 |
+
resampler = torchaudio.transforms.Resample(48_000, 16_000)
|
51 |
+
|
52 |
+
# Preprocessing the datasets.
|
53 |
+
# We need to read the aduio files as arrays
|
54 |
+
def speech_file_to_array_fn(batch):
|
55 |
+
speech_array, sampling_rate = torchaudio.load(batch["path"])
|
56 |
+
batch["speech"] = resampler(speech_array).squeeze().numpy()
|
57 |
+
return batch
|
58 |
+
|
59 |
+
test_dataset = test_dataset.map(speech_file_to_array_fn)
|
60 |
+
inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
|
61 |
+
|
62 |
+
with torch.no_grad():
|
63 |
+
logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
|
64 |
+
|
65 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
66 |
+
|
67 |
+
print("Prediction:", processor.batch_decode(predicted_ids))
|
68 |
+
print("Reference:", test_dataset["sentence"][:2])
|
69 |
+
```
|
70 |
+
|
71 |
+
|
72 |
+
## Evaluation
|
73 |
+
|
74 |
+
The model can be evaluated as follows on the Portuguese test data of Common Voice.
|
75 |
+
|
76 |
+
|
77 |
+
```python
|
78 |
+
import torch
|
79 |
+
import torchaudio
|
80 |
+
from datasets import load_dataset, load_metric
|
81 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
82 |
+
import re
|
83 |
+
|
84 |
+
import jiwer
|
85 |
+
|
86 |
+
def chunked_wer(targets, predictions, chunk_size=None):
|
87 |
+
if chunk_size is None: return jiwer.wer(targets, predictions)
|
88 |
+
start = 0
|
89 |
+
end = chunk_size
|
90 |
+
H, S, D, I = 0, 0, 0, 0
|
91 |
+
while start < len(targets):
|
92 |
+
chunk_metrics = jiwer.compute_measures(targets[start:end], predictions[start:end])
|
93 |
+
H = H + chunk_metrics["hits"]
|
94 |
+
S = S + chunk_metrics["substitutions"]
|
95 |
+
D = D + chunk_metrics["deletions"]
|
96 |
+
I = I + chunk_metrics["insertions"]
|
97 |
+
start += chunk_size
|
98 |
+
end += chunk_size
|
99 |
+
return float(S + D + I) / float(H + S + D)
|
100 |
+
|
101 |
+
test_dataset = load_dataset("common_voice", "eo", split="test") #TODO: replace {lang_id} in your language code here. Make sure the code is one of the *ISO codes* of [this](https://huggingface.co/languages) site.
|
102 |
+
wer = load_metric("wer")
|
103 |
+
|
104 |
+
processor = Wav2Vec2Processor.from_pretrained('gchhablani/wav2vec2-large-xlsr-eo')
|
105 |
+
model = Wav2Vec2ForCTC.from_pretrained('gchhablani/wav2vec2-large-xlsr-eo')
|
106 |
+
|
107 |
+
chars_to_ignore_regex = '[\,\?\.\!\-\;\:\"\“\%\‘\”\�\„\«\(\»\)\’\']'
|
108 |
+
resampler = torchaudio.transforms.Resample(48_000, 16_000)
|
109 |
+
|
110 |
+
# Preprocessing the datasets.
|
111 |
+
# We need to read the aduio files as arrays
|
112 |
+
def speech_file_to_array_fn(batch):
|
113 |
+
batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower().replace('—',' ').replace('–',' ')
|
114 |
+
speech_array, sampling_rate = torchaudio.load(batch["path"])
|
115 |
+
batch["speech"] = resampler(speech_array).squeeze().numpy()
|
116 |
+
return batch
|
117 |
+
|
118 |
+
test_dataset = test_dataset.map(speech_file_to_array_fn)
|
119 |
+
|
120 |
+
# Preprocessing the datasets.
|
121 |
+
# We need to read the aduio files as arrays
|
122 |
+
def evaluate(batch):
|
123 |
+
inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
|
124 |
+
|
125 |
+
with torch.no_grad():
|
126 |
+
logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
|
127 |
+
|
128 |
+
pred_ids = torch.argmax(logits, dim=-1)
|
129 |
+
batch["pred_strings"] = processor.batch_decode(pred_ids)
|
130 |
+
return batch
|
131 |
+
|
132 |
+
result = test_dataset.map(evaluate, batched=True, batch_size=8)
|
133 |
+
|
134 |
+
print("WER: {:2f}".format(100 * chunked_wer(predictions=result["pred_strings"], targets=result["sentence"],chunk_size=5000)))
|
135 |
+
```
|
136 |
+
|
137 |
+
**Test Result**: 9.92 %
|
138 |
+
|
139 |
+
## Training
|
140 |
+
|
141 |
+
The Common Voice `train` and `validation` datasets were used for training. The code can be found [here](https://github.com/gchhablani/wav2vec2-week/blob/main/fine-tune-xlsr-wav2vec2-on-esperanto-asr-with-transformers-final.ipynb).
|