This is a repository for Music Genre Classification Project which includes Data Collection, Feature Extraction, Training and Deployment.
With virtual environment, you can easily install all requirements in one go. To do so, follow these steps:
- Create a virtual environment
python -m venv env
- Activate the virtual environment
Windows:./env/Scripts/activate
Linux:source env/bin/actiavte
- Install requirements
pip install -r requirements.txt
Data in collected in a four step process.
- Song names fetched from Spotify API according to provided Genres.
- Youtube video URL fetched from Youtube API from the song names.
- Songs downloaded using those Youtube video URLs.
- Songs segmented into clips of specified seconds.
How to use
- Install required libraries
- FFMPEG (Add to path)
- PyTube
- PythonDotEnv
- Requests
- Acquire required API Keys (Store all acquired Keys in SongCollection/.env)
- Spotify API Keys
- Login to SpotifyDev
- Store keys as follows in .env file
SPOTIFY_CLIENT_ID = Your client id
SPOTIFY_CLIENT_SECRET = Your client secret
- Youtube API Keys
- Login to GoogleCloudConsole
- Make a Project
- Enable YouTubeAPI
- In sidetabs, go to Credentials > Create API Key
- Store keys as follows in .env file (You can use more than one. One key can be used to get URls of about 500 songs)
YOUTUBE_API_KEY0 = Your 1st api key
YOUTUBE_API_KEY1 = Your 2nd api key
. . .
- Spotify API Keys
- Run the program
-
For Song name and URL
- Make object of APICall as
ac = APICall(genre_list, number_of_songs, number_of_youtube_keys)
Example:
ac = APICall(["classical", "rock"],50, 1)
- Get song names
ac.generate_song_list()
- Get song URLs
ac.generate_song_url()
- Access song name and urls from text files
song_name ,song_url = ac.get_song_name_url()
- Make object of APICall as
-
To Download and Segment
- Make object of SongDownloader as
sc = SongDownloader(n_threads, segment_duration)
Example:
sd = SongDownloader(10, 30)
- Download and segment
sd.download_song(song_name, song_url)
- Make object of SongDownloader as
-
Feature Extraction makes use of Librosa to extract a total of 38 features from both frequenct domain and time domain. Extracted features are Mean and Standard Deviations of:
- Amplitude Envelope
- Root Mean Squared Energy
- Zero Crossing Rate
- Band Energy Ratio
- Spectral Centroid
- Bandwidth
- 13 Mel Frequency Cepstral Coefficients (MFCCs)
How to use
- Complete How to use of Data Collection
- Install required libraries
- Run the program
- Make object of FeatureExtractor as
fe = FeatureExtractor(segment_duration, n_threads)
(Segment duration must be same as Data Collection)
Example:
fe = FeatureExtractor(30, 10)
- Extract features
fe.extract_features()
- Make object of FeatureExtractor as
In this part, I have dome some preliminary exploratory data analysis along with visualization of different features that has been extracted .
The data was trained on different models with primary evalulation metrics being the F1 Score and Confusion Matrix. The data being highly imbalanced, balancing techniques like UnderSampling and OverSampling were used. Random Search was also employed to find best hyperparameters for the model.
The best model was a RandomForestClassifer with test F1 Score of 70%. This model was trained by dropping BER_Mean and BER_Std columns as these data had multiple discrepancies.
The model was extracted into a file by pickle to be used in deployment.
In Progress