Views
No views yet
1
2import torch
3import torchaudio
4from datasets import load_dataset
5from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
6
7test_dataset = load_dataset("common_voice", "uk", split="test[:2%]")
8
9processor = Wav2Vec2Processor.from_pretrained("arampacha/wav2vec2-large-xlsr-ukrainian")
10model = Wav2Vec2ForCTC.from_pretrained("arampacha/wav2vec2-large-xlsr-ukrainian")
11
12# Preprocessing the datasets.
13# We need to read the aduio files as arrays
14
15def speech_file_to_array_fn(batch):
16 speech_array, sampling_rate = torchaudio.load(batch["path"])
17 batch["speech"] = torchaudio.transforms.Resample(sampling_rate, 16_000)(speech_array).squeeze().numpy()
18 return batch
19
20test_dataset = test_dataset.map(speech_file_to_array_fn)
21
22inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
23
24with torch.no_grad():
25 logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
26
27predicted_ids = torch.argmax(logits, dim=-1)
28
29print("Prediction:", processor.batch_decode(predicted_ids))
30print("Reference:", test_dataset["sentence"][:2])
311import torch
2import torchaudio
3from datasets import load_dataset, load_metric
4from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
5import re
6
7test_dataset = load_dataset("common_voice", "uk", split="test")
8
9wer = load_metric("wer")
10processor = Wav2Vec2Processor.from_pretrained("arampacha/wav2vec2-large-xlsr-ukrainian")
11model = Wav2Vec2ForCTC.from_pretrained("arampacha/wav2vec2-large-xlsr-ukrainian")
12model.to("cuda")
13
14chars_to_ignore = [",", "?", ".", "!", "-", ";", ":", '""', "%", "'", '"', "�", '«', '»', '—', '…', '(', ')', '*', '”', '“']
15chars_to_ignore_regex = f'[{"".join(chars_to_ignore)}]'
16resampler = torchaudio.transforms.Resample(48_000, 16_000)
17
18# Preprocessing the datasets.
19# We need to read the aduio files as arrays and normalize charecters
20def speech_file_to_array_fn(batch):
21 batch["sentence"] = re.sub(re.compile("['`]"), '’', batch['sentence'])
22 batch["sentence"] = re.sub(re.compile(chars_to_ignore_regex), '', batch["sentence"]).lower().strip()
23 batch["sentence"] = re.sub(re.compile('i'), 'і', batch['sentence'])
24 batch["sentence"] = re.sub(re.compile('o'), 'о', batch['sentence'])
25 batch["sentence"] = re.sub(re.compile('a'), 'а', batch['sentence'])
26 batch["sentence"] = re.sub(re.compile('ы'), 'и', batch['sentence'])
27 batch["sentence"] = re.sub(re.compile("–"), '', batch['sentence'])
28 batch['sentence'] = re.sub(' ', ' ', batch['sentence'])
29 speech_array, sampling_rate = torchaudio.load(batch["path"])
30 batch["speech"] = torchaudio.transforms.Resample(sampling_rate, 16_000)(speech_array).squeeze().numpy()
31 return batch
32
33test_dataset = test_dataset.map(speech_file_to_array_fn)
34
35def evaluate(batch):
36 inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
37 with torch.no_grad():
38 logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
39
40 pred_ids = torch.argmax(logits, dim=-1)
41 batch["pred_strings"] = processor.batch_decode(pred_ids)
42 return batch
43
44result = test_dataset.map(evaluate, batched=True, batch_size=8)
45
46print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"], references=result["sentence"])))
47train, validation and the M-AILABS Ukrainian corpus.