Views
No views yet
1import torch
2import librosa
3from datasets import load_dataset
4from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
5LANG_ID = "ja"
6MODEL_ID = "NTQAI/wav2vec2-large-japanese"
7SAMPLES = 3
8test_dataset = load_dataset("common_voice", LANG_ID, split=f"test[:{SAMPLES}]")
9processor = Wav2Vec2Processor.from_pretrained(MODEL_ID)
10model = Wav2Vec2ForCTC.from_pretrained(MODEL_ID)
11# Preprocessing the datasets.
12# We need to read the audio files as arrays
13def speech_file_to_array_fn(batch):
14 speech_array, sampling_rate = librosa.load(batch["path"], sr=16_000)
15 batch["speech"] = speech_array
16 batch["sentence"] = batch["sentence"].upper()
17 return batch
18test_dataset = test_dataset.map(speech_file_to_array_fn)
19inputs = processor(test_dataset["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
20with torch.no_grad():
21 logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
22predicted_ids = torch.argmax(logits, dim=-1)
23predicted_sentences = processor.batch_decode(predicted_ids)
24for i, predicted_sentence in enumerate(predicted_sentences):
25 print("-" * 100)
26 print("Reference:", test_dataset[i]["sentence"])
27 print("Prediction:", predicted_sentence)| Reference | Prediction |
|---|---|
| 祖母は、おおむね機嫌よく、サイコロをころがしている。 | 祖母思い切れを最布ロぼがしている |
| 財布をなくしたので、交番へ行きます。 | 財布をなく時間ので交番でへ行きます |
| 飲み屋のおやじ、旅館の主人、医者をはじめ、交際のある人にきいてまわったら、みんな、私より収入が多いはずなのに、税金は安い。 | ロみ屋のおやし旅館の主人に医をはめ交載のあの人に聞いて回ったらみんな私より収入が多い発ずなのに請金は安い |
1import torch
2import re
3import librosa
4from datasets import load_dataset, load_metric
5from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
6LANG_ID = "ja"
7MODEL_ID = "NTQAI/wav2vec2-large-japanese"
8DEVICE = "cuda"
9CHARS_TO_IGNORE = [",", "?", "¿", ".", "!", "¡", ";", ";", ":", '""', "%", '"', "�", "ʿ", "·", "჻", "~", "՞",
10 "؟", "،", "।", "॥", "«", "»", "„", "“", "”", "「", "」", "‘", "’", "《", "》", "(", ")", "[", "]",
11 "{", "}", "=", "`", "_", "+", "<", ">", "…", "–", "°", "´", "ʾ", "‹", "›", "©", "®", "—", "→", "。",
12 "、", "﹂", "﹁", "‧", "~", "﹏", ",", "{", "}", "(", ")", "[", "]", "【", "】", "‥", "〽",
13 "『", "』", "〝", "〟", "⟨", "⟩", "〜", ":", "!", "?", "♪", "؛", "/", "\\", "º", "−", "^", "'", "ʻ", "ˆ"]
14test_dataset = load_dataset("common_voice", LANG_ID, split="test")
15wer = load_metric("wer.py") # https://github.com/jonatasgrosman/wav2vec2-sprint/blob/main/wer.py
16cer = load_metric("cer.py") # https://github.com/jonatasgrosman/wav2vec2-sprint/blob/main/cer.py
17chars_to_ignore_regex = f"[{re.escape(''.join(CHARS_TO_IGNORE))}]"
18processor = Wav2Vec2Processor.from_pretrained(MODEL_ID)
19model = Wav2Vec2ForCTC.from_pretrained(MODEL_ID)
20model.to(DEVICE)
21# Preprocessing the datasets.
22# We need to read the audio files as arrays
23def speech_file_to_array_fn(batch):
24 with warnings.catch_warnings():
25 warnings.simplefilter("ignore")
26 speech_array, sampling_rate = librosa.load(batch["path"], sr=16_000)
27 batch["speech"] = speech_array
28 batch["sentence"] = re.sub(chars_to_ignore_regex, "", batch["sentence"]).upper()
29 return batch
30test_dataset = test_dataset.map(speech_file_to_array_fn)
31# Preprocessing the datasets.
32# We need to read the audio files as arrays
33def evaluate(batch):
34 inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
35 with torch.no_grad():
36 logits = model(inputs.input_values.to(DEVICE), attention_mask=inputs.attention_mask.to(DEVICE)).logits
37 pred_ids = torch.argmax(logits, dim=-1)
38 batch["pred_strings"] = processor.batch_decode(pred_ids)
39 return batch
40result = test_dataset.map(evaluate, batched=True, batch_size=8)
41predictions = [x.upper() for x in result["pred_strings"]]
42references = [x.upper() for x in result["sentence"]]
43print(f"WER: {wer.compute(predictions=predictions, references=references, chunk_size=1000) * 100}")
44print(f"CER: {cer.compute(predictions=predictions, references=references, chunk_size=1000) * 100}")| Model | WER | CER |
|---|---|---|
| NTQAI/wav2vec2-large-japanese | 73.10% | 18.15% |
| vumichien/wav2vec2-large-xlsr-japanese | 1108.86% | 23.40% |
| qqhann/w2v_hf_jsut_xlsr53 | 1012.18% | 70.77% |