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# loading our model weights
10model = AutoModel.from_pretrained("m-a-p/MERT-v1-330M", trust_remote_code=True)
11# loading the corresponding preprocessor config
12processor = Wav2Vec2FeatureExtractor.from_pretrained("m-a-p/MERT-v1-330M",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 25 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) # [25 layer, Time steps, 1024 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) # [25, 1024]
45
46# you can even use a learnable weighted average representation
47aggregator = nn.Conv1d(in_channels=25, out_channels=1, kernel_size=1)
48weighted_avg_hidden_states = aggregator(time_reduced_hidden_states.unsqueeze(0)).squeeze()
49print(weighted_avg_hidden_states.shape) # [1024]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}