{"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":"gpu","dataSources":[{"sourceId":59093,"databundleVersionId":7469972,"sourceType":"competition"}],"dockerImageVersionId":30919,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n #for filename in filenames:\n  #      print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-04-29T15:42:51.838266Z","iopub.execute_input":"2025-04-29T15:42:51.838570Z","iopub.status.idle":"2025-04-29T15:42:52.142036Z","shell.execute_reply.started":"2025-04-29T15:42:51.838546Z","shell.execute_reply":"2025-04-29T15:42:52.141188Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport os\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import StandardScaler, LabelEncoder\nfrom sklearn.metrics import accuracy_score, classification_report, confusion_matrix\nfrom tensorflow.keras.utils import to_categorical\nimport pyarrow.parquet as pq\nimport io\nfrom PIL import Image\n\n# Define dataset directories\ndata_csv = \"/kaggle/input/hms-harmful-brain-activity-classification/train.csv\"\nparquet_dir = \"/kaggle/input/hms-harmful-brain-activity-classification/train_spectrograms\"\n\n# Load metadata\ndf = pd.read_csv(data_csv)\n\n# Filter rows that have corresponding parquet files\nexisting_files = set(os.listdir(parquet_dir))\ndf = df[df[\"spectrogram_id\"].apply(lambda x: f\"{x}.parquet\" in existing_files)]\n\n# Limit to first 5000 samples\ndf = df.head(5000)\n\nif df.empty:\n    raise ValueError(\"No matching parquet files found. Check your directory and filenames.\")\n\n# Encode labels\ny = df[\"expert_consensus\"].dropna()\ndf = df.loc[y.index]\nle = LabelEncoder()\ny_encoded = le.fit_transform(y)\ny_categorical = to_categorical(y_encoded)\n\n# Metadata features\nX_meta = df[['seizure_vote', 'lpd_vote', 'gpd_vote', 'lrda_vote', 'grda_vote', 'other_vote']].values\nscaler = StandardScaler()\nX_meta = scaler.fit_transform(X_meta)\n\n# Helper to convert spectrogram parquet to image array\ndef load_spectrogram_as_image(spectrogram_id, size=(224, 224)):\n    file_path = os.path.join(parquet_dir, f\"{spectrogram_id}.parquet\")\n    table = pq.read_table(file_path)\n    data = table.to_pandas().values.T  # Transpose to get (freq, time)\n    data = np.log1p(np.clip(data, a_min=1e-6, a_max=None))  # Avoid log of zero\n    data = (data - np.min(data)) / (np.max(data) - np.min(data) + 1e-8)\n    img = Image.fromarray(np.uint8(data * 255)).convert(\"RGB\").resize(size)\n    return np.array(img)\n\n# Load spectrogram images\nX_images = np.stack([load_spectrogram_as_image(sid) for sid in df[\"spectrogram_id\"]])\n\n# Normalize images to 0-1 range for Hugging Face models\nX_images = X_images / 255.0\n\n# Split dataset\nX_img_train, X_img_temp, X_meta_train, X_meta_temp, y_train, y_temp = train_test_split(\n    X_images, X_meta, y_categorical, test_size=0.3, random_state=42, stratify=y_categorical)\nX_img_val, X_img_test, X_meta_val, X_meta_test, y_val, y_test = train_test_split(\n    X_img_temp, X_meta_temp, y_temp, test_size=0.5, random_state=42, stratify=y_temp)\n\n# Vision Transformer from Hugging Face\nfrom transformers import TFViTModel\nvit_model = TFViTModel.from_pretrained(\"google/vit-base-patch16-224-in21k\")\n\nimage_input = keras.Input(shape=(224, 224, 3), name=\"image_input\")\n\n# Custom wrapper to apply ViT\nclass ViTWrapper(layers.Layer):\n    def __init__(self, vit_model):\n        super(ViTWrapper, self).__init__()\n        self.vit_model = vit_model\n\n    def call(self, inputs):\n        # Convert from (B, 224, 224, 3) to (B, 3, 224, 224)\n        pixel_values = tf.transpose(inputs, perm=[0, 3, 1, 2])\n        outputs = self.vit_model(pixel_values=pixel_values, training=False)\n        return outputs.last_hidden_state[:, 0]  # CLS token\n\nx = ViTWrapper(vit_model)(image_input)\nx = layers.Dense(128, activation='relu')(x)\nimage_features = layers.Dropout(0.3)(x)\n\n# Metadata model\nmeta_input = keras.Input(shape=(X_meta.shape[1],), name=\"meta_input\")\ny_meta = layers.Dense(32, activation='relu')(meta_input)\nmeta_features = layers.Dense(16, activation='relu')(y_meta)\n\n# Merge models\nmerged = layers.concatenate([image_features, meta_features])\nz = layers.Dense(64, activation='relu')(merged)\nout = layers.Dense(y_categorical.shape[1], activation='softmax')(z)\n\nmodel = keras.Model(inputs=[image_input, meta_input], outputs=out)\nmodel.compile(optimizer=keras.optimizers.Adam(learning_rate=1e-4),\n              loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Train model\nmodel.fit(\n    x=[X_img_train, X_meta_train],\n    y=y_train,\n    epochs=10,\n    batch_size=32,\n    validation_data=([X_img_val, X_meta_val], y_val)\n)\n\n# Evaluate model\ny_pred = model.predict([X_img_test, X_meta_test])\ny_pred_classes = np.argmax(y_pred, axis=1)\ny_true_classes = np.argmax(y_test, axis=1)\n\nprint(\"Test Accuracy:\", accuracy_score(y_true_classes, y_pred_classes))\nprint(\"Test Report:\\n\", classification_report(y_true_classes, y_pred_classes))\n\n# Confusion Matrix\nplt.figure(figsize=(6, 4))\nplt.imshow(confusion_matrix(y_true_classes, y_pred_classes), cmap='Blues', interpolation='nearest')\nplt.colorbar()\nplt.title(\"Confusion Matrix - Test Set\")\nplt.xlabel(\"Predicted\")\nplt.ylabel(\"True\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-29T15:42:52.897030Z","iopub.execute_input":"2025-04-29T15:42:52.897475Z","iopub.status.idle":"2025-04-29T16:01:04.286111Z","shell.execute_reply.started":"2025-04-29T15:42:52.897446Z","shell.execute_reply":"2025-04-29T16:01:04.281151Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}