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 |

1from transformers import Wav2Vec2FeatureExtractor
2from transformers import AutoModel
3import torch
4from torch import nn
5import torchaudio.transforms as T
6from datasets import load_dataset
7
8
9# loading our model weights
10model = AutoModel.from_pretrained("m-a-p/MERT-v0", trust_remote_code=True)
11# loading the corresponding preprocessor config
12processor = Wav2Vec2FeatureExtractor.from_pretrained("m-a-p/MERT-v0",trust_remote_code=True)
13
14# load demo audio and set processor
15dataset = load_dataset("hf-internal-testing/librispeech_asr_demo", "clean", split="validation")
16dataset = dataset.sort("id")
17sampling_rate = dataset.features["audio"].sampling_rate
18
19resample_rate = processor.sampling_rate
20# make sure the sample_rate aligned
21if resample_rate != sampling_rate:
22 print(f'setting rate from {sampling_rate} to {resample_rate}')
23 resampler = T.Resample(sampling_rate, resample_rate)
24else:
25 resampler = None
26
27# audio file is decoded on the fly
28if resampler is None:
29 input_audio = dataset[0]["audio"]["array"]
30else:
31 input_audio = resampler(torch.from_numpy(dataset[0]["audio"]["array"]))
32
33inputs = processor(input_audio, sampling_rate=resample_rate, return_tensors="pt")
34with torch.no_grad():
35 outputs = model(**inputs, output_hidden_states=True)
36
37# take a look at the output shape, there are 13 layers of representation
38# each layer performs differently in different downstream tasks, you should choose empirically
39all_layer_hidden_states = torch.stack(outputs.hidden_states).squeeze()
40print(all_layer_hidden_states.shape) # [13 layer, Time steps, 768 feature_dim]
41
42# for utterance level classification tasks, you can simply reduce the representation in time
43time_reduced_hidden_states = all_layer_hidden_states.mean(-2)
44print(time_reduced_hidden_states.shape) # [13, 768]
45
46# you can even use a learnable weighted average representation
47aggregator = nn.Conv1d(in_channels=13, out_channels=1, kernel_size=1)
48weighted_avg_hidden_states = aggregator(time_reduced_hidden_states.unsqueeze(0)).squeeze()
49print(weighted_avg_hidden_states.shape) # [768]
50
511@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}
9