{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":71549,"databundleVersionId":8561470,"sourceType":"competition"}],"dockerImageVersionId":30732,"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 os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r /kaggle/input/python-packages /kaggle/working\n!pip install -q /kaggle/working/python-packages/pylibjpeg_libjpeg-1.3.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl\n!pip install -q /kaggle/working/python-packages/pylibjpeg_openjpeg-1.2.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl\n!pip install -q /kaggle/working/python-packages/pylibjpeg_rle-1.3.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl\n!pip install -q /kaggle/working/python-packages/iopath-0.1.9-py3-none-any.whl\n!pip install -q /kaggle/working/python-packages/av-9.2.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl\n!pip install -q /kaggle/working/python-packages/fvcore-0.1.5.post20220512/\n!pip install -q /kaggle/working/python-packages/parameterized-0.8.1-py2.py3-none-any.whl\n!pip install -q /kaggle/working/python-packages/pytorchvideo-0.1.5/\n!pip install -q /kaggle/working/python-packages/timm-0.6.7-py3-none-any.whl\n!pip install -q /kaggle/working/python-packages/antlr4-python3-runtime-4.9.3/\n!pip install -q /kaggle/working/python-packages/omegaconf-2.2.2-py3-none-any.whl\n!pip install -q /kaggle/working/python-packages/monai-0.8.1-202202162213-py3-none-any.whl\n\n!cp /kaggle/input/gdcm-conda-install/gdcm.tar /kaggle/working/\n!tar -xzvf gdcm.tar\n!conda install --offline /kaggle/working/gdcm/gdcm-2.8.9-py37h71b2a6d_0.tar.bz2","metadata":{"execution":{"iopub.status.busy":"2024-07-06T11:24:10.900323Z","iopub.execute_input":"2024-07-06T11:24:10.900787Z","iopub.status.idle":"2024-07-06T11:25:02.387840Z","shell.execute_reply.started":"2024-07-06T11:24:10.900751Z","shell.execute_reply":"2024-07-06T11:25:02.386277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport shutil\nimport tempfile\nimport matplotlib.pyplot as plt\nimport PIL\nimport torch\nfrom pathlib import Path\nimport glob\nfrom torch.utils.tensorboard import SummaryWriter\nimport numpy as np\nimport pandas as pd\nfrom sklearn.metrics import classification_report\n\nfrom monai.apps import download_and_extract\nfrom monai.config import print_config\nfrom monai.data import decollate_batch, DataLoader\nfrom monai.metrics import ROCAUCMetric\nfrom monai.networks.nets import DenseNet121\nfrom monai.transforms import (\n    Activations,\n    EnsureChannelFirst,\n    AsDiscrete,\n    Compose,\n    LoadImage,\n    RandFlip,\n    RandRotate,\n    RandZoom,\n    ScaleIntensity,\n)\nfrom monai.utils import set_determinism\n\nprint_config()","metadata":{"execution":{"iopub.status.busy":"2024-07-06T11:59:22.867890Z","iopub.execute_input":"2024-07-06T11:59:22.868290Z","iopub.status.idle":"2024-07-06T11:59:22.908627Z","shell.execute_reply.started":"2024-07-06T11:59:22.868258Z","shell.execute_reply":"2024-07-06T11:59:22.907306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"directory = os.environ.get(\"/kaggle/working\")\nif directory is not None:\n    os.makedirs(directory, exist_ok=True)\nroot_dir = tempfile.mkdtemp() if directory is None else directory\nprint(root_dir)","metadata":{"execution":{"iopub.status.busy":"2024-07-06T11:26:57.047312Z","iopub.execute_input":"2024-07-06T11:26:57.047762Z","iopub.status.idle":"2024-07-06T11:26:57.055358Z","shell.execute_reply.started":"2024-07-06T11:26:57.047728Z","shell.execute_reply":"2024-07-06T11:26:57.054006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"set_determinism(seed=0)","metadata":{"execution":{"iopub.status.busy":"2024-07-06T11:27:30.080465Z","iopub.execute_input":"2024-07-06T11:27:30.081703Z","iopub.status.idle":"2024-07-06T11:27:30.091057Z","shell.execute_reply.started":"2024-07-06T11:27:30.081656Z","shell.execute_reply":"2024-07-06T11:27:30.089835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ROOT = Path('/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/')\ndf = pd.read_csv('/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train.csv')\ndf = df.loc[:,['study_id', 'spinal_canal_stenosis_l1_l2']]\n# df = df[df['spinal_canal_stenosis_l1_l2'] == 'Severe']\ndf1 = pd.read_csv('/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_series_descriptions.csv')\ndf1 = df1[df1['series_description'].str.contains('Sagittal')]\ndf2 = df1.merge(df, on=['study_id'])\n# spinal_canal_stenosis\nprint(df)\nprint(df1)\nprint(df2)\n# print(glob.glob(str(ROOT/'train_images'/'*')))","metadata":{"execution":{"iopub.status.busy":"2024-07-06T12:08:10.155897Z","iopub.execute_input":"2024-07-06T12:08:10.156312Z","iopub.status.idle":"2024-07-06T12:08:10.204160Z","shell.execute_reply.started":"2024-07-06T12:08:10.156278Z","shell.execute_reply":"2024-07-06T12:08:10.202821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = ['Normal/Mild', 'Moderate', 'Severe']\nnum_class = len(class_names)\nROOT = Path('/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/')\nTRAIN_IMAGE_PATH = ROOT / 'train_images'\nimage_files = [[],[],[]]\nfor idx,i in enumerate(class_names):\n    class_df = df2[df2['spinal_canal_stenosis_l1_l2'] == i]\n    for j in range(class_df.shape[0]):\n        image_path = TRAIN_IMAGE_PATH / str(class_df.iloc[j,0]) / str(class_df.iloc[j,1]) / '*'\n        image_files[idx] += glob.glob(str(image_path))\nprint(image_files)","metadata":{"execution":{"iopub.status.busy":"2024-07-06T12:25:01.213224Z","iopub.execute_input":"2024-07-06T12:25:01.214372Z","iopub.status.idle":"2024-07-06T12:25:22.643759Z","shell.execute_reply.started":"2024-07-06T12:25:01.214325Z","shell.execute_reply":"2024-07-06T12:25:22.641795Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimage_files = [\n    [os.path.join(data_dir, class_names[i], x) for x in os.listdir(os.path.join(data_dir, class_names[i]))]\n    for i in range(num_class)\n]\nnum_each = [len(image_files[i]) for i in range(num_class)]\nimage_files_list = []\nimage_class = []\nfor i in range(num_class):\n    image_files_list.extend(image_files[i])\n    image_class.extend([i] * num_each[i])\nnum_total = len(image_class)\nimage_width, image_height = PIL.Image.open(image_files_list[0]).size\n\nprint(f\"Total image count: {num_total}\")\nprint(f\"Image dimensions: {image_width} x {image_height}\")\nprint(f\"Label names: {class_names}\")\nprint(f\"Label counts: {num_each}\")","metadata":{},"execution_count":null,"outputs":[]}]}