Skip to content

Commit

Permalink
intialCommit
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshupathak95 committed Jul 24, 2023
0 parents commit 9129c8a
Show file tree
Hide file tree
Showing 2,019 changed files with 11,680 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/deep-learning-music-genre-classification.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

279 changes: 279 additions & 0 deletions .ipynb_checkpoints/music-genre-classification-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Music Genre Classification"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"pycharm": {
"is_executing": true
}
},
"outputs": [],
"source": [
"from python_speech_features import mfcc\n",
"import scipy.io.wavfile as wav\n",
"import numpy as np\n",
"\n",
"from tempfile import TemporaryFile\n",
"\n",
"import os\n",
"import pickle\n",
"import random\n",
"import operator\n",
"\n",
"import math"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# function to get the distance between feature vecotrs and find neighbors\n",
"def getNeighbors(trainingSet, instance, k):\n",
" distances = []\n",
" for x in range(len(trainingSet)):\n",
" dist = distance(trainingSet[x], instance, k) + distance(instance, trainingSet[x], k)\n",
" distances.append((trainingSet[x][2], dist))\n",
"\n",
" distances.sort(key=operator.itemgetter(1))\n",
" neighbors = []\n",
" for x in range(k):\n",
" neighbors.append(distances[x][0])\n",
" \n",
" return neighbors"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# identify the class of the instance\n",
"def nearestClass(neighbors):\n",
" classVote = {}\n",
"\n",
" for x in range(len(neighbors)):\n",
" response = neighbors[x]\n",
" if response in classVote:\n",
" classVote[response] += 1\n",
" else:\n",
" classVote[response] = 1\n",
"\n",
" sorter = sorted(classVote.items(), key = operator.itemgetter(1), reverse=True)\n",
"\n",
" return sorter[0][0]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# function to evaluate the model\n",
"def getAccuracy(testSet, prediction):\n",
" correct = 0\n",
" for x in range(len(testSet)):\n",
" if testSet[x][-1] == predictions[x]:\n",
" correct += 1\n",
" \n",
" return (1.0 * correct) / len(testSet)\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# directory that holds the wav files\n",
"directory = \"Data/genres_original/\"\n",
"\n",
"# binary file where we will collect all the features extracted using mfcc (Mel Frequency Cepstral Coefficients)\n",
"f = open(\"my.dat\", 'wb')\n",
"\n",
"i = 0\n",
"\n",
"for folder in os.listdir(directory):\n",
" i += 1\n",
" if i == 11:\n",
" break\n",
" for file in os.listdir(directory+folder): \n",
" try:\n",
" (rate, sig) = wav.read(directory+folder+\"/\"+file)\n",
" mfcc_feat = mfcc(sig, rate, winlen=0.020, appendEnergy=False)\n",
" covariance = np.cov(np.matrix.transpose(mfcc_feat))\n",
" mean_matrix = mfcc_feat.mean(0)\n",
" feature = (mean_matrix, covariance, i)\n",
" pickle.dump(feature, f)\n",
" except Exception as e:\n",
" print('Got an exception: ', e, ' in folder: ', folder, ' filename: ', file) \n",
"\n",
"f.close()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# Split the dataset into training and testing sets respectively\n",
"dataset = []\n",
"\n",
"def loadDataset(filename, split, trSet, teSet):\n",
" with open('my.dat', 'rb') as f:\n",
" while True:\n",
" try:\n",
" dataset.append(pickle.load(f))\n",
" except EOFError:\n",
" f.close()\n",
" break\n",
" for x in range(len(dataset)):\n",
" if random.random() < split:\n",
" trSet.append(dataset[x])\n",
" else:\n",
" teSet.append(dataset[x])\n",
"trainingSet = []\n",
"testSet = []\n",
"loadDataset('my.dat', 0.66, trainingSet, testSet)\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"def distance(instance1 , instance2 , k ):\n",
" distance =0 \n",
" mm1 = instance1[0] \n",
" cm1 = instance1[1]\n",
" mm2 = instance2[0]\n",
" cm2 = instance2[1]\n",
" distance = np.trace(np.dot(np.linalg.inv(cm2), cm1)) \n",
" distance+=(np.dot(np.dot((mm2-mm1).transpose() , np.linalg.inv(cm2)) , mm2-mm1 )) \n",
" distance+= np.log(np.linalg.det(cm2)) - np.log(np.linalg.det(cm1))\n",
" distance-= k\n",
" return distance"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# making predictions using KNN\n",
"leng = len(testSet)\n",
"predictions = []\n",
"for x in range(leng):\n",
" predictions.append(nearestClass(getNeighbors(trainingSet, testSet[x], 5)))\n",
"\n",
"accuracy1 = getAccuracy(testSet, predictions)\n",
"print(accuracy1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# testing the code with external samples\n",
"# URL: https://uweb.engr.arizona.edu/~429rns/audiofiles/audiofiles.html\n",
"\n",
"test_dir = \"Test/\"\n",
"# test_file = test_dir + \"test.wav\"\n",
"test_file = test_dir + \"3.wav\"\n",
"# test_file = test_dir + \"test4.wav\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(rate, sig) = wav.read(test_file)\n",
"mfcc_feat = mfcc(sig, rate, winlen=0.020, appendEnergy=False)\n",
"covariance = np.cov(np.matrix.transpose(mfcc_feat))\n",
"mean_matrix = mfcc_feat.mean(0)\n",
"feature = (mean_matrix, covariance, i)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from collections import defaultdict\n",
"results = defaultdict(int)\n",
"\n",
"directory = \"Data/genres_original/\"\n",
"\n",
"i = 1\n",
"for folder in os.listdir(directory):\n",
" results[i] = folder\n",
" i += 1\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"pred = nearestClass(getNeighbors(dataset, feature, 5))\n",
"print(results[pred])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Binary file added Data/corrupted/jazz.00054.wav
Binary file not shown.
Loading

0 comments on commit 9129c8a

Please sign in to comment.