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", "{lang_id}", 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("{model_id}") #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("{model_id}") #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\tspeech_array, sampling_rate = torchaudio.load(batch["path"])
17\tbatch["speech"] = resampler(speech_array).squeeze().numpy()
18\treturn batch
19
20test_dataset = test_dataset.map(speech_file_to_array_fn)
21inputs = processor(test_dataset[:2]["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
22
23with torch.no_grad():
24\tlogits = 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[:2]["sentence"])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", "{lang_id}", 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("{model_id}") #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`
11model = Wav2Vec2ForCTC.from_pretrained("{model_id}") #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`
12model.to("cuda")
13
14chars_to_ignore_regex = '[\\,\\?\\.\\!\\-\\;\\:\\"\\“]' # TODO: adapt this list to include all special characters you removed from the data
15resampler = torchaudio.transforms.Resample(48_000, 16_000)
16
17# Preprocessing the datasets.
18# We need to read the aduio files as arrays
19def speech_file_to_array_fn(batch):
20\tbatch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
21\tspeech_array, sampling_rate = torchaudio.load(batch["path"])
22\tbatch["speech"] = resampler(speech_array).squeeze().numpy()
23\treturn batch
24
25test_dataset = test_dataset.map(speech_file_to_array_fn)
26
27# Preprocessing the datasets.
28# We need to read the aduio files as arrays
29def evaluate(batch):
30\tinputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
31
32\twith torch.no_grad():
33\t\tlogits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
34
35\tpred_ids = torch.argmax(logits, dim=-1)
36\tbatch["pred_strings"] = processor.batch_decode(pred_ids)
37\treturn batch
38
39result = test_dataset.map(evaluate, batched=True, batch_size=8)
40
41print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"], references=result["sentence"])))train, validation, and ... datasets were used for training as well as ... and ... # TODO: adapt to state all the datasets that were used for training.