jimregan commited on
Commit
5759c6a
1 Parent(s): 3b7d661

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +109 -0
README.md ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language: hsb
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 Upper Sorbian mixed by Jim O'Regan
13
+ results:
14
+ - task:
15
+ name: Speech Recognition
16
+ type: automatic-speech-recognition
17
+ dataset:
18
+ name: Common Voice hsb
19
+ type: common_voice
20
+ args: {lang_id}
21
+ metrics:
22
+ - name: Test WER
23
+ type: wer
24
+ value: 43.48
25
+ ---
26
+
27
+ # Wav2Vec2-Large-XLSR-53-Sorbian
28
+
29
+ Fine-tuned [facebook/wav2vec2-large-xlsr-53](https://huggingface.co/facebook/wav2vec2-large-xlsr-53) in Sorbian 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
+ test_dataset = load_dataset("common_voice", "hsb", split="test[:2%]")
42
+ processor = Wav2Vec2Processor.from_pretrained("jimregan/wav2vec2-large-xlsr-upper-sorbian-mixed")
43
+ model = Wav2Vec2ForCTC.from_pretrained("jimregan/wav2vec2-large-xlsr-upper-sorbian-mixed")
44
+ resampler = torchaudio.transforms.Resample(48_000, 16_000)
45
+ # Preprocessing the datasets.
46
+ # We need to read the audio files as arrays
47
+ def speech_file_to_array_fn(batch):
48
+ speech_array, sampling_rate = torchaudio.load(batch["path"])
49
+ batch["speech"] = resampler(speech_array).squeeze().numpy()
50
+ return batch
51
+ test_dataset = test_dataset.map(speech_file_to_array_fn)
52
+ inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
53
+ with torch.no_grad():
54
+ logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
55
+ predicted_ids = torch.argmax(logits, dim=-1)
56
+ print("Prediction:", processor.batch_decode(predicted_ids))
57
+ print("Reference:", test_dataset["sentence"][:2])
58
+ ```
59
+
60
+
61
+ ## Evaluation
62
+
63
+ The model can be evaluated as follows on the Upper Sorbian test data of Common Voice.
64
+
65
+
66
+ ```python
67
+ import torch
68
+ import torchaudio
69
+ from datasets import load_dataset, load_metric
70
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
71
+ import re
72
+ test_dataset = load_dataset("common_voice", "ga-IE", split="test")
73
+ wer = load_metric("wer")
74
+ processor = Wav2Vec2Processor.from_pretrained("jimregan/wav2vec2-large-xlsr-upper-sorbian-mixed")
75
+ model = Wav2Vec2ForCTC.from_pretrained("jimregan/wav2vec2-large-xlsr-upper-sorbian-mixed")
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 audio files as arrays
81
+ def speech_file_to_array_fn(batch):
82
+ batch["sentence"] = remove_special_characters(batch["sentence"])
83
+ speech_array, sampling_rate = torchaudio.load(batch["path"])
84
+ batch["speech"] = resampler(speech_array).squeeze().numpy()
85
+ return batch
86
+ test_dataset = test_dataset.map(speech_file_to_array_fn)
87
+ # Preprocessing the datasets.
88
+ # We need to read the audio files as arrays
89
+ def evaluate(batch):
90
+ inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
91
+ with torch.no_grad():
92
+ logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
93
+ pred_ids = torch.argmax(logits, dim=-1)
94
+ batch["pred_strings"] = processor.batch_decode(pred_ids)
95
+ return batch
96
+ result = test_dataset.map(evaluate, batched=True, batch_size=8)
97
+ print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"], references=result["sentence"])))
98
+ ```
99
+
100
+ **Test Result**: 48.2 %
101
+
102
+
103
+ ## Training
104
+
105
+ The Common Voice `train` and `validation` datasets were used for training, with the vocabulary from the English A1 lesson from an online [Sorbian course](https://sprachkurs.sorbischlernen.de/)
106
+
107
+ The script used for training can be found [here](https://github.com/jimregan/wav2vec2-sprint/blob/main/upper_sorbian/fine-tune-xlsr-wav2vec2-on-upper-sorbian-asr-with-transformers.ipynb)
108
+
109
+ The script used for cleaning the transcripts of the vocabulary data is [here](https://github.com/jimregan/wav2vec2-sprint/blob/main/upper_sorbian/sprachkurs.ipynb)