Views
No views yet
| Name | Pre-train Paradigm | Training Data (hour) | Pre-train Context (second) | Model Size | Transformer Layer-Dimension | Feature Rate | Sample Rate | Release Date |
|---|---|---|---|---|---|---|---|---|
| MERT-v1-330M | MLM | 160K | 5 | 330M | 24-1024 | 75 Hz | 24K Hz | 17/03/2023 |
| MERT-v1-95M | MLM | 20K | 5 | 95M | 12-768 | 75 Hz | 24K Hz | 17/03/2023 |
| MERT-v0-public | MLM | 900 | 5 | 95M | 12-768 | 50 Hz | 16K Hz | 14/03/2023 |
| MERT-v0 | MLM | 1000 | 5 | 95 M | 12-768 | 50 Hz | 16K Hz | 29/12/2022 |
| music2vec-v1 | BYOL | 1000 | 30 | 95 M | 12-768 | 50 Hz | 16K Hz | 30/10/2022 |
1# from transformers import Wav2Vec2Processor
2from transformers import Wav2Vec2FeatureExtractor
3from transformers import AutoModel
4import torch
5from torch import nn
6import torchaudio.transforms as T
7from datasets import load_dataset
8
9
10# loading our model weights
11model = AutoModel.from_pretrained("m-a-p/MERT-v1-95M", trust_remote_code=True)
12# loading the corresponding preprocessor config
13processor = Wav2Vec2FeatureExtractor.from_pretrained("m-a-p/MERT-v1-95M",trust_remote_code=True)
14
15# load demo audio and set processor
16dataset = load_dataset("hf-internal-testing/librispeech_asr_demo", "clean", split="validation")
17dataset = dataset.sort("id")
18sampling_rate = dataset.features["audio"].sampling_rate
19
20resample_rate = processor.sampling_rate
21# make sure the sample_rate aligned
22if resample_rate != sampling_rate:
23 print(f'setting rate from {sampling_rate} to {resample_rate}')
24 resampler = T.Resample(sampling_rate, resample_rate)
25else:
26 resampler = None
27
28# audio file is decoded on the fly
29if resampler is None:
30 input_audio = dataset[0]["audio"]["array"]
31else:
32 input_audio = resampler(torch.from_numpy(dataset[0]["audio"]["array"]))
33
34inputs = processor(input_audio, sampling_rate=resample_rate, return_tensors="pt")
35with torch.no_grad():
36 outputs = model(**inputs, output_hidden_states=True)
37
38# take a look at the output shape, there are 13 layers of representation
39# each layer performs differently in different downstream tasks, you should choose empirically
40all_layer_hidden_states = torch.stack(outputs.hidden_states).squeeze()
41print(all_layer_hidden_states.shape) # [13 layer, Time steps, 768 feature_dim]
42
43# for utterance level classification tasks, you can simply reduce the representation in time
44time_reduced_hidden_states = all_layer_hidden_states.mean(-2)
45print(time_reduced_hidden_states.shape) # [13, 768]
46
47# you can even use a learnable weighted average representation
48aggregator = nn.Conv1d(in_channels=13, out_channels=1, kernel_size=1)
49weighted_avg_hidden_states = aggregator(time_reduced_hidden_states.unsqueeze(0)).squeeze()
50print(weighted_avg_hidden_states.shape) # [768]1@misc{li2023mert,
2 title={MERT: Acoustic Music Understanding Model with Large-Scale Self-supervised Training},
3 author={Yizhi Li and Ruibin Yuan and Ge Zhang and Yinghao Ma and Xingran Chen and Hanzhi Yin and Chenghua Lin and Anton Ragni and Emmanouil Benetos and Norbert Gyenge and Roger Dannenberg and Ruibo Liu and Wenhu Chen and Gus Xia and Yemin Shi and Wenhao Huang and Yike Guo and Jie Fu},
4 year={2023},
5 eprint={2306.00107},
6 archivePrefix={arXiv},
7 primaryClass={cs.SD}
8}