{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","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":368750,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":305417,"modelId":325874}],"dockerImageVersionId":31012,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nfrom tensorflow.keras.models import load_model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:28:36.542165Z","iopub.execute_input":"2025-05-02T18:28:36.542541Z","iopub.status.idle":"2025-05-02T18:28:51.672473Z","shell.execute_reply.started":"2025-05-02T18:28:36.542493Z","shell.execute_reply":"2025-05-02T18:28:51.671666Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:28:51.673674Z","iopub.execute_input":"2025-05-02T18:28:51.674181Z","iopub.status.idle":"2025-05-02T18:28:51.677901Z","shell.execute_reply.started":"2025-05-02T18:28:51.674149Z","shell.execute_reply":"2025-05-02T18:28:51.677126Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.listdir(\"/kaggle/input/modeller/keras/default/1/\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:28:51.678622Z","iopub.execute_input":"2025-05-02T18:28:51.678823Z","iopub.status.idle":"2025-05-02T18:28:51.736346Z","shell.execute_reply.started":"2025-05-02T18:28:51.678807Z","shell.execute_reply":"2025-05-02T18:28:51.735642Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import shutil\n\nshutil.copy(\n    \"/kaggle/input/modeller/keras/default/1/3d model 20nci epoch 67 17.h5\",\n    \"/kaggle/working/3d.keras\"\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:28:51.737820Z","iopub.execute_input":"2025-05-02T18:28:51.738120Z","iopub.status.idle":"2025-05-02T18:28:52.414811Z","shell.execute_reply.started":"2025-05-02T18:28:51.738096Z","shell.execute_reply":"2025-05-02T18:28:52.414152Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\n# Modelleri yükle\nmodel_paths = [\n    \"/kaggle/input/modeller/keras/default/1/densenet 59nokta27.h5\",\n    \"/kaggle/input/modeller/keras/default/1/inceptionresnetv2 57nokta69.h5\",\n    \"/kaggle/input/modeller/keras/default/1/resnetv2 61nokta15.h5\",\n    \"/kaggle/input/modeller/keras/default/1/3d model 20nci epoch 67 17.h5\",\n    \"/kaggle/input/modeller/keras/default/1/63nokta25 ilkelmodel.h5\",\n    \"/kaggle/input/modeller/keras/default/1/nasnet 60nokta62.h5\"\n]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-01T23:30:17.174556Z","iopub.execute_input":"2025-05-01T23:30:17.174897Z","iopub.status.idle":"2025-05-01T23:30:17.179710Z","shell.execute_reply.started":"2025-05-01T23:30:17.174876Z","shell.execute_reply":"2025-05-01T23:30:17.178879Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay\nimport matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:28:59.584110Z","iopub.execute_input":"2025-05-02T18:28:59.584437Z","iopub.status.idle":"2025-05-02T18:29:00.296228Z","shell.execute_reply.started":"2025-05-02T18:28:59.584389Z","shell.execute_reply":"2025-05-02T18:29:00.295609Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cm = confusion_matrix(y_true, y_pred)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cm = confusion_matrix(y_true, y_pred)\ndisp = ConfusionMatrixDisplay(confusion_matrix=cm)\ndisp.plot(cmap='Blues')  # Renk skalası tercihe bağlı\nplt.title(\"Confusion Matrix\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T00:33:31.537387Z","iopub.execute_input":"2025-05-02T00:33:31.537715Z","iopub.status.idle":"2025-05-02T00:33:44.084582Z","shell.execute_reply.started":"2025-05-02T00:33:31.537684Z","shell.execute_reply":"2025-05-02T00:33:44.083712Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"_3dmodel = load_model(\"/kaggle/working/3d.keras\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:34:08.854307Z","iopub.execute_input":"2025-05-02T18:34:08.854997Z","iopub.status.idle":"2025-05-02T18:34:11.990466Z","shell.execute_reply.started":"2025-05-02T18:34:08.854973Z","shell.execute_reply":"2025-05-02T18:34:11.989630Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.argmax(predict(_3dmodel,3372294787))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T17:22:29.253884Z","iopub.execute_input":"2025-05-02T17:22:29.254424Z","iopub.status.idle":"2025-05-02T17:22:31.745125Z","shell.execute_reply.started":"2025-05-02T17:22:29.254344Z","shell.execute_reply":"2025-05-02T17:22:31.744228Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"spectrogram_from_eeg(3372294787).shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T17:22:05.325279Z","iopub.execute_input":"2025-05-02T17:22:05.325610Z","iopub.status.idle":"2025-05-02T17:22:05.450630Z","shell.execute_reply.started":"2025-05-02T17:22:05.325585Z","shell.execute_reply":"2025-05-02T17:22:05.449745Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"_3dmodel.predict(spectrogram_from_eeg(3372294787))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T17:22:08.360114Z","iopub.execute_input":"2025-05-02T17:22:08.360422Z","iopub.status.idle":"2025-05-02T17:22:08.559472Z","shell.execute_reply.started":"2025-05-02T17:22:08.360401Z","shell.execute_reply":"2025-05-02T17:22:08.558256Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"a = np.array([1, 2, 3])\nb = np.array([3, 4, 5])\n\nortalama = (a + b) / 2\nprint(ortalama)  # [2. 3. 4.]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T00:36:07.999294Z","iopub.execute_input":"2025-05-02T00:36:07.999584Z","iopub.status.idle":"2025-05-02T00:36:08.006331Z","shell.execute_reply.started":"2025-05-02T00:36:07.999563Z","shell.execute_reply":"2025-05-02T00:36:08.005466Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"c","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T00:35:00.818756Z","iopub.execute_input":"2025-05-02T00:35:00.819041Z","iopub.status.idle":"2025-05-02T00:35:00.824820Z","shell.execute_reply.started":"2025-05-02T00:35:00.819023Z","shell.execute_reply":"2025-05-02T00:35:00.823889Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def calc_mean(eeg_id):\n    toplam = None\n    aray = spectrogram_from_eeg_2d(eeg_id)\n    aray = np.expand_dims(aray, axis=0)        # (1, 512, 216)\n    aray = np.expand_dims(aray, axis=-1) \n    aray = aray[:, :216, :, :]\n    tahmin = _2dmodels[2].predict(aray)\n    print(\"2 nolu modelin tahmini:\",np.argmax(tahmin))\n    print(tahmin)\n    toplam = tahmin\n\n    # Elindeki veri\n    aray = np.random.rand(1, 216, 512)  # örnek olarak\n    \n    # Ekseni doğru sırala: (1, 512, 216)\n    aray = np.transpose(aray, (0, 2, 1))\n    \n    # Son ekseni ekle: (1, 512, 216, 1)\n    aray = np.expand_dims(aray, axis=-1)\n    \n\n    tahmin = _2dmodels[0].predict(aray)\n    print(\"0 nolu modelin tahmini:\",np.argmax(tahmin))\n    print(tahmin)\n    toplam +=tahmin\n    tahmin = _2dmodels[1].predict(aray)\n    print(\"1 nolu modelin tahmini:\",np.argmax(tahmin))\n    print(tahmin)\n    toplam += tahmin\n    \n    tahmin = _2dmodels[3].predict(aray)\n    print(\"3 nolu modelin tahmini:\",np.argmax(tahmin))\n    print(tahmin)\n    toplam += tahmin\n    tahmin = predict(_3dmodel,eeg_id)\n    print(\"3d modelin tahmini:\",np.argmax(tahmin))\n    print(tahmin)\n    toplam+=tahmin\n    return toplam / (len(_2dmodels)+1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T01:43:02.036586Z","iopub.execute_input":"2025-05-02T01:43:02.036957Z","iopub.status.idle":"2025-05-02T01:43:02.046888Z","shell.execute_reply.started":"2025-05-02T01:43:02.036935Z","shell.execute_reply":"2025-05-02T01:43:02.045892Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay, accuracy_score\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm\nimport numpy as np\n\ndef ensemble_predict(eeg_id):\n    toplam = 0\n\n    # 2D model (2. model) - özel preprocess\n    aray = spectrogram_from_eeg_2d(eeg_id)\n    aray = np.expand_dims(aray, axis=0)  # (1, 512, 216)\n    aray = np.expand_dims(aray, axis=-1)\n    aray = aray[:, :216, :, :]\n    toplam += _2dmodels[2].predict(aray)\n\n    # Diğer 2D modeller (0, 1, 3) - farklı formatta preprocess\n    aray = spectrogram_from_eeg_2d(eeg_id)  # orijinal veri\n    aray = np.transpose(aray, (1, 0))       # (216, 512)\n    aray = np.expand_dims(aray, axis=0)     # (1, 216, 512)\n    aray = np.expand_dims(aray, axis=-1)    # (1, 216, 512, 1)\n\n    for i in [0, 1, 3]:\n        toplam += _2dmodels[i].predict(aray)\n\n    # 3D model\n    aray = spectrogram_from_eeg(eeg_id)     # (128, 256, 4)\n    aray = np.expand_dims(aray, axis=0)     # (1, 128, 256, 4)\n    aray = np.expand_dims(aray, axis=-1)    # (1, 128, 256, 4, 1)\n    toplam += _3dmodel.predict(aray)\n\n    return toplam / (len(_2dmodels) + 1)\n\n# Tüm test verisi için tahmin yap\ny_true = []\ny_pred = []\n\nfor _, row in tqdm(test_df.iterrows(), total=len(test_df)):\n    eeg_id = row['eeg_id']\n    true_label = row['expert_consensus']\n\n    probs = ensemble_predict(eeg_id)\n    predicted_label = np.argmax(probs)\n\n    y_true.append(true_label)\n    y_pred.append(predicted_label)\n\n# Accuracy\nacc = accuracy_score(y_true, y_pred)\nprint(f\"Accuracy: {acc*100:.2f}%\")\n\n# Confusion Matrix\nclass_names = [\"Seizure\", \"GPD\", \"LRDA\", \"LPD\", \"GRDA\", \"Other\"]\ncm = confusion_matrix(y_true, y_pred)\ndisp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=class_names)\ndisp.plot(cmap=\"Blues\", xticks_rotation=45)\nplt.title(\"Ensemble Confusion Matrix\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:21:50.233240Z","iopub.execute_input":"2025-05-02T18:21:50.233834Z","iopub.status.idle":"2025-05-02T18:21:50.385475Z","shell.execute_reply.started":"2025-05-02T18:21:50.233806Z","shell.execute_reply":"2025-05-02T18:21:50.384552Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T01:00:46.354837Z","iopub.execute_input":"2025-05-02T01:00:46.355219Z","iopub.status.idle":"2025-05-02T01:00:46.365505Z","shell.execute_reply.started":"2025-05-02T01:00:46.355194Z","shell.execute_reply":"2025-05-02T01:00:46.364506Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"calc_mean(3372294787)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T01:15:46.836496Z","iopub.execute_input":"2025-05-02T01:15:46.836797Z","iopub.status.idle":"2025-05-02T01:15:50.657966Z","shell.execute_reply.started":"2025-05-02T01:15:46.836777Z","shell.execute_reply":"2025-05-02T01:15:50.657046Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"real = []\npredictions = []","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:25:56.145264Z","iopub.execute_input":"2025-05-02T18:25:56.145538Z","iopub.status.idle":"2025-05-02T18:25:56.149308Z","shell.execute_reply.started":"2025-05-02T18:25:56.145518Z","shell.execute_reply":"2025-05-02T18:25:56.148254Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T17:24:44.546562Z","iopub.execute_input":"2025-05-02T17:24:44.546875Z","iopub.status.idle":"2025-05-02T17:24:44.556504Z","shell.execute_reply.started":"2025-05-02T17:24:44.546852Z","shell.execute_reply":"2025-05-02T17:24:44.555675Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T17:27:02.794646Z","iopub.execute_input":"2025-05-02T17:27:02.795007Z","iopub.status.idle":"2025-05-02T17:27:02.800172Z","shell.execute_reply.started":"2025-05-02T17:27:02.794977Z","shell.execute_reply":"2025-05-02T17:27:02.799226Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"    for idx, row in ornekler.iterrows():\n        eeg_id = row['eeg_id']\n        dogru_label = row['expert_consensus']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T17:27:42.330037Z","iopub.execute_input":"2025-05-02T17:27:42.330356Z","iopub.status.idle":"2025-05-02T17:27:42.336750Z","shell.execute_reply.started":"2025-05-02T17:27:42.330330Z","shell.execute_reply":"2025-05-02T17:27:42.335971Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(test_df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T17:33:20.946294Z","iopub.execute_input":"2025-05-02T17:33:20.946606Z","iopub.status.idle":"2025-05-02T17:33:20.953209Z","shell.execute_reply.started":"2025-05-02T17:33:20.946581Z","shell.execute_reply":"2025-05-02T17:33:20.952166Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n    ornekler = test_df.sample(50, random_state=42)  # aynı sonuç için sabit seed kullanılabilir\n\n    count = 0\n    for idx, row in test_df.iterrows():\n        eeg_id = row['eeg_id']\n        dogru_label = row['expert_consensus']\n\n        tahmin = predict(_3dmodel,eeg_id)\n        tahmin_edilen_label = np.argmax(tahmin)\n        real.append(dogru_label)\n        predictions.append(tahmin_edilen_label)\n        count += 1\n        if (count %25 == 0):\n            print(count,\",\",end=\"\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T17:33:42.096709Z","iopub.execute_input":"2025-05-02T17:33:42.097053Z","iopub.status.idle":"2025-05-02T17:39:34.138273Z","shell.execute_reply.started":"2025-05-02T17:33:42.097028Z","shell.execute_reply":"2025-05-02T17:39:34.136791Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nfrom sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm  # İlerleme çubuğu için\n\n# 1. Tüm spectrogram'ları yükle\ndef prepare_batch(test_df):\n    X = []\n    y = []\n    for _, row in tqdm(test_df.iterrows(), total=len(test_df)):\n        eeg_id = row['eeg_id']\n        label = row['expert_consensus']\n        \n        spectro = spectrogram_from_eeg(eeg_id)\n        spectro = np.expand_dims(spectro, axis=-1)      # (128, 256, 4, 1)\n        X.append(spectro)\n        y.append(label)\n    \n    X = np.array(X)                      # (N, 128, 256, 4, 1)\n    y = np.array(y)                      # (N,)\n    return X, y\n\n# 2. Verileri hazırla\nX_test, y_true = prepare_batch(test_df)\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:30:00.542339Z","iopub.execute_input":"2025-05-02T18:30:00.542932Z","iopub.status.idle":"2025-05-02T18:33:58.748602Z","shell.execute_reply.started":"2025-05-02T18:30:00.542910Z","shell.execute_reply":"2025-05-02T18:33:58.747577Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 3. Toplu tahmin yap\ny_probs = _3dmodel.predict(X_test, batch_size=16, verbose=1)\ny_pred = np.argmax(y_probs, axis=1)\n\n# 4. Confusion matrix çiz\nclass_names = [\"Seizure\", \"GPD\", \"LRDA\", \"LPD\", \"GRDA\", \"Other\"]\ncm = confusion_matrix(y_true, y_pred)\ndisp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=class_names)\ndisp.plot(cmap=\"Blues\", xticks_rotation=45)\nplt.title(\"Confusion Matrix\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:34:49.981989Z","iopub.execute_input":"2025-05-02T18:34:49.982308Z","iopub.status.idle":"2025-05-02T18:36:40.549026Z","shell.execute_reply.started":"2025-05-02T18:34:49.982279Z","shell.execute_reply":"2025-05-02T18:36:40.548133Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_true","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:25:20.353902Z","iopub.execute_input":"2025-05-02T18:25:20.354441Z","iopub.status.idle":"2025-05-02T18:25:20.358763Z","shell.execute_reply.started":"2025-05-02T18:25:20.354420Z","shell.execute_reply":"2025-05-02T18:25:20.358015Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import classification_report\n\nprint(classification_report(y_true, y_pred, target_names=class_names))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:36:54.608575Z","iopub.execute_input":"2025-05-02T18:36:54.609314Z","iopub.status.idle":"2025-05-02T18:36:54.623400Z","shell.execute_reply.started":"2025-05-02T18:36:54.609286Z","shell.execute_reply":"2025-05-02T18:36:54.622469Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 3. Toplu tahmin yap\ny_probs = _3dmodel.predict(X_test, batch_size=16, verbose=1)\ny_pred = np.argmax(y_probs, axis=1)\n\n# 4. Confusion matrix çiz\nclass_names = [\"Seizure\", \"GPD\", \"LRDA\", \"LPD\", \"GRDA\", \"Other\"]\ncm = confusion_matrix(y_true, y_pred)\ndisp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=class_names)\ndisp.plot(cmap=\"Blues\", xticks_rotation=45)\nplt.title(\"Confusion Matrix\")\nplt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\n\naccuracy = accuracy_score(y_true, y_pred)\nprint(f\"Accuracy: {accuracy:.4f}\")  # Virgülden sonra 4 basamaklı gösterim\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:26:08.308680Z","iopub.execute_input":"2025-05-02T18:26:08.308963Z","iopub.status.idle":"2025-05-02T18:26:08.321627Z","shell.execute_reply.started":"2025-05-02T18:26:08.308945Z","shell.execute_reply":"2025-05-02T18:26:08.320937Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cm = confusion_matrix(real, predictions)\ndisp = ConfusionMatrixDisplay(confusion_matrix=cm)\ndisp.plot(cmap='Blues')  # Renk skalası tercihe bağlı\nplt.title(\"Confusion Matrix\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T17:31:50.744223Z","iopub.execute_input":"2025-05-02T17:31:50.744478Z","iopub.status.idle":"2025-05-02T17:31:51.077242Z","shell.execute_reply.started":"2025-05-02T17:31:50.744457Z","shell.execute_reply":"2025-05-02T17:31:51.076269Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"    # dogru_sayisi = 0\n    # toplam = 0\n\n    # ornekler = test_df.sample(50, random_state=42)  # aynı sonuç için sabit seed kullanılabilir\n\n    # for idx, row in ornekler.iterrows():\n    #     eeg_id = row['eeg_id']\n    #     dogru_label = row['expert_consensus']\n\n    #     tahmin = calc_mean(eeg_id)  # 6 elemanlı numpy array bekleniyor\n    #     tahmin_edilen_label = np.argmax(tahmin)\n\n    #     if tahmin_edilen_label == dogru_label:\n    #         dogru_sayisi += 1\n\n    #     print(tahmin)\n    #     print(\"Tahmin:\", tahmin_edilen_label, \" Gerçek:\", dogru_label, tahmin_edilen_label == dogru_label)\n\n    #     toplam += 1\n    #     dogruluk = dogru_sayisi / toplam\n    #     print(f\"Doğruluk: {dogruluk:.2%}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:26:24.475330Z","iopub.execute_input":"2025-05-02T18:26:24.475607Z","iopub.status.idle":"2025-05-02T18:26:24.479680Z","shell.execute_reply.started":"2025-05-02T18:26:24.475586Z","shell.execute_reply":"2025-05-02T18:26:24.478851Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-01T23:59:03.364607Z","iopub.execute_input":"2025-05-01T23:59:03.365295Z","iopub.status.idle":"2025-05-01T23:59:03.374219Z","shell.execute_reply.started":"2025-05-01T23:59:03.365270Z","shell.execute_reply":"2025-05-01T23:59:03.373303Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T00:02:38.007563Z","iopub.execute_input":"2025-05-02T00:02:38.007900Z","iopub.status.idle":"2025-05-02T00:02:38.372482Z","shell.execute_reply.started":"2025-05-02T00:02:38.007878Z","shell.execute_reply":"2025-05-02T00:02:38.371620Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def predict(model,eeg_id):\n    aray = spectrogram_from_eeg(eeg_id)\n    aray.shape\n    aray = np.expand_dims(aray, axis=0)     # (1, 128, 256, 4)\n    aray = np.expand_dims(aray, axis=-1)    # (1, 128, 256, 4, 1)\n    return model.predict(aray)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:29:25.903158Z","iopub.execute_input":"2025-05-02T18:29:25.903812Z","iopub.status.idle":"2025-05-02T18:29:25.908380Z","shell.execute_reply.started":"2025-05-02T18:29:25.903774Z","shell.execute_reply":"2025-05-02T18:29:25.907750Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def predict_2d(model,eeg_id):\n    aray = spectrogram_from_eeg_2d(eeg_id)\n    aray = np.expand_dims(aray, axis=0)        # (1, 512, 216)\n    aray = np.expand_dims(aray, axis=-1) \n    aray = aray[:, :216, :, :]\n    return model.predict(aray)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:29:25.909650Z","iopub.execute_input":"2025-05-02T18:29:25.910389Z","iopub.status.idle":"2025-05-02T18:29:25.960522Z","shell.execute_reply.started":"2025-05-02T18:29:25.910358Z","shell.execute_reply":"2025-05-02T18:29:25.959688Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predict(_3dmodel,3260319360\t)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T00:23:20.263450Z","iopub.execute_input":"2025-05-02T00:23:20.264394Z","iopub.status.idle":"2025-05-02T00:23:25.577794Z","shell.execute_reply.started":"2025-05-02T00:23:20.264365Z","shell.execute_reply":"2025-05-02T00:23:25.577017Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"_3dmodel.predict(test_df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T17:19:43.142435Z","iopub.execute_input":"2025-05-02T17:19:43.142771Z","iopub.status.idle":"2025-05-02T17:19:43.163252Z","shell.execute_reply.started":"2025-05-02T17:19:43.142746Z","shell.execute_reply":"2025-05-02T17:19:43.161995Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"models[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T00:01:27.097981Z","iopub.execute_input":"2025-05-02T00:01:27.098853Z","iopub.status.idle":"2025-05-02T00:01:27.104700Z","shell.execute_reply.started":"2025-05-02T00:01:27.098818Z","shell.execute_reply":"2025-05-02T00:01:27.103702Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"models[0].predict(spectrogram_from_eeg(523993542))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T00:22:23.800301Z","iopub.execute_input":"2025-05-02T00:22:23.800626Z","iopub.status.idle":"2025-05-02T00:22:24.048406Z","shell.execute_reply.started":"2025-05-02T00:22:23.800603Z","shell.execute_reply":"2025-05-02T00:22:24.047188Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"spectrogram_from_eeg(523993542).shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T00:19:09.716083Z","iopub.execute_input":"2025-05-02T00:19:09.716426Z","iopub.status.idle":"2025-05-02T00:19:09.856414Z","shell.execute_reply.started":"2025-05-02T00:19:09.716405Z","shell.execute_reply":"2025-05-02T00:19:09.855354Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"spectrogram_from_eeg_2d(523993542).shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T00:22:12.850253Z","iopub.execute_input":"2025-05-02T00:22:12.850562Z","iopub.status.idle":"2025-05-02T00:22:12.989089Z","shell.execute_reply.started":"2025-05-02T00:22:12.850541Z","shell.execute_reply":"2025-05-02T00:22:12.988321Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"models","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-01T23:42:02.232586Z","iopub.execute_input":"2025-05-01T23:42:02.232968Z","iopub.status.idle":"2025-05-01T23:42:02.239756Z","shell.execute_reply.started":"2025-05-01T23:42:02.232943Z","shell.execute_reply":"2025-05-01T23:42:02.238928Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nimport shutil\n\nshutil.copy(\n    \"/kaggle/input/modeller/keras/default/1/inceptionresnetv2 57nokta69.h5\",\n    \"/kaggle/working/inceptionresnetv2 57nokta69.keras\"\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-01T23:38:53.946475Z","iopub.execute_input":"2025-05-01T23:38:53.946971Z","iopub.status.idle":"2025-05-01T23:38:56.745357Z","shell.execute_reply.started":"2025-05-01T23:38:53.946946Z","shell.execute_reply":"2025-05-01T23:38:56.744138Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"load_model(\"/kaggle/working/3d.keras\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-01T23:34:44.994360Z","iopub.execute_input":"2025-05-01T23:34:44.994675Z","iopub.status.idle":"2025-05-01T23:34:45.460848Z","shell.execute_reply.started":"2025-05-01T23:34:44.994654Z","shell.execute_reply":"2025-05-01T23:34:45.459966Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"models = []\n\nfor p in model_paths:\n    print(p)\n    try:\n        models.append(load_model(p))\n    except:\n        print(\"hata\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-01T23:31:36.983748Z","iopub.execute_input":"2025-05-01T23:31:36.984102Z","iopub.status.idle":"2025-05-01T23:31:49.998931Z","shell.execute_reply.started":"2025-05-01T23:31:36.984078Z","shell.execute_reply":"2025-05-01T23:31:49.998056Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"load_model(\"/kaggle/input/modeller/keras/default/1/densenet 59nokta27.h5\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-01T23:29:04.593657Z","iopub.execute_input":"2025-05-01T23:29:04.594268Z","iopub.status.idle":"2025-05-01T23:29:08.610184Z","shell.execute_reply.started":"2025-05-01T23:29:04.594243Z","shell.execute_reply":"2025-05-01T23:29:08.609270Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions = [model.predict(test_df) for model in models]\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T00:15:11.232871Z","iopub.execute_input":"2025-05-02T00:15:11.233760Z","iopub.status.idle":"2025-05-02T00:15:11.349263Z","shell.execute_reply.started":"2025-05-02T00:15:11.233729Z","shell.execute_reply":"2025-05-02T00:15:11.347937Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Softmax çıktılarının ortalamasını al\n\naverage_prediction = np.mean(predictions, axis=0)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\n# En yüksek olasılığı olan sınıfı al\nfinal_predictions = np.argmax(average_prediction, axis=1)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"BASE_PATH = \"/kaggle/input/hms-harmful-brain-activity-classification\"\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:29:39.578321Z","iopub.execute_input":"2025-05-02T18:29:39.578652Z","iopub.status.idle":"2025-05-02T18:29:39.582204Z","shell.execute_reply.started":"2025-05-02T18:29:39.578631Z","shell.execute_reply":"2025-05-02T18:29:39.581620Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"eeg_path = BASE_PATH+\"/\"+\"train_eegs\"\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:29:39.583656Z","iopub.execute_input":"2025-05-02T18:29:39.583928Z","iopub.status.idle":"2025-05-02T18:29:39.597458Z","shell.execute_reply.started":"2025-05-02T18:29:39.583911Z","shell.execute_reply":"2025-05-02T18:29:39.596734Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:29:39.598315Z","iopub.execute_input":"2025-05-02T18:29:39.598998Z","iopub.status.idle":"2025-05-02T18:29:39.610429Z","shell.execute_reply.started":"2025-05-02T18:29:39.598974Z","shell.execute_reply":"2025-05-02T18:29:39.609804Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pd.read_parquet(eeg_path+\"/\"+os.listdir(eeg_path)[0])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:29:39.612162Z","iopub.execute_input":"2025-05-02T18:29:39.612371Z","iopub.status.idle":"2025-05-02T18:29:39.991995Z","shell.execute_reply.started":"2025-05-02T18:29:39.612356Z","shell.execute_reply":"2025-05-02T18:29:39.991200Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pywt\nprint(\"The wavelet functions we can use:\")\nprint(pywt.wavelist())\n\nUSE_WAVELET = None #or \"db8\" or anything below","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:29:39.992826Z","iopub.execute_input":"2025-05-02T18:29:39.993110Z","iopub.status.idle":"2025-05-02T18:29:40.072186Z","shell.execute_reply.started":"2025-05-02T18:29:39.993091Z","shell.execute_reply":"2025-05-02T18:29:40.071436Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# DENOISE FUNCTION\ndef maddest(d, axis=None):\n    return np.mean(np.absolute(d - np.mean(d, axis)), axis)\n\ndef denoise(x, wavelet='haar', level=1):    \n    coeff = pywt.wavedec(x, wavelet, mode=\"per\")\n    sigma = (1/0.6745) * maddest(coeff[-level])\n\n    uthresh = sigma * np.sqrt(2*np.log(len(x)))\n    coeff[1:] = (pywt.threshold(i, value=uthresh, mode='hard') for i in coeff[1:])\n\n    ret=pywt.waverec(coeff, wavelet, mode='per')\n    \n    return ret","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:29:40.072954Z","iopub.execute_input":"2025-05-02T18:29:40.073485Z","iopub.status.idle":"2025-05-02T18:29:40.079133Z","shell.execute_reply.started":"2025-05-02T18:29:40.073464Z","shell.execute_reply":"2025-05-02T18:29:40.078426Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import librosa\nimport numpy as np","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:29:40.079826Z","iopub.execute_input":"2025-05-02T18:29:40.080068Z","iopub.status.idle":"2025-05-02T18:29:40.098164Z","shell.execute_reply.started":"2025-05-02T18:29:40.080051Z","shell.execute_reply":"2025-05-02T18:29:40.097456Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def spectrogram_from_eeg_2d(eeg_id, display=False):\n    data = spectrogram_from_eeg(eeg_id)\n    concatenated_image = np.vstack((np.hstack((data[:, :, 0], data[:, :, 1])), \n                                    np.hstack((data[:, :, 2], data[:, :, 3]))))\n    return concatenated_image\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:29:40.099094Z","iopub.execute_input":"2025-05-02T18:29:40.099898Z","iopub.status.idle":"2025-05-02T18:29:40.105299Z","shell.execute_reply.started":"2025-05-02T18:29:40.099871Z","shell.execute_reply":"2025-05-02T18:29:40.104456Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def spectrogram_from_eeg(parquet_path, display=False):\n    parquet_path = BASE_PATH+\"/train_eegs/\"+str(parquet_path)+\".parquet\"\n    # LOAD MIDDLE 50 SECONDS OF EEG SERIES\n    eeg = pd.read_parquet(parquet_path)\n    middle = (len(eeg)-10_000)//2\n    eeg = eeg.iloc[middle:middle+10_000]\n    \n    # VARIABLE TO HOLD SPECTROGRAM\n    img = np.zeros((128,256,4),dtype='float32')\n    \n    if display: plt.figure(figsize=(10,7))\n    signals = []\n    for k in range(4):\n        COLS = FEATS[k]\n        \n        for kk in range(4):\n        \n            # COMPUTE PAIR DIFFERENCES\n            x = eeg[COLS[kk]].values - eeg[COLS[kk+1]].values\n\n            # FILL NANS\n            m = np.nanmean(x)\n            if np.isnan(x).mean()<1: x = np.nan_to_num(x,nan=m)\n            else: x[:] = 0\n\n            # DENOISE\n            if USE_WAVELET:\n                x = denoise(x, wavelet=USE_WAVELET)\n            signals.append(x)\n\n            # RAW SPECTROGRAM\n            mel_spec = librosa.feature.melspectrogram(y=x, sr=200, hop_length=len(x)//256, \n                  n_fft=1024, n_mels=128, fmin=0, fmax=20, win_length=128)\n\n            # LOG TRANSFORM\n            width = (mel_spec.shape[1]//32)*32\n            mel_spec_db = librosa.power_to_db(mel_spec, ref=np.max).astype(np.float32)[:,:width]\n\n            # STANDARDIZE TO -1 TO 1\n            mel_spec_db = (mel_spec_db+40)/40 \n            img[:,:,k] += mel_spec_db\n                \n        # AVERAGE THE 4 MONTAGE DIFFERENCES\n        img[:,:,k] /= 4.0\n        \n        if display:\n            plt.subplot(2,2,k+1)\n            plt.imshow(img[:,:,k],aspect='auto',origin='lower')\n            plt.title(f'EEG {eeg_id} - Spectrogram {NAMES[k]}')\n            \n    if display: \n        plt.show()\n        plt.figure(figsize=(10,5))\n        offset = 0\n        for k in range(4):\n            if k>0: offset -= signals[3-k].min()\n            plt.plot(range(10_000),signals[k]+offset,label=NAMES[3-k])\n            offset += signals[3-k].max()\n        plt.legend()\n        plt.title(f'EEG {eeg_id} Signals')\n        plt.show()\n        print(); print('#'*25); print()\n        \n    return img","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:29:40.108497Z","iopub.execute_input":"2025-05-02T18:29:40.108980Z","iopub.status.idle":"2025-05-02T18:29:40.120406Z","shell.execute_reply.started":"2025-05-02T18:29:40.108962Z","shell.execute_reply":"2025-05-02T18:29:40.119696Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"NAMES = ['LL','LP','RP','RR']\n\nFEATS = [['Fp1','F7','T3','T5','O1'],\n         ['Fp1','F3','C3','P3','O1'],\n         ['Fp2','F8','T4','T6','O2'],\n         ['Fp2','F4','C4','P4','O2']]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:29:40.121177Z","iopub.execute_input":"2025-05-02T18:29:40.121406Z","iopub.status.idle":"2025-05-02T18:29:40.135851Z","shell.execute_reply.started":"2025-05-02T18:29:40.121390Z","shell.execute_reply":"2025-05-02T18:29:40.135101Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"csv = pd.read_csv(BASE_PATH+\"/train.csv\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:29:40.136711Z","iopub.execute_input":"2025-05-02T18:29:40.137549Z","iopub.status.idle":"2025-05-02T18:29:40.387238Z","shell.execute_reply.started":"2025-05-02T18:29:40.137525Z","shell.execute_reply":"2025-05-02T18:29:40.386307Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"unique_eeg_ids_df = csv.drop_duplicates(subset='eeg_id')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:29:40.388226Z","iopub.execute_input":"2025-05-02T18:29:40.388548Z","iopub.status.idle":"2025-05-02T18:29:40.403190Z","shell.execute_reply.started":"2025-05-02T18:29:40.388517Z","shell.execute_reply":"2025-05-02T18:29:40.402254Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"unique_eeg_ids_df = unique_eeg_ids_df[['eeg_id', 'expert_consensus']]\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:29:40.404232Z","iopub.execute_input":"2025-05-02T18:29:40.404568Z","iopub.status.idle":"2025-05-02T18:29:40.414082Z","shell.execute_reply.started":"2025-05-02T18:29:40.404547Z","shell.execute_reply":"2025-05-02T18:29:40.413359Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"unique_values = unique_eeg_ids_df['expert_consensus'].unique()\nprint(unique_values)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:29:40.414916Z","iopub.execute_input":"2025-05-02T18:29:40.415384Z","iopub.status.idle":"2025-05-02T18:29:40.429258Z","shell.execute_reply.started":"2025-05-02T18:29:40.415365Z","shell.execute_reply":"2025-05-02T18:29:40.428565Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels = {0:\"Seizure\",1:\"GPD\",2:\"LRDA\",3:\"LPD\",4:\"GRDA\",5:\"Other\"}\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:29:40.429969Z","iopub.execute_input":"2025-05-02T18:29:40.430237Z","iopub.status.idle":"2025-05-02T18:29:40.439287Z","shell.execute_reply.started":"2025-05-02T18:29:40.430210Z","shell.execute_reply":"2025-05-02T18:29:40.438514Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras import layers, models\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:29:40.440071Z","iopub.execute_input":"2025-05-02T18:29:40.440322Z","iopub.status.idle":"2025-05-02T18:29:40.450712Z","shell.execute_reply.started":"2025-05-02T18:29:40.440305Z","shell.execute_reply":"2025-05-02T18:29:40.449950Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Invert the labels dictionary to map string labels to their numeric values\nlabel_map = {v: k for k, v in labels.items()}\n\n# Replace the string values in the expert_consensus column with their numeric values\nunique_eeg_ids_df['expert_consensus'] = unique_eeg_ids_df['expert_consensus'].map(label_map)\nunique_eeg_ids_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:29:40.451713Z","iopub.execute_input":"2025-05-02T18:29:40.451949Z","iopub.status.idle":"2025-05-02T18:29:40.469118Z","shell.execute_reply.started":"2025-05-02T18:29:40.451934Z","shell.execute_reply":"2025-05-02T18:29:40.468316Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:29:40.469891Z","iopub.execute_input":"2025-05-02T18:29:40.470160Z","iopub.status.idle":"2025-05-02T18:29:40.487439Z","shell.execute_reply.started":"2025-05-02T18:29:40.470144Z","shell.execute_reply":"2025-05-02T18:29:40.486681Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Önce eğitim ve geri kalan verileri (validasyon + test) ayıralım\ntrain_df, rest_df = train_test_split(unique_eeg_ids_df, test_size=0.2, random_state=42) # %30 test + validasyon\n\n# Geri kalan verileri validasyon ve test olarak ayıralım\nval_df, test_df = train_test_split(rest_df, test_size=1/2, random_state=42) # %30'un 1/3'ü test, 2/3'ü validasyon","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:29:40.488337Z","iopub.execute_input":"2025-05-02T18:29:40.488992Z","iopub.status.idle":"2025-05-02T18:29:40.496272Z","shell.execute_reply.started":"2025-05-02T18:29:40.488973Z","shell.execute_reply":"2025-05-02T18:29:40.495633Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import keras.utils\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:29:40.497107Z","iopub.execute_input":"2025-05-02T18:29:40.497437Z","iopub.status.idle":"2025-05-02T18:29:40.506689Z","shell.execute_reply.started":"2025-05-02T18:29:40.497396Z","shell.execute_reply":"2025-05-02T18:29:40.505986Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class EEGDataGenerator(keras.utils.Sequence):\n    \"\"\"\n    Data generator for EEG spectrograms for Keras.\n    Converts EEG IDs to spectrograms using the provided function and returns batches.\n    \"\"\"\n    \n    def __init__(self, dataframe, spectrogram_function, batch_size=32, \n                 shuffle=True, seed=None, is_test=False):\n        \"\"\"\n        Initialize the data generator.\n        \n        Args:\n            dataframe (pd.DataFrame): DataFrame containing 'eeg_id' and 'expert_consensus' columns\n            spectrogram_function (callable): Function that converts eeg_id to spectrogram array\n            batch_size (int): Size of batches to generate\n            shuffle (bool): Whether to shuffle the data after each epoch\n            seed (int): Random seed for reproducibility\n            is_test (bool): If True, don't return labels (for prediction)\n        \"\"\"\n        self.df = dataframe.copy()\n        self.batch_size = batch_size\n        self.spectrogram_function = spectrogram_function\n        self.shuffle = shuffle\n        self.seed = seed\n        self.is_test = is_test\n        \n        # Generate indices\n        self.indices = np.arange(len(self.df))\n        \n        # Class mapping if needed\n        self.classes = sorted(self.df['expert_consensus'].unique())\n        self.class_indices = {cls: i for i, cls in enumerate(self.classes)}\n        \n        # Initial shuffle\n        if self.shuffle:\n            np.random.seed(self.seed)\n            np.random.shuffle(self.indices)\n    \n    def __len__(self):\n        \"\"\"Denotes the number of batches per epoch\"\"\"\n        return int(np.ceil(len(self.df) / self.batch_size))\n    \n    def __getitem__(self, index):\n        \"\"\"Generate one batch of data\"\"\"\n        # Generate indices of the batch\n        batch_indices = self.indices[index * self.batch_size:(index + 1) * self.batch_size]\n        \n        # Get batch data\n        batch_df = self.df.iloc[batch_indices]\n        \n        # Load spectrograms as NumPy arrays from .npy files\n        batch_x = np.array([\n            np.load(f\"/kaggle/working/3d_images/{eeg_id}.npy\") \n            for eeg_id in batch_df['eeg_id']\n        ])\n        \n        if self.is_test:\n            return batch_x\n        \n        # Generate labels (one-hot encoded)\n        batch_y = np.array([\n            self.class_indices[label] \n            for label in batch_df['expert_consensus']\n        ])\n        \n        return batch_x, tf.keras.utils.to_categorical(batch_y, num_classes=len(self.classes))\n\n    \n    def on_epoch_end(self):\n        \"\"\"Updates indices after each epoch\"\"\"\n        if self.shuffle:\n            np.random.seed(self.seed)\n            np.random.shuffle(self.indices)\n\n\ndef create_eeg_generators(train_df, val_df, test_df, spectrogram_from_eeg, \n                          batch_size=32, seed=42):\n    \"\"\"\n    Create train, validation, and test generators for EEG data.\n    \n    Args:\n        train_df (pd.DataFrame): Training data with 'eeg_id' and 'expert_consensus' columns\n        val_df (pd.DataFrame): Validation data with 'eeg_id' and 'expert_consensus' columns\n        test_df (pd.DataFrame): Test data with 'eeg_id' column\n        spectrogram_from_eeg (callable): Function to convert eeg_id to spectrogram\n        batch_size (int): Batch size for generators\n        seed (int): Random seed for reproducibility\n        \n    Returns:\n        tuple: (train_generator, val_generator, test_generator)\n    \"\"\"\n    # Create generators\n    train_generator = EEGDataGenerator(\n        dataframe=train_df,\n        spectrogram_function=spectrogram_from_eeg,\n        batch_size=batch_size,\n        shuffle=True,\n        seed=seed,\n        is_test=False\n    )\n    \n    val_generator = EEGDataGenerator(\n        dataframe=val_df,\n        spectrogram_function=spectrogram_from_eeg,\n        batch_size=batch_size,\n        shuffle=False,\n        seed=seed,\n        is_test=False\n    )\n    \n    test_generator = EEGDataGenerator(\n        dataframe=test_df,\n        spectrogram_function=spectrogram_from_eeg,\n        batch_size=batch_size,\n        shuffle=False,\n        seed=seed,\n        is_test=True\n    )\n    \n    return train_generator, val_generator, test_generator","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:29:40.507356Z","iopub.execute_input":"2025-05-02T18:29:40.507595Z","iopub.status.idle":"2025-05-02T18:29:40.520034Z","shell.execute_reply.started":"2025-05-02T18:29:40.507570Z","shell.execute_reply":"2025-05-02T18:29:40.519231Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"BATCH_SIZE = 16\nLEARNING_RATE = 0.001","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-02T18:10:58.807337Z","iopub.execute_input":"2025-05-02T18:10:58.807647Z","iopub.status.idle":"2025-05-02T18:10:58.811585Z","shell.execute_reply.started":"2025-05-02T18:10:58.807626Z","shell.execute_reply":"2025-05-02T18:10:58.810759Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_generator, val_generator, test_generator = create_eeg_generators(train_df, val_df, test_df, spectrogram_from_eeg, \n                          batch_size=BATCH_SIZE, seed=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-01T23:58:28.229263Z","iopub.execute_input":"2025-05-01T23:58:28.229579Z","iopub.status.idle":"2025-05-01T23:58:28.239032Z","shell.execute_reply.started":"2025-05-01T23:58:28.229557Z","shell.execute_reply":"2025-05-01T23:58:28.238097Z"},"jupyter":{"source_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.optimizers import Adam\n\noptimizer = Adam(learning_rate=LEARNING_RATE)\n\nmodel.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-01T23:58:37.706876Z","iopub.execute_input":"2025-05-01T23:58:37.707332Z","iopub.status.idle":"2025-05-01T23:58:37.763360Z","shell.execute_reply.started":"2025-05-01T23:58:37.707305Z","shell.execute_reply":"2025-05-01T23:58:37.762150Z"}},"outputs":[],"execution_count":null}]}