1
---
2
language: kk
3
datasets:
4
- kazakh_speech_corpus
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-XLSR-53 Kazakh by adilism
15
  results:
16
  - task: 
17
      name: Speech Recognition
18
      type: automatic-speech-recognition
19
    dataset:
20
      name: Kazakh Speech Corpus v1.1
21
      type: kazakh_speech_corpus
22
      args: kk
23
    metrics:
24
       - name: Test WER
25
         type: wer
26
         value: 19.65
27
---
28
29
# Wav2Vec2-Large-XLSR-53-Kazakh
30
31
Fine-tuned [facebook/wav2vec2-large-xlsr-53](https://huggingface.co/facebook/wav2vec2-large-xlsr-53) for Kazakh ASR using the [Kazakh Speech Corpus v1.1](https://issai.nu.edu.kz/kz-speech-corpus/?version=1.1)
32
33
When using this model, make sure that your speech input is sampled at 16kHz.
34
35
## Usage
36
37
The model can be used directly (without a language model) as follows:
38
39
```python
40
import torch
41
import torchaudio
42
from datasets import load_dataset
43
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
44
45
from utils import get_test_dataset
46
47
test_dataset = get_test_dataset("ISSAI_KSC_335RS_v1.1")
48
49
processor = Wav2Vec2Processor.from_pretrained("wav2vec2-large-xlsr-kazakh")
50
model = Wav2Vec2ForCTC.from_pretrained("wav2vec2-large-xlsr-kazakh")
51
52
53
# Preprocessing the datasets.
54
# We need to read the audio files as arrays
55
def speech_file_to_array_fn(batch):
56
    speech_array, sampling_rate = torchaudio.load(batch["path"])
57
    batch["speech"] = torchaudio.transforms.Resample(sampling_rate, 16_000)(speech_array).squeeze().numpy()
58
    return batch
59
60
test_dataset = test_dataset.map(speech_file_to_array_fn)
61
inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
62
63
with torch.no_grad():
64
    logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
65
66
predicted_ids = torch.argmax(logits, dim=-1)
67
68
print("Prediction:", processor.batch_decode(predicted_ids))
69
print("Reference:", test_dataset["sentence"][:2])
70
```
71
72
73
## Evaluation
74
75
The model can be evaluated as follows on the test set of [Kazakh Speech Corpus v1.1](https://issai.nu.edu.kz/kz-speech-corpus/?version=1.1). To evaluate, download the [archive](https://www.openslr.org/resources/102/ISSAI_KSC_335RS_v1.1_flac.tar.gz), untar and pass the path to data to `get_test_dataset` as below:
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
from utils import get_test_dataset
85
86
test_dataset = get_test_dataset("ISSAI_KSC_335RS_v1.1")
87
wer = load_metric("wer")
88
89
processor = Wav2Vec2Processor.from_pretrained("adilism/wav2vec2-large-xlsr-kazakh")
90
model = Wav2Vec2ForCTC.from_pretrained("adilism/wav2vec2-large-xlsr-kazakh")
91
model.to("cuda")
92
93
94
# Preprocessing the datasets.
95
# We need to read the audio files as arrays
96
def speech_file_to_array_fn(batch):
97
    batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
98
    speech_array, sampling_rate = torchaudio.load(batch["path"])
99
    batch["speech"] = torchaudio.transforms.Resample(sampling_rate, 16_000)(speech_array).squeeze().numpy()
100
    return batch
101
102
test_dataset = test_dataset.map(speech_file_to_array_fn)
103
104
def evaluate(batch):
105
    inputs = processor(batch["text"], sampling_rate=16_000, return_tensors="pt", padding=True)
106
107
    with torch.no_grad():
108
        logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
109
110
    pred_ids = torch.argmax(logits, dim=-1)
111
    batch["pred_strings"] = processor.batch_decode(pred_ids)
112
    return batch
113
114
result = test_dataset.map(evaluate, batched=True, batch_size=8)
115
116
print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"], references=result["sentence"])))
117
```
118
119
**Test Result**: 19.65%
120
121
122
## Training
123
124
The Kazakh Speech Corpus v1.1 `train` dataset was used for training.