Music Genre Classifier
Model Description
This is a Convolutional Neural Network (CNN) trained for music genre classification using mel-spectrograms. The model can classify music into 10 different genres from the GTZAN dataset.
Model Performance
- Test Accuracy: 99.16%
- Training Framework: PyTorch
- Input: Mel-spectrograms (128 mel bands, 4-second audio clips)
- Classes: 10 genres
Genres
blues, classical, country, disco, hiphop, jazz, metal, pop, reggae, rock
Model Architecture
- Enhanced CNN with attention mechanism
- Batch normalization and dropout for regularization
- AdaptiveAvgPool2d for global feature extraction
- Multi-layer classifier with skip connections
Usage
import torch
import librosa
import numpy as np
# Load model
checkpoint = torch.load('pytorch_model.pth', map_location='cpu')
model = GenreClassifierCNN(num_classes=10)
model.load_state_dict(checkpoint['model_state_dict'])
model.eval()
# Preprocess audio
def preprocess_audio(file_path):
y, sr = librosa.load(file_path, sr=22050, duration=4)
mel_spec = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128)
log_mel_spec = librosa.power_to_db(mel_spec, ref=np.max)
log_mel_spec = 2 * (log_mel_spec - log_mel_spec.min()) / (log_mel_spec.max() - log_mel_spec.min()) - 1
return torch.tensor(log_mel_spec).unsqueeze(0).unsqueeze(0)
# Predict
audio_tensor = preprocess_audio("your_music.wav")
with torch.no_grad():
outputs = model(audio_tensor)
predicted_class = torch.argmax(outputs, dim=1).item()
genres = ['blues', 'classical', 'country', 'disco', 'hiphop', 'jazz', 'metal', 'pop', 'reggae', 'rock']
print(f"Predicted genre: {genres[predicted_class]}")
Training Details
- Dataset: GTZAN Music Genre Dataset
- Optimizer: AdamW with weight decay
- Learning Rate: OneCycleLR scheduler
- Loss Function: Label Smoothing CrossEntropy
- Regularization: Dropout, Batch Normalization
- Data Augmentation: Time shifting, multiple overlapping chunks
Citation
If you use this model, please cite:
@misc{music_genre_classifier_2025,
title={Music Genre Classifier},
author={Your Name},
year={2025},
howpublished={\url{https://huggingface.co/your-username/music-genre-classifier}}
}
- Downloads last month
- 12