![]() |
VOOZH | about |
All the animals have a reaction to music. Music is a special thing that has an effect directly on our brains. Humans are the creators of different types of music, like pop, hip-hop, rap, classical, rock, and many more. Specifically, music can be classified by its genres. Our brains can detect different genres of music by default, but computers don't have this mechanism. But music genre classification has vast usage in recommendation systems, content organization, and the music industry as well. To analyze music genres, we can use machine learning. In this article, we will discuss how we can utilize transformer-based models to perform music genre classification.
Music genre classification is a challenging and complex task that involves several steps like genre analysis, embedding, and categorization of music tracks into distinct genres. All these steps involve several large calculations, which are time- and memory-consuming. Many other methods of music genre classification have already been tested, but recent studies showed that the Transformers module can effectively handle all the complex steps associated with music genre classification. The essence of using transformers in music genre classification lies in their ability to capture intricate patterns, dependencies, and temporal relationships within music data. Unlike other methods, which often struggle to represent the rich and complex structures of music, transformers outperform in modelling sequences of data. It can analyze raw audio, symbolic notation, or even textual descriptions to identify the underlying genre with remarkable accuracy.
At first, we need to install transformers, accelerate, datasets and evaluate modules to our runtime.
!pip install transformers
!pip install accelerate
!pip install datasets
!pip install evaluate
Now we will import all required Python libraries like NumPy and transformers etc.
Now we will load the GTZAN dataset which contains total 10 music genres. Then we will split it into training and testing sets(90:10).
Now we will extract the features of audio files using transformers' AutoFeatureExtractor. And define a driver function to iterate over the audio files(.wav).
To feed the dataset to the model we need to encode it.
Now we will use 'AutoModelForAudioClassification' for the music genre classifiation. We will specify various training arguments for the model as per our choice and machine's capability.
Now we will evaluate our model in the terms of Accuracy.
Output:
Epoch Training Loss Validation Loss Accuracy
1 1.180900 1.429399 0.610000
TrainOutput(global_step=450, training_loss=1.8381363932291668,
metrics= {'train_runtime': 493.46, 'train_samples_per_second': 1.822,
'train_steps_per_second': 0.912, 'total_flos': 4.089325516416e+16, 'train_loss': 1.8381363932291668, 'epoch': 1.0})
Code for Saving the model
Code for loading the model
Using this pipeline you will be able input an audio file and obtain the predicted genre along with the probability score. For the following code we have used a file of genre blue. The file can be downloaded from here.
Output:
Input:/content/sound-genre-blue.wav
Predicted Genre:
The predicted genre is: blues
The prediction score is: 0.9631124138832092
We can conclude that Music genre classification is a complex and computationally costly task. But this is required in many industries. Our model has achieved a good accuracy of 82%. However, using larger dataset can be useful to get better accuracy.