The Vision Transformer (ViT), a powerful model often associated with image tasks, has made its way into the realm of audio processing as well. This guide will walk you through how to utilize the vit_base_patch16_1024_128.audiomae_as2m model, specifically pre-trained on the extensive AudioSet-2M dataset using the innovative Self-Supervised Masked Autoencoder (MAE) methodology.
Model Overview
- Model Type: Audio Feature Backbone
- Pretrain Dataset: AudioSet-2M
- Papers:
- Original Repo: AudioMAE GitHub Repository
Getting Started with Audio MAE Model
Before diving into the code, make sure you have the necessary libraries installed, primarily timm and torchaudio. With those prerequisites met, let’s jump into how you can extract audio embeddings using the pretrained model.
Code Walkthrough
Here is how you can get audio embeddings from the model:
python
import timm
import torch
import torch.nn.functional as F
from torchaudio.compliance import kaldi
# Load the model with fine-tuning
model = timm.create_model("hf_hub:gaunernst/vit_base_patch16_1024_128.audiomae_as2m_ft", pretrained=True)
model = model.eval()
MEAN = -4.2677393
STD = 4.5689974
# Prepare your audio input
audio = torch.randn(1, 10 * 16_000) # Ensure input is 16kHz
melspec = kaldi.fbank(audio, htk_compat=True, window_type='hanning', num_mel_bins=128)
# Reshape mel spectrogram to fit model requirements
if melspec.shape[0] < 1024:
melspec = F.pad(melspec, (0, 0, 0, 1024 - melspec.shape[0]))
else:
melspec = melspec[:1024]
# Normalize and reshape for model input
melspec = (melspec - MEAN) / (STD * 2)
melspec = melspec.view(1, 1, 1024, 128) # Add batch and channel dimensions
# Generate embeddings
output = model(melspec) # Output shape is (1, 768)
An Analogy to Understand the Process
Think of the Audio Model as a chef preparing a dish with a specific recipe (the training data). The chef has a few key ingredients that he needs to prepare before starting, like spices (mean and std values for normalization) and utensils (the parameters to reshape the audio input). The input audio is like the raw ingredients that need to be processed into a form that the recipe can utilize effectively.
Just as the chef might slice, dice, and sauté to ensure the meal turns out delicious and unique, the audio input undergoes transformations (reshaping, normalization) before being fed into the model. The final dish, or in this case, audio embeddings, is the result—a culinary masterpiece ready for analysis!
Troubleshooting
If you run into issues while using the Audio MAE model, consider the following troubleshooting tips:
- Ensure that the input audio matches the required sampling rate of 16 kHz.
- Double-check the input dimensions; the model requires a mel spectrogram input of 1024 frames.
- If you get shape-related errors, revisit your reshaping code for consistency with the model's expected input.
- For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

