Adam Montgomerie commited on
Commit
5b8770d
1 Parent(s): 45e78af

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +122 -0
README.md ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language: fy-NL
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: XLSR Wav2Vec2 Frisian by Adam Montgomerie
13
+ results:
14
+ - task:
15
+ name: Speech Recognition
16
+ type: automatic-speech-recognition
17
+ dataset:
18
+ name: Common Voice fy-NL
19
+ type: common_voice
20
+ args: {lang_id}
21
+ metrics:
22
+ - name: Test WER
23
+ type: wer
24
+ value: 21.72
25
+ ---
26
+
27
+ # Wav2Vec2-Large-XLSR-53-Frisian
28
+
29
+ Fine-tuned [facebook/wav2vec2-large-xlsr-53](https://huggingface.co/facebook/wav2vec2-large-xlsr-53) in Frisian using the [Common Voice](https://huggingface.co/datasets/common_voice)
30
+ When using this model, make sure that your speech input is sampled at 16kHz.
31
+
32
+ ## Usage
33
+
34
+ The model can be used directly (without a language model) as follows:
35
+
36
+ ```python
37
+ import torch
38
+ import torchaudio
39
+ from datasets import load_dataset
40
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
41
+
42
+ test_dataset = load_dataset("common_voice", "fy-NL", split="test[:2%]").
43
+
44
+ processor = Wav2Vec2Processor.from_pretrained("iarfmoose/wav2vec2-large-xlsr-frisian")
45
+ model = Wav2Vec2ForCTC.from_pretrained("iarfmoose/wav2vec2-large-xlsr-frisian")
46
+
47
+ resampler = torchaudio.transforms.Resample(48_000, 16_000)
48
+
49
+
50
+ def speech_file_to_array_fn(batch):
51
+ speech_array, sampling_rate = torchaudio.load(batch["path"])
52
+ tbatch["speech"] = resampler(speech_array).squeeze().numpy()
53
+ return batch
54
+
55
+ test_dataset = test_dataset.map(speech_file_to_array_fn)
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
+
68
+ ## Evaluation
69
+
70
+ The model can be evaluated as follows on the Frisian test data of Common Voice.
71
+
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", "fy-NL", split="test")
81
+ wer = load_metric("wer")
82
+
83
+ processor = Wav2Vec2Processor.from_pretrained("iarfmoose/wav2vec2-large-xlsr-frisian")
84
+ model = Wav2Vec2ForCTC.from_pretrained("iarfmoose/wav2vec2-large-xlsr-frisian")
85
+ model.to("cuda")
86
+
87
+ chars_to_ignore_regex = '[\,\?\.\!\-\;\:\"\“\%\‘\”\�\–\—\¬\⅛]'
88
+ resampler = torchaudio.transforms.Resample(48_000, 16_000)
89
+
90
+ def speech_file_to_array_fn(batch):
91
+ batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
92
+ speech_array, sampling_rate = torchaudio.load(batch["path"])
93
+ batch["speech"] = resampler(speech_array).squeeze().numpy()
94
+ return batch
95
+
96
+ test_dataset = test_dataset.map(speech_file_to_array_fn)
97
+
98
+ def evaluate(batch):
99
+ inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
100
+
101
+ with torch.no_grad():
102
+ logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
103
+
104
+ pred_ids = torch.argmax(logits, dim=-1)
105
+ batch["pred_strings"] = processor.batch_decode(pred_ids)
106
+ return batch
107
+
108
+ result = test_dataset.map(evaluate, batched=True, batch_size=8)
109
+
110
+ print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"], references=result["sentence"])))
111
+ ```
112
+
113
+ **Test Result**: 21.72 %
114
+
115
+
116
+ ## Training
117
+
118
+ The Common Voice `train`, `validation` datasets were used for training.
119
+
120
+ The script used for training can be found [here](https://github.com/AMontgomerie/wav2vec2-xlsr/blob/main/Frisian/XLSR_Frisian.ipynb)
121
+
122
+ A notebook of the evaluation script can be found [here](https://github.com/AMontgomerie/wav2vec2-xlsr/blob/main/Frisian/wav2vec2_fyNL_eval.ipynb)