Alex Leu commited on
Commit
e599383
1 Parent(s): ed8bb41
Files changed (1) hide show
  1. README.md +99 -0
README.md ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language: pl
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-polish by Alex Leu
13
+ results:
14
+ - task:
15
+ name: Speech Recognition
16
+ type: automatic-speech-recognition
17
+ dataset:
18
+ name: Common Voice pl
19
+ type: common_voice
20
+ args: pl
21
+ metrics:
22
+ - name: Test WER
23
+ type: wer
24
+ value:xxx
25
+ ---
26
+ # wav2vec2-large-xlsr-polish
27
+ Fine-tuned [facebook/wav2vec2-large-xlsr-53](https://huggingface.co/facebook/wav2vec2-large-xlsr-53) in Polish using the [Common Voice](https://huggingface.co/datasets/common_voice)
28
+ When using this model, make sure that your speech input is sampled at 16kHz.
29
+ ## Usage
30
+ The model can be used directly (without a language model) as follows:
31
+ ```python
32
+ import torch
33
+ import torchaudio
34
+ from datasets import load_dataset
35
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
36
+ test_dataset = load_dataset("common_voice", "pl", split="test[:2%]")
37
+ processor = Wav2Vec2Processor.from_pretrained("alexcleu/wav2vec2-large-xlsr-polish")
38
+ model = Wav2Vec2ForCTC.from_pretrained("alexcleu/wav2vec2-large-xlsr-polish")
39
+ resampler = torchaudio.transforms.Resample(48_000, 16_000)
40
+ # Preprocessing the datasets.
41
+ # We need to read the aduio files as arrays
42
+ def speech_file_to_array_fn(batch):
43
+ speech_array, sampling_rate = torchaudio.load(batch["path"])
44
+ batch["speech"] = resampler(speech_array).squeeze().numpy()
45
+ return batch
46
+ test_dataset = test_dataset.map(speech_file_to_array_fn)
47
+ inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
48
+ with torch.no_grad():
49
+ logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
50
+ predicted_ids = torch.argmax(logits, dim=-1)
51
+ print("Prediction:", processor.batch_decode(predicted_ids))
52
+ print("Reference:", test_dataset["sentence"][:2])
53
+ ```
54
+ ## Evaluation
55
+ The model can be evaluated as follows on the Turkish test data of Common Voice.
56
+ ```python
57
+ import torch
58
+ import torchaudio
59
+ from datasets import load_dataset, load_metric
60
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
61
+ import re
62
+ test_dataset = load_dataset("common_voice", "pl", split="test")
63
+ wer = load_metric("wer")
64
+ processor = Wav2Vec2Processor.from_pretrained("alexcleu/wav2vec2-large-xlsr-polish")
65
+ model = Wav2Vec2ForCTC.from_pretrained("alexcleu/wav2vec2-large-xlsr-polish")
66
+ model.to("cuda")
67
+ chars_to_ignore_regex = '[\\,\\?\\.\\!\\-\\;\\:\\"\\“]'
68
+ resampler = torchaudio.transforms.Resample(48_000, 16_000)
69
+ # Preprocessing the datasets.
70
+ # We need to read the aduio files as arrays
71
+ def speech_file_to_array_fn(batch):
72
+ batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
73
+
74
+ speech_array, sampling_rate = torchaudio.load(batch["path"])
75
+
76
+ batch["speech"] = resampler(speech_array).squeeze().numpy()
77
+
78
+ return batch
79
+ test_dataset = test_dataset.map(speech_file_to_array_fn)
80
+ # Preprocessing the datasets.
81
+ # We need to read the aduio files as arrays
82
+ def evaluate(batch):
83
+ inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
84
+
85
+ with torch.no_grad():
86
+
87
+ logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
88
+
89
+ pred_ids = torch.argmax(logits, dim=-1)
90
+
91
+ batch["pred_strings"] = processor.batch_decode(pred_ids)
92
+
93
+ return batch
94
+ result = test_dataset.map(evaluate, batched=True, batch_size=8)
95
+ print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"], references=result["sentence"])))
96
+ ```
97
+ **Test Result**: xxx
98
+ ## Training
99
+ The Common Voice `train`, `validation` datasets were used for training.