Views
No views yet
1import torch
2import librosa
3from datasets import load_dataset
4from transformers import Wav2Vec2ForSequenceClassification, Wav2Vec2FeatureExtractor
5
6def map_to_array(example):
7 speech, _ = librosa.load(example["file"], sr=16000, mono=True)
8 example["speech"] = speech
9 return example
10
11# load a demo dataset and read audio files
12dataset = load_dataset("anton-l/superb_demo", "ic", split="test")
13dataset = dataset.map(map_to_array)
14
15model = Wav2Vec2ForSequenceClassification.from_pretrained("superb/wav2vec2-base-superb-ic")
16feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained("superb/wav2vec2-base-superb-ic")
17
18# compute attention masks and normalize the waveform if needed
19inputs = feature_extractor(dataset[:4]["speech"], sampling_rate=16000, padding=True, return_tensors="pt")
20
21logits = model(**inputs).logits
22
23action_ids = torch.argmax(logits[:, :6], dim=-1).tolist()
24action_labels = [model.config.id2label[_id] for _id in action_ids]
25
26object_ids = torch.argmax(logits[:, 6:20], dim=-1).tolist()
27object_labels = [model.config.id2label[_id + 6] for _id in object_ids]
28
29location_ids = torch.argmax(logits[:, 20:24], dim=-1).tolist()
30location_labels = [model.config.id2label[_id + 20] for _id in location_ids]| s3prl | transformers | |
|---|---|---|
| test | 0.9235 | N/A |
1@article{yang2021superb,
2 title={SUPERB: Speech processing Universal PERformance Benchmark},
3 author={Yang, Shu-wen and Chi, Po-Han and Chuang, Yung-Sung and Lai, Cheng-I Jeff and Lakhotia, Kushal and Lin, Yist Y and Liu, Andy T and Shi, Jiatong and Chang, Xuankai and Lin, Guan-Ting and others},
4 journal={arXiv preprint arXiv:2105.01051},
5 year={2021}
6}