{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":52254,"databundleVersionId":6307054,"sourceType":"competition"},{"sourceId":6211844,"sourceType":"datasetVersion","datasetId":3567114}],"dockerImageVersionId":30558,"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\n#import numpy as np # linear algebra\n#import 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\n#import os\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n#    for filename in filenames:\n#        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-08T17:16:42.214889Z","iopub.execute_input":"2023-09-08T17:16:42.215351Z","iopub.status.idle":"2023-09-08T17:16:42.68959Z","shell.execute_reply.started":"2023-09-08T17:16:42.215316Z","shell.execute_reply":"2023-09-08T17:16:42.688216Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"! pip install -q git+https://github.com/keras-team/keras-cv","metadata":{"execution":{"iopub.status.busy":"2023-09-08T18:50:56.001713Z","iopub.execute_input":"2023-09-08T18:50:56.002089Z","iopub.status.idle":"2023-09-08T18:51:25.285998Z","shell.execute_reply.started":"2023-09-08T18:50:56.002056Z","shell.execute_reply":"2023-09-08T18:51:25.284619Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n# You can use `tensorflow`, `pytorch`, `jax` here\n# KerasCore makes the notebook backend agnostic :)\nos.environ[\"KERAS_BACKEND\"] = \"tensorflow\"\n\nimport keras_cv\nimport keras_core as keras\nfrom keras_core import layers\n\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom matplotlib import pyplot as plt\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2023-09-08T20:32:14.649491Z","iopub.execute_input":"2023-09-08T20:32:14.651009Z","iopub.status.idle":"2023-09-08T20:32:32.96287Z","shell.execute_reply.started":"2023-09-08T20:32:14.650959Z","shell.execute_reply":"2023-09-08T20:32:32.961698Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class Config:\n    SEED = 42\n    IMAGE_SIZE = [256, 256]\n    BATCH_SIZE = 64\n    EPOCHS = 10\n    TARGET_COLS  = [\n        \"bowel_injury\", \"extravasation_injury\",\n        \"kidney_healthy\", \"kidney_low\", \"kidney_high\",\n        \"liver_healthy\", \"liver_low\", \"liver_high\",\n        \"spleen_healthy\", \"spleen_low\", \"spleen_high\",\n    ]\n    AUTOTUNE = tf.data.AUTOTUNE\n\nconfig = Config()","metadata":{"execution":{"iopub.status.busy":"2023-09-08T18:52:09.452749Z","iopub.execute_input":"2023-09-08T18:52:09.453512Z","iopub.status.idle":"2023-09-08T18:52:09.46041Z","shell.execute_reply.started":"2023-09-08T18:52:09.453465Z","shell.execute_reply":"2023-09-08T18:52:09.459314Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"keras.utils.set_random_seed(seed=config.SEED)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T18:53:21.532534Z","iopub.execute_input":"2023-09-08T18:53:21.533571Z","iopub.status.idle":"2023-09-08T18:53:21.538017Z","shell.execute_reply.started":"2023-09-08T18:53:21.533534Z","shell.execute_reply":"2023-09-08T18:53:21.536882Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"BASE_PATH = f\"/kaggle/input/rsna-atd-512x512-png-v2-dataset\"","metadata":{"execution":{"iopub.status.busy":"2023-09-08T18:53:32.449438Z","iopub.execute_input":"2023-09-08T18:53:32.449883Z","iopub.status.idle":"2023-09-08T18:53:32.455296Z","shell.execute_reply.started":"2023-09-08T18:53:32.449851Z","shell.execute_reply":"2023-09-08T18:53:32.454181Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# train\ndf = pd.read_csv(f\"{BASE_PATH}/train.csv\")\n#dataframe[\"image_path\"] = f\"{BASE_PATH}/train_images\"\\\n#                    + \"/\" + dataframe.patient_id.astype(str)\\\n#                    + \"/\" + dataframe.series_id.astype(str)\\\n#                    + \"/\" + dataframe.instance_number.astype(str) +\".png\"\n#dataframe = dataframe.drop_duplicates()\n\ndf.head(2)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T18:55:47.510576Z","iopub.execute_input":"2023-09-08T18:55:47.510996Z","iopub.status.idle":"2023-09-08T18:55:47.575626Z","shell.execute_reply.started":"2023-09-08T18:55:47.510966Z","shell.execute_reply":"2023-09-08T18:55:47.574429Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pydicom","metadata":{"execution":{"iopub.status.busy":"2023-09-08T20:31:46.217529Z","iopub.execute_input":"2023-09-08T20:31:46.217915Z","iopub.status.idle":"2023-09-08T20:31:46.444579Z","shell.execute_reply.started":"2023-09-08T20:31:46.217884Z","shell.execute_reply":"2023-09-08T20:31:46.443435Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def load_scan(path):\n    slices = [pydicom.read_file(os.path.join(path, s)) for s in os.listdir(path)]\n    slices.sort(key = lambda x: float(x.ImagePositionPatient[2]))\n    return slices\n\n\ndef get_pixels_hu(slices):\n    image = np.stack([s.pixel_array for s in slices])\n    # Convert to int16 (from sometimes int16), \n    # should be possible as values should always be low enough (<32k)\n    image = image.astype(np.int16)\n\n    # Convert to Hounsfield units (HU)\n    for slice_number in range(len(slices)):\n        intercept = slices[slice_number].RescaleIntercept\n        slope = slices[slice_number].RescaleSlope\n        \n        if slope != 1:\n            image[slice_number] = slope * image[slice_number].astype(np.float64)\n            image[slice_number] = image[slice_number].astype(np.int16)\n            \n        image[slice_number] += np.int16(intercept)\n    \n    return np.array(image, dtype=np.int16)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-08T20:31:49.421924Z","iopub.execute_input":"2023-09-08T20:31:49.422361Z","iopub.status.idle":"2023-09-08T20:31:49.433127Z","shell.execute_reply.started":"2023-09-08T20:31:49.422328Z","shell.execute_reply":"2023-09-08T20:31:49.431335Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"path_to_patient = \"/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/10004/21057\"\nslices = load_scan(path_to_patient)\nvolume = get_pixels_hu(slices)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-08T20:32:42.436103Z","iopub.execute_input":"2023-09-08T20:32:42.436828Z","iopub.status.idle":"2023-09-08T20:33:06.336054Z","shell.execute_reply.started":"2023-09-08T20:32:42.436789Z","shell.execute_reply":"2023-09-08T20:33:06.334822Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"volume.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-08T20:33:31.459256Z","iopub.execute_input":"2023-09-08T20:33:31.459648Z","iopub.status.idle":"2023-09-08T20:33:31.467125Z","shell.execute_reply.started":"2023-09-08T20:33:31.459617Z","shell.execute_reply":"2023-09-08T20:33:31.465921Z"},"trusted":true},"outputs":[],"execution_count":null}]}