When using this model, make sure that your speech input is sampled at 16kHz.
1import torch
2import torchaudio
3from datasets import load_dataset
4from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
5
6test_dataset = load_dataset("common_voice", "lg", split="test[:2%]")
7
8processor = Wav2Vec2Processor.from_pretrained("indonesian-nlp/wav2vec2-luganda")
9model = Wav2Vec2ForCTC.from_pretrained("indonesian-nlp/wav2vec2-luganda")
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 if "audio" in batch:
17 speech_array = torch.tensor(batch["audio"]["array"])
18 else:
19 speech_array, sampling_rate = torchaudio.load(batch["path"])
20 batch["speech"] = resampler(speech_array).squeeze().numpy()
21 return batch
22
23test_dataset = test_dataset.map(speech_file_to_array_fn)
24inputs = processor(test_dataset[:2]["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
25
26with torch.no_grad():
27 logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
28
29predicted_ids = torch.argmax(logits, dim=-1)
30
31print("Prediction:", processor.batch_decode(predicted_ids))
32print("Reference:", test_dataset[:2]["sentence"])
The model can be evaluated as follows on the Indonesian test data of Common Voice.
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", "lg", split="test")
8wer = load_metric("wer")
9
10processor = Wav2Vec2Processor.from_pretrained("indonesian-nlp/wav2vec2-luganda")
11model = Wav2Vec2ForCTC.from_pretrained("indonesian-nlp/wav2vec2-luganda")
12model.to("cuda")
13
14chars_to_ignore = [",", "?", ".", "!", "-", ";", ":", '""', "%", "'", '"', "�", "‘", "’", "’"]
15chars_to_ignore_regex = f'[{"".join(chars_to_ignore)}]'
16
17resampler = torchaudio.transforms.Resample(48_000, 16_000)
18
19# Preprocessing the datasets.
20# We need to read the audio files as arrays
21def speech_file_to_array_fn(batch):
22 batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
23 if "audio" in batch:
24 speech_array = torch.tensor(batch["audio"]["array"])
25 else:
26 speech_array, sampling_rate = torchaudio.load(batch["path"])
27 batch["speech"] = resampler(speech_array).squeeze().numpy()
28 return batch
29
30test_dataset = test_dataset.map(speech_file_to_array_fn)
31
32# Preprocessing the datasets.
33# We need to read the audio files as arrays
34def evaluate(batch):
35 inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
36
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"])))
The script used for training can be found
here