Nhut DOANNGUYEN commited on
Commit
7eb8f5d
2 Parent(s): 66a09a5 417cfa3

Merge branch 'main' of https://huggingface.co/Nhut/wav2vec2-large-xlsr-french

Browse files
Files changed (1) hide show
  1. README.md +117 -0
README.md ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language: fr
3
+ datasets:
4
+ - common_voice
5
+ tags:
6
+ - audio
7
+ - automatic-speech-recognition
8
+ - speech
9
+ - xlsr-fine-tuning-week
10
+ license: apache-2.0
11
+ model-index:
12
+ - name: wav2vec2-large-xlsr-53-French by Nhut DOAN NGUYEN
13
+ results:
14
+ - task:
15
+ name: Speech Recognition
16
+ type: automatic-speech-recognition
17
+ dataset:
18
+ name: Common Voice fr
19
+ type: common_voice
20
+ args: fr
21
+ metrics:
22
+ - name: Test WER
23
+ type: wer
24
+ value: xx.xx
25
+ ---
26
+
27
+ # wav2vec2-large-xlsr-53-french
28
+
29
+ Fine-tuned [facebook/wav2vec2-large-xlsr-53](https://huggingface.co/facebook/wav2vec2-large-xlsr-53) in French using the [Common Voice](https://huggingface.co/datasets/common_voice)
30
+
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
+
41
+ from datasets import load_dataset
42
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
43
+
44
+ test_dataset = load_dataset("common_voice", "fr")
45
+ processor = Wav2Vec2Processor.from_pretrained("Nhut/wav2vec2-large-xlsr-53-french")
46
+ model = Wav2Vec2ForCTC.from_pretrained("Nhut/wav2vec2-large-xlsr-53-french")
47
+ resampler = torchaudio.transforms.Resample(48_000, 16_000)
48
+
49
+ # Preprocessing the datasets.
50
+
51
+ # We need to read the aduio files as arrays
52
+
53
+ def speech_file_to_array_fn(batch):
54
+ speech_array, sampling_rate = torchaudio.load(batch["path"])
55
+ batch["speech"] = resampler(speech_array).squeeze().numpy()
56
+ return batch
57
+
58
+ test_dataset = test_dataset.map(speech_file_to_array_fn)
59
+ inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
60
+
61
+ with torch.no_grad():
62
+ logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
63
+
64
+ predicted_ids = torch.argmax(logits, dim=-1)
65
+ print("Prediction:", processor.batch_decode(predicted_ids))
66
+ print("Reference:", test_dataset["sentence"][:2])
67
+ ```
68
+
69
+ ## Evaluation
70
+
71
+ The model can be evaluated as follows on the French test data of Common Voice.
72
+
73
+ ```python
74
+ import torch
75
+ import torchaudio
76
+ from datasets import load_dataset, load_metric
77
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
78
+ import re
79
+
80
+ test_dataset = load_dataset("common_voice", "fr")
81
+ wer = load_metric("wer")
82
+ processor = Wav2Vec2Processor.from_pretrained("MehdiHosseiniMoghadam/wav2vec2-large-xlsr-53-French")
83
+ model = Wav2Vec2ForCTC.from_pretrained("MehdiHosseiniMoghadam/wav2vec2-large-xlsr-53-French")
84
+ model.to("cuda")
85
+ chars_to_ignore_regex = '[\,\?\.\!\-\;\:\"\“\%\‘\”\�]'
86
+ resampler = torchaudio.transforms.Resample(48_000, 16_000)
87
+
88
+ # Preprocessing the datasets.
89
+
90
+ # We need to read the aduio files as arrays
91
+
92
+ def speech_file_to_array_fn(batch):
93
+ batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
94
+ speech_array, sampling_rate = torchaudio.load(batch["path"])
95
+ batch["speech"] = resampler(speech_array).squeeze().numpy()
96
+ return batch
97
+ test_dataset = test_dataset.map(speech_file_to_array_fn)
98
+
99
+
100
+ def evaluate(batch):
101
+ inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
102
+ with torch.no_grad():
103
+ logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
104
+ pred_ids = torch.argmax(logits, dim=-1)
105
+ batch["pred_strings"] = processor.batch_decode(pred_ids)
106
+ return batch
107
+ result = test_dataset.map(evaluate, batched=True, batch_size=8)
108
+ print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"], references=result["sentence"])))
109
+ ```
110
+
111
+ ## Training
112
+
113
+ V1 of the Common Voice `train`, `validation` datasets were used for training.
114
+
115
+ ## Testing
116
+
117
+ V1 of the Common Voice `Test` dataset were used for training.