In How computers hear a sound became a spectrogram: time across, pitch up, loudness as brightness. A spectrogram is a picture. So a model built to read pictures can read sounds, and that is exactly what the Audio Spectrogram Transformer does. This lesson runs it on your device to answer a question speech recognition does not ask: not what words were said, but what is making the sound. That task is Audio classificationNaming the kinds of sound in a recording, such as speech, rain, or a siren, from a fixed list of labels. Unlike speech recognition, it says what is making the sound, not what words are said.Open in glossary.
A picture of sound, in patches
The model, AST, is from MIT (Gong, Chung, and Glass, 2021). Its input is a log-mel spectrogram with 128 bands, computed over 25 millisecond windows every 10 milliseconds, always 1,024 frames long: 10.24 seconds. Shorter sounds are padded, longer ones cut.
Then it does what a vision transformer does with a photo. It cuts the picture into 16 by 16 squares, turns each into a vector of 768 numbers, adds a learned position for each, and lets 12 transformer layers attend across all of them. One difference: the squares overlap. Each starts 10 frames or 10 bands after the last, so neighbors share 6 rows or columns. That gives 12 squares up the pitch axis and 101 along time: 1,212 patches. Two extra tokens, a classification token and a second one inherited from DeiT, gather what the model needs, and a final layer turns their average into a score for every label.
The authors did not start from random weights. They started from DeiT, a vision transformer trained on ImageNet photos, and averaged its patch layer over the three color channels so it would take a one-channel spectrogram. On AudioSet that start beat random weights clearly, from 0.366 to 0.459 in mean average precision on the full training set, the paper reports. Edges, blobs, and textures turn out to be useful in pictures of sound too.
Each label on its own
The labels come from AudioSet, Google’s collection of 2,084,320 ten-second clips from YouTube videos, each marked by people with the sounds they heard, from 527 labels (AudioSet). A clip of a street can be Speech, Car, and Siren at once, and about half of all clips contain Music and about half Speech.
So the last step is not the SoftmaxA function that turns a list of real-valued scores into probabilities that are positive and sum to 1, by exponentiating each score and dividing by the total.Taught in Predicting the next tokenOpen in glossary you met in Recognizing objects with your camera, where labels compete for one total of 100%. AST gives each label its own SigmoidThe S-shaped function 1 / (1 + e^-z), which squashes any number into the range 0 to 1 so it can be read as a probability.Taught in Classification and probabilityOpen in glossary, a separate yes-or-no probability, and was trained with binary cross-entropy on each one. This is Multi-label classificationClassification where one input can have several labels at once, such as a clip with both speech and music. Each label gets its own yes-or-no probability from a sigmoid, so the probabilities need not add up to 1.Open in glossary: the probabilities need not add up to anything.
What sound is this?
The Audio Spectrogram Transformer reads a sound's spectrogram in patches and scores 527 kinds of sound, each on its own.
Loading the recorded results
Challenges
0 of 3 complete
- Mix two sounds so that the model gives each of them its own label above 50% at the same time. (not yet)
- With both labels above 50%, switch to shared probabilities and see what happens to them. (not yet)
- Get the siren sweep heard as a siren: any siren label above 30%. (not yet)
Try this
- Play the cat, then read its labels. Then mix speech into it.
- Mix the plucked notes into the speech, and count the labels above the 50% line. Then switch to shared probabilities.
- Try the siren sweep alone, then with speech, rain, and applause.
- Watch the readout that adds up all 527 probabilities as you change sounds.
- With the model downloaded, record a sound near you and see what it hears in the padding.
What you saw
Clear recordings mostly got a fitting label at the top, though not always above 50%: the bells and the rain stayed under it. The train scored Train at 95%, and three more train labels above 50%; its 527 probabilities add up to about 4. Applause got Applause at 83%; the reading got Speech at 87%.
Mix speech over the plucked notes and both are heard at once: Speech 54%, Music 51%. Switch the same raw scores to a softmax and the two compete instead, falling to 30% and 26%. A softmax can say which label is most likely; it cannot say two things are both there.
The meow is the instructive mistake. The model hears Bleat at 63% and Sheep at 59%, and gives Cat under 5%. The clip is one and a half seconds of a Siamese cat, padded with almost nine seconds of nothing, and a single drawn-out meow does look like a bleat in a spectrogram. AudioSet’s own quality checks warn that its labels are not perfect either: checking ten clips of each, it found that 9 of those labeled Meow, and only 6 of those labeled Sheep, really contained that sound.
The made-up sounds show what the model learned from. The steady tone is a Sine wave at 84%: AudioSet has that label, with a few hundred clips. The siren sweep, a sine wave rising and falling like a wailing siren, gets no siren label at all alone. Mixed with speech, Police car (siren) comes second at 41%. The model never learned a definition of a siren, only the thousands of YouTube clips labeled Siren.
Mean average precisionOptional
AudioSet results are reported as mean average precision (mAP). For one label, rank every test clip by the model’s score for that label and compute the precision at each clip that really has the label; average those precisions. Then average over all 527 labels. A model that ranks every true clip above every false one gets 1 for that label. AST’s best single model reached 0.459 and an ensemble of models 0.485; the name of the checkpoint in the lab, ast-finetuned-audioset-10-10-0.4593, records the single model’s score and the overlap of 6 that gives patches every 10 frames and bands (Gong, Chung, and Glass, 2021). Because each label is ranked separately, mAP rewards good ranking per label, not well-calibrated probabilities, as Measuring a classifier discusses for thresholds.
Key ideas
- A spectrogram is a picture, so a vision transformer can classify sounds by reading it in patches; AST even starts from ImageNet weights.
- AST reads 10.24 seconds as 1,212 overlapping 16 by 16 patches; shorter sounds are padded.
- Each of its 527 labels has its own sigmoid, because a clip can contain several sounds. The probabilities need not add up to 1; a softmax would force them to compete.
- The model knows sounds only through its training clips: it scores whole scenes, can mistake one animal for another, and hears what tended to occur together.