{"metadata":{"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":52254,"databundleVersionId":6863140,"sourceType":"competition"},{"sourceId":6211844,"sourceType":"datasetVersion","datasetId":3567114}],"dockerImageVersionId":30648,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true},"kernelspec":{"display_name":"Python 3","language":"python","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"},"papermill":{"default_parameters":{},"duration":1133.077013,"end_time":"2023-11-28T20:56:56.068813","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2023-11-28T20:38:02.9918","version":"2.4.0"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Setup and Imports\n","metadata":{"papermill":{"duration":0.007077,"end_time":"2023-11-28T20:38:06.307747","exception":false,"start_time":"2023-11-28T20:38:06.30067","status":"completed"},"tags":[]}},{"cell_type":"code","source":"! pip install -q git+https://github.com/keras-team/keras-cv","metadata":{"_kg_hide-input":false,"_kg_hide-output":true,"papermill":{"duration":29.98361,"end_time":"2023-11-28T20:38:36.298446","exception":false,"start_time":"2023-11-28T20:38:06.314836","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-26T19:40:25.040049Z","iopub.execute_input":"2024-02-26T19:40:25.040925Z","iopub.status.idle":"2024-02-26T19:40:51.46618Z","shell.execute_reply.started":"2024-02-26T19:40:25.040888Z","shell.execute_reply":"2024-02-26T19:40:51.46492Z"},"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\n# To ignore all warnings\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"papermill":{"duration":16.969967,"end_time":"2023-11-28T20:38:53.276153","exception":false,"start_time":"2023-11-28T20:38:36.306186","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-26T19:41:43.968746Z","iopub.execute_input":"2024-02-26T19:41:43.96939Z","iopub.status.idle":"2024-02-26T19:41:43.975603Z","shell.execute_reply.started":"2024-02-26T19:41:43.969357Z","shell.execute_reply":"2024-02-26T19:41:43.974555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Configuration\n\nA particularly good practise is to have a configuration class for your notebooks. This not only keeps your configurations all at a single place but also becomes handy to map the configs to the performance of the model.\n\nPlease play around with the configurations and see how the performance of the model changes.","metadata":{"papermill":{"duration":0.007031,"end_time":"2023-11-28T20:38:53.291291","exception":false,"start_time":"2023-11-28T20:38:53.28426","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"## Note on some observations\n\n\n1. Class Dependencies: Refers to inherent relationships between classes in the analysis.\n2. Complementarity: `bowel_injury` and `bowel_healthy`, as well as `extravasation_injury` and `extravasation_healthy`, are perfectly complementary, with their sum always equal to 1.0.\n3. Simplification: For the model, only `{bowel/extravasation}_injury` will be included, and the corresponding healthy status can be calculated using a sigmoid function.\n4. Softmax: `{kidney/liver/spleen}_{healthy/low/high}` classifications are softmaxed, ensuring their combined probabilities sum up to 1.0 for each organ, simplifying the model while preserving essential information.","metadata":{"papermill":{"duration":0.00705,"end_time":"2023-11-28T20:38:53.305662","exception":false,"start_time":"2023-11-28T20:38:53.298612","status":"completed"},"tags":[]}},{"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":{"papermill":{"duration":0.015665,"end_time":"2023-11-28T20:38:53.328475","exception":false,"start_time":"2023-11-28T20:38:53.31281","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-26T19:42:07.91294Z","iopub.execute_input":"2024-02-26T19:42:07.913688Z","iopub.status.idle":"2024-02-26T19:42:07.918953Z","shell.execute_reply.started":"2024-02-26T19:42:07.913656Z","shell.execute_reply":"2024-02-26T19:42:07.917983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reproducibility\n\nWe would want this notebook to have reproducible results. Here we set the seed for all the random algorithms so that we can reproduce the experiments each time exactly the same way.","metadata":{"papermill":{"duration":0.00707,"end_time":"2023-11-28T20:38:53.342899","exception":false,"start_time":"2023-11-28T20:38:53.335829","status":"completed"},"tags":[]}},{"cell_type":"code","source":"keras.utils.set_random_seed(seed=config.SEED)","metadata":{"papermill":{"duration":0.013974,"end_time":"2023-11-28T20:38:53.364115","exception":false,"start_time":"2023-11-28T20:38:53.350141","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-26T19:42:32.969068Z","iopub.execute_input":"2024-02-26T19:42:32.969445Z","iopub.status.idle":"2024-02-26T19:42:32.974379Z","shell.execute_reply.started":"2024-02-26T19:42:32.969415Z","shell.execute_reply":"2024-02-26T19:42:32.973332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dataset\n\nThe dataset provided in the competition consists of DICOM images. We will not be training on the DICOM images, rather would work on PNG image which are extracted from the DICOM format.\n\n[A helpful resource on the conversion of DICOM to PNG](https://www.kaggle.com/code/radek1/how-to-process-dicom-images-to-pngs)","metadata":{"papermill":{"duration":0.007043,"end_time":"2023-11-28T20:38:53.37833","exception":false,"start_time":"2023-11-28T20:38:53.371287","status":"completed"},"tags":[]}},{"cell_type":"code","source":"BASE_PATH = f\"/kaggle/input/rsna-atd-512x512-png-v2-dataset\"","metadata":{"_kg_hide-input":true,"papermill":{"duration":0.01342,"end_time":"2023-11-28T20:38:53.399126","exception":false,"start_time":"2023-11-28T20:38:53.385706","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-26T19:42:42.734465Z","iopub.execute_input":"2024-02-26T19:42:42.735408Z","iopub.status.idle":"2024-02-26T19:42:42.740735Z","shell.execute_reply.started":"2024-02-26T19:42:42.735364Z","shell.execute_reply":"2024-02-26T19:42:42.739874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Meta Data\n\nThe `train.csv` file contains the following meta information:\n\n- `patient_id`: A unique ID code for each patient.\n- `series_id`: A unique ID code for each scan.\n- `instance_number`: The image number within the scan. The lowest instance number for many series is above zero as the original scans were cropped to the abdomen.\n- `[bowel/extravasation]_[healthy/injury]`: The two injury types with binary targets.\n- `[kidney/liver/spleen]_[healthy/low/high]`: The three injury types with three target levels.\n- `any_injury`: Whether the patient had any injury at all.\n","metadata":{"papermill":{"duration":0.007015,"end_time":"2023-11-28T20:38:53.413291","exception":false,"start_time":"2023-11-28T20:38:53.406276","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# train\ndataframe = pd.read_csv(f\"{BASE_PATH}/train.csv\")\ndataframe[\"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":{"papermill":{"duration":0.170295,"end_time":"2023-11-28T20:38:53.590855","exception":false,"start_time":"2023-11-28T20:38:53.42056","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-26T19:42:46.57018Z","iopub.execute_input":"2024-02-26T19:42:46.570568Z","iopub.status.idle":"2024-02-26T19:42:46.731429Z","shell.execute_reply.started":"2024-02-26T19:42:46.570535Z","shell.execute_reply":"2024-02-26T19:42:46.730536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We split the training dataset into train and validation. This is a common practise in the Machine Learning pipelines. We not only want to train our model, but also want to validate it's training.\n\nA small catch here is that the training and validation data should have an aligned data distribution. Here we handle that by grouping the lables and then splitting the dataset. This ensures an aligned data distribution between the training and the validation splits.","metadata":{"papermill":{"duration":0.007312,"end_time":"2023-11-28T20:38:53.605829","exception":false,"start_time":"2023-11-28T20:38:53.598517","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# Function to handle the split for each group\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 dataframe.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":{"papermill":{"duration":0.083196,"end_time":"2023-11-28T20:38:53.696566","exception":false,"start_time":"2023-11-28T20:38:53.61337","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-26T19:42:53.159576Z","iopub.execute_input":"2024-02-26T19:42:53.159928Z","iopub.status.idle":"2024-02-26T19:42:53.237642Z","shell.execute_reply.started":"2024-02-26T19:42:53.159902Z","shell.execute_reply":"2024-02-26T19:42:53.236642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.shape, val_data.shape","metadata":{"execution":{"iopub.execute_input":"2023-11-28T20:38:53.713258Z","iopub.status.busy":"2023-11-28T20:38:53.712513Z","iopub.status.idle":"2023-11-28T20:38:53.718084Z","shell.execute_reply":"2023-11-28T20:38:53.717272Z"},"papermill":{"duration":0.01576,"end_time":"2023-11-28T20:38:53.719951","exception":false,"start_time":"2023-11-28T20:38:53.704191","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Pipeline /w tf.data\n\nHere we build the data pipeline using `tf.data`. Using `tf.data` we can map out data to an augmentation pipeline simple by using the ` map` API.\n\nAdding augmentations to the data pipeline is as simple as adding a layer into the list of layers that the `Augmenter` processes.\n\nReference: https://keras.io/api/keras_cv/layers/augmentation/","metadata":{"papermill":{"duration":0.007465,"end_time":"2023-11-28T20:38:53.735378","exception":false,"start_time":"2023-11-28T20:38:53.727913","status":"completed"},"tags":[]}},{"cell_type":"code","source":"def 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_cv.layers.Augmenter(\n        [\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    return (augmenter(images), labels)\n\n\ndef 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(apply_augmentation, num_parallel_calls=config.AUTOTUNE)\n        .prefetch(config.AUTOTUNE)\n    )\n    return ds","metadata":{"papermill":{"duration":0.018908,"end_time":"2023-11-28T20:38:53.762037","exception":false,"start_time":"2023-11-28T20:38:53.743129","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-26T19:43:18.988319Z","iopub.execute_input":"2024-02-26T19:43:18.989364Z","iopub.status.idle":"2024-02-26T19:43:19.00266Z","shell.execute_reply.started":"2024-02-26T19:43:18.989319Z","shell.execute_reply":"2024-02-26T19:43:19.001541Z"},"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":{"papermill":{"duration":19.213764,"end_time":"2023-11-28T20:39:12.983443","exception":false,"start_time":"2023-11-28T20:38:53.769679","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-26T19:43:24.192123Z","iopub.execute_input":"2024-02-26T19:43:24.192998Z","iopub.status.idle":"2024-02-26T19:43:37.133988Z","shell.execute_reply.started":"2024-02-26T19:43:24.192957Z","shell.execute_reply":"2024-02-26T19:43:37.133023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# No more customizing your plots by hand, KerasCV has your back ;)\nkeras_cv.visualization.plot_image_gallery(\n    images=images,\n    value_range=(0, 1),\n    rows=2,\n    cols=2,\n)","metadata":{"execution":{"iopub.execute_input":"2023-11-28T20:39:13.000976Z","iopub.status.busy":"2023-11-28T20:39:13.000653Z","iopub.status.idle":"2023-11-28T20:39:13.658656Z","shell.execute_reply":"2023-11-28T20:39:13.657734Z"},"papermill":{"duration":0.668973,"end_time":"2023-11-28T20:39:13.660664","exception":false,"start_time":"2023-11-28T20:39:12.991691","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Build Model\n\n","metadata":{"papermill":{"duration":0.009984,"end_time":"2023-11-28T20:39:13.681326","exception":false,"start_time":"2023-11-28T20:39:13.671342","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.applications import ResNet50\nfrom tensorflow.keras.layers import Input, Dense, GlobalAveragePooling2D\n\ndef 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    # Use ResNet50 as the backbone\n    backbone = ResNet50(weights='imagenet', include_top=False)\n    x = backbone(inputs)\n\n    # GAP to get the activation maps\n    x = GlobalAveragePooling2D()(x)\n\n    # Define 'necks' for each head\n    x_bowel = Dense(32, activation='silu')(x)\n    x_extra = Dense(32, activation='silu')(x)\n    x_liver = Dense(32, activation='silu')(x)\n    x_kidney = Dense(32, activation='silu')(x)\n    x_spleen = Dense(32, activation='silu')(x)\n\n    # Define heads\n    out_bowel = Dense(1, name='bowel', activation='sigmoid')(x_bowel)\n    out_extra = Dense(1, name='extra', activation='sigmoid')(x_extra)\n    out_liver = Dense(3, name='liver', activation='softmax')(x_liver)\n    out_kidney = Dense(3, name='kidney', activation='softmax')(x_kidney)\n    out_spleen = Dense(3, name='spleen', activation='softmax')(x_spleen)\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\"],\n        \"extra\": [\"accuracy\"],\n        \"liver\": [\"accuracy\"],\n        \"kidney\": [\"accuracy\"],\n        \"spleen\": [\"accuracy\"],\n    }\n    print(\"[INFO] Compiling the model...\")\n    model.compile(\n        optimizer=optimizer,\n        loss=loss,\n        metrics=metrics\n    )\n\n    return model\n","metadata":{"papermill":{"duration":0.027971,"end_time":"2023-11-28T20:39:13.720365","exception":false,"start_time":"2023-11-28T20:39:13.692394","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-26T19:44:18.811978Z","iopub.execute_input":"2024-02-26T19:44:18.812444Z","iopub.status.idle":"2024-02-26T19:44:18.832177Z","shell.execute_reply.started":"2024-02-26T19:44:18.812406Z","shell.execute_reply":"2024-02-26T19:44:18.831285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Assuming you have already defined the necessary functions like build_dataset\n\n# Build the dataset\nprint(\"[INFO] Building the dataset...\")\ntrain_paths = train_data.image_path.values\ntrain_labels = train_data[config.TARGET_COLS].values.astype(np.float32)\nvalid_paths = val_data.image_path.values\nvalid_labels = val_data[config.TARGET_COLS].values.astype(np.float32)\n\n# Train and valid datasets\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=}\")\n\n# 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)\n","metadata":{"papermill":{"duration":1053.972472,"end_time":"2023-11-28T20:56:47.703254","exception":false,"start_time":"2023-11-28T20:39:13.730782","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-26T19:44:24.955068Z","iopub.execute_input":"2024-02-26T19:44:24.95574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualize the training plots","metadata":{"papermill":{"duration":0.141654,"end_time":"2023-11-28T20:56:47.987132","exception":false,"start_time":"2023-11-28T20:56:47.845478","status":"completed"},"tags":[]}},{"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.execute_input":"2023-11-28T20:56:48.276718Z","iopub.status.busy":"2023-11-28T20:56:48.27602Z","iopub.status.idle":"2023-11-28T20:56:49.490897Z","shell.execute_reply":"2023-11-28T20:56:49.48987Z"},"papermill":{"duration":1.362487,"end_time":"2023-11-28T20:56:49.493704","exception":false,"start_time":"2023-11-28T20:56:48.131217","status":"completed"},"tags":[]},"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.execute_input":"2023-11-28T20:56:49.78448Z","iopub.status.busy":"2023-11-28T20:56:49.783548Z","iopub.status.idle":"2023-11-28T20:56:49.979832Z","shell.execute_reply":"2023-11-28T20:56:49.978987Z"},"papermill":{"duration":0.343058,"end_time":"2023-11-28T20:56:49.981775","exception":false,"start_time":"2023-11-28T20:56:49.638717","status":"completed"},"tags":[]},"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\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.execute_input":"2023-11-28T20:56:50.276416Z","iopub.status.busy":"2023-11-28T20:56:50.276032Z","iopub.status.idle":"2023-11-28T20:56:50.28467Z","shell.execute_reply":"2023-11-28T20:56:50.283686Z"},"papermill":{"duration":0.15778,"end_time":"2023-11-28T20:56:50.286626","exception":false,"start_time":"2023-11-28T20:56:50.128846","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Store the model for inference","metadata":{"papermill":{"duration":0.143618,"end_time":"2023-11-28T20:56:50.576527","exception":false,"start_time":"2023-11-28T20:56:50.432909","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# Save the model\nmodel.save(\"rsna-atd.keras\")","metadata":{"execution":{"iopub.execute_input":"2023-11-28T20:56:50.867565Z","iopub.status.busy":"2023-11-28T20:56:50.866707Z","iopub.status.idle":"2023-11-28T20:56:52.802243Z","shell.execute_reply":"2023-11-28T20:56:52.801402Z"},"papermill":{"duration":2.085152,"end_time":"2023-11-28T20:56:52.804682","exception":false,"start_time":"2023-11-28T20:56:50.71953","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}