Views
No views yet
actual_text and path_in_folder columns:1import torch, torchaudio
2from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
3
4#Since marathi is not present on Common Voice, script for reading the below dataset can be picked up from the eval script below
5mr_test_dataset = all_data['test']
6
7processor = Wav2Vec2Processor.from_pretrained("Tejas2000/SpeechRecog")
8model = Wav2Vec2ForCTC.from_pretrained("Tejas2000/SpeechRecog")
9
10resampler = torchaudio.transforms.Resample(48_000, 16_000) #first arg - input sample, second arg - output sample
11# Preprocessing the datasets. We need to read the aduio files as arrays
12def speech_file_to_array_fn(batch):
13 speech_array, sampling_rate = torchaudio.load(batch["path_in_folder"])
14 batch["speech"] = resampler(speech_array).squeeze().numpy()
15 return batch
16mr_test_dataset = mr_test_dataset.map(speech_file_to_array_fn)
17inputs = processor(mr_test_dataset["speech"][:5], sampling_rate=16_000, return_tensors="pt", padding=True)
18with torch.no_grad():
19 logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
20predicted_ids = torch.argmax(logits, dim=-1)
21print("Prediction:", processor.batch_decode(predicted_ids))
22print("Reference:", mr_test_dataset["actual_text"][:5])1import os, re, torch, torchaudio
2from datasets import Dataset, load_metric
3import pandas as pd
4from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
5
6#below is a custom script to be used for reading marathi dataset since its not present on the Common Voice
7dataset_path = "./OpenSLR-64_Marathi/mr_in_female/" #TODO : include the path of the dataset extracted from http://openslr.org/64/
8audio_df = pd.read_csv(os.path.join(dataset_path,'line_index.tsv'),sep='\t',header=None)
9audio_df.columns = ['path_in_folder','actual_text']
10audio_df['path_in_folder'] = audio_df['path_in_folder'].apply(lambda x: dataset_path + x + '.wav')
11audio_df = audio_df.sample(frac=1, random_state=2020).reset_index(drop=True) #seed number is important for reproducibility of WER score
12all_data = Dataset.from_pandas(audio_df)
13all_data = all_data.train_test_split(test_size=0.10,seed=2020) #seed number is important for reproducibility of WER score
14
15mr_test_dataset = all_data['test']
16wer = load_metric("wer")
17
18processor = Wav2Vec2Processor.from_pretrained("Tejas2000/SpeechRecog")
19model = Wav2Vec2ForCTC.from_pretrained("Tejas2000/SpeechRecog")
20model.to("cuda")
21
22chars_to_ignore_regex = '[\,\?\.\!\-\;\:\"\“]'
23resampler = torchaudio.transforms.Resample(48_000, 16_000)
24# Preprocessing the datasets. We need to read the aduio files as arrays
25def speech_file_to_array_fn(batch):
26 batch["actual_text"] = re.sub(chars_to_ignore_regex, '', batch["actual_text"]).lower()
27 speech_array, sampling_rate = torchaudio.load(batch["path_in_folder"])
28 batch["speech"] = resampler(speech_array).squeeze().numpy()
29 return batch
30mr_test_dataset = mr_test_dataset.map(speech_file_to_array_fn)
31def evaluate(batch):
32 inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
33 with torch.no_grad():
34 logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
35 pred_ids = torch.argmax(logits, dim=-1)
36 batch["pred_strings"] = processor.batch_decode(pred_ids)
37 return batch
38result = mr_test_dataset.map(evaluate, batched=True, batch_size=8)
39print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"], references=result["actual_text"])))