Upload README.md
Browse files
README.md
ADDED
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
|
3 |
+
language: ne
|
4 |
+
datasets:
|
5 |
+
- OpenSLR
|
6 |
+
- common_voice
|
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: wav2vec2-xlsr-nepali
|
17 |
+
results:
|
18 |
+
- task:
|
19 |
+
name: Speech Recognition
|
20 |
+
type: automatic-speech-recognition
|
21 |
+
dataset:
|
22 |
+
name: OpenSLR ne
|
23 |
+
type: OpenSLR
|
24 |
+
args: ne
|
25 |
+
metrics:
|
26 |
+
- name: Test WER
|
27 |
+
type: wer
|
28 |
+
value: 05.97
|
29 |
+
---
|
30 |
+
|
31 |
+
# Wav2Vec2-Large-XLSR-53-Nepali
|
32 |
+
|
33 |
+
Fine-tuned [facebook/wav2vec2-large-xlsr-53](https://huggingface.co/facebook/wav2vec2-large-xlsr-53) on Nepali using the [Common Voice](https://huggingface.co/datasets/common_voice), and [OpenSLR ne](http://www.openslr.org/43/).
|
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 torch
|
43 |
+
import torchaudio
|
44 |
+
from datasets import load_dataset
|
45 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
46 |
+
|
47 |
+
!wget https://www.openslr.org/resources/43/ne_np_female.zip
|
48 |
+
!unzip ne_np_female.zip
|
49 |
+
!ls ne_np_female
|
50 |
+
|
51 |
+
colnames=['path','sentence']
|
52 |
+
df = pd.read_csv('/content/ne_np_female/line_index.tsv',sep='\\t',header=None,names = colnames)
|
53 |
+
df['path'] = '/content/ne_np_female/wavs/'+df['path'] +'.wav'
|
54 |
+
|
55 |
+
train, test = train_test_split(df, test_size=0.1)
|
56 |
+
|
57 |
+
test.to_csv('/content/ne_np_female/line_index_test.csv')
|
58 |
+
|
59 |
+
test_dataset = load_dataset('csv', data_files='/content/ne_np_female/line_index_test.csv',split = 'train')
|
60 |
+
|
61 |
+
processor = Wav2Vec2Processor.from_pretrained("gagan3012/wav2vec2-xlsr-nepali")
|
62 |
+
model = Wav2Vec2ForCTC.from_pretrained("gagan3012/wav2vec2-xlsr-nepali")
|
63 |
+
|
64 |
+
resampler = torchaudio.transforms.Resample(48_000, 16_000)
|
65 |
+
|
66 |
+
# Preprocessing the datasets.
|
67 |
+
# We need to read the aduio files as arrays
|
68 |
+
def speech_file_to_array_fn(batch):
|
69 |
+
\tspeech_array, sampling_rate = torchaudio.load(batch["path"])
|
70 |
+
\tbatch["speech"] = resampler(speech_array).squeeze().numpy()
|
71 |
+
\treturn batch
|
72 |
+
|
73 |
+
test_dataset = test_dataset.map(speech_file_to_array_fn)
|
74 |
+
inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
|
75 |
+
|
76 |
+
with torch.no_grad():
|
77 |
+
\tlogits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
|
78 |
+
|
79 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
80 |
+
|
81 |
+
print("Prediction:", processor.batch_decode(predicted_ids))
|
82 |
+
print("Reference:", test_dataset["sentence"][:2])
|
83 |
+
|
84 |
+
```
|
85 |
+
#### Result
|
86 |
+
|
87 |
+
Prediction: ['पारानाको ब्राजिली राज्यमा रहेको राजधानी', 'देवराज जोशी त्रिभुवन विश्वविद्यालयबाट शिक्षाशास्त्रमा स्नातक हुनुहुन्छ']
|
88 |
+
|
89 |
+
Reference: ['पारानाको ब्राजिली राज्यमा रहेको राजधानी', 'देवराज जोशी त्रिभुवन विश्वविद्यालयबाट शिक्षाशास्त्रमा स्नातक हुनुहुन्छ']
|
90 |
+
|
91 |
+
## Evaluation
|
92 |
+
|
93 |
+
The model can be evaluated as follows on the {language} test data of Common Voice. # TODO: replace #TODO: replace language with your {language}, *e.g.* French
|
94 |
+
|
95 |
+
|
96 |
+
```python
|
97 |
+
import torch
|
98 |
+
import torchaudio
|
99 |
+
from datasets import load_dataset, load_metric
|
100 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
101 |
+
import re
|
102 |
+
|
103 |
+
!wget https://www.openslr.org/resources/43/ne_np_female.zip
|
104 |
+
!unzip ne_np_female.zip
|
105 |
+
!ls ne_np_female
|
106 |
+
|
107 |
+
colnames=['path','sentence']
|
108 |
+
df = pd.read_csv('/content/ne_np_female/line_index.tsv',sep='\\t',header=None,names = colnames)
|
109 |
+
df['path'] = '/content/ne_np_female/wavs/'+df['path'] +'.wav'
|
110 |
+
|
111 |
+
train, test = train_test_split(df, test_size=0.1)
|
112 |
+
|
113 |
+
test.to_csv('/content/ne_np_female/line_index_test.csv')
|
114 |
+
|
115 |
+
test_dataset = load_dataset('csv', data_files='/content/ne_np_female/line_index_test.csv',split = 'train')
|
116 |
+
wer = load_metric("wer")
|
117 |
+
|
118 |
+
processor = Wav2Vec2Processor.from_pretrained("gagan3012/wav2vec2-xlsr-nepali")
|
119 |
+
model = Wav2Vec2ForCTC.from_pretrained("gagan3012/wav2vec2-xlsr-nepali")
|
120 |
+
model.to("cuda")
|
121 |
+
|
122 |
+
chars_to_ignore_regex = '[\\,\\?\\.\\!\\-\\;\\:\\"\\“]'
|
123 |
+
resampler = torchaudio.transforms.Resample(48_000, 16_000)
|
124 |
+
|
125 |
+
# Preprocessing the datasets.
|
126 |
+
# We need to read the aduio files as arrays
|
127 |
+
def speech_file_to_array_fn(batch):
|
128 |
+
\tbatch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
|
129 |
+
\tspeech_array, sampling_rate = torchaudio.load(batch["path"])
|
130 |
+
\tbatch["speech"] = resampler(speech_array).squeeze().numpy()
|
131 |
+
\treturn batch
|
132 |
+
|
133 |
+
test_dataset = test_dataset.map(speech_file_to_array_fn)
|
134 |
+
|
135 |
+
# Preprocessing the datasets.
|
136 |
+
# We need to read the aduio files as arrays
|
137 |
+
def evaluate(batch):
|
138 |
+
\tinputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
|
139 |
+
|
140 |
+
\twith torch.no_grad():
|
141 |
+
\t\tlogits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
|
142 |
+
|
143 |
+
\tpred_ids = torch.argmax(logits, dim=-1)
|
144 |
+
\tbatch["pred_strings"] = processor.batch_decode(pred_ids)
|
145 |
+
\treturn batch
|
146 |
+
|
147 |
+
result = test_dataset.map(evaluate, batched=True, batch_size=8)
|
148 |
+
|
149 |
+
print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"], references=result["sentence"])))
|
150 |
+
|
151 |
+
```
|
152 |
+
|
153 |
+
**Test Result**: 05.97 %
|
154 |
+
|
155 |
+
## Training
|
156 |
+
|
157 |
+
The script used for training can be found [here](https://colab.research.google.com/drive/1AHnYWXb5cwfMEa2o4O3TSdasAR3iVBFP?usp=sharing)
|