{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":70203,"databundleVersionId":8068726,"sourceType":"competition"},{"sourceId":8244903,"sourceType":"datasetVersion","datasetId":4891409},{"sourceId":8246197,"sourceType":"datasetVersion","datasetId":4870988}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Model Training","metadata":{}},{"cell_type":"markdown","source":"## Importing Libraries","metadata":{}},{"cell_type":"code","source":"import os\n\nimport warnings\nwarnings.filterwarnings(action='ignore')\n\nimport pandas as pd\nimport librosa\nimport numpy as np\n\n\nfrom sklearn.utils import shuffle\nfrom PIL import Image\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\n\nimport tensorflow as tf","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-12T08:55:15.221971Z","iopub.execute_input":"2024-05-12T08:55:15.222642Z","iopub.status.idle":"2024-05-12T08:55:30.333626Z","shell.execute_reply.started":"2024-05-12T08:55:15.222592Z","shell.execute_reply":"2024-05-12T08:55:30.332345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"RANDOM_SEED = 1337\nSAMPLE_RATE = 32000\nSPEC_SHAPE = (48, 128) # height x width\nFMIN = 500\nFMAX = 12500\n","metadata":{"execution":{"iopub.status.busy":"2024-05-15T06:32:19.611558Z","iopub.execute_input":"2024-05-15T06:32:19.611949Z","iopub.status.idle":"2024-05-15T06:32:19.616858Z","shell.execute_reply.started":"2024-05-15T06:32:19.611917Z","shell.execute_reply":"2024-05-15T06:32:19.615990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv('/kaggle/input/birdclef-2024/train_metadata.csv')","metadata":{"execution":{"iopub.status.busy":"2024-05-15T06:32:24.412092Z","iopub.execute_input":"2024-05-15T06:32:24.412832Z","iopub.status.idle":"2024-05-15T06:32:24.514720Z","shell.execute_reply.started":"2024-05-15T06:32:24.412779Z","shell.execute_reply":"2024-05-15T06:32:24.513950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.head())","metadata":{"execution":{"iopub.status.busy":"2024-05-15T06:32:29.491220Z","iopub.execute_input":"2024-05-15T06:32:29.491673Z","iopub.status.idle":"2024-05-15T06:32:29.504156Z","shell.execute_reply.started":"2024-05-15T06:32:29.491635Z","shell.execute_reply":"2024-05-15T06:32:29.503111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\n# Second, assume that birds with the most training samples are also the most common\n# A species needs at least 200 recordings with a rating above 4 to be considered common\n#birds_count = {}\n#for bird_species, count in zip(train.primary_label.unique(), \n#                               train.groupby('primary_label')['primary_label'].count().values):\n#    birds_count[bird_species] = count\n#most_represented_birds = [key for key,value in birds_count.items() if value >= 1] \n\n#TRAIN = train.query('primary_label in @most_represented_birds')\n#LABELS = sorted(TRAIN.primary_label.unique())\n\n# Let's see how many species and samples we have left\n#print('NUMBER OF SPECIES IN TRAIN DATA:', len(LABELS))\n#print('NUMBER OF SAMPLES IN TRAIN DATA:', len(TRAIN))\n#print('LABELS:', most_represented_birds)","metadata":{"execution":{"iopub.status.busy":"2024-05-11T08:54:14.650827Z","iopub.execute_input":"2024-05-11T08:54:14.651195Z","iopub.status.idle":"2024-05-11T08:54:14.657481Z","shell.execute_reply.started":"2024-05-11T08:54:14.651168Z","shell.execute_reply":"2024-05-11T08:54:14.656051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.query('rating>=4')\n\n# Second, assume that birds with the most training samples are also the most common\n# A species needs at least 200 recordings with a rating above 4 to be considered common\nbirds_count = {}\nfor bird_species, count in zip(train.primary_label.unique(), \n                               train.groupby('primary_label')['primary_label'].count().values):\n    birds_count[bird_species] = count\nmost_represented_birds = [key for key,value in birds_count.items() if value >= 200] \n\nTRAIN = train.query('primary_label in @most_represented_birds')\nLABELS = sorted(TRAIN.primary_label.unique())\n\n# Let's see how many species and samples we have left\nprint('NUMBER OF SPECIES IN TRAIN DATA:', len(LABELS))\nprint('NUMBER OF SAMPLES IN TRAIN DATA:', len(TRAIN))\nprint('LABELS:', most_represented_birds)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T06:32:32.250860Z","iopub.execute_input":"2024-05-15T06:32:32.251418Z","iopub.status.idle":"2024-05-15T06:32:32.278612Z","shell.execute_reply.started":"2024-05-15T06:32:32.251390Z","shell.execute_reply":"2024-05-15T06:32:32.277677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Extract training samples","metadata":{}},{"cell_type":"code","source":"import os\nfrom tqdm import tqdm\nfrom sklearn.utils import shuffle\n\ndef load_spectrograms(input_dir, TRAIN, LABELS, RANDOM_SEED):\n    samples = []\n\n    with tqdm(total=len(TRAIN)) as pbar:\n        for idx, row in TRAIN.iterrows():\n            pbar.update(1)\n            \n            spectrogram_filename = os.path.splitext(os.path.basename(row.filename))[0]\n                \n            for class_name in os.listdir(input_dir):\n                    \n                if class_name in LABELS:\n                    class_dir = os.path.join(input_dir, class_name)\n                    for filename in os.listdir(class_dir):\n                        sample_base = filename.split('_')[0] \n                        if sample_base == spectrogram_filename:\n                            full_path = os.path.join(class_dir, filename)\n                            samples.append(full_path)\n\n    TRAIN_SPECS = shuffle(samples, random_state=RANDOM_SEED)\n    print('SUCCESSFULLY LOADED {} SPECTROGRAMS'.format(len(TRAIN_SPECS)))\n    \n    return TRAIN_SPECS\n\n# Example usage:\n# TRAIN_SPECS = load_spectrograms('/kaggle/input/birdclef-2024-5s-spectrogram-features/features/harmonic_training', TRAIN, LABELS, RANDOM_SEED)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-15T06:32:37.301066Z","iopub.execute_input":"2024-05-15T06:32:37.301392Z","iopub.status.idle":"2024-05-15T06:32:37.310637Z","shell.execute_reply.started":"2024-05-15T06:32:37.301366Z","shell.execute_reply":"2024-05-15T06:32:37.309505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_dir='/kaggle/input/birdclef-2024-5s-spectrogram-features/features/harmonic_training'\nTRAIN_SPECS=load_spectrograms(input_dir, TRAIN, LABELS, RANDOM_SEED)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T06:32:45.493699Z","iopub.execute_input":"2024-05-15T06:32:45.494247Z","iopub.status.idle":"2024-05-15T06:34:33.470197Z","shell.execute_reply.started":"2024-05-15T06:32:45.494218Z","shell.execute_reply":"2024-05-15T06:34:33.469492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(TRAIN_SPECS))","metadata":{"execution":{"iopub.status.busy":"2024-05-12T09:43:35.522571Z","iopub.execute_input":"2024-05-12T09:43:35.523657Z","iopub.status.idle":"2024-05-12T09:43:35.529052Z","shell.execute_reply.started":"2024-05-12T09:43:35.523616Z","shell.execute_reply":"2024-05-12T09:43:35.527883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Ploting The spectrograms","metadata":{}},{"cell_type":"code","source":"# Plot the first 12 spectrograms of TRAIN_SPECS\nplt.figure(figsize=(15, 7))\nfor i in range(12):\n    spec = Image.open(TRAIN_SPECS[i])\n    spec = spec.resize((128, 48))\n    plt.subplot(3, 4, i + 1)\n    plt.title(TRAIN_SPECS[i].split(os.sep)[-1])\n    plt.imshow(spec, origin='lower')","metadata":{"execution":{"iopub.status.busy":"2024-05-12T09:43:40.347697Z","iopub.execute_input":"2024-05-12T09:43:40.348079Z","iopub.status.idle":"2024-05-12T09:43:42.683125Z","shell.execute_reply.started":"2024-05-12T09:43:40.348048Z","shell.execute_reply":"2024-05-12T09:43:42.682064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load training samples","metadata":{}},{"cell_type":"code","source":"# Parse all samples and add spectrograms into train data, primary_labels into label data\n\n\ndef process_train_data(TRAIN_SPECS,LABELS):\n\n    train_specs, train_labels = [], []\n    with tqdm(total=len(TRAIN_SPECS)) as pbar:\n        for path in TRAIN_SPECS:\n            pbar.update(1)\n\n            # Open image\n            spec = Image.open(path)\n            spec = spec.resize((128, 48))\n            # Convert to numpy array\n            spec = np.array(spec, dtype='float32')\n        \n            # Normalize between 0.0 and 1.0\n            # and exclude samples with nan \n            spec -= spec.min()\n            spec /= spec.max()\n            if not spec.max() == 1.0 or not spec.min() == 0.0:\n                continue\n\n            # Add channel axis to 2D array\n            spec = np.expand_dims(spec, -1)\n\n            # Add new dimension for batch size\n            spec = np.expand_dims(spec, 0)\n\n            # Add to train data\n            if len(train_specs) == 0:\n                train_specs = spec\n            else:\n                train_specs = np.vstack((train_specs, spec))\n\n            # Add to label data\n            target = np.zeros((len(LABELS)), dtype='float32')\n            bird = path.split(os.sep)[-2]\n            target[LABELS.index(bird)] = 1.0\n            if len(train_labels) == 0:\n                train_labels = target\n            else:\n                train_labels = np.vstack((train_labels, target))\n    \n    return train_specs,train_labels\n            ","metadata":{"execution":{"iopub.status.busy":"2024-05-12T10:26:11.001243Z","iopub.execute_input":"2024-05-12T10:26:11.003853Z","iopub.status.idle":"2024-05-12T10:26:11.018947Z","shell.execute_reply.started":"2024-05-12T10:26:11.003767Z","shell.execute_reply":"2024-05-12T10:26:11.017665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_specs,train_labels=process_train_data(TRAIN_SPECS,LABELS)","metadata":{"execution":{"iopub.status.busy":"2024-05-12T10:26:14.890725Z","iopub.execute_input":"2024-05-12T10:26:14.891909Z","iopub.status.idle":"2024-05-12T10:32:06.954704Z","shell.execute_reply.started":"2024-05-12T10:26:14.891869Z","shell.execute_reply":"2024-05-12T10:32:06.952954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_specs = train_specs[:, :, :, 0] ","metadata":{"execution":{"iopub.status.busy":"2024-05-12T10:32:13.221384Z","iopub.execute_input":"2024-05-12T10:32:13.222097Z","iopub.status.idle":"2024-05-12T10:32:13.227441Z","shell.execute_reply.started":"2024-05-12T10:32:13.222058Z","shell.execute_reply":"2024-05-12T10:32:13.226323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_labels)","metadata":{"execution":{"iopub.status.busy":"2024-05-12T10:32:23.320214Z","iopub.execute_input":"2024-05-12T10:32:23.320873Z","iopub.status.idle":"2024-05-12T10:32:23.327230Z","shell.execute_reply.started":"2024-05-12T10:32:23.320815Z","shell.execute_reply":"2024-05-12T10:32:23.326092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"train_specs shape:\", train_specs.shape)\nprint(\"train_labels shape:\", train_labels.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-12T10:37:02.468364Z","iopub.execute_input":"2024-05-12T10:37:02.468799Z","iopub.status.idle":"2024-05-12T10:37:02.475102Z","shell.execute_reply.started":"2024-05-12T10:37:02.468766Z","shell.execute_reply":"2024-05-12T10:37:02.473847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert numpy arrays to DataFrame\nspecs_df = pd.DataFrame(train_specs.reshape(train_specs.shape[0], -1))\nlabels_df = pd.DataFrame(train_labels, columns=LABELS)\n\n# Merge DataFrames on 'train_id' (assuming 'train_id' exists in your data)\nmerged_df = pd.concat([specs_df, labels_df], axis=1)\n\n# Save to CSV\noutput_dir = '/kaggle/working/'\noutput_file = os.path.join(output_dir, 'harmonic_feature.csv')\nmerged_df.to_csv(output_file, index=False)","metadata":{"execution":{"iopub.status.busy":"2024-05-12T10:10:28.119460Z","iopub.execute_input":"2024-05-12T10:10:28.119896Z","iopub.status.idle":"2024-05-12T10:11:02.719563Z","shell.execute_reply.started":"2024-05-12T10:10:28.119864Z","shell.execute_reply":"2024-05-12T10:11:02.718375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Mel Training","metadata":{}},{"cell_type":"code","source":"input_dir='/kaggle/input/birdclef-2024-5s-spectrogram-features/features/mel_training'\nTRAIN_SPECS_Mel=load_spectrograms(input_dir, TRAIN, LABELS, RANDOM_SEED)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T06:35:25.909539Z","iopub.execute_input":"2024-05-15T06:35:25.909893Z","iopub.status.idle":"2024-05-15T06:37:11.029407Z","shell.execute_reply.started":"2024-05-15T06:35:25.909853Z","shell.execute_reply":"2024-05-15T06:37:11.028443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(TRAIN_SPECS_Mel))","metadata":{"execution":{"iopub.status.busy":"2024-05-15T06:37:14.317324Z","iopub.execute_input":"2024-05-15T06:37:14.317912Z","iopub.status.idle":"2024-05-15T06:37:14.323041Z","shell.execute_reply.started":"2024-05-15T06:37:14.317881Z","shell.execute_reply":"2024-05-15T06:37:14.322104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the first 12 spectrograms of TRAIN_SPECS\nplt.figure(figsize=(15, 7))\nfor i in range(12):\n    spec = Image.open(TRAIN_SPECS_Mel[i])\n    spec = spec.resize((128, 48))\n    plt.subplot(3, 4, i + 1)\n    plt.title(TRAIN_SPECS[i].split(os.sep)[-1])\n    plt.imshow(spec, origin='lower')","metadata":{"execution":{"iopub.status.busy":"2024-05-15T06:37:16.131619Z","iopub.execute_input":"2024-05-15T06:37:16.131961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_specs_mel,train_labels_mel=process_train_data(TRAIN_SPECS_Mel,LABELS)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T06:38:49.950335Z","iopub.execute_input":"2024-05-15T06:38:49.951383Z","iopub.status.idle":"2024-05-15T06:44:03.906868Z","shell.execute_reply.started":"2024-05-15T06:38:49.951341Z","shell.execute_reply":"2024-05-15T06:44:03.905744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_specs_mel = train_specs_mel[:, :, :, 0] ","metadata":{"execution":{"iopub.status.busy":"2024-05-15T06:44:21.659466Z","iopub.execute_input":"2024-05-15T06:44:21.659803Z","iopub.status.idle":"2024-05-15T06:44:21.664366Z","shell.execute_reply.started":"2024-05-15T06:44:21.659760Z","shell.execute_reply":"2024-05-15T06:44:21.663461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_labels_mel)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T06:44:25.874384Z","iopub.execute_input":"2024-05-15T06:44:25.874733Z","iopub.status.idle":"2024-05-15T06:44:25.880308Z","shell.execute_reply.started":"2024-05-15T06:44:25.874707Z","shell.execute_reply":"2024-05-15T06:44:25.879208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"train_specs_mel shape:\", train_specs_mel.shape)\nprint(\"train_labels_mel shape:\", train_labels_mel.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T06:44:29.063620Z","iopub.execute_input":"2024-05-15T06:44:29.064505Z","iopub.status.idle":"2024-05-15T06:44:29.069161Z","shell.execute_reply.started":"2024-05-15T06:44:29.064470Z","shell.execute_reply":"2024-05-15T06:44:29.068032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert numpy arrays to DataFrame\nspecs_df_mel = pd.DataFrame(train_specs_mel.reshape(train_specs.shape[0], -1))\nlabels_df_mel = pd.DataFrame(train_labels_mel, columns=LABELS)\n\n# Merge DataFrames on 'train_id' (assuming 'train_id' exists in your data)\nmerged_df = pd.concat([specs_df_mel, labels_df_mel], axis=1)\n\n# Save to CSV\noutput_dir = '/kaggle/working/'\noutput_file = os.path.join(output_dir, 'mel_feature.csv')\nmerged_df.to_csv(output_file, index=False)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T06:44:32.362801Z","iopub.execute_input":"2024-05-15T06:44:32.363194Z","iopub.status.idle":"2024-05-15T06:45:13.732287Z","shell.execute_reply.started":"2024-05-15T06:44:32.363152Z","shell.execute_reply":"2024-05-15T06:45:13.731156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Percussive Training","metadata":{}},{"cell_type":"code","source":"input_dir='/kaggle/input/birdclef-2024-5s-spectrogram-features/features/percussive_training'\nTRAIN_SPECS_Perc=load_spectrograms(input_dir, TRAIN, LABELS, RANDOM_SEED)","metadata":{"execution":{"iopub.status.busy":"2024-05-12T10:51:05.758354Z","iopub.execute_input":"2024-05-12T10:51:05.758844Z","iopub.status.idle":"2024-05-12T10:53:22.786026Z","shell.execute_reply.started":"2024-05-12T10:51:05.758796Z","shell.execute_reply":"2024-05-12T10:53:22.784685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(TRAIN_SPECS_Perc))","metadata":{"execution":{"iopub.status.busy":"2024-05-12T10:53:46.103625Z","iopub.execute_input":"2024-05-12T10:53:46.104052Z","iopub.status.idle":"2024-05-12T10:53:46.109923Z","shell.execute_reply.started":"2024-05-12T10:53:46.104021Z","shell.execute_reply":"2024-05-12T10:53:46.108646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the first 12 spectrograms of TRAIN_SPECS\nplt.figure(figsize=(15, 7))\nfor i in range(12):\n    spec = Image.open(TRAIN_SPECS_Perc[i])\n    spec = spec.resize((128, 48))\n    plt.subplot(3, 4, i + 1)\n    plt.title(TRAIN_SPECS[i].split(os.sep)[-1])\n    plt.imshow(spec, origin='lower')","metadata":{"execution":{"iopub.status.busy":"2024-05-12T10:54:17.750128Z","iopub.execute_input":"2024-05-12T10:54:17.750551Z","iopub.status.idle":"2024-05-12T10:54:20.264375Z","shell.execute_reply.started":"2024-05-12T10:54:17.750517Z","shell.execute_reply":"2024-05-12T10:54:20.263260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_specs_perc,train_labels_perc=process_train_data(TRAIN_SPECS_Perc,LABELS)","metadata":{"execution":{"iopub.status.busy":"2024-05-12T10:58:07.407326Z","iopub.execute_input":"2024-05-12T10:58:07.408444Z","iopub.status.idle":"2024-05-12T11:04:00.156515Z","shell.execute_reply.started":"2024-05-12T10:58:07.408402Z","shell.execute_reply":"2024-05-12T11:04:00.155311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_specs_perc = train_specs_perc[:, :, :, 0] ","metadata":{"execution":{"iopub.status.busy":"2024-05-12T11:11:27.438577Z","iopub.execute_input":"2024-05-12T11:11:27.439817Z","iopub.status.idle":"2024-05-12T11:11:27.445164Z","shell.execute_reply.started":"2024-05-12T11:11:27.439767Z","shell.execute_reply":"2024-05-12T11:11:27.443904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_labels_perc)","metadata":{"execution":{"iopub.status.busy":"2024-05-12T11:12:29.197725Z","iopub.execute_input":"2024-05-12T11:12:29.198388Z","iopub.status.idle":"2024-05-12T11:12:29.203818Z","shell.execute_reply.started":"2024-05-12T11:12:29.198328Z","shell.execute_reply":"2024-05-12T11:12:29.202883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"train_specs_perc shape:\", train_specs_perc.shape)\nprint(\"train_labels_perc shape:\", train_labels_perc.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-12T11:13:45.458084Z","iopub.execute_input":"2024-05-12T11:13:45.458572Z","iopub.status.idle":"2024-05-12T11:13:45.464725Z","shell.execute_reply.started":"2024-05-12T11:13:45.458537Z","shell.execute_reply":"2024-05-12T11:13:45.463297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert numpy arrays to DataFrame\nspecs_df_perc = pd.DataFrame(train_specs_perc.reshape(train_specs.shape[0], -1))\nlabels_df_perc = pd.DataFrame(train_labels_perc, columns=LABELS)\n\n# Merge DataFrames on 'train_id' (assuming 'train_id' exists in your data)\nmerged_df = pd.concat([specs_df_perc, labels_df_perc], axis=1)\n\n# Save to CSV\noutput_dir = '/kaggle/working/'\noutput_file = os.path.join(output_dir, 'percussive_feature.csv')\nmerged_df.to_csv(output_file, index=False)","metadata":{"execution":{"iopub.status.busy":"2024-05-12T11:17:22.107403Z","iopub.execute_input":"2024-05-12T11:17:22.107909Z","iopub.status.idle":"2024-05-12T11:17:58.599313Z","shell.execute_reply.started":"2024-05-12T11:17:22.107868Z","shell.execute_reply":"2024-05-12T11:17:58.598431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##  Split Data into Train and Test","metadata":{}},{"cell_type":"code","source":"data=pd.read_csv('/kaggle/working/mel_feature.csv')","metadata":{"execution":{"iopub.status.busy":"2024-05-15T06:45:20.325326Z","iopub.execute_input":"2024-05-15T06:45:20.326281Z","iopub.status.idle":"2024-05-15T06:45:27.689119Z","shell.execute_reply.started":"2024-05-15T06:45:20.326236Z","shell.execute_reply":"2024-05-15T06:45:27.687879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = data.iloc[:, :-25]  # All columns except the last 25 columns\ny = data.iloc[:, -25:]  # Last 25 columns","metadata":{"execution":{"iopub.status.busy":"2024-05-15T06:45:39.998509Z","iopub.execute_input":"2024-05-15T06:45:39.998885Z","iopub.status.idle":"2024-05-15T06:45:40.003890Z","shell.execute_reply.started":"2024-05-15T06:45:39.998855Z","shell.execute_reply":"2024-05-15T06:45:40.002959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T06:46:31.929698Z","iopub.execute_input":"2024-05-15T06:46:31.930069Z","iopub.status.idle":"2024-05-15T06:46:31.946140Z","shell.execute_reply.started":"2024-05-15T06:46:31.930041Z","shell.execute_reply":"2024-05-15T06:46:31.945149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T06:46:35.498207Z","iopub.execute_input":"2024-05-15T06:46:35.498560Z","iopub.status.idle":"2024-05-15T06:46:35.521711Z","shell.execute_reply.started":"2024-05-15T06:46:35.498531Z","shell.execute_reply":"2024-05-15T06:46:35.520960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from 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=42)\n\n# Print the shape of the resulting subsets to verify the split\nprint(\"Shape of X_train:\", X_train.shape)\nprint(\"Shape of X_test:\", X_test.shape)\nprint(\"Shape of y_train:\", y_train.shape)\nprint(\"Shape of y_test:\", y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T06:46:41.018798Z","iopub.execute_input":"2024-05-15T06:46:41.019656Z","iopub.status.idle":"2024-05-15T06:46:41.274232Z","shell.execute_reply.started":"2024-05-15T06:46:41.019625Z","shell.execute_reply":"2024-05-15T06:46:41.273140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model","metadata":{}},{"cell_type":"code","source":"tf.random.set_seed(RANDOM_SEED)\n\n\nmodel = tf.keras.Sequential([\n    \n    # First conv block\n    tf.keras.layers.Conv2D(16, (3, 3), activation='relu', \n                           input_shape=(SPEC_SHAPE[0], SPEC_SHAPE[1], 1)),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.MaxPooling2D((2, 2)),\n    \n    # Second conv block\n    tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.MaxPooling2D((2, 2)), \n    \n\n    # Third conv block\n    tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.MaxPooling2D((2, 2)), \n    \n    # Fourth conv block\n    tf.keras.layers.Conv2D(128, (3, 3), activation='relu'),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.MaxPooling2D((2, 2)),\n    \n    # Global pooling instead of flatten()\n    tf.keras.layers.GlobalAveragePooling2D(), \n    \n    # Dense block\n    tf.keras.layers.Dense(256, activation='relu'),   \n    tf.keras.layers.Dropout(0.5),  \n    tf.keras.layers.Dense(256, activation='relu'),   \n    tf.keras.layers.Dropout(0.5),\n    \n    # Classification layer\n    tf.keras.layers.Dense(len(LABELS), activation='softmax')\n])\nprint('MODEL HAS {} PARAMETERS.'.format(model.count_params()))","metadata":{"execution":{"iopub.status.busy":"2024-05-15T06:46:46.581812Z","iopub.execute_input":"2024-05-15T06:46:46.582431Z","iopub.status.idle":"2024-05-15T06:46:46.731037Z","shell.execute_reply.started":"2024-05-15T06:46:46.582401Z","shell.execute_reply":"2024-05-15T06:46:46.730039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the model and specify optimizer, loss, and metric\nmodel.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),\n              loss=tf.keras.losses.CategoricalCrossentropy(label_smoothing=0.01),\n              metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2024-05-15T06:46:49.519483Z","iopub.execute_input":"2024-05-15T06:46:49.519866Z","iopub.status.idle":"2024-05-15T06:46:49.534383Z","shell.execute_reply.started":"2024-05-15T06:46:49.519832Z","shell.execute_reply":"2024-05-15T06:46:49.533334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks = [tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', \n                                                  patience=2, \n                                                  verbose=1, \n                                                  factor=0.5),\n             tf.keras.callbacks.EarlyStopping(monitor='val_loss', \n                                              verbose=1,\n                                              patience=5),\n             tf.keras.callbacks.ModelCheckpoint(filepath='best_model.keras',  # Change filepath here\n                                                monitor='val_loss',\n                                                verbose=0,\n                                                save_best_only=True)]\n","metadata":{"execution":{"iopub.status.busy":"2024-05-15T06:46:51.547020Z","iopub.execute_input":"2024-05-15T06:46:51.547398Z","iopub.status.idle":"2024-05-15T06:46:51.553549Z","shell.execute_reply.started":"2024-05-15T06:46:51.547365Z","shell.execute_reply":"2024-05-15T06:46:51.552636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_array = X_train.values\nX_train_reshaped = X_train_array.reshape(-1, SPEC_SHAPE[0], SPEC_SHAPE[1], 1)\nprint(\"New shape of X_train_reshaped:\", X_train_reshaped.shape)\n\n\n\ny_train_array = y_train.values\ny_train_reshaped = y_train_array.reshape(-1, 25)  \nprint(\"New shape of y_train_reshaped:\", y_train_reshaped.shape)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-15T06:46:53.939430Z","iopub.execute_input":"2024-05-15T06:46:53.940419Z","iopub.status.idle":"2024-05-15T06:46:53.946688Z","shell.execute_reply.started":"2024-05-15T06:46:53.940378Z","shell.execute_reply":"2024-05-15T06:46:53.945687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test_array = y_test.values\ntrue_labels = np.argmax(y_test_array, axis=1)\nprint(true_labels)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T06:46:56.469554Z","iopub.execute_input":"2024-05-15T06:46:56.469928Z","iopub.status.idle":"2024-05-15T06:46:56.478991Z","shell.execute_reply.started":"2024-05-15T06:46:56.469897Z","shell.execute_reply":"2024-05-15T06:46:56.478062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's train the model for a few epochs\n#model.fit(X_train_reshaped, \n#          y_train_reshaped,  \n#          batch_size=32,\n#          validation_split=0.2,\n#          callbacks=callbacks,\n#          epochs=25)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-15T06:58:45.238975Z","iopub.execute_input":"2024-05-15T06:58:45.239360Z","iopub.status.idle":"2024-05-15T06:58:45.243781Z","shell.execute_reply.started":"2024-05-15T06:58:45.239329Z","shell.execute_reply":"2024-05-15T06:58:45.242729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = X.values\nX = X.reshape(-1, SPEC_SHAPE[0], SPEC_SHAPE[1], 1)\nprint(\"New shape of X:\", X.shape)\n\ny = y.values\ny = y.reshape(-1, 25)  \nprint(\"New shape of y_train_reshaped:\", y.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T06:59:50.166060Z","iopub.execute_input":"2024-05-15T06:59:50.166447Z","iopub.status.idle":"2024-05-15T06:59:50.173233Z","shell.execute_reply.started":"2024-05-15T06:59:50.166418Z","shell.execute_reply":"2024-05-15T06:59:50.172205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Train the model and store the history\nhistory = model.fit(X, \n                    y,  \n                    batch_size=32,\n                    validation_split=0.2,\n                    callbacks=callbacks,\n                    epochs=25)\n\n# Plot training history\nplt.plot(history.history['accuracy'], label='Training Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.title('Training and Validation Accuracy')\nplt.legend()\nplt.show()\n\nplt.plot(history.history['loss'], label='Training Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.title('Training and Validation Loss')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-15T06:59:53.594976Z","iopub.execute_input":"2024-05-15T06:59:53.595389Z","iopub.status.idle":"2024-05-15T07:07:16.407734Z","shell.execute_reply.started":"2024-05-15T06:59:53.595355Z","shell.execute_reply":"2024-05-15T07:07:16.406721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Predicting X Test","metadata":{}},{"cell_type":"code","source":"X_test_array = X_test.values\nX_test_reshaped = X_test_array.reshape(-1, SPEC_SHAPE[0], SPEC_SHAPE[1], 1)\nprint(\"New shape of X_train_reshaped:\", X_train_reshaped.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-12T13:30:28.649219Z","iopub.execute_input":"2024-05-12T13:30:28.649646Z","iopub.status.idle":"2024-05-12T13:30:28.656320Z","shell.execute_reply.started":"2024-05-12T13:30:28.649611Z","shell.execute_reply":"2024-05-12T13:30:28.655046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make predictions on the test data\ny_pred = model.predict(X_test_reshaped)\n\n# Print the shape of the predictions\nprint(\"Shape of y_pred:\", y_pred.shape)\n\n# If you want to convert the predicted probabilities to class labels (assuming y_pred is one-hot encoded):\npredicted_labels = np.argmax(y_pred, axis=1)\n\n# Print the first few predicted labels\nprint(\"Predicted labels:\", predicted_labels)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-12T13:33:00.199010Z","iopub.execute_input":"2024-05-12T13:33:00.199421Z","iopub.status.idle":"2024-05-12T13:33:01.114159Z","shell.execute_reply.started":"2024-05-12T13:33:00.199380Z","shell.execute_reply":"2024-05-12T13:33:01.112926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, classification_report\n\ny_test_array = y_test.values\n\ntrue_labels = np.argmax(y_test_array, axis=1)\n\n# Compute accuracy\naccuracy = accuracy_score(true_labels, predicted_labels)\nprint(\"Accuracy:\", accuracy)\n\n# Print classification report\nprint(\"Classification Report:\")\nprint(classification_report(true_labels, predicted_labels))\n","metadata":{"execution":{"iopub.status.busy":"2024-05-12T13:33:38.633159Z","iopub.execute_input":"2024-05-12T13:33:38.634121Z","iopub.status.idle":"2024-05-12T13:33:38.657791Z","shell.execute_reply.started":"2024-05-12T13:33:38.634082Z","shell.execute_reply":"2024-05-12T13:33:38.656646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}