{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","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"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":52254,"databundleVersionId":8756537,"sourceType":"competition"},{"sourceId":6211844,"sourceType":"datasetVersion","datasetId":3567114}],"dockerImageVersionId":30699,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# <center>Medical image ...</center>\n\n","metadata":{"papermill":{"duration":0.025483,"end_time":"2022-02-01T10:21:43.84374","exception":false,"start_time":"2022-02-01T10:21:43.818257","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"## To commence this project, the neccesary libraries need to be installed.\n## We would be using the **TensorFlow** framework and the pretrained **EfficientNetB1**\n","metadata":{}},{"cell_type":"code","source":"# Imporitng libraries\nimport pandas as pd\nimport os\nimport random\nimport numpy as np\nimport tensorflow as tf\nimport cv2\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom IPython.display import clear_output\nfrom sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay\nfrom sklearn.model_selection import StratifiedKFold\nfrom tensorflow.keras import backend as K\n\nrandom.seed(42)","metadata":{"id":"fUPKjZaaJHkM","papermill":{"duration":5.021373,"end_time":"2022-02-01T10:21:48.88737","exception":false,"start_time":"2022-02-01T10:21:43.865997","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-08-07T16:22:54.305548Z","iopub.execute_input":"2024-08-07T16:22:54.305856Z","iopub.status.idle":"2024-08-07T16:23:07.787936Z","shell.execute_reply.started":"2024-08-07T16:22:54.305831Z","shell.execute_reply":"2024-08-07T16:23:07.786974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Loading the training dataset\ntrain_img = \"/kaggle/input/rsna-atd-512x512-png-v2-dataset/train_images\"","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:07.790067Z","iopub.execute_input":"2024-08-07T16:23:07.790732Z","iopub.status.idle":"2024-08-07T16:23:07.794937Z","shell.execute_reply.started":"2024-08-07T16:23:07.790693Z","shell.execute_reply":"2024-08-07T16:23:07.794063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in os.listdir(train_img):\n    for j in os.listdir(os.path.join(train_img,i)):\n        print(len(os.listdir(os.path.join(train_img,i,j))))","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:07.796095Z","iopub.execute_input":"2024-08-07T16:23:07.796375Z","iopub.status.idle":"2024-08-07T16:23:13.878584Z","shell.execute_reply.started":"2024-08-07T16:23:07.796352Z","shell.execute_reply":"2024-08-07T16:23:13.877675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Part A\n## Import and explore the data","metadata":{"papermill":{"duration":0.02193,"end_time":"2022-02-01T10:21:48.93356","exception":false,"start_time":"2022-02-01T10:21:48.91163","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# Making a list containing all unique classes in the training set\n\n# Reading the training labels\ntraining_labels = pd.read_csv(\"/kaggle/input/rsna-2023-abdominal-trauma-detection/train_2024.csv\")","metadata":{"id":"PBlH8ae3LbG0","papermill":{"duration":0.069238,"end_time":"2022-02-01T10:21:49.024476","exception":false,"start_time":"2022-02-01T10:21:48.955238","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-08-07T16:23:13.881514Z","iopub.execute_input":"2024-08-07T16:23:13.8818Z","iopub.status.idle":"2024-08-07T16:23:13.899996Z","shell.execute_reply.started":"2024-08-07T16:23:13.881777Z","shell.execute_reply":"2024-08-07T16:23:13.899336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Viewing the dataset in a structured format\ntraining_labels","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:13.900937Z","iopub.execute_input":"2024-08-07T16:23:13.901196Z","iopub.status.idle":"2024-08-07T16:23:13.926189Z","shell.execute_reply.started":"2024-08-07T16:23:13.901175Z","shell.execute_reply":"2024-08-07T16:23:13.925352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Part B\n## In the next few cells, we chose to explore the dataframe to check for missing values","metadata":{}},{"cell_type":"code","source":"training_labels.columns","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:13.927229Z","iopub.execute_input":"2024-08-07T16:23:13.927474Z","iopub.status.idle":"2024-08-07T16:23:13.933242Z","shell.execute_reply.started":"2024-08-07T16:23:13.927452Z","shell.execute_reply":"2024-08-07T16:23:13.93241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels['patient_id'].isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:13.934423Z","iopub.execute_input":"2024-08-07T16:23:13.934828Z","iopub.status.idle":"2024-08-07T16:23:13.950566Z","shell.execute_reply.started":"2024-08-07T16:23:13.934798Z","shell.execute_reply":"2024-08-07T16:23:13.949772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels['bowel_healthy'].isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:13.951626Z","iopub.execute_input":"2024-08-07T16:23:13.951952Z","iopub.status.idle":"2024-08-07T16:23:13.958834Z","shell.execute_reply.started":"2024-08-07T16:23:13.951924Z","shell.execute_reply":"2024-08-07T16:23:13.958051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels['bowel_injury'].isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:13.959784Z","iopub.execute_input":"2024-08-07T16:23:13.960023Z","iopub.status.idle":"2024-08-07T16:23:13.970055Z","shell.execute_reply.started":"2024-08-07T16:23:13.959987Z","shell.execute_reply":"2024-08-07T16:23:13.969202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels['extravasation_healthy'].isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:13.97345Z","iopub.execute_input":"2024-08-07T16:23:13.973711Z","iopub.status.idle":"2024-08-07T16:23:13.982751Z","shell.execute_reply.started":"2024-08-07T16:23:13.97369Z","shell.execute_reply":"2024-08-07T16:23:13.981931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels['extravasation_injury'].isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:13.983959Z","iopub.execute_input":"2024-08-07T16:23:13.984289Z","iopub.status.idle":"2024-08-07T16:23:13.99408Z","shell.execute_reply.started":"2024-08-07T16:23:13.98426Z","shell.execute_reply":"2024-08-07T16:23:13.993344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels['kidney_healthy'].isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:13.995079Z","iopub.execute_input":"2024-08-07T16:23:13.995344Z","iopub.status.idle":"2024-08-07T16:23:14.006199Z","shell.execute_reply.started":"2024-08-07T16:23:13.995323Z","shell.execute_reply":"2024-08-07T16:23:14.005364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels['kidney_low'].isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:14.007166Z","iopub.execute_input":"2024-08-07T16:23:14.00745Z","iopub.status.idle":"2024-08-07T16:23:14.01815Z","shell.execute_reply.started":"2024-08-07T16:23:14.007424Z","shell.execute_reply":"2024-08-07T16:23:14.017327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels['kidney_high'].isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:14.019224Z","iopub.execute_input":"2024-08-07T16:23:14.019475Z","iopub.status.idle":"2024-08-07T16:23:14.031154Z","shell.execute_reply.started":"2024-08-07T16:23:14.019454Z","shell.execute_reply":"2024-08-07T16:23:14.03027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels['liver_healthy'].isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:14.032163Z","iopub.execute_input":"2024-08-07T16:23:14.032432Z","iopub.status.idle":"2024-08-07T16:23:14.043149Z","shell.execute_reply.started":"2024-08-07T16:23:14.03241Z","shell.execute_reply":"2024-08-07T16:23:14.042322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels['liver_low'].isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:14.044169Z","iopub.execute_input":"2024-08-07T16:23:14.044396Z","iopub.status.idle":"2024-08-07T16:23:14.054116Z","shell.execute_reply.started":"2024-08-07T16:23:14.044371Z","shell.execute_reply":"2024-08-07T16:23:14.053313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels['liver_high'].isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:14.05504Z","iopub.execute_input":"2024-08-07T16:23:14.055303Z","iopub.status.idle":"2024-08-07T16:23:14.066272Z","shell.execute_reply.started":"2024-08-07T16:23:14.055275Z","shell.execute_reply":"2024-08-07T16:23:14.065472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels['spleen_healthy'].isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:14.067366Z","iopub.execute_input":"2024-08-07T16:23:14.067642Z","iopub.status.idle":"2024-08-07T16:23:14.078954Z","shell.execute_reply.started":"2024-08-07T16:23:14.067619Z","shell.execute_reply":"2024-08-07T16:23:14.078123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels['spleen_low'].isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:14.080193Z","iopub.execute_input":"2024-08-07T16:23:14.080512Z","iopub.status.idle":"2024-08-07T16:23:14.090629Z","shell.execute_reply.started":"2024-08-07T16:23:14.080483Z","shell.execute_reply":"2024-08-07T16:23:14.089916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels['spleen_high'].isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:14.091732Z","iopub.execute_input":"2024-08-07T16:23:14.092072Z","iopub.status.idle":"2024-08-07T16:23:14.101561Z","shell.execute_reply.started":"2024-08-07T16:23:14.092042Z","shell.execute_reply":"2024-08-07T16:23:14.100749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels['any_injury'].isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:14.102767Z","iopub.execute_input":"2024-08-07T16:23:14.103503Z","iopub.status.idle":"2024-08-07T16:23:14.113538Z","shell.execute_reply.started":"2024-08-07T16:23:14.103469Z","shell.execute_reply":"2024-08-07T16:23:14.112715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Metrics(tf.keras.metrics.Metric):\n    def __init__(self, name='metrics', **kwargs):\n        super(Metrics, self).__init__(name=name, **kwargs)\n        self.precision = tf.keras.metrics.Precision()\n        self.recall = tf.keras.metrics.Recall()\n\n    def update_state(self, y_true, y_pred, sample_weight=None):\n        self.precision.update_state(y_true, y_pred, sample_weight)\n        self.recall.update_state(y_true, y_pred, sample_weight)\n\n    def result(self):\n        return {\n            \"precision\": self.precision.result(),\n            \"recall\": self.recall.result(),\n            \"f1_score\": 2 * ((self.precision.result() * self.recall.result()) / (self.precision.result() + self.recall.result() + K.epsilon()))\n        }\n\n    def reset_states(self):\n        self.precision.reset_states()\n        self.recall.reset_states()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:14.114559Z","iopub.execute_input":"2024-08-07T16:23:14.114868Z","iopub.status.idle":"2024-08-07T16:23:14.124345Z","shell.execute_reply.started":"2024-08-07T16:23:14.114839Z","shell.execute_reply":"2024-08-07T16:23:14.123512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_model(decay_steps=1000, warmup_steps=10):\n    base_model = tf.keras.applications.EfficientNetV2M(\n        weights=\"imagenet\", \n        include_top=False, \n        input_shape=(512, 512, 3)  # Original input size\n    )\n\n    x = base_model.output\n    x = tf.keras.layers.GlobalAveragePooling2D()(x)\n    x = tf.keras.layers.Dropout(0.2)(x)\n    \n    x_bowel = tf.keras.layers.Dense(32, activation='silu')(x)\n    x_extra = tf.keras.layers.Dense(32, activation='silu')(x)\n    x_liver = tf.keras.layers.Dense(32, activation='silu')(x)\n    x_kidney = tf.keras.layers.Dense(32, activation='silu')(x)\n    x_spleen = tf.keras.layers.Dense(32, activation='silu')(x)\n\n    # Define heads\n    out_bowel = tf.keras.layers.Dense(1, activation='sigmoid', name='bowel')(x_bowel)\n    out_extra = tf.keras.layers.Dense(1, activation='sigmoid', name='extra')(x_extra)\n    out_liver = tf.keras.layers.Dense(3, activation='softmax', name='liver')(x_liver)\n    out_kidney = tf.keras.layers.Dense(3, activation='softmax', name='kidney')(x_kidney)\n    out_spleen = tf.keras.layers.Dense(3, activation='softmax', name='spleen')(x_spleen)\n\n    model = tf.keras.Model(inputs=base_model.input, outputs=[out_bowel, out_extra, out_liver, out_kidney, out_spleen])\n\n    # Cosine Decay\n    cosine_decay = tf.keras.optimizers.schedules.CosineDecay(\n        initial_learning_rate=1e-4,\n        decay_steps=decay_steps,\n        alpha=0.0\n    )\n\n    # Compile the model\n    optimizer = tf.keras.optimizers.Adam(learning_rate=cosine_decay)\n    loss = [\n        tf.keras.losses.BinaryCrossentropy(),\n        tf.keras.losses.BinaryCrossentropy(),\n        tf.keras.losses.CategoricalCrossentropy(),\n        tf.keras.losses.CategoricalCrossentropy(),\n        tf.keras.losses.CategoricalCrossentropy()\n    ]\n    \n    metrics = [\n        [tf.keras.metrics.BinaryAccuracy(name=\"bowel_binary_accuracy\"), Metrics(name=\"bowel_metrics\")],\n        [tf.keras.metrics.BinaryAccuracy(name=\"extra_binary_accuracy\"), Metrics(name=\"extra_metrics\")],\n        [tf.keras.metrics.CategoricalAccuracy(name=\"liver_cat_accuracy\"), Metrics(name=\"liver_metrics\")],\n        [tf.keras.metrics.CategoricalAccuracy(name=\"kidney_cat_accuracy\"), Metrics(name=\"kidney_metrics\")],\n        [tf.keras.metrics.CategoricalAccuracy(name=\"spleen_cat_accuracy\"), Metrics(name=\"spleen_metrics\")],\n    ]\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":"2024-08-07T16:23:14.125469Z","iopub.execute_input":"2024-08-07T16:23:14.125954Z","iopub.status.idle":"2024-08-07T16:23:14.140671Z","shell.execute_reply.started":"2024-08-07T16:23:14.125924Z","shell.execute_reply":"2024-08-07T16:23:14.139873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Callbacks\nearly_stopping = tf.keras.callbacks.EarlyStopping(patience=5, restore_best_weights=True)\nreduce_lr = tf.keras.callbacks.ReduceLROnPlateau(factor=0.1, patience=2)\n\nmodel_checkpoint = tf.keras.callbacks.ModelCheckpoint('best_model.keras', save_best_only=True)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:14.141601Z","iopub.execute_input":"2024-08-07T16:23:14.141883Z","iopub.status.idle":"2024-08-07T16:23:14.152872Z","shell.execute_reply.started":"2024-08-07T16:23:14.141861Z","shell.execute_reply":"2024-08-07T16:23:14.152119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_model()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:14.154222Z","iopub.execute_input":"2024-08-07T16:23:14.15456Z","iopub.status.idle":"2024-08-07T16:23:20.937875Z","shell.execute_reply.started":"2024-08-07T16:23:14.154537Z","shell.execute_reply":"2024-08-07T16:23:20.936984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:20.938872Z","iopub.execute_input":"2024-08-07T16:23:20.939152Z","iopub.status.idle":"2024-08-07T16:23:22.129897Z","shell.execute_reply.started":"2024-08-07T16:23:20.939128Z","shell.execute_reply":"2024-08-07T16:23:22.129043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.utils.plot_model(\n    model,\n    to_file='model.png'\n)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:22.131243Z","iopub.execute_input":"2024-08-07T16:23:22.131527Z","iopub.status.idle":"2024-08-07T16:23:24.590506Z","shell.execute_reply.started":"2024-08-07T16:23:22.131503Z","shell.execute_reply":"2024-08-07T16:23:24.589567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels = training_labels.set_index('patient_id')\ntraining_labels.head(5)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:24.596343Z","iopub.execute_input":"2024-08-07T16:23:24.596642Z","iopub.status.idle":"2024-08-07T16:23:24.611406Z","shell.execute_reply.started":"2024-08-07T16:23:24.596617Z","shell.execute_reply":"2024-08-07T16:23:24.610254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Part C","metadata":{}},{"cell_type":"code","source":"#Using histogram to get the distribution of the labels and to check if there are outliers and wrong labels\ntraining_labels.hist(figsize=(20,12),bins=2)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:24.612627Z","iopub.execute_input":"2024-08-07T16:23:24.612959Z","iopub.status.idle":"2024-08-07T16:23:26.836396Z","shell.execute_reply.started":"2024-08-07T16:23:24.612936Z","shell.execute_reply":"2024-08-07T16:23:26.835518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = []\nlabels = []\nfor i in sorted(os.listdir(train_img)):\n    folder = os.listdir(os.path.join(train_img,i))[0]\n    file = os.listdir(os.path.join(train_img,i,folder))[0]\n    images.append(cv2.imread(os.path.join(train_img,i,folder,file), cv2.IMREAD_COLOR ))\n    labels.append(np.asarray(training_labels.loc[int(i)]))\nimages = np.asarray(images)\nlabels = np.asarray(labels)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:26.837755Z","iopub.execute_input":"2024-08-07T16:23:26.838126Z","iopub.status.idle":"2024-08-07T16:23:30.058108Z","shell.execute_reply.started":"2024-08-07T16:23:26.838094Z","shell.execute_reply":"2024-08-07T16:23:30.057066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(images.shape)\nprint(labels.shape)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:30.059526Z","iopub.execute_input":"2024-08-07T16:23:30.059821Z","iopub.status.idle":"2024-08-07T16:23:30.064912Z","shell.execute_reply.started":"2024-08-07T16:23:30.059792Z","shell.execute_reply":"2024-08-07T16:23:30.063928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Example of images","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(30,12))\nfor i in range(20):\n    plt.subplot(4,5,i+1)\n    plt.imshow(images[i])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:30.06628Z","iopub.execute_input":"2024-08-07T16:23:30.066912Z","iopub.status.idle":"2024-08-07T16:23:33.457814Z","shell.execute_reply.started":"2024-08-07T16:23:30.066879Z","shell.execute_reply":"2024-08-07T16:23:33.456926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Augumenting the training dataset\n# Create an instance of ImageDataGenerator\ndatagen = tf.keras.preprocessing.image.ImageDataGenerator(\n    rotation_range=10,   # Randomly rotate images by up to 20 degrees\n    width_shift_range=0.5,   # Randomly shift images horizontally by up to 5% of the width\n    height_shift_range=0.5,  # Randomly shift images vertically by up to 5% of the height\n    shear_range=0,   # Shear transformations\n    zoom_range=0.1,    # Randomly zoom in on images\n    horizontal_flip=True,   # Randomly flip images horizontally\n    fill_mode='nearest'     # How to fill in newly created pixels after rotation/shifts\n)\n\n# Fit the data generator on your training data\ndatagen.fit(images)\n\n# Generate augmented data\naugmented_data = datagen.flow(images, labels, batch_size=32)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:33.459129Z","iopub.execute_input":"2024-08-07T16:23:33.459565Z","iopub.status.idle":"2024-08-07T16:23:34.100914Z","shell.execute_reply.started":"2024-08-07T16:23:33.45953Z","shell.execute_reply":"2024-08-07T16:23:34.100127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Splittig the training dataset into training set and validation set\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:34.102087Z","iopub.execute_input":"2024-08-07T16:23:34.102868Z","iopub.status.idle":"2024-08-07T16:23:34.107167Z","shell.execute_reply.started":"2024-08-07T16:23:34.102833Z","shell.execute_reply":"2024-08-07T16:23:34.106135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(images, labels, test_size=0.25)\n\n#Printing the shapes of the split datasets\nprint(\"Shape of Training Images:\", X_train.shape)\nprint(\"Shape of Training Labels:\", y_train.shape)\nprint(\"Shape of Validation Images:\", X_val.shape)\nprint(\"Shape of Validation Labels:\", y_val.shape)\n","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:34.108408Z","iopub.execute_input":"2024-08-07T16:23:34.108794Z","iopub.status.idle":"2024-08-07T16:23:34.177742Z","shell.execute_reply.started":"2024-08-07T16:23:34.108729Z","shell.execute_reply":"2024-08-07T16:23:34.176805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert augmented data to an array\naugmented_images, augmented_labels = next(augmented_data)\n\n# Print the shapes of the augmented datasets\nprint(\"Shape of Augmented Images:\", augmented_images.shape)\nprint(\"Shape of Augmented Labels:\", augmented_labels.shape)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:34.17893Z","iopub.execute_input":"2024-08-07T16:23:34.179269Z","iopub.status.idle":"2024-08-07T16:23:36.250351Z","shell.execute_reply.started":"2024-08-07T16:23:34.179242Z","shell.execute_reply":"2024-08-07T16:23:36.249412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(40,20))\nfor i in range(20):\n    plt.subplot(5,4,i+1)\n    plt.imshow(augmented_images[i])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:36.251372Z","iopub.execute_input":"2024-08-07T16:23:36.251661Z","iopub.status.idle":"2024-08-07T16:23:39.858948Z","shell.execute_reply.started":"2024-08-07T16:23:36.251637Z","shell.execute_reply":"2024-08-07T16:23:39.858032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(augmented_labels)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:39.85997Z","iopub.execute_input":"2024-08-07T16:23:39.860336Z","iopub.status.idle":"2024-08-07T16:23:39.881157Z","shell.execute_reply.started":"2024-08-07T16:23:39.860304Z","shell.execute_reply":"2024-08-07T16:23:39.880083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Part D\n## Training the model with the training dataset","metadata":{}},{"cell_type":"code","source":"pd.DataFrame(y_train)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:39.882434Z","iopub.execute_input":"2024-08-07T16:23:39.882772Z","iopub.status.idle":"2024-08-07T16:23:39.902963Z","shell.execute_reply.started":"2024-08-07T16:23:39.882739Z","shell.execute_reply":"2024-08-07T16:23:39.902051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(y_val)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:39.904078Z","iopub.execute_input":"2024-08-07T16:23:39.904589Z","iopub.status.idle":"2024-08-07T16:23:39.922475Z","shell.execute_reply.started":"2024-08-07T16:23:39.904556Z","shell.execute_reply":"2024-08-07T16:23:39.921551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bowel_labels = labels[:,2]\nextravasation_labels = labels[:,4]\nkidney_labels = labels[:,4:7]\nliver_labels = labels[:,7:10]\nspleen_labels = labels[:,10:13]\nany_labels = labels[:,-1]","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:39.923461Z","iopub.execute_input":"2024-08-07T16:23:39.923787Z","iopub.status.idle":"2024-08-07T16:23:39.932347Z","shell.execute_reply.started":"2024-08-07T16:23:39.923756Z","shell.execute_reply":"2024-08-07T16:23:39.931447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bowel_val = y_val[:,2]\nextravasation_val = y_val[:,4]\nkidney_val = y_val[:,4:7]\nliver_val = y_val[:,7:10]\nspleen_val = y_val[:,10:13]\nany_val = y_val[:,-1]","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:39.933327Z","iopub.execute_input":"2024-08-07T16:23:39.933569Z","iopub.status.idle":"2024-08-07T16:23:39.943374Z","shell.execute_reply.started":"2024-08-07T16:23:39.933547Z","shell.execute_reply":"2024-08-07T16:23:39.942581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bowel_train = y_train[:,2]\nextravasation_train = y_train[:,4]\nkidney_train = y_train[:,4:7]\nliver_train = y_train[:,7:10]\nspleen_train = y_train[:,10:13]\nany_train = y_train[:,-1]","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:39.944524Z","iopub.execute_input":"2024-08-07T16:23:39.944846Z","iopub.status.idle":"2024-08-07T16:23:39.954573Z","shell.execute_reply.started":"2024-08-07T16:23:39.944815Z","shell.execute_reply":"2024-08-07T16:23:39.953831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bowel_test = augmented_labels[:,2]\nextravasation_test = augmented_labels[:,4]\nkidney_test = augmented_labels[:,4:7]\nliver_test = augmented_labels[:,7:10]\nspleen_test = augmented_labels[:,10:13]\nany_test = augmented_labels[:,-1]","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:23:39.955606Z","iopub.execute_input":"2024-08-07T16:23:39.955932Z","iopub.status.idle":"2024-08-07T16:23:39.96642Z","shell.execute_reply.started":"2024-08-07T16:23:39.955902Z","shell.execute_reply":"2024-08-07T16:23:39.965661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 20\nnum_epoch = 20\nhistory = model.fit(x = X_train,\n                    y =[bowel_train,extravasation_train,kidney_train,liver_train,spleen_train],\n                    validation_data = (X_val, [bowel_val, extravasation_val, kidney_val, liver_val, spleen_val]),\n                    batch_size=batch_size, \n                    epochs = num_epoch, \n                    verbose = 1,\n                    callbacks=[early_stopping, model_checkpoint]\n                   )","metadata":{"papermill":{"duration":1176.379334,"end_time":"2022-02-01T14:07:30.902597","exception":false,"start_time":"2022-02-01T13:47:54.523263","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-08-07T16:23:39.967452Z","iopub.execute_input":"2024-08-07T16:23:39.967781Z","iopub.status.idle":"2024-08-07T16:36:23.690258Z","shell.execute_reply.started":"2024-08-07T16:23:39.96775Z","shell.execute_reply":"2024-08-07T16:36:23.689335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history.history.keys()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:36:23.691833Z","iopub.execute_input":"2024-08-07T16:36:23.692167Z","iopub.status.idle":"2024-08-07T16:36:23.698314Z","shell.execute_reply.started":"2024-08-07T16:36:23.69214Z","shell.execute_reply":"2024-08-07T16:36:23.697401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_acc = ['val_bowel_bowel_binary_accuracy', 'val_extra_extra_binary_accuracy', 'val_liver_liver_cat_accuracy', 'val_kidney_kidney_cat_accuracy', 'val_spleen_spleen_cat_accuracy']","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:36:23.69951Z","iopub.execute_input":"2024-08-07T16:36:23.701489Z","iopub.status.idle":"2024-08-07T16:36:23.710398Z","shell.execute_reply.started":"2024-08-07T16:36:23.701452Z","shell.execute_reply":"2024-08-07T16:36:23.709376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i,j in enumerate(val_acc):\n    print(j, np.asarray(history.history[val_acc[i]])[-1].round(2))\n#np.asarray(history.history['val_bowel_bowel_binary_accuracy'])[-1].round(2)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:36:23.711404Z","iopub.execute_input":"2024-08-07T16:36:23.711948Z","iopub.status.idle":"2024-08-07T16:36:23.721693Z","shell.execute_reply.started":"2024-08-07T16:36:23.711924Z","shell.execute_reply":"2024-08-07T16:36:23.720738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in history.history.keys():\n    if i.endswith(\"accuracy\") and not i ==\"val_accuracy\":\n        plt.plot(history.history[i], label=i)\nplt.xlabel(\"Epochs\")\nplt.ylabel(\"Loss\")\nplt.legend(loc=(1.05,0.0))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:36:23.722782Z","iopub.execute_input":"2024-08-07T16:36:23.723055Z","iopub.status.idle":"2024-08-07T16:36:24.056773Z","shell.execute_reply.started":"2024-08-07T16:36:23.723027Z","shell.execute_reply":"2024-08-07T16:36:24.055843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history[\"val_loss\"], label=\"val_loss\")\nplt.plot(history.history[\"loss\"], label=\"loss\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:36:24.058149Z","iopub.execute_input":"2024-08-07T16:36:24.058503Z","iopub.status.idle":"2024-08-07T16:36:24.3064Z","shell.execute_reply.started":"2024-08-07T16:36:24.058466Z","shell.execute_reply":"2024-08-07T16:36:24.305588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Testing the model using the Augmented data as test data","metadata":{}},{"cell_type":"code","source":"test_results = model.evaluate(\n    augmented_images, [bowel_test, extravasation_test, kidney_test, liver_test, spleen_test]\n) \n\n# Print the structure of test_results\nprint(\"Test Results Structure:\")\nprint(test_results)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:36:24.307418Z","iopub.execute_input":"2024-08-07T16:36:24.30769Z","iopub.status.idle":"2024-08-07T16:36:56.737614Z","shell.execute_reply.started":"2024-08-07T16:36:24.307668Z","shell.execute_reply":"2024-08-07T16:36:56.736744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_prob = model.predict(augmented_images)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:36:56.738674Z","iopub.execute_input":"2024-08-07T16:36:56.738967Z","iopub.status.idle":"2024-08-07T16:37:10.838213Z","shell.execute_reply.started":"2024-08-07T16:36:56.738942Z","shell.execute_reply":"2024-08-07T16:37:10.837232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output_names = [\"bowel\", \"extra\", \"liver\", \"kidney\", \"spleen\"]","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:37:10.840133Z","iopub.execute_input":"2024-08-07T16:37:10.840531Z","iopub.status.idle":"2024-08-07T16:37:10.845166Z","shell.execute_reply.started":"2024-08-07T16:37:10.840495Z","shell.execute_reply":"2024-08-07T16:37:10.844185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Model Output Names:\", model.output_names)  # Check names in model\nprint(\"y_pred_prob:\", y_pred_prob)\nprint(len(y_pred_prob))  # Print predictions and their shape","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:37:10.846456Z","iopub.execute_input":"2024-08-07T16:37:10.846889Z","iopub.status.idle":"2024-08-07T16:37:10.865143Z","shell.execute_reply.started":"2024-08-07T16:37:10.846855Z","shell.execute_reply":"2024-08-07T16:37:10.864163Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_true_dict = {\n    \"bowel\": bowel_test, \n    \"extra\": extravasation_test,\n    \"liver\": liver_test,\n    \"kidney\": kidney_test,\n    \"spleen\": spleen_test,\n}\n\n\n# Generate and plot confusion matrices for each output\nfor i, output_name in enumerate(output_names):\n    y_true = y_true_dict[output_name]\n    num_classes = 1 if i < 2 else 3  # Binary: 1 class, Multiclass: 3 classes\n    \n    # Ensure y_pred_prob is an array or similar structure\n    if not isinstance(y_pred_prob, (np.ndarray, list, tuple)):\n        y_pred_prob = np.array([y_pred_prob])\n        \n    # If predictions are a tuple of 5 elements\n    if len(y_pred_prob) == 5:\n        # Select the relevant element of the tuple based on the output name\n        y_pred_prob = y_pred_prob[i]\n        \n    # Make sure the predictions are 2D arrays with samples in the first dimension\n    if y_pred_prob.ndim == 1:\n        y_pred_prob = y_pred_prob.reshape(-1, 1)  # Reshape to 2D if necessary\n    \n    if num_classes == 1:\n        # Binary case (but with an extra dimension)\n        y_pred = (y_pred_prob > 0.5).astype(int).flatten()\n        labels = [\"Negative\", \"Positive\"]\n    else:\n        # Multiclass case \n        y_pred = np.argmax(y_pred_prob, axis=1)\n        labels = [f\"Class {i}\" for i in range(y_pred_prob.shape[1])]  \n\n    # Ensure lengths match before calculating the confusion matrix\n    min_len = min(len(y_true), len(y_pred))\n    y_true = y_true[:min_len]\n    y_pred = y_pred[:min_len]\n\n\n    # Ensure both y_true and y_pred are interpreted as multiclass if y_true.ndim > 1:\n    if y_true.ndim > 1:\n        y_true = np.argmax(y_true, axis=1)\n    if y_pred.ndim > 1:\n        y_pred = np.argmax(y_pred, axis=1)\n\n    cm = confusion_matrix(y_true, y_pred)\n    disp = ConfusionMatrixDisplay(confusion_matrix=cm)\n    disp.plot(cmap=plt.cm.Blues)\n\n    # Update ticks and labels directly on the Axes object\n    ax = disp.ax_ \n    ticks = np.arange(len(labels))\n    ax.set_xticks(ticks)\n    ax.set_yticks(ticks)\n    ax.set_xticklabels(labels)\n    ax.set_yticklabels(labels)\n    \n    plt.title(f\"Confusion Matrix - {output_name}\")\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:37:10.866894Z","iopub.execute_input":"2024-08-07T16:37:10.867268Z","iopub.status.idle":"2024-08-07T16:37:12.125267Z","shell.execute_reply.started":"2024-08-07T16:37:10.867237Z","shell.execute_reply":"2024-08-07T16:37:12.124383Z"},"trusted":true},"execution_count":null,"outputs":[]}]}