{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"BASE_PATH = '/kaggle/input/rsna-atd-512x512-png-v2-dataset'","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-20T16:52:37.322361Z","iopub.execute_input":"2023-09-20T16:52:37.325761Z","iopub.status.idle":"2023-09-20T16:52:37.339247Z","shell.execute_reply.started":"2023-09-20T16:52:37.325721Z","shell.execute_reply":"2023-09-20T16:52:37.336557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\ndf = pd.read_csv(f'{BASE_PATH}/train.csv')\ndf['image_path'] = f'{BASE_PATH}/train_images'\\\n                    + '/' + df.patient_id.astype(str)\\\n                    + '/' + df.series_id.astype(str)\\\n                    + '/' + df.instance_number.astype(str) +'.png'\ndf = df.drop_duplicates()\nprint('Train:')\ndisplay(df)\n\n# test\ntest_df = pd.read_csv(f'{BASE_PATH}/test.csv')\ntest_df['image_path'] = f'{BASE_PATH}/test_images'\\\n                    + '/' + test_df.patient_id.astype(str)\\\n                    + '/' + test_df.series_id.astype(str)\\\n                    + '/' + test_df.instance_number.astype(str) +'.png'\ntest_df = test_df.drop_duplicates()\nprint('\\nTest:')\ndisplay(test_df.head(3))","metadata":{"execution":{"iopub.status.busy":"2023-09-20T16:52:37.341114Z","iopub.execute_input":"2023-09-20T16:52:37.342401Z","iopub.status.idle":"2023-09-20T16:52:37.578932Z","shell.execute_reply.started":"2023-09-20T16:52:37.342358Z","shell.execute_reply":"2023-09-20T16:52:37.577905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom sklearn.model_selection import train_test_split\nimport numpy as np\n\nclass Config:\n    SEED = 101\n    IMAGE_SIZE = [256, 256]\n    BATCH_SIZE = 64\n    EPOCHS = 30\n    TARGET_COLS  = ['bowel_healthy', 'bowel_injury',\n                    'extravasation_healthy', '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()\n\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=config.SEED)\n\ntrain_data = pd.DataFrame()\nval_data = pd.DataFrame()\n\nfor _, group in 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)\n\ntrain_data.shape, val_data.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-20T16:52:37.58334Z","iopub.execute_input":"2023-09-20T16:52:37.585227Z","iopub.status.idle":"2023-09-20T16:52:37.689796Z","shell.execute_reply.started":"2023-09-20T16:52:37.585185Z","shell.execute_reply":"2023-09-20T16:52:37.688798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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:2], label[2:4], label[4:7], label[7:10], label[10:13])\n    \n    return (image, labels)\n\n\ndef apply_augmentation(images, labels):\n    \n    images = tf.image.random_flip_left_right(images)\n    images = tf.image.random_flip_up_down(images)\n    \n    return images, labels\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":{"execution":{"iopub.status.busy":"2023-09-20T16:52:37.692568Z","iopub.execute_input":"2023-09-20T16:52:37.692946Z","iopub.status.idle":"2023-09-20T16:52:37.702433Z","shell.execute_reply.started":"2023-09-20T16:52:37.692913Z","shell.execute_reply":"2023-09-20T16:52:37.701352Z"},"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-09-20T16:52:37.704069Z","iopub.execute_input":"2023-09-20T16:52:37.704554Z","iopub.status.idle":"2023-09-20T16:52:40.300398Z","shell.execute_reply.started":"2023-09-20T16:52:37.704523Z","shell.execute_reply":"2023-09-20T16:52:40.29905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras_cv\n\nkeras_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-09-20T16:52:40.301831Z","iopub.execute_input":"2023-09-20T16:52:40.302923Z","iopub.status.idle":"2023-09-20T16:52:42.734972Z","shell.execute_reply.started":"2023-09-20T16:52:40.302886Z","shell.execute_reply":"2023-09-20T16:52:42.733559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"[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-09-20T16:52:42.736869Z","iopub.execute_input":"2023-09-20T16:52:42.737503Z","iopub.status.idle":"2023-09-20T16:52:43.035208Z","shell.execute_reply.started":"2023-09-20T16:52:42.737462Z","shell.execute_reply":"2023-09-20T16:52:43.034033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cosine_lr(initial_learning_rate, total_epochs, epoch):\n    if epoch < total_epochs:\n        return initial_learning_rate * (epoch / total_epochs)\n    else:\n        return initial_learning_rate * (math.cos(math.pi * (epoch - total_epochs) / total_epochs) + 1) / 2","metadata":{"execution":{"iopub.status.busy":"2023-09-20T16:52:43.036708Z","iopub.execute_input":"2023-09-20T16:52:43.037072Z","iopub.status.idle":"2023-09-20T16:52:43.044659Z","shell.execute_reply.started":"2023-09-20T16:52:43.037038Z","shell.execute_reply":"2023-09-20T16:52:43.043629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# model_1: Xception","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nimport keras  \n\nbase_model = keras.applications.Xception(\n    weights='imagenet',\n    input_shape=config.IMAGE_SIZE + [3,],\n    include_top=False)  \n\nbase_model.trainable = False\n\ninputs = keras.Input(shape=config.IMAGE_SIZE + [3,], batch_size=config.BATCH_SIZE)\n\nx = base_model(inputs, training=False)\n\nx = keras.layers.GlobalAveragePooling2D()(x)\n\nx_bowel = keras.layers.Dense(32, activation='silu')(x)\nx_extra = keras.layers.Dense(32, activation='silu')(x)\nx_liver = keras.layers.Dense(32, activation='silu')(x)\nx_kidney = keras.layers.Dense(32, activation='silu')(x)\nx_spleen = keras.layers.Dense(32, activation='silu')(x)\n\nout_bowel = keras.layers.Dense(2, name='bowel', activation='sigmoid')(x_bowel)\nout_extra = keras.layers.Dense(2, name='extra', activation='sigmoid')(x_extra)\nout_liver = keras.layers.Dense(3, name='liver', activation='softmax')(x_liver)\nout_kidney = keras.layers.Dense(3, name='kidney', activation='softmax')(x_kidney)\nout_spleen = keras.layers.Dense(3, name='spleen', activation='softmax')(x_spleen)\n\noutputs = [out_bowel, out_extra, out_liver, out_kidney, out_spleen]\n\nprint(\"[INFO] Building the model...\")\nmodel = keras.Model(inputs, outputs)\n\nloss = {\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    }\nmetrics = {\n    \"bowel\": [\"accuracy\"],\n    \"extra\": [\"accuracy\"],\n    \"liver\": [\"accuracy\"],\n    \"kidney\": [\"accuracy\"],\n    \"spleen\": [\"accuracy\"],\n}\n\nmodel.compile(\n    optimizer=keras.optimizers.Adam(learning_rate=cosine_lr(0.01,total_train_steps ,config.EPOCHS)),\n    loss=loss,\n    metrics=metrics\n)\n\nprint(\"[INFO] Training...\")\n\nhistory = model.fit(\n    train_ds,\n    epochs=config.EPOCHS,\n    validation_data=val_ds,\n)","metadata":{"execution":{"iopub.status.busy":"2023-09-20T16:52:43.046326Z","iopub.execute_input":"2023-09-20T16:52:43.047014Z","iopub.status.idle":"2023-09-20T17:37:02.008194Z","shell.execute_reply.started":"2023-09-20T16:52:43.046981Z","shell.execute_reply":"2023-09-20T17:37:02.007171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Create a 3x2 grid for the subplots\nfig, axes = plt.subplots(5, 1, figsize=(6, 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-09-20T17:37:02.016328Z","iopub.execute_input":"2023-09-20T17:37:02.019119Z","iopub.status.idle":"2023-09-20T17:37:03.514341Z","shell.execute_reply.started":"2023-09-20T17:37:02.019083Z","shell.execute_reply":"2023-09-20T17:37:03.513199Z"},"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-09-20T17:37:03.515811Z","iopub.execute_input":"2023-09-20T17:37:03.516276Z","iopub.status.idle":"2023-09-20T17:37:03.784889Z","shell.execute_reply.started":"2023-09-20T17:37:03.516239Z","shell.execute_reply":"2023-09-20T17:37:03.783799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_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\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.status.busy":"2023-09-20T17:37:03.786361Z","iopub.execute_input":"2023-09-20T17:37:03.786712Z","iopub.status.idle":"2023-09-20T17:37:03.795822Z","shell.execute_reply.started":"2023-09-20T17:37:03.786679Z","shell.execute_reply":"2023-09-20T17:37:03.79481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('/kaggle/working/rsna-Xception.h5')","metadata":{"execution":{"iopub.status.busy":"2023-09-20T17:51:19.353104Z","iopub.execute_input":"2023-09-20T17:51:19.35348Z","iopub.status.idle":"2023-09-20T17:51:19.801846Z","shell.execute_reply.started":"2023-09-20T17:51:19.353454Z","shell.execute_reply":"2023-09-20T17:51:19.800895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# model_2: VGG16","metadata":{}},{"cell_type":"code","source":"from keras.applications import VGG16\nimport tensorflow as tf\nimport keras  \n\nvgg16_model = VGG16(\n    weights='imagenet',\n    input_shape=config.IMAGE_SIZE + [3,],\n    include_top=False)\n\nvgg16_model.trainable = False\n\nfor layer in vgg16_model.layers[:-5]:\n    layer.trainable = False\n\ninputs = keras.Input(shape=config.IMAGE_SIZE + [3,], batch_size=config.BATCH_SIZE)\n\n\nx = vgg16_model(inputs, training=False)\n\nx = keras.layers.GlobalAveragePooling2D()(x)\n\nx_bowel = keras.layers.Dense(32, activation='silu')(x)\nx_extra = keras.layers.Dense(32, activation='silu')(x)\nx_liver = keras.layers.Dense(32, activation='silu')(x)\nx_kidney = keras.layers.Dense(32, activation='silu')(x)\nx_spleen = keras.layers.Dense(32, activation='silu')(x)\n\nout_bowel = keras.layers.Dense(2, name='bowel', activation='sigmoid')(x_bowel)\nout_extra = keras.layers.Dense(2, name='extra', activation='sigmoid')(x_extra)\nout_liver = keras.layers.Dense(3, name='liver', activation='softmax')(x_liver)\nout_kidney = keras.layers.Dense(3, name='kidney', activation='softmax')(x_kidney)\nout_spleen = keras.layers.Dense(3, name='spleen', activation='softmax')(x_spleen)\n\noutputs = [out_bowel, out_extra, out_liver, out_kidney, out_spleen]\n\nprint(\"[INFO] Building the model...\")\nmodel_2 = keras.Model(inputs, outputs)\n\nloss = {\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    }\nmetrics = {\n    \"bowel\": [\"accuracy\"],\n    \"extra\": [\"accuracy\"],\n    \"liver\": [\"accuracy\"],\n    \"kidney\": [\"accuracy\"],\n    \"spleen\": [\"accuracy\"],\n}\n\nmodel_2.compile(\n    optimizer=keras.optimizers.Adam(learning_rate=cosine_lr(0.01,total_train_steps ,config.EPOCHS)),\n    loss=loss,\n    metrics=metrics\n)\n\nprint(\"[INFO] Training...\")\nhistory = model_2.fit(\n    train_ds,\n    epochs=config.EPOCHS,\n    validation_data=val_ds,\n)","metadata":{"execution":{"iopub.status.busy":"2023-09-20T17:51:25.935817Z","iopub.execute_input":"2023-09-20T17:51:25.936517Z","iopub.status.idle":"2023-09-20T18:33:57.093509Z","shell.execute_reply.started":"2023-09-20T17:51:25.936478Z","shell.execute_reply":"2023-09-20T18:33:57.091591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Create a 3x2 grid for the subplots\nfig, axes = plt.subplots(5, 1, figsize=(6, 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-09-20T18:33:57.096808Z","iopub.execute_input":"2023-09-20T18:33:57.097122Z","iopub.status.idle":"2023-09-20T18:33:58.52862Z","shell.execute_reply.started":"2023-09-20T18:33:57.097096Z","shell.execute_reply":"2023-09-20T18:33:58.527765Z"},"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-09-20T18:33:58.529992Z","iopub.execute_input":"2023-09-20T18:33:58.530594Z","iopub.status.idle":"2023-09-20T18:33:58.780778Z","shell.execute_reply.started":"2023-09-20T18:33:58.53056Z","shell.execute_reply":"2023-09-20T18:33:58.779903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_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\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.status.busy":"2023-09-20T18:33:58.783331Z","iopub.execute_input":"2023-09-20T18:33:58.783755Z","iopub.status.idle":"2023-09-20T18:33:58.792827Z","shell.execute_reply.started":"2023-09-20T18:33:58.783722Z","shell.execute_reply":"2023-09-20T18:33:58.791858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_2.save('/kaggle/working/rsna-VGG16.h5')","metadata":{"execution":{"iopub.status.busy":"2023-09-20T20:17:05.915187Z","iopub.status.idle":"2023-09-20T20:17:05.915881Z","shell.execute_reply.started":"2023-09-20T20:17:05.915623Z","shell.execute_reply":"2023-09-20T20:17:05.915645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# model_3: InceptionV3","metadata":{}},{"cell_type":"code","source":"from keras.applications import InceptionV3\n\ninceptionv3_model = InceptionV3(\n    weights='imagenet',\n    input_shape=config.IMAGE_SIZE + [3,],\n    include_top=False)\n\ninceptionv3_model.trainable = False\n\nfor layer in inceptionv3_model.layers[:-5]:\n    layer.trainable = False\n\ninputs = keras.Input(shape=config.IMAGE_SIZE + [3,], batch_size=config.BATCH_SIZE)\n\nx = inceptionv3_model(inputs, training=False)\n\nx = keras.layers.GlobalAveragePooling2D()(x)\n\nx_bowel = keras.layers.Dense(32, activation='silu')(x)\nx_extra = keras.layers.Dense(32, activation='silu')(x)\nx_liver = keras.layers.Dense(32, activation='silu')(x)\nx_kidney = keras.layers.Dense(32, activation='silu')(x)\nx_spleen = keras.layers.Dense(32, activation='silu')(x)\n\nout_bowel = keras.layers.Dense(2, name='bowel', activation='sigmoid')(x_bowel)\nout_extra = keras.layers.Dense(2, name='extra', activation='sigmoid')(x_extra)\nout_liver = keras.layers.Dense(3, name='liver', activation='softmax')(x_liver)\nout_kidney = keras.layers.Dense(3, name='kidney', activation='softmax')(x_kidney)\nout_spleen = keras.layers.Dense(3, name='spleen', activation='softmax')(x_spleen)\n\noutputs = [out_bowel, out_extra, out_liver, out_kidney, out_spleen]\n\nprint(\"[INFO] Building the model...\")\nmodel_3 = keras.Model(inputs, outputs)\n\nloss = {\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    }\nmetrics = {\n    \"bowel\": [\"accuracy\"],\n    \"extra\": [\"accuracy\"],\n    \"liver\": [\"accuracy\"],\n    \"kidney\": [\"accuracy\"],\n    \"spleen\": [\"accuracy\"],\n}\n\nmodel_3.compile(\n    optimizer=keras.optimizers.Adam(learning_rate=cosine_lr(0.01,total_train_steps ,config.EPOCHS)),\n    loss=loss,\n    metrics=metrics\n)\n\nprint(\"[INFO] Training...\")\nhistory = model_3.fit(\n    train_ds,\n    epochs=config.EPOCHS,\n    validation_data=val_ds,\n)","metadata":{"execution":{"iopub.status.busy":"2023-09-20T18:33:58.794249Z","iopub.execute_input":"2023-09-20T18:33:58.794831Z","iopub.status.idle":"2023-09-20T19:13:49.361239Z","shell.execute_reply.started":"2023-09-20T18:33:58.794798Z","shell.execute_reply":"2023-09-20T19:13:49.360204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Create a 3x2 grid for the subplots\nfig, axes = plt.subplots(5, 1, figsize=(6, 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-09-20T19:13:49.570594Z","iopub.execute_input":"2023-09-20T19:13:49.570977Z","iopub.status.idle":"2023-09-20T19:13:50.88583Z","shell.execute_reply.started":"2023-09-20T19:13:49.570944Z","shell.execute_reply":"2023-09-20T19:13:50.884937Z"},"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-09-20T19:13:50.88736Z","iopub.execute_input":"2023-09-20T19:13:50.887978Z","iopub.status.idle":"2023-09-20T19:13:51.162898Z","shell.execute_reply.started":"2023-09-20T19:13:50.887941Z","shell.execute_reply":"2023-09-20T19:13:51.161813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_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\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.status.busy":"2023-09-20T19:13:51.164238Z","iopub.execute_input":"2023-09-20T19:13:51.164668Z","iopub.status.idle":"2023-09-20T19:13:51.174205Z","shell.execute_reply.started":"2023-09-20T19:13:51.164635Z","shell.execute_reply":"2023-09-20T19:13:51.173072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_3.save('/kaggle/working/rsna-Inception.h5')","metadata":{"execution":{"iopub.status.busy":"2023-09-20T19:13:51.179205Z","iopub.execute_input":"2023-09-20T19:13:51.179678Z","iopub.status.idle":"2023-09-20T19:13:51.783456Z","shell.execute_reply.started":"2023-09-20T19:13:51.179599Z","shell.execute_reply":"2023-09-20T19:13:51.782456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# model_4: InceptionResNetV2","metadata":{}},{"cell_type":"code","source":"from keras.applications import InceptionResNetV2\n\n# Replace DenseNet201 with InceptionResNetV2\ninception_resnet_v2_model = InceptionResNetV2(\n    weights='imagenet',\n    input_shape=config.IMAGE_SIZE + [3,],\n    include_top=False)\n\n# Freeze the first few layers of the model\nfor layer in inception_resnet_v2_model.layers[:-5]:\n    layer.trainable = False\n\n# Update the output layer(s) of the model\ninputs = keras.Input(shape=config.IMAGE_SIZE + [3,], batch_size=config.BATCH_SIZE)\n\nx = inception_resnet_v2_model(inputs, training=False)\n\nx = keras.layers.GlobalAveragePooling2D()(x)\n\nx_bowel = keras.layers.Dense(32, activation='silu')(x)\nx_extra = keras.layers.Dense(32, activation='silu')(x)\nx_liver = keras.layers.Dense(32, activation='silu')(x)\nx_kidney = keras.layers.Dense(32, activation='silu')(x)\nx_spleen = keras.layers.Dense(32, activation='silu')(x)\n\nout_bowel = keras.layers.Dense(2, name='bowel', activation='sigmoid')(x_bowel)\nout_extra = keras.layers.Dense(2, name='extra', activation='sigmoid')(x_extra)\nout_liver = keras.layers.Dense(3, name='liver', activation='softmax')(x_liver)\nout_kidney = keras.layers.Dense(3, name='kidney', activation='softmax')(x_kidney)\nout_spleen = keras.layers.Dense(3, name='spleen', activation='softmax')(x_spleen)\n\noutputs = [out_bowel, out_extra, out_liver, out_kidney, out_spleen]\n\n# Compile the model\nmodel_4 = keras.Model(inputs, outputs)\n\nloss = {\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}\nmetrics = {\n    \"bowel\": [\"accuracy\"],\n    \"extra\": [\"accuracy\"],\n    \"liver\": [\"accuracy\"],\n    \"kidney\": [\"accuracy\"],\n    \"spleen\": [\"accuracy\"],\n}\n\nmodel_4.compile(\n    optimizer=keras.optimizers.Adam(learning_rate=cosine_lr(0.01,total_train_steps ,config.EPOCHS)),\n    loss=loss,\n    metrics=metrics\n)\n\n# Train the model\nhistory = model_4.fit(\n    train_ds,\n    epochs=config.EPOCHS,\n    validation_data=val_ds,\n)","metadata":{"execution":{"iopub.status.busy":"2023-09-20T21:06:39.003811Z","iopub.execute_input":"2023-09-20T21:06:39.004154Z","iopub.status.idle":"2023-09-20T21:56:27.532436Z","shell.execute_reply.started":"2023-09-20T21:06:39.004127Z","shell.execute_reply":"2023-09-20T21:56:27.531096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Create a 3x2 grid for the subplots\nfig, axes = plt.subplots(5, 1, figsize=(6, 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-09-20T21:56:27.534474Z","iopub.execute_input":"2023-09-20T21:56:27.534821Z","iopub.status.idle":"2023-09-20T21:56:28.745831Z","shell.execute_reply.started":"2023-09-20T21:56:27.534787Z","shell.execute_reply":"2023-09-20T21:56:28.744762Z"},"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-09-20T21:56:28.747421Z","iopub.execute_input":"2023-09-20T21:56:28.747803Z","iopub.status.idle":"2023-09-20T21:56:29.011576Z","shell.execute_reply.started":"2023-09-20T21:56:28.747768Z","shell.execute_reply":"2023-09-20T21:56:29.010667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_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\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.status.busy":"2023-09-20T21:56:29.014617Z","iopub.execute_input":"2023-09-20T21:56:29.016123Z","iopub.status.idle":"2023-09-20T21:56:29.025937Z","shell.execute_reply.started":"2023-09-20T21:56:29.016089Z","shell.execute_reply":"2023-09-20T21:56:29.02501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_4.save('/kaggle/working/rsna-InceptionResNetV2.h5')","metadata":{"execution":{"iopub.status.busy":"2023-09-20T21:56:29.027423Z","iopub.execute_input":"2023-09-20T21:56:29.028006Z","iopub.status.idle":"2023-09-20T21:56:30.573412Z","shell.execute_reply.started":"2023-09-20T21:56:29.027964Z","shell.execute_reply":"2023-09-20T21:56:30.572391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# model_5: DenseNet201","metadata":{}},{"cell_type":"code","source":"from keras.applications import DenseNet201\n\n# Replace AlexNet with DenseNet201\ndensenet_model = DenseNet201(\n    weights='imagenet',\n    input_shape=config.IMAGE_SIZE + [3,],\n    include_top=False)\n\n# Freeze the first few layers of the model\nfor layer in densenet_model.layers[:-5]:\n    layer.trainable = False\n\n# Update the output layer(s) of the model\ninputs = keras.Input(shape=config.IMAGE_SIZE + [3,], batch_size=config.BATCH_SIZE)\n\nx = densenet_model(inputs, training=False)\n\nx = keras.layers.GlobalAveragePooling2D()(x)\n\nx_bowel = keras.layers.Dense(32, activation='silu')(x)\nx_extra = keras.layers.Dense(32, activation='silu')(x)\nx_liver = keras.layers.Dense(32, activation='silu')(x)\nx_kidney = keras.layers.Dense(32, activation='silu')(x)\nx_spleen = keras.layers.Dense(32, activation='silu')(x)\n\nout_bowel = keras.layers.Dense(2, name='bowel', activation='sigmoid')(x_bowel)\nout_extra = keras.layers.Dense(2, name='extra', activation='sigmoid')(x_extra)\nout_liver = keras.layers.Dense(3, name='liver', activation='softmax')(x_liver)\nout_kidney = keras.layers.Dense(3, name='kidney', activation='softmax')(x_kidney)\nout_spleen = keras.layers.Dense(3, name='spleen', activation='softmax')(x_spleen)\n\noutputs = [out_bowel, out_extra, out_liver, out_kidney, out_spleen]\n\n# Compile the model\nmodel_5 = keras.Model(inputs, outputs)\n\nloss = {\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}\nmetrics = {\n    \"bowel\": [\"accuracy\"],\n    \"extra\": [\"accuracy\"],\n    \"liver\": [\"accuracy\"],\n    \"kidney\": [\"accuracy\"],\n    \"spleen\": [\"accuracy\"],\n}\n\nmodel_5.compile(\n    optimizer=keras.optimizers.Adam(learning_rate=cosine_lr(0.01,total_train_steps ,config.EPOCHS)),\n    loss=loss,\n    metrics=metrics\n)\n\n# Train the model\nhistory = model_5.fit(\n    train_ds,\n    epochs=config.EPOCHS,\n    validation_data=val_ds,\n)","metadata":{"execution":{"iopub.status.busy":"2023-09-20T20:20:51.756841Z","iopub.execute_input":"2023-09-20T20:20:51.757252Z","iopub.status.idle":"2023-09-20T21:06:35.055041Z","shell.execute_reply.started":"2023-09-20T20:20:51.757223Z","shell.execute_reply":"2023-09-20T21:06:35.053939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Create a 3x2 grid for the subplots\nfig, axes = plt.subplots(5, 1, figsize=(6, 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-09-20T21:06:35.057601Z","iopub.execute_input":"2023-09-20T21:06:35.058071Z","iopub.status.idle":"2023-09-20T21:06:36.293509Z","shell.execute_reply.started":"2023-09-20T21:06:35.058036Z","shell.execute_reply":"2023-09-20T21:06:36.292614Z"},"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-09-20T21:06:36.295042Z","iopub.execute_input":"2023-09-20T21:06:36.295622Z","iopub.status.idle":"2023-09-20T21:06:36.543737Z","shell.execute_reply.started":"2023-09-20T21:06:36.295588Z","shell.execute_reply":"2023-09-20T21:06:36.542784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_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\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.status.busy":"2023-09-20T21:06:36.54648Z","iopub.execute_input":"2023-09-20T21:06:36.546808Z","iopub.status.idle":"2023-09-20T21:06:36.556Z","shell.execute_reply.started":"2023-09-20T21:06:36.546782Z","shell.execute_reply":"2023-09-20T21:06:36.55485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_5.save('/kaggle/working/rsna-DenseNet201.h5')","metadata":{"execution":{"iopub.status.busy":"2023-09-20T21:06:37.833889Z","iopub.execute_input":"2023-09-20T21:06:37.834265Z","iopub.status.idle":"2023-09-20T21:06:38.999353Z","shell.execute_reply.started":"2023-09-20T21:06:37.834232Z","shell.execute_reply":"2023-09-20T21:06:38.998237Z"},"trusted":true},"execution_count":null,"outputs":[]}]}