nhanv commited on
Commit
b03b794
2 Parent(s): 1a73e80 e5d20a5

Merge branch 'main' of https://huggingface.co/NTQAI/wav2vec2-large-japanese into main

Browse files
Files changed (1) hide show
  1. README.md +132 -0
README.md ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language: ja
3
+ datasets:
4
+ - common_voice
5
+ metrics:
6
+ - wer
7
+ - cer
8
+ tags:
9
+ - audio
10
+ - automatic-speech-recognition
11
+ - speech
12
+
13
+ model-index:
14
+ - name: Wav2Vec2 Japanese by NTQAI
15
+ results:
16
+ - task:
17
+ name: Speech Recognition
18
+ type: automatic-speech-recognition
19
+ dataset:
20
+ name: Common Voice ja
21
+ type: common_voice
22
+ args: ja
23
+ metrics:
24
+ - name: Test WER
25
+ type: wer
26
+ value: 81.3
27
+ - name: Test CER
28
+ type: cer
29
+ value: 21.9
30
+ ---
31
+ # Wav2Vec2-Large-Japanese
32
+ Fine-tuned [facebook/wav2vec2-large-xlsr-53](https://huggingface.co/facebook/wav2vec2-large-xlsr-53) on Japanese using the [Common Voice](https://huggingface.co/datasets/common_voice), [CSS10](https://github.com/Kyubyong/css10) and [JSUT](https://sites.google.com/site/shinnosuketakamichi/publication/jsut) and [CSJ]
33
+ When using this model, make sure that your speech input is sampled at 16kHz.
34
+
35
+ ## Usage
36
+ The model can be used directly (without a language model) as follows:
37
+ ```python
38
+ import torch
39
+ import librosa
40
+ from datasets import load_dataset
41
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
42
+ LANG_ID = "ja"
43
+ MODEL_ID = "NTQAI/wav2vec2-large-japanese"
44
+ SAMPLES = 10
45
+ test_dataset = load_dataset("common_voice", LANG_ID, split=f"test[:{SAMPLES}]")
46
+ processor = Wav2Vec2Processor.from_pretrained(MODEL_ID)
47
+ model = Wav2Vec2ForCTC.from_pretrained(MODEL_ID)
48
+ # Preprocessing the datasets.
49
+ # We need to read the audio files as arrays
50
+ def speech_file_to_array_fn(batch):
51
+ speech_array, sampling_rate = librosa.load(batch["path"], sr=16_000)
52
+ batch["speech"] = speech_array
53
+ batch["sentence"] = batch["sentence"].upper()
54
+ return batch
55
+ test_dataset = test_dataset.map(speech_file_to_array_fn)
56
+ inputs = processor(test_dataset["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
57
+ with torch.no_grad():
58
+ logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
59
+ predicted_ids = torch.argmax(logits, dim=-1)
60
+ predicted_sentences = processor.batch_decode(predicted_ids)
61
+ for i, predicted_sentence in enumerate(predicted_sentences):
62
+ print("-" * 100)
63
+ print("Reference:", test_dataset[i]["sentence"])
64
+ print("Prediction:", predicted_sentence)
65
+ ```
66
+ | Reference | Prediction |
67
+ | ------------- | ------------- |
68
+ | 祖母は、おおむね機嫌よく、サイコロをころがしている。 | 人母は重にきね起くさいがしている |
69
+ | 財布をなくしたので、交番へ行きます。 | 財布をなく手端ので勾番へ行きます |
70
+ | 飲み屋のおやじ、旅館の主人、医者をはじめ、交際のある人にきいてまわったら、みんな、私より収入が多いはずなのに、税金は安い。 | ノ宮屋のお親じ旅館の主に医者をはじめ交際のアル人トに聞いて回ったらみんな私より収入が多いはなうに税金は安い |
71
+ | 新しい靴をはいて出かけます。 | だらしい靴をはいて出かけます |
72
+ | このためプラズマ中のイオンや電子の持つ平均運動エネルギーを温度で表現することがある | このためプラズマ中のイオンや電子の持つ平均運動エネルギーを温度で表弁することがある |
73
+ | 松井さんはサッカーより野球のほうが上手です。 | 松井さんはサッカーより野球のほうが上手です |
74
+ | 新しいお皿を使います。 | 新しいお皿を使います |
75
+ | 結婚以来三年半ぶりの東京も、旧友とのお酒も、夜行列車も、駅で寝て、朝を待つのも久しぶりだ。 | 結婚ル二来三年半降りの東京も吸とのお酒も野越者も駅で寝て朝を待つの久しぶりた |
76
+ | これまで、少年野球、ママさんバレーなど、地域スポーツを支え、市民に密着してきたのは、無数のボランティアだった。 | これまで少年野球<unk>三バレーなど地域スポーツを支え市民に満着してきたのは娘数のボランティアだった |
77
+ | 靴を脱いで、スリッパをはきます。 | 靴を脱いでスイパーをはきます |
78
+ ## Evaluation
79
+ The model can be evaluated as follows on the Japanese test data of Common Voice.
80
+ ```python
81
+ import torch
82
+ import re
83
+ import librosa
84
+ from datasets import load_dataset, load_metric
85
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
86
+ LANG_ID = "ja"
87
+ MODEL_ID = "jonatasgrosman/wav2vec2-large-xlsr-53-japanese"
88
+ DEVICE = "cuda"
89
+ CHARS_TO_IGNORE = [",", "?", "¿", ".", "!", "¡", ";", ";", ":", '""', "%", '"', "�", "ʿ", "·", "჻", "~", "՞",
90
+ "؟", "،", "।", "॥", "«", "»", "„", "“", "”", "「", "」", "‘", "’", "《", "》", "(", ")", "[", "]",
91
+ "{", "}", "=", "`", "_", "+", "<", ">", "…", "–", "°", "´", "ʾ", "‹", "›", "©", "®", "—", "→", "。",
92
+ "、", "﹂", "﹁", "‧", "~", "﹏", ",", "{", "}", "(", ")", "[", "]", "【", "】", "‥", "〽",
93
+ "『", "』", "〝", "〟", "⟨", "⟩", "〜", ":", "!", "?", "♪", "؛", "/", "\\", "º", "−", "^", "'", "ʻ", "ˆ"]
94
+ test_dataset = load_dataset("common_voice", LANG_ID, split="test")
95
+ wer = load_metric("wer.py") # https://github.com/jonatasgrosman/wav2vec2-sprint/blob/main/wer.py
96
+ cer = load_metric("cer.py") # https://github.com/jonatasgrosman/wav2vec2-sprint/blob/main/cer.py
97
+ chars_to_ignore_regex = f"[{re.escape(''.join(CHARS_TO_IGNORE))}]"
98
+ processor = Wav2Vec2Processor.from_pretrained(MODEL_ID)
99
+ model = Wav2Vec2ForCTC.from_pretrained(MODEL_ID)
100
+ model.to(DEVICE)
101
+ # Preprocessing the datasets.
102
+ # We need to read the audio files as arrays
103
+ def speech_file_to_array_fn(batch):
104
+ with warnings.catch_warnings():
105
+ warnings.simplefilter("ignore")
106
+ speech_array, sampling_rate = librosa.load(batch["path"], sr=16_000)
107
+ batch["speech"] = speech_array
108
+ batch["sentence"] = re.sub(chars_to_ignore_regex, "", batch["sentence"]).upper()
109
+ return batch
110
+ test_dataset = test_dataset.map(speech_file_to_array_fn)
111
+ # Preprocessing the datasets.
112
+ # We need to read the audio files as arrays
113
+ def evaluate(batch):
114
+ inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
115
+ with torch.no_grad():
116
+ logits = model(inputs.input_values.to(DEVICE), attention_mask=inputs.attention_mask.to(DEVICE)).logits
117
+ pred_ids = torch.argmax(logits, dim=-1)
118
+ batch["pred_strings"] = processor.batch_decode(pred_ids)
119
+ return batch
120
+ result = test_dataset.map(evaluate, batched=True, batch_size=8)
121
+ predictions = [x.upper() for x in result["pred_strings"]]
122
+ references = [x.upper() for x in result["sentence"]]
123
+ print(f"WER: {wer.compute(predictions=predictions, references=references, chunk_size=1000) * 100}")
124
+ print(f"CER: {cer.compute(predictions=predictions, references=references, chunk_size=1000) * 100}")
125
+ ```
126
+ **Test Result**:
127
+ In the table below I report the Word Error Rate (WER) and the Character Error Rate (CER) of the model. I ran the evaluation script described above on other models as well (on 2021-05-10). Note that the table below may show different results from those already reported, this may have been caused due to some specificity of the other evaluation scripts used.
128
+ | Model | WER | CER |
129
+ | ------------- | ------------- | ------------- |
130
+ | jonatasgrosman/wav2vec2-large-xlsr-53-japanese | **81.80%** | **20.16%** |
131
+ | vumichien/wav2vec2-large-xlsr-japanese | 1108.86% | 23.40% |
132
+ | qqhann/w2v_hf_jsut_xlsr53 | 1012.18% | 70.77% |