Speech & Audio AI Lab

Interactive labs for speech recognition, speech synthesis, and audio in general: in-browser DSP experiments, algorithms, datasets, and a learning path, without storing any recording.

43experiments

Audio Spectrogram Transformer

Audio Spectrogram Transformer (AST)

AdvancedAcoustic Models

Educational content in the form of a scientific draft that needs expert review before being adopted as an academic reference. No experimental result that was not actually run is shown here.

Definition

An audio classifier without convolution that splits the spectrogram into patches, treats them as tokens in a vision transformer (ViT), and then outputs a classification from the CLS token.

Mathematical Idea

The mel map is cut into $P\times P$ patches with stride $S$ (which may overlap), each patch is linearly projected to dimension $d$, and positional encodings and a CLS token are added. A standard transformer with self-attention processes them. Initializing the weights from a pre-trained vision model eases training.

How It Works

  1. Compute a log-mel spectrogram
  2. Split into patches with optional overlap
  3. Linear projection, then add positional encoding and a CLS token
  4. Transformer layers with self-attention
  5. Feed the CLS output to a classification layer
  6. Fine-tune on the audio task

Stages Diagram

  1. Mel Spectrogram
  2. Patches
  3. Projection + positional encoding
  4. Transformer
  5. CLS
  6. Classification

Equations

$$n_f=\left\lfloor\frac{F-P}{S}\right\rfloor+1,\quad n_t=\left\lfloor\frac{T-P}{S}\right\rfloor+1$$

Number of patches on each axis; $F,T$ are the spectrogram dimensions.

$$N=n_f\,n_t$$

Total number of patches (the CLS token is added to it).

Advantages

  • Architecture unified with vision
  • Global attention over the spectrogram
  • Benefits from image pre-training

Limitations

  • Quadratic cost in the number of patches
  • Needs more data and resources
  • Positional encoding is tied to a specific training length

Use Cases

  • Audio event classification
  • Transfer to other audio tasks
  • Research on vision-audio fusion

Applied Example

A 128×1024 spectrogram with patch size 16 and stride 10 gives 12×101 = 1212 patches; with the CLS token it becomes 1213 tokens.

Code Example

PythonImplemented and Tested
import numpy as np
def n_patches(F, T, P=16, S=10):
    nf = (F - P)//S + 1
    nt = (T - P)//S + 1
    return nf, nt, nf*nt
print(n_patches(128, 1024))          # (12, 101, 1212): spectrogram of 128 mels × 1024 frames
print(n_patches(128, 1024, S=16))    # no overlap: (8, 64, 512)
# learnable positional encoding: matrix (n_patches+1, d) with a CLS token
print((1212 + 1, 768))

References

  1. Gong et al.. AST: Audio Spectrogram Transformer. arXiv (2021). DOI
  2. Vaswani, Shazeer, Parmar, Uszkoreit, Jones, Gomez, Kaiser, Polosukhin. Attention Is All You Need. arXiv preprint (NeurIPS 2017 not shown on page) (2017). DOI
  3. Gemmeke et al.. Audio Set: An ontology and human-labeled dataset for audio events (2017). Source

Audio processing in the experiments takes place inside your browser. Audio, texts, and conversations are not saved on the server. Privacy Policy