The idea
A raw audio clip is a very long list of numbers: three seconds at 22,050 samples per second is 66,150 values, most of which say little about genre. Instead of feeding that to a model, I treat genre recognition as an image problem. Each clip becomes a map of Mel-frequency cepstral coefficients (MFCCs), a compact description of the sound’s texture on a scale close to how people hear. A heavy-metal clip looks dense and bright; a classical one looks sparse. A convolutional network is good at spotting exactly that kind of visual pattern.
The data
I trained on the GTZAN collection: 1,000 tracks of 30 seconds, 100 for each of 10 genres (blues, classical, country, disco, hip-hop, jazz, metal, pop, reggae and rock).
Each track is cut into ten 3-second segments, which multiplies the number of training examples by ten. For every segment, Librosa computes 13 MFCCs with a 2,048-sample window and a hop of 512 samples, giving a grid of 130 time steps by 13 coefficients. Segments that come out shorter than that are dropped so every input has the same shape.
The model
Three convolution blocks learn increasingly complex textures, then a small dense head turns them into probabilities:
| Block | Layers |
|---|---|
| 1 | Conv2D, 32 filters, 3×3 · max pooling 3×3, stride 2 · batch normalization |
| 2 | Conv2D, 64 filters, 3×3 · max pooling 3×3, stride 2 · batch normalization |
| 3 | Conv2D, 128 filters, 2×2 · max pooling 2×2, stride 2 · batch normalization |
| Head | Flatten · Dense 64 (ReLU) · Dropout 0.3 · Dense 10 (softmax) |
It’s trained with Adam at a learning rate of 0.0001 and sparse categorical cross-entropy, for 30 epochs in batches of 32. Dropout switches off 30% of the dense neurons on each step, so the network has to rely on several cues instead of memorizing particular tracks.
Results
The data is split 70/30 into training and test sets, and the model reaches 94% accuracy on the test set.
One caveat I want to be upfront about: the split is made per segment, so different 3-second pieces of the same song can end up on both sides. That makes the test easier than meeting a completely new song, and the number is probably optimistic. Fixing that is the first item below.
The app
The Streamlit app lets anyone upload a WAV or MP3 file. It takes a 3-second slice from the middle of the track, runs it through the model and shows:
- the most likely genre and how confident the model is,
- a bar chart of the probabilities for all 10 genres,
- the MFCC heatmap itself, labelled “what the AI sees”, so you can compare how different genres look.
What I’d do next
- Split by song, not by segment, and report accuracy with cross-validation, to get an honest number for unseen music.
- Publish a confusion matrix to show which genres the model mixes up.
- Predict on several slices and average them instead of using only the middle 3 seconds, which should make the app’s answers more stable.
- Augment the data with pitch shifts, time stretching and background noise so the model copes better with real recordings.