Views
No views yet
1import torch
2import torchaudio
3from datasets import load_dataset
4from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
5test_dataset = load_dataset("common_voice", "ga-IE", split="test[:2%]")
6processor = Wav2Vec2Processor.from_pretrained("jimregan/wav2vec2-large-xlsr-irish-basic")
7model = Wav2Vec2ForCTC.from_pretrained("jimregan/wav2vec2-large-xlsr-irish-basic")
8resampler = torchaudio.transforms.Resample(48_000, 16_000)
9# Preprocessing the datasets.
10# We need to read the audio files as arrays
11def speech_file_to_array_fn(batch):
12 speech_array, sampling_rate = torchaudio.load(batch["path"])
13 batch["speech"] = resampler(speech_array).squeeze().numpy()
14 return batch
15test_dataset = test_dataset.map(speech_file_to_array_fn)
16inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
17with torch.no_grad():
18 logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
19predicted_ids = torch.argmax(logits, dim=-1)
20print("Prediction:", processor.batch_decode(predicted_ids))
21print("Reference:", test_dataset["sentence"][:2])1import torch
2import torchaudio
3from datasets import load_dataset, load_metric
4from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
5import re
6test_dataset = load_dataset("common_voice", "ga-IE", split="test")
7wer = load_metric("wer")
8processor = Wav2Vec2Processor.from_pretrained("jimregan/wav2vec2-large-xlsr-irish-basic")
9model = Wav2Vec2ForCTC.from_pretrained("jimregan/wav2vec2-large-xlsr-irish-basic")
10model.to("cuda")
11# So, tolower() for Irish is a bit complicated: tAthar -> t-athair
12# toupper() is non-deterministic :)
13def is_upper_vowel(letter):
14 if letter in ['A', 'E', 'I', 'O', 'U', 'Á', 'É', 'Í', 'Ó', 'Ú']:
15 return True
16 else:
17 return False
18def irish_lower(word):
19 if len(word) > 1 and word[0] in ['n', 't'] and is_upper_vowel(word[1]):
20 return word[0] + '-' + word[1:].lower()
21 else:
22 return word.lower()
23def irish_lower_sentence(sentence):
24 return " ".join([irish_lower(w) for w in sentence.split(" ")])
25chars_to_ignore_regex = '[,\?\.\!\;\:\"\“\%\‘\”\(\)\*]'
26def remove_special_characters(sentence):
27 tmp = re.sub('’ ', ' ', sentence)
28 tmp = re.sub("’$", '', tmp)
29 tmp = re.sub('’', '\'', tmp)
30 tmp = re.sub(chars_to_ignore_regex, '', tmp)
31 sentence = irish_lower_sentence(tmp) + ' '
32 return sentence
33resampler = torchaudio.transforms.Resample(48_000, 16_000)
34# Preprocessing the datasets.
35# We need to read the audio files as arrays
36def speech_file_to_array_fn(batch):
37 batch["sentence"] = remove_special_characters(batch["sentence"])
38 speech_array, sampling_rate = torchaudio.load(batch["path"])
39 batch["speech"] = resampler(speech_array).squeeze().numpy()
40 return batch
41test_dataset = test_dataset.map(speech_file_to_array_fn)
42# Preprocessing the datasets.
43# We need to read the audio files as arrays
44def evaluate(batch):
45 inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
46 with torch.no_grad():
47 logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
48 pred_ids = torch.argmax(logits, dim=-1)
49 batch["pred_strings"] = processor.batch_decode(pred_ids)
50 return batch
51result = test_dataset.map(evaluate, batched=True, batch_size=8)
52print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"], references=result["sentence"])))train and validation datasets were used for training.