Views
No views yet
1import torch
2import torchaudio
3from datasets import load_dataset
4from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
5
6test_dataset = load_dataset("common_voice", "el", split="test[:2%]") #TODO: replace {lang_id} in your language code here. Make sure the code is one of the *ISO codes* of [this](https://huggingface.co/languages) site.
7
8processor = Wav2Vec2Processor.from_pretrained("vasilis/wav2vec2-large-xlsr-53-finnish") #TODO: replace {model_id} with your model id. The model id consists of {your_username}/{your_modelname}, *e.g.* `elgeish/wav2vec2-large-xlsr-53-arabic`
9model = Wav2Vec2ForCTC.from_pretrained("vasilis/wav2vec2-large-xlsr-53-finnish") #TODO: replace {model_id} with your model id. The model id consists of {your_username}/{your_modelname}, *e.g.* `elgeish/wav2vec2-large-xlsr-53-arabic`
10
11resampler = torchaudio.transforms.Resample(48_000, 16_000)
12
13# Preprocessing the datasets.
14# We need to read the aduio files as arrays
15def speech_file_to_array_fn(batch):
16 speech_array, sampling_rate = torchaudio.load(batch["path"])
17 batch["speech"] = resampler(speech_array).squeeze().numpy()
18 return batch
19
20test_dataset = test_dataset.map(speech_file_to_array_fn)
21inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
22
23with torch.no_grad():
24 logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
25
26predicted_ids = torch.argmax(logits, dim=-1)
27
28print("Prediction:", processor.batch_decode(predicted_ids))
29print("Reference:", test_dataset["sentence"][:2])1import torch
2import torchaudio
3from datasets import load_dataset, load_metric
4from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
5import re
6
7test_dataset = load_dataset("common_voice", "fi", split="test") #TODO: replace {lang_id} in your language code here. Make sure the code is one of the *ISO codes* of [this](https://huggingface.co/languages) site.
8wer = load_metric("wer")
9
10processor = Wav2Vec2Processor.from_pretrained("vasilis/wav2vec2-large-xlsr-53-finnish")
11model = Wav2Vec2ForCTC.from_pretrained("vasilis/wav2vec2-large-xlsr-53-finnish")
12model.to("cuda")
13
14chars_to_ignore_regex = "[\,\?\.\!\-\;\:\"\“\%\‘\”\�\']" # TODO: adapt this list to include all special characters you removed from the data
15replacements = {"…": "", "–": ''}
16
17resampler = {
18 48_000: torchaudio.transforms.Resample(48_000, 16_000),
19 44100: torchaudio.transforms.Resample(44100, 16_000),
20 32000: torchaudio.transforms.Resample(32000, 16_000)
21}
22
23
24# Preprocessing the datasets.
25# We need to read the aduio files as arrays
26def speech_file_to_array_fn(batch):
27 batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
28 for key, value in replacements.items():
29 batch["sentence"] = batch["sentence"].replace(key, value)
30 speech_array, sampling_rate = torchaudio.load(batch["path"])
31 batch["speech"] = resampler[sampling_rate](speech_array).squeeze().numpy()
32 return batch
33
34
35test_dataset = test_dataset.map(speech_file_to_array_fn)
36
37# Preprocessing the datasets.
38# We need to read the aduio files as arrays
39def evaluate(batch):
40 inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
41
42 with torch.no_grad():
43 logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
44
45 pred_ids = torch.argmax(logits, dim=-1)
46 batch["pred_strings"] = processor.batch_decode(pred_ids)
47 return batch
48
49result = test_dataset.map(evaluate, batched=True, batch_size=8)
50
51print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"], references=result["sentence"])))
52print("CER: {:2f}".format(100 * wer.compute(predictions=[" ".join(list(entry)) for entry in result["pred_strings"]], references=[" ".join(list(entry)) for entry in result["sentence"]])))
53CSS10 Finnish was used using the normalized transcripts.
After 20000 steps the models was finetuned using the common voice train and validation sets for 2000 steps more.