{"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":11689210,"sourceType":"datasetVersion","datasetId":7336709}],"dockerImageVersionId":31011,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T12:02:22.018068Z","iopub.execute_input":"2025-05-10T12:02:22.018331Z","iopub.status.idle":"2025-05-10T12:02:22.283485Z","shell.execute_reply.started":"2025-05-10T12:02:22.018313Z","shell.execute_reply":"2025-05-10T12:02:22.282722Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df = pd.read_csv(\"/kaggle/input/hms-dataset-split/test_df.csv\")\nuse_df = pd.read_csv(\"/kaggle/input/hms-dataset-split/use_df.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T13:17:11.659441Z","iopub.execute_input":"2025-05-10T13:17:11.660267Z","iopub.status.idle":"2025-05-10T13:17:11.683204Z","shell.execute_reply.started":"2025-05-10T13:17:11.660235Z","shell.execute_reply":"2025-05-10T13:17:11.682547Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T13:17:12.785784Z","iopub.execute_input":"2025-05-10T13:17:12.786064Z","iopub.status.idle":"2025-05-10T13:17:12.794253Z","shell.execute_reply.started":"2025-05-10T13:17:12.786041Z","shell.execute_reply":"2025-05-10T13:17:12.793515Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"use_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T13:17:14.138892Z","iopub.execute_input":"2025-05-10T13:17:14.139437Z","iopub.status.idle":"2025-05-10T13:17:14.146798Z","shell.execute_reply.started":"2025-05-10T13:17:14.139414Z","shell.execute_reply":"2025-05-10T13:17:14.146107Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Tüm verilerden eşit miktarda kullanarak eğitmek istiyoruz.","metadata":{}},{"cell_type":"code","source":"use_df['expert_consensus'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T13:17:05.600924Z","iopub.execute_input":"2025-05-10T13:17:05.601494Z","iopub.status.idle":"2025-05-10T13:17:05.607795Z","shell.execute_reply.started":"2025-05-10T13:17:05.601470Z","shell.execute_reply":"2025-05-10T13:17:05.607036Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# En düşük olan 824 olduğu için 824'te birleştireceğiz","metadata":{}},{"cell_type":"code","source":"# Her bir expert_consensus sınıfı için maksimum 1608 örnek al, daha azsa hepsini al\nuse_df = use_df.groupby('expert_consensus').apply(\n    lambda x: x.sample(n=min(len(x), 1608), random_state=42)\n)\n\n# MultiIndex düzelt\nuse_df = use_df.reset_index(drop=True)\n\n# Dağılımı yazdır\nprint(use_df['expert_consensus'].value_counts())\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T12:02:29.534087Z","iopub.execute_input":"2025-05-10T12:02:29.534805Z","iopub.status.idle":"2025-05-10T12:02:29.553806Z","shell.execute_reply.started":"2025-05-10T12:02:29.534781Z","shell.execute_reply":"2025-05-10T12:02:29.553154Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"use_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T12:02:31.197867Z","iopub.execute_input":"2025-05-10T12:02:31.198634Z","iopub.status.idle":"2025-05-10T12:02:31.207083Z","shell.execute_reply.started":"2025-05-10T12:02:31.198601Z","shell.execute_reply":"2025-05-10T12:02:31.206263Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"use_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-05T18:58:32.597496Z","iopub.execute_input":"2025-05-05T18:58:32.598098Z","iopub.status.idle":"2025-05-05T18:58:32.606318Z","shell.execute_reply.started":"2025-05-05T18:58:32.598073Z","shell.execute_reply":"2025-05-05T18:58:32.605656Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Eldeki tüm veriyi ayrıca import edelim\n\nBASE_PATH = \"/kaggle/input/hms-harmful-brain-activity-classification\"\neeg_path = BASE_PATH+\"/\"+\"train_eegs\"\nimport pandas as pd\npd.read_parquet(eeg_path+\"/\"+os.listdir(eeg_path)[0])\n\ncsv = pd.read_csv(BASE_PATH+\"/train.csv\")\nunique_eeg_ids_df = csv.drop_duplicates(subset='eeg_id')\nunique_eeg_ids_df = unique_eeg_ids_df[['eeg_id', 'expert_consensus']]\nunique_values = unique_eeg_ids_df['expert_consensus'].unique()\nprint(unique_values)\nlabels = {0:\"Seizure\",1:\"GPD\",2:\"LRDA\",3:\"LPD\",4:\"GRDA\",5:\"Other\"}\n# 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-10T12:02:46.909642Z","iopub.execute_input":"2025-05-10T12:02:46.910138Z","iopub.status.idle":"2025-05-10T12:02:47.064363Z","shell.execute_reply.started":"2025-05-10T12:02:46.910100Z","shell.execute_reply":"2025-05-10T12:02:47.063634Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#ilk etapta bunu yapacağım\n# 5 olanları filtrele\nconsensus_5_df = use_df[use_df['expert_consensus'] == 5]\n\n# 4000 tanesini rastgele seç ve düşür\ndrop_5_indices = consensus_5_df.sample(n=4000, random_state=42).index\n\n# Bu indeksleri orijinal DataFrame'den çıkar\nuse_df = use_df.drop(index=drop_5_indices)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T12:02:39.754342Z","iopub.execute_input":"2025-05-10T12:02:39.754798Z","iopub.status.idle":"2025-05-10T12:02:39.793019Z","shell.execute_reply.started":"2025-05-10T12:02:39.754776Z","shell.execute_reply":"2025-05-10T12:02:39.792164Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"use_df.value_counts([\"expert_consensus\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-05T20:17:28.067838Z","iopub.execute_input":"2025-05-05T20:17:28.068533Z","iopub.status.idle":"2025-05-05T20:17:28.075670Z","shell.execute_reply.started":"2025-05-05T20:17:28.068506Z","shell.execute_reply":"2025-05-05T20:17:28.074979Z"}},"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\n\n# 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\n\nimport librosa\n\ndef 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\n\ndef 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","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T12:02:50.168907Z","iopub.execute_input":"2025-05-10T12:02:50.169469Z","iopub.status.idle":"2025-05-10T12:02:50.419683Z","shell.execute_reply.started":"2025-05-10T12:02:50.169446Z","shell.execute_reply":"2025-05-10T12:02:50.419098Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras import layers, models\nfrom sklearn.model_selection import train_test_split","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T12:02:52.922214Z","iopub.execute_input":"2025-05-10T12:02:52.922798Z","iopub.status.idle":"2025-05-10T12:03:07.123246Z","shell.execute_reply.started":"2025-05-10T12:02:52.922777Z","shell.execute_reply":"2025-05-10T12:03:07.122624Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# TEST_DF ZATEN BELİRLİ TRAİN-VALIDATION BÖLELİM\n\ntrain_df, val_df = train_test_split(use_df, test_size=0.1, random_state=42) ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T12:03:07.124459Z","iopub.execute_input":"2025-05-10T12:03:07.124913Z","iopub.status.idle":"2025-05-10T12:03:07.131170Z","shell.execute_reply.started":"2025-05-10T12:03:07.124894Z","shell.execute_reply":"2025-05-10T12:03:07.130205Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(len(use_df))\nprint(len(train_df))\nprint(len(val_df))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T12:03:12.242572Z","iopub.execute_input":"2025-05-10T12:03:12.243089Z","iopub.status.idle":"2025-05-10T12:03:12.247053Z","shell.execute_reply.started":"2025-05-10T12:03:12.243066Z","shell.execute_reply":"2025-05-10T12:03:12.246242Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# veriyi hazırlayalım\ntry:\n    os.mkdir(\"np_files\")\nexcept:\n    pass\n\nNAMES = ['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']]\n\ncount = 0\nfor index, row in unique_eeg_ids_df.iterrows():\n    count += 1\n    data = spectrogram_from_eeg_2d(row[\"eeg_id\"])\n    if count % 100 == 0:\n        print(count, \",\", end=\"\")\n    np.save(f\"np_files/{row['eeg_id']}.npy\", data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T12:03:13.770644Z","iopub.execute_input":"2025-05-10T12:03:13.771182Z","iopub.status.idle":"2025-05-10T12:38:31.022290Z","shell.execute_reply.started":"2025-05-10T12:03:13.771159Z","shell.execute_reply":"2025-05-10T12:38:31.021465Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.applications import DenseNet121\nfrom tensorflow.keras.applications import ResNet50V2\nfrom tensorflow.keras.applications import NASNetMobile\nfrom tensorflow.keras.applications import InceptionResNetV2\nfrom tensorflow.keras.applications import DenseNet121\nfrom tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, Flatten, Dense, Dropout, Concatenate, GlobalAveragePooling2D\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout, BatchNormalization","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T12:39:43.936502Z","iopub.execute_input":"2025-05-10T12:39:43.937006Z","iopub.status.idle":"2025-05-10T12:39:43.941631Z","shell.execute_reply.started":"2025-05-10T12:39:43.936971Z","shell.execute_reply":"2025-05-10T12:39:43.941036Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout, BatchNormalization\n\n# Modeli oluştur\nmodel = Sequential()\n\n# İlk Convolutional katmanı ve MaxPooling katmanı\nmodel.add(Conv2D(32, (3, 3), activation='relu', input_shape=(512, 256, 1)))\nmodel.add(MaxPooling2D((2, 2)))\n\n# İkinci Convolutional katmanı ve MaxPooling katmanı\nmodel.add(Conv2D(64, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D((2, 2)))\n\n# Üçüncü Convolutional katmanı ve MaxPooling katmanı\nmodel.add(Conv2D(128, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D((2, 2)))\n\n# Dördüncü Convolutional katmanı ve MaxPooling katmanı\nmodel.add(Conv2D(256, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D((2, 2)))\n\n# Flatten katmanı\nmodel.add(Flatten())\n\n# Tam bağlantılı katmanlar\nmodel.add(Dense(512, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(256, activation='relu'))\nmodel.add(Dropout(0.5))\n\n# Çıkış katmanı\nmodel.add(Dense(6, activation='softmax'))\n\n\n# Derleme\nmodel.compile(\n    optimizer='adam',\n    loss='categorical_crossentropy',\n    metrics=['accuracy']\n)\n\n# Özet\nmodel.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T14:14:03.454066Z","iopub.execute_input":"2025-05-10T14:14:03.454577Z","iopub.status.idle":"2025-05-10T14:14:03.581449Z","shell.execute_reply.started":"2025-05-10T14:14:03.454553Z","shell.execute_reply":"2025-05-10T14:14:03.580840Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import keras.utils\n\nclass 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        # Generate spectrograms\n        batch_x = np.array([\n            self.spectrogram_function(eeg_id) \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    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)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T12:39:55.678266Z","iopub.execute_input":"2025-05-10T12:39:55.678606Z","iopub.status.idle":"2025-05-10T12:39:55.693579Z","shell.execute_reply.started":"2025-05-10T12:39:55.678586Z","shell.execute_reply":"2025-05-10T12:39:55.690031Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"use_df_Buyuk = pd.read_csv(\"/kaggle/input/hms-dataset-split/use_df.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-05T19:52:03.305895Z","iopub.execute_input":"2025-05-05T19:52:03.306166Z","iopub.status.idle":"2025-05-05T19:52:03.332645Z","shell.execute_reply.started":"2025-05-05T19:52:03.306145Z","shell.execute_reply":"2025-05-05T19:52:03.332105Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"use_df_Buyuk","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-05T19:52:05.098678Z","iopub.execute_input":"2025-05-05T19:52:05.099473Z","iopub.status.idle":"2025-05-05T19:52:05.109013Z","shell.execute_reply.started":"2025-05-05T19:52:05.099440Z","shell.execute_reply":"2025-05-05T19:52:05.108296Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def create_eeg_generators(train_df, val_df, test_df, spectrogram_from_eeg, \n                          batch_size=32, seed=42):\n\n    # Create generators\n    train_generator = EEGDataGenerator(\n        dataframe=train_df,\n        spectrogram_function=get_img,\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=get_img,\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=get_img,\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-10T12:40:04.061631Z","iopub.execute_input":"2025-05-10T12:40:04.062083Z","iopub.status.idle":"2025-05-10T12:40:04.066755Z","shell.execute_reply.started":"2025-05-10T12:40:04.062061Z","shell.execute_reply":"2025-05-10T12:40:04.066016Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 5 olanları filtrele\nconsensus_5_df = use_df_Buyuk[use_df_Buyuk['expert_consensus'] == 5]\n\n# 4000 tanesini rastgele seç ve düşür\ndrop_5_indices = consensus_5_df.sample(n=4000, random_state=42).index\n\n# Bu indeksleri orijinal DataFrame'den çıkar\nuse_df_Buyuk = use_df_Buyuk.drop(index=drop_5_indices)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-05T19:52:57.719078Z","iopub.execute_input":"2025-05-05T19:52:57.719389Z","iopub.status.idle":"2025-05-05T19:52:57.726958Z","shell.execute_reply.started":"2025-05-05T19:52:57.719346Z","shell.execute_reply":"2025-05-05T19:52:57.726265Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"use_df_Buyuk.value_counts([\"expert_consensus\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-05T19:53:44.970539Z","iopub.execute_input":"2025-05-05T19:53:44.970813Z","iopub.status.idle":"2025-05-05T19:53:44.978723Z","shell.execute_reply.started":"2025-05-05T19:53:44.970791Z","shell.execute_reply":"2025-05-05T19:53:44.978119Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df, val_df = train_test_split(use_df, test_size=0.2, random_state=42) # %30 test + validasyon","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T13:17:41.609225Z","iopub.execute_input":"2025-05-10T13:17:41.609501Z","iopub.status.idle":"2025-05-10T13:17:41.615058Z","shell.execute_reply.started":"2025-05-10T13:17:41.609479Z","shell.execute_reply":"2025-05-10T13:17:41.614357Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(train_df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T13:17:43.535785Z","iopub.execute_input":"2025-05-10T13:17:43.536084Z","iopub.status.idle":"2025-05-10T13:17:43.540781Z","shell.execute_reply.started":"2025-05-10T13:17:43.536064Z","shell.execute_reply":"2025-05-10T13:17:43.540033Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(val_df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T13:17:44.720837Z","iopub.execute_input":"2025-05-10T13:17:44.721130Z","iopub.status.idle":"2025-05-10T13:17:44.725947Z","shell.execute_reply.started":"2025-05-10T13:17:44.721109Z","shell.execute_reply":"2025-05-10T13:17:44.725254Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(test_df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T13:17:45.761322Z","iopub.execute_input":"2025-05-10T13:17:45.761946Z","iopub.status.idle":"2025-05-10T13:17:45.767778Z","shell.execute_reply.started":"2025-05-10T13:17:45.761916Z","shell.execute_reply":"2025-05-10T13:17:45.767024Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def get_img(eeg_id):\n    data = np.load(f\"np_files/{eeg_id}.npy\")\n    return data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T12:40:37.003950Z","iopub.execute_input":"2025-05-10T12:40:37.004512Z","iopub.status.idle":"2025-05-10T12:40:37.008134Z","shell.execute_reply.started":"2025-05-10T12:40:37.004489Z","shell.execute_reply":"2025-05-10T12:40:37.007428Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_generator, val_generator, test_generator = create_eeg_generators(train_df, val_df, test_df, get_img, \n                          batch_size=32, seed=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T13:17:50.115483Z","iopub.execute_input":"2025-05-10T13:17:50.115948Z","iopub.status.idle":"2025-05-10T13:17:50.121067Z","shell.execute_reply.started":"2025-05-10T13:17:50.115925Z","shell.execute_reply":"2025-05-10T13:17:50.120428Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T13:17:31.435759Z","iopub.execute_input":"2025-05-10T13:17:31.436066Z","iopub.status.idle":"2025-05-10T13:17:31.445202Z","shell.execute_reply.started":"2025-05-10T13:17:31.436043Z","shell.execute_reply":"2025-05-10T13:17:31.444248Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nprint(\"GPU Available:\", tf.config.list_physical_devices('GPU'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T12:40:41.151505Z","iopub.execute_input":"2025-05-10T12:40:41.151771Z","iopub.status.idle":"2025-05-10T12:40:41.155787Z","shell.execute_reply.started":"2025-05-10T12:40:41.151752Z","shell.execute_reply":"2025-05-10T12:40:41.154959Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.utils import class_weight\n\nclass_weights = class_weight.compute_class_weight(\n    class_weight='balanced',\n    classes=np.unique(train_df['expert_consensus']),\n    y=train_df['expert_consensus']\n)\n\n# Sonuç bir dict'e çevrilmeli\nclass_weights = dict(zip(np.unique(train_df['expert_consensus']), class_weights))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T12:57:03.092359Z","iopub.execute_input":"2025-05-10T12:57:03.093053Z","iopub.status.idle":"2025-05-10T12:57:03.098873Z","shell.execute_reply.started":"2025-05-10T12:57:03.093024Z","shell.execute_reply":"2025-05-10T12:57:03.098314Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class_weights = {\n    0: 1.0,   # Seizure\n    1: 1.0,   # GPD\n    2: 1.0,   # LRDA\n    3: 1.0,   # LPD\n    4: 1.0,   # GRDA\n    5: 1    # Other — cezalandır\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T13:29:35.271812Z","iopub.execute_input":"2025-05-10T13:29:35.272105Z","iopub.status.idle":"2025-05-10T13:29:35.275994Z","shell.execute_reply.started":"2025-05-10T13:29:35.272085Z","shell.execute_reply":"2025-05-10T13:29:35.275136Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.callbacks import ModelCheckpoint\n\n# Checkpoint callback: her epoch sonunda modeli kaydeder\ncheckpoint_cb = ModelCheckpoint(\n    filepath='model_epoch_{epoch:02d}.keras',  # örnek: model_epoch_01.keras\n    save_freq='epoch',\n    save_weights_only=False,  # modeli tam olarak kaydetsin (ağırlık + yapı)\n    verbose=1\n)\n\n# Modeli eğit\nhistory = model.fit(\n    train_generator,\n    validation_data=val_generator,\n    epochs=10,\n    callbacks=[checkpoint_cb],\n    class_weight=class_weights  # ✅ Doğru yerde\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T14:07:45.332143Z","iopub.execute_input":"2025-05-10T14:07:45.332788Z","iopub.status.idle":"2025-05-10T14:07:45.438732Z","shell.execute_reply.started":"2025-05-10T14:07:45.332767Z","shell.execute_reply":"2025-05-10T14:07:45.437754Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.models import load_model\nmodel = load_model(\"model_epoch_06.keras\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T14:08:09.300277Z","iopub.execute_input":"2025-05-10T14:08:09.300879Z","iopub.status.idle":"2025-05-10T14:08:21.523059Z","shell.execute_reply.started":"2025-05-10T14:08:09.300858Z","shell.execute_reply":"2025-05-10T14:08:21.522487Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.metrics import confusion_matrix, classification_report\n\n# Gerçek ve tahmin edilen değerler\ny_true = test_df['expert_consensus'].values.astype(int)\nsteps = int(np.ceil(len(test_generator)))\ny_pred_probs = model.predict(test_generator, steps=steps, verbose=1)\ny_pred = np.argmax(y_pred_probs, axis=1)\n\n# Confusion Matrix\ncm = confusion_matrix(y_true, y_pred)\ncm_normalized = cm.astype('float') / cm.sum(axis=1, keepdims=True)\n\n# Sınıf isimleri\nclass_names = [\"Seizure\", \"GPD\", \"LRDA\", \"LPD\", \"GRDA\", \"Other\"]\n\n# Görselleştirme (satır-normalize edilmiş)\nplt.figure(figsize=(8, 6))\nsns.heatmap(cm_normalized, annot=cm, fmt='d', cmap='Blues',\n            xticklabels=class_names, yticklabels=class_names)\n\nplt.xlabel('Predicted')\nplt.ylabel('True')\nplt.title('Normalized Confusion Matrix (row-wise)')\nplt.show()\n\n# Rapor\nprint(classification_report(y_true, y_pred, target_names=class_names))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T16:01:13.314098Z","iopub.execute_input":"2025-05-10T16:01:13.314734Z","iopub.status.idle":"2025-05-10T16:01:27.231594Z","shell.execute_reply.started":"2025-05-10T16:01:13.314713Z","shell.execute_reply":"2025-05-10T16:01:27.230752Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.layers import Input, Concatenate, GlobalAveragePooling2D, Dense\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.applications import ResNet50V2\n\n# Giriş katmanı (tek kanal)\ninput_layer = Input(shape=(256,512, 1))\n\n# 1 kanalı 3 kanala çıkar\nx = Concatenate()([input_layer, input_layer, input_layer])  # (512, 216, 3)\n\n# Base model\nbase_model = ResNet50V2(weights='imagenet', include_top=False, input_tensor=x)\n\n# Base modelin katmanlarını dondur\nbase_model.trainable = False\n\n# Çıkış işlemleri\nx = base_model.output\nx = GlobalAveragePooling2D()(x)\noutput = Dense(6, activation='softmax')(x)\n\n# Model tanımı\nmodel = Model(inputs=input_layer, outputs=output)\n\n# Modeli derle\nmodel.compile(optimizer=Adam(learning_rate=0.001), loss='categorical_crossentropy', metrics=['accuracy'])\n\n\n# Model özeti\nmodel.summary()\n\n# Modeli eğit\nhistory = model.fit(train_generator, epochs=5, validation_data=val_generator)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T14:29:28.803879Z","iopub.execute_input":"2025-05-10T14:29:28.804498Z","iopub.status.idle":"2025-05-10T14:36:08.315786Z","shell.execute_reply.started":"2025-05-10T14:29:28.804473Z","shell.execute_reply":"2025-05-10T14:36:08.314939Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Giriş katmanı (tek kanal)\ninput_layer = Input(shape=(256,512, 1))\n\n# 1 kanalı 3 kanala çıkar\nx = Concatenate()([input_layer, input_layer, input_layer])  # (512, 216, 3)\n\n# Base model\nbase_model = ResNet50V2(weights='imagenet', include_top=False, input_tensor=x)\n\n# Base modelin katmanlarını dondur\nbase_model.trainable = False\n\n# Çıkış işlemleri\nx = base_model.output\nx = GlobalAveragePooling2D()(x)\noutput = Dense(6, activation='softmax')(x)\n\n# Model tanımı\nmodel = Model(inputs=input_layer, outputs=output)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T15:19:35.485840Z","iopub.execute_input":"2025-05-10T15:19:35.486541Z","iopub.status.idle":"2025-05-10T15:19:36.411755Z","shell.execute_reply.started":"2025-05-10T15:19:35.486517Z","shell.execute_reply":"2025-05-10T15:19:36.411202Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.callbacks import ModelCheckpoint\n\n# Checkpoint callback: her epoch sonunda modeli kaydeder\ncheckpoint_cb = ModelCheckpoint(\n    filepath='model_epoch_{epoch:02d}.keras',  # örnek: model_epoch_01.h5\n    save_freq='epoch',\n    save_weights_only=False,  # modeli tam olarak kaydetsin (ağırlık + yapı)\n    verbose=1\n)\n\n# Modeli eğit\nhistory = model.fit(\n    train_generator,\n    validation_data=val_generator,\n    epochs=10,\n    callbacks=[checkpoint_cb]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T15:28:34.892735Z","iopub.execute_input":"2025-05-10T15:28:34.893346Z","iopub.status.idle":"2025-05-10T15:42:43.141498Z","shell.execute_reply.started":"2025-05-10T15:28:34.893321Z","shell.execute_reply":"2025-05-10T15:42:43.140905Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Tüm katmanları unfreeze et\nfor layer in base_model.layers:\n    layer.trainable = True\n\n# Modeli yeniden derle (daha düşük bir öğrenme oranı ile)\nmodel.compile(optimizer=Adam(learning_rate=0.00001), loss='categorical_crossentropy', metrics=['accuracy'])\n\nfrom tensorflow.keras.callbacks import ModelCheckpoint\n\n# Checkpoint callback: her epoch sonunda modeli kaydeder\ncheckpoint_cb = ModelCheckpoint(\n    filepath='model_epoch_{epoch:02d}.keras',  # örnek: model_epoch_01.h5\n    save_freq='epoch',\n    save_weights_only=False,  # modeli tam olarak kaydetsin (ağırlık + yapı)\n    verbose=1\n)\n\n# Modeli eğit\nhistory = model.fit(\n    train_generator,\n    validation_data=val_generator,\n    epochs=10,\n    callbacks=[checkpoint_cb]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T15:43:57.939776Z","iopub.execute_input":"2025-05-10T15:43:57.940096Z","iopub.status.idle":"2025-05-10T15:58:19.359914Z","shell.execute_reply.started":"2025-05-10T15:43:57.940076Z","shell.execute_reply":"2025-05-10T15:58:19.359365Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.save(\"7133.h5\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T15:58:45.902584Z","iopub.execute_input":"2025-05-10T15:58:45.903290Z","iopub.status.idle":"2025-05-10T15:58:47.722932Z","shell.execute_reply.started":"2025-05-10T15:58:45.903268Z","shell.execute_reply":"2025-05-10T15:58:47.722140Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = load_model(\"6873.h5\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T15:19:53.862727Z","iopub.execute_input":"2025-05-10T15:19:53.863033Z","iopub.status.idle":"2025-05-10T15:19:55.887864Z","shell.execute_reply.started":"2025-05-10T15:19:53.863011Z","shell.execute_reply":"2025-05-10T15:19:55.887331Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Base modelin son birkaç katmanını unfreeze et\nfor layer in base_model.layers[-20:]:  # Son 10 katmanı unfreeze ediyoruz\n    layer.trainable = True\n\n# Modeli yeniden derle (daha düşük bir öğrenme oranı ile)\nmodel.compile(optimizer=Adam(learning_rate=0.00001), loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Modeli yeniden eğit\nhistory_finetune = model.fit(train_generator, epochs=5, validation_data=val_generator)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T15:20:17.819001Z","iopub.execute_input":"2025-05-10T15:20:17.819279Z","iopub.status.idle":"2025-05-10T15:27:32.574378Z","shell.execute_reply.started":"2025-05-10T15:20:17.819259Z","shell.execute_reply":"2025-05-10T15:27:32.573625Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.save(\"6873.h5\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T14:45:08.246188Z","iopub.execute_input":"2025-05-10T14:45:08.246452Z","iopub.status.idle":"2025-05-10T14:45:08.710869Z","shell.execute_reply.started":"2025-05-10T14:45:08.246433Z","shell.execute_reply":"2025-05-10T14:45:08.710260Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Tüm katmanları unfreeze et\nfor layer in base_model.layers:\n    layer.trainable = True\n\n# Modeli yeniden derle (daha düşük bir öğrenme oranı ile)\nmodel.compile(optimizer=Adam(learning_rate=0.00001), loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Modeli yeniden eğit\nhistory_finetune_all = model.fit(train_generator, epochs=5, validation_data=val_generator)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-10T14:48:01.936201Z","iopub.execute_input":"2025-05-10T14:48:01.936480Z","iopub.status.idle":"2025-05-10T15:09:19.684885Z","shell.execute_reply.started":"2025-05-10T14:48:01.936458Z","shell.execute_reply":"2025-05-10T15:09:19.684114Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}