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-greek") #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-greek") #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", "el", 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-greek") #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("vasilis/wav2vec2-large-xlsr-53-greek") #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
15
16normalize_greek_letters = {"ς": "σ"}
17# normalize_greek_letters = {"ά": "α", "έ": "ε", "ί": "ι", 'ϊ': "ι", "ύ": "υ", "ς": "σ", "ΐ": "ι", 'ϋ': "υ", "ή": "η", "ώ": "ω", 'ό': "ο"}
18remove_chars_greek = {"a": "", "h": "", "n": "", "g": "", "o": "", "v": "", "e": "", "r": "", "t": "", "«": "", "»": "", "m": "", '́': '', "·": "", "’": "", '´': ""}
19replacements = {**normalize_greek_letters, **remove_chars_greek}
20
21resampler = {
22 48_000: torchaudio.transforms.Resample(48_000, 16_000),
23 44100: torchaudio.transforms.Resample(44100, 16_000),
24 32000: torchaudio.transforms.Resample(32000, 16_000)
25}
26
27
28# Preprocessing the datasets.
29# We need to read the aduio files as arrays
30def speech_file_to_array_fn(batch):
31 batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
32 for key, value in replacements.items():
33 batch["sentence"] = batch["sentence"].replace(key, value)
34 speech_array, sampling_rate = torchaudio.load(batch["path"])
35 batch["speech"] = resampler[sampling_rate](speech_array).squeeze().numpy()
36 return batch
37
38
39test_dataset = test_dataset.map(speech_file_to_array_fn)
40
41# Preprocessing the datasets.
42# We need to read the aduio files as arrays
43def evaluate(batch):
44 inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
45
46 with torch.no_grad():
47 logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
48
49 pred_ids = torch.argmax(logits, dim=-1)
50 batch["pred_strings"] = processor.batch_decode(pred_ids)
51 return batch
52
53result = test_dataset.map(evaluate, batched=True, batch_size=8)
54
55print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"], references=result["sentence"])))
56print("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"]])))
57CSS10 Greek was used using the normalized transcripts.
During text preprocessing letter ς is normalized to σ the reason is that both letters sound the same with ς only used as the ending character of words. So, the change can be mapped up to proper dictation easily. I tried removing all accents from letters as well that improved WER significantly. The model was reaching 17% WER easily without having converged. However, the text preprocessing needed to do after to fix transcrtiptions would be more complicated. A language model should fix things easily though. Another thing that could be tried out would be to change all of ι, η ... etc to a single character since all sound the same. similar for o and ω these should help the acoustic model part significantly since all these characters map to the same sound. But further text normlization would be needed.