holylovenia commited on
Commit
8a88000
1 Parent(s): 079dafa

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +134 -0
README.md CHANGED
@@ -1,3 +1,137 @@
1
  ---
 
 
 
 
 
 
 
 
 
 
 
 
2
  license: apache-2.0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ language:
3
+ - zh-HK
4
+ - yue
5
+ datasets:
6
+ - common_voice
7
+ metrics:
8
+ - cer
9
+ tags:
10
+ - audio
11
+ - automatic-speech-recognition
12
+ - speech
13
+ - xlsr-fine-tuning-week
14
  license: apache-2.0
15
+ model-index:
16
+ - name: Wav2Vec2-Large-XLSR-53-Cantonese
17
+ results:
18
+ - task:
19
+ name: Speech Recognition
20
+ type: automatic-speech-recognition
21
+ dataset:
22
+ name: Common Voice zh-HK
23
+ type: common_voice
24
+ args: zh-HK
25
+ metrics:
26
+ - name: Test CER
27
+ type: cer
28
+ value: [18.55%]
29
  ---
30
+
31
+ # Wav2Vec2-Large-XLSR-53-Cantonese
32
+
33
+ Fine-tuned [facebook/wav2vec2-large-xlsr-53](https://huggingface.co/facebook/wav2vec2-large-xlsr-53) on Cantonese using the [Common Voice](https://huggingface.co/datasets/common_voice).
34
+ When using this model, make sure that your speech input is sampled at 16kHz.
35
+
36
+ The Common Voice `train` and `dev` were used for training.
37
+
38
+ The script used for training can be found at [https://github.com/holylovenia/wav2vec2-pretraining](https://github.com/holylovenia/wav2vec2-pretraining).
39
+
40
+ ## Usage
41
+ The model can be used directly (without a language model) as follows:
42
+ ```python
43
+ import torch
44
+ import torchaudio
45
+ from datasets import load_dataset
46
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
47
+
48
+ test_dataset = load_dataset("common_voice", "zh-HK", split="test[:2%]")
49
+
50
+ processor = Wav2Vec2Processor.from_pretrained("CAiRE/wav2vec2-large-xlsr-53-cantonese")
51
+ model = Wav2Vec2ForCTC.from_pretrained("CAiRE/wav2vec2-large-xlsr-53-cantonese")
52
+
53
+
54
+ # Preprocessing the datasets.
55
+ # We need to read the aduio files as arrays
56
+ def speech_file_to_array_fn(batch):
57
+ speech_array, sampling_rate = torchaudio.load(batch["path"])
58
+ resampler = torchaudio.transforms.Resample(sampling_rate, 16_000)
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[:2]["speech"], 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[:2]["sentence"])
72
+ ```
73
+
74
+
75
+ ## Evaluation
76
+
77
+ The model can be evaluated as follows on the zh-HK test data of Common Voice.
78
+
79
+ ```python
80
+ import torch
81
+ import torchaudio
82
+ from datasets import load_dataset, load_metric
83
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
84
+ import re
85
+
86
+ test_dataset = load_dataset("common_voice", "zh-HK", split="test")
87
+ wer = load_metric("cer")
88
+
89
+ processor = Wav2Vec2Processor.from_pretrained("CAiRE/wav2vec2-large-xlsr-53-cantonese")
90
+ model = Wav2Vec2ForCTC.from_pretrained("CAiRE/wav2vec2-large-xlsr-53-cantonese")
91
+ model.to("cuda")
92
+
93
+ chars_to_ignore_regex = '[\,\?\.\!\-\;\:\"\“\%\‘\'\”\�]'
94
+
95
+
96
+ # Preprocessing the datasets.
97
+ # We need to read the aduio files as arrays
98
+ def speech_file_to_array_fn(batch):
99
+ batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
100
+ speech_array, sampling_rate = torchaudio.load(batch["path"])
101
+ resampler = torchaudio.transforms.Resample(sampling_rate, 16_000)
102
+ batch["speech"] = resampler(speech_array).squeeze().numpy()
103
+ return batch
104
+
105
+ test_dataset = test_dataset.map(speech_file_to_array_fn)
106
+
107
+ # Preprocessing the datasets.
108
+ # We need to read the aduio files as arrays
109
+ def evaluate(batch):
110
+ inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
111
+
112
+ with torch.no_grad():
113
+ logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
114
+
115
+ pred_ids = torch.argmax(logits, dim=-1)
116
+ batch["pred_strings"] = processor.batch_decode(pred_ids)
117
+ return batch
118
+
119
+ result = test_dataset.map(evaluate, batched=True, batch_size=8)
120
+
121
+ print("CER: {:2f}".format(100 * cer.compute(predictions=result["pred_strings"], references=result["sentence"])))
122
+ ```
123
+
124
+ **Test Result**: CER: 18.55 %
125
+
126
+ ## Citation
127
+
128
+ If you use our code/model, please cite us:
129
+
130
+ ```
131
+ @inproceedings{lovenia2022ascend,
132
+ title={ASCEND: A Spontaneous Chinese-English Dataset for Code-switching in Multi-turn Conversation},
133
+ author={Lovenia, Holy and Cahyawijaya, Samuel and Winata, Genta Indra and Xu, Peng and Yan, Xu and Liu, Zihan and Frieske, Rita and Yu, Tiezheng and Dai, Wenliang and Barezi, Elham J and others},
134
+ booktitle={Proceedings of the 13th Language Resources and Evaluation Conference (LREC)},
135
+ year={2022}
136
+ }
137
+ ```