{"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"},{"sourceId":145103181,"sourceType":"kernelVersion"}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install pydot graphviz","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nfrom tqdm import tqdm\nimport keras","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-27T00:54:32.314672Z","iopub.execute_input":"2023-11-27T00:54:32.315031Z","iopub.status.idle":"2023-11-27T00:54:44.608472Z","shell.execute_reply.started":"2023-11-27T00:54:32.315001Z","shell.execute_reply":"2023-11-27T00:54:44.607504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = pd.read_csv('/kaggle/input/rsna-2023-abdominal-trauma-detection/train.csv')\nlabels","metadata":{"execution":{"iopub.status.busy":"2023-11-27T00:54:44.610414Z","iopub.execute_input":"2023-11-27T00:54:44.611604Z","iopub.status.idle":"2023-11-27T00:54:44.655202Z","shell.execute_reply.started":"2023-11-27T00:54:44.611563Z","shell.execute_reply":"2023-11-27T00:54:44.654211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data = {\"id\": [], \"X\": []}\ndirect = \"/kaggle/input/rsna23-dicom-to-3d-array-128x128x128-fixed\"\nfor path in tqdm(os.listdir(direct)):\n    file_path = direct + '/' + path\n    if not path.endswith('.npy'):\n        continue\n    start_index = file_path.find(\"train_images\") + len(\"train_images\")\n    end_index = file_path.find(\"_\", start_index)\n\n    # Extract the number\n    number = file_path[start_index:end_index]\n    if number in all_data['id']:\n        continue\n    all_data['id'].append(number)\n    all_data['X'].append(np.load(file_path, allow_pickle=True))","metadata":{"execution":{"iopub.status.busy":"2023-11-27T00:54:44.656522Z","iopub.execute_input":"2023-11-27T00:54:44.656878Z","iopub.status.idle":"2023-11-27T00:55:37.145158Z","shell.execute_reply.started":"2023-11-27T00:54:44.656851Z","shell.execute_reply":"2023-11-27T00:55:37.144234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data['labels'] = []","metadata":{"execution":{"iopub.status.busy":"2023-11-27T00:55:37.147411Z","iopub.execute_input":"2023-11-27T00:55:37.147751Z","iopub.status.idle":"2023-11-27T00:55:37.152158Z","shell.execute_reply.started":"2023-11-27T00:55:37.147725Z","shell.execute_reply":"2023-11-27T00:55:37.151097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for id in all_data['id']:\n    n = np.delete(labels[labels['patient_id']==int(id)].values[0][1:], [1,3,13])\n#     cur = np.array([np.array([n[0]]), np.array(n[1]), np.array([n[2],n[3],n[4]]), np.array([n[5],n[6],n[7]]), np.array([n[8],n[9],n[10]])], dtype=object)\n#     print(cur, cur.shape)\n#     break\n    all_data[\"labels\"].append(n)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T00:55:37.153536Z","iopub.execute_input":"2023-11-27T00:55:37.153937Z","iopub.status.idle":"2023-11-27T00:55:38.126622Z","shell.execute_reply.started":"2023-11-27T00:55:37.153883Z","shell.execute_reply":"2023-11-27T00:55:38.125763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(all_data['id']),len(all_data['X']),len(all_data['labels'])","metadata":{"execution":{"iopub.status.busy":"2023-11-27T00:55:38.127755Z","iopub.execute_input":"2023-11-27T00:55:38.128078Z","iopub.status.idle":"2023-11-27T00:55:38.135923Z","shell.execute_reply.started":"2023-11-27T00:55:38.12805Z","shell.execute_reply":"2023-11-27T00:55:38.134806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = np.array(all_data['X'])\ny = np.array(all_data['labels'])\n\nX.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-27T00:55:38.137362Z","iopub.execute_input":"2023-11-27T00:55:38.13773Z","iopub.status.idle":"2023-11-27T00:55:40.052413Z","shell.execute_reply.started":"2023-11-27T00:55:38.137691Z","shell.execute_reply":"2023-11-27T00:55:40.051538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del all_data, labels","metadata":{"execution":{"iopub.status.busy":"2023-11-27T00:55:40.053758Z","iopub.execute_input":"2023-11-27T00:55:40.05412Z","iopub.status.idle":"2023-11-27T00:55:40.458121Z","shell.execute_reply.started":"2023-11-27T00:55:40.054087Z","shell.execute_reply":"2023-11-27T00:55:40.456891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T00:55:40.460098Z","iopub.execute_input":"2023-11-27T00:55:40.460826Z","iopub.status.idle":"2023-11-27T00:55:43.034144Z","shell.execute_reply.started":"2023-11-27T00:55:40.460792Z","shell.execute_reply":"2023-11-27T00:55:43.033024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del X, y","metadata":{"execution":{"iopub.status.busy":"2023-11-27T00:55:43.038165Z","iopub.execute_input":"2023-11-27T00:55:43.038802Z","iopub.status.idle":"2023-11-27T00:55:43.05553Z","shell.execute_reply.started":"2023-11-27T00:55:43.038771Z","shell.execute_reply":"2023-11-27T00:55:43.05445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape, X_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-27T00:55:43.056776Z","iopub.execute_input":"2023-11-27T00:55:43.057677Z","iopub.status.idle":"2023-11-27T00:55:43.068373Z","shell.execute_reply.started":"2023-11-27T00:55:43.057651Z","shell.execute_reply":"2023-11-27T00:55:43.067566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_model():\n    \n    input_layer = keras.layers.Input(shape=(128, 128, 128, 1))\n\n    conv1 = keras.layers.Conv3D(16, (5, 5, 5), 2, activation='elu')(input_layer)\n    pool1 = keras.layers.MaxPooling3D((3, 3, 3))(conv1)\n    conv2 = keras.layers.Conv3D(32, (3, 3, 3), activation='elu')(pool1)\n    pool2 = keras.layers.MaxPooling3D((2, 2, 2))(conv2)\n    conv3 = keras.layers.Conv3D(32, (3, 3, 3), activation='elu')(pool1)\n    pool3 = keras.layers.MaxPooling3D((2, 2, 2))(conv3)\n\n    flatten = keras.layers.Flatten()(pool2)\n    \n    x = keras.layers.Dense(512, activation='elu')(flatten)\n    x = keras.layers.BatchNormalization()(x)\n    x = tf.keras.layers.Dropout(0.5)(x)\n    \n    x_bowel = keras.layers.Dense(128, activation='elu')(x)\n    x_extra = keras.layers.Dense(128, activation='elu')(x)\n    x_liver = keras.layers.Dense(128, activation='elu')(x)\n    x_kidney = keras.layers.Dense(128, activation='elu')(x)\n    x_spleen = keras.layers.Dense(128, activation='elu')(x)\n    \n    x_bowel = tf.keras.layers.BatchNormalization()(x_bowel)\n    x_extra = tf.keras.layers.BatchNormalization()(x_extra)\n    x_liver = tf.keras.layers.BatchNormalization()(x_liver)\n    x_kidney = tf.keras.layers.BatchNormalization()(x_kidney)\n    x_spleen = tf.keras.layers.BatchNormalization()(x_spleen)\n    \n    x_bowel = tf.keras.layers.Dropout(0.3)(x_bowel)\n    x_extra = tf.keras.layers.Dropout(0.3)(x_extra)\n    x_liver = tf.keras.layers.Dropout(0.3)(x_liver)\n    x_kidney = tf.keras.layers.Dropout(0.3)(x_kidney)\n    x_spleen = tf.keras.layers.Dropout(0.3)(x_spleen)\n    \n    x_bowel = keras.layers.Dense(16, activation='elu')(x_bowel)\n    x_extra = keras.layers.Dense(16, activation='elu')(x_extra)\n    x_liver = keras.layers.Dense(16, activation='elu')(x_liver)\n    x_kidney = keras.layers.Dense(16, activation='elu')(x_kidney)\n    x_spleen = keras.layers.Dense(16, activation='elu')(x_spleen)\n    \n    x_bowel = tf.keras.layers.BatchNormalization()(x_bowel)\n    x_extra = tf.keras.layers.BatchNormalization()(x_extra)\n    x_liver = tf.keras.layers.BatchNormalization()(x_liver)\n    x_kidney = tf.keras.layers.BatchNormalization()(x_kidney)\n    x_spleen = tf.keras.layers.BatchNormalization()(x_spleen)\n\n    x_bowel = tf.keras.layers.Dropout(0.2)(x_bowel)\n    x_extra = tf.keras.layers.Dropout(0.2)(x_extra)\n    x_liver = tf.keras.layers.Dropout(0.2)(x_liver)\n    x_kidney = tf.keras.layers.Dropout(0.2)(x_kidney)\n    x_spleen = tf.keras.layers.Dropout(0.2)(x_spleen)\n    \n    out_bowel = keras.layers.Dense(1, name='bowel', activation='sigmoid')(x_bowel) \n    out_extra = keras.layers.Dense(1, name='extra', activation='sigmoid')(x_extra) \n    out_liver = keras.layers.Dense(3, name='liver', activation='softmax')(x_liver) \n    out_kidney = keras.layers.Dense(3, name='kidney', activation='softmax')(x_kidney) \n    out_spleen = keras.layers.Dense(3, name='spleen', activation='softmax')(x_spleen) \n    \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=input_layer, outputs=outputs)\n    \n    optimizer = keras.optimizers.Adam(learning_rate=1e-4)\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\", tf.keras.metrics.AUC()],\n        \"extra\":[\"accuracy\", tf.keras.metrics.AUC()],\n        \"liver\":[\"accuracy\", tf.keras.metrics.AUC()],\n        \"kidney\":[\"accuracy\", tf.keras.metrics.AUC()],\n        \"spleen\":[\"accuracy\", tf.keras.metrics.AUC()],\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-27T01:02:26.003529Z","iopub.execute_input":"2023-11-27T01:02:26.003951Z","iopub.status.idle":"2023-11-27T01:02:26.030941Z","shell.execute_reply.started":"2023-11-27T01:02:26.003916Z","shell.execute_reply":"2023-11-27T01:02:26.029826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def decode_label(arr, label):\n    image = tf.cast(arr, tf.float32)\n    label = tf.cast(label, tf.float32)\n    \n    labels = (label[0:1], label[1:2], label[2:5], label[5:8], label[8:11])\n    return (image, labels)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T00:55:43.097541Z","iopub.execute_input":"2023-11-27T00:55:43.09784Z","iopub.status.idle":"2023-11-27T00:55:43.107845Z","shell.execute_reply.started":"2023-11-27T00:55:43.097815Z","shell.execute_reply":"2023-11-27T00:55:43.106996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = tf.data.Dataset.from_tensor_slices((X_train, y_train)).map(decode_label).batch(32)\ntest_ds = tf.data.Dataset.from_tensor_slices((X_test, y_test)).map(decode_label).batch(32)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T00:55:43.108997Z","iopub.execute_input":"2023-11-27T00:55:43.109328Z","iopub.status.idle":"2023-11-27T00:56:02.641247Z","shell.execute_reply.started":"2023-11-27T00:55:43.109302Z","shell.execute_reply":"2023-11-27T00:56:02.640359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del X_train, X_test, y_train, y_test","metadata":{"execution":{"iopub.status.busy":"2023-11-27T00:56:02.642549Z","iopub.execute_input":"2023-11-27T00:56:02.64294Z","iopub.status.idle":"2023-11-27T00:56:02.661401Z","shell.execute_reply.started":"2023-11-27T00:56:02.642909Z","shell.execute_reply":"2023-11-27T00:56:02.660171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils import plot_model\n\nprint(\"[INFO] Building the model...\")\nmodel = build_model()\n\nplot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T01:02:28.81586Z","iopub.execute_input":"2023-11-27T01:02:28.816261Z","iopub.status.idle":"2023-11-27T01:02:29.594158Z","shell.execute_reply.started":"2023-11-27T01:02:28.816229Z","shell.execute_reply":"2023-11-27T01:02:29.592827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"[INFO] Training...\")\nhistory = model.fit(\n    train_ds,\n    epochs=100,\n    validation_data=test_ds,\n#     callbacks=[early_stopping]\n)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T01:02:39.265996Z","iopub.execute_input":"2023-11-27T01:02:39.266702Z","iopub.status.idle":"2023-11-27T01:28:36.934044Z","shell.execute_reply.started":"2023-11-27T01:02:39.266668Z","shell.execute_reply":"2023-11-27T01:28:36.932881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n# 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-27T01:39:07.376352Z","iopub.execute_input":"2023-11-27T01:39:07.376827Z","iopub.status.idle":"2023-11-27T01:39:08.831102Z","shell.execute_reply.started":"2023-11-27T01:39:07.37679Z","shell.execute_reply":"2023-11-27T01:39:08.830026Z"},"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-27T01:39:11.737571Z","iopub.execute_input":"2023-11-27T01:39:11.738653Z","iopub.status.idle":"2023-11-27T01:39:11.942351Z","shell.execute_reply.started":"2023-11-27T01:39:11.738608Z","shell.execute_reply":"2023-11-27T01:39:11.941371Z"},"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]\nbest_auc_bowel = history.history['val_bowel_auc_5'][best_epoch]\nbest_auc_extra = history.history['val_extra_auc_6'][best_epoch]\nbest_auc_liver = history.history['val_liver_auc_7'][best_epoch]\nbest_auc_kidney = history.history['val_kidney_auc_8'][best_epoch]\nbest_auc_spleen = history.history['val_spleen_auc_9'][best_epoch]\n\n\n# Find mean accuracy\nbest_acc = np.mean(\n    [best_auc_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}')\nprint('ORGAN AUC:')\nprint(f'  >>>> {\"Bowel\".ljust(15)} : {best_auc_bowel:.3f}')\nprint(f'  >>>> {\"Extravasation\".ljust(15)} : {best_auc_extra:.3f}')\nprint(f'  >>>> {\"Liver\".ljust(15)} : {best_auc_liver:.3f}')\nprint(f'  >>>> {\"Kidney\".ljust(15)} : {best_auc_kidney:.3f}')\nprint(f'  >>>> {\"Spleen\".ljust(15)} : {best_auc_spleen:.3f}')","metadata":{"execution":{"iopub.status.busy":"2023-11-27T01:39:35.447451Z","iopub.execute_input":"2023-11-27T01:39:35.448138Z","iopub.status.idle":"2023-11-27T01:39:35.461094Z","shell.execute_reply.started":"2023-11-27T01:39:35.448105Z","shell.execute_reply":"2023-11-27T01:39:35.459968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}