mohamed1ai commited on
Commit
6eb1a1a
1 Parent(s): aed7a50

add model-card

Browse files

Fine-tune Wav2Vec2-Large-XLSR-53-Arabic model

Files changed (1) hide show
  1. README.md +113 -0
README.md ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language: ar
3
+ use datasets:
4
+ - common_voice: Common Voice Corpus 5.1
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: Hasni XLSR Wav2Vec2 Large 53
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: 52
27
+ ---
28
+ # Wav2Vec2-Large-XLSR-53-Arabic
29
+
30
+ Fine-tuned [facebook/wav2vec2-large-xlsr-53](https://huggingface.co/facebook/wav2vec2-large-xlsr-53) on Arabic using the [Common Voice Corpus 5.1](https://commonvoice.mozilla.org/en/datasets) dataset.
31
+ When using this model, make sure that your speech input is sampled at 16kHz.
32
+
33
+ ## Usage
34
+
35
+ The model can be used directly (without a language model) as follows:
36
+
37
+ ```python
38
+ import torch
39
+ import torchaudio
40
+ from datasets import load_dataset
41
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
42
+ test_dataset = load_dataset("common_voice", "ar", split="test[:2%]")
43
+ processor = Wav2Vec2Processor.from_pretrained("mohamed1ai/wav2vec2-large-xls-ar")
44
+ model = Wav2Vec2ForCTC.from_pretrained("mohamed1ai/wav2vec2-large-xls-ar")
45
+ resampler = torchaudio.transforms.Resample(48_000, 16_000)
46
+ # Preprocessing the datasets.
47
+ # We need to read the aduio files as arrays
48
+ def speech_file_to_array_fn(batch):
49
+ speech_array, sampling_rate = torchaudio.load(batch["path"])
50
+ batch["speech"] = resampler(speech_array).squeeze().numpy()
51
+ return batch
52
+ test_dataset = test_dataset.map(speech_file_to_array_fn)
53
+ inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
54
+ with torch.no_grad():
55
+ logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
56
+ predicted_ids = torch.argmax(logits, dim=-1)
57
+ print("Prediction:", processor.batch_decode(predicted_ids))
58
+ print("Reference:", test_dataset["sentence"][:2])
59
+ ```
60
+
61
+
62
+ ## Evaluation
63
+
64
+ The model can be evaluated as follows on the Arabic test data of Common Voice.
65
+
66
+
67
+ ```python
68
+ import torch
69
+ import torchaudio
70
+ from datasets import load_dataset, load_metric
71
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
72
+ import re
73
+ test_dataset = load_dataset("common_voice", "ar", split="test")
74
+ processor = Wav2Vec2Processor.from_pretrained("mohamed1ai/wav2vec2-large-xls-ar")
75
+ model = Wav2Vec2ForCTC.from_pretrained("mohamed1ai/wav2vec2-large-xls-ar")
76
+ model.to("cuda")
77
+ chars_to_ignore_regex = '[\,\؟\.\!\-\;\\:\'\"\☭\«\»\؛\—\ـ\_\،\“\%\‘\”\�]'
78
+ resampler = torchaudio.transforms.Resample(48_000, 16_000)
79
+ # Preprocessing the datasets.
80
+ # We need to read the aduio files as arrays
81
+ def speech_file_to_array_fn(batch):
82
+ batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
83
+ batch["sentence"] = re.sub('[a-z]','',batch["sentence"])
84
+ batch["sentence"] = re.sub("[إأٱآا]", "ا", batch["sentence"])
85
+ noise = re.compile(""" ّ | # Tashdid
86
+ َ | # Fatha
87
+ ً | # Tanwin Fath
88
+ ُ | # Damma
89
+ ٌ | # Tanwin Damm
90
+ ِ | # Kasra
91
+ ٍ | # Tanwin Kasr
92
+ ْ | # Sukun
93
+ ـ # Tatwil/Kashida
94
+ """, re.VERBOSE)
95
+ batch["sentence"] = re.sub(noise, '', batch["sentence"])
96
+ speech_array, sampling_rate = torchaudio.load(batch["path"])
97
+ batch["speech"] = resampler(speech_array).squeeze().numpy()
98
+ return batch
99
+ test_dataset = test_dataset.map(speech_file_to_array_fn)
100
+ # Preprocessing the datasets.
101
+ # We need to read the aduio files as arrays
102
+ def evaluate(batch):
103
+ inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
104
+ with torch.no_grad():
105
+ logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
106
+ pred_ids = torch.argmax(logits, dim=-1)
107
+ batch["pred_strings"] = processor.batch_decode(pred_ids)
108
+ return batch
109
+ result = test_dataset.map(evaluate, batched=True, batch_size=8)
110
+ print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"], references=result["sentence"])))
111
+ ```
112
+
113
+ **Test Result**: 52 %