voidful commited on
Commit
93533d4
0 Parent(s):

Initial commit

Browse files
.gitattributes ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.bin.* filter=lfs diff=lfs merge=lfs -text
2
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.h5 filter=lfs diff=lfs merge=lfs -text
5
+ *.tflite filter=lfs diff=lfs merge=lfs -text
6
+ *.tar.gz filter=lfs diff=lfs merge=lfs -text
7
+ *.ot filter=lfs diff=lfs merge=lfs -text
8
+ *.onnx filter=lfs diff=lfs merge=lfs -text
9
+ *.arrow filter=lfs diff=lfs merge=lfs -text
10
+ *.ftz filter=lfs diff=lfs merge=lfs -text
11
+ *.joblib filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.pb filter=lfs diff=lfs merge=lfs -text
15
+ *.pt filter=lfs diff=lfs merge=lfs -text
16
+ *.pth filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,181 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language: zh
3
+ datasets:
4
+ - common_voice
5
+ tags:
6
+ - speech
7
+ - audio
8
+ - automatic-speech-recognition
9
+ - xlsr_fine_tuning_week
10
+ license: apache-2.0
11
+ ---
12
+
13
+ ## Colab trial with recording or voice file
14
+ [Colab trial](https://colab.research.google.com/drive/1e_z5jQHYbO2YKEaUgzb1ww1WwiAyydAj?usp=sharing)
15
+
16
+ ```
17
+ import torchaudio
18
+ from datasets import load_dataset, load_metric
19
+ from transformers import (
20
+ Wav2Vec2ForCTC,
21
+ Wav2Vec2Processor,
22
+ )
23
+ import torch
24
+ import re
25
+ import sys
26
+
27
+ model_name = "voidful/wav2vec2-large-xlsr-53-tw"
28
+ device = "cuda"
29
+ processor_name = "voidful/wav2vec2-large-xlsr-53-tw"
30
+
31
+ chars_to_ignore_regex = r"[¥•"#$%&'()*+,-/:;<=>@[\]^_`{|}~⦅⦆「」、 、〃〈〉《》「」『』【】〔〕〖〗〘〙〚〛〜〝〞〟〰〾〿–—‘’‛“”„‟…‧﹏﹑﹔·'℃°•·.﹑︰〈〉─《﹖﹣﹂﹁﹔!?。。"#$%&'()*+,﹐-/:;<=>@[\]^_`{|}~⦅⦆「」、、〃》「」『』【】〔〕〖〗〘〙〚〛〜〝〞〟〰〾〿–—‘’‛“”„‟…‧﹏..!\"#$%&()*+,\-.\:;<=>?@\[\]\\\/^_`{|}~]"
32
+
33
+ model = Wav2Vec2ForCTC.from_pretrained(model_name).to(device)
34
+ processor = Wav2Vec2Processor.from_pretrained(processor_name)
35
+
36
+ resampler = torchaudio.transforms.Resample(orig_freq=48_000, new_freq=16_000)
37
+
38
+ def load_file_to_data(file):
39
+ batch = {}
40
+ speech, _ = torchaudio.load(file)
41
+ batch["speech"] = resampler.forward(speech.squeeze(0)).numpy()
42
+ batch["sampling_rate"] = resampler.new_freq
43
+ return batch
44
+
45
+
46
+ def predict(data):
47
+ features = processor(data["speech"], sampling_rate=data["sampling_rate"], padding=True, return_tensors="pt")
48
+ input_values = features.input_values.to(device)
49
+ attention_mask = features.attention_mask.to(device)
50
+ with torch.no_grad():
51
+ logits = model(input_values, attention_mask=attention_mask).logits
52
+ pred_ids = torch.argmax(logits, dim=-1)
53
+ return processor.batch_decode(pred_ids)
54
+
55
+ ```
56
+
57
+ Predict
58
+ ```python
59
+ predict(load_file_to_data('voice file path'))
60
+ ```
61
+
62
+ ## Evaluation on Common Voice TW Test
63
+ ```python
64
+ import torchaudio
65
+ from datasets import load_dataset, load_metric
66
+ from transformers import (
67
+ Wav2Vec2ForCTC,
68
+ Wav2Vec2Processor,
69
+ )
70
+ import torch
71
+ import re
72
+
73
+ model_name = "voidful/wav2vec2-large-xlsr-53-tw"
74
+ device = "cuda"
75
+ processor_name = "voidful/wav2vec2-large-xlsr-53-tw"
76
+
77
+ chars_to_ignore_regex = r"[¥•"#$%&'()*+,-/:;<=>@[\]^_`{|}~⦅⦆「」、 、〃〈〉《》「」『』【】〔〕〖〗〘〙〚〛〜〝〞〟〰〾〿–—‘’‛“”„‟…‧﹏﹑﹔·'℃°•·.﹑︰〈〉─《﹖﹣﹂﹁﹔!?。。"#$%&'()*+,﹐-/:;<=>@[\]^_`{|}~⦅⦆「」、、〃》「」『』【】〔〕〖〗〘〙〚〛〜〝〞〟〰〾〿–—‘’‛“”„‟…‧﹏..!\"#$%&()*+,\-.\:;<=>?@\[\]\\\/^_`{|}~]"
78
+
79
+ model = Wav2Vec2ForCTC.from_pretrained(model_name).to(device)
80
+ processor = Wav2Vec2Processor.from_pretrained(processor_name)
81
+
82
+ ds = load_dataset("common_voice", 'zh-TW', data_dir="./cv-corpus-6.1-2020-12-11", split="test")
83
+
84
+ resampler = torchaudio.transforms.Resample(orig_freq=48_000, new_freq=16_000)
85
+
86
+ def map_to_array(batch):
87
+ speech, _ = torchaudio.load(batch["path"])
88
+ batch["speech"] = resampler.forward(speech.squeeze(0)).numpy()
89
+ batch["sampling_rate"] = resampler.new_freq
90
+ batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower().replace("’", "'")
91
+ return batch
92
+
93
+ ds = ds.map(map_to_array)
94
+
95
+ def map_to_pred(batch):
96
+ features = processor(batch["speech"], sampling_rate=batch["sampling_rate"][0], padding=True, return_tensors="pt")
97
+ input_values = features.input_values.to(device)
98
+ attention_mask = features.attention_mask.to(device)
99
+ with torch.no_grad():
100
+ logits = model(input_values, attention_mask=attention_mask).logits
101
+ pred_ids = torch.argmax(logits, dim=-1)
102
+ batch["predicted"] = processor.batch_decode(pred_ids)
103
+ batch["target"] = batch["sentence"]
104
+ return batch
105
+
106
+ result = ds.map(map_to_pred, batched=True, batch_size=16, remove_columns=list(ds.features.keys()))
107
+
108
+ wer = load_metric("wer")
109
+
110
+ print(wer.compute(predictions=result["predicted"], references=result["target"]))
111
+ ```
112
+
113
+ `CER: 0.8635578583765112`
114
+
115
+ Inference with GPT LM:
116
+ ```python
117
+ import torchaudio
118
+ from datasets import load_dataset, load_metric
119
+ from transformers import (
120
+ Wav2Vec2ForCTC,
121
+ Wav2Vec2Processor,
122
+ )
123
+ import torch
124
+ import re
125
+ from transformers import AutoTokenizer, AutoModelWithLMHead
126
+
127
+ model_name = "voidful/wav2vec2-large-xlsr-53-tw"
128
+ device = "cuda"
129
+ processor_name = "voidful/wav2vec2-large-xlsr-53-tw"
130
+
131
+ chars_to_ignore_regex = r"[¥•"#$%&'()*+,-/:;<=>@[\]^_`{|}~⦅⦆「」、 、〃〈〉《》「」『』【】〔〕〖〗〘〙〚〛〜〝〞���〰〾〿–—‘’‛“”„‟…‧﹏﹑﹔·'℃°•·.﹑︰〈〉─《﹖﹣﹂﹁﹔!?。。"#$%&'()*+,﹐-/:;<=>@[\]^_`{|}~⦅⦆「」、、〃》「」『』【】〔〕〖〗〘〙〚〛〜〝〞〟〰〾〿–—‘’‛“”„‟…‧﹏..!\"#$%&()*+,\-.\:;<=>?@\[\]\\\/^_`{|}~]"
132
+
133
+ tokenizer = AutoTokenizer.from_pretrained("ckiplab/gpt2-base-chinese")
134
+ gpt_model = AutoModelWithLMHead.from_pretrained("ckiplab/gpt2-base-chinese").to(device)
135
+ model = Wav2Vec2ForCTC.from_pretrained(model_name).to(device)
136
+ processor = Wav2Vec2Processor.from_pretrained(processor_name)
137
+
138
+ ds = load_dataset("common_voice", 'zh-TW', data_dir="./cv-corpus-6.1-2020-12-11", split="test")
139
+
140
+ resampler = torchaudio.transforms.Resample(orig_freq=48_000, new_freq=16_000)
141
+
142
+ def map_to_array(batch):
143
+ speech, _ = torchaudio.load(batch["path"])
144
+ batch["speech"] = resampler.forward(speech.squeeze(0)).numpy()
145
+ batch["sampling_rate"] = resampler.new_freq
146
+ batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower().replace("’", "'")
147
+ return batch
148
+
149
+ ds = ds.map(map_to_array)
150
+
151
+ def map_to_pred(batch):
152
+ features = processor(batch["speech"], sampling_rate=batch["sampling_rate"][0], padding=True, return_tensors="pt")
153
+ input_values = features.input_values.to(device)
154
+ attention_mask = features.attention_mask.to(device)
155
+ with torch.no_grad():
156
+ logits = model(input_values, attention_mask=attention_mask).logits
157
+
158
+ decoded_results = []
159
+ for logit in logits:
160
+ pred_ids = torch.argmax(logit, dim=-1)
161
+ mask = pred_ids.ge(1).unsqueeze(-1).expand(logit.size())
162
+ vocab_size = logit.size()[-1]
163
+ voice_prob = torch.nn.functional.softmax((torch.masked_select(logit, mask).view(-1,vocab_size)),dim=-1)
164
+ gpt_input = torch.cat((torch.tensor([tokenizer.cls_token_id]).to(device),pred_ids[pred_ids>0]), 0)
165
+ gpt_prob = torch.nn.functional.softmax(gpt_model(gpt_input).logits, dim=-1)[:voice_prob.size()[0],:]
166
+ comb_pred_ids = torch.argmax(gpt_prob*voice_prob, dim=-1)
167
+ decoded_results.append(processor.decode(comb_pred_ids))
168
+
169
+ batch["predicted"] = decoded_results
170
+ batch["target"] = batch["sentence"]
171
+ return batch
172
+
173
+
174
+ result = ds.map(map_to_pred, batched=True, batch_size=16, remove_columns=list(ds.features.keys()))
175
+
176
+ wer = load_metric("wer")
177
+
178
+ print(wer.compute(predictions=result["predicted"], references=result["target"]))
179
+ ```
180
+
181
+ `CER 0.7927461139896373`
config.json ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "facebook/wav2vec2-large-xlsr-53",
3
+ "activation_dropout": 0.0,
4
+ "apply_spec_augment": true,
5
+ "architectures": [
6
+ "Wav2Vec2ForCTC"
7
+ ],
8
+ "attention_dropout": 0.1,
9
+ "bos_token_id": 1,
10
+ "conv_bias": true,
11
+ "conv_dim": [
12
+ 512,
13
+ 512,
14
+ 512,
15
+ 512,
16
+ 512,
17
+ 512,
18
+ 512
19
+ ],
20
+ "conv_kernel": [
21
+ 10,
22
+ 3,
23
+ 3,
24
+ 3,
25
+ 3,
26
+ 2,
27
+ 2
28
+ ],
29
+ "conv_stride": [
30
+ 5,
31
+ 2,
32
+ 2,
33
+ 2,
34
+ 2,
35
+ 2,
36
+ 2
37
+ ],
38
+ "ctc_loss_reduction": "mean",
39
+ "ctc_zero_infinity": false,
40
+ "do_stable_layer_norm": true,
41
+ "eos_token_id": 2,
42
+ "feat_extract_activation": "gelu",
43
+ "feat_extract_dropout": 0.0,
44
+ "feat_extract_norm": "layer",
45
+ "feat_proj_dropout": 0.0,
46
+ "final_dropout": 0.0,
47
+ "gradient_checkpointing": true,
48
+ "hidden_act": "gelu",
49
+ "hidden_dropout": 0.1,
50
+ "hidden_size": 1024,
51
+ "initializer_range": 0.02,
52
+ "intermediate_size": 4096,
53
+ "layer_norm_eps": 1e-05,
54
+ "layerdrop": 0.1,
55
+ "mask_channel_length": 10,
56
+ "mask_channel_min_space": 1,
57
+ "mask_channel_other": 0.0,
58
+ "mask_channel_prob": 0.0,
59
+ "mask_channel_selection": "static",
60
+ "mask_feature_length": 10,
61
+ "mask_feature_prob": 0.0,
62
+ "mask_time_length": 10,
63
+ "mask_time_min_space": 1,
64
+ "mask_time_other": 0.0,
65
+ "mask_time_prob": 0.05,
66
+ "mask_time_selection": "static",
67
+ "model_type": "wav2vec2",
68
+ "num_attention_heads": 16,
69
+ "num_conv_pos_embedding_groups": 16,
70
+ "num_conv_pos_embeddings": 128,
71
+ "num_feat_extract_layers": 7,
72
+ "num_hidden_layers": 24,
73
+ "pad_token_id": 0,
74
+ "transformers_version": "4.4.0.dev0",
75
+ "vocab_size": 21128
76
+ }
feature_extractor_config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "do_normalize": true,
3
+ "feature_size": 1,
4
+ "padding_side": "right",
5
+ "padding_value": 0.0,
6
+ "return_attention_mask": true,
7
+ "sampling_rate": 16000
8
+ }
9
+
preprocessor_config.json ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "do_normalize": true,
3
+ "feature_size": 1,
4
+ "padding_side": "right",
5
+ "padding_value": 0.0,
6
+ "return_attention_mask": true,
7
+ "sampling_rate": 16000
8
+ }
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:80b6393d293991d01b78ee9219ad2a32d65cb30f33433491a795de7879322b8a
3
+ size 1348554373
special_tokens_map.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"unk_token": "[UNK]", "pad_token": "[PAD]"}
tokenizer_config.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"unk_token": "[UNK]", "bos_token": null, "eos_token": null, "pad_token": "[PAD]", "do_lower_case": false, "word_delimiter_token": "|"}
vocab.json ADDED
The diff for this file is too large to render. See raw diff