Add README.md
Browse files
README.md
ADDED
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
language: lg
|
3 |
+
datasets:
|
4 |
+
- common_voice
|
5 |
+
metrics:
|
6 |
+
- wer
|
7 |
+
tags:
|
8 |
+
- audio
|
9 |
+
- automatic-speech-recognition
|
10 |
+
- speech
|
11 |
+
license: apache-2.0
|
12 |
+
model-index:
|
13 |
+
- name: Wav2Vec2 Luganda
|
14 |
+
results:
|
15 |
+
- task:
|
16 |
+
name: Speech Recognition
|
17 |
+
type: automatic-speech-recognition
|
18 |
+
dataset:
|
19 |
+
name: Common Voice lg
|
20 |
+
type: common_voice
|
21 |
+
args: lg
|
22 |
+
metrics:
|
23 |
+
- name: Test WER
|
24 |
+
type: wer
|
25 |
+
value: 7.53
|
26 |
+
---
|
27 |
+
|
28 |
+
# Automatic Speech Recognition for Luganda
|
29 |
+
|
30 |
+
This is the model built for the
|
31 |
+
[Mozilla Luganda Automatic Speech Recognition competition](https://zindi.africa/competitions/mozilla-luganda-automatic-speech-recognition).
|
32 |
+
It is a fine-tuned [facebook/wav2vec2-large-xlsr-53](https://huggingface.co/facebook/wav2vec2-large-xlsr-53)
|
33 |
+
model on the [Luganda Common Voice dataset](https://huggingface.co/datasets/common_voice) version 7.0.
|
34 |
+
|
35 |
+
We also provide a [live demo](https://huggingface.co/spaces/indonesian-nlp/luganda-asr) to test the model.
|
36 |
+
|
37 |
+
When using this model, make sure that your speech input is sampled at 16kHz.
|
38 |
+
|
39 |
+
## Usage
|
40 |
+
The model can be used directly (without a language model) as follows:
|
41 |
+
```python
|
42 |
+
import torch
|
43 |
+
import torchaudio
|
44 |
+
from datasets import load_dataset
|
45 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
46 |
+
|
47 |
+
test_dataset = load_dataset("common_voice", "lg", split="test[:2%]")
|
48 |
+
|
49 |
+
processor = Wav2Vec2Processor.from_pretrained("indonesian-nlp/wav2vec2-luganda")
|
50 |
+
model = Wav2Vec2ForCTC.from_pretrained("indonesian-nlp/wav2vec2-luganda")
|
51 |
+
|
52 |
+
resampler = torchaudio.transforms.Resample(48_000, 16_000)
|
53 |
+
|
54 |
+
# Preprocessing the datasets.
|
55 |
+
# We need to read the aduio files as arrays
|
56 |
+
def speech_file_to_array_fn(batch):
|
57 |
+
speech_array, sampling_rate = torchaudio.load(batch["path"])
|
58 |
+
batch["speech"] = resampler(speech_array).squeeze().numpy()
|
59 |
+
return batch
|
60 |
+
|
61 |
+
test_dataset = test_dataset.map(speech_file_to_array_fn)
|
62 |
+
inputs = processor(test_dataset[:2]["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
|
63 |
+
|
64 |
+
with torch.no_grad():
|
65 |
+
logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
|
66 |
+
|
67 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
68 |
+
|
69 |
+
print("Prediction:", processor.batch_decode(predicted_ids))
|
70 |
+
print("Reference:", test_dataset[:2]["sentence"])
|
71 |
+
```
|
72 |
+
|
73 |
+
|
74 |
+
## Evaluation
|
75 |
+
|
76 |
+
The model can be evaluated as follows on the Indonesian test data of Common Voice.
|
77 |
+
|
78 |
+
```python
|
79 |
+
import torch
|
80 |
+
import torchaudio
|
81 |
+
from datasets import load_dataset, load_metric
|
82 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
83 |
+
import re
|
84 |
+
|
85 |
+
test_dataset = load_dataset("common_voice", "lg", split="test")
|
86 |
+
wer = load_metric("wer")
|
87 |
+
|
88 |
+
processor = Wav2Vec2Processor.from_pretrained("indonesian-nlp/wav2vec2-luganda")
|
89 |
+
model = Wav2Vec2ForCTC.from_pretrained("indonesian-nlp/wav2vec2-luganda")
|
90 |
+
model.to("cuda")
|
91 |
+
|
92 |
+
chars_to_ignore = [",", "?", ".", "!", "-", ";", ":", '""', "%", "'", '"', "�", "‘", "’", "’"]
|
93 |
+
chars_to_ignore_regex = f'[{"".join(chars_to_ignore)}]'
|
94 |
+
|
95 |
+
resampler = torchaudio.transforms.Resample(48_000, 16_000)
|
96 |
+
|
97 |
+
# Preprocessing the datasets.
|
98 |
+
# We need to read the audio files as arrays
|
99 |
+
def speech_file_to_array_fn(batch):
|
100 |
+
batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
|
101 |
+
speech_array, sampling_rate = torchaudio.load(batch["path"])
|
102 |
+
batch["speech"] = resampler(speech_array).squeeze().numpy()
|
103 |
+
return batch
|
104 |
+
|
105 |
+
test_dataset = test_dataset.map(speech_file_to_array_fn)
|
106 |
+
|
107 |
+
# Preprocessing the datasets.
|
108 |
+
# We need to read the audio files as arrays
|
109 |
+
def evaluate(batch):
|
110 |
+
inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
|
111 |
+
|
112 |
+
with torch.no_grad():
|
113 |
+
logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
|
114 |
+
|
115 |
+
pred_ids = torch.argmax(logits, dim=-1)
|
116 |
+
batch["pred_strings"] = processor.batch_decode(pred_ids)
|
117 |
+
return batch
|
118 |
+
|
119 |
+
result = test_dataset.map(evaluate, batched=True, batch_size=8)
|
120 |
+
|
121 |
+
print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"], references=result["sentence"])))
|
122 |
+
```
|
123 |
+
|
124 |
+
**Test Result**: 7.53 %
|
125 |
+
|
126 |
+
## Training
|
127 |
+
|
128 |
+
The Common Voice `train`, `validation`, and ... datasets were used for training as well as ... and ... # TODO
|
129 |
+
|
130 |
+
The script used for training can be found [here](https://github.com/indonesian-nlp/luganda-asr)
|