{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":59093,"databundleVersionId":7469972,"sourceType":"competition"},{"sourceId":10935084,"sourceType":"datasetVersion","datasetId":6799929},{"sourceId":6127,"sourceType":"modelInstanceVersion","modelInstanceId":4598,"modelId":2797}],"dockerImageVersionId":30918,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# **Create 2 Directories for train and test Spectrograms**","metadata":{}},{"cell_type":"code","source":"import os  # Import the OS module for working with file paths and directories\n\n# Define the base path for the dataset\nbase_path = \"/kaggle/input/hms-harmful-brain-activity-classification\"\n\n# Define the directory for storing spectrograms\nSPEC_DIR = \"/tmp/dataset/hms-hbac\"\n\n# Create directories for training and testing spectrograms if they don't exist\nos.makedirs(SPEC_DIR + '/train_spectrograms', exist_ok=True)\nos.makedirs(SPEC_DIR + '/test_spectrograms', exist_ok=True)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:11:18.310689Z","iopub.execute_input":"2025-03-06T19:11:18.310887Z","iopub.status.idle":"2025-03-06T19:11:18.315324Z","shell.execute_reply.started":"2025-03-06T19:11:18.310867Z","shell.execute_reply":"2025-03-06T19:11:18.314569Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Load train dataset**","metadata":{}},{"cell_type":"code","source":"import pandas as pd \ndf =  pd.read_csv(f\"{base_path}/train.csv\")\ndf.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:11:18.316038Z","iopub.execute_input":"2025-03-06T19:11:18.316294Z","iopub.status.idle":"2025-03-06T19:11:18.879320Z","shell.execute_reply.started":"2025-03-06T19:11:18.316262Z","shell.execute_reply":"2025-03-06T19:11:18.878259Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Create a Dictionary with labels**","metadata":{}},{"cell_type":"code","source":"df[\"expert_consensus\"].unique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:11:18.880305Z","iopub.execute_input":"2025-03-06T19:11:18.880626Z","iopub.status.idle":"2025-03-06T19:11:18.892751Z","shell.execute_reply.started":"2025-03-06T19:11:18.880595Z","shell.execute_reply":"2025-03-06T19:11:18.891947Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels = {\"Seizure\": 0 ,\n          \"GPD\" : 1 ,\n          \"LRDA\" : 2 ,\n          \"Other\" : 3 ,\n          \"GRDA\" : 4 ,\n          \"LPD\" : 5 }","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:11:18.893539Z","iopub.execute_input":"2025-03-06T19:11:18.893757Z","iopub.status.idle":"2025-03-06T19:11:18.902622Z","shell.execute_reply.started":"2025-03-06T19:11:18.893733Z","shell.execute_reply":"2025-03-06T19:11:18.901881Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **create new columns in train dataset**","metadata":{}},{"cell_type":"code","source":"# Create the EEG file path for each row in the DataFrame based on `eeg_id`\ndf[\"eeg_path\"] = f\"{base_path}/train_eegs/\" + df[\"eeg_id\"].astype(str) + \".parquet\"\n\n# Create the spectrogram file path for each row in the DataFrame based on `spectrogram_id`\ndf[\"spec_path\"] = f\"{base_path}/train_spectrograms/\" + df[\"spectrogram_id\"].astype(str) + \".parquet\"\n\n# Create an alternative spectrogram file path with .npy format for each row\ndf[\"spec_path2\"] = f\"{SPEC_DIR}/train_spectrograms/\" + df[\"spectrogram_id\"].astype(str) + \".npy\"\n\n# Copy the values from the `expert_consensus` column to `class_name`\ndf[\"class_name\"] = df[\"expert_consensus\"].copy()\n\n# Map class names to integer labels using the `labels` dictionary\ndf[\"class_label\"] = df.expert_consensus.map(labels).astype(\"int64\")\n\n# Display the first few rows of the DataFrame\ndf.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:11:18.904361Z","iopub.execute_input":"2025-03-06T19:11:18.904555Z","iopub.status.idle":"2025-03-06T19:11:19.104380Z","shell.execute_reply.started":"2025-03-06T19:11:18.904539Z","shell.execute_reply":"2025-03-06T19:11:19.103638Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **the same code for test set**","metadata":{}},{"cell_type":"markdown","source":"*the new column contain the paths of each file in addaddition to spec_path2 , you will ses letter the function of this column*","metadata":{}},{"cell_type":"code","source":"test_df = pd.read_csv(f\"{base_path}/test.csv\")\ntest_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:11:19.105572Z","iopub.execute_input":"2025-03-06T19:11:19.105841Z","iopub.status.idle":"2025-03-06T19:11:19.120076Z","shell.execute_reply.started":"2025-03-06T19:11:19.105820Z","shell.execute_reply":"2025-03-06T19:11:19.119212Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_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[\"spec_path2\"] = f\"{SPEC_DIR}/test_spectrograms/\"+test_df[\"spectrogram_id\"].astype(str)+\".npy\"\ntest_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:11:19.120923Z","iopub.execute_input":"2025-03-06T19:11:19.121228Z","iopub.status.idle":"2025-03-06T19:11:19.131368Z","shell.execute_reply.started":"2025-03-06T19:11:19.121198Z","shell.execute_reply":"2025-03-06T19:11:19.130554Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **create function process_spec to read Spectrogram parquet file and process it**","metadata":{}},{"cell_type":"code","source":"def process_spec(spec_id, split=\"train\"):\n    # Define the path to the spectrogram file (parquet format)\n    spec_path = f\"{base_path}/{split}_spectrograms/{spec_id}.parquet\"\n    \n    # Read the spectrogram data from the parquet file\n    spec = pd.read_parquet(spec_path)\n    \n    # Fill any missing values with 0, remove the first column, and transpose the matrix\n    spec = spec.fillna(0).values[:, 1:].T  \n    \n    # Convert the spectrogram data type to float32\n    spec = spec.astype(\"float32\")\n    \n    # Save the processed spectrogram as a .npy file in the specified directory\n    np.save(f\"{SPEC_DIR}/{split}_spectrograms/{spec_id}.npy\", spec)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:11:19.132138Z","iopub.execute_input":"2025-03-06T19:11:19.132413Z","iopub.status.idle":"2025-03-06T19:11:19.143071Z","shell.execute_reply.started":"2025-03-06T19:11:19.132393Z","shell.execute_reply":"2025-03-06T19:11:19.142421Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"*this fuction read the parquet file and with spectrogram values and transpose it and then convert the values to numpy format*","metadata":{}},{"cell_type":"markdown","source":"**the change from (Time:Frequency) to (Frequency:Time) can explain more how the Frequency change over time this can help us in our task to detect the patterns in the EEG signal**","metadata":{}},{"cell_type":"markdown","source":"# **apply the function and load the data**","metadata":{}},{"cell_type":"code","source":"import numpy as np\nfrom tqdm.notebook import tqdm  # Import tqdm for progress bar display in Jupyter Notebook\nimport joblib  # Import joblib for parallel processing\n\n# Get unique spectrogram IDs from the training DataFrame\nspec_ids = df[\"spectrogram_id\"].unique()\n\n# Process each spectrogram ID in parallel for the training dataset\n_ = joblib.Parallel(n_jobs=-1, backend=\"loky\")(\n    joblib.delayed(process_spec)(spec_id, \"train\")  # Call `process_spec` for each spectrogram ID\n    for spec_id in tqdm(spec_ids, total=len(spec_ids))  # Show progress bar\n)\n\n# Get unique spectrogram IDs from the test DataFrame\ntest_spec_ids = test_df[\"spectrogram_id\"].unique()\n\n# Process each spectrogram ID in parallel for the test dataset\n_ = joblib.Parallel(n_jobs=-1, backend=\"loky\")(\n    joblib.delayed(process_spec)(spec_id, \"test\")  # Call `process_spec` for each spectrogram ID\n    for spec_id in tqdm(test_spec_ids, total=len(test_spec_ids))  # Show progress bar\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:11:19.143751Z","iopub.execute_input":"2025-03-06T19:11:19.143971Z","iopub.status.idle":"2025-03-06T19:14:01.528790Z","shell.execute_reply.started":"2025-03-06T19:11:19.143951Z","shell.execute_reply":"2025-03-06T19:14:01.527957Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Data Augmentation**","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nimport keras_cv  # Import KerasCV for advanced augmentation techniques\n\ndef build_augmenter(dim=[400,300]):\n    # Define a list of augmentation layers\n    augmenters = [\n        keras_cv.layers.MixUp(alpha=2.0),  # MixUp augmentation to blend images\n        keras_cv.layers.RandomCutout(height_factor=(1.0, 1.0), \n                                     width_factor=(0.06, 0.1)),  # Horizontal cutout\n        keras_cv.layers.RandomCutout(height_factor=(0.06, 0.1), \n                                     width_factor=(1.0, 1.0)),  # Vertical cutout\n    ]\n    \n    def augment(img, label):\n        # Wrap image and label into a dictionary for augmentation layers\n        data = {\"images\": img, \"labels\": label}\n        \n        # Apply each augmentation with a 50% probability\n        for augmenter in augmenters:\n            if tf.random.uniform([]) < 0.5:\n                data = augmenter(data, training=True)\n        \n        # Return the augmented image and label\n        return data[\"images\"], data[\"labels\"]\n    \n    return augment","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:14:01.529985Z","iopub.execute_input":"2025-03-06T19:14:01.530323Z","iopub.status.idle":"2025-03-06T19:14:19.417414Z","shell.execute_reply.started":"2025-03-06T19:14:01.530286Z","shell.execute_reply":"2025-03-06T19:14:19.416766Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def build_decoder(with_labels=True, target_size=[400, 300], dtype=32):\n    def decode_signal(path, offset=None):\n        # Read the raw binary file\n        file_bytes = tf.io.read_file(path)\n        \n        # Decode the raw bytes into a float32 tensor\n        sig = tf.io.decode_raw(file_bytes, tf.float32)\n        \n        # Skip the first 1024/dtype elements (likely metadata or header)\n        sig = sig[1024 // dtype:]\n        \n        # Reshape the signal into a 2D array of shape [400, -1]\n        sig = tf.reshape(sig, [400, -1])\n\n        # Apply offset-based cropping if needed\n        if offset is not None:\n            offset = offset // 2  # Adjust offset\n            sig = sig[:, offset:offset + 300]  # Crop the signal\n            \n            # Calculate padding size if cropped signal is smaller than 300 columns\n            pad_size = tf.math.maximum(0, 300 - tf.shape(sig)[1])\n            \n            # Apply padding to maintain the shape [400, 300]\n            sig = tf.pad(sig, [[0, 0], [0, pad_size]])\n            sig = tf.reshape(sig, [400, 300])\n\n        # Clip values to avoid log(0) issues, using an exponential range\n        sig = tf.clip_by_value(sig, tf.math.exp(-4.0), tf.math.exp(8.0))\n        \n        # Apply logarithm to the signal values\n        sig = tf.math.log(sig)\n\n        # Normalize the signal (zero mean, unit variance)\n        sig -= tf.math.reduce_mean(sig)\n        sig /= tf.math.reduce_std(sig) + 1e-6  # Avoid division by zero\n\n        # Convert the signal into a 3-channel format (for CNN input compatibility)\n        sig = tf.tile(sig[..., None], [1, 1, 3])  \n\n        return sig\n\n    def decode_label(label):\n        # Convert label into a one-hot encoded vector with 6 classes\n        label = tf.one_hot(label, 6)\n        \n        # Cast label to float32\n        label = tf.cast(label, tf.float32)\n        \n        # Ensure the shape is [6]\n        label = tf.reshape(label, [6])\n        \n        return label\n\n    def decode_with_labels(path, offset=None, label=None):\n        # Decode signal and label together\n        sig = decode_signal(path, offset)\n        label = decode_label(label)\n        return sig, label\n\n    # Return the appropriate function based on `with_labels`\n    return decode_with_labels if with_labels else decode_signal\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:14:19.418151Z","iopub.execute_input":"2025-03-06T19:14:19.418611Z","iopub.status.idle":"2025-03-06T19:14:19.425950Z","shell.execute_reply.started":"2025-03-06T19:14:19.418587Z","shell.execute_reply":"2025-03-06T19:14:19.425182Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Data Builder**","metadata":{}},{"cell_type":"code","source":"def 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    # Create cache directory if caching is enabled\n    if cache_dir != \"\" and cache is True:\n        os.makedirs(cache_dir, exist_ok=True)\n    \n    # If no decoding function is provided, use the default decoder\n    if decode_fn is None:\n        decode_fn = build_decoder(labels is not None)\n    \n    # If no augmentation function is provided, use the default augmenter\n    if augment_fn is None:\n        augment_fn = build_augmenter()\n    \n    AUTO = tf.data.experimental.AUTOTUNE  # Optimize performance with auto-tuning\n\n    # Create dataset slices: (paths, offsets) for unsupervised, (paths, offsets, labels) for supervised\n    slices = (paths, offsets) if labels is None else (paths, offsets, labels)\n    \n    # Load data from tensor slices\n    ds = tf.data.Dataset.from_tensor_slices(slices)\n    \n    # Decode the signals and labels\n    ds = ds.map(decode_fn, num_parallel_calls=AUTO)\n    \n    # Cache dataset if enabled\n    ds = ds.cache(cache_dir) if cache else ds\n    \n    # Repeat dataset indefinitely if repeat is True\n    ds = ds.repeat() if repeat else ds\n    \n    # Shuffle dataset if shuffle is enabled\n    if shuffle: \n        ds = ds.shuffle(shuffle, seed=42)  # Ensure reproducibility\n        opt = tf.data.Options()\n        opt.experimental_deterministic = False  # Improve performance\n        ds = ds.with_options(opt)\n    \n    # Batch the dataset\n    ds = ds.batch(batch_size, drop_remainder=drop_remainder)\n    \n    # Apply data augmentation if enabled\n    ds = ds.map(augment_fn, num_parallel_calls=AUTO) if augment else ds\n    \n    # Prefetch data for better performance\n    ds = ds.prefetch(AUTO)\n    \n    return ds","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:14:19.426883Z","iopub.execute_input":"2025-03-06T19:14:19.427200Z","iopub.status.idle":"2025-03-06T19:14:19.450790Z","shell.execute_reply.started":"2025-03-06T19:14:19.427169Z","shell.execute_reply":"2025-03-06T19:14:19.449977Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Create Folds form Data**","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import StratifiedGroupKFold\n\n# Initialize StratifiedGroupKFold with 5 splits, shuffling enabled, and a fixed random state for reproducibility\nsgkf = StratifiedGroupKFold(n_splits=5, shuffle=True, random_state=42)\n\n# Initialize a new column in the dataframe to store fold assignments\ndf[\"fold\"] = -1\n\n# Reset index to ensure proper indexing before splitting\ndf.reset_index(drop=True, inplace=True)\n\n# Perform stratified group k-fold splitting\nfor fold, (train_idx, valid_idx) in enumerate(\n    sgkf.split(df, y=df[\"class_label\"], groups=df[\"patient_id\"])\n):\n    # Assign the fold number to the validation set\n    df.loc[valid_idx, \"fold\"] = fold\n\n# Display the count of EEG samples per fold and class\ndf.groupby([\"fold\", \"class_name\"])[[\"eeg_id\"]].count().T","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:14:19.451585Z","iopub.execute_input":"2025-03-06T19:14:19.451815Z","iopub.status.idle":"2025-03-06T19:14:20.646089Z","shell.execute_reply.started":"2025-03-06T19:14:19.451796Z","shell.execute_reply":"2025-03-06T19:14:20.645388Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_df = df.groupby(\"spectrogram_id\").head(1).reset_index(drop=True)\nsample_df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:14:20.649241Z","iopub.execute_input":"2025-03-06T19:14:20.649722Z","iopub.status.idle":"2025-03-06T19:14:20.683563Z","shell.execute_reply.started":"2025-03-06T19:14:20.649696Z","shell.execute_reply":"2025-03-06T19:14:20.682879Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(sample_df[\"spectrogram_id\"].unique())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:14:20.684978Z","iopub.execute_input":"2025-03-06T19:14:20.685192Z","iopub.status.idle":"2025-03-06T19:14:20.690418Z","shell.execute_reply.started":"2025-03-06T19:14:20.685173Z","shell.execute_reply":"2025-03-06T19:14:20.689738Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **split the data**","metadata":{}},{"cell_type":"code","source":"# Split the dataset into training and validation sets based on the assigned fold values\ntrain_df = sample_df[sample_df.fold != 0]  # Use all folds except fold 0 for training\nvalid_df = sample_df[sample_df.fold == 0]  # Use fold 0 as the validation set\n\n# Print the number of samples in the training and validation sets\nprint(f\"# Num Train: {len(train_df)} | Num Valid: {len(valid_df)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:14:20.691273Z","iopub.execute_input":"2025-03-06T19:14:20.691588Z","iopub.status.idle":"2025-03-06T19:14:20.702852Z","shell.execute_reply.started":"2025-03-06T19:14:20.691557Z","shell.execute_reply":"2025-03-06T19:14:20.702071Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:14:20.703719Z","iopub.execute_input":"2025-03-06T19:14:20.704007Z","iopub.status.idle":"2025-03-06T19:14:20.723095Z","shell.execute_reply.started":"2025-03-06T19:14:20.703978Z","shell.execute_reply":"2025-03-06T19:14:20.722301Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Builde the Dataset**","metadata":{}},{"cell_type":"code","source":"# Extract paths, offsets, and labels for the training dataset\ntrain_paths = train_df[\"spec_path2\"].values  # File paths to the spectrograms\ntrain_offsets = train_df[\"spectrogram_label_offset_seconds\"].values.astype(int)  # Time offsets for labels\ntrain_labels = train_df[\"class_label\"].values  # Class labels\n\n# Build the training dataset using the custom function\ntrain_ds = build_dataset(\n    train_paths,           # Paths to spectrogram files\n    train_offsets,         # Time offsets for segmentation\n    train_labels,          # Class labels\n    batch_size=64,         # Number of samples per batch\n    repeat=True,           # Repeat dataset for continuous training\n    shuffle=True,          # Shuffle the dataset to improve generalization\n    augment=True,          # Apply data augmentation\n    cache=True             # Cache data for faster access\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:14:20.723892Z","iopub.execute_input":"2025-03-06T19:14:20.724196Z","iopub.status.idle":"2025-03-06T19:14:23.682455Z","shell.execute_reply.started":"2025-03-06T19:14:20.724156Z","shell.execute_reply":"2025-03-06T19:14:23.681729Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_ds","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:14:23.683357Z","iopub.execute_input":"2025-03-06T19:14:23.683706Z","iopub.status.idle":"2025-03-06T19:14:23.688499Z","shell.execute_reply.started":"2025-03-06T19:14:23.683675Z","shell.execute_reply":"2025-03-06T19:14:23.687733Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"valid_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:14:23.689345Z","iopub.execute_input":"2025-03-06T19:14:23.689620Z","iopub.status.idle":"2025-03-06T19:14:23.712313Z","shell.execute_reply.started":"2025-03-06T19:14:23.689599Z","shell.execute_reply":"2025-03-06T19:14:23.711623Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **same code for Validation**","metadata":{}},{"cell_type":"code","source":"valid_paths = valid_df[\"spec_path2\"].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=64,\n                         repeat=False, shuffle=False, augment=False, cache=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:14:23.713206Z","iopub.execute_input":"2025-03-06T19:14:23.713515Z","iopub.status.idle":"2025-03-06T19:14:23.774616Z","shell.execute_reply.started":"2025-03-06T19:14:23.713487Z","shell.execute_reply":"2025-03-06T19:14:23.774039Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"valid_ds","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:14:23.775471Z","iopub.execute_input":"2025-03-06T19:14:23.775774Z","iopub.status.idle":"2025-03-06T19:14:23.780641Z","shell.execute_reply.started":"2025-03-06T19:14:23.775744Z","shell.execute_reply":"2025-03-06T19:14:23.779695Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **use efficientnetv2_b2 to train the model**","metadata":{}},{"cell_type":"code","source":"model = keras_cv.models.ImageClassifier.from_preset(\n    \"efficientnetv2_b2_imagenet\", num_classes=6\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:14:23.781544Z","iopub.execute_input":"2025-03-06T19:14:23.781833Z","iopub.status.idle":"2025-03-06T19:14:30.620821Z","shell.execute_reply.started":"2025-03-06T19:14:23.781803Z","shell.execute_reply":"2025-03-06T19:14:30.620143Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **model compailation**","metadata":{}},{"cell_type":"code","source":"model.compile(\n    optimizer=\"adam\",\n    loss = \"categorical_crossentropy\",\n    metrics = [\"accuracy\"]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:14:30.621589Z","iopub.execute_input":"2025-03-06T19:14:30.621836Z","iopub.status.idle":"2025-03-06T19:14:30.632587Z","shell.execute_reply.started":"2025-03-06T19:14:30.621814Z","shell.execute_reply":"2025-03-06T19:14:30.632042Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:14:30.633228Z","iopub.execute_input":"2025-03-06T19:14:30.633402Z","iopub.status.idle":"2025-03-06T19:14:30.889116Z","shell.execute_reply.started":"2025-03-06T19:14:30.633386Z","shell.execute_reply":"2025-03-06T19:14:30.888351Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping\nearly_stopping = EarlyStopping(monitor='val_loss', patience=4)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:14:30.890127Z","iopub.execute_input":"2025-03-06T19:14:30.890463Z","iopub.status.idle":"2025-03-06T19:14:30.894814Z","shell.execute_reply.started":"2025-03-06T19:14:30.890431Z","shell.execute_reply":"2025-03-06T19:14:30.894118Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **fit the model**","metadata":{}},{"cell_type":"code","source":"history = model.fit(\n    train_ds, \n    epochs=6,\n    callbacks=early_stopping, \n    steps_per_epoch=len(train_df)//64,\n    validation_data=valid_ds\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:14:30.895469Z","iopub.execute_input":"2025-03-06T19:14:30.895690Z","iopub.status.idle":"2025-03-06T19:28:25.715756Z","shell.execute_reply.started":"2025-03-06T19:14:30.895671Z","shell.execute_reply":"2025-03-06T19:28:25.714863Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt \n\nplt.plot(history.history[\"accuracy\"],label=\"Train Accuracy\")\nplt.plot(history.history[\"val_accuracy\"],label=\"Validation Accuracy\")\nplt.legend()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:28:25.717975Z","iopub.execute_input":"2025-03-06T19:28:25.718250Z","iopub.status.idle":"2025-03-06T19:28:25.946112Z","shell.execute_reply.started":"2025-03-06T19:28:25.718215Z","shell.execute_reply":"2025-03-06T19:28:25.945332Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **function to prediction**","metadata":{}},{"cell_type":"code","source":"def prediction(arr):\n    # Generate predictions using the trained model\n    pred = model.predict(arr)\n    \n    # Find the class label with the highest probability\n    for k, v in labels.items():\n        if np.argmax(pred) == v:\n            print(k)  # Print the corresponding class name\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:28:25.946819Z","iopub.execute_input":"2025-03-06T19:28:25.947118Z","iopub.status.idle":"2025-03-06T19:28:25.951217Z","shell.execute_reply.started":"2025-03-06T19:28:25.947093Z","shell.execute_reply":"2025-03-06T19:28:25.950379Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"random_arr = np.random.rand(1,300,300,3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:28:25.952050Z","iopub.execute_input":"2025-03-06T19:28:25.952297Z","iopub.status.idle":"2025-03-06T19:28:25.967575Z","shell.execute_reply.started":"2025-03-06T19:28:25.952273Z","shell.execute_reply":"2025-03-06T19:28:25.966719Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"prediction(random_arr)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:28:25.968476Z","iopub.execute_input":"2025-03-06T19:28:25.968723Z","iopub.status.idle":"2025-03-06T19:28:34.612962Z","shell.execute_reply.started":"2025-03-06T19:28:25.968694Z","shell.execute_reply":"2025-03-06T19:28:34.612217Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **predict the test set**","metadata":{}},{"cell_type":"code","source":"test_paths = test_df[\"spec_path2\"].values\ntest_ds = build_dataset(test_paths, batch_size=min(64, len(test_df)),\n                         repeat=False, shuffle=False, cache=False, augment=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:28:34.614117Z","iopub.execute_input":"2025-03-06T19:28:34.614468Z","iopub.status.idle":"2025-03-06T19:28:34.654350Z","shell.execute_reply.started":"2025-03-06T19:28:34.614443Z","shell.execute_reply":"2025-03-06T19:28:34.653724Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_ds","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:28:34.655130Z","iopub.execute_input":"2025-03-06T19:28:34.655457Z","iopub.status.idle":"2025-03-06T19:28:34.660421Z","shell.execute_reply.started":"2025-03-06T19:28:34.655425Z","shell.execute_reply":"2025-03-06T19:28:34.659607Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"prediction(test_ds)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T19:28:34.661172Z","iopub.execute_input":"2025-03-06T19:28:34.661392Z","iopub.status.idle":"2025-03-06T19:28:42.953635Z","shell.execute_reply.started":"2025-03-06T19:28:34.661374Z","shell.execute_reply":"2025-03-06T19:28:42.952700Z"}},"outputs":[],"execution_count":null}]}