anuragshas commited on
Commit
f1ff9aa
1 Parent(s): 1aead65

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +130 -0
README.md ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language: te
3
+ datasets:
4
+ - openslr
5
+ metrics:
6
+ - wer
7
+ tags:
8
+ - audio
9
+ - automatic-speech-recognition
10
+ - speech
11
+ - xlsr-fine-tuning-week
12
+ license: apache-2.0
13
+ model-index:
14
+ - name: Anurag Singh XLSR Wav2Vec2 Large 53 Telugu
15
+ results:
16
+ - task:
17
+ name: Speech Recognition
18
+ type: automatic-speech-recognition
19
+ dataset:
20
+ name: OpenSLR te
21
+ type: openslr
22
+ args: te
23
+ metrics:
24
+ - name: Test WER
25
+ type: wer
26
+ value: 44.98
27
+ ---
28
+ # Wav2Vec2-Large-XLSR-53-Telugu
29
+ Fine-tuned [facebook/wav2vec2-large-xlsr-53](https://huggingface.co/facebook/wav2vec2-large-xlsr-53) on Telugu using the [OpenSLR SLR66](http://openslr.org/66/) dataset.
30
+ When using this model, make sure that your speech input is sampled at 16kHz.
31
+ ## Usage
32
+ The model can be used directly (without a language model) as follows:
33
+ ```python
34
+ import torch
35
+ import torchaudio
36
+ from datasets import load_dataset
37
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
38
+ import pandas as pd
39
+
40
+ # Evaluation notebook contains the procedure to download the data
41
+ df = pd.read_csv("/content/te/test.tsv", sep="\t")
42
+ df["path"] = "/content/te/clips/" + df["path"]
43
+ test_dataset = Dataset.from_pandas(df)
44
+
45
+ processor = Wav2Vec2Processor.from_pretrained("anuragshas/wav2vec2-large-xlsr-53-telugu")
46
+ model = Wav2Vec2ForCTC.from_pretrained("anuragshas/wav2vec2-large-xlsr-53-telugu")
47
+
48
+ resampler = torchaudio.transforms.Resample(48_000, 16_000)
49
+ # Preprocessing the datasets.
50
+ # We need to read the aduio files as arrays
51
+ def speech_file_to_array_fn(batch):
52
+ speech_array, sampling_rate = torchaudio.load(batch["path"])
53
+ batch["speech"] = resampler(speech_array).squeeze().numpy()
54
+ return batch
55
+
56
+ test_dataset = test_dataset.map(speech_file_to_array_fn)
57
+ inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
58
+ with torch.no_grad():
59
+ logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
60
+ predicted_ids = torch.argmax(logits, dim=-1)
61
+ print("Prediction:", processor.batch_decode(predicted_ids))
62
+ print("Reference:", test_dataset["sentence"][:2])
63
+ ```
64
+ ## Evaluation
65
+ ```python
66
+ import torch
67
+ import torchaudio
68
+ from datasets import Dataset, load_metric
69
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
70
+ import re
71
+ from sklearn.model_selection import train_test_split
72
+ import pandas as pd
73
+
74
+ # Evaluation notebook contains the procedure to download the data
75
+ df = pd.read_csv("/content/te/test.tsv", sep="\t")
76
+ df["path"] = "/content/te/clips/" + df["path"]
77
+ test_dataset = Dataset.from_pandas(df)
78
+ wer = load_metric("wer")
79
+
80
+ processor = Wav2Vec2Processor.from_pretrained("anuragshas/wav2vec2-large-xlsr-53-telugu")
81
+ model = Wav2Vec2ForCTC.from_pretrained("anuragshas/wav2vec2-large-xlsr-53-telugu")
82
+ model.to("cuda")
83
+
84
+ chars_to_ignore_regex = '[\,\?\.\!\-\_\;\:\"\“\%\‘\”\।\’\'\&]'
85
+ resampler = torchaudio.transforms.Resample(48_000, 16_000)
86
+
87
+ def normalizer(text):
88
+ # Use your custom normalizer
89
+ text = text.replace("\\n","\n")
90
+ text = ' '.join(text.split())
91
+ text = re.sub(r'''([a-z]+)''','',text,flags=re.IGNORECASE)
92
+ text = re.sub(r'''%'''," శాతం ", text)
93
+ text = re.sub(r'''(/|-|_)'''," ", text)
94
+ text = re.sub("ై","ై", text)
95
+ text = text.strip()
96
+ return text
97
+
98
+ def speech_file_to_array_fn(batch):
99
+ batch["sentence"] = normalizer(batch["sentence"])
100
+ batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()+ " "
101
+ speech_array, sampling_rate = torchaudio.load(batch["path"])
102
+ batch["speech"] = resampler(speech_array).squeeze().numpy()
103
+ return batch
104
+ test_dataset = test_dataset.map(speech_file_to_array_fn)
105
+ # Preprocessing the datasets.
106
+ # We need to read the aduio files as arrays
107
+ def evaluate(batch):
108
+ inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
109
+ with torch.no_grad():
110
+ logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
111
+ pred_ids = torch.argmax(logits, dim=-1)
112
+ batch["pred_strings"] = processor.batch_decode(pred_ids)
113
+ return batch
114
+ result = test_dataset.map(evaluate, batched=True, batch_size=8)
115
+ print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"], references=result["sentence"])))
116
+ ```
117
+
118
+ **Test Result**: 44.98%
119
+ ## Training
120
+ 70% of the OpenSLR Marathi dataset was used for training.
121
+
122
+ Train Split of annotations is [here](https://www.dropbox.com/s/xqc0wtour7f9h4c/train.tsv)
123
+
124
+ Test Split of annotations is [here](https://www.dropbox.com/s/qw1uy63oj4qdiu4/test.tsv)
125
+
126
+ Training Data Preparation notebook can be found [here](https://colab.research.google.com/drive/1_VR1QtY9qoiabyXBdJcOI29-xIKGdIzU?usp=sharing)
127
+
128
+ Training notebook can be found[here](https://colab.research.google.com/drive/14N-j4m0Ng_oktPEBN5wiUhDDbyrKYt8I?usp=sharing)
129
+
130
+ Evaluation notebook is [here](https://colab.research.google.com/drive/1SLEvbTWBwecIRTNqpQ0fFTqmr1-7MnSI?usp=sharing)