{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline\nimport numpy as np\nfrom tqdm import tqdm\nimport IPython.display as ipd\nimport librosa\nimport librosa.display\nimport pandas as pd\nimport librosa\nimport tensorflow as tf\nimport os\n\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense,Dropout,Activation,Flatten\nfrom tensorflow.keras.optimizers import Adam\nfrom sklearn import metrics","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-12T15:17:16.347767Z","iopub.execute_input":"2022-04-12T15:17:16.348219Z","iopub.status.idle":"2022-04-12T15:17:23.811747Z","shell.execute_reply.started":"2022-04-12T15:17:16.348133Z","shell.execute_reply":"2022-04-12T15:17:23.81094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename='../input/kaggle-pog-series-s01e02/train/000004.ogg' #A file to test\nmetadata=pd.read_csv('../input/kaggle-pog-series-s01e02/train.csv') #Read the csv 2 train\naudio_dataset_path='../input/kaggle-pog-series-s01e02/train' #Direction of the songs","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:38:11.415296Z","iopub.execute_input":"2022-04-12T14:38:11.416994Z","iopub.status.idle":"2022-04-12T14:38:11.486654Z","shell.execute_reply.started":"2022-04-12T14:38:11.41691Z","shell.execute_reply":"2022-04-12T14:38:11.48578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Show the sound\nplt.figure(figsize=(14,5))\ndata,sample_rate=librosa.load(filename)\nipd.Audio(filename)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:38:11.930624Z","iopub.execute_input":"2022-04-12T14:38:11.931201Z","iopub.status.idle":"2022-04-12T14:38:14.143562Z","shell.execute_reply.started":"2022-04-12T14:38:11.931148Z","shell.execute_reply":"2022-04-12T14:38:14.142656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metadata.head(10) #Show the first 10 rows","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:38:14.145252Z","iopub.execute_input":"2022-04-12T14:38:14.14553Z","iopub.status.idle":"2022-04-12T14:38:14.159716Z","shell.execute_reply.started":"2022-04-12T14:38:14.145498Z","shell.execute_reply":"2022-04-12T14:38:14.15879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Check the num of songs in each genre\nmetadata['genre'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:38:20.575214Z","iopub.execute_input":"2022-04-12T14:38:20.575577Z","iopub.status.idle":"2022-04-12T14:38:20.592184Z","shell.execute_reply.started":"2022-04-12T14:38:20.575538Z","shell.execute_reply":"2022-04-12T14:38:20.591239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Let's read a sample audio using librosa\nlibrosa_audio_data,librosa_sample_rate=librosa.load(filename)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:38:24.676455Z","iopub.execute_input":"2022-04-12T14:38:24.676857Z","iopub.status.idle":"2022-04-12T14:38:26.130998Z","shell.execute_reply.started":"2022-04-12T14:38:24.676812Z","shell.execute_reply":"2022-04-12T14:38:26.129832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"librosa_audio_data","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:38:27.475215Z","iopub.execute_input":"2022-04-12T14:38:27.475593Z","iopub.status.idle":"2022-04-12T14:38:27.482696Z","shell.execute_reply.started":"2022-04-12T14:38:27.475554Z","shell.execute_reply":"2022-04-12T14:38:27.48193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Lets plot the librosa audio data\n# Original audio with 1 channel \nplt.figure(figsize=(12, 4))\nplt.plot(librosa_audio_data)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:38:29.388111Z","iopub.execute_input":"2022-04-12T14:38:29.388474Z","iopub.status.idle":"2022-04-12T14:38:30.361902Z","shell.execute_reply.started":"2022-04-12T14:38:29.388437Z","shell.execute_reply":"2022-04-12T14:38:30.360865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#  **Now we use the Mel-Frequency Cepstral Coefficients**","metadata":{}},{"cell_type":"markdown","source":"**Clic [here](https://www.youtube.com/watch?v=4_SH2nfbQZ8) 4 credits or know more about**","metadata":{}},{"cell_type":"code","source":"mfccs = librosa.feature.mfcc(y=librosa_audio_data, sr=librosa_sample_rate, n_mfcc=40)\nprint(mfccs.shape)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:38:37.190485Z","iopub.execute_input":"2022-04-12T14:38:37.190854Z","iopub.status.idle":"2022-04-12T14:38:37.263405Z","shell.execute_reply.started":"2022-04-12T14:38:37.190807Z","shell.execute_reply":"2022-04-12T14:38:37.262356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mfccs","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:38:38.541609Z","iopub.execute_input":"2022-04-12T14:38:38.541971Z","iopub.status.idle":"2022-04-12T14:38:38.549105Z","shell.execute_reply.started":"2022-04-12T14:38:38.541933Z","shell.execute_reply":"2022-04-12T14:38:38.548362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Extracting MFCC's For every audio file**","metadata":{}},{"cell_type":"code","source":"def features_extractor(file):\n    audio, sample_rate = librosa.load(file_name, res_type='kaiser_fast') \n    mfccs_features = librosa.feature.mfcc(y=audio, sr=sample_rate, n_mfcc=40)\n    mfccs_scaled_features = np.mean(mfccs_features.T,axis=0)\n    \n    return mfccs_scaled_features","metadata":{"execution":{"iopub.status.busy":"2022-04-12T15:17:35.105421Z","iopub.execute_input":"2022-04-12T15:17:35.106112Z","iopub.status.idle":"2022-04-12T15:17:35.111049Z","shell.execute_reply.started":"2022-04-12T15:17:35.106071Z","shell.execute_reply":"2022-04-12T15:17:35.110212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Here covert all the dataset but take a lot of time, so i run the code and save into a .npy to load every time i want and save time","metadata":{}},{"cell_type":"code","source":"### Now we iterate through every audio file and extract features using Mel-Frequency Cepstral Coefficients\n#If you want run the code only delet the \"#\" below of here\n#extracted_features=[]\n#for index_num,row in tqdm(metadata.iterrows()):\n#Here the try and except is for the lost files, if the file exist append and if the file don't exist jump the file\n#    try:\n#        file_name = audio_dataset_path + '/' + str(row[\"filename\"])\n#        final_class_labels = row['genre_id']\n#        data=features_extractor(file_name)\n#        extracted_features.append([data,final_class_labels])\n#    except:\n#        continue","metadata":{"execution":{"iopub.status.busy":"2022-04-10T13:48:13.458371Z","iopub.execute_input":"2022-04-10T13:48:13.458652Z","iopub.status.idle":"2022-04-10T16:39:23.949697Z","shell.execute_reply.started":"2022-04-10T13:48:13.458616Z","shell.execute_reply":"2022-04-10T16:39:23.94702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#And this is to save the datset in a numpy\n#np.save('songs' , extracted_features)","metadata":{"execution":{"iopub.status.busy":"2022-04-11T00:12:02.564832Z","iopub.execute_input":"2022-04-11T00:12:02.56567Z","iopub.status.idle":"2022-04-11T00:12:02.934726Z","shell.execute_reply.started":"2022-04-11T00:12:02.565616Z","shell.execute_reply":"2022-04-11T00:12:02.93345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Here load the songs of the .npy\nextracted_features = []\nextracted_features = np.load('../input/songsgener/songs.npy', allow_pickle=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T15:17:38.685456Z","iopub.execute_input":"2022-04-12T15:17:38.686257Z","iopub.status.idle":"2022-04-12T15:17:38.802664Z","shell.execute_reply.started":"2022-04-12T15:17:38.686211Z","shell.execute_reply":"2022-04-12T15:17:38.801871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Converting extracted_features to Pandas dataframe\nextracted_features_df=pd.DataFrame(extracted_features,columns=['feature','class'])\nextracted_features_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T15:17:39.280396Z","iopub.execute_input":"2022-04-12T15:17:39.280801Z","iopub.status.idle":"2022-04-12T15:17:39.306498Z","shell.execute_reply.started":"2022-04-12T15:17:39.280771Z","shell.execute_reply":"2022-04-12T15:17:39.3056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Numbers of songs\nextracted_features_df[extracted_features_df.columns[0]].count()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T15:17:42.499688Z","iopub.execute_input":"2022-04-12T15:17:42.499968Z","iopub.status.idle":"2022-04-12T15:17:42.50982Z","shell.execute_reply.started":"2022-04-12T15:17:42.499936Z","shell.execute_reply":"2022-04-12T15:17:42.50906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Here is a small experimenta unused code to delet a \"x\" class**","metadata":{}},{"cell_type":"code","source":"#Know the axix of \"x\" class\n#delet_18=[]\n#delet_18.clear()\n#for x in range(extracted_features_df[extracted_features_df.columns[0]].count()):\n#    if extracted_features_df.iat[x,1] == 10 or extracted_features_df.iat[x,1] == 18 or extracted_features_df.iat[x,1] == 17 or extracted_features_df.iat[x,1] == 16 or extracted_features_df.iat[x,1] == 15 or extracted_features_df.iat[x,1] == 14 or extracted_features_df.iat[x,1] == 13 or extracted_features_df.iat[x,1] == 12:\n#        delet_18.append(x)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:11:04.882073Z","iopub.execute_input":"2022-04-12T14:11:04.882366Z","iopub.status.idle":"2022-04-12T14:11:07.88743Z","shell.execute_reply.started":"2022-04-12T14:11:04.882331Z","shell.execute_reply":"2022-04-12T14:11:07.886626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Delet a class\n#extracted_features_df = extracted_features_df.drop(labels=delet_18, axis=0)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:11:10.375802Z","iopub.execute_input":"2022-04-12T14:11:10.376083Z","iopub.status.idle":"2022-04-12T14:11:10.383539Z","shell.execute_reply.started":"2022-04-12T14:11:10.376053Z","shell.execute_reply":"2022-04-12T14:11:10.382915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Now return the normla code**","metadata":{}},{"cell_type":"code","source":"### Split the dataset into independent and dependent dataset\nX=np.array(extracted_features_df ['feature'].tolist())\ny=np.array(extracted_features_df['class'].tolist())","metadata":{"execution":{"iopub.status.busy":"2022-04-12T15:17:45.073835Z","iopub.execute_input":"2022-04-12T15:17:45.074657Z","iopub.status.idle":"2022-04-12T15:17:45.08936Z","shell.execute_reply.started":"2022-04-12T15:17:45.074613Z","shell.execute_reply":"2022-04-12T15:17:45.088389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-12T15:17:45.309774Z","iopub.execute_input":"2022-04-12T15:17:45.310057Z","iopub.status.idle":"2022-04-12T15:17:45.314886Z","shell.execute_reply.started":"2022-04-12T15:17:45.310027Z","shell.execute_reply":"2022-04-12T15:17:45.3144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y","metadata":{"execution":{"iopub.status.busy":"2022-04-12T15:17:45.817403Z","iopub.execute_input":"2022-04-12T15:17:45.817788Z","iopub.status.idle":"2022-04-12T15:17:45.822328Z","shell.execute_reply.started":"2022-04-12T15:17:45.817757Z","shell.execute_reply":"2022-04-12T15:17:45.821652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Label Encoding\n###y=np.array(pd.get_dummies(y))\n### Label Encoder\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.preprocessing import LabelEncoder\nlabelencoder=LabelEncoder()\ny=to_categorical(labelencoder.fit_transform(y))","metadata":{"execution":{"iopub.status.busy":"2022-04-12T15:17:46.275411Z","iopub.execute_input":"2022-04-12T15:17:46.275801Z","iopub.status.idle":"2022-04-12T15:17:46.282378Z","shell.execute_reply.started":"2022-04-12T15:17:46.27577Z","shell.execute_reply":"2022-04-12T15:17:46.281689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y","metadata":{"execution":{"iopub.status.busy":"2022-04-12T15:17:47.237817Z","iopub.execute_input":"2022-04-12T15:17:47.238436Z","iopub.status.idle":"2022-04-12T15:17:47.2457Z","shell.execute_reply.started":"2022-04-12T15:17:47.238396Z","shell.execute_reply":"2022-04-12T15:17:47.245031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Train Test Split\nfrom sklearn.model_selection import train_test_split\nX_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=0)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T15:17:47.603521Z","iopub.execute_input":"2022-04-12T15:17:47.604289Z","iopub.status.idle":"2022-04-12T15:17:47.615531Z","shell.execute_reply.started":"2022-04-12T15:17:47.604241Z","shell.execute_reply":"2022-04-12T15:17:47.614823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train","metadata":{"execution":{"iopub.status.busy":"2022-04-12T15:17:48.221423Z","iopub.execute_input":"2022-04-12T15:17:48.221701Z","iopub.status.idle":"2022-04-12T15:17:48.227466Z","shell.execute_reply.started":"2022-04-12T15:17:48.221668Z","shell.execute_reply":"2022-04-12T15:17:48.226659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y","metadata":{"execution":{"iopub.status.busy":"2022-04-12T15:17:48.907779Z","iopub.execute_input":"2022-04-12T15:17:48.90817Z","iopub.status.idle":"2022-04-12T15:17:48.913886Z","shell.execute_reply.started":"2022-04-12T15:17:48.908127Z","shell.execute_reply":"2022-04-12T15:17:48.913149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-12T15:17:49.729742Z","iopub.execute_input":"2022-04-12T15:17:49.730026Z","iopub.status.idle":"2022-04-12T15:17:49.734931Z","shell.execute_reply.started":"2022-04-12T15:17:49.729994Z","shell.execute_reply":"2022-04-12T15:17:49.734198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-12T15:17:50.679202Z","iopub.execute_input":"2022-04-12T15:17:50.679622Z","iopub.status.idle":"2022-04-12T15:17:50.684788Z","shell.execute_reply.started":"2022-04-12T15:17:50.679584Z","shell.execute_reply":"2022-04-12T15:17:50.683954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-12T15:18:08.002508Z","iopub.execute_input":"2022-04-12T15:18:08.003222Z","iopub.status.idle":"2022-04-12T15:18:08.009183Z","shell.execute_reply.started":"2022-04-12T15:18:08.003181Z","shell.execute_reply":"2022-04-12T15:18:08.008394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-12T15:18:10.804625Z","iopub.execute_input":"2022-04-12T15:18:10.80535Z","iopub.status.idle":"2022-04-12T15:18:10.810343Z","shell.execute_reply.started":"2022-04-12T15:18:10.805312Z","shell.execute_reply":"2022-04-12T15:18:10.809667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y.shape[1]) #Number of casses","metadata":{"execution":{"iopub.status.busy":"2022-04-12T15:18:11.738272Z","iopub.execute_input":"2022-04-12T15:18:11.739102Z","iopub.status.idle":"2022-04-12T15:18:11.744731Z","shell.execute_reply.started":"2022-04-12T15:18:11.739041Z","shell.execute_reply":"2022-04-12T15:18:11.743967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### No of classes\nnum_labels=y.shape[1]","metadata":{"execution":{"iopub.status.busy":"2022-04-12T15:18:13.616385Z","iopub.execute_input":"2022-04-12T15:18:13.616799Z","iopub.status.idle":"2022-04-12T15:18:13.62062Z","shell.execute_reply.started":"2022-04-12T15:18:13.616759Z","shell.execute_reply":"2022-04-12T15:18:13.620087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Sequential()\n###first layer\nmodel.add(Dense(100,input_shape=(40,)))\nmodel.add(Activation('relu'))\nmodel.add(Dropout(0.5))\n###second layer\nmodel.add(Dense(200))\nmodel.add(Activation('relu'))\nmodel.add(Dropout(0.5))\n###third layer\nmodel.add(Dense(100))\nmodel.add(Activation('relu'))\nmodel.add(Dropout(0.5))\n\n###final layer\nmodel.add(Dense(num_labels))\nmodel.add(Activation('softmax'))","metadata":{"execution":{"iopub.status.busy":"2022-04-12T15:18:30.252552Z","iopub.execute_input":"2022-04-12T15:18:30.253325Z","iopub.status.idle":"2022-04-12T15:18:30.39466Z","shell.execute_reply.started":"2022-04-12T15:18:30.253267Z","shell.execute_reply":"2022-04-12T15:18:30.393998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T15:18:31.78454Z","iopub.execute_input":"2022-04-12T15:18:31.785002Z","iopub.status.idle":"2022-04-12T15:18:31.796067Z","shell.execute_reply.started":"2022-04-12T15:18:31.784965Z","shell.execute_reply":"2022-04-12T15:18:31.795371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',metrics=['accuracy'],optimizer='adam')","metadata":{"execution":{"iopub.status.busy":"2022-04-12T15:18:32.715365Z","iopub.execute_input":"2022-04-12T15:18:32.716016Z","iopub.status.idle":"2022-04-12T15:18:32.726612Z","shell.execute_reply.started":"2022-04-12T15:18:32.715979Z","shell.execute_reply":"2022-04-12T15:18:32.726082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Trianing my model\nfrom tensorflow.keras.callbacks import ModelCheckpoint\nfrom datetime import datetime \n\nnum_epochs = 100\nnum_batch_size = 32\n\ncheckpointer = ModelCheckpoint(filepath='saved_models/audio_classification.hdf5', \n                               verbose=1, save_best_only=True)\nstart = datetime.now()\n\nmodel.fit(X_train, y_train, batch_size=num_batch_size, epochs=num_epochs, validation_data=(X_test, y_test), callbacks=[checkpointer], verbose=1)\n\n\nduration = datetime.now() - start\nprint(\"Training completed in time: \", duration)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T15:18:34.133124Z","iopub.execute_input":"2022-04-12T15:18:34.133871Z","iopub.status.idle":"2022-04-12T15:18:51.893281Z","shell.execute_reply.started":"2022-04-12T15:18:34.133826Z","shell.execute_reply":"2022-04-12T15:18:51.891959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_accuracy=model.evaluate(X_test,y_test,verbose=0)\nprint(test_accuracy[1])","metadata":{"execution":{"iopub.status.busy":"2022-04-12T15:19:00.59333Z","iopub.execute_input":"2022-04-12T15:19:00.593581Z","iopub.status.idle":"2022-04-12T15:19:00.787239Z","shell.execute_reply.started":"2022-04-12T15:19:00.593553Z","shell.execute_reply":"2022-04-12T15:19:00.78643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Test the model with a file**","metadata":{}},{"cell_type":"code","source":"filename=\"../input/kaggle-pog-series-s01e02/train/000839.ogg\"\naudio, sample_rate = librosa.load(filename, res_type='kaiser_fast') \nmfccs_features = librosa.feature.mfcc(y=audio, sr=sample_rate, n_mfcc=40)\nmfccs_scaled_features = np.mean(mfccs_features.T,axis=0)\n\nprint(mfccs_scaled_features)\nmfccs_scaled_features=mfccs_scaled_features.reshape(1,-1)\nprint(mfccs_scaled_features)\nprint(mfccs_scaled_features.shape)\n\npredict_x=model.predict(mfccs_scaled_features) \nclasses_x=np.argmax(predict_x,axis=1)\npredicted_label=classes_x #The prediction\nprint(predicted_label)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T15:19:18.591442Z","iopub.execute_input":"2022-04-12T15:19:18.592212Z","iopub.status.idle":"2022-04-12T15:19:19.159083Z","shell.execute_reply.started":"2022-04-12T15:19:18.592166Z","shell.execute_reply":"2022-04-12T15:19:19.156342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Make a prediction of the test datset**","metadata":{}},{"cell_type":"code","source":"#A function to predict a file\ndef predict_file(filename):\n    audio, sample_rate = librosa.load(filename, res_type='kaiser_fast') \n    mfccs_features = librosa.feature.mfcc(y=audio, sr=sample_rate, n_mfcc=40)\n    mfccs_scaled_features = np.mean(mfccs_features.T,axis=0)\n    mfccs_scaled_features=mfccs_scaled_features.reshape(1,-1)\n    predict_x=model.predict(mfccs_scaled_features) \n    classes_x=np.argmax(predict_x,axis=1)\n    predicted_label=classes_x\n    return predicted_label","metadata":{"execution":{"iopub.status.busy":"2022-04-12T15:33:03.735046Z","iopub.execute_input":"2022-04-12T15:33:03.735788Z","iopub.status.idle":"2022-04-12T15:33:03.741482Z","shell.execute_reply.started":"2022-04-12T15:33:03.735754Z","shell.execute_reply":"2022-04-12T15:33:03.740735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Test the function\npred_wa  = predict_file(filename)\npred_woa= pred_wa[0]\nprint(pred_woa)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T15:33:04.486131Z","iopub.execute_input":"2022-04-12T15:33:04.486673Z","iopub.status.idle":"2022-04-12T15:33:05.039042Z","shell.execute_reply.started":"2022-04-12T15:33:04.486635Z","shell.execute_reply":"2022-04-12T15:33:05.038152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Load the test dataset\ntest_sng = pd.read_csv('../input/kaggle-pog-series-s01e02/test.csv') #Read csv\ndata_test ='../input/kaggle-pog-series-s01e02/test/' #Data of the songs 2 test\nprint(test_sng)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T15:33:11.377108Z","iopub.execute_input":"2022-04-12T15:33:11.377677Z","iopub.status.idle":"2022-04-12T15:33:11.397833Z","shell.execute_reply.started":"2022-04-12T15:33:11.377629Z","shell.execute_reply":"2022-04-12T15:33:11.396966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_sng[test_sng.columns[0]].count()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T15:33:44.169958Z","iopub.execute_input":"2022-04-12T15:33:44.170516Z","iopub.status.idle":"2022-04-12T15:33:44.177815Z","shell.execute_reply.started":"2022-04-12T15:33:44.170466Z","shell.execute_reply":"2022-04-12T15:33:44.177128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Make the predictions of each song in the test dataset\n#This take a long time\npredictions = []\nfor x in range(test_sng[test_sng.columns[0]].count()):\n    #Here the try and except is for the lost files, if the file exist append and if the file don't exist append a numer 4\n    try: \n        filename = data_test + test_sng.iat[x,1]\n        x  = predict_file(filename)\n        y = x[0]\n        predictions.append(y)\n    except:\n        predictions.append(4)","metadata":{"execution":{"iopub.status.busy":"2022-04-11T19:44:09.386722Z","iopub.execute_input":"2022-04-11T19:44:09.387158Z","iopub.status.idle":"2022-04-11T20:32:04.394301Z","shell.execute_reply.started":"2022-04-11T19:44:09.387125Z","shell.execute_reply":"2022-04-11T20:32:04.392874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Add extra column with the predictions\ntest_sng['genre_id'] = predictions","metadata":{"execution":{"iopub.status.busy":"2022-04-11T20:41:32.388117Z","iopub.execute_input":"2022-04-11T20:41:32.38853Z","iopub.status.idle":"2022-04-11T20:41:32.399122Z","shell.execute_reply.started":"2022-04-11T20:41:32.388488Z","shell.execute_reply":"2022-04-11T20:41:32.398102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Delet the columns filename and filepath\ndel test_sng['filename']\ndel test_sng['filepath']","metadata":{"execution":{"iopub.status.busy":"2022-04-11T20:42:02.863554Z","iopub.execute_input":"2022-04-11T20:42:02.864139Z","iopub.status.idle":"2022-04-11T20:42:02.875904Z","shell.execute_reply.started":"2022-04-11T20:42:02.8641Z","shell.execute_reply":"2022-04-11T20:42:02.875357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#See the new data\ntest_sng","metadata":{"execution":{"iopub.status.busy":"2022-04-11T20:43:10.789334Z","iopub.execute_input":"2022-04-11T20:43:10.789702Z","iopub.status.idle":"2022-04-11T20:43:10.919098Z","shell.execute_reply.started":"2022-04-11T20:43:10.789667Z","shell.execute_reply":"2022-04-11T20:43:10.91758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Save the csv\ntest_sng.to_csv('predictions_as.csv')","metadata":{"execution":{"iopub.status.busy":"2022-04-11T20:42:12.976565Z","iopub.execute_input":"2022-04-11T20:42:12.976917Z","iopub.status.idle":"2022-04-11T20:42:12.999432Z","shell.execute_reply.started":"2022-04-11T20:42:12.976877Z","shell.execute_reply":"2022-04-11T20:42:12.998513Z"},"trusted":true},"execution_count":null,"outputs":[]}]}