{"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":"markdown","source":"## Imports","metadata":{}},{"cell_type":"code","source":"import os\nfrom joblib import Parallel, delayed\nimport numpy as np\nimport pandas as pd\n\nimport librosa\nimport librosa.display\nfrom matplotlib import pyplot as plt\nfrom tqdm.notebook import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-26T20:18:29.307685Z","iopub.execute_input":"2022-03-26T20:18:29.308045Z","iopub.status.idle":"2022-03-26T20:18:31.756979Z","shell.execute_reply.started":"2022-03-26T20:18:29.307952Z","shell.execute_reply":"2022-03-26T20:18:31.755974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Loading Datasets","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv('../input/kaggle-pog-series-s01e02/train.csv')\ntest_df = pd.read_csv('../input/kaggle-pog-series-s01e02/test.csv')\n\ngenre = pd.read_csv('../input/kaggle-pog-series-s01e02/genres.csv')","metadata":{"execution":{"iopub.status.busy":"2022-03-26T20:18:31.760789Z","iopub.execute_input":"2022-03-26T20:18:31.761503Z","iopub.status.idle":"2022-03-26T20:18:31.832727Z","shell.execute_reply.started":"2022-03-26T20:18:31.761449Z","shell.execute_reply":"2022-03-26T20:18:31.832108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Basic EDA","metadata":{}},{"cell_type":"markdown","source":"### Train dataset","metadata":{}},{"cell_type":"code","source":"train_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-03-26T20:18:31.834032Z","iopub.execute_input":"2022-03-26T20:18:31.834361Z","iopub.status.idle":"2022-03-26T20:18:31.856213Z","shell.execute_reply.started":"2022-03-26T20:18:31.834322Z","shell.execute_reply":"2022-03-26T20:18:31.855643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[\"filepath\"] = \"../input/kaggle-pog-series-s01e02/\" + train_df[\"filepath\"]\ntrain_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-03-26T20:18:31.857807Z","iopub.execute_input":"2022-03-26T20:18:31.858527Z","iopub.status.idle":"2022-03-26T20:18:31.877492Z","shell.execute_reply.started":"2022-03-26T20:18:31.858484Z","shell.execute_reply":"2022-03-26T20:18:31.876553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-03-26T20:18:31.879127Z","iopub.execute_input":"2022-03-26T20:18:31.879726Z","iopub.status.idle":"2022-03-26T20:18:31.890929Z","shell.execute_reply.started":"2022-03-26T20:18:31.879682Z","shell.execute_reply":"2022-03-26T20:18:31.890111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-26T20:18:31.892212Z","iopub.execute_input":"2022-03-26T20:18:31.892504Z","iopub.status.idle":"2022-03-26T20:18:31.903304Z","shell.execute_reply.started":"2022-03-26T20:18:31.892467Z","shell.execute_reply":"2022-03-26T20:18:31.902581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[\"genre\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-03-26T20:18:31.904473Z","iopub.execute_input":"2022-03-26T20:18:31.904674Z","iopub.status.idle":"2022-03-26T20:18:31.921500Z","shell.execute_reply.started":"2022-03-26T20:18:31.904650Z","shell.execute_reply":"2022-03-26T20:18:31.920775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Test dataset","metadata":{}},{"cell_type":"code","source":"test_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-03-26T20:18:31.922694Z","iopub.execute_input":"2022-03-26T20:18:31.923400Z","iopub.status.idle":"2022-03-26T20:18:31.939378Z","shell.execute_reply.started":"2022-03-26T20:18:31.923368Z","shell.execute_reply":"2022-03-26T20:18:31.938531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df[\"filepath\"] = \"../input/kaggle-pog-series-s01e02/\" + test_df[\"filepath\"]\ntest_df[\"genre\"] = \"test\"\ntest_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-03-26T20:18:31.940745Z","iopub.execute_input":"2022-03-26T20:18:31.941294Z","iopub.status.idle":"2022-03-26T20:18:31.958572Z","shell.execute_reply.started":"2022-03-26T20:18:31.941251Z","shell.execute_reply":"2022-03-26T20:18:31.957518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-03-26T20:18:31.962524Z","iopub.execute_input":"2022-03-26T20:18:31.963355Z","iopub.status.idle":"2022-03-26T20:18:31.972677Z","shell.execute_reply.started":"2022-03-26T20:18:31.963311Z","shell.execute_reply":"2022-03-26T20:18:31.971711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-26T20:18:31.974116Z","iopub.execute_input":"2022-03-26T20:18:31.974641Z","iopub.status.idle":"2022-03-26T20:18:31.982234Z","shell.execute_reply.started":"2022-03-26T20:18:31.974600Z","shell.execute_reply":"2022-03-26T20:18:31.981148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Generating directory structure for 'Image Data Generator'","metadata":{}},{"cell_type":"code","source":"os.mkdir(\"train\")\nos.mkdir(\"test\")\nos.mkdir(\"test/test\")\n\nfor g in genre[\"genre\"]:\n    dir_name = os.path.join(\"train\", g.split(\"/\")[0].strip())\n    \n    if not os.path.isdir(dir_name):\n        os.mkdir(dir_name)\n        \n    print(dir_name)","metadata":{"execution":{"iopub.status.busy":"2022-03-26T20:18:31.983318Z","iopub.execute_input":"2022-03-26T20:18:31.984095Z","iopub.status.idle":"2022-03-26T20:18:31.998443Z","shell.execute_reply.started":"2022-03-26T20:18:31.984055Z","shell.execute_reply":"2022-03-26T20:18:31.997350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Generating Spectrograms","metadata":{}},{"cell_type":"markdown","source":"### Utility function","metadata":{}},{"cell_type":"code","source":"N_FFT = 2048\nHOP_LENGTH = 512\nN_MELS = 256\n\ndef get_spectrogram(row, folder):\n    \n    if os.path.exists(row.filepath):\n        y, sr = librosa.load(row.filepath)\n\n        S = librosa.feature.melspectrogram(y = y, sr = sr, n_fft = N_FFT, hop_length = HOP_LENGTH, n_mels = N_MELS)\n        S_DB = librosa.power_to_db(S, ref = np.max)\n\n        im_array = (S_DB - S_DB.min()) / (S_DB.max() - S_DB.min())\n        plt.imsave(os.path.join(folder, row.genre.split(\"/\")[0].strip(), str(row.song_id) + \".jpg\"), im_array)\n\n        return im_array.shape\n    \n    else:\n        return (0, 0)","metadata":{"execution":{"iopub.status.busy":"2022-03-26T20:18:31.999677Z","iopub.execute_input":"2022-03-26T20:18:31.999953Z","iopub.status.idle":"2022-03-26T20:18:32.008599Z","shell.execute_reply.started":"2022-03-26T20:18:31.999916Z","shell.execute_reply":"2022-03-26T20:18:32.007533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Training spectrograms","metadata":{}},{"cell_type":"code","source":"%%time\n\ntrain_imgs = Parallel(n_jobs = -1)(delayed(get_spectrogram)(row, \"train\") for row in tqdm(train_df.itertuples(), total = len(train_df)))","metadata":{"execution":{"iopub.status.busy":"2022-03-26T20:18:32.010001Z","iopub.execute_input":"2022-03-26T20:18:32.010299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imgs = np.array(train_imgs)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(train_imgs == 0).sum()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Testing spectrograms","metadata":{}},{"cell_type":"code","source":"%%time\n\ntest_imgs = Parallel(n_jobs = -1)(delayed(get_spectrogram)(row, \"test\") for row in tqdm(test_df.itertuples(), total = len(test_df)))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_imgs = np.array(test_imgs)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(test_imgs == 0).sum()","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}