sakares commited on
Commit
428433e
1 Parent(s): d5f96e0

update model_card

Browse files
Files changed (1) hide show
  1. README.md +137 -0
README.md CHANGED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language: th
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: sakares/wav2vec2-large-xlsr-thai-demo
13
+ results:
14
+ - task:
15
+ name: Speech Recognition
16
+ type: automatic-speech-recognition
17
+ dataset:
18
+ name: Common Voice th
19
+ type: common_voice
20
+ args: {lang_id}
21
+ metrics:
22
+ - name: Test WER
23
+ type: wer
24
+ value: 44.46
25
+ ---
26
+
27
+ # Wav2Vec2-Large-XLSR-53-Thai
28
+
29
+ Fine-tuned [facebook/wav2vec2-large-xlsr-53](https://huggingface.co/facebook/wav2vec2-large-xlsr-53) in Thai 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
+ from pythainlp.tokenize import word_tokenize
42
+
43
+ test_dataset = load_dataset("common_voice", "th", split="test[:2%]").
44
+
45
+ processor = Wav2Vec2Processor.from_pretrained("sakares/wav2vec2-large-xlsr-thai-demo")
46
+ model = Wav2Vec2ForCTC.from_pretrained("sakares/wav2vec2-large-xlsr-thai-demo")
47
+
48
+ resampler = torchaudio.transforms.Resample(48_000, 16_000)
49
+
50
+ ## For Thai NLP Library, please feel free to check https://pythainlp.github.io/docs/2.2/api/tokenize.html
51
+ def th_tokenize(batch):
52
+ batch["sentence"] = " ".join(word_tokenize(batch["sentence"], engine="newmm"))
53
+ return batch
54
+
55
+ # Preprocessing the datasets.
56
+ # We need to read the aduio files as arrays
57
+ def speech_file_to_array_fn(batch):
58
+ speech_array, sampling_rate = torchaudio.load(batch["path"])
59
+ batch["speech"] = resampler(speech_array).squeeze().numpy()
60
+ return batch
61
+
62
+ test_dataset = test_dataset.map(speech_file_to_array_fn)
63
+ inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
64
+
65
+ with torch.no_grad():
66
+ logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
67
+
68
+ predicted_ids = torch.argmax(logits, dim=-1)
69
+
70
+ print("Prediction:", processor.batch_decode(predicted_ids))
71
+ print("Reference:", test_dataset["sentence"][:2])
72
+ ```
73
+ Usage script [here](https://colab.research.google.com/drive/1w0VywsBtjrO2pHHPmiPugYI9yeF8nUKg?usp=sharing)
74
+
75
+ ## Evaluation
76
+
77
+ The model can be evaluated as follows on the {language} test data of Common Voice.
78
+
79
+
80
+ ```python
81
+ import torch
82
+ import torchaudio
83
+ from datasets import load_dataset, load_metric
84
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
85
+ from pythainlp.tokenize import word_tokenize
86
+ import re
87
+
88
+ test_dataset = load_dataset("common_voice", "th", split="test")
89
+ wer = load_metric("wer")
90
+
91
+ processor = Wav2Vec2Processor.from_pretrained("sakares/wav2vec2-large-xlsr-thai-demo")
92
+ model = Wav2Vec2ForCTC.from_pretrained("sakares/wav2vec2-large-xlsr-thai-demo")
93
+ model.to("cuda")
94
+
95
+ chars_to_ignore_regex = '[\,\?\.\!\-\;\:\"\“]'
96
+ resampler = torchaudio.transforms.Resample(48_000, 16_000)
97
+
98
+ ## For Thai NLP Library, please feel free to check https://pythainlp.github.io/docs/2.2/api/tokenize.html
99
+ def th_tokenize(batch):
100
+ batch["sentence"] = " ".join(word_tokenize(batch["sentence"], engine="newmm"))
101
+ return batch
102
+
103
+ # Preprocessing the datasets.
104
+ # We need to read the aduio files as arrays
105
+ def speech_file_to_array_fn(batch):
106
+ batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
107
+ speech_array, sampling_rate = torchaudio.load(batch["path"])
108
+ batch["speech"] = resampler(speech_array).squeeze().numpy()
109
+ return batch
110
+
111
+ test_dataset = test_dataset.map(speech_file_to_array_fn).map(th_tokenize)
112
+
113
+ # Preprocessing the datasets.
114
+ # We need to read the aduio files as arrays
115
+ def evaluate(batch):
116
+ inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
117
+
118
+ with torch.no_grad():
119
+ logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
120
+
121
+ pred_ids = torch.argmax(logits, dim=-1)
122
+ batch["pred_strings"] = processor.batch_decode(pred_ids)
123
+ return batch
124
+
125
+ result = test_dataset.map(evaluate, batched=True, batch_size=8).map(th_tokenize)
126
+
127
+ print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"], references=result["sentence"])))
128
+ ```
129
+
130
+ **Test Result**: 44.46 %
131
+ Evaluate script [here](https://colab.research.google.com/drive/1WZGtHKWXBztRsuXHIdebf6uoAsp7rTnK?usp=sharing)
132
+
133
+ ## Training
134
+
135
+ The Common Voice `train`, `validation` datasets were used for training.
136
+
137
+ The script used for training can be found [here](https://colab.research.google.com/drive/18oUbeZgBGSkz16zC_WOa154QZOdmvjyt?usp=sharing)