{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":59093,"databundleVersionId":7469972,"sourceType":"competition"},{"sourceId":6091,"sourceType":"modelInstanceVersion","modelInstanceId":4623},{"sourceId":6098,"sourceType":"modelInstanceVersion","modelInstanceId":4629},{"sourceId":6113,"sourceType":"modelInstanceVersion","modelInstanceId":4618},{"sourceId":6127,"sourceType":"modelInstanceVersion","modelInstanceId":4598}],"dockerImageVersionId":30648,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n#    for filename in filenames:\n        #print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session\n\nimport os\nos.environ[\"KERAS_BACKEND\"] = \"jax\" # you can also use tensorflow or torch\n\nimport keras_cv\nimport keras\nfrom tensorflow.python.framework import ops as tf_ops\n\n#from keras import ops\nimport tensorflow as tf\n\nimport cv2\nimport pandas as pd\nimport numpy as np\nfrom glob import glob\nfrom tqdm.notebook import tqdm\nimport joblib\n\nimport matplotlib.pyplot as plt \nfrom sklearn.ensemble import RandomForestClassifier\nfrom xgboost import XGBClassifier\nfrom sklearn import svm\nimport gc\n\n\nclass CFG:\n    verbose = 1  # Verbosity\n    seed = 42  # Random seed\n    preset = \"resnet50_v2_imagenet\"#\"mobilenet_v3_large_imagenet\"#\"efficientnetv2_b2_imagenet\"  # Name of pretrained classifier\n    image_size = [400, 300]  # Input image size\n    epochs = 1 # Training epochs\n    batch_size = 64  # Batch size\n    lr_mode = \"cos\" # LR scheduler mode from one of \"cos\", \"step\", \"exp\"\n    drop_remainder = True  # Drop incomplete batches\n    num_classes = 6 # Number of classes in the dataset\n    fold = 0 # Which fold to set as validation data\n    class_names = ['Seizure', 'LPD', 'GPD', 'LRDA','GRDA', 'Other']\n    label2name = dict(enumerate(class_names))\n    name2label = {v:k for k, v in label2name.items()}\n    \n    \n    \nkeras.utils.set_random_seed(CFG.seed)\n\n\nBASE_PATH = \"/kaggle/input/hms-harmful-brain-activity-classification\"\n\nSPEC_DIR = \"/tmp/dataset/hms-hbac\"\nos.makedirs(SPEC_DIR+'/train_spectrograms', exist_ok=True)\nos.makedirs(SPEC_DIR+'/test_spectrograms', exist_ok=True)\n\n# Train + Valid\ndf = pd.read_csv(f'{BASE_PATH}/train.csv')\ndf['eeg_path'] = f'{BASE_PATH}/train_eegs/'+df['eeg_id'].astype(str)+'.parquet'\ndf['spec_path'] = f'{BASE_PATH}/train_spectrograms/'+df['spectrogram_id'].astype(str)+'.parquet'\ndf['spec2_path'] = f'{SPEC_DIR}/train_spectrograms/'+df['spectrogram_id'].astype(str)+'.npy'\ndf['class_name'] = df.expert_consensus.copy()\ndf['class_label'] = df.expert_consensus.map(CFG.name2label)\ndisplay(df.head(2))\n\n# Test\ntest_df = pd.read_csv(f'{BASE_PATH}/test.csv')\ntest_df['eeg_path'] = f'{BASE_PATH}/test_eegs/'+test_df['eeg_id'].astype(str)+'.parquet'\ntest_df['spec_path'] = f'{BASE_PATH}/test_spectrograms/'+test_df['spectrogram_id'].astype(str)+'.parquet'\ntest_df['spec2_path'] = f'{SPEC_DIR}/test_spectrograms/'+test_df['spectrogram_id'].astype(str)+'.npy'\ndisplay(test_df.head(2))\n\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-04T17:18:55.945300Z","iopub.execute_input":"2024-04-04T17:18:55.945617Z","iopub.status.idle":"2024-04-04T17:19:16.283868Z","shell.execute_reply.started":"2024-04-04T17:18:55.945588Z","shell.execute_reply":"2024-04-04T17:19:16.282866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define a function to process a single eeg_id\ndef process_spec(spec_id, split=\"train\"):\n    spec_path = f\"{BASE_PATH}/{split}_spectrograms/{spec_id}.parquet\"\n    spec = pd.read_parquet(spec_path)\n    spec = spec.fillna(0).values[:, 1:].T # fill NaN values with 0, transpose for (Time, Freq) -> (Freq, Time)\n    spec = spec.astype(\"float32\")\n    np.save(f\"{SPEC_DIR}/{split}_spectrograms/{spec_id}.npy\", spec)\n\n# Get unique spec_ids of train and valid data\nspec_ids = df[\"spectrogram_id\"].unique()\n\n# Parallelize the processing using joblib for training data\n_ = joblib.Parallel(n_jobs=-1, backend=\"loky\")(\n    joblib.delayed(process_spec)(spec_id, \"train\")\n    for spec_id in tqdm(spec_ids, total=len(spec_ids))\n)\n\n# Get unique spec_ids of test data\ntest_spec_ids = test_df[\"spectrogram_id\"].unique()\n\n# Parallelize the processing using joblib for test data\n_ = joblib.Parallel(n_jobs=-1, backend=\"loky\")(\n    joblib.delayed(process_spec)(spec_id, \"test\")\n    for spec_id in tqdm(test_spec_ids, total=len(test_spec_ids))\n)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:19:26.423536Z","iopub.execute_input":"2024-04-04T17:19:26.423899Z","iopub.status.idle":"2024-04-04T17:22:30.059802Z","shell.execute_reply.started":"2024-04-04T17:19:26.423869Z","shell.execute_reply":"2024-04-04T17:22:30.058905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_augmenter(dim=CFG.image_size):\n    augmenters = [\n        keras_cv.layers.MixUp(alpha=2.0),\n        keras_cv.layers.RandomCutout(height_factor=(1.0, 1.0),\n                                     width_factor=(0.06, 0.1)), # freq-masking\n        keras_cv.layers.RandomCutout(height_factor=(0.06, 0.1),\n                                     width_factor=(1.0, 1.0)), # time-masking\n    ]\n    \n    def augment(img, label):\n        data = {\"images\":img, \"labels\":label}\n        for augmenter in augmenters:\n            if tf.random.uniform([]) < 0.5:\n                data = augmenter(data, training=True)\n        return data[\"images\"], data[\"labels\"]\n    \n    return augment\n\n\ndef build_decoder(with_labels=True, target_size=CFG.image_size, dtype=32):\n    def decode_signal(path, offset=None):\n        # Read .npy files and process the signal\n        file_bytes = tf.io.read_file(path)\n        sig = tf.io.decode_raw(file_bytes, tf.float32)\n        sig = sig[1024//dtype:]  # Remove header tag\n        sig = tf.reshape(sig, [400, -1])\n        \n        # Extract labeled subsample from full spectrogram using \"offset\"\n        if offset is not None: \n            offset = offset // 2  # Only odd values are given\n            sig = sig[:, offset:offset+300]\n            \n            # Pad spectrogram to ensure the same input shape of [400, 300]\n            pad_size = tf.math.maximum(0, 300 - tf.shape(sig)[1])\n            sig = tf.pad(sig, [[0, 0], [0, pad_size]])\n            sig = tf.reshape(sig, [400, 300])\n        \n        # Log spectrogram \n        sig = tf.clip_by_value(sig, tf.math.exp(-4.0), tf.math.exp(8.0)) # avoid 0 in log\n        sig = tf.math.log(sig)\n        \n        # Normalize spectrogram\n        sig -= tf.math.reduce_mean(sig)\n        sig /= tf.math.reduce_std(sig) + 1e-6\n        \n        # Mono channel to 3 channels to use \"ImageNet\" weights\n        sig = tf.tile(sig[..., None], [1, 1, 3])\n        return sig\n    \n    def decode_label(label):\n        label = tf.one_hot(label, CFG.num_classes)\n        label = tf.cast(label, tf.float32)\n        label = tf.reshape(label, [CFG.num_classes])\n        return label\n    \n    def decode_with_labels(path, offset=None, label=None):\n        sig = decode_signal(path, offset)\n        label = decode_label(label)\n        return (sig, label)\n    \n    return decode_with_labels if with_labels else decode_signal\n\n\ndef build_dataset(paths, offsets=None, labels=None, batch_size=32, cache=True,\n                  decode_fn=None, augment_fn=None,\n                  augment=False, repeat=True, shuffle=1024, \n                  cache_dir=\"\", drop_remainder=False):\n    if cache_dir != \"\" and cache is True:\n        os.makedirs(cache_dir, exist_ok=True)\n    \n    if decode_fn is None:\n        decode_fn = build_decoder(labels is not None)\n    \n    if augment_fn is None:\n        augment_fn = build_augmenter()\n    \n    AUTO = tf.data.experimental.AUTOTUNE\n    slices = (paths, offsets) if labels is None else (paths, offsets, labels)\n    \n    ds = tf.data.Dataset.from_tensor_slices(slices)\n    ds = ds.map(decode_fn, num_parallel_calls=AUTO)\n    ds = ds.cache(cache_dir) if cache else ds\n    ds = ds.repeat() if repeat else ds\n    if shuffle: \n        ds = ds.shuffle(shuffle, seed=CFG.seed)\n        opt = tf.data.Options()\n        opt.experimental_deterministic = False\n        ds = ds.with_options(opt)\n    ds = ds.batch(batch_size, drop_remainder=drop_remainder)\n    ds = ds.map(augment_fn, num_parallel_calls=AUTO) if augment else ds\n    ds = ds.prefetch(AUTO)\n    return ds","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:22:37.453228Z","iopub.execute_input":"2024-04-04T17:22:37.453582Z","iopub.status.idle":"2024-04-04T17:22:37.474616Z","shell.execute_reply.started":"2024-04-04T17:22:37.453556Z","shell.execute_reply":"2024-04-04T17:22:37.473299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import StratifiedGroupKFold\n\nsgkf = StratifiedGroupKFold(n_splits=5, shuffle=True, random_state=CFG.seed)\n\ndf[\"fold\"] = -1\ndf.reset_index(drop=True, inplace=True)\nfor fold, (train_idx, valid_idx) in enumerate(\n    sgkf.split(df, y=df[\"class_label\"], groups=df[\"patient_id\"])\n):\n    df.loc[valid_idx, \"fold\"] = fold\ndf.groupby([\"fold\", \"class_name\"])[[\"eeg_id\"]].count().T","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:22:44.157276Z","iopub.execute_input":"2024-04-04T17:22:44.158109Z","iopub.status.idle":"2024-04-04T17:22:45.448124Z","shell.execute_reply.started":"2024-04-04T17:22:44.158074Z","shell.execute_reply":"2024-04-04T17:22:45.447246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Sample from full data\nsample_df = df.groupby(\"spectrogram_id\").head(1).reset_index(drop=True)\ntrain_df = sample_df[sample_df.fold != CFG.fold]\nvalid_df = sample_df[sample_df.fold == CFG.fold]\nprint(f\"# Num Train: {len(train_df)} | Num Valid: {len(valid_df)}\")\n\n# Train\ntrain_paths = train_df.spec2_path.values\ntrain_offsets = train_df.spectrogram_label_offset_seconds.values.astype(int)\ntrain_labels = train_df.class_label.values\ntrain_ds = build_dataset(train_paths, train_offsets, train_labels, batch_size=CFG.batch_size,\n                         repeat=True, shuffle=True, augment=True, cache=True)\n\n# Valid\nvalid_paths = valid_df.spec2_path.values\nvalid_offsets = valid_df.spectrogram_label_offset_seconds.values.astype(int)\nvalid_labels = valid_df.class_label.values\nvalid_ds = build_dataset(valid_paths, valid_offsets, valid_labels, batch_size=CFG.batch_size,\n                         repeat=False, shuffle=False, augment=False, cache=False)\n\n\n#Train Data in Step 1 for Random Forest Classifier\nrf_train_ds = build_dataset(train_paths, train_offsets, batch_size=CFG.batch_size,\n                         repeat=False, shuffle=False, cache=False, augment=False)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:22:52.941328Z","iopub.execute_input":"2024-04-04T17:22:52.941703Z","iopub.status.idle":"2024-04-04T17:23:02.935914Z","shell.execute_reply.started":"2024-04-04T17:22:52.941670Z","shell.execute_reply":"2024-04-04T17:23:02.934829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Use to create Pair dataset dataframes for training set\n\nclass_grouped = sample_df.groupby(sample_df.class_label)\n\ndf_seizure = class_grouped.get_group(0)\ndf_lpd = class_grouped.get_group(1)\ndf_gpd = class_grouped.get_group(2)\ndf_lrda = class_grouped.get_group(3)\ndf_grda = class_grouped.get_group(4)\ndf_other = class_grouped.get_group(5)\n\n#Seizure vs rest of the classses\nseizure_lpd_frame = [df_seizure, df_lpd]\nseizure_lpd_df = pd.concat(seizure_lpd_frame)\n\nseizure_gpd_frame = [df_seizure, df_gpd]\nseizure_gpd_df = pd.concat(seizure_gpd_frame)\n\nseizure_lrda_frame = [df_seizure, df_lrda]\nseizure_lrda_df = pd.concat(seizure_lrda_frame)\n\nseizure_grda_frame = [df_seizure, df_grda]\nseizure_grda_df = pd.concat(seizure_grda_frame)\n\nseizure_other_frame = [df_seizure, df_other]\nseizure_other_df = pd.concat(seizure_other_frame)\n\n#LPD vs rest of the classes\nlpd_gpd_frame = [df_lpd, df_gpd]\nlpd_gpd_df = pd.concat(lpd_gpd_frame)\n\nlpd_lrda_frame = [df_lpd, df_lrda]\nlpd_lrda_df = pd.concat(lpd_lrda_frame)\n\nlpd_grda_frame = [df_lpd, df_grda]\nlpd_grda_df = pd.concat(lpd_grda_frame)\n\nlpd_other_frame = [df_lpd, df_other]\nlpd_other_df = pd.concat(lpd_other_frame)\n\n#GPD vs rest of the classes\n\ngpd_lrda_frame = [df_gpd, df_lrda]\ngpd_lrda_df = pd.concat(lpd_gpd_frame)\n\ngpd_grda_frame = [df_gpd, df_grda]\ngpd_grda_df = pd.concat(gpd_grda_frame)\n\ngpd_other_frame = [df_gpd, df_other]\ngpd_other_df = pd.concat(gpd_other_frame)\n\n#LRDA vs rest of the classes\n\nlrda_grda_frame = [df_lrda, df_grda]\nlrda_grda_df = pd.concat(lrda_grda_frame)\n\nlrda_other_frame = [df_lrda, df_other]\nlrda_other_df = pd.concat(lrda_other_frame)\n\n#GRDA vs rest of the classes\ngrda_other_frame = [df_grda, df_other]\ngrda_other_df = pd.concat(grda_other_frame)\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:23:10.703134Z","iopub.execute_input":"2024-04-04T17:23:10.703773Z","iopub.status.idle":"2024-04-04T17:23:10.741802Z","shell.execute_reply.started":"2024-04-04T17:23:10.703742Z","shell.execute_reply":"2024-04-04T17:23:10.740944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Use to create Pair datasets and training Labels\n\n#Seizure vs rest of the classes\nseizure_lpd_paths = seizure_lpd_df.spec2_path.values\nseizure_lpd_offsets = seizure_lpd_df.spectrogram_label_offset_seconds.values.astype(int)\nseizure_lpd_labels = seizure_lpd_df.class_label.values\nseizure_lpd_ds = build_dataset(seizure_lpd_paths, seizure_lpd_offsets, seizure_lpd_labels, batch_size=CFG.batch_size,\n                         repeat=False, shuffle=False, augment=False, cache=False)\n\nseizure_gpd_paths = seizure_gpd_df.spec2_path.values\nseizure_gpd_offsets = seizure_gpd_df.spectrogram_label_offset_seconds.values.astype(int)\nseizure_gpd_labels = seizure_gpd_df.class_label.values\nseizure_gpd_ds = build_dataset(seizure_gpd_paths, seizure_gpd_offsets, seizure_gpd_labels, batch_size=CFG.batch_size,\n                         repeat=False, shuffle=False, augment=False, cache=False)\n\nseizure_lrda_paths = seizure_lrda_df.spec2_path.values\nseizure_lrda_offsets = seizure_lrda_df.spectrogram_label_offset_seconds.values.astype(int)\nseizure_lrda_labels = seizure_lrda_df.class_label.values\nseizure_lrda_ds = build_dataset(seizure_lrda_paths, seizure_lrda_offsets, seizure_lrda_labels, batch_size=CFG.batch_size,\n                         repeat=False, shuffle=False, augment=False, cache=False)\n\nseizure_grda_paths = seizure_grda_df.spec2_path.values\nseizure_grda_offsets = seizure_grda_df.spectrogram_label_offset_seconds.values.astype(int)\nseizure_grda_labels = seizure_grda_df.class_label.values\nseizure_grda_ds = build_dataset(seizure_grda_paths, seizure_grda_offsets, seizure_grda_labels, batch_size=CFG.batch_size,\n                         repeat=False, shuffle=False, augment=False, cache=False)\n\nseizure_other_paths = seizure_other_df.spec2_path.values\nseizure_other_offsets = seizure_other_df.spectrogram_label_offset_seconds.values.astype(int)\nseizure_other_labels = seizure_other_df.class_label.values\nseizure_other_ds = build_dataset(seizure_other_paths, seizure_other_offsets, seizure_other_labels, batch_size=CFG.batch_size,\n                         repeat=False, shuffle=False, augment=False, cache=False)\n\n#LPD vs rest of the classes\n\nlpd_gpd_paths = lpd_gpd_df.spec2_path.values\nlpd_gpd_offsets = lpd_gpd_df.spectrogram_label_offset_seconds.values.astype(int)\nlpd_gpd_labels = lpd_gpd_df.class_label.values\nlpd_gpd_ds = build_dataset(lpd_gpd_paths, lpd_gpd_offsets, lpd_gpd_labels, batch_size=CFG.batch_size,\n                         repeat=False, shuffle=False, augment=False, cache=False)\n\nlpd_lrda_paths = lpd_lrda_df.spec2_path.values\nlpd_lrda_offsets = lpd_lrda_df.spectrogram_label_offset_seconds.values.astype(int)\nlpd_lrda_labels = lpd_lrda_df.class_label.values\nlpd_lrda_ds = build_dataset(lpd_lrda_paths, lpd_lrda_offsets, lpd_lrda_labels, batch_size=CFG.batch_size,\n                         repeat=False, shuffle=False, augment=False, cache=False)\n\nlpd_grda_paths = lpd_grda_df.spec2_path.values\nlpd_grda_offsets = lpd_grda_df.spectrogram_label_offset_seconds.values.astype(int)\nlpd_grda_labels = lpd_grda_df.class_label.values\nlpd_grda_ds = build_dataset(lpd_grda_paths, lpd_grda_offsets, lpd_grda_labels, batch_size=CFG.batch_size,\n                         repeat=False, shuffle=False, augment=False, cache=False)\n\nlpd_other_paths = lpd_other_df.spec2_path.values\nlpd_other_offsets = lpd_other_df.spectrogram_label_offset_seconds.values.astype(int)\nlpd_other_labels = lpd_other_df.class_label.values\nlpd_other_ds = build_dataset(lpd_other_paths, lpd_other_offsets, lpd_other_labels, batch_size=CFG.batch_size,\n                         repeat=False, shuffle=False, augment=False, cache=False)\n\n\n#GPD vs rest of the classes\n\ngpd_lrda_paths = gpd_lrda_df.spec2_path.values\ngpd_lrda_offsets = gpd_lrda_df.spectrogram_label_offset_seconds.values.astype(int)\ngpd_lrda_labels = gpd_lrda_df.class_label.values\ngpd_lrda_ds = build_dataset(gpd_lrda_paths, gpd_lrda_offsets, gpd_lrda_labels, batch_size=CFG.batch_size,\n                         repeat=False, shuffle=False, augment=False, cache=False)\n\ngpd_grda_paths = gpd_grda_df.spec2_path.values\ngpd_grda_offsets = gpd_grda_df.spectrogram_label_offset_seconds.values.astype(int)\ngpd_grda_labels = gpd_grda_df.class_label.values\ngpd_grda_ds = build_dataset(gpd_grda_paths, gpd_grda_offsets, gpd_grda_labels, batch_size=CFG.batch_size,\n                         repeat=False, shuffle=False, augment=False, cache=False)\n\ngpd_other_paths = gpd_other_df.spec2_path.values\ngpd_other_offsets = gpd_other_df.spectrogram_label_offset_seconds.values.astype(int)\ngpd_other_labels = gpd_other_df.class_label.values\ngpd_other_ds = build_dataset(gpd_other_paths, gpd_other_offsets, gpd_other_labels, batch_size=CFG.batch_size,\n                         repeat=False, shuffle=False, augment=False, cache=False)\n\n\n#LRDA vs rest of the classes\n\nlrda_grda_paths = lrda_grda_df.spec2_path.values\nlrda_grda_offsets = lrda_grda_df.spectrogram_label_offset_seconds.values.astype(int)\nlrda_grda_labels = lrda_grda_df.class_label.values\nlrda_grda_ds = build_dataset(lrda_grda_paths, lrda_grda_offsets, lrda_grda_labels, batch_size=CFG.batch_size,\n                         repeat=False, shuffle=False, augment=False, cache=False)\n\nlrda_other_paths = lrda_other_df.spec2_path.values\nlrda_other_offsets = lrda_other_df.spectrogram_label_offset_seconds.values.astype(int)\nlrda_other_labels = lrda_other_df.class_label.values\nlrda_other_ds = build_dataset(lrda_other_paths, lrda_other_offsets, lrda_other_labels, batch_size=CFG.batch_size,\n                         repeat=False, shuffle=False, augment=False, cache=False)\n\n#GRDA vs rest of the classes\n\ngrda_other_paths = grda_other_df.spec2_path.values\ngrda_other_offsets = grda_other_df.spectrogram_label_offset_seconds.values.astype(int)\ngrda_other_labels = grda_other_df.class_label.values\ngrda_other_ds = build_dataset(grda_other_paths, grda_other_offsets, grda_other_labels, batch_size=CFG.batch_size,\n                         repeat=False, shuffle=False, augment=False, cache=False)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:23:29.824668Z","iopub.execute_input":"2024-04-04T17:23:29.825127Z","iopub.status.idle":"2024-04-04T17:23:30.750510Z","shell.execute_reply.started":"2024-04-04T17:23:29.825092Z","shell.execute_reply":"2024-04-04T17:23:30.749730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgs, tars = next(iter(train_ds))\n\nnum_imgs = 8\nplt.figure(figsize=(4*4, num_imgs//4*5))\nfor i in range(num_imgs):\n    plt.subplot(num_imgs//4, 4, i + 1)\n    img = imgs[i].numpy()[...,0]  # Adjust as per your image data format\n    img -= img.min()\n    img /= img.max() + 1e-4\n    tar = CFG.label2name[np.argmax(tars[i].numpy())]\n    plt.imshow(img)\n    plt.title(f\"Target: {tar}\")\n    plt.axis('off')\n    \nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:23:35.662798Z","iopub.execute_input":"2024-04-04T17:23:35.663652Z","iopub.status.idle":"2024-04-04T17:23:38.587689Z","shell.execute_reply.started":"2024-04-04T17:23:35.663617Z","shell.execute_reply":"2024-04-04T17:23:38.586285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LOSS = keras.losses.KLDivergence()\n\n# Build Classifier\nmodel1 = keras_cv.models.ImageClassifier.from_preset(\n    CFG.preset, num_classes=400#CFG.num_classes\n)\n\nmodel2 = keras_cv.models.ImageClassifier.from_preset(\n    \"mobilenet_v3_large_imagenet\", num_classes=400#CFG.num_classes\n)\n\nmodel3 = keras_cv.models.ImageClassifier.from_preset(\n    \"efficientnetv2_b2_imagenet\", num_classes=400#CFG.num_classes\n)\n\n\nmodel1.layers[1].trainable = False\nmodel1.layers[2].trainable = False\n\n# Compile the model1  \nmodel1.compile(optimizer=keras.optimizers.Adam(learning_rate=1e-4),\n              loss=LOSS)\n\n# Model1 Sumamry\n#model1.summary()\n\n#ResNet Model without training \ndef ResNetModel():\n    model = tf.keras.Sequential()\n    \n    model.add(model1.layers[0])\n    model.add(model1.layers[1])\n    model.add(model1.layers[2])\n \n    return model\n\n#MobileNet Model without training \ndef MobileNetModel():\n    model = tf.keras.Sequential()\n    \n    model.add(model2.layers[0])\n    model.add(model2.layers[1])\n    model.add(model2.layers[2])\n\n \n    return model\n\n#EfficientNet Model without training \ndef EfficientNetModel():\n    model = tf.keras.Sequential()\n    \n    model.add(model3.layers[0])\n    model.add(model3.layers[1])\n    model.add(model3.layers[2])\n\n    print(model3.layers[0])\n    print(model3.layers[1])\n    print(model3.layers[2])\n \n    return model\n\n\ndef XGBFeedModel2():\n    model = tf.keras.Sequential()\n    \n    model.add(model1.layers[0])\n    model.add(model1.layers[1])\n    model.add(model1.layers[2])\n    \n    model.add(tf.keras.layers.Dense(100, activation=\"relu\"))\n    model.add(tf.keras.layers.Dropout(0.2))\n    model.add(tf.keras.layers.Dense(25, activation=\"relu\"))\n    model.add(tf.keras.layers.Dropout(0.2))\n    model.add(tf.keras.layers.Dense(6, activation=\"softmax\"))\n \n    return model\n\ndef XGBFeedModel3():\n    model = tf.keras.Sequential()\n    \n    model.add(model2.layers[0])\n    model.add(model2.layers[1])\n    model.add(model2.layers[2])\n    \n    model.add(tf.keras.layers.Dense(100, activation=\"relu\"))\n    model.add(tf.keras.layers.Dropout(0.2))\n    model.add(tf.keras.layers.Dense(25, activation=\"relu\"))\n    model.add(tf.keras.layers.Dropout(0.2))\n    model.add(tf.keras.layers.Dense(6, activation=\"softmax\"))\n \n    return model\n\n\n\ndef DynamicModel2():\n    model = tf.keras.Sequential()\n    \n    #model.add(tf.keras.layers.Input(shape=(400, 300, 3)))\n    #model.add(tf.keras.layers.Conv2D(3, (3,3), input_shape=(400, 300)))\n    #model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))\n    #model.add(tf.keras.layers.UpSampling2D(size=(2, 2)))\n    #model.add(tf.keras.layers.Conv2D(3, (3,3)))\n    #model.add(tf.keras.layers.Reshape(CFG.image_size))\n    model.add(model1.layers[0])\n    model.add(model1.layers[1])\n    model.add(model1.layers[2])\n    model.add(model1.layers[3])\n    model.add(tf.keras.layers.Dense(25, activation=\"relu\"))\n    model.add(tf.keras.layers.Dropout(0.2))\n    model.add(tf.keras.layers.Dense(10, activation=\"relu\"))\n    model.add(tf.keras.layers.Dropout(0.1))\n    model.add(tf.keras.layers.Dense(6, activation=\"softmax\"))\n    \n    #model.compile(optimizer=tf.keras.optimizers.RMSprop(), loss=LOSS,metrics=['accuracy'])\n    \n    return model\n\nfinal_model = DynamicModel2()\n\nfinal_model.layers[0].trainable = False\n#final_model.layers[1].trainable = False\n\n\n#final_model.summary()\n\n\n#final_model = keras.Sequential([model2, model1])\n#final_model = keras.Sequential([model2.layers[3].output, model1])\n\n# Freeze the model1\n#model1.trainable = False\n\n\nfinal_model.compile(optimizer=keras.optimizers.Adam(learning_rate=1e-4),\n              loss=LOSS)\n\nrfInputModel = XGBFeedModel2()\n\nrfInputModel.layers[0].trainable = False\nrfInputModel.layers[1].trainable = False\n\nprint(rfInputModel.layers[0])\nprint(rfInputModel.layers[1])\n\n\nrfInputModel2 = XGBFeedModel3()\n\nrfInputModel2.layers[0].trainable = False\nrfInputModel2.layers[1].trainable = False\n\nprint(rfInputModel2.layers[0])\nprint(rfInputModel2.layers[1])\n","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:23:51.146794Z","iopub.execute_input":"2024-04-04T17:23:51.147264Z","iopub.status.idle":"2024-04-04T17:24:09.245155Z","shell.execute_reply.started":"2024-04-04T17:23:51.147223Z","shell.execute_reply":"2024-04-04T17:24:09.244199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Get the first intermedaite output from the dataset using ResNet Model\n\nsvmTempFeed = EfficientNetModel()#MobileNetModel()\n\nseizure_lpd_out1 = svmTempFeed.predict(seizure_lpd_ds)\ngc.collect()\nseizure_gpd_out1 = svmTempFeed.predict(seizure_gpd_ds)\ngc.collect()\nseizure_lrda_out1 = svmTempFeed.predict(seizure_lrda_ds)\ngc.collect()\nseizure_grda_out1 = svmTempFeed.predict(seizure_grda_ds)\ngc.collect()\nseizure_other_out1 = svmTempFeed.predict(seizure_other_ds)\n\nlpd_gpd_out1 = svmTempFeed.predict(lpd_gpd_ds)\ngc.collect()\nlpd_lrda_out1 = svmTempFeed.predict(lpd_lrda_ds)\ngc.collect()\nlpd_grda_out1 = svmTempFeed.predict(lpd_grda_ds)\ngc.collect()\nlpd_other_out1 = svmTempFeed.predict(lpd_other_ds)\n\ngpd_lrda_out1 = svmTempFeed.predict(gpd_lrda_ds)\ngc.collect()\ngpd_grda_out1 = svmTempFeed.predict(gpd_grda_ds)\ngc.collect()\ngpd_other_out1 = svmTempFeed.predict(gpd_other_ds)\n\nlrda_grda_out1 = svmTempFeed.predict(lrda_grda_ds)\ngc.collect()\nlrda_other_out1 = svmTempFeed.predict(lrda_other_ds)\ngc.collect()\n\ngrda_other_out1 = svmTempFeed.predict(grda_other_ds)\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:24:16.533039Z","iopub.execute_input":"2024-04-04T17:24:16.534165Z","iopub.status.idle":"2024-04-04T17:29:07.442512Z","shell.execute_reply.started":"2024-04-04T17:24:16.534120Z","shell.execute_reply":"2024-04-04T17:29:07.441316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Train seperate SVM models for binary group pairs\n\nseizure_lpd_svm_model = svm.SVC(kernel=\"linear\", probability=True)\nseizure_gpd_svm_model = svm.SVC(kernel=\"linear\", probability=True)\nseizure_lrda_svm_model = svm.SVC(kernel=\"linear\", probability=True)\nseizure_grda_svm_model = svm.SVC(kernel=\"linear\", probability=True)\nseizure_other_svm_model = svm.SVC(kernel=\"linear\", probability=True)\n\nlpd_gpd_svm_model = svm.SVC(kernel=\"linear\", probability=True)\nlpd_lrda_svm_model = svm.SVC(kernel=\"linear\", probability=True)\nlpd_grda_svm_model = svm.SVC(kernel=\"linear\", probability=True)\nlpd_other_svm_model = svm.SVC(kernel=\"linear\", probability=True)\n\ngpd_lrda_svm_model = svm.SVC(kernel=\"linear\", probability=True)\ngpd_grda_svm_model = svm.SVC(kernel=\"linear\", probability=True)\ngpd_other_svm_model = svm.SVC(kernel=\"linear\", probability=True)\n\nlrda_grda_svm_model = svm.SVC(kernel=\"linear\", probability=True)\nlrda_other_svm_model = svm.SVC(kernel=\"linear\", probability=True)\n\ngrda_other_svm_model = svm.SVC(kernel=\"linear\", probability=True)\n\nseizure_lpd_svm_model.fit(seizure_lpd_out1, seizure_lpd_labels)\nseizure_gpd_svm_model.fit(seizure_gpd_out1, seizure_gpd_labels)\nseizure_lrda_svm_model.fit(seizure_lrda_out1, seizure_lrda_labels)\nseizure_grda_svm_model.fit(seizure_grda_out1, seizure_grda_labels)\nseizure_other_svm_model.fit(seizure_other_out1, seizure_other_labels)\n\nlpd_gpd_svm_model.fit(lpd_gpd_out1, lpd_gpd_labels)\nlpd_lrda_svm_model.fit(lpd_lrda_out1, lpd_lrda_labels)\nlpd_grda_svm_model.fit(lpd_grda_out1, lpd_grda_labels)\nlpd_other_svm_model.fit(lpd_other_out1, lpd_other_labels)\n\ngpd_lrda_svm_model.fit(gpd_lrda_out1, gpd_lrda_labels)\ngpd_grda_svm_model.fit(gpd_grda_out1, gpd_grda_labels)\ngpd_other_svm_model.fit(gpd_other_out1, gpd_other_labels)\n\nlrda_grda_svm_model.fit(lrda_grda_out1, lrda_grda_labels)\nlrda_other_svm_model.fit(lrda_other_out1, lrda_other_labels)\n\ngrda_other_svm_model.fit(grda_other_out1, grda_other_labels)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:30:39.323515Z","iopub.execute_input":"2024-04-04T17:30:39.324516Z","iopub.status.idle":"2024-04-04T17:37:53.707119Z","shell.execute_reply.started":"2024-04-04T17:30:39.324471Z","shell.execute_reply":"2024-04-04T17:37:53.706197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import math\n\ndef get_lr_callback(batch_size=8, mode='cos', epochs=10, plot=False):\n    lr_start, lr_max, lr_min = 5e-5, 6e-6 * batch_size, 1e-5\n    lr_ramp_ep, lr_sus_ep, lr_decay = 3, 0, 0.75\n\n    def lrfn(epoch):  # Learning rate update function\n        if epoch < lr_ramp_ep: lr = (lr_max - lr_start) / lr_ramp_ep * epoch + lr_start\n        elif epoch < lr_ramp_ep + lr_sus_ep: lr = lr_max\n        elif mode == 'exp': lr = (lr_max - lr_min) * lr_decay**(epoch - lr_ramp_ep - lr_sus_ep) + lr_min\n        elif mode == 'step': lr = lr_max * lr_decay**((epoch - lr_ramp_ep - lr_sus_ep) // 2)\n        elif mode == 'cos':\n            decay_total_epochs, decay_epoch_index = epochs - lr_ramp_ep - lr_sus_ep + 3, epoch - lr_ramp_ep - lr_sus_ep\n            phase = math.pi * decay_epoch_index / decay_total_epochs\n            lr = (lr_max - lr_min) * 0.5 * (1 + math.cos(phase)) + lr_min\n        return lr\n    \n    if plot:  # Plot lr curve if plot is True\n        plt.figure(figsize=(10, 5))\n        plt.plot(np.arange(epochs), [lrfn(epoch) for epoch in np.arange(epochs)], marker='o')\n        plt.xlabel('epoch'); plt.ylabel('lr')\n        plt.title('LR Scheduler')\n        plt.show()\n\n    return keras.callbacks.LearningRateScheduler(lrfn, verbose=False)  # Create lr callback\n\nlr_cb = get_lr_callback(CFG.batch_size, mode=CFG.lr_mode, plot=True)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:40:08.635458Z","iopub.execute_input":"2024-04-04T17:40:08.636310Z","iopub.status.idle":"2024-04-04T17:40:08.859499Z","shell.execute_reply.started":"2024-04-04T17:40:08.636275Z","shell.execute_reply":"2024-04-04T17:40:08.858625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ckpt_cb = keras.callbacks.ModelCheckpoint(\"best_model.keras\",\n                                         monitor='val_loss',\n                                         save_best_only=True,\n                                         save_weights_only=False,\n                                         mode='min')\n\n","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:40:13.309683Z","iopub.execute_input":"2024-04-04T17:40:13.310034Z","iopub.status.idle":"2024-04-04T17:40:13.314800Z","shell.execute_reply.started":"2024-04-04T17:40:13.310005Z","shell.execute_reply":"2024-04-04T17:40:13.313790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#svm_model = svm.SVC(kernel=\"poly\", probability=True)\n\n#svmTempFeed = ResNetModel()\n#svmInput = svmTempFeed.predict(rf_train_ds)\n\n\n#svm_model.fit(svmInput, train_labels)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T12:59:57.694528Z","iopub.status.idle":"2024-04-04T12:59:57.694902Z","shell.execute_reply.started":"2024-04-04T12:59:57.694710Z","shell.execute_reply":"2024-04-04T12:59:57.694726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out1 = svmTempFeed.predict(rf_train_ds)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:40:18.130372Z","iopub.execute_input":"2024-04-04T17:40:18.130733Z","iopub.status.idle":"2024-04-04T17:41:01.453894Z","shell.execute_reply.started":"2024-04-04T17:40:18.130706Z","shell.execute_reply":"2024-04-04T17:41:01.452946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out2_seizure_lpd = seizure_lpd_svm_model.predict_proba(out1)\nout2_seizure_gpd = seizure_gpd_svm_model.predict_proba(out1)\nout2_seizure_lrda = seizure_lrda_svm_model.predict_proba(out1)\nout2_seizure_grda = seizure_grda_svm_model.predict_proba(out1)\nout2_seizure_other = seizure_other_svm_model.predict_proba(out1)\n\nout2_lpd_gpd = lpd_gpd_svm_model.predict_proba(out1)\nout2_lpd_lrda = lpd_lrda_svm_model.predict_proba(out1)\nout2_lpd_grda = lpd_grda_svm_model.predict_proba(out1)\nout2_lpd_other  = lpd_other_svm_model.predict_proba(out1)\n\nout2_gpd_lrda = gpd_lrda_svm_model.predict_proba(out1)\nout2_gpd_grda = gpd_grda_svm_model.predict_proba(out1)\nout2_gpd_other  = gpd_other_svm_model.predict_proba(out1)\n\nout2_lrda_grda = lrda_grda_svm_model.predict_proba(out1)\nout2_lrda_other = lrda_other_svm_model.predict_proba(out1)\n\nout2_grda_other = grda_other_svm_model.predict_proba(out1)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:41:47.045522Z","iopub.execute_input":"2024-04-04T17:41:47.045871Z","iopub.status.idle":"2024-04-04T17:43:45.593590Z","shell.execute_reply.started":"2024-04-04T17:41:47.045845Z","shell.execute_reply":"2024-04-04T17:43:45.592732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"set1 = np.concatenate((out2_seizure_lpd, out2_seizure_gpd, out2_seizure_lrda, \n                out2_seizure_grda, out2_seizure_other), axis=1)\nset2 = np.concatenate((set1, out2_lpd_gpd, out2_lpd_lrda, out2_lpd_grda, out2_lpd_other), axis=1)\nset3 = np.concatenate((set2, out2_gpd_lrda, out2_gpd_grda, out2_gpd_other), axis=1)\nset4 = np.concatenate((set3, out2_lrda_grda, out2_lrda_other), axis=1)\nout3 = np.concatenate((set4, out2_grda_other), axis=1)\nprint(out3.shape)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:45:22.204218Z","iopub.execute_input":"2024-04-04T17:45:22.205157Z","iopub.status.idle":"2024-04-04T17:45:22.219252Z","shell.execute_reply.started":"2024-04-04T17:45:22.205115Z","shell.execute_reply":"2024-04-04T17:45:22.217879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#svm_final_model = svm.SVC(kernel=\"linear\", probability=True)\n#svm_final_model.fit(out3, train_labels)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T12:59:57.701765Z","iopub.status.idle":"2024-04-04T12:59:57.702081Z","shell.execute_reply.started":"2024-04-04T12:59:57.701922Z","shell.execute_reply":"2024-04-04T12:59:57.701935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf_model = RandomForestClassifier(n_estimators=275, max_depth=4, min_samples_split=40, \n                                  min_samples_leaf=1, max_leaf_nodes=None)\nrf_model.fit(out3, train_labels)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:45:26.153841Z","iopub.execute_input":"2024-04-04T17:45:26.154194Z","iopub.status.idle":"2024-04-04T17:45:30.835194Z","shell.execute_reply.started":"2024-04-04T17:45:26.154167Z","shell.execute_reply":"2024-04-04T17:45:30.834286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#Validate Set\n\n#Step 1\nsvmInputValid_out1 = svmTempFeed.predict(valid_ds)\n\n#Step 2\nvalid_out2_seizure_lpd = seizure_lpd_svm_model.predict_proba(svmInputValid_out1)\nvalid_out2_seizure_gpd = seizure_gpd_svm_model.predict_proba(svmInputValid_out1)\nvalid_out2_seizure_lrda = seizure_lrda_svm_model.predict_proba(svmInputValid_out1)\nvalid_out2_seizure_grda = seizure_grda_svm_model.predict_proba(svmInputValid_out1)\nvalid_out2_seizure_other = seizure_other_svm_model.predict_proba(svmInputValid_out1)\n\nvalid_out2_lpd_gpd = lpd_gpd_svm_model.predict_proba(svmInputValid_out1)\nvalid_out2_lpd_lrda = lpd_lrda_svm_model.predict_proba(svmInputValid_out1)\nvalid_out2_lpd_grda = lpd_grda_svm_model.predict_proba(svmInputValid_out1)\nvalid_out2_lpd_other  = lpd_other_svm_model.predict_proba(svmInputValid_out1)\n\nvalid_out2_gpd_lrda = gpd_lrda_svm_model.predict_proba(svmInputValid_out1)\nvalid_out2_gpd_grda = gpd_grda_svm_model.predict_proba(svmInputValid_out1)\nvalid_out2_gpd_other  = gpd_other_svm_model.predict_proba(svmInputValid_out1)\n\nvalid_out2_lrda_grda = lrda_grda_svm_model.predict_proba(svmInputValid_out1)\nvalid_out2_lrda_other = lrda_other_svm_model.predict_proba(svmInputValid_out1)\n\nvalid_out2_grda_other = grda_other_svm_model.predict_proba(svmInputValid_out1)\n\n#Step 3\nvalid_set1 = np.concatenate((valid_out2_seizure_lpd, valid_out2_seizure_gpd, \n                                         valid_out2_seizure_lrda, valid_out2_seizure_grda, valid_out2_seizure_other), axis=1)\nvalid_set2 = np.concatenate((valid_set1, valid_out2_lpd_gpd, valid_out2_lpd_lrda, valid_out2_lpd_grda, valid_out2_lpd_other), axis=1)\nvalid_set3 = np.concatenate((valid_set2, valid_out2_gpd_lrda, valid_out2_gpd_grda, valid_out2_gpd_other), axis=1)\nvalid_set4 = np.concatenate((valid_set3, valid_out2_lrda_grda, valid_out2_lrda_other), axis=1)\nvalid_out3 = np.concatenate((valid_set4, valid_out2_grda_other), axis=1)\n#print(valid_out3.shape)\n\n#Step 4\nrf_model.predict_proba(valid_out3)\n\naccuracy = rf_model.score(valid_out3, valid_labels)\n            \n#rf_results_table = np.append(rf_results_table, np.array([[curr_max_depth, curr_min_samp_split, n_est, (accuracy*100)]]), axis=0)\n            \nprint(\"Accuracy: %.2f%%\" % (accuracy * 100.0))","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:47:29.367546Z","iopub.execute_input":"2024-04-04T17:47:29.367913Z","iopub.status.idle":"2024-04-04T17:48:03.881479Z","shell.execute_reply.started":"2024-04-04T17:47:29.367885Z","shell.execute_reply":"2024-04-04T17:48:03.880450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#xgb_valid_ds = build_dataset(valid_paths, valid_offsets, batch_size=CFG.batch_size,repeat=False, shuffle=False, cache=False, augment=False)\n\n#xgbInputValid = xgboostInputModel.predict(xgb_valid_ds)\n#xgbInputValid2 = xgboostInputModel2.predict(xgb_valid_ds)\n\n#xgbInputValidFinal = np.concatenate((xgbInputValid, xgbInputValid2), 1)\n#print(xgbInputValidFinal.shape)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-04-04T12:59:57.707025Z","iopub.status.idle":"2024-04-04T12:59:57.707460Z","shell.execute_reply.started":"2024-04-04T12:59:57.707225Z","shell.execute_reply":"2024-04-04T12:59:57.707246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#svmInputValid = svmTempFeed.predict(xgb_valid_ds)\n\n#svm_model.predict_proba(svmInputValid)\n\n#accuracy = svm_model.score(svmInputValid, valid_labels)\n#print(\"Accuracy: %.2f%%\" % (accuracy * 100.0))","metadata":{"execution":{"iopub.status.busy":"2024-04-04T12:59:57.709005Z","iopub.status.idle":"2024-04-04T12:59:57.709448Z","shell.execute_reply.started":"2024-04-04T12:59:57.709233Z","shell.execute_reply":"2024-04-04T12:59:57.709256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#final_model.load_weights(\"best_model.keras\")\n\ntest_paths = test_df.spec2_path.values\ntest_ds = build_dataset(test_paths, batch_size=min(CFG.batch_size, len(test_df)),\n                         repeat=False, shuffle=False, cache=False, augment=False)\n\n#preds = final_model.predict(test_ds)\n#xgbInputTest = xgboostInputModel.predict(test_ds)\n#xgbInputTest2 = xgboostInputModel2.predict(test_ds)\n#xgbInputTestFinal = np.concatenate((xgbInputTest, xgbInputTest2), 1)\n\n#preds = xgboost_model.predict_proba(xgbInputTestFinal)\n\n#svmTestInput = svmTempFeed.predict(test_ds)\n#preds = svm_model.predict_proba(svmTestInput)\n\n\n#Validate Set\n\n#Step 1\nsvmInputTest_out1 = svmTempFeed.predict(test_ds)\n\n#Step 2\ntest_out2_seizure_lpd = seizure_lpd_svm_model.predict_proba(svmInputTest_out1)\ntest_out2_seizure_gpd = seizure_gpd_svm_model.predict_proba(svmInputTest_out1)\ntest_out2_seizure_lrda = seizure_lrda_svm_model.predict_proba(svmInputTest_out1)\ntest_out2_seizure_grda = seizure_grda_svm_model.predict_proba(svmInputTest_out1)\ntest_out2_seizure_other = seizure_other_svm_model.predict_proba(svmInputTest_out1)\n\ntest_out2_lpd_gpd = lpd_gpd_svm_model.predict_proba(svmInputTest_out1)\ntest_out2_lpd_lrda = lpd_lrda_svm_model.predict_proba(svmInputTest_out1)\ntest_out2_lpd_grda = lpd_grda_svm_model.predict_proba(svmInputTest_out1)\ntest_out2_lpd_other  = lpd_other_svm_model.predict_proba(svmInputTest_out1)\n\ntest_out2_gpd_lrda = gpd_lrda_svm_model.predict_proba(svmInputTest_out1)\ntest_out2_gpd_grda = gpd_grda_svm_model.predict_proba(svmInputTest_out1)\ntest_out2_gpd_other  = gpd_other_svm_model.predict_proba(svmInputTest_out1)\n\ntest_out2_lrda_grda = lrda_grda_svm_model.predict_proba(svmInputTest_out1)\ntest_out2_lrda_other = lrda_other_svm_model.predict_proba(svmInputTest_out1)\n\ntest_out2_grda_other = grda_other_svm_model.predict_proba(svmInputTest_out1)\n\n#Step 3\ntest_set1 = np.concatenate((test_out2_seizure_lpd, test_out2_seizure_gpd, test_out2_seizure_lrda, \n                test_out2_seizure_grda, test_out2_seizure_other), axis=1)\ntest_set2 = np.concatenate((test_set1, test_out2_lpd_gpd, test_out2_lpd_lrda, test_out2_lpd_grda, test_out2_lpd_other), axis=1)\ntest_set3 = np.concatenate((test_set2, test_out2_gpd_lrda, test_out2_gpd_grda, test_out2_gpd_other), axis=1)\ntest_set4 = np.concatenate((test_set3, test_out2_lrda_grda, test_out2_lrda_other), axis=1)\ntest_out3 = np.concatenate((test_set4, test_out2_grda_other), axis=1)\nprint(test_out3.shape)\n\n#Step 4\ntest_pred = rf_model.predict_proba(test_out3)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:48:17.551718Z","iopub.execute_input":"2024-04-04T17:48:17.552465Z","iopub.status.idle":"2024-04-04T17:48:17.712939Z","shell.execute_reply.started":"2024-04-04T17:48:17.552430Z","shell.execute_reply":"2024-04-04T17:48:17.711891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_df = test_df[[\"eeg_id\"]].copy()\ntarget_cols = [x.lower()+'_vote' for x in CFG.class_names]\npred_df[target_cols] = test_pred.tolist()\n\nsub_df = pd.read_csv(f'{BASE_PATH}/sample_submission.csv')\nsub_df = sub_df[[\"eeg_id\"]].copy()\nsub_df = sub_df.merge(pred_df, on=\"eeg_id\", how=\"left\")\nsub_df.to_csv(\"submission.csv\", index=False)\nsub_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:48:22.186091Z","iopub.execute_input":"2024-04-04T17:48:22.186760Z","iopub.status.idle":"2024-04-04T17:48:22.214237Z","shell.execute_reply.started":"2024-04-04T17:48:22.186724Z","shell.execute_reply":"2024-04-04T17:48:22.213374Z"},"trusted":true},"execution_count":null,"outputs":[]}]}