Views
No views yet
| Dataset | Train | Valid | Test |
|---|---|---|---|
| CETUC | 94h | -- | 5.4h |
| Common Voice | -- | 9.5h | |
| LaPS BM | -- | 0.1h | |
| MLS | -- | 3.7h | |
| Multilingual TEDx (Portuguese) | -- | 1.8h | |
| SID | -- | 1.0h | |
| VoxForge | -- | 0.1h | |
| Total | -- | 21.6h |
| CETUC | CV | LaPS | MLS | SID | TEDx | VF | AVG | |
|---|---|---|---|---|---|---|---|---|
| cetuc_100 (demonstration below) | 0.446 | 0.856 | 0.089 | 0.967 | 1.172 | 0.929 | 0.902 | 0.765 |
| cetuc_100 + 4-gram (demonstration below) | 0.339 | 0.734 | 0.076 | 0.961 | 1.188 | 1.227 | 0.801 | 0.760 |
MODEL_NAME = "lgris/cetuc100-xlsr" 1%%capture
2!pip install torch==1.8.2+cu111 torchvision==0.9.2+cu111 torchaudio===0.8.2 -f https://download.pytorch.org/whl/lts/1.8/torch_lts.html
3!pip install datasets
4!pip install jiwer
5!pip install transformers
6!pip install soundfile
7!pip install pyctcdecode
8!pip install https://github.com/kpu/kenlm/archive/master.zip1import jiwer
2import torchaudio
3from datasets import load_dataset, load_metric
4from transformers import (
5 Wav2Vec2ForCTC,
6 Wav2Vec2Processor,
7)
8from pyctcdecode import build_ctcdecoder
9import torch
10import re
11import sys1chars_to_ignore_regex = '[\,\?\.\!\;\:\"]' # noqa: W605
2
3def map_to_array(batch):
4 speech, _ = torchaudio.load(batch["path"])
5 batch["speech"] = speech.squeeze(0).numpy()
6 batch["sampling_rate"] = 16_000
7 batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower().replace("’", "'")
8 batch["target"] = batch["sentence"]
9 return batch1def calc_metrics(truths, hypos):
2 wers = []
3 mers = []
4 wils = []
5 for t, h in zip(truths, hypos):
6 try:
7 wers.append(jiwer.wer(t, h))
8 mers.append(jiwer.mer(t, h))
9 wils.append(jiwer.wil(t, h))
10 except: # Empty string?
11 pass
12 wer = sum(wers)/len(wers)
13 mer = sum(mers)/len(mers)
14 wil = sum(wils)/len(wils)
15 return wer, mer, wil1def load_data(dataset):
2 data_files = {'test': f'{dataset}/test.csv'}
3 dataset = load_dataset('csv', data_files=data_files)["test"]
4 return dataset.map(map_to_array)1class STT:
2
3 def __init__(self,
4 model_name,
5 device='cuda' if torch.cuda.is_available() else 'cpu',
6 lm=None):
7 self.model_name = model_name
8 self.model = Wav2Vec2ForCTC.from_pretrained(model_name).to(device)
9 self.processor = Wav2Vec2Processor.from_pretrained(model_name)
10 self.vocab_dict = self.processor.tokenizer.get_vocab()
11 self.sorted_dict = {
12 k.lower(): v for k, v in sorted(self.vocab_dict.items(),
13 key=lambda item: item[1])
14 }
15 self.device = device
16 self.lm = lm
17 if self.lm:
18 self.lm_decoder = build_ctcdecoder(
19 list(self.sorted_dict.keys()),
20 self.lm
21 )
22
23 def batch_predict(self, batch):
24 features = self.processor(batch["speech"],
25 sampling_rate=batch["sampling_rate"][0],
26 padding=True,
27 return_tensors="pt")
28 input_values = features.input_values.to(self.device)
29 attention_mask = features.attention_mask.to(self.device)
30 with torch.no_grad():
31 logits = self.model(input_values, attention_mask=attention_mask).logits
32 if self.lm:
33 logits = logits.cpu().numpy()
34 batch["predicted"] = []
35 for sample_logits in logits:
36 batch["predicted"].append(self.lm_decoder.decode(sample_logits))
37 else:
38 pred_ids = torch.argmax(logits, dim=-1)
39 batch["predicted"] = self.processor.batch_decode(pred_ids)
40 return batch1%%capture
2!gdown --id 1HFECzIizf-bmkQRLiQD0QVqcGtOG5upI
3!mkdir bp_dataset
4!unzip bp_dataset -d bp_dataset/stt = STT(MODEL_NAME)1ds = load_data('cetuc_dataset')
2result = ds.map(stt.batch_predict, batched=True, batch_size=8)
3wer, mer, wil = calc_metrics(result["sentence"], result["predicted"])
4print("CETUC WER:", wer)CETUC WER: 0.446775818292208251ds = load_data('commonvoice_dataset')
2result = ds.map(stt.batch_predict, batched=True, batch_size=8)
3wer, mer, wil = calc_metrics(result["sentence"], result["predicted"])
4print("CV WER:", wer)CV WER: 0.85619198991390651ds = load_data('lapsbm_dataset')
2result = ds.map(stt.batch_predict, batched=True, batch_size=8)
3wer, mer, wil = calc_metrics(result["sentence"], result["predicted"])
4print("Laps WER:", wer)Laps WER: 0.089558080808080811ds = load_data('mls_dataset')
2result = ds.map(stt.batch_predict, batched=True, batch_size=8)
3wer, mer, wil = calc_metrics(result["sentence"], result["predicted"])
4print("MLS WER:", wer)MLS WER: 0.96700087909797181ds = load_data('sid_dataset')
2result = ds.map(stt.batch_predict, batched=True, batch_size=8)
3wer, mer, wil = calc_metrics(result["sentence"], result["predicted"])
4print("Sid WER:", wer)Sid WER: 1.17237383436328611ds = load_data('tedx_dataset')
2result = ds.map(stt.batch_predict, batched=True, batch_size=8)
3wer, mer, wil = calc_metrics(result["sentence"], result["predicted"])
4print("TEDx WER:", wer)TEDx WER: 0.9299764363175391ds = load_data('voxforge_dataset')
2result = ds.map(stt.batch_predict, batched=True, batch_size=8)
3wer, mer, wil = calc_metrics(result["sentence"], result["predicted"])
4print("VoxForge WER:", wer)VoxForge WER: 0.90201839826839851# !find -type f -name "*.wav" -delete
2!rm -rf ~/.cache
3!gdown --id 1GJIKseP5ZkTbllQVgOL98R4yYAcIySFP # trained with wikipedia
4stt = STT(MODEL_NAME, lm='pt-BR-wiki.word.4-gram.arpa')
5# !gdown --id 1dLFldy7eguPtyJj5OAlI4Emnx0BpFywg # trained with bp
6# stt = STT(MODEL_NAME, lm='pt-BR.word.4-gram.arpa')1ds = load_data('cetuc_dataset')
2result = ds.map(stt.batch_predict, batched=True, batch_size=8)
3wer, mer, wil = calc_metrics(result["sentence"], result["predicted"])
4print("CETUC WER:", wer)CETUC WER: 0.33963466633548271ds = load_data('commonvoice_dataset')
2result = ds.map(stt.batch_predict, batched=True, batch_size=8)
3wer, mer, wil = calc_metrics(result["sentence"], result["predicted"])
4print("CV WER:", wer)CV WER: 0.73410132427195121ds = load_data('lapsbm_dataset')
2result = ds.map(stt.batch_predict, batched=True, batch_size=8)
3wer, mer, wil = calc_metrics(result["sentence"], result["predicted"])
4print("Laps WER:", wer)Laps WER: 0.076123737373737371ds = load_data('mls_dataset')
2result = ds.map(stt.batch_predict, batched=True, batch_size=8)
3wer, mer, wil = calc_metrics(result["sentence"], result["predicted"])
4print("MLS WER:", wer)MLS WER: 0.9609089402432121ds = load_data('sid_dataset')
2result = ds.map(stt.batch_predict, batched=True, batch_size=8)
3wer, mer, wil = calc_metrics(result["sentence"], result["predicted"])
4print("Sid WER:", wer)Sid WER: 1.1881185405335791ds = load_data('tedx_dataset')
2result = ds.map(stt.batch_predict, batched=True, batch_size=8)
3wer, mer, wil = calc_metrics(result["sentence"], result["predicted"])
4print("TEDx WER:", wer)TEDx WER: 1.22710771783396181ds = load_data('voxforge_dataset')
2result = ds.map(stt.batch_predict, batched=True, batch_size=8)
3wer, mer, wil = calc_metrics(result["sentence"], result["predicted"])
4print("VoxForge WER:", wer)VoxForge WER: 0.800196158008658