Tommi Vehviläinen
commited on
Commit
·
918159b
1
Parent(s):
c5c1bfe
Add README
Browse files
README.md
CHANGED
|
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language: fi
|
| 3 |
+
datasets:
|
| 4 |
+
- common_voice
|
| 5 |
+
- CSS10
|
| 6 |
+
- Finnish parliament session 2
|
| 7 |
+
metrics:
|
| 8 |
+
- wer
|
| 9 |
+
tags:
|
| 10 |
+
- audio
|
| 11 |
+
- automatic-speech-recognition
|
| 12 |
+
- speech
|
| 13 |
+
- xlsr-fine-tuning-week
|
| 14 |
+
license: apache-2.0
|
| 15 |
+
model-index:
|
| 16 |
+
- name: Finnish XLSR Wav2Vec2 Large 53
|
| 17 |
+
results:
|
| 18 |
+
- task:
|
| 19 |
+
name: Speech Recognition
|
| 20 |
+
type: automatic-speech-recognition
|
| 21 |
+
dataset:
|
| 22 |
+
name: Common Voice fi
|
| 23 |
+
type: common_voice
|
| 24 |
+
args: fi
|
| 25 |
+
metrics:
|
| 26 |
+
- name: Test WER
|
| 27 |
+
type: wer
|
| 28 |
+
value: 35.43
|
| 29 |
+
---
|
| 30 |
+
|
| 31 |
+
# Wav2Vec2-Large-XLSR-53-Finnish
|
| 32 |
+
|
| 33 |
+
Fine-tuned [facebook/wav2vec2-large-xlsr-53](https://huggingface.co/facebook/wav2vec2-large-xlsr-53) on Finnish using the [Common Voice](https://huggingface.co/datasets/common_voice), [CSS10](https://www.kaggle.com/bryanpark/finnish-single-speaker-speech-dataset) and [Finnish parliament session 2](https://b2share.eudat.eu/records/4df422d631544ce682d6af1d4714b2d4) datasets.
|
| 34 |
+
|
| 35 |
+
When using this model, make sure that your speech input is sampled at 16kHz.
|
| 36 |
+
|
| 37 |
+
## Usage
|
| 38 |
+
|
| 39 |
+
The model can be used directly (without a language model) as follows:
|
| 40 |
+
|
| 41 |
+
```python
|
| 42 |
+
import numpy as np
|
| 43 |
+
import torch
|
| 44 |
+
import torchaudio
|
| 45 |
+
from datasets import load_dataset
|
| 46 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
| 47 |
+
|
| 48 |
+
test_dataset = load_dataset("common_voice", "fi", split="test[:2%]")
|
| 49 |
+
|
| 50 |
+
processor = Wav2Vec2Processor.from_pretrained("Tommi/wav2vec2-large-xlsr-53-finnish")
|
| 51 |
+
model = Wav2Vec2ForCTC.from_pretrained("Tommi/wav2vec2-large-xlsr-53-finnish")
|
| 52 |
+
|
| 53 |
+
resampler = lambda sr, y: librosa.resample(y.squeeze(), sr, 16_000)
|
| 54 |
+
|
| 55 |
+
# Preprocessing the datasets.
|
| 56 |
+
# We need to read the aduio files as arrays
|
| 57 |
+
def speech_file_to_array_fn(batch):
|
| 58 |
+
speech_array, sampling_rate = torchaudio.load(batch["path"])
|
| 59 |
+
batch["speech"] = resampler(sampling_rate, speech_array.numpy()).squeeze()
|
| 60 |
+
return batch
|
| 61 |
+
|
| 62 |
+
test_dataset = test_dataset.map(speech_file_to_array_fn)
|
| 63 |
+
inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
|
| 64 |
+
|
| 65 |
+
with torch.no_grad():
|
| 66 |
+
logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
|
| 67 |
+
|
| 68 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
| 69 |
+
|
| 70 |
+
print("Prediction:", processor.batch_decode(predicted_ids))
|
| 71 |
+
print("Reference:", test_dataset["sentence"][:2])
|
| 72 |
+
```
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
## Evaluation
|
| 76 |
+
|
| 77 |
+
The model can be evaluated as follows on the Finnish test data of Common Voice.
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
```python
|
| 81 |
+
import librosa
|
| 82 |
+
import torch
|
| 83 |
+
import torchaudio
|
| 84 |
+
from datasets import load_dataset, load_metric
|
| 85 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
| 86 |
+
import re
|
| 87 |
+
|
| 88 |
+
test_dataset = load_dataset("common_voice", "fi", split="test")
|
| 89 |
+
wer = load_metric("wer")
|
| 90 |
+
|
| 91 |
+
processor = Wav2Vec2Processor.from_pretrained("Tommi/wav2vec2-large-xlsr-53-finnish")
|
| 92 |
+
model = Wav2Vec2ForCTC.from_pretrained("Tommi/wav2vec2-large-xlsr-53-finnish")
|
| 93 |
+
model.to("cuda")
|
| 94 |
+
|
| 95 |
+
chars_to_ignore_regex = '[\,\?\.\!\-\;\:\"\"\%\'\"\�\'\...\…\–\é]'
|
| 96 |
+
|
| 97 |
+
resampler = lambda sr, y: librosa.resample(y.numpy().squeeze(), sr, 16_000)
|
| 98 |
+
|
| 99 |
+
# Preprocessing the datasets.
|
| 100 |
+
# We need to read the audio files as arrays
|
| 101 |
+
def speech_file_to_array_fn(batch):
|
| 102 |
+
batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
|
| 103 |
+
speech_array, sampling_rate = torchaudio.load(batch["path"])
|
| 104 |
+
batch["speech"] = resampler(sampling_rate, speech_array).squeeze()
|
| 105 |
+
return batch
|
| 106 |
+
|
| 107 |
+
test_dataset = test_dataset.map(speech_file_to_array_fn)
|
| 108 |
+
|
| 109 |
+
# Preprocessing the datasets.
|
| 110 |
+
# We need to read the audio files as arrays
|
| 111 |
+
def evaluate(batch):
|
| 112 |
+
inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
|
| 113 |
+
|
| 114 |
+
with torch.no_grad():
|
| 115 |
+
logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
|
| 116 |
+
|
| 117 |
+
pred_ids = torch.argmax(logits, dim=-1)
|
| 118 |
+
batch["pred_strings"] = processor.batch_decode(pred_ids)
|
| 119 |
+
|
| 120 |
+
return batch
|
| 121 |
+
|
| 122 |
+
result = test_dataset.map(evaluate, batched=True, batch_size=8)
|
| 123 |
+
|
| 124 |
+
print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"], references=result["sentence"])))
|
| 125 |
+
```
|
| 126 |
+
|
| 127 |
+
**Test Result**: 35.43 %
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
## Training
|
| 131 |
+
|
| 132 |
+
The Common Voice `train`, `validation`, and `other` datasets were used for training as well as CSS10 and Finnish parliament session 2
|
| 133 |
+
|
| 134 |
+
The script used for training can be found [here](...) # TODO: fill in a link to your training script here. If you trained your model in a colab, simply fill in the link here. If you trained the model locally, it would be great if you could upload the training script on github and paste the link here.
|