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", "cv", split="test")
7
8processor = Wav2Vec2Processor.from_pretrained("gagan3012/wav2vec2-xlsr-chuvash")
9model = Wav2Vec2ForCTC.from_pretrained("gagan3012/wav2vec2-xlsr-chuvash")
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["speech"][:2], 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["sentence"][:2])
301import torch
2import torchaudio
3from datasets import load_dataset, load_metric
4from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
5import re
6
7!mkdir cer
8!wget -O cer/cer.py https://huggingface.co/ctl/wav2vec2-large-xlsr-cantonese/raw/main/cer.py
9
10test_dataset = load_dataset("common_voice", "cv", 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.
11wer = load_metric("wer")
12cer = load_metric("cer")
13
14processor = Wav2Vec2Processor.from_pretrained("gagan3012/wav2vec2-xlsr-chuvash")
15model = Wav2Vec2ForCTC.from_pretrained("gagan3012/wav2vec2-xlsr-chuvash")
16model.to("cuda")
17
18
19
20chars_to_ignore_regex = '[\\\\,\\\\?\\\\.\\\\!\\\\-\\\\;\\\\:\\\\"\\\\“]' # TODO: adapt this list to include all special characters you removed from the data
21resampler = torchaudio.transforms.Resample(48_000, 16_000)
22
23# Preprocessing the datasets.
24# We need to read the aduio files as arrays
25def speech_file_to_array_fn(batch):
26\\tbatch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
27\\tspeech_array, sampling_rate = torchaudio.load(batch["path"])
28\\tbatch["speech"] = resampler(speech_array).squeeze().numpy()
29\\treturn batch
30
31test_dataset = test_dataset.map(speech_file_to_array_fn)
32
33# Preprocessing the datasets.
34# We need to read the aduio files as arrays
35def evaluate(batch):
36\\tinputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
37
38\\twith torch.no_grad():
39\\t\\tlogits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
40
41\\tpred_ids = torch.argmax(logits, dim=-1)
42\\tbatch["pred_strings"] = processor.batch_decode(pred_ids)
43\\treturn batch
44
45result = test_dataset.map(evaluate, batched=True, batch_size=8)
46
47print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"], references=result["sentence"])))
48print("CER: {:2f}".format(100 * cer.compute(predictions=result["pred_strings"], references=result["sentence"])))