{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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":"gpu","dataSources":[{"sourceId":59093,"databundleVersionId":7469972,"sourceType":"competition"}],"dockerImageVersionId":30674,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# **Harmful Brain Activity Classification / Sub-Classing with Keras on Tensorflow**\n\n## **Project by:** [Aarish Asif Khan](https://www.kaggle.com/aarishasifkhan)\n\n## **Date:** 1st April 2024\n\n## **Official Website:** [Tensorflow Org](http://tensorflow.org/)\n\n## **Dataset:** [HMC - Harmful Brain Activity Dataset](https://www.kaggle.com/competitions/hms-harmful-brain-activity-classification)","metadata":{}},{"cell_type":"code","source":"# Import libraries\nimport numpy as np\nimport pandas as pd\n\nfrom sklearn.preprocessing import StandardScaler, LabelEncoder\nfrom sklearn.model_selection import train_test_split\n\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import SimpleRNN, Dense\n\nfrom tensorflow.keras.layers import Dropout","metadata":{"execution":{"iopub.status.busy":"2024-04-02T00:00:27.830172Z","iopub.execute_input":"2024-04-02T00:00:27.830776Z","iopub.status.idle":"2024-04-02T00:00:42.048093Z","shell.execute_reply.started":"2024-04-02T00:00:27.830743Z","shell.execute_reply":"2024-04-02T00:00:42.047001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load data\ntrain_data = pd.read_csv(\"/kaggle/input/hms-harmful-brain-activity-classification/train.csv\")\ntest_data = pd.read_csv(\"/kaggle/input/hms-harmful-brain-activity-classification/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-04-02T00:00:42.049925Z","iopub.execute_input":"2024-04-02T00:00:42.050463Z","iopub.status.idle":"2024-04-02T00:00:42.315747Z","shell.execute_reply.started":"2024-04-02T00:00:42.050437Z","shell.execute_reply":"2024-04-02T00:00:42.314925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split data into features (X) and labels (y)\nX = train_data.drop(columns=['eeg_id', 'eeg_sub_id', 'spectrogram_id', 'spectrogram_sub_id', 'label_id', 'patient_id', 'expert_consensus'])\ny = train_data[['seizure_vote', 'lpd_vote', 'gpd_vote', 'lrda_vote', 'grda_vote', 'other_vote']]","metadata":{"execution":{"iopub.status.busy":"2024-04-02T00:00:42.321098Z","iopub.execute_input":"2024-04-02T00:00:42.321483Z","iopub.status.idle":"2024-04-02T00:00:42.337201Z","shell.execute_reply.started":"2024-04-02T00:00:42.321450Z","shell.execute_reply":"2024-04-02T00:00:42.336376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split data into training and validation sets\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-04-02T00:00:42.338307Z","iopub.execute_input":"2024-04-02T00:00:42.338602Z","iopub.status.idle":"2024-04-02T00:00:42.362672Z","shell.execute_reply.started":"2024-04-02T00:00:42.338574Z","shell.execute_reply":"2024-04-02T00:00:42.361660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Scale features\nscaler = StandardScaler()\nX_train_scaled = scaler.fit_transform(X_train)\nX_val_scaled = scaler.transform(X_val)","metadata":{"execution":{"iopub.status.busy":"2024-04-02T00:00:42.363882Z","iopub.execute_input":"2024-04-02T00:00:42.364163Z","iopub.status.idle":"2024-04-02T00:00:42.385646Z","shell.execute_reply.started":"2024-04-02T00:00:42.364134Z","shell.execute_reply":"2024-04-02T00:00:42.384756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reshape data for RNN input\nX_train_reshaped = X_train_scaled.reshape((X_train_scaled.shape[0], X_train_scaled.shape[1], 1))\nX_val_reshaped = X_val_scaled.reshape((X_val_scaled.shape[0], X_val_scaled.shape[1], 1))","metadata":{"execution":{"iopub.status.busy":"2024-04-02T00:00:42.387036Z","iopub.execute_input":"2024-04-02T00:00:42.387511Z","iopub.status.idle":"2024-04-02T00:00:42.393321Z","shell.execute_reply.started":"2024-04-02T00:00:42.387476Z","shell.execute_reply":"2024-04-02T00:00:42.392419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n    SimpleRNN(128, return_sequences=True, input_shape=(X_train_reshaped.shape[1], X_train_reshaped.shape[2])),\n    Dropout(0.2),\n    SimpleRNN(64, return_sequences=True),\n    Dropout(0.2),\n    SimpleRNN(32, return_sequences=True),  # Adding an additional SimpleRNN layer with return_sequences=True\n    Dropout(0.2),\n    SimpleRNN(16),  # Adding another SimpleRNN layer\n    Dropout(0.2),\n    Dense(64, activation='relu'),\n    Dropout(0.2),\n    Dense(32, activation='relu'),\n    Dense(6, activation='softmax')  # Assuming there are 6 output classes\n])","metadata":{"execution":{"iopub.status.busy":"2024-04-02T00:00:42.394589Z","iopub.execute_input":"2024-04-02T00:00:42.394896Z","iopub.status.idle":"2024-04-02T00:00:43.617893Z","shell.execute_reply.started":"2024-04-02T00:00:42.394874Z","shell.execute_reply":"2024-04-02T00:00:43.617100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the model\nmodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-04-02T00:00:43.618972Z","iopub.execute_input":"2024-04-02T00:00:43.619251Z","iopub.status.idle":"2024-04-02T00:00:43.631694Z","shell.execute_reply.started":"2024-04-02T00:00:43.619227Z","shell.execute_reply":"2024-04-02T00:00:43.630744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-04-02T00:00:43.636662Z","iopub.execute_input":"2024-04-02T00:00:43.636954Z","iopub.status.idle":"2024-04-02T00:00:43.665652Z","shell.execute_reply.started":"2024-04-02T00:00:43.636930Z","shell.execute_reply":"2024-04-02T00:00:43.664738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the model\nhistory = model.fit(X_train_reshaped, y_train, epochs=10, batch_size=32, validation_data=(X_val_reshaped, y_val))","metadata":{"execution":{"iopub.status.busy":"2024-04-02T00:00:43.667030Z","iopub.execute_input":"2024-04-02T00:00:43.667509Z","iopub.status.idle":"2024-04-02T00:03:22.174725Z","shell.execute_reply.started":"2024-04-02T00:00:43.667476Z","shell.execute_reply":"2024-04-02T00:03:22.173842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate the model on the validation set\nval_loss, val_accuracy = model.evaluate(X_val_reshaped, y_val)\nprint(\"Validation Loss:\", val_loss)\nprint(\"Validation Accuracy:\", val_accuracy)","metadata":{"execution":{"iopub.status.busy":"2024-04-02T00:03:22.176165Z","iopub.execute_input":"2024-04-02T00:03:22.176455Z","iopub.status.idle":"2024-04-02T00:03:23.497764Z","shell.execute_reply.started":"2024-04-02T00:03:22.176429Z","shell.execute_reply":"2024-04-02T00:03:23.496916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the pre-trained model\n# model_path = 'my_custom_model.h5'\n\n# Save the model\n# model.save(model_path)\n\n# print(\"Model saved successfully.\")","metadata":{"execution":{"iopub.status.busy":"2024-04-02T00:03:23.499056Z","iopub.execute_input":"2024-04-02T00:03:23.499436Z","iopub.status.idle":"2024-04-02T00:03:23.571208Z","shell.execute_reply.started":"2024-04-02T00:03:23.499403Z","shell.execute_reply":"2024-04-02T00:03:23.570167Z"},"trusted":true},"execution_count":null,"outputs":[]}]}