Music Classification aims to understand the music semantics over various different features. In this project we have proposed a novel ensemble model for the Music Genre Classification task which try to classify music based on its genre. The final model is created by combining the predictions from multiple models: Random forest algorithm and Convolutional Neural Network. It scores on test set an accuracy of 87%.
This project used the Cookiecutter๐ช template for project strucutre and the
conventional commit specification for adding human and machine readable meaning to commit messages.
It is composed of 3 components:
- WEB APP Module:
- Observability Module:
- Grafana dashboard created with:
- Prometheus
- Tempo
- Loki
- Grafana dashboard created with:
- PyPI package for song features extraction:
Project detailed tree structure ๐ [CLICK TO EXPAND]
๐ฆmusic-genre-classification
โฃ ๐.dvc
โฃ ๐.github
โ โ ๐workflows : project pipelines
โ โ โฃ ๐aws_deploy_api.yml : backend app aws deploy
โ โ โฃ ๐aws_deploy_app.yml : frontend app aws deploy
โ โ โฃ ๐linter.yml : code checks and tests
โ โ โ ๐release_to_pypi.yml : pypi package release
โ โฃ ๐.gitignore
โฃ ๐data : Hosted Dataset
โ โฃ ๐processed : PROCESSED DATA - DVC hosted
โ โ
โ โ โฃ ๐gtzan_data : 1ยฐ dataset
โ โ โ โฃ ๐x_test.pkl : test dataset features
โ โ โ โฃ ๐x_train.pkl : train dataset features
โ โ โ โฃ ๐x_train_split.pkl : train subset dataset features
โ โ โ โฃ ๐x_validation.pkl : test subset dataset features
โ โ โ โฃ ๐y_test.pkl : test dataset labels
โ โ โ โฃ ๐y_train.pkl : train dataset labels
โ โ โ โฃ ๐y_train_split.pkl : train subset dataset labels
โ โ โ โ ๐y_validation.pkl : test subset dataset labels
โ โ โ
โ โ โ ๐mfcc_data : 2ยฐ dataset
โ โ โ โฃ ๐x_test.pkl : ...
โ โ โ โฃ ๐x_train.pkl : ...
โ โ โ โฃ ๐x_train_split.pkl : ...
โ โ โ โฃ ๐x_validation.pkl : ...
โ โ โ โฃ ๐y_test.pkl : ...
โ โ โ โฃ ๐y_train.pkl : ...
โ โ โ โฃ ๐y_train_split.pkl : ...
โ โ โ โ ๐y_validation.pkl : ...
โ โ
โ โ ๐raw - RAW DATA - Google Drive hosted
โ โ โ ๐dataset : 1000 songs, 10x genre
โ โ โ โฃ ๐genres_original : Original .wav song
โ โ โ โ โฃ ๐blues : 100 blues songs
โ โ โ โ โฃ ๐classical : 100 classical songs
โ โ โ โ โฃ ๐country : 100 contry songs
โ โ โ โ โฃ ๐disco : ...
โ โ โ โ โฃ ๐hiphop : ...
โ โ โ โ โฃ ๐jazz : ...
โ โ โ โ โฃ ๐metal : ...
โ โ โ โ โฃ ๐pop : ...
โ โ โ โ โฃ ๐reggae : ...
โ โ โ โ โ ๐rock : 100 rock songs
โ โ โ โ ๐features_3_sec.csv : Song features
โฃ ๐notebooks
โ โฃ ๐audio_augmentation.ipynb : Song augmentation notebook
โ โ ๐feat_extractor.ipynb : Song features extractor
โฃ ๐observability : Observability module
โ โฃ ๐grafana
โ โ โฃ ๐dashboards
โ โ โ โ ๐dashboards.json : Grafana dashboard implementation
โ โ โฃ ๐dashboards.yml : Grafana config
โ โ โฃ ๐data_source.yml : Grafana data source
โ โ โ ๐grafana.ini
โ โฃ ๐prometheus
โ โ โฃ ๐alert.yml : Prometheus alerts
โ โ โ ๐prometheus.yml : Prometheus config
โ โ ๐tempo
โ โ โ ๐tempo.yml : Tempo config
โฃ ๐reports
โ โฃ ๐figures
โ โฃ ๐history : Pipeline track files
โ โ โฃ ๐gtzan_history.json
โ โ โ ๐mfcc_history.json
โ โ ๐tests : Test track files
โ โ โฃ ๐deep_checks.json : โโ
โ โ โฃ ๐deep_gtzan_checks.html : โฃโโ> Deep checks reports file
โ โ โ ๐deep_mfcc_checks.html : โโ
โฃ ๐src
โ โฃ ๐api : App BE folder
โ โ โฃ ๐entities : Api models
โ โ โ โฃ ๐model_allowed_enum.py
โ โ โ โฃ ๐predict_model_request.py
โ โ โฃ ๐api_rest.py : Api controller
โ โ โฃ ๐music_prediction.py : Api services
โ โ
โ โฃ ๐app : App FE folder
โ โ โฃ ๐gradio_app.py : App main
โ โฃ ๐data : Data modeling
โ โ โฃ ๐data_utils.py
โ โ โฃ ๐make_dataset.py
โ โฃ ๐feat_extractor : PyPi package used in APP
โ โ โฃ ๐feat_extractor.py
โ โฃ ๐models
โ โ โฃ ๐classes
โ โ โ โฃ ๐base_model.py : Common Model
โ โ โ โฃ ๐gtzan_model.py
โ โ โ โฃ ๐mfcc_model.py
โ โ โฃ ๐evaluation.py : Model evaluation utils
โ โ โฃ ๐model_utils.py : Model creation utils
โ โ โฃ ๐predict_model.py : Pipeline script for testing
โ โ โฃ ๐train_model.py : Pipeline script for training
โ โฃ ๐visualization
โ โ โฃ ๐visualize.py : Song feature visualization
โ โฃ ๐pathUtils.py : Relative project paths
โ โฃ ๐setup.py
โฃ ๐tests
โ โฃ ๐api_tests
โ โ โ ๐test_api.py : Unit tests - API
โ โฃ ๐dataset_tests
โ โ โฃ ๐test_dataset_integrity.py : Integrity tests - DATASET
โ โ โ ๐test_dataset_util.py : Unit tests - DATASET
โ โฃ ๐feat_extractor_tests
โ โ โ ๐test_feat_extractor.py : Unit tests - PyPI package
โ โฃ ๐models_tests
โ โ โฃ ๐test_behavioral_model.py : Behavioral Tests - MODEL
โ โ โ ๐test_model.py : Unit tests - MODEL
โ โฃ ๐path_utils_tests
โ โ โ ๐test_path_utils.py : Unit tests - PATH UTILS
โ โฃ ๐resources
โ โ โฃ ๐augmented
โ โ โ โฃ ๐noise
โ โ โ โ ๐shift_time
โ โ โ ๐hip_hop_test.wav
โ โฃ ๐test_utils
โ โ โฃ ๐mock_dataset.py
โ โ โฃ ๐utils.py
โฃ ๐docker-compose.yml : docker compose for BE/FE/Observability
โฃ ๐Dockerfile-be : BE docker file
โฃ ๐Dockerfile-fe : FE docker file
โฃ ๐dvc.yaml : DVC pipeline file
โฃ ๐params.yaml : DVC pipeline params
โฃ ๐requirements.txt
โฃ ๐requirements_be.txt
โฃ ๐requirements_fe.txt
โฃ ๐setup.py : Src folder installation
The Music Genre classifier is an ensemble model which combines:
The models described above use the following datasets respectively:
Dagshub is a Github's inspired platform, specifically created for data science projects, that allows to host, version, and manage code, data, models, experiments, Dagshub is free and open-source.
Music Genre Classification - Dagshub Repository
DVC is a software, based on Git, that allows to version data and track data science experiments. In this project, the contents of the data folder is stored and tracked using DVC. The remote storage used is the one offered by Dagshub.
The Python Package Index (PyPI) is a repository of software for the Python programming language.
PyPI helps you find and install software developed and shared by the Python community.
In order to split the models module and the GUI app, the share logic, for features extraction, is exported as pypy package.
This choice provides not only a logical separation but also allowed to divide this project into 3 sub-project:
- one for the model module
- one for the app
- one for the pypi package
Docker is a software platform that allows you to build, test, and deploy applications quickly. It is possibile to run the entire project:
- APP
- API
- GRAFANA
- PROMETHEUS
- LOKI
- TEMPO just by run the command
docker compose up
or to build them using the parameter
docker compose up --build
DVC allows not only to version data, but also to create fully reproducible pipelines. The pipelines are defined using the CLI or by manually editing the dvc.yaml file.
A pipelines of 5 steps has been defined:
- prepare: dowload dataset if not exitst from a google drive source, load GTZAN dataset and create MFCC dataset.
- train gtzan: train model on the train data of the GTZAN dataset
- train mfcc: train model on train data of the MFCC dataset
- test gtzan: test model on the test data of the GTZAN dataset
- test mfcc: test model on the test data of the MFCC dataset
The pipeline can be configured using the params.yaml file. This file contains configurations for the type of the model. By setting the correct params, it is possible to choose which model should be trained or tested.
MLFLow is a software that allows to track Machine Learning experiments and models. It stores the metrics of the experiments, allowing the developer to compare different models and parameters. Also, allows to store the models and retrieve them when needed.
In this project MLflow tracks every experiment, params and metrics which are available for consultation in a convenient GUI.
The .env file is not shared for security reason. But the env of this project contain the following variables:
- MLFLOW_TRACKING_URI
- MLFLOW_TRACKING_USERNAME
- MLFLOW_TRACKING_PASSWORD
- API_URL
In the github actions the env variables described above are needed to run the different pipeline. In order to store them
in a safe place, the following github secrets has been defined:
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
- MLFLOW_TRACKING_PASSWORD
- MLFLOW_TRACKING_URI
- MLFLOW_TRACKING_USERNAME
- PYPI_API_TOKEN
- PYPI_USERNAME
- Trigger: src/api or src/app folder modified
- Action: aws deploy
- Trigger: every commit
- Action: code checks (better explain in next section)
- Trigger: new tag created
- Action: pypi release
This project integrates pylint, which is a static code analyser for Python, which checks the quality of the source code.
The code has been rated at 8.53/10
This project integrates Flake8, which is a linter that verifies pep8, pyflakes, and circular complexity
This project integrates pynblint, which is a static code analyser for Python notebooks.
For code formatting it is used autopep8 which automatically formats Python code to conform to the PEP 8 style guide.
$ pip install --upgrade autopep8
$ cd <folder-to-format>
$ autopep8 --in-place --recursive .
Pytest is a Python testing framework. This project integrates pytest for unit testing of the code.
Deepchecks Open Source is a python library for data scientists and ML engineers. The package includes extensive test suites for machine learning models and data, built in a way thatโs flexible, extendable and editable.
And the genererated report are:
Behavioral testing is concerned with testing different capabilities of a system by validating the output, without any knowledge of the internal structure. In this project the following Behavioural tests have been made:
- Test with normal music
- Test with augmented music:
- add noise to the song
- change the order of some song parts in different direction More details in Behavioural Test Reports
Both the modules app and api are deployed on aws serverless Fargate instance.
The api docs are avaiable here
The api rest exposed are:
- /
- happy path for server test
- /predict/music
- used for predict music genre
The app module is available here
The FE code is generated by using gradio python package. It is a powerful library for machine learning frontend application.
With this snippet:
demo = gradio.Interface(
fn=predict,
inputs=gradio.Audio(),
outputs=gradio.Label(label='Predicted Genre'),
allow_flagging='never',
title='Music Genre Classification',
description='This is a Music Genre Classification model based on a novel ensemble approach'
)
gradio generates this interface: You can drop audio and click predict. In a local env it takes 10 seconds to predict the genre but on AWS it takes 50 seconds because of low ram ECS instance (0.5 GB)
Application monitoring is the process of monitoring an application's performance, availability, and end-user experience to ensure the application is functioning properly.
Grafana allows you to query, visualize, alert on and understand your metrics no matter where they are stored. In this project the Grafana dashboards are build on these three components:
- Traces with Tempo and OpenTelemetry Python SDK
- Metrics with Prometheus and Prometheus Python Client
- Logs with Loki
The dashboard implemented is:
It consists of 4 panels:
- pie chart for genre predicted
- number of prediction graph
- highest ram capacity used indicator
- log section
Application monitoring is important not only it is necessary to track an application's performance but also identify when and where along the journey an abnormality was found and why it happened.
Better Uptime send notification when a server is down. In this project the free plan it is used and the notification are available only by mails.
These are the monitored servers (API and APP):
Dashboard details:
In case of down, this is the mail received:
Finally, if you are a programmer, check this utils !!