{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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"},"colab":{"provenance":[],"authorship_tag":"ABX9TyOlR30+wmJAl4MZiYfsjaI/"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":71549,"databundleVersionId":8561470,"sourceType":"competition"}],"dockerImageVersionId":30761,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\ntrain = pd.read_csv('/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train.csv')\ntrain.head(2)\n\nimport warnings\n\n# Disable all warnings\nwarnings.filterwarnings('ignore')","metadata":{"executionInfo":{"elapsed":1847,"status":"ok","timestamp":1727077006182,"user":{"displayName":"WANGA PETER OTIENO PA106/G/15065/21","userId":"04631351876979831553"},"user_tz":-180},"id":"1m6tCnDT_Gz4","outputId":"a6d8e53b-cdfb-47c6-db07-d30d815e69d5","execution":{"iopub.status.busy":"2025-02-03T11:18:28.366454Z","iopub.execute_input":"2025-02-03T11:18:28.366698Z","iopub.status.idle":"2025-02-03T11:18:28.724297Z","shell.execute_reply.started":"2025-02-03T11:18:28.366671Z","shell.execute_reply":"2025-02-03T11:18:28.723414Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_submission=pd.read_csv('/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/sample_submission.csv')\nsample_submission.head(2)","metadata":{"executionInfo":{"elapsed":432,"status":"ok","timestamp":1727077009625,"user":{"displayName":"WANGA PETER OTIENO PA106/G/15065/21","userId":"04631351876979831553"},"user_tz":-180},"id":"NM80PvORikYs","outputId":"85713999-7448-45d1-ba62-58ff2b51c7b1","execution":{"iopub.status.busy":"2025-02-03T11:18:28.725394Z","iopub.execute_input":"2025-02-03T11:18:28.725662Z","iopub.status.idle":"2025-02-03T11:18:28.749037Z","shell.execute_reply.started":"2025-02-03T11:18:28.725636Z","shell.execute_reply":"2025-02-03T11:18:28.748086Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_label_coordinates=pd.read_csv('/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_label_coordinates.csv')\ntrain_label_coordinates.head()","metadata":{"executionInfo":{"elapsed":18,"status":"ok","timestamp":1727077011504,"user":{"displayName":"WANGA PETER OTIENO PA106/G/15065/21","userId":"04631351876979831553"},"user_tz":-180},"id":"5wncr19mi5li","outputId":"9ee37f70-d385-4eeb-d337-3689132d73b0","execution":{"iopub.status.busy":"2025-02-03T11:18:28.750831Z","iopub.execute_input":"2025-02-03T11:18:28.751218Z","iopub.status.idle":"2025-02-03T11:18:28.898747Z","shell.execute_reply.started":"2025-02-03T11:18:28.751189Z","shell.execute_reply":"2025-02-03T11:18:28.897726Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_series_descriptions=pd.read_csv('/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_series_descriptions.csv')\ntrain_series_descriptions.head(2)","metadata":{"executionInfo":{"elapsed":462,"status":"ok","timestamp":1727077013665,"user":{"displayName":"WANGA PETER OTIENO PA106/G/15065/21","userId":"04631351876979831553"},"user_tz":-180},"id":"HkkdITW4bAB1","outputId":"2ca85fb9-0c6b-4c10-e3db-6c7b332b1a5a","execution":{"iopub.status.busy":"2025-02-03T11:18:28.899859Z","iopub.execute_input":"2025-02-03T11:18:28.900172Z","iopub.status.idle":"2025-02-03T11:18:28.920344Z","shell.execute_reply.started":"2025-02-03T11:18:28.900145Z","shell.execute_reply":"2025-02-03T11:18:28.919164Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_label_coordinates['label']=train_label_coordinates['condition'].str.lower().str.replace(' ','_')+'_'+train_label_coordinates['level'].str.lower().str.replace(\"/\", \"_\").str.replace(\" \", \"_\")\ntrain_label_coordinates.head(2)","metadata":{"executionInfo":{"elapsed":863,"status":"ok","timestamp":1727077016365,"user":{"displayName":"WANGA PETER OTIENO PA106/G/15065/21","userId":"04631351876979831553"},"user_tz":-180},"id":"z2ekpcGFop8U","outputId":"d78f38b9-7910-4e9a-9069-2d67a99d5b64","execution":{"iopub.status.busy":"2025-02-03T11:18:28.921631Z","iopub.execute_input":"2025-02-03T11:18:28.921987Z","iopub.status.idle":"2025-02-03T11:18:29.006205Z","shell.execute_reply.started":"2025-02-03T11:18:28.921932Z","shell.execute_reply":"2025-02-03T11:18:29.005301Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"final_df = pd.merge(train_label_coordinates, train, on='study_id', how='left')\n\ndef get_case(row):\n    # Check if the label in the final_df matches any of the columns in df\n    if row['label'] in train.columns:\n        return row[row['label']]\n    else:\n        return None\n\n# Apply the get_case function to create the \"case\" column\nfinal_df['case'] = final_df.apply(get_case, axis=1)\n\nfinal_df = final_df[['study_id', 'series_id', 'instance_number', 'label', 'x', 'y', 'case']]\nfinal_df = pd.merge(final_df, train_series_descriptions[['series_id', 'series_description']],\n                    on='series_id', how='left')","metadata":{"executionInfo":{"elapsed":837,"status":"ok","timestamp":1727077019551,"user":{"displayName":"WANGA PETER OTIENO PA106/G/15065/21","userId":"04631351876979831553"},"user_tz":-180},"id":"T9op7Neece3f","execution":{"iopub.status.busy":"2025-02-03T11:18:29.007421Z","iopub.execute_input":"2025-02-03T11:18:29.007739Z","iopub.status.idle":"2025-02-03T11:18:29.634087Z","shell.execute_reply.started":"2025-02-03T11:18:29.007710Z","shell.execute_reply":"2025-02-03T11:18:29.633276Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"final_df.head(2)","metadata":{"executionInfo":{"elapsed":427,"status":"ok","timestamp":1727077022263,"user":{"displayName":"WANGA PETER OTIENO PA106/G/15065/21","userId":"04631351876979831553"},"user_tz":-180},"id":"-ansgnMrdUTI","outputId":"69d740ec-be1c-4ba1-95f8-cfb38e59cc38","execution":{"iopub.status.busy":"2025-02-03T11:18:29.635105Z","iopub.execute_input":"2025-02-03T11:18:29.635379Z","iopub.status.idle":"2025-02-03T11:18:29.645606Z","shell.execute_reply.started":"2025-02-03T11:18:29.635346Z","shell.execute_reply":"2025-02-03T11:18:29.644522Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"final_df['case'].unique()","metadata":{"id":"bdv4LDD0k0Ih","executionInfo":{"status":"ok","timestamp":1727077024247,"user_tz":-180,"elapsed":7,"user":{"displayName":"WANGA PETER OTIENO PA106/G/15065/21","userId":"04631351876979831553"}},"outputId":"adc93d28-2212-401c-b27b-4fcbfda5e2ae","execution":{"iopub.status.busy":"2025-02-03T11:18:29.647087Z","iopub.execute_input":"2025-02-03T11:18:29.647466Z","iopub.status.idle":"2025-02-03T11:18:29.660756Z","shell.execute_reply.started":"2025-02-03T11:18:29.647426Z","shell.execute_reply":"2025-02-03T11:18:29.659997Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"diagnoses=final_df['label'].unique()[:].tolist()\ndiagnoses","metadata":{"executionInfo":{"elapsed":406,"status":"ok","timestamp":1727077026541,"user":{"displayName":"WANGA PETER OTIENO PA106/G/15065/21","userId":"04631351876979831553"},"user_tz":-180},"id":"79FutWuUdpkf","outputId":"dc224f88-6ac0-4c93-e346-3b9e652bfb3f","execution":{"iopub.status.busy":"2025-02-03T11:18:29.663873Z","iopub.execute_input":"2025-02-03T11:18:29.664418Z","iopub.status.idle":"2025-02-03T11:18:29.679310Z","shell.execute_reply.started":"2025-02-03T11:18:29.664378Z","shell.execute_reply":"2025-02-03T11:18:29.678518Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_list=[]\nfor diagnosis in diagnoses:\n  df=final_df[final_df['label']==diagnosis]\n  df_list.append(df)","metadata":{"executionInfo":{"elapsed":942,"status":"ok","timestamp":1727077029365,"user":{"displayName":"WANGA PETER OTIENO PA106/G/15065/21","userId":"04631351876979831553"},"user_tz":-180},"id":"mXTVZgEdeez-","execution":{"iopub.status.busy":"2025-02-03T11:18:29.680360Z","iopub.execute_input":"2025-02-03T11:18:29.680655Z","iopub.status.idle":"2025-02-03T11:18:29.790692Z","shell.execute_reply.started":"2025-02-03T11:18:29.680615Z","shell.execute_reply":"2025-02-03T11:18:29.789893Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(df_list)","metadata":{"executionInfo":{"elapsed":429,"status":"ok","timestamp":1727077033916,"user":{"displayName":"WANGA PETER OTIENO PA106/G/15065/21","userId":"04631351876979831553"},"user_tz":-180},"id":"7669ughdfGly","outputId":"c729bf58-f165-4326-d250-5aa82568f76f","execution":{"iopub.status.busy":"2025-02-03T11:18:29.791911Z","iopub.execute_input":"2025-02-03T11:18:29.792281Z","iopub.status.idle":"2025-02-03T11:18:29.798394Z","shell.execute_reply.started":"2025-02-03T11:18:29.792241Z","shell.execute_reply":"2025-02-03T11:18:29.797590Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nimport tensorflow_io as tfio\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications import VGG16\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, Dropout\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report, confusion_matrix","metadata":{"executionInfo":{"elapsed":18483,"status":"ok","timestamp":1727077058448,"user":{"displayName":"WANGA PETER OTIENO PA106/G/15065/21","userId":"04631351876979831553"},"user_tz":-180},"id":"W3bqt22Wtyhw","execution":{"iopub.status.busy":"2025-02-03T11:18:29.799466Z","iopub.execute_input":"2025-02-03T11:18:29.799760Z","iopub.status.idle":"2025-02-03T11:18:42.550644Z","shell.execute_reply.started":"2025-02-03T11:18:29.799732Z","shell.execute_reply":"2025-02-03T11:18:42.549724Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"main_directory = '/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_images'\nimage_paths = []\ndiag_list=[]\nlist_labels=[]\nlist_of_label=[]\nfor i in range(len(df_list)):\n  for index, row in df_list[i].iterrows():\n    study_id = row['study_id']\n    image_path = os.path.join(main_directory,  str(study_id), str(row['series_id']), f\"{row['instance_number']}.dcm\")\n    label = row['case']\n    list_of_label.append(label)\n    image_paths.append(image_path)\n  diag_list.append(image_paths)\n  list_labels.append(list_of_label)","metadata":{"executionInfo":{"elapsed":11695,"status":"ok","timestamp":1727077072285,"user":{"displayName":"WANGA PETER OTIENO PA106/G/15065/21","userId":"04631351876979831553"},"user_tz":-180},"id":"ZnN44J0Zfyqn","execution":{"iopub.status.busy":"2025-02-03T11:52:06.693728Z","iopub.execute_input":"2025-02-03T11:52:06.694537Z","iopub.status.idle":"2025-02-03T11:52:09.183163Z","shell.execute_reply.started":"2025-02-03T11:52:06.694499Z","shell.execute_reply":"2025-02-03T11:52:09.182494Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pydicom\nn_images = len(diag_list)\nn_cols = 4  # Set the number of columns to 4\nn_rows = (n_images + n_cols - 1) // n_cols\nfig, axes = plt.subplots(n_rows, n_cols, figsize=(20, 5 * n_rows))  # Adjust figsize for better spacing\naxes = axes.flatten()\nfor i in range(len(diag_list[0][:25])):\n  dcm=pydicom.dcmread(diag_list[0][i])\n  image = dcm.pixel_array\n  print('image shape:',image.shape)\n  axes[i].imshow(image, cmap='gray')\n  axes[i].set_title(f\"Image {i+1}\")\n  axes[i].axis('off')\nplt.tight_layout()\nplt.show()","metadata":{"id":"sNA_KGh_n8ni","outputId":"2ad11d20-3e17-4dfb-f154-346b73ed9f0d","executionInfo":{"status":"ok","timestamp":1727077110620,"user_tz":-180,"elapsed":27987,"user":{"displayName":"WANGA PETER OTIENO PA106/G/15065/21","userId":"04631351876979831553"}},"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T11:18:45.181731Z","iopub.execute_input":"2025-02-03T11:18:45.182140Z","iopub.status.idle":"2025-02-03T11:18:50.606624Z","shell.execute_reply.started":"2025-02-03T11:18:45.182095Z","shell.execute_reply":"2025-02-03T11:18:50.605708Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pydicom\nimport tempfile\n\ndef process_dicom(file_name_tensor):\n    try:\n        # Convert TensorFlow tensor to a string\n        file_name = file_name_tensor.numpy().decode(\"utf-8\")\n\n        # Read the DICOM file\n        dicom = pydicom.dcmread(file_name, force=True)\n\n        # Validate DICOM file (check pixel data availability)\n        if not hasattr(dicom, \"PixelData\"):\n            raise ValueError(f\"File {file_name} is missing pixel data. Skipping.\")\n\n        # Check and handle missing Group Length (optional)\n        if (0x0002, 0x0000) not in dicom.file_meta:\n            dicom.file_meta[(0x0002, 0x0000)] = pydicom.DataElement((0x0002, 0x0000), \"UL\", 0)\n\n        # Save the modified DICOM to a temporary file\n        temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=\".dcm\")\n        dicom.save_as(temp_file.name)\n\n        # Return the file path\n        return temp_file.name.encode(\"utf-8\")  # Return as bytes for TensorFlow compatibility\n\n    except Exception as e:\n        # Log the error and raise an exception for further handling\n        raise ValueError(f\"Error processing DICOM file: {e}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T11:18:50.608063Z","iopub.execute_input":"2025-02-03T11:18:50.608341Z","iopub.status.idle":"2025-02-03T11:18:50.617115Z","shell.execute_reply.started":"2025-02-03T11:18:50.608314Z","shell.execute_reply":"2025-02-03T11:18:50.616378Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\ndef load_image(file_name, label):\n    file_name = tf.py_function(func=process_dicom, inp=[file_name], Tout=tf.string)\n\n    # Skip corrupt files\n    if tf.strings.length(file_name) == 0:\n        return tf.zeros([128, 128, 1], dtype=tf.float32), tf.zeros([3], dtype=tf.float32)\n\n    raw = tf.io.read_file(file_name)\n    dicom_array = tfio.image.decode_dicom_image(raw, dtype=tf.uint16)\n    tensor = tf.cast(dicom_array, tf.float32)\n    \n    # Check if the tensor has the correct number of dimensions (3D: height, width, channels)\n    tensor_shape = tf.shape(tensor)\n    if len(tensor_shape) != 3:  # It should be 3D (height, width, channels)\n        print(f\"❌ Skipping file due to incorrect shape: {tensor_shape}\")\n        return tf.zeros([128, 128, 1], dtype=tf.float32), tf.zeros([3], dtype=tf.float32)\n\n    tensor = tf.image.resize(tensor, [128, 128])  # Resize only if valid shape\n    tensor = tf.reshape(tensor, [128, 128, 1])  # Ensure it's 3D (height, width, channels)\n    tensor = tf.cast(tensor, tf.float32) / 255.0  # Normalize\n\n    target = tf.keras.utils.to_categorical(label, num_classes=3)\n    label = tf.convert_to_tensor(target, dtype=tf.float32)\n\n    return tensor, label\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T11:18:50.618117Z","iopub.execute_input":"2025-02-03T11:18:50.618426Z","iopub.status.idle":"2025-02-03T11:18:50.637453Z","shell.execute_reply.started":"2025-02-03T11:18:50.618394Z","shell.execute_reply":"2025-02-03T11:18:50.636614Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder","metadata":{"id":"qnVTSj9c0jwy","executionInfo":{"status":"ok","timestamp":1727077124369,"user_tz":-180,"elapsed":498,"user":{"displayName":"WANGA PETER OTIENO PA106/G/15065/21","userId":"04631351876979831553"}},"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T11:18:50.638590Z","iopub.execute_input":"2025-02-03T11:18:50.638875Z","iopub.status.idle":"2025-02-03T11:18:50.649918Z","shell.execute_reply.started":"2025-02-03T11:18:50.638848Z","shell.execute_reply":"2025-02-03T11:18:50.648985Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport re\ndf_ylabel=pd.DataFrame(list_labels[0],columns=['case'])\npattern = r'[^\\w\\s]'\ndf_ylabel = df_ylabel.replace(pattern,'', regex=True)\ndf_ylabel.head(2)","metadata":{"executionInfo":{"elapsed":10,"status":"ok","timestamp":1727077125733,"user":{"displayName":"WANGA PETER OTIENO PA106/G/15065/21","userId":"04631351876979831553"},"user_tz":-180},"id":"Kea_J0IOpmLF","outputId":"8dd12eed-8c49-4e5a-ad02-0391de40caff","trusted":true,"execution":{"iopub.status.busy":"2025-02-03T11:18:50.650863Z","iopub.execute_input":"2025-02-03T11:18:50.651187Z","iopub.status.idle":"2025-02-03T11:18:50.701216Z","shell.execute_reply.started":"2025-02-03T11:18:50.651162Z","shell.execute_reply":"2025-02-03T11:18:50.700308Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"label_encoder = LabelEncoder()\ndf_xtrain = pd.DataFrame(diag_list[0], columns=['images'])\n\ndf_train=pd.concat([df_xtrain,df_ylabel],axis=1)\ndf_train=df_train.dropna()\ndf_train['case']=label_encoder.fit_transform(df_train['case'])\ndf_train.head(2)","metadata":{"id":"xX7dB4nuq6k9","executionInfo":{"status":"ok","timestamp":1727077127846,"user_tz":-180,"elapsed":438,"user":{"displayName":"WANGA PETER OTIENO PA106/G/15065/21","userId":"04631351876979831553"}},"outputId":"cafec2dc-b819-4430-a493-47a1f72e4355","trusted":true,"execution":{"iopub.status.busy":"2025-02-03T11:18:50.702514Z","iopub.execute_input":"2025-02-03T11:18:50.702795Z","iopub.status.idle":"2025-02-03T11:18:50.733166Z","shell.execute_reply.started":"2025-02-03T11:18:50.702769Z","shell.execute_reply":"2025-02-03T11:18:50.732271Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train['case'].unique()","metadata":{"id":"9WKKBBZodz86","executionInfo":{"status":"ok","timestamp":1727077128414,"user_tz":-180,"elapsed":7,"user":{"displayName":"WANGA PETER OTIENO PA106/G/15065/21","userId":"04631351876979831553"}},"outputId":"8ca5453e-fb22-4ff3-f4ab-69305759c845","trusted":true,"execution":{"iopub.status.busy":"2025-02-03T11:18:50.734315Z","iopub.execute_input":"2025-02-03T11:18:50.734593Z","iopub.status.idle":"2025-02-03T11:18:50.740345Z","shell.execute_reply.started":"2025-02-03T11:18:50.734566Z","shell.execute_reply":"2025-02-03T11:18:50.739467Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T11:18:50.741492Z","iopub.execute_input":"2025-02-03T11:18:50.741780Z","iopub.status.idle":"2025-02-03T11:18:50.748986Z","shell.execute_reply.started":"2025-02-03T11:18:50.741753Z","shell.execute_reply":"2025-02-03T11:18:50.748196Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"file_names = tf.cast(df_train['images'],dtype=tf.string)\nlabels = tf.cast(df_train['case'],dtype=tf.int64)\ndataset = tf.data.Dataset.from_tensor_slices((file_names, labels))","metadata":{"id":"rFiI6l_bkhGB","executionInfo":{"status":"ok","timestamp":1727077129811,"user_tz":-180,"elapsed":19,"user":{"displayName":"WANGA PETER OTIENO PA106/G/15065/21","userId":"04631351876979831553"}},"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T11:18:50.750019Z","iopub.execute_input":"2025-02-03T11:18:50.750289Z","iopub.status.idle":"2025-02-03T11:18:51.585804Z","shell.execute_reply.started":"2025-02-03T11:18:50.750265Z","shell.execute_reply":"2025-02-03T11:18:51.584999Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for elements in dataset.take(6):\n  print(elements[0].numpy(),elements[1].numpy())","metadata":{"id":"voZyx1S3JMHY","executionInfo":{"status":"ok","timestamp":1727077131395,"user_tz":-180,"elapsed":5,"user":{"displayName":"WANGA PETER OTIENO PA106/G/15065/21","userId":"04631351876979831553"}},"outputId":"ca28e8c3-074c-4100-ad8d-7f7bcd7eec1f","trusted":true,"execution":{"iopub.status.busy":"2025-02-03T11:18:51.587279Z","iopub.execute_input":"2025-02-03T11:18:51.587648Z","iopub.status.idle":"2025-02-03T11:18:51.627430Z","shell.execute_reply.started":"2025-02-03T11:18:51.587621Z","shell.execute_reply":"2025-02-03T11:18:51.626648Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dataset=dataset.map(lambda x,y:load_image(x,y))","metadata":{"id":"qvIRfvtpK-9t","executionInfo":{"status":"ok","timestamp":1727077134398,"user_tz":-180,"elapsed":1383,"user":{"displayName":"WANGA PETER OTIENO PA106/G/15065/21","userId":"04631351876979831553"}},"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T11:18:51.628446Z","iopub.execute_input":"2025-02-03T11:18:51.628702Z","iopub.status.idle":"2025-02-03T11:18:52.305266Z","shell.execute_reply.started":"2025-02-03T11:18:51.628678Z","shell.execute_reply":"2025-02-03T11:18:52.304376Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for elements in dataset.take(6):\n  print(\"image shape:\",elements[0].shape,\"labels shape:\",elements[1].shape)","metadata":{"executionInfo":{"elapsed":6,"status":"ok","timestamp":1727077134400,"user":{"displayName":"WANGA PETER OTIENO PA106/G/15065/21","userId":"04631351876979831553"},"user_tz":-180},"id":"EfFd3NJGyX6D","outputId":"e93c853d-5c10-4df5-bb3b-e2504bc3b33b","trusted":true,"execution":{"iopub.status.busy":"2025-02-03T11:18:52.306513Z","iopub.execute_input":"2025-02-03T11:18:52.306778Z","iopub.status.idle":"2025-02-03T11:18:52.364477Z","shell.execute_reply.started":"2025-02-03T11:18:52.306753Z","shell.execute_reply":"2025-02-03T11:18:52.363680Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"BUFFER_SIZE = 1000\nBATCH_SIZE = 16\nNUM_EPOCHS = 20","metadata":{"id":"5eeysjI2GREt","executionInfo":{"status":"ok","timestamp":1727077144395,"user_tz":-180,"elapsed":405,"user":{"displayName":"WANGA PETER OTIENO PA106/G/15065/21","userId":"04631351876979831553"}},"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T11:18:52.365454Z","iopub.execute_input":"2025-02-03T11:18:52.365711Z","iopub.status.idle":"2025-02-03T11:18:52.369332Z","shell.execute_reply.started":"2025-02-03T11:18:52.365686Z","shell.execute_reply":"2025-02-03T11:18:52.368483Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_size = int(0.8 * 48656)  # Adjust the split ratio\nval_size = 38924 - train_size\n\nx_train = dataset.take(train_size).batch(BATCH_SIZE)\nx_val = dataset.skip(train_size).batch(BATCH_SIZE)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T11:18:52.372974Z","iopub.execute_input":"2025-02-03T11:18:52.373270Z","iopub.status.idle":"2025-02-03T11:18:52.391212Z","shell.execute_reply.started":"2025-02-03T11:18:52.373246Z","shell.execute_reply":"2025-02-03T11:18:52.390329Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for elements in x_train.take(6):\n  print(\"image shape:\",elements[0].shape,\"labels shape:\",elements[1].shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T11:18:52.392234Z","iopub.execute_input":"2025-02-03T11:18:52.392455Z","iopub.status.idle":"2025-02-03T11:18:54.154052Z","shell.execute_reply.started":"2025-02-03T11:18:52.392433Z","shell.execute_reply":"2025-02-03T11:18:54.153244Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, BatchNormalization\n\nmodel = Sequential([\n    Conv2D(32, (3, 3), padding='same', data_format='channels_last',\n           activation='relu', name='conv_1', input_shape=(128, 128, 1)),  # Adjusted input shape\n    MaxPooling2D((2, 2), padding='same'),\n    Dropout(0.5),\n\n    Conv2D(64, (3, 3), padding='same', data_format='channels_last',\n           activation='relu', name='conv_2'),\n    MaxPooling2D((2, 2), padding='same'),\n    Dropout(0.5),\n\n    Conv2D(128, (3, 3), padding='same', data_format='channels_last',\n           activation='relu', name='conv_3'),\n    MaxPooling2D((2, 2), padding='same'),\n    Dropout(0.5),\n\n    Flatten(),\n    Dense(1024, activation='relu', name='dense_1'),\n    Dense(3, activation='sigmoid', name='output')  # Adjusted output activation to sigmoid\n])\n","metadata":{"id":"FYsRfPDEGb4U","executionInfo":{"status":"ok","timestamp":1727077146670,"user_tz":-180,"elapsed":429,"user":{"displayName":"WANGA PETER OTIENO PA106/G/15065/21","userId":"04631351876979831553"}},"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T11:18:54.154897Z","iopub.execute_input":"2025-02-03T11:18:54.155169Z","iopub.status.idle":"2025-02-03T11:18:54.255612Z","shell.execute_reply.started":"2025-02-03T11:18:54.155143Z","shell.execute_reply":"2025-02-03T11:18:54.254120Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.build(input_shape=(None , 128, 128, 1))\nmodel.summary()","metadata":{"id":"Nul1DEx6HdU3","trusted":true,"execution":{"iopub.status.busy":"2025-02-03T11:18:54.256843Z","iopub.execute_input":"2025-02-03T11:18:54.257278Z","iopub.status.idle":"2025-02-03T11:18:54.298184Z","shell.execute_reply.started":"2025-02-03T11:18:54.257233Z","shell.execute_reply":"2025-02-03T11:18:54.297282Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\nmodel.compile(optimizer=Adam(),\n              loss=tf.keras.losses.CategoricalCrossentropy(),\n              metrics=['accuracy'])","metadata":{"id":"hSi4VJ9AIFov","executionInfo":{"status":"ok","timestamp":1727076309269,"user_tz":-180,"elapsed":431,"user":{"displayName":"WANGA PETER OTIENO PA106/G/15065/21","userId":"04631351876979831553"}},"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T11:28:19.669827Z","iopub.execute_input":"2025-02-03T11:28:19.670229Z","iopub.status.idle":"2025-02-03T11:28:19.681877Z","shell.execute_reply.started":"2025-02-03T11:28:19.670191Z","shell.execute_reply":"2025-02-03T11:28:19.681008Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(\n    x_train,  # Training data\n    epochs=1,  # Number of epochs to train the model\n    validation_data=x_val,\n    verbose=1 \n)","metadata":{"id":"eUynDFppHa-h","trusted":true,"execution":{"iopub.status.busy":"2025-02-03T11:29:06.806181Z","iopub.execute_input":"2025-02-03T11:29:06.807064Z","iopub.status.idle":"2025-02-03T11:35:38.891581Z","shell.execute_reply.started":"2025-02-03T11:29:06.807026Z","shell.execute_reply":"2025-02-03T11:35:38.890642Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_directory = '/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/test_images'\ntest_image_paths = []\ntest_diag_list=[]\ntest_list_labels=[]\ntest_list_of_label=[]\nfor i in range(len(df_list)):\n  for index, row in df_list[i].iterrows():\n    study_id = row['study_id']\n    test_image_path = os.path.join(test_directory,  str(study_id), str(row['series_id']), f\"{row['instance_number']}.dcm\")\n    label = row['case']\n    test_list_of_label.append(label)\n    test_image_paths.append(test_image_path)\n  test_diag_list.append(image_paths)\n  test_list_labels.append(test_list_of_label)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T12:02:03.799306Z","iopub.execute_input":"2025-02-03T12:02:03.799662Z","iopub.status.idle":"2025-02-03T12:02:06.355759Z","shell.execute_reply.started":"2025-02-03T12:02:03.799629Z","shell.execute_reply":"2025-02-03T12:02:06.355085Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pydicom\nn_images = len(test_diag_list)\nn_cols = 4  # Set the number of columns to 4\nn_rows = (n_images + n_cols - 1) // n_cols\nfig, axes = plt.subplots(n_rows, n_cols, figsize=(20, 5 * n_rows))  # Adjust figsize for better spacing\naxes = axes.flatten()\nfor i in range(len(test_diag_list[0][:25])):\n  dcm=pydicom.dcmread(test_diag_list[0][i])\n  image = dcm.pixel_array\n  print('image shape:',image.shape)\n  axes[i].imshow(image, cmap='gray')\n  axes[i].set_title(f\"Image {i+1}\")\n  axes[i].axis('off')\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T12:02:06.357564Z","iopub.execute_input":"2025-02-03T12:02:06.358000Z","iopub.status.idle":"2025-02-03T12:02:10.917747Z","shell.execute_reply.started":"2025-02-03T12:02:06.357935Z","shell.execute_reply":"2025-02-03T12:02:10.916860Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport re\ntest_df_ylabel=pd.DataFrame(test_list_labels[0],columns=['case'])\npattern = r'[^\\w\\s]'\ntest_df_ylabel = test_df_ylabel.replace(pattern,'', regex=True)\ntest_df_ylabel.head(2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T11:56:49.664396Z","iopub.execute_input":"2025-02-03T11:56:49.664751Z","iopub.status.idle":"2025-02-03T11:56:49.698786Z","shell.execute_reply.started":"2025-02-03T11:56:49.664721Z","shell.execute_reply":"2025-02-03T11:56:49.697996Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"label_encoder = LabelEncoder()\ntest = pd.DataFrame(test_diag_list[0], columns=['images'])\n\ndf_test=pd.concat([test,test_df_ylabel],axis=1)\ndf_test=df_test.dropna()\ndf_test['case']=label_encoder.fit_transform(df_train['case'])\ndf_test.head(2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T11:59:41.253352Z","iopub.execute_input":"2025-02-03T11:59:41.253695Z","iopub.status.idle":"2025-02-03T11:59:41.280075Z","shell.execute_reply.started":"2025-02-03T11:59:41.253666Z","shell.execute_reply":"2025-02-03T11:59:41.279331Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_test.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T12:00:25.350437Z","iopub.execute_input":"2025-02-03T12:00:25.350778Z","iopub.status.idle":"2025-02-03T12:00:25.356348Z","shell.execute_reply.started":"2025-02-03T12:00:25.350746Z","shell.execute_reply":"2025-02-03T12:00:25.355525Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.save('my_model.h5')\nprint(\"Model saved as my_model.h5\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T11:37:36.360697Z","iopub.execute_input":"2025-02-03T11:37:36.361077Z","iopub.status.idle":"2025-02-03T11:37:37.498621Z","shell.execute_reply.started":"2025-02-03T11:37:36.361045Z","shell.execute_reply":"2025-02-03T11:37:37.497679Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T11:28:14.809509Z","iopub.status.idle":"2025-02-03T11:28:14.809787Z","shell.execute_reply.started":"2025-02-03T11:28:14.809655Z","shell.execute_reply":"2025-02-03T11:28:14.809669Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}