manandey commited on
Commit
0c2d8e0
1 Parent(s): 490eacd

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +91 -0
README.md ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language: pa-IN
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 Punjabi by Manan Dey
13
+ results:
14
+ - task:
15
+ name: Speech Recognition
16
+ type: automatic-speech-recognition
17
+ dataset:
18
+ name: Common Voice pa-IN
19
+ type: common_voice
20
+ args: pa-IN
21
+ metrics:
22
+ - name: Test WER
23
+ type: wer
24
+ value: 57.31
25
+ ---
26
+ # Wav2Vec2-Large-XLSR-53-Punjabi
27
+ Fine-tuned [facebook/wav2vec2-large-xlsr-53](https://huggingface.co/facebook/wav2vec2-large-xlsr-53) in Punjabi 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", "pa-IN", split="test[:2%]").
37
+ processor = Wav2Vec2Processor.from_pretrained("manandey/wav2vec2-large-xlsr-punjabi")
38
+ model = Wav2Vec2ForCTC.from_pretrained("manandey/wav2vec2-large-xlsr-punjabi")
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 {language} 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", "pa-IN", split="test")
63
+ wer = load_metric("wer")
64
+ processor = Wav2Vec2Processor.from_pretrained("manandey/wav2vec2-large-xlsr-punjabi")
65
+ model = Wav2Vec2ForCTC.from_pretrained("manandey/wav2vec2-large-xlsr-punjabi")
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
+ speech_array, sampling_rate = torchaudio.load(batch["path"])
74
+ batch["speech"] = resampler(speech_array).squeeze().numpy()
75
+ return batch
76
+ test_dataset = test_dataset.map(speech_file_to_array_fn)
77
+ # Preprocessing the datasets.
78
+ # We need to read the aduio files as arrays
79
+ def evaluate(batch):
80
+ inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
81
+ with torch.no_grad():
82
+ logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
83
+ pred_ids = torch.argmax(logits, dim=-1)
84
+ batch["pred_strings"] = processor.batch_decode(pred_ids)
85
+ return batch
86
+ result = test_dataset.map(evaluate, batched=True, batch_size=8)
87
+ print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"], references=result["sentence"])))
88
+ ```
89
+ **Test Result**: 57.31%
90
+ ## Training
91
+ The Common Voice `train`, `validation` datasets were used for training.