{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":59093,"databundleVersionId":7469972,"sourceType":"competition"},{"sourceId":7491819,"sourceType":"datasetVersion","datasetId":4361975}],"dockerImageVersionId":30636,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report, confusion_matrix\nimport tensorflow as tf\nimport os\nimport torch","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-27T12:45:42.106175Z","iopub.execute_input":"2024-01-27T12:45:42.106481Z","iopub.status.idle":"2024-01-27T12:46:01.035395Z","shell.execute_reply.started":"2024-01-27T12:45:42.106449Z","shell.execute_reply":"2024-01-27T12:46:01.034292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.environ[\"CUDA_VISIBLE_DEVICES\"]=\"0,1\"\n\ngpus = tf.config.list_physical_devices('GPU')\nif len(gpus)<=1: \n    strategy = tf.distribute.OneDeviceStrategy(device=\"/gpu:0\")\n    print(f'Using {len(gpus)} GPU')\nelse: \n    strategy = tf.distribute.MirroredStrategy()\n    print(f'Using {len(gpus)} GPUs')\n    \nMIX = True\nif MIX:\n    tf.config.optimizer.set_experimental_options({\"auto_mixed_precision\": True})\n    print('Mixed precision enabled')\nelse:\n    print('Using full precision')","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:01.038067Z","iopub.execute_input":"2024-01-27T12:46:01.039240Z","iopub.status.idle":"2024-01-27T12:46:02.070049Z","shell.execute_reply.started":"2024-01-27T12:46:01.039185Z","shell.execute_reply":"2024-01-27T12:46:02.069051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sample_submission_df = pd.read_csv('/kaggle/input/hms-harmful-brain-activity-classification/sample_submission.csv')\n# sample_submission_df.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:02.071336Z","iopub.execute_input":"2024-01-27T12:46:02.071693Z","iopub.status.idle":"2024-01-27T12:46:02.076149Z","shell.execute_reply.started":"2024-01-27T12:46:02.071658Z","shell.execute_reply":"2024-01-27T12:46:02.075140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sample_submission_df.head(5)","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:02.077462Z","iopub.execute_input":"2024-01-27T12:46:02.078208Z","iopub.status.idle":"2024-01-27T12:46:02.085728Z","shell.execute_reply.started":"2024-01-27T12:46:02.078173Z","shell.execute_reply":"2024-01-27T12:46:02.084775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/hms-harmful-brain-activity-classification/train.csv')\ntrain_data.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:02.087199Z","iopub.execute_input":"2024-01-27T12:46:02.087473Z","iopub.status.idle":"2024-01-27T12:46:02.398823Z","shell.execute_reply.started":"2024-01-27T12:46:02.087449Z","shell.execute_reply":"2024-01-27T12:46:02.397824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_data.head(20)","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:02.402885Z","iopub.execute_input":"2024-01-27T12:46:02.403260Z","iopub.status.idle":"2024-01-27T12:46:02.407899Z","shell.execute_reply.started":"2024-01-27T12:46:02.403225Z","shell.execute_reply":"2024-01-27T12:46:02.406907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sns.countplot(x='expert_consensus', data=train_data)\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:02.409220Z","iopub.execute_input":"2024-01-27T12:46:02.409543Z","iopub.status.idle":"2024-01-27T12:46:02.418966Z","shell.execute_reply.started":"2024-01-27T12:46:02.409516Z","shell.execute_reply":"2024-01-27T12:46:02.417813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = ['seizure', 'lpd', 'gpd', 'lrda', 'grda', 'other']\nclass_name_to_index = {'Seizure' : 0 , 'LPD' : 1 , \n                       'LRDA' : 3 , 'GPD' : 2 , \n                       'GRDA' : 4 , 'Other' : 5}\n\nplt.figure(figsize=(15, 10)) \n\nfor i, class_name in enumerate(class_names):\n    plt.subplot(2, 3, i+1) \n    sns.countplot(x=f'{class_name}_vote', data=train_data)\n    plt.title(f'Distribution of {class_name} votes')\n    plt.tight_layout()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:02.420535Z","iopub.execute_input":"2024-01-27T12:46:02.420934Z","iopub.status.idle":"2024-01-27T12:46:05.243025Z","shell.execute_reply.started":"2024-01-27T12:46:02.420897Z","shell.execute_reply":"2024-01-27T12:46:05.241900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.hist(bins=10, figsize=(15, 20), layout=(7, 2))\nplt.suptitle('Feature Distributions')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:05.244652Z","iopub.execute_input":"2024-01-27T12:46:05.245015Z","iopub.status.idle":"2024-01-27T12:46:08.102588Z","shell.execute_reply.started":"2024-01-27T12:46:05.244985Z","shell.execute_reply":"2024-01-27T12:46:08.101561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vote_columns = [f'{name}_vote' for name in class_names]\ncorr_matrix = train_data[vote_columns].corr()\n\nplt.figure(figsize=(12, 8))\nsns.heatmap(corr_matrix, annot=True, fmt='.2f', cmap='viridis')\nplt.title('Correlation Matrix for Vote Columns')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:08.103957Z","iopub.execute_input":"2024-01-27T12:46:08.104335Z","iopub.status.idle":"2024-01-27T12:46:08.566255Z","shell.execute_reply.started":"2024-01-27T12:46:08.104299Z","shell.execute_reply":"2024-01-27T12:46:08.565354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.figure(figsize=(15, 10))\n\n# for i, col in enumerate([f'{name}_vote' for name in class_names]):\n#     plt.subplot(2, 3, i+1)\n#     sns.boxplot(y='expert_consensus', x=col, data=train_data)\n#     plt.title(f'Box Plot of {col} vs Expert Consensus')\n#     plt.tight_layout() \n\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:08.567457Z","iopub.execute_input":"2024-01-27T12:46:08.567745Z","iopub.status.idle":"2024-01-27T12:46:08.571937Z","shell.execute_reply.started":"2024-01-27T12:46:08.567719Z","shell.execute_reply":"2024-01-27T12:46:08.570916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eeg_dir = '/kaggle/input/hms-harmful-brain-activity-classification/train_eegs'\nspectrogram_dir = '/kaggle/input/hms-harmful-brain-activity-classification/train_spectrograms'\nmetadata_path = '/kaggle/input/hms-harmful-brain-activity-classification/train.csv'\n","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:08.573122Z","iopub.execute_input":"2024-01-27T12:46:08.573407Z","iopub.status.idle":"2024-01-27T12:46:08.587892Z","shell.execute_reply.started":"2024-01-27T12:46:08.573382Z","shell.execute_reply":"2024-01-27T12:46:08.586894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_data(ids, file_dir):\n    file_path = f\"{file_dir}/{int(ids)}.parquet\"\n    data_df = pd.read_parquet(file_path)\n    return data_df\n\ndef load_eeg_data(ids):\n    return load_data(ids, eeg_dir)\n\ndef load_spectrogram_data(ids):\n    return load_data(ids, spectrogram_dir).drop(columns=['time'])","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:08.589193Z","iopub.execute_input":"2024-01-27T12:46:08.589486Z","iopub.status.idle":"2024-01-27T12:46:08.600419Z","shell.execute_reply.started":"2024-01-27T12:46:08.589461Z","shell.execute_reply":"2024-01-27T12:46:08.599475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_eeg_example = load_eeg_data(1628180742)\n# df_eeg_example.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:08.601526Z","iopub.execute_input":"2024-01-27T12:46:08.601817Z","iopub.status.idle":"2024-01-27T12:46:08.611433Z","shell.execute_reply.started":"2024-01-27T12:46:08.601774Z","shell.execute_reply":"2024-01-27T12:46:08.610588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_spectro_example = load_spectrogram_data(999431)\n# df_spectro_example.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:08.612507Z","iopub.execute_input":"2024-01-27T12:46:08.612781Z","iopub.status.idle":"2024-01-27T12:46:08.622652Z","shell.execute_reply.started":"2024-01-27T12:46:08.612757Z","shell.execute_reply":"2024-01-27T12:46:08.621753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_spectro_example.columns","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:08.623740Z","iopub.execute_input":"2024-01-27T12:46:08.624083Z","iopub.status.idle":"2024-01-27T12:46:08.633235Z","shell.execute_reply.started":"2024-01-27T12:46:08.624049Z","shell.execute_reply":"2024-01-27T12:46:08.632277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load_eeg_data(train_data['eeg_id'][190])","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:08.634382Z","iopub.execute_input":"2024-01-27T12:46:08.634670Z","iopub.status.idle":"2024-01-27T12:46:08.643081Z","shell.execute_reply.started":"2024-01-27T12:46:08.634644Z","shell.execute_reply":"2024-01-27T12:46:08.642217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load_spectrogram_data(train_data['spectrogram_id'][28])","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:08.649159Z","iopub.execute_input":"2024-01-27T12:46:08.649519Z","iopub.status.idle":"2024-01-27T12:46:08.653701Z","shell.execute_reply.started":"2024-01-27T12:46:08.649494Z","shell.execute_reply":"2024-01-27T12:46:08.652755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = train_data.drop(columns=['eeg_sub_id','eeg_label_offset_seconds',\n                         'spectrogram_sub_id','spectrogram_label_offset_seconds',\n                         'label_id','patient_id'])\n\ndf_train = df_train.drop_duplicates().reset_index()\ndf_train.drop(columns=['index'], inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:08.654839Z","iopub.execute_input":"2024-01-27T12:46:08.655755Z","iopub.status.idle":"2024-01-27T12:46:08.696923Z","shell.execute_reply.started":"2024-01-27T12:46:08.655727Z","shell.execute_reply":"2024-01-27T12:46:08.696124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['total'] = df_train[vote_columns].sum(axis=1)\ndf_train[vote_columns] = df_train[vote_columns].div(df_train['total'], axis=0)\ndf_train.drop(columns=['total'], inplace=True)\n\ndf_train['expert_consensus'] = df_train['expert_consensus'].map(class_name_to_index)","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:08.698087Z","iopub.execute_input":"2024-01-27T12:46:08.698398Z","iopub.status.idle":"2024-01-27T12:46:08.719380Z","shell.execute_reply.started":"2024-01-27T12:46:08.698370Z","shell.execute_reply":"2024-01-27T12:46:08.718342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:08.720458Z","iopub.execute_input":"2024-01-27T12:46:08.720780Z","iopub.status.idle":"2024-01-27T12:46:08.744568Z","shell.execute_reply.started":"2024-01-27T12:46:08.720752Z","shell.execute_reply":"2024-01-27T12:46:08.743728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_train[vote_columns]","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:08.745740Z","iopub.execute_input":"2024-01-27T12:46:08.746105Z","iopub.status.idle":"2024-01-27T12:46:08.750416Z","shell.execute_reply.started":"2024-01-27T12:46:08.746071Z","shell.execute_reply":"2024-01-27T12:46:08.749415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess(dataframe, eeg_dir, spectrogram_dir, vote_columns):\n    eeg_features_list = []\n    spectrogram_features_list = []\n    labels_list = []\n\n    for idx in range(len(dataframe)):\n        eeg_id = dataframe.iloc[idx]['eeg_id']\n        spectrogram_id = dataframe.iloc[idx]['spectrogram_id']\n\n        eeg_data = load_data(eeg_id, eeg_dir)\n        eeg_features = extract_features(eeg_data)\n        eeg_features_list.append(eeg_features)\n\n        spectrogram_data = load_data(spectrogram_id, spectrogram_dir).drop(columns=['time'])\n        spectrogram_features = extract_features(spectrogram_data)\n        spectrogram_features_list.append(spectrogram_features)\n\n        label = dataframe.iloc[idx][vote_columns].values\n        labels_list.append(label)\n\n    eeg_features_tensor = torch.tensor(eeg_features_list, dtype=torch.float32)\n    spectrogram_features_tensor = torch.tensor(spectrogram_features_list, dtype=torch.float32)\n    labels_tensor = torch.tensor(labels_list, dtype=torch.float32)\n\n    return eeg_features_tensor, spectrogram_features_tensor, labels_tensor\n\n\ndef extract_features(df):\n    current_size = len(df)\n\n    # Basic statistical features\n    min_values = df.min()\n    max_values = df.max()\n    mean_values = df.mean()\n    std_values = df.std()\n\n    # Time-domain features\n    rms_values = np.sqrt(np.mean(np.square(df), axis=0))\n    var_values = df.var()\n    skew_values = df.skew()\n    kurtosis_values = df.kurtosis()\n\n    # Concatenate all features\n    features = np.concatenate([\n        min_values, max_values, mean_values, std_values, \n        rms_values, var_values, skew_values, kurtosis_values\n    ])\n\n\n    return features","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:08.751558Z","iopub.execute_input":"2024-01-27T12:46:08.751897Z","iopub.status.idle":"2024-01-27T12:46:08.764399Z","shell.execute_reply.started":"2024-01-27T12:46:08.751840Z","shell.execute_reply":"2024-01-27T12:46:08.763648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# eeg_features_tensor, spectrogram_features_tensor, labels_tensor = preprocess(df_train, eeg_dir, spectrogram_dir, vote_columns)","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:08.765368Z","iopub.execute_input":"2024-01-27T12:46:08.766094Z","iopub.status.idle":"2024-01-27T12:46:08.780487Z","shell.execute_reply.started":"2024-01-27T12:46:08.766068Z","shell.execute_reply":"2024-01-27T12:46:08.779676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# torch.save(eeg_features_tensor, '/kaggle/working/eeg_features.pt')\n# torch.save(spectrogram_features_tensor, '/kaggle/working/spectrogram_features.pt')\n# torch.save(labels_tensor, '/kaggle/working/labels.pt')","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:08.781613Z","iopub.execute_input":"2024-01-27T12:46:08.781917Z","iopub.status.idle":"2024-01-27T12:46:08.791894Z","shell.execute_reply.started":"2024-01-27T12:46:08.781889Z","shell.execute_reply":"2024-01-27T12:46:08.791007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\neeg_features_tensor = torch.load('/kaggle/input/harmful-brain-activity-contest-simplied-dataset/Harmful Brain Activity/eeg_features.pt', map_location=device)\nspectrogram_features_tensor = torch.load('/kaggle/input/harmful-brain-activity-contest-simplied-dataset/Harmful Brain Activity/spectrogram_features.pt', map_location=device)\nlabels_tensor = torch.load('/kaggle/input/harmful-brain-activity-contest-simplied-dataset/Harmful Brain Activity/labels.pt', map_location=device)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:08.793250Z","iopub.execute_input":"2024-01-27T12:46:08.793542Z","iopub.status.idle":"2024-01-27T12:46:10.771095Z","shell.execute_reply.started":"2024-01-27T12:46:08.793515Z","shell.execute_reply":"2024-01-27T12:46:10.770193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spectrogram_features_tensor.shape","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:10.772278Z","iopub.execute_input":"2024-01-27T12:46:10.772552Z","iopub.status.idle":"2024-01-27T12:46:10.778662Z","shell.execute_reply.started":"2024-01-27T12:46:10.772528Z","shell.execute_reply":"2024-01-27T12:46:10.777656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import Dataset\n\nclass EEGSpectrogramDataset(Dataset):\n    def __init__(self, eeg_features, spectrogram_features, labels):\n        self.eeg_features = eeg_features\n        self.spectrogram_features = spectrogram_features\n        self.labels = labels\n\n    def __len__(self):\n        return len(self.labels)\n\n    def __getitem__(self, idx):\n        return self.eeg_features[idx], self.spectrogram_features[idx], self.labels[idx]","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:10.780019Z","iopub.execute_input":"2024-01-27T12:46:10.780330Z","iopub.status.idle":"2024-01-27T12:46:10.791056Z","shell.execute_reply.started":"2024-01-27T12:46:10.780304Z","shell.execute_reply":"2024-01-27T12:46:10.790068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import DataLoader\n\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\ndataset = EEGSpectrogramDataset(eeg_features_tensor, spectrogram_features_tensor, labels_tensor)","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:10.792075Z","iopub.execute_input":"2024-01-27T12:46:10.792411Z","iopub.status.idle":"2024-01-27T12:46:10.803024Z","shell.execute_reply.started":"2024-01-27T12:46:10.792386Z","shell.execute_reply":"2024-01-27T12:46:10.802123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import random_split\n\ntotal_size = len(dataset)\ntrain_size = int(0.8 * total_size) \ntest_size = total_size - train_size \n\ntrain_dataset, test_dataset = random_split(dataset, [train_size, test_size])\n\ntrain_loader = DataLoader(train_dataset, batch_size=512, shuffle=True)\ntest_loader = DataLoader(test_dataset, batch_size=512, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:10.804085Z","iopub.execute_input":"2024-01-27T12:46:10.804346Z","iopub.status.idle":"2024-01-27T12:46:10.830045Z","shell.execute_reply.started":"2024-01-27T12:46:10.804324Z","shell.execute_reply":"2024-01-27T12:46:10.829203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\n\nclass EEGSpectrogramNet(nn.Module):\n    def __init__(self):\n        super(EEGSpectrogramNet, self).__init__()\n        self.eeg_layer = nn.Linear(160, 512)\n        self.spectrogram_layer = nn.Linear(3200, 512)\n        \n        self.eeg_fc1 = nn.Linear(512, 256)\n        self.eeg_dropout = nn.Dropout(0.2)\n        \n        self.spectrogram_fc1 = nn.Linear(512, 256)\n        self.spectrogram_dropout = nn.Dropout(0.2)\n        \n        self.fc1 = nn.Linear(256 + 256, 512) \n        self.fc2 = nn.Linear(512, 256)\n        self.fc3 = nn.Linear(256, 128) \n        self.dropout = nn.Dropout(0.3)\n        self.fc4 = nn.Linear(128, 64)\n        self.fc5 = nn.Linear(64, 32)\n\n        self.output = nn.Linear(32, 6)\n        \n        self._initialize_weights()\n\n    def forward(self, eeg_data, spectrogram_data):\n        # EEG and spectrogram pathways\n        eeg_features = self.eeg_dropout(F.relu(self.eeg_fc1(self.eeg_layer(eeg_data))))\n        spectrogram_features = self.spectrogram_dropout(F.relu(self.spectrogram_fc1(self.spectrogram_layer(spectrogram_data))))\n\n        # Concatenate features\n        combined = torch.cat((eeg_features, spectrogram_features), dim=1)\n\n        # Further processing\n        x = F.relu(self.fc1(combined))\n        x = F.relu(self.fc2(x))\n        x = F.relu(self.fc3(x))\n        x = self.dropout(x)\n        x = F.relu(self.fc4(x))\n        x = F.relu(self.fc5(x))\n        \n        # Output layer\n        x = self.output(x)\n        return F.softmax(x, dim=1)\n\n    def _initialize_weights(self):\n        for m in self.modules():\n            if isinstance(m, nn.Linear):\n                nn.init.kaiming_normal_(m.weight, mode='fan_in', nonlinearity='relu')\n                if m.bias is not None:\n                    nn.init.constant_(m.bias, 0)","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:10.831142Z","iopub.execute_input":"2024-01-27T12:46:10.831456Z","iopub.status.idle":"2024-01-27T12:46:10.845657Z","shell.execute_reply.started":"2024-01-27T12:46:10.831424Z","shell.execute_reply":"2024-01-27T12:46:10.844711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = EEGSpectrogramNet()\nprint(f\"Using {device}\")\nmodel.to(device)","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:10.846659Z","iopub.execute_input":"2024-01-27T12:46:10.846955Z","iopub.status.idle":"2024-01-27T12:46:10.934340Z","shell.execute_reply.started":"2024-01-27T12:46:10.846929Z","shell.execute_reply":"2024-01-27T12:46:10.933454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.optim import Adam\nfrom torch.nn.utils import clip_grad_norm_\nimport torch.nn.functional as F\n\noptimizer = Adam(model.parameters(), lr=0.01)\nscheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min', factor=0.50, patience=50, verbose=True)\n\nrunning_loss = 0.0\nnum_epochs = 100\nprint_interval = 10\nval_loss = 0.0\nval_mse = 0.0\n\nfor epoch in range(num_epochs):\n    model.train()\n    \n    for eeg_data, spectrogram_data, labels in train_loader:\n        optimizer.zero_grad()\n\n        outputs = model(eeg_data, spectrogram_data)\n        loss = F.mse_loss(outputs, labels)\n        loss.backward()\n\n        clip_grad_norm_(model.parameters(), max_norm=1.0)\n\n        optimizer.step()\n\n        running_loss += loss.item()\n    \n    print(f\"Epoch [{epoch+1}/{num_epochs}], Loss: {running_loss/(len(train_loader)):.4f}\")\n    running_loss = 0.0\n\n    model.eval()  # Set model to evaluation mode\n    val_running_loss = 0.0\n    val_running_mse = 0.0\n    with torch.no_grad():\n        for eeg_data, spectrogram_data, labels in test_loader:\n            outputs = model(eeg_data, spectrogram_data)\n            loss = F.mse_loss(outputs, labels)\n            val_running_loss += loss.item()\n\n            mse_loss = F.mse_loss(outputs, labels)\n            val_running_mse += mse_loss.item()\n\n    val_loss = val_running_loss / len(test_loader)\n    val_mse = val_running_mse / len(test_loader)\n    print(f\"Validation Loss: {val_loss:.4f}, Validation MSE: {val_mse:.4f}\")\n        \n    scheduler.step(val_loss)","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:10.935643Z","iopub.execute_input":"2024-01-27T12:46:10.936088Z","iopub.status.idle":"2024-01-27T12:46:52.366753Z","shell.execute_reply.started":"2024-01-27T12:46:10.936054Z","shell.execute_reply":"2024-01-27T12:46:52.365840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess_test(dataframe, eeg_test_dir, spectrogram_test_dir, vote_columns):\n    eeg_features_list = []\n    spectrogram_features_list = []\n\n    for idx in range(len(dataframe)):\n        eeg_id = dataframe.iloc[idx]['eeg_id']\n        spectrogram_id = dataframe.iloc[idx]['spectrogram_id']\n\n        eeg_data = load_data(eeg_id, eeg_test_dir)\n        eeg_features = extract_features(eeg_data)\n        eeg_features_list.append(eeg_features)\n\n        spectrogram_data = load_data(spectrogram_id, spectrogram_test_dir).drop(columns=['time'])\n        spectrogram_features = extract_features(spectrogram_data)\n        spectrogram_features_list.append(spectrogram_features)\n\n\n    eeg_features_tensor = torch.tensor(eeg_features_list, dtype=torch.float32)\n    spectrogram_features_tensor = torch.tensor(spectrogram_features_list, dtype=torch.float32)\n\n    return eeg_features_tensor, spectrogram_features_tensor","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:52.367990Z","iopub.execute_input":"2024-01-27T12:46:52.368274Z","iopub.status.idle":"2024-01-27T12:46:52.375803Z","shell.execute_reply.started":"2024-01-27T12:46:52.368248Z","shell.execute_reply":"2024-01-27T12:46:52.374758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eeg_test_dir = '/kaggle/input/hms-harmful-brain-activity-classification/test_eegs'\nspectrogram_test_dir = '/kaggle/input/hms-harmful-brain-activity-classification/test_spectrograms'\n\ntest_data = pd.read_csv('/kaggle/input/hms-harmful-brain-activity-classification/test.csv')","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:52.377037Z","iopub.execute_input":"2024-01-27T12:46:52.377323Z","iopub.status.idle":"2024-01-27T12:46:52.398360Z","shell.execute_reply.started":"2024-01-27T12:46:52.377297Z","shell.execute_reply":"2024-01-27T12:46:52.397416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eeg_features_tensor, spectrogram_features_tensor = preprocess_test(test_data, eeg_test_dir, spectrogram_test_dir, vote_columns)\neeg_features_tensor = eeg_features_tensor.to(device)\nspectrogram_features_tensor = spectrogram_features_tensor.to(device)","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:52.399696Z","iopub.execute_input":"2024-01-27T12:46:52.399986Z","iopub.status.idle":"2024-01-27T12:46:52.685849Z","shell.execute_reply.started":"2024-01-27T12:46:52.399960Z","shell.execute_reply":"2024-01-27T12:46:52.684904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.eval()\n\noutputs = model(eeg_features_tensor, spectrogram_features_tensor)\noutputs = outputs.cpu().detach().numpy()","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:52.686826Z","iopub.execute_input":"2024-01-27T12:46:52.687097Z","iopub.status.idle":"2024-01-27T12:46:52.715236Z","shell.execute_reply.started":"2024-01-27T12:46:52.687075Z","shell.execute_reply":"2024-01-27T12:46:52.714329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.DataFrame({\n    'id': test_data['eeg_id'],\n    'seizure_vote': outputs[:, 0],\n    'lpd_vote': outputs[:, 1],\n    'gpd_vote': outputs[:, 2],\n    'lrda_vote': outputs[:, 3],\n    'grda_vote': outputs[:, 4],\n    'other_vote': outputs[:, 5]\n})","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:46:52.716379Z","iopub.execute_input":"2024-01-27T12:46:52.716645Z","iopub.status.idle":"2024-01-27T12:46:52.722266Z","shell.execute_reply.started":"2024-01-27T12:46:52.716622Z","shell.execute_reply":"2024-01-27T12:46:52.721270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv('submission.csv', index=False)\nsubmission_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-27T12:51:22.478973Z","iopub.execute_input":"2024-01-27T12:51:22.479974Z","iopub.status.idle":"2024-01-27T12:51:22.496138Z","shell.execute_reply.started":"2024-01-27T12:51:22.479937Z","shell.execute_reply":"2024-01-27T12:51:22.495046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}