{"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 os\nimport numpy as np\nimport pandas as pd\nfrom matplotlib import pyplot as plt\nimport tensorflow as tf\nfrom tensorflow.keras import Input, Model\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.layers import Conv2D, Dense, Concatenate, Dropout, MaxPooling2D, GlobalAveragePooling2D, LeakyReLU\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.utils import plot_model","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-16T18:27:57.711099Z","iopub.execute_input":"2022-04-16T18:27:57.711453Z","iopub.status.idle":"2022-04-16T18:27:59.472295Z","shell.execute_reply.started":"2022-04-16T18:27:57.711354Z","shell.execute_reply":"2022-04-16T18:27:59.471559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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')\ngenre = pd.read_csv('../input/kaggle-pog-series-s01e02/genres.csv')\nsubmission_df = pd.read_csv('../input/kaggle-pog-series-s01e02/sample_submission.csv')\n\nsubmission_df","metadata":{"execution":{"iopub.status.busy":"2022-04-16T18:27:59.474852Z","iopub.execute_input":"2022-04-16T18:27:59.475262Z","iopub.status.idle":"2022-04-16T18:27:59.531105Z","shell.execute_reply.started":"2022-04-16T18:27:59.475224Z","shell.execute_reply":"2022-04-16T18:27:59.530411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"genre[\"genre_folder\"] = genre[\"genre\"].str.split(\"/\", expand = True)[0].str.strip()\ngenre","metadata":{"execution":{"iopub.status.busy":"2022-04-16T18:27:59.532379Z","iopub.execute_input":"2022-04-16T18:27:59.532635Z","iopub.status.idle":"2022-04-16T18:27:59.547264Z","shell.execute_reply.started":"2022-04-16T18:27:59.532603Z","shell.execute_reply":"2022-04-16T18:27:59.546365Z"},"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-04-16T18:27:59.550040Z","iopub.execute_input":"2022-04-16T18:27:59.550405Z","iopub.status.idle":"2022-04-16T18:27:59.565450Z","shell.execute_reply.started":"2022-04-16T18:27:59.550349Z","shell.execute_reply":"2022-04-16T18:27:59.564800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df[\"filepath\"] = \"../input/kaggle-pog-series-s01e02/\" + test_df[\"filepath\"]\ntest_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-04-16T18:27:59.566468Z","iopub.execute_input":"2022-04-16T18:27:59.566725Z","iopub.status.idle":"2022-04-16T18:27:59.580318Z","shell.execute_reply.started":"2022-04-16T18:27:59.566690Z","shell.execute_reply":"2022-04-16T18:27:59.579560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 64\nTRAIN_DIR = '../input/music-classification-mel-spectrograms/train'\nTEST_DIR = '../input/music-classification-mel-spectrograms/test'\nIMG_SHAPE = (256, 1280)\nEPOCHS = 20","metadata":{"execution":{"iopub.status.busy":"2022-04-16T18:27:59.581682Z","iopub.execute_input":"2022-04-16T18:27:59.581931Z","iopub.status.idle":"2022-04-16T18:27:59.586348Z","shell.execute_reply.started":"2022-04-16T18:27:59.581898Z","shell.execute_reply":"2022-04-16T18:27:59.585627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_gen = ImageDataGenerator(rescale = 1./255, validation_split = 0.1, fill_mode = 'nearest', width_shift_range = 0.1)\n\ntrain_data_gen = image_gen.flow_from_directory(\n    batch_size = BATCH_SIZE,\n    directory = TRAIN_DIR,\n    shuffle = True,\n    target_size = IMG_SHAPE,\n    subset = \"training\"\n    )","metadata":{"execution":{"iopub.status.busy":"2022-04-16T18:27:59.587410Z","iopub.execute_input":"2022-04-16T18:27:59.588196Z","iopub.status.idle":"2022-04-16T18:28:02.864088Z","shell.execute_reply.started":"2022-04-16T18:27:59.588159Z","shell.execute_reply":"2022-04-16T18:28:02.863345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_data_gen = image_gen.flow_from_directory(\n    batch_size = BATCH_SIZE,\n    directory = TRAIN_DIR,\n    shuffle = True,\n    target_size = IMG_SHAPE,\n    subset = \"validation\"\n    )","metadata":{"execution":{"iopub.status.busy":"2022-04-16T18:28:02.865437Z","iopub.execute_input":"2022-04-16T18:28:02.865862Z","iopub.status.idle":"2022-04-16T18:28:03.083484Z","shell.execute_reply.started":"2022-04-16T18:28:02.865823Z","shell.execute_reply":"2022-04-16T18:28:03.082407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image_gen = ImageDataGenerator(rescale = 1./255)\ntest_data_gen = image_gen.flow_from_directory(\n    batch_size = BATCH_SIZE,\n    directory = TEST_DIR,\n    target_size = IMG_SHAPE,\n    class_mode = None,\n    shuffle = False\n    )","metadata":{"execution":{"iopub.status.busy":"2022-04-16T18:28:03.084682Z","iopub.execute_input":"2022-04-16T18:28:03.085018Z","iopub.status.idle":"2022-04-16T18:28:04.287375Z","shell.execute_reply.started":"2022-04-16T18:28:03.084897Z","shell.execute_reply":"2022-04-16T18:28:04.285964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_weight_dict = {}\nlabel_reverse_map = {}\ns = 0\n\nfor k, v in train_data_gen.class_indices.items():\n    c = len(os.listdir(os.path.join(TRAIN_DIR, k)))\n    s+=c\n    class_weight_dict[v] = c\n    label_reverse_map[v] = k\n    \nprint(class_weight_dict)\n\nfor i in class_weight_dict.keys():\n    class_weight_dict[i] = round(np.sqrt(s / class_weight_dict[i]), 3)\n    \nprint(class_weight_dict)\nprint(label_reverse_map)","metadata":{"execution":{"iopub.status.busy":"2022-04-16T18:28:04.290381Z","iopub.execute_input":"2022-04-16T18:28:04.290720Z","iopub.status.idle":"2022-04-16T18:28:04.312435Z","shell.execute_reply.started":"2022-04-16T18:28:04.290687Z","shell.execute_reply":"2022-04-16T18:28:04.311643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model():\n\n    input_layer = Input(shape = (*IMG_SHAPE, 3), name = \"Input\")\n\n    conv_1 = Conv2D(16, 8, 1, padding = \"same\", activation = LeakyReLU(), name = \"Conv_1\")(input_layer)\n    mp_1 = MaxPooling2D(2, name = \"MP_1\")(conv_1)\n\n    conv_2 = Conv2D(32, 8, 1, padding = \"same\", activation = LeakyReLU(), name = \"Conv_2\")(mp_1)\n    mp_2 = MaxPooling2D(2, name = \"MP_2\")(conv_2)\n    do_1 = Dropout(0.25, name = \"Dropout_1\")(mp_2)\n\n    conv_3 = Conv2D(64, 4, 1, padding = \"same\", activation = LeakyReLU(), name = \"Conv_3\")(do_1)\n    mp_3 = MaxPooling2D(2, name = \"MP_3\")(conv_3)\n\n    conv_4 = Conv2D(128, 4, 1, padding = \"same\", activation = LeakyReLU(), name = \"Conv_4\")(mp_3)\n    mp_4 = MaxPooling2D(2, name = \"MP_4\")(conv_4)\n    do_2 = Dropout(0.25, name = \"Dropout_2\")(mp_4)\n\n    conv_5 = Conv2D(256, 2, 1, padding = \"same\", activation = LeakyReLU(), name = \"Conv_5\")(do_2)\n    mp_5 = MaxPooling2D(2, name = \"MP_5\")(conv_5)\n\n    conv_6 = Conv2D(512, 2, 1, padding = \"same\", activation = LeakyReLU(), name = \"Conv_6\")(mp_5)\n    mp_6 = MaxPooling2D(2, name = \"MP_6\")(conv_6)\n    do_3 = Dropout(0.25, name = \"Dropout_3\")(mp_6)\n\n    gap_1 = GlobalAveragePooling2D(name = \"GAP_1\")(do_1)\n    gap_2 = GlobalAveragePooling2D(name = \"GAP_2\")(do_2)\n    gap_3 = GlobalAveragePooling2D(name = \"GAP_3\")(do_3)\n\n    concat = Concatenate(name = \"Concat\")([gap_1, gap_2, gap_3])\n    dense_1 = Dense(256, activation = LeakyReLU(), name = \"Dense_1\")(concat)\n    do_4 = Dropout(0.25, name = \"Dropout_4\")(dense_1)\n    dense_2 = Dense(256, activation = LeakyReLU(), name = \"Dense_2\")(do_4)\n    \n    output_layer = Dense(len(train_data_gen.class_indices.items()), activation = \"softmax\", name = \"Output\")(dense_2)\n\n    return Model(inputs = input_layer, outputs = output_layer, name = \"Music_Classification\")\n    \nmodel = get_model()\nmodel.compile(optimizer = Adam(0.0005), loss = 'categorical_crossentropy', metrics = ['accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-04-16T18:28:04.313918Z","iopub.execute_input":"2022-04-16T18:28:04.314177Z","iopub.status.idle":"2022-04-16T18:28:05.516793Z","shell.execute_reply.started":"2022-04-16T18:28:04.314143Z","shell.execute_reply":"2022-04-16T18:28:05.516123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(model, show_shapes = True, dpi = 150)","metadata":{"execution":{"iopub.status.busy":"2022-04-16T18:28:05.519385Z","iopub.execute_input":"2022-04-16T18:28:05.522220Z","iopub.status.idle":"2022-04-16T18:28:06.284050Z","shell.execute_reply.started":"2022-04-16T18:28:05.522180Z","shell.execute_reply":"2022-04-16T18:28:06.281593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nearlyStop = EarlyStopping(monitor = 'val_loss', patience = 4, restore_best_weights = True)\n\nhistory = model.fit(\n    train_data_gen,\n    validation_data = val_data_gen,\n    class_weight = class_weight_dict,\n    epochs = EPOCHS,\n    callbacks = [earlyStop],\n    workers = 4,\n    use_multiprocessing = True\n)","metadata":{"execution":{"iopub.status.busy":"2022-04-16T18:28:06.285746Z","iopub.execute_input":"2022-04-16T18:28:06.286294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\ntest_preds = model.predict(test_data_gen).argmax(axis = 1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_labels = [label_reverse_map[i] for i in test_preds]\n\npred_df = pd.DataFrame({\n    \"filenames\": test_data_gen.filenames,\n    \"predictions\": test_preds,\n    \"prediction_labels\": pred_labels\n})\n\npred_df[\"song_id\"] = pred_df[\"filenames\"].str.split(\"/\", expand = True)[1].str.split(\".\", expand = True)[0].astype(int)\npred_df","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_df = pd.merge(pred_df, genre, how=\"left\", left_on = \"prediction_labels\", right_on = \"genre_folder\")\npred_df","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_df = pd.merge(submission_df[[\"song_id\"]], pred_df, how=\"left\", on = \"song_id\")\npred_df","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_df.isnull().sum()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_df[\"genre_id\"] = pred_df[\"genre_id\"].fillna(1)\npred_df[\"genre_id\"] = pred_df[\"genre_id\"].astype(int)\npred_df","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_df[\"genre_id\"].value_counts().sort_index()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_df[[\"song_id\", \"genre_id\"]].to_csv(\"submission.csv\", index = False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}