{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":52254,"databundleVersionId":6863140,"sourceType":"competition"}],"dockerImageVersionId":30579,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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        pass\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":{"execution":{"iopub.status.busy":"2023-11-24T09:58:04.004932Z","iopub.execute_input":"2023-11-24T09:58:04.005777Z","iopub.status.idle":"2023-11-24T09:58:11.585998Z","shell.execute_reply.started":"2023-11-24T09:58:04.005738Z","shell.execute_reply":"2023-11-24T09:58:11.585011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_metadata=pd.read_csv('/kaggle/input/rsna-2023-abdominal-trauma-detection/train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-11-24T09:58:11.588344Z","iopub.execute_input":"2023-11-24T09:58:11.589195Z","iopub.status.idle":"2023-11-24T09:58:11.601929Z","shell.execute_reply.started":"2023-11-24T09:58:11.589153Z","shell.execute_reply":"2023-11-24T09:58:11.600691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_metadata","metadata":{"execution":{"iopub.status.busy":"2023-11-24T09:58:11.603231Z","iopub.execute_input":"2023-11-24T09:58:11.603966Z","iopub.status.idle":"2023-11-24T09:58:11.623827Z","shell.execute_reply.started":"2023-11-24T09:58:11.603925Z","shell.execute_reply":"2023-11-24T09:58:11.622875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_metadata.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-11-24T09:58:11.625324Z","iopub.execute_input":"2023-11-24T09:58:11.625686Z","iopub.status.idle":"2023-11-24T09:58:11.643739Z","shell.execute_reply.started":"2023-11-24T09:58:11.625657Z","shell.execute_reply":"2023-11-24T09:58:11.642713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"no_of_rows=train_metadata.shape[0]\nno_of_rows\n#no_of_cols=train_metadata.shape[1]\n#no_of_cols","metadata":{"execution":{"iopub.status.busy":"2023-11-24T09:58:11.646764Z","iopub.execute_input":"2023-11-24T09:58:11.647112Z","iopub.status.idle":"2023-11-24T09:58:11.654371Z","shell.execute_reply.started":"2023-11-24T09:58:11.64707Z","shell.execute_reply":"2023-11-24T09:58:11.653416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_metadata.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2023-11-24T09:58:11.655641Z","iopub.execute_input":"2023-11-24T09:58:11.656027Z","iopub.status.idle":"2023-11-24T09:58:11.665299Z","shell.execute_reply.started":"2023-11-24T09:58:11.65599Z","shell.execute_reply":"2023-11-24T09:58:11.664274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_metadata.info()","metadata":{"execution":{"iopub.status.busy":"2023-11-24T09:58:11.666732Z","iopub.execute_input":"2023-11-24T09:58:11.667124Z","iopub.status.idle":"2023-11-24T09:58:11.681012Z","shell.execute_reply.started":"2023-11-24T09:58:11.667088Z","shell.execute_reply":"2023-11-24T09:58:11.680027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"perC=[.20, .40, .55, .70, .75, .85, .90]\n#describe(percentiles=perC,include=[none(default)/object/float/int],exclude=none(default))\ndata_description=train_metadata.describe(percentiles=perC)\ndata_description","metadata":{"execution":{"iopub.status.busy":"2023-11-24T09:58:11.682251Z","iopub.execute_input":"2023-11-24T09:58:11.682817Z","iopub.status.idle":"2023-11-24T09:58:11.73951Z","shell.execute_reply.started":"2023-11-24T09:58:11.682779Z","shell.execute_reply":"2023-11-24T09:58:11.738616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Extravasation[The leakage of blood from a vessel into tissues surronding it. \n#This can occur in injuries or burns or allergic reactions]\nimport matplotlib.pyplot as plt\nExtrav=train_metadata['any_injury'].value_counts()\nmycolors=[\"blue\",\"red\"]\nExtrav.plot(kind='pie', autopct='%1.2f%%',colors=mycolors)\nplt.legend()\nplt.show","metadata":{"execution":{"iopub.status.busy":"2023-11-24T09:58:11.740816Z","iopub.execute_input":"2023-11-24T09:58:11.741218Z","iopub.status.idle":"2023-11-24T09:58:11.927508Z","shell.execute_reply.started":"2023-11-24T09:58:11.741167Z","shell.execute_reply":"2023-11-24T09:58:11.926187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#kidney condition\nimport seaborn as sns\n#colors=['#747FE3','#E37346']\n#sns.set_palette(sns.color_palette(colors))\nsns.scatterplot(x=\"kidney_healthy\", y=\"kidney_low\", data= train_metadata,hue=\"kidney_high\",legend=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-24T09:58:11.929425Z","iopub.execute_input":"2023-11-24T09:58:11.930745Z","iopub.status.idle":"2023-11-24T09:58:12.589986Z","shell.execute_reply.started":"2023-11-24T09:58:11.93068Z","shell.execute_reply":"2023-11-24T09:58:12.588927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set(rc={'figure.figsize':(10,4)})\nsns.histplot(x=\"liver_low\", y=\"liver_high\", data= train_metadata,hue=\"liver_high\",legend=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-24T09:58:12.591403Z","iopub.execute_input":"2023-11-24T09:58:12.591794Z","iopub.status.idle":"2023-11-24T09:58:13.064082Z","shell.execute_reply.started":"2023-11-24T09:58:12.591763Z","shell.execute_reply":"2023-11-24T09:58:13.063302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set(rc={'figure.figsize':(15,4)})\nsns.barplot(x=\"patient_id\",y=\"bowel_healthy\",data=train_metadata.head(20));","metadata":{"execution":{"iopub.status.busy":"2023-11-24T09:58:13.065106Z","iopub.execute_input":"2023-11-24T09:58:13.065371Z","iopub.status.idle":"2023-11-24T09:58:13.681757Z","shell.execute_reply.started":"2023-11-24T09:58:13.065348Z","shell.execute_reply":"2023-11-24T09:58:13.680746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set(rc={'figure.figsize':(15,6)})\nsns.barplot(x=\"patient_id\",y=\"bowel_injury\",data=train_metadata.head(15),color=\"yellow\");","metadata":{"execution":{"iopub.status.busy":"2023-11-24T09:58:13.683014Z","iopub.execute_input":"2023-11-24T09:58:13.683318Z","iopub.status.idle":"2023-11-24T09:58:14.167116Z","shell.execute_reply.started":"2023-11-24T09:58:13.68329Z","shell.execute_reply":"2023-11-24T09:58:14.165938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set(rc={'figure.figsize':(15,4)})\nsns.barplot(x=\"patient_id\",y=\"extravasation_healthy\",data=train_metadata.loc[20:31]);","metadata":{"execution":{"iopub.status.busy":"2023-11-24T09:58:14.172251Z","iopub.execute_input":"2023-11-24T09:58:14.172668Z","iopub.status.idle":"2023-11-24T09:58:14.574035Z","shell.execute_reply.started":"2023-11-24T09:58:14.172639Z","shell.execute_reply":"2023-11-24T09:58:14.573123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport pydicom\nfrom PIL import Image\npic_of_train_img = pydicom.dcmread('/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/10005/18667/104.dcm')\nimg_of_traindata = pic_of_train_img.pixel_array\nplt.axis(False)\nplt.imshow(img_of_traindata)","metadata":{"execution":{"iopub.status.busy":"2023-11-24T09:58:14.575172Z","iopub.execute_input":"2023-11-24T09:58:14.575452Z","iopub.status.idle":"2023-11-24T09:58:14.897395Z","shell.execute_reply.started":"2023-11-24T09:58:14.575427Z","shell.execute_reply":"2023-11-24T09:58:14.896448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nos.listdir('/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images')","metadata":{"execution":{"iopub.status.busy":"2023-11-24T09:58:14.89872Z","iopub.execute_input":"2023-11-24T09:58:14.899097Z","iopub.status.idle":"2023-11-24T09:58:14.924488Z","shell.execute_reply.started":"2023-11-24T09:58:14.899059Z","shell.execute_reply":"2023-11-24T09:58:14.923609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label_df = pd.DataFrame(train_metadata)\ntrain_label_df","metadata":{"execution":{"iopub.status.busy":"2023-11-24T09:58:14.92585Z","iopub.execute_input":"2023-11-24T09:58:14.92616Z","iopub.status.idle":"2023-11-24T09:58:14.944682Z","shell.execute_reply.started":"2023-11-24T09:58:14.926133Z","shell.execute_reply":"2023-11-24T09:58:14.943688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_row=train_label_df.loc[train_label_df['patient_id'] == 9961]\nlabel_row","metadata":{"execution":{"iopub.status.busy":"2023-11-24T09:58:14.945836Z","iopub.execute_input":"2023-11-24T09:58:14.946139Z","iopub.status.idle":"2023-11-24T09:58:14.960582Z","shell.execute_reply.started":"2023-11-24T09:58:14.946113Z","shell.execute_reply":"2023-11-24T09:58:14.959643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_row.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2023-11-24T09:58:14.961646Z","iopub.execute_input":"2023-11-24T09:58:14.962738Z","iopub.status.idle":"2023-11-24T09:58:14.969499Z","shell.execute_reply.started":"2023-11-24T09:58:14.962707Z","shell.execute_reply":"2023-11-24T09:58:14.968495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root='/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images'","metadata":{"execution":{"iopub.status.busy":"2023-11-24T09:58:14.97063Z","iopub.execute_input":"2023-11-24T09:58:14.970939Z","iopub.status.idle":"2023-11-24T09:58:14.976072Z","shell.execute_reply.started":"2023-11-24T09:58:14.970914Z","shell.execute_reply":"2023-11-24T09:58:14.975113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! pip install -q git+https://github.com/keras-team/keras-cv","metadata":{"execution":{"iopub.status.busy":"2023-11-24T09:58:14.977226Z","iopub.execute_input":"2023-11-24T09:58:14.977663Z","iopub.status.idle":"2023-11-24T09:58:41.538116Z","shell.execute_reply.started":"2023-11-24T09:58:14.977625Z","shell.execute_reply":"2023-11-24T09:58:41.536864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-11-24T09:58:41.539554Z","iopub.execute_input":"2023-11-24T09:58:41.539855Z","iopub.status.idle":"2023-11-24T09:58:41.546137Z","shell.execute_reply.started":"2023-11-24T09:58:41.539824Z","shell.execute_reply":"2023-11-24T09:58:41.545184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Config:\n    SEED = 42\n    IMAGE_SIZE = [256, 256]\n    RESIZE_DIM = 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-11-24T09:58:41.547684Z","iopub.execute_input":"2023-11-24T09:58:41.548024Z","iopub.status.idle":"2023-11-24T09:58:41.559573Z","shell.execute_reply.started":"2023-11-24T09:58:41.54799Z","shell.execute_reply":"2023-11-24T09:58:41.55859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nos.environ[\"KERAS_BACKEND\"] = \"tensorflow\"\n# import keras_cv\n# import keras_core as keras\nimport gc\nimport cv2\nimport pydicom\nfrom joblib import Parallel, delayed\nimport tensorflow as tf\n# import numpy as np\n# import pandas as pd\nfrom tqdm.notebook import tqdm\nfrom glob import glob","metadata":{"execution":{"iopub.status.busy":"2023-11-24T09:58:41.561107Z","iopub.execute_input":"2023-11-24T09:58:41.56151Z","iopub.status.idle":"2023-11-24T09:58:41.57324Z","shell.execute_reply.started":"2023-11-24T09:58:41.561357Z","shell.execute_reply":"2023-11-24T09:58:41.572366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BASE_PATH = \"/kaggle/input/rsna-2023-abdominal-trauma-detection\"\nIMAGE_DIR = \"/tmp/dataset/rsna-atd\"\nINPUT_MODEL_PATH = \"/kaggle/input/kerascv-starter-notebook-train/rsna-atd.keras\"\nMODEL_PATH = \"/kaggle/working/rsna-atd.keras\"\nSTRIDE = 10","metadata":{"execution":{"iopub.status.busy":"2023-11-24T09:58:41.574661Z","iopub.execute_input":"2023-11-24T09:58:41.575246Z","iopub.status.idle":"2023-11-24T09:58:41.58288Z","shell.execute_reply.started":"2023-11-24T09:58:41.575212Z","shell.execute_reply":"2023-11-24T09:58:41.58182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_data_pipeline\n\ntrain_meta_df = pd.read_csv(f\"{BASE_PATH}/train_series_meta.csv\")\n\n# Checking if patients are repeated by finding the number of unique patient IDs\nnum_rows = train_meta_df.shape[0]\nunique_patients = train_meta_df[\"patient_id\"].nunique()\n\nprint(f\"{num_rows=}\")\nprint(f\"{unique_patients=}\")","metadata":{"execution":{"iopub.status.busy":"2023-11-24T09:58:41.584232Z","iopub.execute_input":"2023-11-24T09:58:41.584603Z","iopub.status.idle":"2023-11-24T09:58:41.601156Z","shell.execute_reply.started":"2023-11-24T09:58:41.584566Z","shell.execute_reply":"2023-11-24T09:58:41.60034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from tqdm.notebook import tqdm\nfrom glob import glob\ntrain_meta_df[\"dicom_folder\"] = BASE_PATH + \"/\" + \"train_images\"\\\n                                    + \"/\" + train_meta_df.patient_id.astype(str)\\\n                                    + \"/\" + train_meta_df.series_id.astype(str)\n\ntrain_folders = train_meta_df.dicom_folder.tolist()\ntrain_paths = []\nfor folder in train_folders:\n    train_paths += sorted(glob(os.path.join(folder, \"*dcm\")))[::STRIDE]","metadata":{"execution":{"iopub.status.busy":"2023-11-24T09:58:41.602184Z","iopub.execute_input":"2023-11-24T09:58:41.60247Z","iopub.status.idle":"2023-11-24T09:58:49.456607Z","shell.execute_reply.started":"2023-11-24T09:58:41.602431Z","shell.execute_reply":"2023-11-24T09:58:49.455746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.DataFrame(train_paths, columns=[\"dicom_path\"])\ntrain_df[\"patient_id\"] = train_df.dicom_path.map(lambda x: x.split(\"/\")[-3]).astype(int)\ntrain_df[\"series_id\"] = train_df.dicom_path.map(lambda x: x.split(\"/\")[-2]).astype(int)\ntrain_df[\"instance_number\"] = train_df.dicom_path.map(lambda x: x.split(\"/\")[-1].replace(\".dcm\",\"\")).astype(int)\n\ntrain_df[\"image_path\"] = f\"{IMAGE_DIR}/train_images\"\\\n                    + \"/\" + train_df.patient_id.astype(str)\\\n                    + \"/\" + train_df.series_id.astype(str)\\\n                    + \"/\" + train_df.instance_number.astype(str) +\".png\"\n\ntrain_df.head(2)","metadata":{"execution":{"iopub.status.busy":"2023-11-24T09:58:49.45769Z","iopub.execute_input":"2023-11-24T09:58:49.457978Z","iopub.status.idle":"2023-11-24T09:58:50.394447Z","shell.execute_reply.started":"2023-11-24T09:58:49.457951Z","shell.execute_reply":"2023-11-24T09:58:50.393366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_rows = train_df.shape[0]\nunique_patients = train_df[\"patient_id\"].nunique()\n\nprint(f\"{num_rows=}\")\nprint(f\"{unique_patients=}\")","metadata":{"execution":{"iopub.status.busy":"2023-11-24T09:58:50.396042Z","iopub.execute_input":"2023-11-24T09:58:50.396799Z","iopub.status.idle":"2023-11-24T09:58:50.404664Z","shell.execute_reply.started":"2023-11-24T09:58:50.396743Z","shell.execute_reply":"2023-11-24T09:58:50.40364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -r {IMAGE_DIR}\nos.makedirs(f\"{IMAGE_DIR}/train_images\", exist_ok=True)\nos.makedirs(f\"{IMAGE_DIR}/test_images\", exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-24T09:58:50.406241Z","iopub.execute_input":"2023-11-24T09:58:50.406738Z","iopub.status.idle":"2023-11-24T09:58:51.430186Z","shell.execute_reply.started":"2023-11-24T09:58:50.406701Z","shell.execute_reply":"2023-11-24T09:58:51.428905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def standardize_pixel_array(dcm):\n    # Correct DICOM pixel_array if PixelRepresentation == 1.\n    pixel_array = dcm.pixel_array\n    if dcm.PixelRepresentation == 1:\n        bit_shift = dcm.BitsAllocated - dcm.BitsStored\n        dtype = pixel_array.dtype \n        new_array = (pixel_array << bit_shift).astype(dtype) >>  bit_shift\n        pixel_array = pydicom.pixel_data_handlers.util.apply_modality_lut(new_array, dcm)\n    return pixel_array\n\ndef read_xray(path, fix_monochrome=True):\n    dicom = pydicom.dcmread(path)\n    data = standardize_pixel_array(dicom)\n    data = data - np.min(data)\n    data = data / (np.max(data) + 1e-5)\n    if fix_monochrome and dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        data = 1.0 - data\n    return data\n\ndef resize_and_save(file_path):\n    img = read_xray(file_path)\n    h, w = img.shape[:2]  # orig hw\n    img = cv2.resize(img, (config.RESIZE_DIM, config.RESIZE_DIM), cv2.INTER_LINEAR)\n    img = (img * 255).astype(np.uint8)\n    \n    sub_path = file_path.split(\"/\",4)[-1].split(\".dcm\")[0] + \".png\"\n    infos = sub_path.split(\"/\")\n    sub_path = file_path.split(\"/\",4)[-1].split(\".dcm\")[0] + \".png\"\n    infos = sub_path.split(\"/\")\n    pid = infos[-3]\n    sid = infos[-2]\n    iid = infos[-1]; iid = iid.replace(\".png\",\"\")\n    new_path = os.path.join(IMAGE_DIR, sub_path)\n    os.makedirs(new_path.rsplit(\"/\",1)[0], exist_ok=True)\n    cv2.imwrite(new_path, img)\n    return","metadata":{"execution":{"iopub.status.busy":"2023-11-24T09:58:51.43208Z","iopub.execute_input":"2023-11-24T09:58:51.432507Z","iopub.status.idle":"2023-11-24T09:58:51.444733Z","shell.execute_reply.started":"2023-11-24T09:58:51.432446Z","shell.execute_reply":"2023-11-24T09:58:51.4438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nfile_paths = train_df.dicom_path.tolist()\n_ = Parallel(n_jobs=8, backend=\"threading\")(\n    delayed(resize_and_save)(file_path) for file_path in tqdm(file_paths, leave=True, position=0)\n)\n\ndel _; gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-11-24T09:58:51.446165Z","iopub.execute_input":"2023-11-24T09:58:51.446449Z","iopub.status.idle":"2023-11-24T10:43:20.711088Z","shell.execute_reply.started":"2023-11-24T09:58:51.446423Z","shell.execute_reply":"2023-11-24T10:43:20.710157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def decode_image(image_path):\n    file_bytes = tf.io.read_file(image_path)\n    image = tf.io.decode_png(file_bytes, channels=3, dtype=tf.uint8)\n    image = tf.image.resize(image, config.IMAGE_SIZE, method=\"bilinear\")\n    image = tf.cast(image, tf.float32) / 255.0\n    return image\n\ndef build_dataset(image_paths):\n    ds = (\n        tf.data.Dataset.from_tensor_slices(image_paths)\n        .map(decode_image, num_parallel_calls=config.AUTOTUNE)\n        .shuffle(config.BATCH_SIZE * 10)\n        .batch(config.BATCH_SIZE)\n        .prefetch(config.AUTOTUNE)\n    )\n    return ds","metadata":{"execution":{"iopub.status.busy":"2023-11-24T10:43:20.712435Z","iopub.execute_input":"2023-11-24T10:43:20.712741Z","iopub.status.idle":"2023-11-24T10:43:20.719289Z","shell.execute_reply.started":"2023-11-24T10:43:20.712712Z","shell.execute_reply":"2023-11-24T10:43:20.718495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"paths  = train_df.image_path.tolist()\n\nds = build_dataset(paths)\nimages = next(iter(ds))\n\nimages.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-24T10:43:20.720537Z","iopub.execute_input":"2023-11-24T10:43:20.72083Z","iopub.status.idle":"2023-11-24T10:43:29.453142Z","shell.execute_reply.started":"2023-11-24T10:43:20.720805Z","shell.execute_reply":"2023-11-24T10:43:29.452033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"keras_cv.visualization.plot_image_gallery(\n    images=images,\n    value_range=(0, 1),\n    rows=1,\n    cols=3,\n)","metadata":{"execution":{"iopub.status.busy":"2023-11-24T10:43:29.456497Z","iopub.execute_input":"2023-11-24T10:43:29.457202Z","iopub.status.idle":"2023-11-24T10:43:30.624624Z","shell.execute_reply.started":"2023-11-24T10:43:29.457171Z","shell.execute_reply":"2023-11-24T10:43:30.623703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train\ndataframe = 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\"\ndataframe = dataframe.drop_duplicates()\n\ndataframe.head(2)","metadata":{"execution":{"iopub.status.busy":"2023-11-24T10:43:30.626072Z","iopub.execute_input":"2023-11-24T10:43:30.626378Z","iopub.status.idle":"2023-11-24T10:43:30.654326Z","shell.execute_reply.started":"2023-11-24T10:43:30.626351Z","shell.execute_reply":"2023-11-24T10:43:30.653355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.merge(train_df, dataframe, on='patient_id', how='inner')","metadata":{"execution":{"iopub.status.busy":"2023-11-24T10:43:30.655613Z","iopub.execute_input":"2023-11-24T10:43:30.655927Z","iopub.status.idle":"2023-11-24T10:43:30.717304Z","shell.execute_reply.started":"2023-11-24T10:43:30.655898Z","shell.execute_reply":"2023-11-24T10:43:30.716495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Function to handle the split for each group\nimport pandas as pd\ndef split_group(group, test_size=0.2):\n    if len(group) == 1:\n        return (group, pd.DataFrame()) if np.random.rand() < test_size else (pd.DataFrame(), group)\n    else:\n        return train_test_split(group, test_size=test_size, random_state=42)\n\n# Initialize the train and validation datasets\ntrain_data = pd.DataFrame()\nval_data = pd.DataFrame()\n\n# Iterate through the groups and split them, handling single-sample groups\nfor _, group in train_df.groupby(config.TARGET_COLS):\n    train_group, val_group = split_group(group)\n    train_data = pd.concat([train_data, train_group], ignore_index=True)\n    val_data = pd.concat([val_data, val_group], ignore_index=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-24T10:43:30.718549Z","iopub.execute_input":"2023-11-24T10:43:30.718852Z","iopub.status.idle":"2023-11-24T10:43:31.345705Z","shell.execute_reply.started":"2023-11-24T10:43:30.718824Z","shell.execute_reply":"2023-11-24T10:43:31.3447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.shape, val_data.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-24T10:43:31.346992Z","iopub.execute_input":"2023-11-24T10:43:31.34729Z","iopub.status.idle":"2023-11-24T10:43:31.354692Z","shell.execute_reply.started":"2023-11-24T10:43:31.347264Z","shell.execute_reply":"2023-11-24T10:43:31.353664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#import necessary libs\nimport os\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-11-24T10:43:31.356121Z","iopub.execute_input":"2023-11-24T10:43:31.356433Z","iopub.status.idle":"2023-11-24T10:43:31.363023Z","shell.execute_reply.started":"2023-11-24T10:43:31.356404Z","shell.execute_reply":"2023-11-24T10:43:31.361921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from keras_cv.augment.preprocess.augment import RandomFlip\nfrom tensorflow.keras.layers.experimental import preprocessing\n\ndef decode_image_and_label(image_path, label):\n    file_bytes = tf.io.read_file(image_path)\n    image = tf.io.decode_png(file_bytes, channels=3, dtype=tf.uint8)\n    image = tf.image.resize(image, config.IMAGE_SIZE, method=\"bilinear\")\n    image = tf.cast(image, tf.float32) / 255.0\n    \n    label = tf.cast(label, tf.float32)\n    #         bowel       fluid       kidney      liver       spleen\n    labels = (label[0:1], label[1:2], label[2:5], label[5:8], label[8:11])\n    \n    return (image, labels)\n\n\ndef apply_augmentation(images, labels):\n    augmenter = keras.Sequential(\n        \n        layers=[\n            keras_cv.layers.RandomFlip(mode=\"horizontal_and_vertical\"),\n            keras_cv.layers.RandomCutout(height_factor=0.2, width_factor=0.2),\n            \n        ]\n    )\n    aug = augmenter(images)\n    return (aug, labels)\n\ndef aug(image,labels):\n    #image = preprocessing.Rescaling(1.0 / 255)(image)  # Rescale pixel values\n    image = preprocessing.RandomFlip(mode=\"horizontal_and_vertical\")(image)\n    image = preprocessing.RandomRotation(factor=0.2)(image)\n    #image = preprocessing.RandomZoom(height_factor=(0.8, 1.2), width_factor=(0.8, 1.2))(image)\n    #image = preprocessing.RandomContrast(factor=0.2)(image)\n    image = preprocessing.RandomTranslation(height_factor=0.2, width_factor=0.2)(image)\n    return (image, labels)","metadata":{"execution":{"iopub.status.busy":"2023-11-24T10:43:31.372187Z","iopub.execute_input":"2023-11-24T10:43:31.372868Z","iopub.status.idle":"2023-11-24T10:43:31.389317Z","shell.execute_reply.started":"2023-11-24T10:43:31.372823Z","shell.execute_reply":"2023-11-24T10:43:31.388353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the preprocessing layers outside the tf.function\nrandom_flip_layer = preprocessing.RandomFlip(mode=\"horizontal_and_vertical\")\nrandom_rotation_layer = preprocessing.RandomRotation(factor=0.2)\nrandom_translation_layer = preprocessing.RandomTranslation(height_factor=0.2, width_factor=0.2)\n\n@tf.function\ndef aug(image, labels):\n    # Rescale pixel values\n    # image = preprocessing.Rescaling(1.0 / 255)(image)  # Uncomment if needed\n\n    # Apply the predefined layers\n    image = random_flip_layer(image)\n    image = random_rotation_layer(image)\n    image = random_translation_layer(image)\n\n    return image, labels\n","metadata":{"execution":{"iopub.status.busy":"2023-11-24T10:43:31.390601Z","iopub.execute_input":"2023-11-24T10:43:31.391009Z","iopub.status.idle":"2023-11-24T10:43:31.425948Z","shell.execute_reply.started":"2023-11-24T10:43:31.390976Z","shell.execute_reply":"2023-11-24T10:43:31.425165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_dataset(image_paths, labels):\n    ds = (\n        tf.data.Dataset.from_tensor_slices((image_paths, labels))\n        .map(decode_image_and_label, num_parallel_calls=config.AUTOTUNE)\n        .shuffle(config.BATCH_SIZE * 10)\n        .batch(config.BATCH_SIZE)\n        .map(aug, num_parallel_calls=config.AUTOTUNE)\n        .prefetch(config.AUTOTUNE)\n    )\n    return ds","metadata":{"execution":{"iopub.status.busy":"2023-11-24T10:43:31.427225Z","iopub.execute_input":"2023-11-24T10:43:31.427902Z","iopub.status.idle":"2023-11-24T10:43:31.433763Z","shell.execute_reply.started":"2023-11-24T10:43:31.427869Z","shell.execute_reply":"2023-11-24T10:43:31.432782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"paths  = train_data.image_path.tolist()\nlabels = train_data[config.TARGET_COLS].values\n\nds = build_dataset(image_paths=paths, labels=labels)\nimages, labels = next(iter(ds))\nimages.shape, [label.shape for label in labels]","metadata":{"execution":{"iopub.status.busy":"2023-11-24T10:43:31.434868Z","iopub.execute_input":"2023-11-24T10:43:31.435116Z","iopub.status.idle":"2023-11-24T10:43:35.206318Z","shell.execute_reply.started":"2023-11-24T10:43:31.435092Z","shell.execute_reply":"2023-11-24T10:43:35.205392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"keras_cv.visualization.plot_image_gallery(\n    images=images,\n    value_range=(0, 1),\n    rows=2,\n    cols=2,\n)","metadata":{"execution":{"iopub.status.busy":"2023-11-24T10:43:35.207522Z","iopub.execute_input":"2023-11-24T10:43:35.207815Z","iopub.status.idle":"2023-11-24T10:43:36.67602Z","shell.execute_reply.started":"2023-11-24T10:43:35.207788Z","shell.execute_reply":"2023-11-24T10:43:36.675082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.metrics import *","metadata":{"execution":{"iopub.status.busy":"2023-11-24T10:43:36.677278Z","iopub.execute_input":"2023-11-24T10:43:36.677606Z","iopub.status.idle":"2023-11-24T10:43:36.683826Z","shell.execute_reply.started":"2023-11-24T10:43:36.677576Z","shell.execute_reply":"2023-11-24T10:43:36.682733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_model(warmup_steps, decay_steps):\n    # Define Input\n    inputs = keras.Input(shape=config.IMAGE_SIZE + [3,], batch_size=config.BATCH_SIZE)\n    \n    # Define Backbone\n    include_rescaling = False\n    x = inputs\n    \n    # GAP to get the activation maps\n    gap = keras.layers.GlobalAveragePooling2D()\n    x = gap(x)\n\n    # Define 'necks' for each head\n    x_bowel = keras.layers.Dense(32, activation='silu')(x)\n    x_extra = keras.layers.Dense(32, activation='silu')(x)\n    x_liver = keras.layers.Dense(32, activation='silu')(x)\n    x_kidney = keras.layers.Dense(32, activation='silu')(x)\n    x_spleen = keras.layers.Dense(32, activation='silu')(x)\n\n    # Define heads\n    out_bowel = keras.layers.Dense(1, name='bowel', activation='sigmoid')(x_bowel) # use sigmoid to convert predictions to [0-1]\n    out_extra = keras.layers.Dense(1, name='extra', activation='sigmoid')(x_extra) # use sigmoid to convert predictions to [0-1]\n    out_liver = keras.layers.Dense(3, name='liver', activation='softmax')(x_liver) # use softmax for the liver head\n    out_kidney = keras.layers.Dense(3, name='kidney', activation='softmax')(x_kidney) # use softmax for the kidney head\n    out_spleen = keras.layers.Dense(3, name='spleen', activation='softmax')(x_spleen) # use softmax for the spleen head\n    \n    # Concatenate the outputs\n    outputs = [out_bowel, out_extra, out_liver, out_kidney, out_spleen]\n\n    # Create model\n    print(\"[INFO] Building the model...\")\n    model = keras.Model(inputs=inputs, outputs=outputs)\n    \n    # Cosine Decay\n    cosine_decay = keras.optimizers.schedules.CosineDecay(\n        initial_learning_rate=1e-4,\n        decay_steps=decay_steps,\n               alpha=0.0,\n        warmup_target=1e-3,\n        warmup_steps=warmup_steps,\n    )\n\n    # Compile the model\n    optimizer = keras.optimizers.Adam(learning_rate=cosine_decay)\n    loss = {\n        \"bowel\":keras.losses.BinaryCrossentropy(),\n        \"extra\":keras.losses.BinaryCrossentropy(),\n        \"liver\":keras.losses.CategoricalCrossentropy(),\n        \"kidney\":keras.losses.CategoricalCrossentropy(),\n        \"spleen\":keras.losses.CategoricalCrossentropy(),\n    }\n    metrics = {\n        \"bowel\":[\"accuracy\",Precision(),Recall(),F1Score()],\n        \"extra\":[\"accuracy\",Precision(),Recall(),F1Score()],\n        \"liver\":[\"accuracy\",Precision(),Recall(),F1Score()],\n        \"kidney\":[\"accuracy\",Precision(),Recall(),F1Score()],\n        \"spleen\":[\"accuracy\",Precision(),Recall(),F1Score()],\n    }\n    print(\"[INFO] Compiling the model...\")\n    model.compile(\n        optimizer=optimizer,\n      loss=loss,\n      metrics=metrics\n    )\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2023-11-24T10:43:36.685422Z","iopub.execute_input":"2023-11-24T10:43:36.686065Z","iopub.status.idle":"2023-11-24T10:43:36.70262Z","shell.execute_reply.started":"2023-11-24T10:43:36.686029Z","shell.execute_reply":"2023-11-24T10:43:36.701773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get image_paths and labels\nprint(\"[INFO] Building the dataset...\")\ntrain_paths = train_data.image_path.values; train_labels = train_data[config.TARGET_COLS].values.astype(np.float32)\nvalid_paths = val_data.image_path.values; valid_labels = val_data[config.TARGET_COLS].values.astype(np.float32)\n\n# train and valid dataset\ntrain_ds = build_dataset(image_paths=train_paths, labels=train_labels)\nval_ds = build_dataset(image_paths=valid_paths, labels=valid_labels)\n\ntotal_train_steps = train_ds.cardinality().numpy() * config.BATCH_SIZE * config.EPOCHS\nwarmup_steps = int(total_train_steps * 0.10)\ndecay_steps = total_train_steps - warmup_steps\n\nprint(f\"{total_train_steps=}\")\nprint(f\"{warmup_steps=}\")\nprint(f\"{decay_steps=}\")","metadata":{"execution":{"iopub.status.busy":"2023-11-24T10:43:36.703747Z","iopub.execute_input":"2023-11-24T10:43:36.704092Z","iopub.status.idle":"2023-11-24T10:43:36.905902Z","shell.execute_reply.started":"2023-11-24T10:43:36.704058Z","shell.execute_reply":"2023-11-24T10:43:36.904874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # build the model\nprint(\"[INFO] Building the model...\")\nmodel = build_model(warmup_steps, decay_steps)\n\n# # train\nprint(\"[INFO] Training...\")\nhistory = model.fit(\n    train_ds,\n    epochs=config.EPOCHS,\n    validation_data=val_ds,\n)","metadata":{"execution":{"iopub.status.busy":"2023-11-24T10:43:36.907052Z","iopub.execute_input":"2023-11-24T10:43:36.907777Z","iopub.status.idle":"2023-11-24T13:30:21.715878Z","shell.execute_reply.started":"2023-11-24T10:43:36.907734Z","shell.execute_reply":"2023-11-24T13:30:21.715011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a 3x2 grid for the subplots\nfig, axes = plt.subplots(5, 1, figsize=(5, 15))\n\n# Flatten axes to iterate through them\naxes = axes.flatten()\n\n# Iterate through the metrics and plot them\nfor i, name in enumerate([\"bowel\", \"extra\", \"kidney\", \"liver\", \"spleen\"]):\n    # Plot training accuracy\n    axes[i].plot(history.history[name + '_accuracy'], label='Training ' + name)\n    # Plot validation accuracy\n    axes[i].plot(history.history['val_' + name + '_accuracy'], label='Validation ' + name)\n    axes[i].set_title(name)\n    axes[i].set_xlabel('Epoch')\n    axes[i].set_ylabel('Accuracy')\n    axes[i].legend()\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-24T13:30:21.720116Z","iopub.execute_input":"2023-11-24T13:30:21.720759Z","iopub.status.idle":"2023-11-24T13:30:23.594093Z","shell.execute_reply.started":"2023-11-24T13:30:21.720729Z","shell.execute_reply":"2023-11-24T13:30:23.593169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history[\"loss\"], label=\"loss\")\nplt.plot(history.history[\"val_loss\"], label=\"val loss\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-24T13:30:23.595264Z","iopub.execute_input":"2023-11-24T13:30:23.595583Z","iopub.status.idle":"2023-11-24T13:30:24.292384Z","shell.execute_reply.started":"2023-11-24T13:30:23.595554Z","shell.execute_reply":"2023-11-24T13:30:24.291432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# store best results\nbest_epoch = np.argmin(history.history['val_loss'])\nbest_loss = history.history['val_loss'][best_epoch]\nbest_acc_bowel = history.history['val_bowel_accuracy'][best_epoch]\nbest_acc_extra = history.history['val_extra_accuracy'][best_epoch]\nbest_acc_liver = history.history['val_liver_accuracy'][best_epoch]\nbest_acc_kidney = history.history['val_kidney_accuracy'][best_epoch]\nbest_acc_spleen = history.history['val_spleen_accuracy'][best_epoch]\n\n# Find mean accuracy\nbest_acc = np.mean(\n    [best_acc_bowel,\n     best_acc_extra,\n     best_acc_liver,\n     best_acc_kidney,\n     best_acc_spleen\n])\n\nprint(f'>>>> BEST Loss  : {best_loss:.3f}\\n>>>> BEST Acc   : {best_acc:.3f}\\n>>>> BEST Epoch : {best_epoch}\\n')\nprint('ORGAN Acc:')\nprint(f'  >>>> {\"Bowel\".ljust(15)} : {best_acc_bowel:.3f}')\nprint(f'  >>>> {\"Extravasation\".ljust(15)} : {best_acc_extra:.3f}')\nprint(f'  >>>> {\"Liver\".ljust(15)} : {best_acc_liver:.3f}')\nprint(f'  >>>> {\"Kidney\".ljust(15)} : {best_acc_kidney:.3f}')\nprint(f'  >>>> {\"Spleen\".ljust(15)} : {best_acc_spleen:.3f}')","metadata":{"execution":{"iopub.status.busy":"2023-11-24T13:30:24.293678Z","iopub.execute_input":"2023-11-24T13:30:24.29398Z","iopub.status.idle":"2023-11-24T13:30:24.302743Z","shell.execute_reply.started":"2023-11-24T13:30:24.293953Z","shell.execute_reply":"2023-11-24T13:30:24.301834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-11-24T13:30:24.304043Z","iopub.execute_input":"2023-11-24T13:30:24.304726Z","iopub.status.idle":"2023-11-24T13:30:24.341429Z","shell.execute_reply.started":"2023-11-24T13:30:24.304689Z","shell.execute_reply":"2023-11-24T13:30:24.340525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the model\n#import keras.models\nmodel.save(\"rsna-atd.keras\")","metadata":{"execution":{"iopub.status.busy":"2023-11-24T13:30:24.342753Z","iopub.execute_input":"2023-11-24T13:30:24.343366Z","iopub.status.idle":"2023-11-24T13:30:24.472787Z","shell.execute_reply.started":"2023-11-24T13:30:24.34333Z","shell.execute_reply":"2023-11-24T13:30:24.471885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def post_proc(pred):\n    proc_pred = np.empty((pred.shape[0], 2*2 + 3*3), dtype=\"float32\")\n\n    # bowel, extravasation\n    proc_pred[:, 0] = pred[:, 0]\n    proc_pred[:, 1] = 1 - proc_pred[:, 0]\n    proc_pred[:, 2] = pred[:, 1]\n    proc_pred[:, 3] = 1 - proc_pred[:, 2]\n    \n    # liver, kidney, sneel\n    proc_pred[:, 4:7] = pred[:, 2:5]\n    proc_pred[:, 7:10] = pred[:, 5:8]\n    proc_pred[:, 10:13] = pred[:, 8:11]\n\n    return proc_pred","metadata":{"execution":{"iopub.status.busy":"2023-11-24T13:30:24.473995Z","iopub.execute_input":"2023-11-24T13:30:24.474342Z","iopub.status.idle":"2023-11-24T13:30:24.481772Z","shell.execute_reply.started":"2023-11-24T13:30:24.474307Z","shell.execute_reply":"2023-11-24T13:30:24.480426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_rdataset(image_paths,labels):\n    ds = (\n        tf.data.Dataset.from_tensor_slices(image_paths)\n        .map(decode_image, num_parallel_calls=config.AUTOTUNE)\n        .shuffle(config.BATCH_SIZE * 10)\n        .batch(config.BATCH_SIZE)\n        .prefetch(config.AUTOTUNE)\n    )\n    return ds","metadata":{"execution":{"iopub.status.busy":"2023-11-24T14:13:30.764203Z","iopub.execute_input":"2023-11-24T14:13:30.765318Z","iopub.status.idle":"2023-11-24T14:13:30.770413Z","shell.execute_reply.started":"2023-11-24T14:13:30.765276Z","shell.execute_reply":"2023-11-24T14:13:30.769513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Getting unique patient IDs from train dataset\npatient_ids = train_df[\"patient_id\"].unique()\n\n# Initializing array to store predictions\npatient_preds = np.zeros(\n    shape=(len(patient_ids), 2*2 + 3*3),\n    dtype=\"float32\"\n)\n\n# Iterating over each patient\nfor pidx, patient_id in tqdm(enumerate(patient_ids), total=len(patient_ids), desc=\"Patients \"):\n    print(f\"Patient ID: {patient_id}\")\n    \n    # Query the dataframe for a particular patient\n    patient_df = train_df.query(\"patient_id == @patient_id\")\n    \n    # Getting image paths for a patient\n    patient_paths = patient_df.image_path.tolist()\n\n    # Building dataset for prediction\n    dtrain = build_rdataset(patient_paths,labels)\n    \n    # Predicting with the model\n    pred = model.predict(dtrain)\n    pred = np.concatenate(pred, axis=-1).astype(\"float32\")\n    pred = pred[:len(patient_paths), :]\n    pred = np.mean(pred.reshape(1, len(patient_paths), 11), axis=0)\n    pred = np.max(pred, axis=0, keepdims=True)\n    \n    patient_preds[pidx, :] += post_proc(pred)[0]","metadata":{"execution":{"iopub.status.busy":"2023-11-24T15:21:06.69855Z","iopub.execute_input":"2023-11-24T15:21:06.698987Z","iopub.status.idle":"2023-11-24T15:26:12.70996Z","shell.execute_reply.started":"2023-11-24T15:21:06.698952Z","shell.execute_reply":"2023-11-24T15:26:12.708893Z"},"trusted":true},"execution_count":null,"outputs":[]}]}