Update README.md
Browse files
README.md
CHANGED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language: mt
|
| 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 Maltese by Akash PB
|
| 13 |
+
results:
|
| 14 |
+
- task:
|
| 15 |
+
name: Speech Recognition
|
| 16 |
+
type: automatic-speech-recognition
|
| 17 |
+
dataset:
|
| 18 |
+
name: Common Voice mt
|
| 19 |
+
type: common_voice
|
| 20 |
+
args: {lang_id}
|
| 21 |
+
metrics:
|
| 22 |
+
- name: Test WER
|
| 23 |
+
type: wer
|
| 24 |
+
value: 32.77
|
| 25 |
+
---
|
| 26 |
+
# Wav2Vec2-Large-XLSR-53-Maltese
|
| 27 |
+
Fine-tuned [facebook/wav2vec2-large-xlsr-53](https://huggingface.co/facebook/wav2vec2-large-xlsr-53) in Maltese using the [Common Voice](https://huggingface.co/datasets/common_voice)
|
| 28 |
+
When using this model, make sure that your speech input is sampled at 16kHz.
|
| 29 |
+
## Usage
|
| 30 |
+
The model can be used directly (without a language model) as follows:
|
| 31 |
+
```python
|
| 32 |
+
import torch
|
| 33 |
+
import torchaudio
|
| 34 |
+
from datasets import load_dataset
|
| 35 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
| 36 |
+
test_dataset = load_dataset("common_voice", "tr", split="test[:2%]").
|
| 37 |
+
processor = Wav2Vec2Processor.from_pretrained("akashpb13/wav2vec2-large-xlsr-Maltese")
|
| 38 |
+
model = Wav2Vec2ForCTC.from_pretrained("akashpb13/wav2vec2-large-xlsr-Maltese")
|
| 39 |
+
resampler = torchaudio.transforms.Resample(48_000, 16_000)
|
| 40 |
+
# Preprocessing the datasets.
|
| 41 |
+
# We need to read the aduio files as arrays
|
| 42 |
+
def speech_file_to_array_fn(batch):
|
| 43 |
+
speech_array, sampling_rate = torchaudio.load(batch["path"])
|
| 44 |
+
batch["speech"] = resampler(speech_array).squeeze().numpy()
|
| 45 |
+
return batch
|
| 46 |
+
test_dataset = test_dataset.map(speech_file_to_array_fn)
|
| 47 |
+
inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
|
| 48 |
+
with torch.no_grad():
|
| 49 |
+
logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
|
| 50 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
| 51 |
+
print("Prediction:", processor.batch_decode(predicted_ids))
|
| 52 |
+
print("Reference:", test_dataset["sentence"][:2])
|
| 53 |
+
```
|
| 54 |
+
## Evaluation
|
| 55 |
+
The model can be evaluated as follows on the {language} test data of Common Voice.
|
| 56 |
+
```python
|
| 57 |
+
import torch
|
| 58 |
+
import torchaudio
|
| 59 |
+
from datasets import load_dataset, load_metric
|
| 60 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
| 61 |
+
import re
|
| 62 |
+
test_dataset = load_dataset("common_voice", "mt", split="test")
|
| 63 |
+
wer = load_metric("wer")
|
| 64 |
+
processor = Wav2Vec2Processor.from_pretrained("patrickvonplaten/wav2vec2-large-xlsr-Maltese-demo")
|
| 65 |
+
model = Wav2Vec2ForCTC.from_pretrained("patrickvonplaten/wav2vec2-large-xlsr-Maltese-demo")
|
| 66 |
+
model.to("cuda")
|
| 67 |
+
chars_to_ignore_regex = '[\,\?\.\!\-\;\:\"\“]'
|
| 68 |
+
resampler = torchaudio.transforms.Resample(48_000, 16_000)
|
| 69 |
+
# Preprocessing the datasets.
|
| 70 |
+
# We need to read the aduio files as arrays
|
| 71 |
+
def speech_file_to_array_fn(batch):
|
| 72 |
+
batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
|
| 73 |
+
speech_array, sampling_rate = torchaudio.load(batch["path"])
|
| 74 |
+
batch["speech"] = resampler(speech_array).squeeze().numpy()
|
| 75 |
+
return batch
|
| 76 |
+
test_dataset = test_dataset.map(speech_file_to_array_fn)
|
| 77 |
+
# Preprocessing the datasets.
|
| 78 |
+
# We need to read the aduio files as arrays
|
| 79 |
+
def evaluate(batch):
|
| 80 |
+
inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
|
| 81 |
+
with torch.no_grad():
|
| 82 |
+
logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
|
| 83 |
+
pred_ids = torch.argmax(logits, dim=-1)
|
| 84 |
+
batch["pred_strings"] = processor.batch_decode(pred_ids)
|
| 85 |
+
return batch
|
| 86 |
+
result = test_dataset.map(evaluate, batched=True, batch_size=8)
|
| 87 |
+
print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"], references=result["sentence"])))
|
| 88 |
+
```
|
| 89 |
+
**Test Result**: 32.77 %
|