{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":71549,"databundleVersionId":8561470,"sourceType":"competition"}],"dockerImageVersionId":30761,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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 gc\nimport sys\nfrom PIL import Image\nimport cv2\nimport math, random\nimport numpy as np\nimport pandas as pd\nimport glob\nfrom glob import glob\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import KFold\nfrom collections import OrderedDict\nimport tensorflow as tf\nfrom tensorflow.keras import layers, models, optimizers\nfrom tensorflow.keras.models import load_model\nimport albumentations as A\nfrom sklearn.model_selection import KFold\nimport re\nimport pydicom\nfrom typing import Optional\nimport torch\nfrom torch.utils.data import Dataset, DataLoader  # Import Dataset here\nimport torch.nn as nn\nimport torch.optim as optim\nimport glob\nimport torch\nfrom torch.utils.data import DataLoader, Dataset\nfrom tqdm import tqdm\nimport timm\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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-10-02T13:38:27.793333Z","iopub.execute_input":"2024-10-02T13:38:27.793852Z","iopub.status.idle":"2024-10-02T13:38:53.311284Z","shell.execute_reply.started":"2024-10-02T13:38:27.793784Z","shell.execute_reply":"2024-10-02T13:38:53.310014Z"},"trusted":true},"execution_count":1,"outputs":[{"name":"stderr","text":"/opt/conda/lib/python3.10/site-packages/albumentations/__init__.py:13: UserWarning: A new version of Albumentations is available: 1.4.17 (you have 1.4.14). Upgrade using: pip install -U albumentations. To disable automatic update checks, set the environment variable NO_ALBUMENTATIONS_UPDATE to 1.\n  check_for_updates()\n","output_type":"stream"}]},{"cell_type":"code","source":"import os\nimport numpy as np\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\nfrom sklearn.model_selection import train_test_split\n\n# Directory where your 2D images are stored","metadata":{"execution":{"iopub.status.busy":"2024-10-02T13:38:53.313648Z","iopub.execute_input":"2024-10-02T13:38:53.314149Z","iopub.status.idle":"2024-10-02T13:38:53.3274Z","shell.execute_reply.started":"2024-10-02T13:38:53.314092Z","shell.execute_reply":"2024-10-02T13:38:53.326073Z"},"trusted":true},"execution_count":2,"outputs":[]},{"cell_type":"code","source":"# import pandas as pd\n\n# # Load the data\n# df = pd.read_csv('/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_series_descriptions.csv')\n# train = pd.read_csv('/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train.csv')\n\n# # Columns to check for 'Moderate' or 'Severe' conditions\n# columns_to_check = train.columns[1:6]\n\n# # Create a condition based on 'Moderate' or 'Severe'\n# condition = train[columns_to_check].apply(lambda row: any(val == 'Moderate' or val == 'Severe' for val in row), axis=1)\n\n# # Filter the train DataFrame\n# df_train = train[condition]\n\n# # Filter the df DataFrame to include only study_id values that are present in df_train\n# df_filtered = df[df['study_id'].isin(df_train['study_id'])]\n\n# # Display the filtered DataFrame\n# df_filtered","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Define the order for series_description\n# order = [\"Axial T2\", \"Sagittal T1\", \"Sagittal T2/STIR\"]\n\n# # Create a categorical type for series_description based on the order\n# df_filtered['series_description'] = pd.Categorical(df_filtered['series_description'], categories=order, ordered=True)\n\n# # Sort the DataFrame by study_id and series_description to ensure correct order\n# df_filtered = df_filtered.sort_values(by=['study_id', 'series_description'])\n\n# # For each study_id, drop extra duplicates, keeping the first occurrence for each series_description\n# df_filtered = df_filtered.drop_duplicates(subset=['study_id', 'series_description'], keep='first')\n\n# # Reset the index if needed\n# df_filtered.reset_index(drop=True, inplace=True)\n\n# # Display the rearranged DataFrame\n# df_filtered\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\n# # Load the data\n# df = pd.read_csv('/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_series_descriptions.csv')\n# train = pd.read_csv('/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train.csv')\n\n# # Columns to check for 'Moderate' or 'Severe' conditions\n# columns_to_check = train.columns[1:6]\n\n# # Create a condition based on 'Moderate' or 'Severe'\n# condition = train[columns_to_check].apply(lambda row: any(val == 'Moderate' or val == 'Severe' for val in row), axis=1)\n\n# # Filter the train DataFrame\n# df_train = train[condition]\n\n# # Filter the df DataFrame to include only study_id values that are present in df_train\n# df_filtered = df[df['study_id'].isin(df_train['study_id'])]\n\n# # Define the order for series_description\n# order = [\"Axial T2\", \"Sagittal T1\", \"Sagittal T2/STIR\"]\n\n# # Create a categorical type for series_description based on the order\n# df_filtered['series_description'] = pd.Categorical(df_filtered['series_description'], categories=order, ordered=True)\n\n# # Sort the DataFrame by study_id and series_description to ensure correct order\n# df_filtered = df_filtered.sort_values(by=['study_id', 'series_description'])\n\n# # For each study_id, drop extra duplicates, keeping the first occurrence for each series_description\n# df_filtered = df_filtered.drop_duplicates(subset=['study_id', 'series_description'], keep='first')\n\n# # Reset the index if needed\n# df_filtered.reset_index(drop=True, inplace=True)\n\n# # Ensure study_id in df_train is in the same order as in df_filtered\n# ordered_study_ids = df_filtered['study_id'].unique()\n# df_train = df_train[df_train['study_id'].isin(ordered_study_ids)]\n# df_train = df_train.set_index('study_id').reindex(ordered_study_ids).reset_index()\n\n# # Display the rearranged DataFrame\n# df_filtered, df_train\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\n# Load the data\ndf = pd.read_csv('/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_series_descriptions.csv')\ntrain = pd.read_csv('/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train.csv')\n\n# Columns to check for 'Moderate' or 'Severe' conditions\ncolumns_to_check = train.columns[1:6]\n\n# Create a condition based on 'Moderate' or 'Severe'\ncondition = train[columns_to_check].apply(lambda row: any(val == 'Moderate' or val == 'Severe' for val in row), axis=1)\n\n# Filter the train DataFrame\ndf_train = train[condition]\n\n# Select the first 30 study_id from df_train\nfirst_30_study_ids = df_train['study_id'].unique()[:540]\n\n# Filter df_train to include only the first 30 study_id\ndf_train = df_train[df_train['study_id'].isin(first_30_study_ids)]\n\n# Filter df DataFrame to include only study_id values that are present in df_train\ndf_filtered = df[df['study_id'].isin(first_30_study_ids)]\n\n# Define the order for series_description\norder = [\"Axial T2\", \"Sagittal T1\", \"Sagittal T2/STIR\"]\n\n# Create a categorical type for series_description based on the order\ndf_filtered['series_description'] = pd.Categorical(df_filtered['series_description'], categories=order, ordered=True)\n\n# Sort the DataFrame by study_id and series_description to ensure correct order\ndf_filtered = df_filtered.sort_values(by=['study_id', 'series_description'])\n\n# For each study_id, drop extra duplicates, keeping the first occurrence for each series_description\ndf_filtered = df_filtered.drop_duplicates(subset=['study_id', 'series_description'], keep='first')\n\n# Reset the index if needed\ndf_filtered.reset_index(drop=True, inplace=True)\n\n# Ensure study_id in df_train is in the same order as in df_filtered\nordered_study_ids = df_filtered['study_id'].unique()\ndf_train = df_train[df_train['study_id'].isin(ordered_study_ids)]\ndf_train = df_train.set_index('study_id').reindex(ordered_study_ids).reset_index()\n","metadata":{"execution":{"iopub.status.busy":"2024-10-02T13:39:05.014601Z","iopub.execute_input":"2024-10-02T13:39:05.015103Z","iopub.status.idle":"2024-10-02T13:39:05.161511Z","shell.execute_reply.started":"2024-10-02T13:39:05.015054Z","shell.execute_reply":"2024-10-02T13:39:05.16017Z"},"trusted":true},"execution_count":3,"outputs":[{"name":"stderr","text":"/tmp/ipykernel_36/2173964938.py:29: SettingWithCopyWarning: \nA value is trying to be set on a copy of a slice from a DataFrame.\nTry using .loc[row_indexer,col_indexer] = value instead\n\nSee the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n  df_filtered['series_description'] = pd.Categorical(df_filtered['series_description'], categories=order, ordered=True)\n","output_type":"stream"}]},{"cell_type":"code","source":"# import pandas as pd\n\n# # Load the data\n# df = pd.read_csv('/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_series_descriptions.csv')\n# train = pd.read_csv('/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train.csv')\n\n# # Columns to check for 'Moderate' or 'Severe' conditions\n# columns_to_check = train.columns[1:6]\n\n# # Create a condition based on 'Moderate' or 'Severe'\n# condition = train[columns_to_check].apply(lambda row: any(val == 'Moderate' or val == 'Severe' for val in row), axis=1)\n\n# # Filter the train DataFrame based on the condition\n# df_train = train[condition]\n\n# # Select study_id from the range 180:270 (90 study_ids)\n# study_ids_180_270 = df_train['study_id'].unique()[180:270]\n\n# # Filter df_train to include only the study_ids in the range 180:270\n# df_train = df_train[df_train['study_id'].isin(study_ids_180_270)]\n\n# # Filter df DataFrame to include only study_id values that are present in df_train\n# df_filtered = df[df['study_id'].isin(study_ids_180_270)]\n\n# # Define the order for series_description\n# order = [\"Axial T2\", \"Sagittal T1\", \"Sagittal T2/STIR\"]\n\n# # Create a categorical type for series_description based on the order\n# df_filtered['series_description'] = pd.Categorical(df_filtered['series_description'], categories=order, ordered=True)\n\n# # Sort the DataFrame by study_id and series_description to ensure correct order\n# df_filtered = df_filtered.sort_values(by=['study_id', 'series_description'])\n\n# # For each study_id, drop extra duplicates, keeping the first occurrence for each series_description\n# df_filtered = df_filtered.drop_duplicates(subset=['study_id', 'series_description'], keep='first')\n\n# # Reset the index if needed\n# df_filtered.reset_index(drop=True, inplace=True)\n\n# # Ensure study_id in df_train is in the same order as in df_filtered\n# ordered_study_ids = df_filtered['study_id'].unique()\n# df_train = df_train[df_train['study_id'].isin(ordered_study_ids)]\n# df_train = df_train.set_index('study_id').reindex(ordered_study_ids).reset_index()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train","metadata":{"execution":{"iopub.status.busy":"2024-10-02T13:39:14.067603Z","iopub.execute_input":"2024-10-02T13:39:14.068818Z","iopub.status.idle":"2024-10-02T13:39:14.109819Z","shell.execute_reply.started":"2024-10-02T13:39:14.068762Z","shell.execute_reply":"2024-10-02T13:39:14.108416Z"},"trusted":true},"execution_count":4,"outputs":[{"execution_count":4,"output_type":"execute_result","data":{"text/plain":"       study_id spinal_canal_stenosis_l1_l2 spinal_canal_stenosis_l2_l3  \\\n0       4646740                 Normal/Mild                 Normal/Mild   \n1      22191399                 Normal/Mild                 Normal/Mild   \n2      29931867                 Normal/Mild                    Moderate   \n3      38281420                 Normal/Mild                 Normal/Mild   \n4      40745534                 Normal/Mild                 Normal/Mild   \n..          ...                         ...                         ...   \n535  3158185203                 Normal/Mild                    Moderate   \n536  3160528641                 Normal/Mild                 Normal/Mild   \n537  3163594538                      Severe                      Severe   \n538  3168755174                 Normal/Mild                 Normal/Mild   \n539  3172703788                    Moderate                 Normal/Mild   \n\n    spinal_canal_stenosis_l3_l4 spinal_canal_stenosis_l4_l5  \\\n0                      Moderate                      Severe   \n1                   Normal/Mild                      Severe   \n2                      Moderate                 Normal/Mild   \n3                   Normal/Mild                    Moderate   \n4                      Moderate                      Severe   \n..                          ...                         ...   \n535                 Normal/Mild                 Normal/Mild   \n536                 Normal/Mild                    Moderate   \n537                      Severe                      Severe   \n538                      Severe                 Normal/Mild   \n539                 Normal/Mild                 Normal/Mild   \n\n    spinal_canal_stenosis_l5_s1 left_neural_foraminal_narrowing_l1_l2  \\\n0                   Normal/Mild                           Normal/Mild   \n1                   Normal/Mild                           Normal/Mild   \n2                   Normal/Mild                           Normal/Mild   \n3                   Normal/Mild                           Normal/Mild   \n4                   Normal/Mild                           Normal/Mild   \n..                          ...                                   ...   \n535                 Normal/Mild                           Normal/Mild   \n536                 Normal/Mild                           Normal/Mild   \n537                 Normal/Mild                              Moderate   \n538                 Normal/Mild                           Normal/Mild   \n539                 Normal/Mild                           Normal/Mild   \n\n    left_neural_foraminal_narrowing_l2_l3  \\\n0                             Normal/Mild   \n1                             Normal/Mild   \n2                             Normal/Mild   \n3                             Normal/Mild   \n4                             Normal/Mild   \n..                                    ...   \n535                              Moderate   \n536                           Normal/Mild   \n537                              Moderate   \n538                           Normal/Mild   \n539                           Normal/Mild   \n\n    left_neural_foraminal_narrowing_l3_l4  \\\n0                             Normal/Mild   \n1                             Normal/Mild   \n2                             Normal/Mild   \n3                             Normal/Mild   \n4                             Normal/Mild   \n..                                    ...   \n535                              Moderate   \n536                              Moderate   \n537                              Moderate   \n538                           Normal/Mild   \n539                           Normal/Mild   \n\n    left_neural_foraminal_narrowing_l4_l5  ...  \\\n0                                Moderate  ...   \n1                             Normal/Mild  ...   \n2                                Moderate  ...   \n3                                Moderate  ...   \n4                                Moderate  ...   \n..                                    ...  ...   \n535                                Severe  ...   \n536                              Moderate  ...   \n537                              Moderate  ...   \n538                              Moderate  ...   \n539                           Normal/Mild  ...   \n\n    left_subarticular_stenosis_l1_l2 left_subarticular_stenosis_l2_l3  \\\n0                        Normal/Mild                      Normal/Mild   \n1                        Normal/Mild                      Normal/Mild   \n2                        Normal/Mild                      Normal/Mild   \n3                        Normal/Mild                      Normal/Mild   \n4                        Normal/Mild                      Normal/Mild   \n..                               ...                              ...   \n535                      Normal/Mild                         Moderate   \n536                      Normal/Mild                      Normal/Mild   \n537                           Severe                           Severe   \n538                              NaN                              NaN   \n539                      Normal/Mild                      Normal/Mild   \n\n    left_subarticular_stenosis_l3_l4 left_subarticular_stenosis_l4_l5  \\\n0                        Normal/Mild                           Severe   \n1                        Normal/Mild                         Moderate   \n2                           Moderate                         Moderate   \n3                        Normal/Mild                      Normal/Mild   \n4                           Moderate                           Severe   \n..                               ...                              ...   \n535                         Moderate                           Severe   \n536                         Moderate                         Moderate   \n537                           Severe                           Severe   \n538                           Severe                      Normal/Mild   \n539                      Normal/Mild                      Normal/Mild   \n\n    left_subarticular_stenosis_l5_s1 right_subarticular_stenosis_l1_l2  \\\n0                        Normal/Mild                       Normal/Mild   \n1                           Moderate                       Normal/Mild   \n2                           Moderate                          Moderate   \n3                        Normal/Mild                       Normal/Mild   \n4                           Moderate                       Normal/Mild   \n..                               ...                               ...   \n535                      Normal/Mild                       Normal/Mild   \n536                      Normal/Mild                       Normal/Mild   \n537                      Normal/Mild                          Moderate   \n538                      Normal/Mild                               NaN   \n539                      Normal/Mild                       Normal/Mild   \n\n    right_subarticular_stenosis_l2_l3 right_subarticular_stenosis_l3_l4  \\\n0                            Moderate                          Moderate   \n1                         Normal/Mild                       Normal/Mild   \n2                              Severe                            Severe   \n3                            Moderate                          Moderate   \n4                         Normal/Mild                          Moderate   \n..                                ...                               ...   \n535                          Moderate                       Normal/Mild   \n536                       Normal/Mild                       Normal/Mild   \n537                            Severe                            Severe   \n538                               NaN                            Severe   \n539                       Normal/Mild                       Normal/Mild   \n\n    right_subarticular_stenosis_l4_l5 right_subarticular_stenosis_l5_s1  \n0                            Moderate                       Normal/Mild  \n1                              Severe                          Moderate  \n2                            Moderate                            Severe  \n3                              Severe                            Severe  \n4                              Severe                       Normal/Mild  \n..                                ...                               ...  \n535                          Moderate                       Normal/Mild  \n536                            Severe                       Normal/Mild  \n537                            Severe                          Moderate  \n538                            Severe                            Severe  \n539                          Moderate                          Moderate  \n\n[540 rows x 26 columns]","text/html":"<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>study_id</th>\n      <th>spinal_canal_stenosis_l1_l2</th>\n      <th>spinal_canal_stenosis_l2_l3</th>\n      <th>spinal_canal_stenosis_l3_l4</th>\n      <th>spinal_canal_stenosis_l4_l5</th>\n      <th>spinal_canal_stenosis_l5_s1</th>\n      <th>left_neural_foraminal_narrowing_l1_l2</th>\n      <th>left_neural_foraminal_narrowing_l2_l3</th>\n      <th>left_neural_foraminal_narrowing_l3_l4</th>\n      <th>left_neural_foraminal_narrowing_l4_l5</th>\n      <th>...</th>\n      <th>left_subarticular_stenosis_l1_l2</th>\n      <th>left_subarticular_stenosis_l2_l3</th>\n      <th>left_subarticular_stenosis_l3_l4</th>\n      <th>left_subarticular_stenosis_l4_l5</th>\n      <th>left_subarticular_stenosis_l5_s1</th>\n      <th>right_subarticular_stenosis_l1_l2</th>\n      <th>right_subarticular_stenosis_l2_l3</th>\n      <th>right_subarticular_stenosis_l3_l4</th>\n      <th>right_subarticular_stenosis_l4_l5</th>\n      <th>right_subarticular_stenosis_l5_s1</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>4646740</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Moderate</td>\n      <td>Severe</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Moderate</td>\n      <td>...</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Severe</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Moderate</td>\n      <td>Moderate</td>\n      <td>Moderate</td>\n      <td>Normal/Mild</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>22191399</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Severe</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>...</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Moderate</td>\n      <td>Moderate</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Severe</td>\n      <td>Moderate</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>29931867</td>\n      <td>Normal/Mild</td>\n      <td>Moderate</td>\n      <td>Moderate</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Moderate</td>\n      <td>...</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Moderate</td>\n      <td>Moderate</td>\n      <td>Moderate</td>\n      <td>Moderate</td>\n      <td>Severe</td>\n      <td>Severe</td>\n      <td>Moderate</td>\n      <td>Severe</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>38281420</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Moderate</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Moderate</td>\n      <td>...</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Moderate</td>\n      <td>Moderate</td>\n      <td>Severe</td>\n      <td>Severe</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>40745534</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Moderate</td>\n      <td>Severe</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Moderate</td>\n      <td>...</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Moderate</td>\n      <td>Severe</td>\n      <td>Moderate</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Moderate</td>\n      <td>Severe</td>\n      <td>Normal/Mild</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>535</th>\n      <td>3158185203</td>\n      <td>Normal/Mild</td>\n      <td>Moderate</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Moderate</td>\n      <td>Moderate</td>\n      <td>Severe</td>\n      <td>...</td>\n      <td>Normal/Mild</td>\n      <td>Moderate</td>\n      <td>Moderate</td>\n      <td>Severe</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Moderate</td>\n      <td>Normal/Mild</td>\n      <td>Moderate</td>\n      <td>Normal/Mild</td>\n    </tr>\n    <tr>\n      <th>536</th>\n      <td>3160528641</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Moderate</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Moderate</td>\n      <td>Moderate</td>\n      <td>...</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Moderate</td>\n      <td>Moderate</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Severe</td>\n      <td>Normal/Mild</td>\n    </tr>\n    <tr>\n      <th>537</th>\n      <td>3163594538</td>\n      <td>Severe</td>\n      <td>Severe</td>\n      <td>Severe</td>\n      <td>Severe</td>\n      <td>Normal/Mild</td>\n      <td>Moderate</td>\n      <td>Moderate</td>\n      <td>Moderate</td>\n      <td>Moderate</td>\n      <td>...</td>\n      <td>Severe</td>\n      <td>Severe</td>\n      <td>Severe</td>\n      <td>Severe</td>\n      <td>Normal/Mild</td>\n      <td>Moderate</td>\n      <td>Severe</td>\n      <td>Severe</td>\n      <td>Severe</td>\n      <td>Moderate</td>\n    </tr>\n    <tr>\n      <th>538</th>\n      <td>3168755174</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Severe</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Moderate</td>\n      <td>...</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>Severe</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>Severe</td>\n      <td>Severe</td>\n      <td>Severe</td>\n    </tr>\n    <tr>\n      <th>539</th>\n      <td>3172703788</td>\n      <td>Moderate</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>...</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Normal/Mild</td>\n      <td>Moderate</td>\n      <td>Moderate</td>\n    </tr>\n  </tbody>\n</table>\n<p>540 rows × 26 columns</p>\n</div>"},"metadata":{}}]},{"cell_type":"code","source":"df_filtered","metadata":{"execution":{"iopub.status.busy":"2024-10-02T13:39:16.985145Z","iopub.execute_input":"2024-10-02T13:39:16.985654Z","iopub.status.idle":"2024-10-02T13:39:17.000769Z","shell.execute_reply.started":"2024-10-02T13:39:16.985605Z","shell.execute_reply":"2024-10-02T13:39:16.999355Z"},"trusted":true},"execution_count":5,"outputs":[{"execution_count":5,"output_type":"execute_result","data":{"text/plain":"        study_id   series_id series_description\n0        4646740  3201256954           Axial T2\n1        4646740  3486248476        Sagittal T1\n2        4646740  3666319702   Sagittal T2/STIR\n3       22191399  3687121182           Axial T2\n4       22191399   434280813        Sagittal T1\n...          ...         ...                ...\n1614  3168755174  3335509714        Sagittal T1\n1615  3168755174  3299192635   Sagittal T2/STIR\n1616  3172703788  3357340646           Axial T2\n1617  3172703788  1681952265        Sagittal T1\n1618  3172703788   619632020   Sagittal T2/STIR\n\n[1619 rows x 3 columns]","text/html":"<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>study_id</th>\n      <th>series_id</th>\n      <th>series_description</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>4646740</td>\n      <td>3201256954</td>\n      <td>Axial T2</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>4646740</td>\n      <td>3486248476</td>\n      <td>Sagittal T1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>4646740</td>\n      <td>3666319702</td>\n      <td>Sagittal T2/STIR</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>22191399</td>\n      <td>3687121182</td>\n      <td>Axial T2</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>22191399</td>\n      <td>434280813</td>\n      <td>Sagittal T1</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>1614</th>\n      <td>3168755174</td>\n      <td>3335509714</td>\n      <td>Sagittal T1</td>\n    </tr>\n    <tr>\n      <th>1615</th>\n      <td>3168755174</td>\n      <td>3299192635</td>\n      <td>Sagittal T2/STIR</td>\n    </tr>\n    <tr>\n      <th>1616</th>\n      <td>3172703788</td>\n      <td>3357340646</td>\n      <td>Axial T2</td>\n    </tr>\n    <tr>\n      <th>1617</th>\n      <td>3172703788</td>\n      <td>1681952265</td>\n      <td>Sagittal T1</td>\n    </tr>\n    <tr>\n      <th>1618</th>\n      <td>3172703788</td>\n      <td>619632020</td>\n      <td>Sagittal T2/STIR</td>\n    </tr>\n  </tbody>\n</table>\n<p>1619 rows × 3 columns</p>\n</div>"},"metadata":{}}]},{"cell_type":"code","source":"df_filtered.head(30)","metadata":{"execution":{"iopub.status.busy":"2024-10-02T13:39:19.308369Z","iopub.execute_input":"2024-10-02T13:39:19.308875Z","iopub.status.idle":"2024-10-02T13:39:19.326062Z","shell.execute_reply.started":"2024-10-02T13:39:19.308829Z","shell.execute_reply":"2024-10-02T13:39:19.324664Z"},"trusted":true},"execution_count":6,"outputs":[{"execution_count":6,"output_type":"execute_result","data":{"text/plain":"    study_id   series_id series_description\n0    4646740  3201256954           Axial T2\n1    4646740  3486248476        Sagittal T1\n2    4646740  3666319702   Sagittal T2/STIR\n3   22191399  3687121182           Axial T2\n4   22191399   434280813        Sagittal T1\n5   22191399  3753885158   Sagittal T2/STIR\n6   29931867   231278500           Axial T2\n7   29931867  1152175603        Sagittal T1\n8   29931867  1676821058   Sagittal T2/STIR\n9   38281420  1178941473           Axial T2\n10  38281420  2565838687        Sagittal T1\n11  38281420   880361156   Sagittal T2/STIR\n12  40745534  1395478236           Axial T2\n13  40745534  2948359731        Sagittal T1\n14  40745534  3918681171   Sagittal T2/STIR\n15  41477684  3749739759           Axial T2\n16  41477684  3009399271        Sagittal T1\n17  41477684  2595734107   Sagittal T2/STIR\n18  46494080  1543341132           Axial T2\n19  46494080  4061588226        Sagittal T1\n20  46494080  1763376930   Sagittal T2/STIR\n21  52397721  3220885418           Axial T2\n22  52397721  2770638094        Sagittal T1\n23  52397721  2452297573   Sagittal T2/STIR\n24  52695609  3778925627           Axial T2\n25  52695609  3284178089        Sagittal T1\n26  52695609    32135551   Sagittal T2/STIR\n27  58813022   302608236           Axial T2\n28  58813022   692718517        Sagittal T1\n29  58813022  3875372861   Sagittal T2/STIR","text/html":"<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>study_id</th>\n      <th>series_id</th>\n      <th>series_description</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>4646740</td>\n      <td>3201256954</td>\n      <td>Axial T2</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>4646740</td>\n      <td>3486248476</td>\n      <td>Sagittal T1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>4646740</td>\n      <td>3666319702</td>\n      <td>Sagittal T2/STIR</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>22191399</td>\n      <td>3687121182</td>\n      <td>Axial T2</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>22191399</td>\n      <td>434280813</td>\n      <td>Sagittal T1</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>22191399</td>\n      <td>3753885158</td>\n      <td>Sagittal T2/STIR</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>29931867</td>\n      <td>231278500</td>\n      <td>Axial T2</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>29931867</td>\n      <td>1152175603</td>\n      <td>Sagittal T1</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>29931867</td>\n      <td>1676821058</td>\n      <td>Sagittal T2/STIR</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>38281420</td>\n      <td>1178941473</td>\n      <td>Axial T2</td>\n    </tr>\n    <tr>\n      <th>10</th>\n      <td>38281420</td>\n      <td>2565838687</td>\n      <td>Sagittal T1</td>\n    </tr>\n    <tr>\n      <th>11</th>\n      <td>38281420</td>\n      <td>880361156</td>\n      <td>Sagittal T2/STIR</td>\n    </tr>\n    <tr>\n      <th>12</th>\n      <td>40745534</td>\n      <td>1395478236</td>\n      <td>Axial T2</td>\n    </tr>\n    <tr>\n      <th>13</th>\n      <td>40745534</td>\n      <td>2948359731</td>\n      <td>Sagittal T1</td>\n    </tr>\n    <tr>\n      <th>14</th>\n      <td>40745534</td>\n      <td>3918681171</td>\n      <td>Sagittal T2/STIR</td>\n    </tr>\n    <tr>\n      <th>15</th>\n      <td>41477684</td>\n      <td>3749739759</td>\n      <td>Axial T2</td>\n    </tr>\n    <tr>\n      <th>16</th>\n      <td>41477684</td>\n      <td>3009399271</td>\n      <td>Sagittal T1</td>\n    </tr>\n    <tr>\n      <th>17</th>\n      <td>41477684</td>\n      <td>2595734107</td>\n      <td>Sagittal T2/STIR</td>\n    </tr>\n    <tr>\n      <th>18</th>\n      <td>46494080</td>\n      <td>1543341132</td>\n      <td>Axial T2</td>\n    </tr>\n    <tr>\n      <th>19</th>\n      <td>46494080</td>\n      <td>4061588226</td>\n      <td>Sagittal T1</td>\n    </tr>\n    <tr>\n      <th>20</th>\n      <td>46494080</td>\n      <td>1763376930</td>\n      <td>Sagittal T2/STIR</td>\n    </tr>\n    <tr>\n      <th>21</th>\n      <td>52397721</td>\n      <td>3220885418</td>\n      <td>Axial T2</td>\n    </tr>\n    <tr>\n      <th>22</th>\n      <td>52397721</td>\n      <td>2770638094</td>\n      <td>Sagittal T1</td>\n    </tr>\n    <tr>\n      <th>23</th>\n      <td>52397721</td>\n      <td>2452297573</td>\n      <td>Sagittal T2/STIR</td>\n    </tr>\n    <tr>\n      <th>24</th>\n      <td>52695609</td>\n      <td>3778925627</td>\n      <td>Axial T2</td>\n    </tr>\n    <tr>\n      <th>25</th>\n      <td>52695609</td>\n      <td>3284178089</td>\n      <td>Sagittal T1</td>\n    </tr>\n    <tr>\n      <th>26</th>\n      <td>52695609</td>\n      <td>32135551</td>\n      <td>Sagittal T2/STIR</td>\n    </tr>\n    <tr>\n      <th>27</th>\n      <td>58813022</td>\n      <td>302608236</td>\n      <td>Axial T2</td>\n    </tr>\n    <tr>\n      <th>28</th>\n      <td>58813022</td>\n      <td>692718517</td>\n      <td>Sagittal T1</td>\n    </tr>\n    <tr>\n      <th>29</th>\n      <td>58813022</td>\n      <td>3875372861</td>\n      <td>Sagittal T2/STIR</td>\n    </tr>\n  </tbody>\n</table>\n</div>"},"metadata":{}}]},{"cell_type":"code","source":"df_filtered.tail(30)","metadata":{"execution":{"iopub.status.busy":"2024-10-02T13:39:22.751474Z","iopub.execute_input":"2024-10-02T13:39:22.751936Z","iopub.status.idle":"2024-10-02T13:39:22.766903Z","shell.execute_reply.started":"2024-10-02T13:39:22.751892Z","shell.execute_reply":"2024-10-02T13:39:22.765497Z"},"trusted":true},"execution_count":7,"outputs":[{"execution_count":7,"output_type":"execute_result","data":{"text/plain":"        study_id   series_id series_description\n1589  3128795155  2985676628           Axial T2\n1590  3128795155   483456514        Sagittal T1\n1591  3128795155   246275868   Sagittal T2/STIR\n1592  3138242355   684398187           Axial T2\n1593  3138242355  3310553683        Sagittal T1\n1594  3138242355   992065261   Sagittal T2/STIR\n1595  3139274536  1038845969           Axial T2\n1596  3139274536  2171939378        Sagittal T1\n1597  3139274536  3906320584   Sagittal T2/STIR\n1598  3141331592  3267884718           Axial T2\n1599  3141331592    72737584        Sagittal T1\n1600  3141331592  1244841079   Sagittal T2/STIR\n1601  3156269631   501368228           Axial T2\n1602  3156269631  4030602643        Sagittal T1\n1603  3156269631  4059442027   Sagittal T2/STIR\n1604  3158185203  2163803231           Axial T2\n1605  3158185203  1054679659        Sagittal T1\n1606  3158185203  2946239560   Sagittal T2/STIR\n1607  3160528641  3183076389           Axial T2\n1608  3160528641  1845964212        Sagittal T1\n1609  3160528641  3180832116   Sagittal T2/STIR\n1610  3163594538   294771064           Axial T2\n1611  3163594538    15983577        Sagittal T1\n1612  3163594538  2752448967   Sagittal T2/STIR\n1613  3168755174  3241932744           Axial T2\n1614  3168755174  3335509714        Sagittal T1\n1615  3168755174  3299192635   Sagittal T2/STIR\n1616  3172703788  3357340646           Axial T2\n1617  3172703788  1681952265        Sagittal T1\n1618  3172703788   619632020   Sagittal T2/STIR","text/html":"<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>study_id</th>\n      <th>series_id</th>\n      <th>series_description</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1589</th>\n      <td>3128795155</td>\n      <td>2985676628</td>\n      <td>Axial T2</td>\n    </tr>\n    <tr>\n      <th>1590</th>\n      <td>3128795155</td>\n      <td>483456514</td>\n      <td>Sagittal T1</td>\n    </tr>\n    <tr>\n      <th>1591</th>\n      <td>3128795155</td>\n      <td>246275868</td>\n      <td>Sagittal T2/STIR</td>\n    </tr>\n    <tr>\n      <th>1592</th>\n      <td>3138242355</td>\n      <td>684398187</td>\n      <td>Axial T2</td>\n    </tr>\n    <tr>\n      <th>1593</th>\n      <td>3138242355</td>\n      <td>3310553683</td>\n      <td>Sagittal T1</td>\n    </tr>\n    <tr>\n      <th>1594</th>\n      <td>3138242355</td>\n      <td>992065261</td>\n      <td>Sagittal T2/STIR</td>\n    </tr>\n    <tr>\n      <th>1595</th>\n      <td>3139274536</td>\n      <td>1038845969</td>\n      <td>Axial T2</td>\n    </tr>\n    <tr>\n      <th>1596</th>\n      <td>3139274536</td>\n      <td>2171939378</td>\n      <td>Sagittal T1</td>\n    </tr>\n    <tr>\n      <th>1597</th>\n      <td>3139274536</td>\n      <td>3906320584</td>\n      <td>Sagittal T2/STIR</td>\n    </tr>\n    <tr>\n      <th>1598</th>\n      <td>3141331592</td>\n      <td>3267884718</td>\n      <td>Axial T2</td>\n    </tr>\n    <tr>\n      <th>1599</th>\n      <td>3141331592</td>\n      <td>72737584</td>\n      <td>Sagittal T1</td>\n    </tr>\n    <tr>\n      <th>1600</th>\n      <td>3141331592</td>\n      <td>1244841079</td>\n      <td>Sagittal T2/STIR</td>\n    </tr>\n    <tr>\n      <th>1601</th>\n      <td>3156269631</td>\n      <td>501368228</td>\n      <td>Axial T2</td>\n    </tr>\n    <tr>\n      <th>1602</th>\n      <td>3156269631</td>\n      <td>4030602643</td>\n      <td>Sagittal T1</td>\n    </tr>\n    <tr>\n      <th>1603</th>\n      <td>3156269631</td>\n      <td>4059442027</td>\n      <td>Sagittal T2/STIR</td>\n    </tr>\n    <tr>\n      <th>1604</th>\n      <td>3158185203</td>\n      <td>2163803231</td>\n      <td>Axial T2</td>\n    </tr>\n    <tr>\n      <th>1605</th>\n      <td>3158185203</td>\n      <td>1054679659</td>\n      <td>Sagittal T1</td>\n    </tr>\n    <tr>\n      <th>1606</th>\n      <td>3158185203</td>\n      <td>2946239560</td>\n      <td>Sagittal T2/STIR</td>\n    </tr>\n    <tr>\n      <th>1607</th>\n      <td>3160528641</td>\n      <td>3183076389</td>\n      <td>Axial T2</td>\n    </tr>\n    <tr>\n      <th>1608</th>\n      <td>3160528641</td>\n      <td>1845964212</td>\n      <td>Sagittal T1</td>\n    </tr>\n    <tr>\n      <th>1609</th>\n      <td>3160528641</td>\n      <td>3180832116</td>\n      <td>Sagittal T2/STIR</td>\n    </tr>\n    <tr>\n      <th>1610</th>\n      <td>3163594538</td>\n      <td>294771064</td>\n      <td>Axial T2</td>\n    </tr>\n    <tr>\n      <th>1611</th>\n      <td>3163594538</td>\n      <td>15983577</td>\n      <td>Sagittal T1</td>\n    </tr>\n    <tr>\n      <th>1612</th>\n      <td>3163594538</td>\n      <td>2752448967</td>\n      <td>Sagittal T2/STIR</td>\n    </tr>\n    <tr>\n      <th>1613</th>\n      <td>3168755174</td>\n      <td>3241932744</td>\n      <td>Axial T2</td>\n    </tr>\n    <tr>\n      <th>1614</th>\n      <td>3168755174</td>\n      <td>3335509714</td>\n      <td>Sagittal T1</td>\n    </tr>\n    <tr>\n      <th>1615</th>\n      <td>3168755174</td>\n      <td>3299192635</td>\n      <td>Sagittal T2/STIR</td>\n    </tr>\n    <tr>\n      <th>1616</th>\n      <td>3172703788</td>\n      <td>3357340646</td>\n      <td>Axial T2</td>\n    </tr>\n    <tr>\n      <th>1617</th>\n      <td>3172703788</td>\n      <td>1681952265</td>\n      <td>Sagittal T1</td>\n    </tr>\n    <tr>\n      <th>1618</th>\n      <td>3172703788</td>\n      <td>619632020</td>\n      <td>Sagittal T2/STIR</td>\n    </tr>\n  </tbody>\n</table>\n</div>"},"metadata":{}}]},{"cell_type":"code","source":"image_dir = \"/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_images\"\nstudy_id_dir = '/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_images/100206310'\nseries_id_dir = '/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_images/100206310/1012284084'\n\n# Example image size and number of stacked slices (depth for 3D)\nimg_width, img_height = 256, 256\ndepth = 50  # Number of 2D slices to stack","metadata":{"execution":{"iopub.status.busy":"2024-10-02T13:39:30.172139Z","iopub.execute_input":"2024-10-02T13:39:30.172607Z","iopub.status.idle":"2024-10-02T13:39:30.178722Z","shell.execute_reply.started":"2024-10-02T13:39:30.172562Z","shell.execute_reply":"2024-10-02T13:39:30.177408Z"},"trusted":true},"execution_count":8,"outputs":[]},{"cell_type":"code","source":"# import os\n# import numpy as np\n# import pandas as pd\n# import pydicom\n# from tensorflow.keras.preprocessing.image import img_to_array\n# from skimage.transform import resize\n\n# # Load 2D DICOM images and stack them into 3D arrays\n# def load_images(df_filtered, image_dir, img_width, img_height, depth):\n#     images = []\n#     labels = []  # Assuming you have some labels like 'series_description'\n\n#     # Iterate over the rows in the filtered DataFrame\n#     for _, row in df_filtered.iterrows():\n#         study_id = str(row['study_id'])\n#         series_id = str(row['series_id'])\n        \n#         # Construct the series directory path\n#         series_id_dir = os.path.join(image_dir, study_id, series_id)\n        \n#         # Ensure the directory exists\n#         if not os.path.exists(series_id_dir):\n#             continue\n\n#         # Collect DICOM files, sorted by their numeric order\n#         dicom_files = sorted([f for f in os.listdir(series_id_dir) if f.endswith('.dcm')],\n#                              key=lambda x: int(x.split('.')[0]))  # Sort by the number in the filename\n        \n#         stacked_images = []\n\n#         # Load the first 'depth' number of DICOM files (or all if less than 'depth')\n#         for i, dicom_file in enumerate(dicom_files[:depth]):\n#             dicom_path = os.path.join(series_id_dir, dicom_file)\n#             dicom_data = pydicom.dcmread(dicom_path)\n\n#             # Convert the DICOM pixel array to a NumPy array and resize it\n#             img_array = dicom_data.pixel_array\n#             img_resized = resize(img_array, (img_width, img_height), preserve_range=True)\n#             img_resized = img_to_array(img_resized)\n\n#             stacked_images.append(img_resized)\n        \n#         # Stack the 2D slices to create a 3D array\n#         if len(stacked_images) > 0:\n#             stacked_images = np.stack(stacked_images, axis=-1)  # Create 3D array\n        \n#         images.append(stacked_images)\n#         labels.append(row['series_description'])  # Add the label, assuming 'series_description' is the label\n    \n#     return np.array(images), np.array(labels)\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport pydicom\nfrom tensorflow.keras.preprocessing.image import img_to_array\nfrom skimage.transform import resize\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import OneHotEncoder\n","metadata":{"execution":{"iopub.status.busy":"2024-10-02T13:39:34.319554Z","iopub.execute_input":"2024-10-02T13:39:34.319986Z","iopub.status.idle":"2024-10-02T13:39:34.354407Z","shell.execute_reply.started":"2024-10-02T13:39:34.319946Z","shell.execute_reply":"2024-10-02T13:39:34.353244Z"},"trusted":true},"execution_count":9,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\n# Map categories to integers\nlabel_map = {'Normal/Mild': 0.0, 'Moderate': 1.0, 'Severe': 2.0}\n\n# Convert targets in train.csv to numeric form\ndef prepare_labels(train_csv):\n    # Replace the text labels with numbers for all target columns\n    target_columns = train_csv.columns[1:]  # Exclude 'study_id' column\n    for col in target_columns:\n        train_csv[col] = train_csv[col].map(label_map)\n    \n    return train_csv\n","metadata":{"execution":{"iopub.status.busy":"2024-10-02T13:39:36.214201Z","iopub.execute_input":"2024-10-02T13:39:36.215448Z","iopub.status.idle":"2024-10-02T13:39:36.222349Z","shell.execute_reply.started":"2024-10-02T13:39:36.215372Z","shell.execute_reply":"2024-10-02T13:39:36.221134Z"},"trusted":true},"execution_count":10,"outputs":[]},{"cell_type":"code","source":"train_csv = prepare_labels(df_train)\ntrain_csv","metadata":{"execution":{"iopub.status.busy":"2024-10-02T13:39:38.151638Z","iopub.execute_input":"2024-10-02T13:39:38.152203Z","iopub.status.idle":"2024-10-02T13:39:38.231899Z","shell.execute_reply.started":"2024-10-02T13:39:38.152146Z","shell.execute_reply":"2024-10-02T13:39:38.230524Z"},"trusted":true},"execution_count":11,"outputs":[{"execution_count":11,"output_type":"execute_result","data":{"text/plain":"       study_id  spinal_canal_stenosis_l1_l2  spinal_canal_stenosis_l2_l3  \\\n0       4646740                          0.0                          0.0   \n1      22191399                          0.0                          0.0   \n2      29931867                          0.0                          1.0   \n3      38281420                          0.0                          0.0   \n4      40745534                          0.0                          0.0   \n..          ...                          ...                          ...   \n535  3158185203                          0.0                          1.0   \n536  3160528641                          0.0                          0.0   \n537  3163594538                          2.0                          2.0   \n538  3168755174                          0.0                          0.0   \n539  3172703788                          1.0                          0.0   \n\n     spinal_canal_stenosis_l3_l4  spinal_canal_stenosis_l4_l5  \\\n0                            1.0                          2.0   \n1                            0.0                          2.0   \n2                            1.0                          0.0   \n3                            0.0                          1.0   \n4                            1.0                          2.0   \n..                           ...                          ...   \n535                          0.0                          0.0   \n536                          0.0                          1.0   \n537                          2.0                          2.0   \n538                          2.0                          0.0   \n539                          0.0                          0.0   \n\n     spinal_canal_stenosis_l5_s1  left_neural_foraminal_narrowing_l1_l2  \\\n0                            0.0                                    0.0   \n1                            0.0                                    0.0   \n2                            0.0                                    0.0   \n3                            0.0                                    0.0   \n4                            0.0                                    0.0   \n..                           ...                                    ...   \n535                          0.0                                    0.0   \n536                          0.0                                    0.0   \n537                          0.0                                    1.0   \n538                          0.0                                    0.0   \n539                          0.0                                    0.0   \n\n     left_neural_foraminal_narrowing_l2_l3  \\\n0                                      0.0   \n1                                      0.0   \n2                                      0.0   \n3                                      0.0   \n4                                      0.0   \n..                                     ...   \n535                                    1.0   \n536                                    0.0   \n537                                    1.0   \n538                                    0.0   \n539                                    0.0   \n\n     left_neural_foraminal_narrowing_l3_l4  \\\n0                                      0.0   \n1                                      0.0   \n2                                      0.0   \n3                                      0.0   \n4                                      0.0   \n..                                     ...   \n535                                    1.0   \n536                                    1.0   \n537                                    1.0   \n538                                    0.0   \n539                                    0.0   \n\n     left_neural_foraminal_narrowing_l4_l5  ...  \\\n0                                      1.0  ...   \n1                                      0.0  ...   \n2                                      1.0  ...   \n3                                      1.0  ...   \n4                                      1.0  ...   \n..                                     ...  ...   \n535                                    2.0  ...   \n536                                    1.0  ...   \n537                                    1.0  ...   \n538                                    1.0  ...   \n539                                    0.0  ...   \n\n     left_subarticular_stenosis_l1_l2  left_subarticular_stenosis_l2_l3  \\\n0                                 0.0                               0.0   \n1                                 0.0                               0.0   \n2                                 0.0                               0.0   \n3                                 0.0                               0.0   \n4                                 0.0                               0.0   \n..                                ...                               ...   \n535                               0.0                               1.0   \n536                               0.0                               0.0   \n537                               2.0                               2.0   \n538                               NaN                               NaN   \n539                               0.0                               0.0   \n\n     left_subarticular_stenosis_l3_l4  left_subarticular_stenosis_l4_l5  \\\n0                                 0.0                               2.0   \n1                                 0.0                               1.0   \n2                                 1.0                               1.0   \n3                                 0.0                               0.0   \n4                                 1.0                               2.0   \n..                                ...                               ...   \n535                               1.0                               2.0   \n536                               1.0                               1.0   \n537                               2.0                               2.0   \n538                               2.0                               0.0   \n539                               0.0                               0.0   \n\n     left_subarticular_stenosis_l5_s1  right_subarticular_stenosis_l1_l2  \\\n0                                 0.0                                0.0   \n1                                 1.0                                0.0   \n2                                 1.0                                1.0   \n3                                 0.0                                0.0   \n4                                 1.0                                0.0   \n..                                ...                                ...   \n535                               0.0                                0.0   \n536                               0.0                                0.0   \n537                               0.0                                1.0   \n538                               0.0                                NaN   \n539                               0.0                                0.0   \n\n     right_subarticular_stenosis_l2_l3  right_subarticular_stenosis_l3_l4  \\\n0                                  1.0                                1.0   \n1                                  0.0                                0.0   \n2                                  2.0                                2.0   \n3                                  1.0                                1.0   \n4                                  0.0                                1.0   \n..                                 ...                                ...   \n535                                1.0                                0.0   \n536                                0.0                                0.0   \n537                                2.0                                2.0   \n538                                NaN                                2.0   \n539                                0.0                                0.0   \n\n     right_subarticular_stenosis_l4_l5  right_subarticular_stenosis_l5_s1  \n0                                  1.0                                0.0  \n1                                  2.0                                1.0  \n2                                  1.0                                2.0  \n3                                  2.0                                2.0  \n4                                  2.0                                0.0  \n..                                 ...                                ...  \n535                                1.0                                0.0  \n536                                2.0                                0.0  \n537                                2.0                                1.0  \n538                                2.0                                2.0  \n539                                1.0                                1.0  \n\n[540 rows x 26 columns]","text/html":"<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>study_id</th>\n      <th>spinal_canal_stenosis_l1_l2</th>\n      <th>spinal_canal_stenosis_l2_l3</th>\n      <th>spinal_canal_stenosis_l3_l4</th>\n      <th>spinal_canal_stenosis_l4_l5</th>\n      <th>spinal_canal_stenosis_l5_s1</th>\n      <th>left_neural_foraminal_narrowing_l1_l2</th>\n      <th>left_neural_foraminal_narrowing_l2_l3</th>\n      <th>left_neural_foraminal_narrowing_l3_l4</th>\n      <th>left_neural_foraminal_narrowing_l4_l5</th>\n      <th>...</th>\n      <th>left_subarticular_stenosis_l1_l2</th>\n      <th>left_subarticular_stenosis_l2_l3</th>\n      <th>left_subarticular_stenosis_l3_l4</th>\n      <th>left_subarticular_stenosis_l4_l5</th>\n      <th>left_subarticular_stenosis_l5_s1</th>\n      <th>right_subarticular_stenosis_l1_l2</th>\n      <th>right_subarticular_stenosis_l2_l3</th>\n      <th>right_subarticular_stenosis_l3_l4</th>\n      <th>right_subarticular_stenosis_l4_l5</th>\n      <th>right_subarticular_stenosis_l5_s1</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>4646740</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>2.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>...</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>2.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>1.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>22191399</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>2.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>...</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>2.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>29931867</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>...</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>1.0</td>\n      <td>1.0</td>\n      <td>1.0</td>\n      <td>2.0</td>\n      <td>2.0</td>\n      <td>1.0</td>\n      <td>2.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>38281420</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>...</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>1.0</td>\n      <td>2.0</td>\n      <td>2.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>40745534</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>2.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>...</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>2.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>2.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>535</th>\n      <td>3158185203</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>1.0</td>\n      <td>2.0</td>\n      <td>...</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>1.0</td>\n      <td>2.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>536</th>\n      <td>3160528641</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>1.0</td>\n      <td>...</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>2.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>537</th>\n      <td>3163594538</td>\n      <td>2.0</td>\n      <td>2.0</td>\n      <td>2.0</td>\n      <td>2.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>1.0</td>\n      <td>1.0</td>\n      <td>1.0</td>\n      <td>...</td>\n      <td>2.0</td>\n      <td>2.0</td>\n      <td>2.0</td>\n      <td>2.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>2.0</td>\n      <td>2.0</td>\n      <td>2.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>538</th>\n      <td>3168755174</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>2.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>...</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>2.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>2.0</td>\n      <td>2.0</td>\n      <td>2.0</td>\n    </tr>\n    <tr>\n      <th>539</th>\n      <td>3172703788</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>...</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>1.0</td>\n    </tr>\n  </tbody>\n</table>\n<p>540 rows × 26 columns</p>\n</div>"},"metadata":{}}]},{"cell_type":"code","source":"# import pandas as pd\n# from sklearn.preprocessing import OneHotEncoder\n\n# # Map categories to integers\n# label_map = {'Normal/Mild': 0, 'Moderate': 1, 'Severe': 2}\n\n# # Convert targets in train.csv to numeric form and then one-hot encode\n# def prepare_labels(train_csv):\n#     # Replace the text labels with numbers\n#     target_columns = train_csv.columns[1:]  # Exclude 'study_id' column\n#     for col in target_columns:\n#         train_csv[col] = train_csv[col].map(label_map)\n    \n#     # Initialize OneHotEncoder\n#     encoder = OneHotEncoder(categories=[0, 1, 2], sparse=False, drop=None)\n    \n#     # Apply OneHotEncoder to each target column and concatenate\n#     encoded_targets = []\n#     for col in target_columns:\n#         y = encoder.fit_transform(train_csv[[col]])\n#         encoded_df = pd.DataFrame(y, columns=[f\"{col}_{i}\" for i in range(y.shape[1])])\n#         encoded_targets.append(encoded_df)\n    \n#     # Concatenate all encoded target DataFrames\n#     y_df = pd.concat(encoded_targets, axis=1)\n    \n#     # Concatenate the study_id column with the one-hot encoded columns\n#     train_csv_encoded = pd.concat([train_csv[['study_id']], y_df], axis=1)\n    \n#     return train_csv_encoded\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import pandas as pd\n# from sklearn.preprocessing import OneHotEncoder\n\n# # Map categories to integers\n# label_map = {'Normal/Mild': 0, 'Moderate': 1, 'Severe': 2}\n\n# # Convert targets in train.csv to numeric form and then one-hot encode\n# def prepare_labels(train_csv):\n#     # Replace the text labels with numbers\n#     target_columns = train_csv.columns[1:]  # Exclude 'study_id' column\n#     for col in target_columns:\n#         train_csv[col] = train_csv[col].map(label_map)\n    \n#     # One-hot encode the target columns\n#     encoder = OneHotEncoder(sparse=False)\n#     y = encoder.fit_transform(train_csv[target_columns])\n    \n#     # Create a DataFrame from the one-hot encoded array\n#     y_df = pd.DataFrame(y, columns=encoder.get_feature_names_out(target_columns))\n    \n#     # Concatenate the study_id column with the one-hot encoded columns\n#     train_csv_encoded = pd.concat([train_csv[['study_id']], y_df], axis=1)\n    \n#     return train_csv_encoded\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import pandas as pd\n\n# # Define the mapping from categories to integers\n# label_map = {'Normal/Mild': 0, 'Moderate': 1, 'Severe': 2}\n\n# # Function to prepare labels with integer encoding\n# def prepare_labels(train_csv):\n#     # Replace text labels with their corresponding numbers\n#     target_columns = train_csv.columns[1:]  # Exclude 'study_id' column\n#     for col in target_columns:\n#         train_csv[col] = train_csv[col].map(label_map)\n    \n#     return train_csv\n\n# # Load the data\n# df = pd.read_csv('/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_series_descriptions.csv')\n# train = pd.read_csv('/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train.csv')\n\n# # Prepare the labels with integer encoding\n# train_encoded = prepare_labels(train)\n\n# # Save or inspect the encoded DataFrame\n# train_encoded.head()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Now series_ids_ordered contains the series_ids in the order defined by custom_order\n# Use these ordered series_ids to loop through the series folders\n\n\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport os\nimport pydicom\nfrom sklearn.preprocessing import OneHotEncoder\nfrom skimage.transform import resize\nfrom tensorflow.keras.preprocessing.image import img_to_array\n\n# Define the function to load 2D DICOM images and stack them into 3D arrays with consistent depth\ndef load_images(df_filtered, train_csv, image_dir, img_width, img_height, depth):\n    images = []\n    labels = []  # Targets from the 25 columns\n\n    # Iterate over the rows in the filtered DataFrame\n    for _, row in df_filtered.iterrows():\n        study_id = str(row['study_id'])\n        \n        # Match the study_id from df_filtered with the corresponding study_id in train_csv\n        target_row = train_csv[train_csv['study_id'] == int(study_id)]\n        \n        if target_row.empty:\n            print(f\"No matching targets found for study_id: {study_id}\")\n            continue\n\n        # Construct the series directory path (assuming df_filtered has the correct structure)\n        series_id = str(row['series_id'])\n        series_id_dir = os.path.join(image_dir, study_id, series_id)\n        \n        # Ensure the directory exists\n        if not os.path.exists(series_id_dir):\n            print(f\"Directory not found: {series_id_dir}\")\n            continue\n\n        # Collect DICOM files, sorted by their numeric order\n        dicom_files = sorted([f for f in os.listdir(series_id_dir) if f.endswith('.dcm')],\n                             key=lambda x: int(x.split('.')[0]))  # Sort by the number in the filename\n        \n        if len(dicom_files) == 0:\n            print(f\"No DICOM files found in directory: {series_id_dir}\")\n            continue\n\n        stacked_images = []\n\n        # Load the DICOM files, resizing and stacking them\n        for dicom_file in dicom_files[:depth]:  # Limit to 'depth' slices\n            dicom_path = os.path.join(series_id_dir, dicom_file)\n            dicom_data = pydicom.dcmread(dicom_path)\n\n            # Convert the DICOM pixel array to a NumPy array and resize it\n            img_array = dicom_data.pixel_array\n            img_resized = resize(img_array, (img_width, img_height), preserve_range=True)\n            img_resized = img_to_array(img_resized)\n\n            stacked_images.append(img_resized)\n        \n        # Handle case where the number of slices is less than 'depth'\n        if len(stacked_images) < depth:\n            # Pad with zeros to maintain consistent depth\n            padding_needed = depth - len(stacked_images)\n            padding = [np.zeros((img_width, img_height, 1))] * padding_needed\n            stacked_images.extend(padding)\n        \n        # Stack the 2D slices to create a 3D array\n        stacked_images = np.stack(stacked_images, axis=-1)  # Create 3D array\n\n        images.append(stacked_images)\n        \n        # Append the target for this study_id\n        labels.append(target_row.iloc[0, 1:].values)  # Get the 25 targets (already numeric)\n\n    return np.array(images), np.array(labels)\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-10-02T13:40:20.423309Z","iopub.execute_input":"2024-10-02T13:40:20.424648Z","iopub.status.idle":"2024-10-02T13:40:20.441248Z","shell.execute_reply.started":"2024-10-02T13:40:20.42459Z","shell.execute_reply":"2024-10-02T13:40:20.439945Z"},"trusted":true},"execution_count":12,"outputs":[]},{"cell_type":"code","source":"# Example usage:\nimg_width, img_height = 128, 128\ndepth = 50  # Number of slices per 3D image\n\ny_all = train_csv  # This converts labels into one-hot encoding\n\n# Assuming df_filtered is already loaded with 'study_id', 'series_id', and 'series_description'\nX, y = load_images(df_filtered, y_all, image_dir=\"/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_images\", \n                   img_width=img_width, img_height=img_height, depth=depth)\n\nX = np.squeeze(X, axis=-2)  # Removes the 4th dimension\n\n# Check if images and labels are loaded correctly\nprint(f\"Number of images loaded: {len(X)}\")\nprint(f\"Number of labels loaded: {len(y)}\")\n\nprint(X.shape)\nprint(y.shape)","metadata":{"execution":{"iopub.status.busy":"2024-10-02T13:40:44.709115Z","iopub.execute_input":"2024-10-02T13:40:44.709608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Ensure X has the correct shape (num_samples, img_width, img_height, depth, 1)\n#X = np.expand_dims(X, axis=-1)  # Adding channel dimension\n\n# Ensure y is one-hot encoded and has shape (num_samples, num_classes)\nprint(X.shape)  # Should be (num_samples, img_width, img_height, depth, 1)\nprint(y.shape)  # Should be (num_samples, num_classes)\n#Remove the extra dimension (channel = 1) from the data\n#X = np.squeeze(X, axis=-2)  # Removes the 4th dimension\n\n# Now X will have the shape (batch_size, img_width, img_height, depth)\nprint(X.shape)  # Should be (num_samples, img_width, img_height, depth, 1)\nprint(y.shape)  # Should be (num_samples, num_classes)\n\n# Proceed with normalization and train-test split\nX = X / 255.0\n\n# Train-test split\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# Now X_train and X_test should have the correct shape for your CNN","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del df_train, df_filtered","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# del X, y, y_all","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv3D, MaxPooling3D, Flatten, Dense, Dropout\n\ndef create_cnn_model(input_shape, num_classes):\n    model = Sequential()\n    model.add(Conv3D(32, (3, 3, 3), activation='relu', input_shape=input_shape))\n    model.add(MaxPooling3D(pool_size=(2, 2, 2)))\n    model.add(Conv3D(64, (3, 3, 3), activation='relu'))\n    model.add(MaxPooling3D(pool_size=(2, 2, 2)))\n    model.add(Conv3D(128, (3, 3, 3), activation='relu'))\n    model.add(MaxPooling3D(pool_size=(2, 2, 2)))\n    model.add(Flatten())\n    model.add(Dense(128, activation='relu'))\n    model.add(Dense(128, activation='relu'))\n    model.add(Dense(128, activation='relu'))\n    model.add(Dropout(0.2))\n    model.add(Dense(num_classes, activation='softmax'))\n    \n    model.compile(\n        loss='categorical_crossentropy',\n        optimizer='adam',\n        metrics=['accuracy']\n    )\n    \n    return model\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import layers, models\nfrom tensorflow.keras.applications import EfficientNetB0\nfrom tensorflow.keras.layers import Conv3D, MaxPooling3D, Flatten, Dense, Dropout, GlobalAveragePooling2D, Input\n\ndef model(input_shape, num_classes):\n    # Use EfficientNetB0 as the backbone for feature extraction\n    efficientnet_base = EfficientNetB0(\n        include_top=False,   # Exclude the fully connected layer\n        input_shape=input_shape[:2] + (3,),  # Using only the 2D input with 3 channels for EfficientNet\n        weights='imagenet'   # Pre-trained weights on ImageNet\n    )\n    \n    # Input layer\n    inputs = Input(shape=input_shape)\n    \n    # Apply 3D convolution on the input data to convert it to a 2D EfficientNet-compatible shape\n    x = Conv3D(3, (3, 3, 3), activation='relu', padding='same')(inputs)\n    x = MaxPooling3D(pool_size=(1, 1, input_shape[2]))(x)  # Reduce the depth to 1 for EfficientNet input\n\n    # Reshape for EfficientNet compatibility\n    x = layers.Reshape(target_shape=(input_shape[0], input_shape[1], 3))(x)\n    \n    # Pass through EfficientNet backbone\n    x = efficientnet_base(x)\n    \n    # Global average pooling and fully connected layers\n    x = GlobalAveragePooling2D()(x)\n    x = Dense(256, activation='relu')(x)\n    x = Dropout(0.5)(x)\n    \n    # Output layer\n    outputs = Dense(num_classes, activation='softmax')(x)\n    \n    model = models.Model(inputs=inputs, outputs=outputs)\n    \n    model.compile(\n        loss='categorical_crossentropy',\n        optimizer='adam',\n        metrics=['accuracy']\n    )\n    \n    return model\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras\nfrom tensorflow.keras.applications import EfficientNetB0\nfrom keras.applications.resnet50 import ResNet50\nfrom keras.layers import Conv2D, MaxPooling2D, GlobalAveragePooling2D, Dense, Dropout, Input, Reshape, Flatten, Concatenate\nfrom keras.models import Model\n# from keras.applications import ResNet18\n\ndef create_cnn_model(input_shape, num_classes):\n    # Input layer for the 3D CNN\n    inputs = Input(shape=input_shape)  # Input shape: (256, 256, 50, 1)\n    \n    # Reshape to (50, 256, 256, 1), treat the depth as the batch size for ResNet50\n    reshaped_input = Reshape((input_shape[2], input_shape[0], input_shape[1], input_shape[3]))(inputs)  # (depth, width, height, channels)\n    \n    # Load the ResNet50 base model without the top layers\n    resnet_base = EfficientNetB0(weights='imagenet', include_top=False, input_shape=(input_shape[0], input_shape[1], 3))\n    \n    # Initialize a list to collect the outputs for each slice\n    feature_maps = []\n    \n    for i in range(input_shape[2]):  # For each slice in the depth dimension\n        # Extract the slice\n        slice_input = reshaped_input[:, i, :, :, :]\n        \n        # Convert 1 channel to 3 channels by repeating it 3 times\n        slice_input_rgb = Concatenate()([slice_input] * 3)\n        \n        # Apply ResNet50 to each slice\n        slice_features = resnet_base(slice_input_rgb)\n        \n        # Global Average Pooling after ResNet50 to reduce the feature maps\n        slice_features = GlobalAveragePooling2D()(slice_features)  # (None, feature_maps)\n        \n        # Add the feature map for this slice to the list\n        feature_maps.append(slice_features)\n    \n    # Concatenate all feature maps from each slice\n    x = Concatenate()(feature_maps)  # Shape: (None, depth * feature_maps)\n    \n    # Dense and Dropout layers for classification\n    x = Dense(256, activation='relu')(x)\n    x = Dropout(0.3)(x)\n    outputs = Dense(num_classes, activation='softmax')(x)\n    \n    # Define the final model\n    model = Model(inputs, outputs)\n    \n    # Compile the model\n    model.compile(\n        loss='categorical_crossentropy',\n        optimizer='adam',\n        metrics=['accuracy']\n    )\n    \n    return model\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import Conv2D, MaxPooling2D, Conv2DTranspose, Concatenate, Input, Dropout, UpSampling2D\nfrom tensorflow.keras.models import Model\n\ndef create_unet_model(input_shape, num_classes):\n    inputs = Input(shape=input_shape)  # Input shape: (256, 256, 50, 1)\n    \n    # Encoding Path (Downsampling)\n    conv1 = Conv2D(64, (3, 3), activation='relu', padding='same')(inputs)\n    conv1 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv1)\n    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)\n\n    conv2 = Conv2D(128, (3, 3), activation='relu', padding='same')(pool1)\n    conv2 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv2)\n    pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)\n\n    conv3 = Conv2D(256, (3, 3), activation='relu', padding='same')(pool2)\n    conv3 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv3)\n    pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)\n\n    conv4 = Conv2D(512, (3, 3), activation='relu', padding='same')(pool3)\n    conv4 = Conv2D(512, (3, 3), activation='relu', padding='same')(conv4)\n    pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)\n\n    conv5 = Conv2D(1024, (3, 3), activation='relu', padding='same')(pool4)\n    conv5 = Conv2D(1024, (3, 3), activation='relu', padding='same')(conv5)\n    drop5 = Dropout(0.5)(conv5)\n\n    # Decoding Path (Upsampling)\n    up6 = UpSampling2D(size=(2, 2))(drop5)\n    up6 = Conv2D(512, (2, 2), activation='relu', padding='same')(up6)\n    merge6 = Concatenate()([conv4, up6])\n    conv6 = Conv2D(512, (3, 3), activation='relu', padding='same')(merge6)\n    conv6 = Conv2D(512, (3, 3), activation='relu', padding='same')(conv6)\n\n    up7 = UpSampling2D(size=(2, 2))(conv6)\n    up7 = Conv2D(256, (2, 2), activation='relu', padding='same')(up7)\n    merge7 = Concatenate()([conv3, up7])\n    conv7 = Conv2D(256, (3, 3), activation='relu', padding='same')(merge7)\n    conv7 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv7)\n\n    up8 = UpSampling2D(size=(2, 2))(conv7)\n    up8 = Conv2D(128, (2, 2), activation='relu', padding='same')(up8)\n    merge8 = Concatenate()([conv2, up8])\n    conv8 = Conv2D(128, (3, 3), activation='relu', padding='same')(merge8)\n    conv8 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv8)\n\n    up9 = UpSampling2D(size=(2, 2))(conv8)\n    up9 = Conv2D(64, (2, 2), activation='relu', padding='same')(up9)\n    merge9 = Concatenate()([conv1, up9])\n    conv9 = Conv2D(64, (3, 3), activation='relu', padding='same')(merge9)\n    conv9 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv9)\n\n    # Final output layer\n    outputs = Conv2D(num_classes, (1, 1), activation='softmax')(conv9)\n\n    # Define the model\n    model = Model(inputs, outputs)\n\n    # Compile the model\n    model.compile(\n        loss='categorical_crossentropy',\n        optimizer='adam',\n        metrics=['accuracy']\n    )\n\n    return model\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import Conv3D, MaxPooling3D, Conv3DTranspose, Concatenate, Input, Dropout, UpSampling3D\nfrom tensorflow.keras.models import Model\n\ndef create_unet_model(input_shape, num_classes):\n    inputs = Input(shape=input_shape)  # Input shape: (256, 256, 50, 1) for 3D grayscale\n\n    # Encoding Path (Downsampling)\n    conv1 = Conv3D(64, (3, 3, 3), activation='relu', padding='same')(inputs)\n    conv1 = Conv3D(64, (3, 3, 3), activation='relu', padding='same')(conv1)\n    pool1 = MaxPooling3D(pool_size=(2, 2, 2))(conv1)\n\n    conv2 = Conv3D(128, (3, 3, 3), activation='relu', padding='same')(pool1)\n    conv2 = Conv3D(128, (3, 3, 3), activation='relu', padding='same')(conv2)\n    pool2 = MaxPooling3D(pool_size=(2, 2, 2))(conv2)\n\n    conv3 = Conv3D(256, (3, 3, 3), activation='relu', padding='same')(pool2)\n    conv3 = Conv3D(256, (3, 3, 3), activation='relu', padding='same')(conv3)\n    pool3 = MaxPooling3D(pool_size=(2, 2, 2))(conv3)\n\n    conv4 = Conv3D(512, (3, 3, 3), activation='relu', padding='same')(pool3)\n    conv4 = Conv3D(512, (3, 3, 3), activation='relu', padding='same')(conv4)\n    pool4 = MaxPooling3D(pool_size=(2, 2, 2))(conv4)\n\n    conv5 = Conv3D(1024, (3, 3, 3), activation='relu', padding='same')(pool4)\n    conv5 = Conv3D(1024, (3, 3, 3), activation='relu', padding='same')(conv5)\n    drop5 = Dropout(0.5)(conv5)\n\n    # Decoding Path (Upsampling)\n    up6 = UpSampling3D(size=(2, 2, 2))(drop5)\n    up6 = Conv3D(512, (2, 2, 2), activation='relu', padding='same')(up6)\n    merge6 = Concatenate()([conv4, up6])\n    conv6 = Conv3D(512, (3, 3, 3), activation='relu', padding='same')(merge6)\n    conv6 = Conv3D(512, (3, 3, 3), activation='relu', padding='same')(conv6)\n\n    up7 = UpSampling3D(size=(2, 2, 2))(conv6)\n    up7 = Conv3D(256, (2, 2, 2), activation='relu', padding='same')(up7)\n    merge7 = Concatenate()([conv3, up7])\n    conv7 = Conv3D(256, (3, 3, 3), activation='relu', padding='same')(merge7)\n    conv7 = Conv3D(256, (3, 3, 3), activation='relu', padding='same')(conv7)\n\n    up8 = UpSampling3D(size=(2, 2, 2))(conv7)\n    up8 = Conv3D(128, (2, 2, 2), activation='relu', padding='same')(up8)\n    merge8 = Concatenate()([conv2, up8])\n    conv8 = Conv3D(128, (3, 3, 3), activation='relu', padding='same')(merge8)\n    conv8 = Conv3D(128, (3, 3, 3), activation='relu', padding='same')(conv8)\n\n    up9 = UpSampling3D(size=(2, 2, 2))(conv8)\n    up9 = Conv3D(64, (2, 2, 2), activation='relu', padding='same')(up9)\n    merge9 = Concatenate()([conv1, up9])\n    conv9 = Conv3D(64, (3, 3, 3), activation='relu', padding='same')(merge9)\n    conv9 = Conv3D(64, (3, 3, 3), activation='relu', padding='same')(conv9)\n\n    # Final output layer\n    outputs = Conv3D(num_classes, (1, 1, 1), activation='softmax')(conv9)\n\n    # Define the model\n    model = Model(inputs, outputs)\n\n    # Compile the model\n    model.compile(\n        loss='categorical_crossentropy',\n        optimizer='adam',\n        metrics=['accuracy']\n    )\n\n    return model\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import Conv3D, MaxPooling3D, Conv3DTranspose, Concatenate, Input, Dropout, UpSampling3D, ZeroPadding3D\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import GlobalAveragePooling3D, Dense\n\n\ndef create_unet_model(input_shape, num_classes):\n    inputs = Input(shape=input_shape)  # Input shape: (256, 256, 50, 1) for 3D grayscale\n\n    # Encoding Path (Downsampling)\n    conv1 = Conv3D(64, (3, 3, 3), activation='relu', padding='same')(inputs)\n    conv1 = Conv3D(64, (3, 3, 3), activation='relu', padding='same')(conv1)\n    pool1 = MaxPooling3D(pool_size=(2, 2, 2))(conv1)\n\n    conv2 = Conv3D(128, (3, 3, 3), activation='relu', padding='same')(pool1)\n    conv2 = Conv3D(128, (3, 3, 3), activation='relu', padding='same')(conv2)\n    pool2 = MaxPooling3D(pool_size=(2, 2, 2))(conv2)\n\n    conv3 = Conv3D(256, (3, 3, 3), activation='relu', padding='same')(pool2)\n    conv3 = Conv3D(256, (3, 3, 3), activation='relu', padding='same')(conv3)\n    pool3 = MaxPooling3D(pool_size=(2, 2, 2))(conv3)\n\n    conv4 = Conv3D(512, (3, 3, 3), activation='relu', padding='same')(pool3)\n    conv4 = Conv3D(512, (3, 3, 3), activation='relu', padding='same')(conv4)\n    pool4 = MaxPooling3D(pool_size=(2, 2, 2))(conv4)\n\n    conv5 = Conv3D(1024, (3, 3, 3), activation='relu', padding='same')(pool4)\n    conv5 = Conv3D(1024, (3, 3, 3), activation='relu', padding='same')(conv5)\n    drop5 = Dropout(0.5)(conv5)\n\n    # Decoding Path (Upsampling)\n    up6 = UpSampling3D(size=(2, 2, 2))(drop5)\n    up6 = Conv3D(512, (2, 2, 2), activation='relu', padding='same')(up6)\n    merge6 = Concatenate()([conv4, up6])\n    conv6 = Conv3D(512, (3, 3, 3), activation='relu', padding='same')(merge6)\n    conv6 = Conv3D(512, (3, 3, 3), activation='relu', padding='same')(conv6)\n\n    up7 = UpSampling3D(size=(2, 2, 2))(conv6)\n    up7 = Conv3D(256, (2, 2, 2), activation='relu', padding='same')(up7)\n    merge7 = Concatenate()([conv3, up7])\n    conv7 = Conv3D(256, (3, 3, 3), activation='relu', padding='same')(merge7)\n    conv7 = Conv3D(256, (3, 3, 3), activation='relu', padding='same')(conv7)\n\n    up8 = UpSampling3D(size=(2, 2, 2))(conv7)\n    up8 = Conv3D(128, (2, 2, 2), activation='relu', padding='same')(up8)\n    up8 = ZeroPadding3D(padding=((0, 0), (0, 0), (0, 1)))(up8)  # Adjust depth\n    merge8 = Concatenate()([conv2, up8])\n    conv8 = Conv3D(128, (3, 3, 3), activation='relu', padding='same')(merge8)\n    conv8 = Conv3D(128, (3, 3, 3), activation='relu', padding='same')(conv8)\n\n    up9 = UpSampling3D(size=(2, 2, 2))(conv8)\n    up9 = Conv3D(64, (2, 2, 2), activation='relu', padding='same')(up9)\n    merge9 = Concatenate()([conv1, up9])\n    conv9 = Conv3D(64, (3, 3, 3), activation='relu', padding='same')(merge9)\n    conv9 = Conv3D(64, (3, 3, 3), activation='relu', padding='same')(conv9)\n\n    # Final output layer\n    outputs = Conv3D(num_classes, (1, 1, 1), activation='softmax')(conv9)\n    outputs = GlobalAveragePooling3D()(outputs)  # Pool to a single value per class\n    outputs = Dense(num_classes, activation='softmax')(outputs)\n\n    # Define the model\n    model = Model(inputs, outputs)\n    \n    # Use sparse_categorical_crossentropy since we expect integer labels for each voxel\n    model.compile(\n    loss='sparse_categorical_crossentropy',\n    optimizer='adam',\n    metrics=['accuracy']\n    )\n\n    return model\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from tensorflow.keras.models import Sequential\n# from tensorflow.keras.layers import Conv3D, MaxPooling3D, Flatten, Dense, Dropout, GlobalAveragePooling3D\n\n# def create_cnn_model(input_shape, num_classes):\n#     model = Sequential()\n    \n#     # First Conv3D layer\n#     model.add(Conv3D(32, (3, 3, 3), activation='relu', input_shape=input_shape))\n#     model.add(MaxPooling3D(pool_size=(2, 2, 2)))\n    \n#     # Flatten the 3D output before feeding into a Dense layer\n#     model.add(Flatten())\n#     model.add(Dense(64, activation='relu'))  # Dense layer after the first Conv3D\n    \n#     # Second Conv3D layer\n#     model.add(Conv3D(64, (3, 3, 3), activation='relu'))\n#     model.add(MaxPooling3D(pool_size=(2, 2, 2)))\n    \n#     # Flatten the 3D output before feeding into a Dense layer\n#     model.add(Flatten())\n#     model.add(Dense(128, activation='relu'))  # Dense layer after the second Conv3D\n    \n#     # Third Conv3D layer\n#     model.add(Conv3D(128, (3, 3, 3), activation='relu'))\n#     model.add(MaxPooling3D(pool_size=(2, 2, 2)))\n\n#     # Global average pooling (to reduce dimensions)\n#     model.add(GlobalAveragePooling3D())\n    \n#     # Fully connected layers after all Conv3D layers\n#     model.add(Dense(256, activation='relu'))\n#     model.add(Dropout(0.5))\n#     model.add(Dense(num_classes, activation='softmax'))\n    \n#     model.compile(\n#         loss='categorical_crossentropy',\n#         optimizer='adam',\n#         metrics=['accuracy']\n#     )\n    \n#     return model\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Input, Dense, Conv2D, MaxPooling2D, Dropout, Flatten\nfrom tensorflow.keras.metrics import categorical_accuracy","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import layers, models\nfrom tensorflow.keras.applications import ResNet50\nfrom tensorflow.keras.layers import Conv3D, MaxPooling3D, Flatten, Dense, Dropout, GlobalAveragePooling2D, Input\nfrom tensorflow.keras.applications import ResNet50\nfrom tensorflow.keras import backend as K\n\ndef build_resnet18_base(input_shape):\n    # Load ResNet50 as a base model (since ResNet18 is not directly available)\n    base_model = ResNet50(\n        include_top=False,   # Exclude the fully connected layer\n        input_shape=input_shape[:2] + (3,),  # Using only the 2D input with 3 channels for ResNet50\n        weights='imagenet'   # Pre-trained weights on ImageNet\n    )\n    \n    return base_model\n\ndef model(input_shape, num_classes):\n    # Build ResNet50 base model\n    resnet_base = build_resnet18_base(input_shape)\n    \n    # Input layer\n    inputs = Input(shape=input_shape)\n    \n    # Apply 3D convolution on the input data to convert it to a 2D ResNet-compatible shape\n    x = Conv3D(3, (3, 3, 3), activation='relu', padding='same')(inputs)\n    x = MaxPooling3D(pool_size=(1, 1, input_shape[2]))(x)  # Reduce the depth to 1 for ResNet input\n\n    # Reshape for ResNet compatibility\n    x = layers.Reshape(target_shape=(input_shape[0], input_shape[1], 3))(x)\n    \n    # Pass through ResNet backbone\n    x = resnet_base(x)\n    \n    # Global average pooling and fully connected layers\n    x = GlobalAveragePooling2D()(x)\n    x = Dense(256, activation='relu')(x)\n    x = Dropout(0.2)(x)\n    \n    # Output layer\n    outputs = Dense(num_classes, activation='softmax')(x)\n    \n    model = models.Model(inputs=inputs, outputs=outputs)\n    \n    model.compile(\n        loss='categorical_crossentropy',\n        optimizer='adam',\n        metrics=['accuracy']\n    )\n    \n    return model\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create and train the model\ninput_shape = (img_width, img_height, depth, 1)  # Adjusted for 3D input\nnum_classes = 25\nmodel = model(input_shape, num_classes)\n\n# Train the model\nmodel.fit(X_train, y_train, epochs=5, batch_size=8, validation_data=(X_test, y_test))\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}