Views
No views yet
1from transformers import Wav2Vec2Processor, Wav2Vec2ForCTC
2from datasets import load_dataset
3import torch
4
5# load model and tokenizer
6processor = Wav2Vec2Processor.from_pretrained("bond005/wav2vec2-large-ru-golos")
7model = Wav2Vec2ForCTC.from_pretrained("bond005/wav2vec2-large-ru-golos")
8
9# load the test part of Golos dataset and read first soundfile
10ds = load_dataset("bond005/sberdevices_golos_10h_crowd", split="test")
11
12# tokenize
13processed = processor(ds[0]["audio"]["array"], return_tensors="pt", padding="longest") # Batch size 1
14
15# retrieve logits
16logits = model(processed.input_values, attention_mask=processed.attention_mask).logits
17
18# take argmax and decode
19predicted_ids = torch.argmax(logits, dim=-1)
20transcription = processor.batch_decode(predicted_ids)[0]
21print(transcription)1from datasets import load_dataset
2from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
3import torch
4from jiwer import wer, cer # we need word error rate (WER) and character error rate (CER)
5
6# load the test part of Golos Crowd and remove samples with empty "true" transcriptions
7golos_crowd_test = load_dataset("bond005/sberdevices_golos_10h_crowd", split="test")
8golos_crowd_test = golos_crowd_test.filter(
9 lambda it1: (it1["transcription"] is not None) and (len(it1["transcription"].strip()) > 0)
10)
11
12# load the test part of Golos Farfield and remove sampels with empty "true" transcriptions
13golos_farfield_test = load_dataset("bond005/sberdevices_golos_100h_farfield", split="test")
14golos_farfield_test = golos_farfield_test.filter(
15 lambda it2: (it2["transcription"] is not None) and (len(it2["transcription"].strip()) > 0)
16)
17
18# load model and tokenizer
19model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-base-960h").to("cuda")
20processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-base-960h")
21
22# recognize one sound
23def map_to_pred(batch):
24 # tokenize and vectorize
25 processed = processor(
26 batch["audio"]["array"], sampling_rate=batch["audio"]["sampling_rate"],
27 return_tensors="pt", padding="longest"
28 )
29 input_values = processed.input_values.to("cuda")
30 attention_mask = processed.attention_mask.to("cuda")
31
32 # recognize
33 with torch.no_grad():
34 logits = model(input_values, attention_mask=attention_mask).logits
35 predicted_ids = torch.argmax(logits, dim=-1)
36
37 # decode
38 transcription = processor.batch_decode(predicted_ids)
39 batch["text"] = transcription[0]
40 return batch
41
42# calculate WER and CER on the crowd domain
43crowd_result = golos_crowd_test.map(map_to_pred, remove_columns=["audio"])
44crowd_wer = wer(crowd_result["transcription"], crowd_result["text"])
45crowd_cer = cer(crowd_result["transcription"], crowd_result["text"])
46print("Word error rate on the Crowd domain:", crowd_wer)
47print("Character error rate on the Crowd domain:", crowd_cer)
48
49# calculate WER and CER on the farfield domain
50farfield_result = golos_farfield_test.map(map_to_pred, remove_columns=["audio"])
51farfield_wer = wer(farfield_result["transcription"], farfield_result["text"])
52farfield_cer = cer(farfield_result["transcription"], farfield_result["text"])
53print("Word error rate on the Farfield domain:", farfield_wer)
54print("Character error rate on the Farfield domain:", farfield_cer)| "crowd" | "farfield" |
|---|---|
| 10.144 | 20.353 |
| "crowd" | "farfield" |
|---|---|
| 2.168 | 6.030 |
1@misc{bondarenko2022wav2vec2-large-ru-golos,
2 title={XLSR Wav2Vec2 Russian by Ivan Bondarenko},
3 author={Bondarenko, Ivan},
4 publisher={Hugging Face},
5 journal={Hugging Face Hub},
6 howpublished={\url{https://huggingface.co/bond005/wav2vec2-large-ru-golos}},
7 year={2022}
8}