{"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":"import os\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-04T10:49:15.466296Z","iopub.execute_input":"2025-04-04T10:49:15.466638Z","iopub.status.idle":"2025-04-04T10:49:15.470622Z","shell.execute_reply.started":"2025-04-04T10:49:15.466607Z","shell.execute_reply":"2025-04-04T10:49:15.469783Z"}},"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-04-04T10:49:15.471752Z","iopub.execute_input":"2025-04-04T10:49:15.472049Z","iopub.status.idle":"2025-04-04T10:49:15.506541Z","shell.execute_reply.started":"2025-04-04T10:49:15.472013Z","shell.execute_reply":"2025-04-04T10:49:15.505835Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.listdir(\"/kaggle/input/\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-04T10:49:15.508194Z","iopub.execute_input":"2025-04-04T10:49:15.508419Z","iopub.status.idle":"2025-04-04T10:49:15.522966Z","shell.execute_reply.started":"2025-04-04T10:49:15.508364Z","shell.execute_reply":"2025-04-04T10:49:15.522152Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.listdir(\"/kaggle/input/hms-harmful-brain-activity-classification\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-04T10:49:15.524128Z","iopub.execute_input":"2025-04-04T10:49:15.524430Z","iopub.status.idle":"2025-04-04T10:49:15.538347Z","shell.execute_reply.started":"2025-04-04T10:49:15.524400Z","shell.execute_reply":"2025-04-04T10:49:15.537747Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"eeg_path = BASE_PATH+\"/\"+\"train_eegs\"\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-04T10:49:15.539171Z","iopub.execute_input":"2025-04-04T10:49:15.539458Z","iopub.status.idle":"2025-04-04T10:49:15.548819Z","shell.execute_reply.started":"2025-04-04T10:49:15.539432Z","shell.execute_reply":"2025-04-04T10:49:15.548025Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.listdir(eeg_path)[:10]\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-04T10:49:15.549600Z","iopub.execute_input":"2025-04-04T10:49:15.549820Z","iopub.status.idle":"2025-04-04T10:49:15.717438Z","shell.execute_reply.started":"2025-04-04T10:49:15.549802Z","shell.execute_reply":"2025-04-04T10:49:15.716615Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-04T10:49:15.718219Z","iopub.execute_input":"2025-04-04T10:49:15.718531Z","iopub.status.idle":"2025-04-04T10:49:16.633262Z","shell.execute_reply.started":"2025-04-04T10:49:15.718498Z","shell.execute_reply":"2025-04-04T10:49:16.632648Z"}},"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-04-04T10:49:16.634077Z","iopub.execute_input":"2025-04-04T10:49:16.634561Z","iopub.status.idle":"2025-04-04T10:49:16.826191Z","shell.execute_reply.started":"2025-04-04T10:49:16.634525Z","shell.execute_reply":"2025-04-04T10:49:16.825462Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.read_csv(BASE_PATH+\"/train.csv\")\nprint('Train shape', train.shape )\ndisplay( train.head() )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-04T10:49:16.829112Z","iopub.execute_input":"2025-04-04T10:49:16.829329Z","iopub.status.idle":"2025-04-04T10:49:17.071130Z","shell.execute_reply.started":"2025-04-04T10:49:16.829311Z","shell.execute_reply":"2025-04-04T10:49:17.070196Z"}},"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-04-04T10:49:17.072956Z","iopub.execute_input":"2025-04-04T10:49:17.073210Z","iopub.status.idle":"2025-04-04T10:49:17.445992Z","shell.execute_reply.started":"2025-04-04T10:49:17.073187Z","shell.execute_reply":"2025-04-04T10:49:17.445265Z"}},"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-04-04T10:49:17.446735Z","iopub.execute_input":"2025-04-04T10:49:17.447122Z","iopub.status.idle":"2025-04-04T10:49:17.452258Z","shell.execute_reply.started":"2025-04-04T10:49:17.447066Z","shell.execute_reply":"2025-04-04T10:49:17.451599Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import librosa\nimport numpy as np","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-04T10:49:17.453074Z","iopub.execute_input":"2025-04-04T10:49:17.453292Z","iopub.status.idle":"2025-04-04T10:49:17.476591Z","shell.execute_reply.started":"2025-04-04T10:49:17.453262Z","shell.execute_reply":"2025-04-04T10:49:17.475852Z"}},"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-04-04T10:49:17.477351Z","iopub.execute_input":"2025-04-04T10:49:17.477604Z","iopub.status.idle":"2025-04-04T10:49:17.487234Z","shell.execute_reply.started":"2025-04-04T10:49:17.477584Z","shell.execute_reply":"2025-04-04T10:49:17.486591Z"}},"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-04-04T10:49:17.488071Z","iopub.execute_input":"2025-04-04T10:49:17.488343Z","iopub.status.idle":"2025-04-04T10:49:17.504525Z","shell.execute_reply.started":"2025-04-04T10:49:17.488313Z","shell.execute_reply":"2025-04-04T10:49:17.503813Z"}},"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-04-04T10:49:17.505201Z","iopub.execute_input":"2025-04-04T10:49:17.505485Z","iopub.status.idle":"2025-04-04T10:49:17.641078Z","shell.execute_reply.started":"2025-04-04T10:49:17.505461Z","shell.execute_reply":"2025-04-04T10:49:17.640129Z"}},"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-04-04T10:49:17.642078Z","iopub.execute_input":"2025-04-04T10:49:17.642316Z","iopub.status.idle":"2025-04-04T10:49:17.654467Z","shell.execute_reply.started":"2025-04-04T10:49:17.642294Z","shell.execute_reply":"2025-04-04T10:49:17.653526Z"}},"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-04-04T10:49:17.655314Z","iopub.execute_input":"2025-04-04T10:49:17.655605Z","iopub.status.idle":"2025-04-04T10:49:17.665658Z","shell.execute_reply.started":"2025-04-04T10:49:17.655582Z","shell.execute_reply":"2025-04-04T10:49:17.664820Z"}},"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-04-04T10:49:17.666544Z","iopub.execute_input":"2025-04-04T10:49:17.666780Z","iopub.status.idle":"2025-04-04T10:49:17.682478Z","shell.execute_reply.started":"2025-04-04T10:49:17.666759Z","shell.execute_reply":"2025-04-04T10:49:17.681737Z"}},"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-04-04T10:49:17.683341Z","iopub.execute_input":"2025-04-04T10:49:17.683669Z","iopub.status.idle":"2025-04-04T10:49:17.696472Z","shell.execute_reply.started":"2025-04-04T10:49:17.683631Z","shell.execute_reply":"2025-04-04T10:49:17.695635Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras import layers, models\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-04T10:49:17.697153Z","iopub.execute_input":"2025-04-04T10:49:17.697355Z","iopub.status.idle":"2025-04-04T10:49:29.226019Z","shell.execute_reply.started":"2025-04-04T10:49:17.697336Z","shell.execute_reply":"2025-04-04T10:49:29.225273Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = models.Sequential([\n    layers.Conv3D(64, (3, 3, 3), activation='relu', padding='same', input_shape=(128, 256, 4, 1)),\n    layers.BatchNormalization(),\n    layers.Conv3D(64, (3, 3, 3), activation='relu', padding='same'),\n    layers.MaxPooling3D((2, 2, 1)),\n\n    layers.Conv3D(128, (3, 3, 3), activation='relu', padding='same'),\n    layers.BatchNormalization(),\n    layers.Conv3D(128, (3, 3, 3), activation='relu', padding='same'),\n    layers.MaxPooling3D((2, 2, 1)),\n\n    layers.Conv3D(256, (3, 3, 3), activation='relu', padding='same'),\n    layers.BatchNormalization(),\n    layers.Conv3D(256, (3, 3, 3), activation='relu', padding='same'),\n\n    layers.GlobalAveragePooling3D(),\n    layers.Dense(512, activation='relu'),\n    layers.Dropout(0.5),\n    layers.Dense(256, activation='relu'),\n    layers.Dropout(0.3),\n    layers.Dense(6, activation='softmax')\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-04T10:49:29.226833Z","iopub.execute_input":"2025-04-04T10:49:29.227269Z","iopub.status.idle":"2025-04-04T10:49:31.442719Z","shell.execute_reply.started":"2025-04-04T10:49:29.227247Z","shell.execute_reply":"2025-04-04T10:49:31.441798Z"}},"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-04-04T10:49:31.443595Z","iopub.execute_input":"2025-04-04T10:49:31.443852Z","iopub.status.idle":"2025-04-04T10:49:31.454202Z","shell.execute_reply.started":"2025-04-04T10:49:31.443817Z","shell.execute_reply":"2025-04-04T10:49:31.453601Z"}},"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-04-04T10:49:31.455031Z","iopub.execute_input":"2025-04-04T10:49:31.455316Z","iopub.status.idle":"2025-04-04T10:49:32.211304Z","shell.execute_reply.started":"2025-04-04T10:49:31.455287Z","shell.execute_reply":"2025-04-04T10:49:32.210641Z"}},"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-04-04T10:49:32.212087Z","iopub.execute_input":"2025-04-04T10:49:32.212616Z","iopub.status.idle":"2025-04-04T10:49:32.221184Z","shell.execute_reply.started":"2025-04-04T10:49:32.212583Z","shell.execute_reply":"2025-04-04T10:49:32.220264Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import keras.utils\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-04T10:49:32.222170Z","iopub.execute_input":"2025-04-04T10:49:32.222520Z","iopub.status.idle":"2025-04-04T10:49:32.236314Z","shell.execute_reply.started":"2025-04-04T10:49:32.222485Z","shell.execute_reply":"2025-04-04T10:49:32.235393Z"}},"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        # 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)\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-04-04T10:49:32.237104Z","iopub.execute_input":"2025-04-04T10:49:32.237338Z","iopub.status.idle":"2025-04-04T10:49:32.252060Z","shell.execute_reply.started":"2025-04-04T10:49:32.237309Z","shell.execute_reply":"2025-04-04T10:49:32.251210Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"BATCH_SIZE = 32\nLEARNING_RATE = 0.01","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-04T10:49:32.255273Z","iopub.execute_input":"2025-04-04T10:49:32.255527Z","iopub.status.idle":"2025-04-04T10:49:32.271561Z","shell.execute_reply.started":"2025-04-04T10:49:32.255505Z","shell.execute_reply":"2025-04-04T10:49:32.270754Z"}},"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-04-04T10:49:32.272845Z","iopub.execute_input":"2025-04-04T10:49:32.273195Z","iopub.status.idle":"2025-04-04T10:49:32.290050Z","shell.execute_reply.started":"2025-04-04T10:49:32.273164Z","shell.execute_reply":"2025-04-04T10:49:32.289416Z"}},"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-04-04T10:49:32.290817Z","iopub.execute_input":"2025-04-04T10:49:32.291126Z","iopub.status.idle":"2025-04-04T10:49:32.317692Z","shell.execute_reply.started":"2025-04-04T10:49:32.291103Z","shell.execute_reply":"2025-04-04T10:49:32.316761Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-04T10:49:32.318776Z","iopub.execute_input":"2025-04-04T10:49:32.319098Z","iopub.status.idle":"2025-04-04T10:49:32.351293Z","shell.execute_reply.started":"2025-04-04T10:49:32.319069Z","shell.execute_reply":"2025-04-04T10:49:32.350668Z"}},"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-04-04T10:49:32.351994Z","iopub.execute_input":"2025-04-04T10:49:32.352192Z","iopub.status.idle":"2025-04-04T10:49:32.356521Z","shell.execute_reply.started":"2025-04-04T10:49:32.352165Z","shell.execute_reply":"2025-04-04T10:49:32.355710Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Train the model\nhistory = model.fit(\n    train_generator,\n    validation_data=val_generator,\n    epochs=10\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-04T10:49:32.357299Z","iopub.execute_input":"2025-04-04T10:49:32.357544Z"}},"outputs":[],"execution_count":null}]}