Views
No views yet
timm. The naming convention is adopted from other timm's ViT models.1import timm
2import torch
3import torch.nn.functional as F
4from torchaudio.compliance import kaldi
5
6# for fine-tuning, you can pass `num_classes={your number of classes}`
7model = timm.create_model("hf_hub:gaunernst/vit_base_patch16_1024_128.audiomae_as2m", pretrained=True)
8model = model.eval()
9
10MEAN = -4.2677393
11STD = 4.5689974
12
13audio = torch.randn(1, 10 * 16_000) # make sure input is 16kHz
14melspec = kaldi.fbank(audio, htk_compat=True, window_type="hanning", num_mel_bins=128) # shape (n_frames, 128)
15
16# AudioMAE only accepts 1024-frame input
17if melspec.shape[0] < 1024:
18 melspec = F.pad(melspec, (0, 0, 0, 1024 - melspec.shape[0]))
19else:
20 melspec = melspec[:1024]
21melspec = (melspec - MEAN) / (STD * 2)
22
23melspec = melspec.view(1, 1, 1024, 128) # add batch dim and channel dim
24output = model(melspec) # embeddings with shape (1, 768)
25
26# to get frame level embeddings
27output = model.forward_features(melspec) # shape (1, 513, 768)
28output = output[:, 1:] # remove [CLS] token
29output = output.unflatten(1, (1024 // 16, 128 // 16)) # (1, 64, 8, 768) -> 2D patches
30output = output.mean(2) # (1, 64, 768) -> mean pooling across mel dimension1@inproceedings{huang2022amae,
2 title = {Masked Autoencoders that Listen},
3 author = {Huang, Po-Yao and Xu, Hu and Li, Juncheng and Baevski, Alexei and Auli, Michael and Galuba, Wojciech and Metze, Florian and Feichtenhofer, Christoph}
4 booktitle = {NeurIPS},
5 year = {2022}
6}1@misc{rw2019timm,
2 author = {Ross Wightman},
3 title = {PyTorch Image Models},
4 year = {2019},
5 publisher = {GitHub},
6 journal = {GitHub repository},
7 doi = {10.5281/zenodo.4414861},
8 howpublished = {\url{https://github.com/huggingface/pytorch-image-models}}
9}