Views
No views yet
| Step | Training Loss | Validation Loss | WER |
|---|---|---|---|
| 1000 | 12.29880 | 3.610288 | 1.00000 |
| 2000 | 3.601800 | 3.505306 | 1.00000 |
| 3000 | 2.80300 | 1.948012 | 0.722361 |
| 4000 | 1.961500 | 1.545842 | 0.558738 |
| 5000 | 1.712000 | 1.420027 | 0.509049 |
| 6000 | 1.565500 | 1.235171 | 0.466279 |
| 7000 | 1.504900 | 1.160565 | 0.461829 |
| 8000 | 1.409800 | 1.088012 | 0.427435 |
| 9000 | 1.358800 | 1.097211 | 0.409861 |
| 10000 | 1.318600 | 1.062294 | 0.403694 |
| 11000 | 1.258500 | 1.026783 | 0.385464 |
| 12000 | 1.245100 | 1.024860 | 0.379845 |
| 13000 | 1.217700 | 0.985201 | 0.375634 |
| 14000 | 1.187900 | 0.977686 | 0.367163 |
| 15000 | 1.168100 | 0.978529 | 0.363656 |
| 16000 | 1.135800 | 0.965668 | 0.363942 |
| 17000 | 1.140600 | 0.953237 | 0.360912 |
| Step | Training Loss | Validation Loss | WER |
|---|---|---|---|
| 1000 | 1.08950 | 0.49275 | 0.302035 |
| 2000 | 0.86100 | 0.45113 | 0.266950 |
| 3000 | 0.76240 | 0.442281 | 0.244981 |
| 4000 | 0.70170 | 0.411666 | 0.234287 |
| 5000 | 0.66400 | 0.411769 | 0.227942 |
| 6000 | 0.63810 | 0.413067 | 0.225690 |
1from transformers import HubertForCTC, Wav2Vec2Processor
2from datasets import load_dataset
3import torch
4import torchaudio
5import librosa
6import numpy as np
7import re
8import MeCab
9import pykakasi
10from evaluate import load
11
12model = HubertForCTC.from_pretrained('TKU410410103/hubert-large-japanese-asr')
13processor = Wav2Vec2Processor.from_pretrained("TKU410410103/hubert-large-japanese-asr")
14
15# load dataset
16test_dataset = load_dataset('mozilla-foundation/common_voice_11_0', 'ja', split='test')
17remove_columns = [col for col in test_dataset.column_names if col not in ['audio', 'sentence']]
18test_dataset = test_dataset.remove_columns(remove_columns)
19
20# resample
21def process_waveforms(batch):
22 speech_arrays = []
23 sampling_rates = []
24
25 for audio_path in batch['audio']:
26 speech_array, _ = torchaudio.load(audio_path['path'])
27 speech_array_resampled = librosa.resample(np.asarray(speech_array[0].numpy()), orig_sr=48000, target_sr=16000)
28 speech_arrays.append(speech_array_resampled)
29 sampling_rates.append(16000)
30
31 batch["array"] = speech_arrays
32 batch["sampling_rate"] = sampling_rates
33
34 return batch
35
36# hiragana
37CHARS_TO_IGNORE = [",", "?", "¿", ".", "!", "¡", ";", ";", ":", '""', "%", '"', "�", "ʿ", "·", "჻", "~", "՞",
38 "؟", "،", "।", "॥", "«", "»", "„", "“", "”", "「", "」", "‘", "’", "《", "》", "(", ")", "[", "]",
39 "{", "}", "=", "`", "_", "+", "<", ">", "…", "–", "°", "´", "ʾ", "‹", "›", "©", "®", "—", "→", "。",
40 "、", "﹂", "﹁", "‧", "~", "﹏", ",", "{", "}", "(", ")", "[", "]", "【", "】", "‥", "〽",
41 "『", "』", "〝", "〟", "⟨", "⟩", "〜", ":", "!", "?", "♪", "؛", "/", "\\", "º", "−", "^", "'", "ʻ", "ˆ"]
42chars_to_ignore_regex = f"[{re.escape(''.join(CHARS_TO_IGNORE))}]"
43
44wakati = MeCab.Tagger("-Owakati")
45kakasi = pykakasi.kakasi()
46kakasi.setMode("J","H")
47kakasi.setMode("K","H")
48kakasi.setMode("r","Hepburn")
49conv = kakasi.getConverter()
50
51def prepare_char(batch):
52 batch["sentence"] = conv.do(wakati.parse(batch["sentence"]).strip())
53 batch["sentence"] = re.sub(chars_to_ignore_regex,'', batch["sentence"]).strip()
54 return batch
55
56
57resampled_eval_dataset = test_dataset.map(process_waveforms, batched=True, batch_size=50, num_proc=4)
58eval_dataset = resampled_eval_dataset.map(prepare_char, num_proc=4)
59
60# begin the evaluation process
61wer = load("wer")
62cer = load("cer")
63
64def evaluate(batch):
65 inputs = processor(batch["array"], sampling_rate=16_000, return_tensors="pt", padding=True)
66 with torch.no_grad():
67 logits = model(inputs.input_values.to(device), attention_mask=inputs.attention_mask.to(device)).logits
68 pred_ids = torch.argmax(logits, dim=-1)
69 batch["pred_strings"] = processor.batch_decode(pred_ids)
70 return batch
71
72columns_to_remove = [column for column in eval_dataset.column_names if column != "sentence"]
73batch_size = 16
74result = eval_dataset.map(evaluate, remove_columns=columns_to_remove, batched=True, batch_size=batch_size)
75
76wer_result = wer.compute(predictions=result["pred_strings"], references=result["sentence"])
77cer_result = cer.compute(predictions=result["pred_strings"], references=result["sentence"])
78
79print("WER: {:2f}%".format(100 * wer_result))
80print("CER: {:2f}%".format(100 * cer_result))