m3hrdadfi's picture
Update readme for more info
72cabd7
|
raw
history blame
11 kB
---
language: ka
datasets:
- common_voice
tags:
- audio
- automatic-speech-recognition
- speech
- xlsr-fine-tuning-week
license: apache-2.0
widget:
- label: Common Voice sample 566
src: https://huggingface.co/m3hrdadfi/wav2vec2-large-xlsr-georgian/resolve/main/sample566.flac
- label: Common Voice sample 95
src: https://huggingface.co/m3hrdadfi/wav2vec2-large-xlsr-georgian/resolve/main/sample95.flac
model-index:
- name: XLSR Wav2Vec2 Georgian by Mehrdad Farahani
results:
- task:
name: Speech Recognition
type: automatic-speech-recognition
dataset:
name: Common Voice ka
type: common_voice
args: ka
metrics:
- name: Test WER
type: wer
value: 54.00
---
# Wav2Vec2-Large-XLSR-53-Georgian
Fine-tuned [facebook/wav2vec2-large-xlsr-53](https://huggingface.co/facebook/wav2vec2-large-xlsr-53) in Georgian using [Common Voice](https://huggingface.co/datasets/common_voice). When using this model, make sure that your speech input is sampled at 16kHz.
## Usage
The model can be used directly (without a language model) as follows:
**Requirements**
```bash
# requirement packages
!pip install git+https://github.com/huggingface/datasets.git
!pip install git+https://github.com/huggingface/transformers.git
!pip install torchaudio
!pip install librosa
!pip install jiwer
```
**Prediction**
```python
import librosa
import torch
import torchaudio
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
from datasets import load_dataset
import numpy as np
import re
import string
import IPython.display as ipd
chars_to_ignore = [
",", "?", ".", "!", "-", ";", ":", '""', "%", "'", '"', "๏ฟฝ",
"#", "!", "?", "ยซ", "ยป", "(", ")", "ุ›", ",", "?", ".", "!", "-", ";", ":", '"',
"โ€œ", "%", "โ€˜", "๏ฟฝ", "โ€“", "โ€ฆ", "_", "โ€", 'โ€œ', 'โ€ž'
]
chars_to_mapping = {
"\u200c": " ", "\u200d": " ", "\u200e": " ", "\u200f": " ", "\ufeff": " ",
}
def multiple_replace(text, chars_to_mapping):
pattern = "|".join(map(re.escape, chars_to_mapping.keys()))
return re.sub(pattern, lambda m: chars_to_mapping[m.group()], str(text))
def remove_special_characters(text, chars_to_ignore_regex):
text = re.sub(chars_to_ignore_regex, '', text).lower() + " "
return text
def normalizer(batch, chars_to_ignore, chars_to_mapping):
chars_to_ignore_regex = f"""[{"".join(chars_to_ignore)}]"""
text = batch["sentence"].lower().strip()
text = multiple_replace(text, chars_to_mapping)
text = remove_special_characters(text, chars_to_ignore_regex)
batch["sentence"] = text
return batch
def speech_file_to_array_fn(batch):
speech_array, sampling_rate = torchaudio.load(batch["path"])
speech_array = speech_array.squeeze().numpy()
speech_array = librosa.resample(np.asarray(speech_array), sampling_rate, 16_000)
batch["speech"] = speech_array
return batch
def predict(batch):
features = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
input_values = features.input_values.to(device)
attention_mask = features.attention_mask.to(device)
with torch.no_grad():
logits = model(input_values, attention_mask=attention_mask).logits
pred_ids = torch.argmax(logits, dim=-1)
batch["predicted"] = processor.batch_decode(pred_ids)[0]
return batch
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
processor = Wav2Vec2Processor.from_pretrained("m3hrdadfi/wav2vec2-large-xlsr-georgian")
model = Wav2Vec2ForCTC.from_pretrained("m3hrdadfi/wav2vec2-large-xlsr-georgian").to(device)
dataset = load_dataset("common_voice", "ka", split="test[:1%]")
dataset = dataset.map(
normalizer,
fn_kwargs={"chars_to_ignore": chars_to_ignore, "chars_to_mapping": chars_to_mapping},
remove_columns=list(set(dataset.column_names) - set(['sentence', 'path']))
)
dataset = dataset.map(speech_file_to_array_fn)
result = dataset.map(predict)
max_items = np.random.randint(0, len(result), 20).tolist()
for i in max_items:
reference, predicted = result["sentence"][i], result["predicted"][i]
print("reference:", reference)
print("predicted:", predicted)
print('---')
```
**Output:**
```text
reference: แƒแƒ“แƒ›แƒ˜แƒœแƒ˜แƒกแƒขแƒ แƒแƒชแƒ˜แƒฃแƒšแƒ˜ แƒชแƒ”แƒœแƒขแƒ แƒ˜ แƒฅแƒแƒšแƒแƒฅแƒ˜ แƒ˜แƒ›แƒ˜แƒจแƒšแƒ˜
predicted: แƒแƒ“แƒ›แƒ˜แƒœแƒ˜แƒกแƒขแƒ แƒแƒชแƒ˜แƒฃแƒšแƒ˜ แƒชแƒ”แƒœแƒขแƒ แƒ˜ แƒฅแƒแƒšแƒแƒฅแƒ˜ แƒ˜แƒ›แƒ˜แƒจแƒšแƒ˜
---
reference: แƒ“แƒแƒ˜แƒ‘แƒแƒ“แƒ แƒแƒ“แƒ•แƒแƒ™แƒแƒขแƒ˜แƒก แƒแƒฏแƒแƒฎแƒจแƒ˜
predicted: แƒแƒ˜แƒ‘แƒแƒ“แƒ แƒแƒ“แƒ›แƒแƒ™แƒแƒขแƒ˜แƒก แƒแƒฏแƒแƒฎแƒจแƒ˜
---
reference: แƒแƒฆแƒกแƒแƒœแƒ˜แƒจแƒœแƒแƒ•แƒ˜แƒ แƒ แƒแƒ› แƒกแƒ˜แƒ›แƒฆแƒ”แƒ แƒ แƒฌแƒแƒ แƒ›แƒแƒแƒ“แƒ’แƒ”แƒœแƒก แƒžแƒแƒš แƒ›แƒแƒ™แƒ™แƒแƒ แƒขแƒœแƒ˜แƒกแƒ แƒ“แƒ แƒฏแƒแƒ แƒฏ แƒฐแƒแƒ แƒ˜แƒกแƒแƒœแƒ˜แƒก แƒ˜แƒจแƒ•แƒ˜แƒแƒ— แƒ•แƒแƒ™แƒแƒšแƒฃแƒ  แƒ“แƒฃแƒ”แƒขแƒก
predicted: แƒแƒฆแƒกแƒ”แƒœแƒ˜แƒจแƒœแƒแƒ•แƒ˜แƒแƒ แƒ แƒกแƒ˜แƒ›แƒฆแƒ” แƒ แƒแƒฌแƒแƒ แƒ›แƒแƒแƒ“แƒ’แƒ”แƒ›แƒก แƒ‘แƒแƒš แƒ›แƒแƒ™แƒแƒ แƒ“แƒœแƒ˜แƒก แƒ“แƒ แƒฏแƒแƒ แƒฉแƒฎแƒแƒ แƒ˜แƒกแƒแƒœแƒ˜แƒก แƒ˜แƒจแƒ•แƒ˜แƒแƒ“ แƒ•แƒแƒ™แƒแƒšแƒฃแƒ  แƒ“แƒฃแƒ”แƒ—แƒก
---
reference: แƒ˜แƒ™แƒ แƒซแƒแƒšแƒ”แƒ‘แƒแƒ“แƒ แƒฌแƒ˜แƒ แƒ•แƒแƒšแƒแƒชแƒ•แƒ แƒฅแƒแƒ แƒ—แƒฃแƒš แƒ”แƒœแƒแƒ–แƒ”
predicted: แƒ˜แƒ™แƒ แƒซแƒแƒšแƒ”แƒ‘แƒแƒ“แƒ” แƒฌแƒ˜แƒ แƒ•แƒ แƒšแƒแƒชแƒ•แƒ แƒฅแƒแƒ แƒ—แƒฃแƒš แƒ”แƒœแƒแƒ–แƒ”
---
reference: แƒแƒฆแƒ›แƒแƒ แƒ—แƒฃแƒšแƒ˜แƒ แƒ•แƒแƒšแƒ”แƒกแƒ แƒ“แƒ แƒ‘แƒ”แƒ แƒœแƒ˜แƒก แƒ™แƒแƒœแƒขแƒแƒœแƒ”แƒ‘แƒ˜แƒก แƒกแƒแƒ–แƒฆแƒ•แƒแƒ แƒ–แƒ”
predicted: แƒแƒฆแƒ›แƒแƒ แƒ—แƒฃแƒšแƒ˜แƒ แƒ•แƒแƒšแƒ”แƒกแƒ แƒ“แƒ แƒ‘แƒ”แƒ แƒœแƒ˜แƒก แƒ™แƒแƒœแƒ—แƒแƒœแƒ”แƒ‘แƒ˜แƒก แƒกแƒแƒ–แƒฆแƒ•แƒแƒ แƒ–แƒ”
---
reference: แƒแƒฅ แƒ˜แƒ’แƒ˜ แƒ›แƒ˜แƒ˜แƒฌแƒ•แƒ˜แƒ”แƒก แƒกแƒแƒ›แƒฎแƒแƒขแƒ•แƒ แƒ แƒแƒ™แƒแƒ“แƒ”แƒ›แƒ˜แƒแƒจแƒ˜ แƒกแƒแƒ“แƒแƒช แƒกแƒ˜แƒชแƒแƒชแƒฎแƒšแƒ˜แƒก แƒ‘แƒแƒšแƒแƒ›แƒ“แƒ” แƒ”แƒฌแƒ”แƒแƒ“แƒ แƒžแƒ”แƒ“แƒแƒ’แƒแƒ’แƒ˜แƒฃแƒ  แƒ›แƒแƒฆแƒ•แƒแƒฌแƒ”แƒแƒ‘แƒแƒก
predicted: แƒแƒฅ แƒ˜แƒ’แƒ˜ แƒ›แƒ˜แƒ˜แƒกแƒฌแƒ แƒ•แƒ˜แƒ”แƒก แƒกแƒแƒ›แƒฎแƒแƒขแƒ แƒ แƒแƒ™แƒแƒ“แƒ”แƒ›แƒ˜ แƒแƒจแƒ˜แƒกแƒ แƒ“แƒ แƒชแƒ˜แƒชแƒแƒชแƒฎแƒšแƒ˜แƒก แƒ‘แƒแƒšแƒแƒ›แƒ“แƒ” แƒ”แƒฌแƒงแƒ”แƒ‘แƒแƒ‘ แƒ“แƒ แƒžแƒ”แƒ“แƒแƒ’แƒฃแƒ“แƒ˜แƒ•แƒ˜แƒ  แƒ›แƒแƒงแƒ•แƒแƒฌแƒ”แƒ•แƒ”แƒ‘แƒแƒก
---
reference: แƒ™แƒšแƒแƒ แƒ˜แƒกแƒ แƒ—แƒแƒœแƒฎแƒ›แƒ“แƒ”แƒ‘แƒ แƒจแƒ”แƒ›แƒแƒ—แƒแƒ•แƒแƒ–แƒ”แƒ‘แƒแƒ–แƒ” แƒ“แƒ แƒšแƒ”แƒฅแƒขแƒ”แƒ แƒ˜แƒก แƒ“แƒแƒฎแƒ›แƒแƒ แƒ”แƒ‘แƒ˜แƒ— แƒกแƒ”แƒ แƒ˜แƒฃแƒšแƒ˜ แƒ›แƒ™แƒ•แƒšแƒ”แƒšแƒ˜แƒก แƒ™แƒ•แƒแƒšแƒก แƒ“แƒแƒแƒ“แƒ’แƒ”แƒ‘แƒ
predicted: แƒ™แƒšแƒแƒ แƒ˜แƒก แƒ—แƒแƒœ แƒฎแƒ•แƒ“แƒ”แƒ‘แƒ แƒจแƒ”แƒ›แƒฃแƒ—แƒแƒ•แƒแƒ–แƒ” แƒ‘แƒแƒ–แƒ” แƒ“แƒ แƒšแƒ”แƒฅแƒขแƒ”แƒ แƒ˜แƒก แƒ“แƒแƒฎแƒ›แƒแƒ แƒ”แƒ‘แƒ˜แƒช แƒกแƒ”แƒ แƒ˜แƒฃแƒ แƒ˜ แƒ›แƒ™แƒ•แƒšแƒ”แƒšแƒ˜แƒก แƒ™แƒ•แƒ”แƒšแƒก แƒ“แƒแƒแƒ“แƒ’แƒ”แƒ‘แƒแƒ
---
reference: แƒ˜แƒ‘แƒ แƒซแƒแƒ“แƒ แƒขแƒงแƒ•แƒ”แƒ”แƒ‘แƒ˜แƒ— แƒ•แƒแƒญแƒ แƒแƒ‘แƒ˜แƒก แƒฌแƒ˜แƒœแƒแƒแƒฆแƒ›แƒ“แƒ”แƒ’
predicted: แƒ“แƒ˜แƒ‘แƒ แƒซแƒแƒขแƒ แƒขแƒงแƒ•แƒ”แƒ”แƒ‘แƒ˜แƒ— แƒ•แƒแƒญแƒ แƒแƒ‘แƒ˜แƒก แƒฌแƒ˜แƒœแƒแƒแƒฆแƒ“แƒ”แƒ’
---
reference: แƒกแƒแƒ—แƒแƒ•แƒกแƒก แƒแƒฆแƒ›แƒแƒกแƒแƒ•แƒšแƒ”แƒ—แƒ˜แƒ— แƒ“แƒ แƒ“แƒแƒกแƒแƒ•แƒšแƒ”แƒ—แƒ˜แƒ— แƒ—แƒ˜แƒ—แƒ แƒกแƒแƒ แƒ™แƒ›แƒ”แƒšแƒ˜ แƒแƒฅแƒ•แƒก
predicted: แƒกแƒแƒ—แƒแƒ•แƒก แƒแƒฆแƒ›แƒแƒกแƒแƒ•แƒ”แƒšแƒ”แƒ—แƒ˜ แƒ“แƒ แƒ“แƒแƒกแƒแƒ•แƒšแƒ”แƒ— แƒ›แƒ—แƒ˜แƒ“แƒ แƒกแƒแƒ แƒ™แƒ›แƒ”แƒšแƒ˜ แƒแƒฅแƒ•แƒก
---
reference: แƒ˜แƒ’แƒ˜ แƒ›แƒ“แƒ”แƒ‘แƒแƒ แƒ”แƒแƒ‘แƒก แƒฅแƒแƒšแƒแƒฅแƒ˜แƒก แƒฉแƒ แƒ“แƒ˜แƒšแƒแƒแƒฆแƒ›แƒแƒกแƒแƒ•แƒšแƒ”แƒ— แƒœแƒแƒฌแƒ˜แƒšแƒจแƒ˜
predicted: แƒ˜แƒ’แƒ˜ แƒ›แƒ“แƒ”แƒ‘แƒแƒ แƒ”แƒแƒ‘แƒก แƒฅแƒแƒšแƒแƒฅแƒ˜แƒก แƒฉแƒ แƒ“แƒ˜แƒšแƒ แƒแƒฆแƒ›แƒแƒกแƒแƒ•แƒšแƒ”แƒ— แƒœแƒแƒฌแƒ˜แƒšแƒจแƒ˜
---
```
## Evaluation
The model can be evaluated as follows on the Georgian test data of Common Voice.
```python
import librosa
import torch
import torchaudio
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
from datasets import load_dataset, load_metric
import numpy as np
import re
import string
chars_to_ignore = [
",", "?", ".", "!", "-", ";", ":", '""', "%", "'", '"', "๏ฟฝ",
"#", "!", "?", "ยซ", "ยป", "(", ")", "ุ›", ",", "?", ".", "!", "-", ";", ":", '"',
"โ€œ", "%", "โ€˜", "๏ฟฝ", "โ€“", "โ€ฆ", "_", "โ€", 'โ€œ', 'โ€ž'
]
chars_to_mapping = {
"\u200c": " ", "\u200d": " ", "\u200e": " ", "\u200f": " ", "\ufeff": " ",
}
def multiple_replace(text, chars_to_mapping):
pattern = "|".join(map(re.escape, chars_to_mapping.keys()))
return re.sub(pattern, lambda m: chars_to_mapping[m.group()], str(text))
def remove_special_characters(text, chars_to_ignore_regex):
text = re.sub(chars_to_ignore_regex, '', text).lower() + " "
return text
def normalizer(batch, chars_to_ignore, chars_to_mapping):
chars_to_ignore_regex = f"""[{"".join(chars_to_ignore)}]"""
text = batch["sentence"].lower().strip()
text = multiple_replace(text, chars_to_mapping)
text = remove_special_characters(text, chars_to_ignore_regex)
batch["sentence"] = text
return batch
def speech_file_to_array_fn(batch):
speech_array, sampling_rate = torchaudio.load(batch["path"])
speech_array = speech_array.squeeze().numpy()
speech_array = librosa.resample(np.asarray(speech_array), sampling_rate, 16_000)
batch["speech"] = speech_array
return batch
def predict(batch):
features = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
input_values = features.input_values.to(device)
attention_mask = features.attention_mask.to(device)
with torch.no_grad():
logits = model(input_values, attention_mask=attention_mask).logits
pred_ids = torch.argmax(logits, dim=-1)
batch["predicted"] = processor.batch_decode(pred_ids)[0]
return batch
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
processor = Wav2Vec2Processor.from_pretrained("m3hrdadfi/wav2vec2-large-xlsr-georgian")
model = Wav2Vec2ForCTC.from_pretrained("m3hrdadfi/wav2vec2-large-xlsr-georgian").to(device)
dataset = load_dataset("common_voice", "ka", split="test")
dataset = dataset.map(
normalizer,
fn_kwargs={"chars_to_ignore": chars_to_ignore, "chars_to_mapping": chars_to_mapping},
remove_columns=list(set(dataset.column_names) - set(['sentence', 'path']))
)
dataset = dataset.map(speech_file_to_array_fn)
result = dataset.map(predict)
wer = load_metric("wer")
print("WER: {:.2f}".format(100 * wer.compute(predictions=result["predicted"], references=result["sentence"])))
```
**Test Result**:
- WER: 54.00%
## Training & Report
The Common Voice `train`, `validation` datasets were used for training.
You can see the training states [here](https://wandb.ai/m3hrdadfi/finetuned_wav2vec_xlsr_georgian/reports/Fine-Tuning-for-Wav2Vec2-Large-XLSR-53-Georgian--Vmlldzo1NTg5MDQ?accessToken=rsmd0p83iln13yq23b9kzj8bim6nco21w8cqn2tb19v51okakqk92c71h6hbxmfj)
The script used for training can be found [here](https://colab.research.google.com/github/m3hrdadfi/notebooks/blob/main/Fine_Tune_XLSR_Wav2Vec2_on_Georgian_ASR_with_%F0%9F%A4%97_Transformers_ipynb.ipynb)