kmfoda commited on
Commit
5d76233
1 Parent(s): 30e4171

Update README.MD

Browse files
Files changed (1) hide show
  1. README.md +118 -1
README.md CHANGED
@@ -1 +1,118 @@
1
- hello
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language: ar
3
+ datasets:
4
+ - common_voice
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: XLSR Wav2Vec2 Arabic by Othmane Rifki
15
+ results:
16
+ - task:
17
+ name: Speech Recognition
18
+ type: automatic-speech-recognition
19
+ dataset:
20
+ name: Common Voice ar
21
+ type: common_voice
22
+ args: ar
23
+ metrics:
24
+ - name: Test WER
25
+ type: wer
26
+ value: 46.77
27
+ ---
28
+
29
+ # Wav2Vec2-Large-XLSR-53-Arabic
30
+
31
+ Fine-tuned [facebook/wav2vec2-large-xlsr-53](https://huggingface.co/facebook/wav2vec2-large-xlsr-53) on Arabic using the [Common Voice](https://huggingface.co/datasets/common_voice).
32
+ When using this model, make sure that your speech input is sampled at 16kHz.
33
+
34
+ ## Usage
35
+
36
+ The model can be used directly (without a language model) as follows:
37
+
38
+ ```python
39
+ import librosa
40
+ import torch
41
+ import torchaudio
42
+ from datasets import load_dataset
43
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
44
+
45
+ test_dataset = load_dataset("common_voice", "ar", split="test[:2%]")
46
+
47
+ processor = Wav2Vec2Processor.from_pretrained("kmfoda/wav2vec2-large-xlsr-arabic")
48
+ model = Wav2Vec2ForCTC.from_pretrained("kmfoda/wav2vec2-large-xlsr-arabic")
49
+
50
+ def prepare_example(example):
51
+ example["speech"], _ = librosa.load(example["path"], sr=16000)
52
+ return example
53
+
54
+ test_dataset = test_dataset.map(prepare_example)
55
+
56
+ inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
57
+
58
+ with torch.no_grad():
59
+ logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
60
+
61
+ predicted_ids = torch.argmax(logits, dim=-1)
62
+
63
+ print("Prediction:", processor.batch_decode(predicted_ids))
64
+ print("Reference:", test_dataset["sentence"][:2])
65
+ ```
66
+
67
+ ## Evaluation
68
+
69
+ The model can be evaluated as follows on the Arabic test data of Common Voice.
70
+
71
+ ```python
72
+ import librosa
73
+ import torch
74
+ import torchaudio
75
+ from datasets import load_dataset, load_metric
76
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
77
+ import re
78
+
79
+ test_dataset = load_dataset("common_voice", "ar", split="test")
80
+ wer = load_metric("wer")
81
+ processor = Wav2Vec2Processor.from_pretrained("kmfoda/wav2vec2-large-xlsr-arabic")
82
+ model = Wav2Vec2ForCTC.from_pretrained("kmfoda/wav2vec2-large-xlsr-arabic")
83
+ model.to("cuda")
84
+
85
+ chars_to_ignore_regex = '[\,\?\.\!\-\;\:\"\“\؟\_\؛\ـ\—]'
86
+
87
+ def prepare_example(example):
88
+ example["speech"], _ = librosa.load(example["path"], sr=16000)
89
+ return example
90
+
91
+ test_dataset = test_dataset.map(prepare_example)
92
+
93
+ # Preprocessing the datasets.
94
+ # We need to read the audio files as arrays
95
+ def evaluate(batch):
96
+ inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
97
+
98
+ with torch.no_grad():
99
+ logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
100
+
101
+ pred_ids = torch.argmax(logits, dim=-1)
102
+ batch["pred_strings"] = processor.batch_decode(pred_ids)
103
+ return batch
104
+
105
+ result = test_dataset.map(evaluate, batched=True, batch_size=8)
106
+
107
+ print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"], references=result["sentence"])))
108
+
109
+ ```
110
+
111
+ **Test Result**: ??
112
+
113
+
114
+ ## Training
115
+
116
+ The Common Voice `train`, `validation` datasets were used for training.
117
+
118
+ The script used for training can be found [here](https://huggingface.co/othrif/wav2vec2-large-xlsr-arabic/tree/main)