{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":52254,"databundleVersionId":9674523,"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 **ResNet-50**\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\nfrom tensorflow.keras import regularizers\n\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-11-11T15:24:19.947222Z","iopub.execute_input":"2024-11-11T15:24:19.947653Z","iopub.status.idle":"2024-11-11T15:24:19.953837Z","shell.execute_reply.started":"2024-11-11T15:24:19.947606Z","shell.execute_reply":"2024-11-11T15:24:19.952911Z"},"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-11-11T15:24:19.955493Z","iopub.execute_input":"2024-11-11T15:24:19.955757Z","iopub.status.idle":"2024-11-11T15:24:19.972827Z","shell.execute_reply.started":"2024-11-11T15:24:19.955735Z","shell.execute_reply":"2024-11-11T15:24:19.972075Z"},"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-11-11T15:24:19.974145Z","iopub.execute_input":"2024-11-11T15:24:19.974612Z","iopub.status.idle":"2024-11-11T15:24:20.25107Z","shell.execute_reply.started":"2024-11-11T15:24:19.974581Z","shell.execute_reply":"2024-11-11T15:24:20.250167Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"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-11-11T15:24:20.252498Z","iopub.execute_input":"2024-11-11T15:24:20.253155Z","iopub.status.idle":"2024-11-11T15:24:20.263977Z","shell.execute_reply.started":"2024-11-11T15:24:20.25312Z","shell.execute_reply":"2024-11-11T15:24:20.26301Z"},"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-11-11T15:24:20.266924Z","iopub.execute_input":"2024-11-11T15:24:20.26724Z","iopub.status.idle":"2024-11-11T15:24:20.282685Z","shell.execute_reply.started":"2024-11-11T15:24:20.267218Z","shell.execute_reply":"2024-11-11T15:24:20.28175Z"},"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-11-11T15:24:20.284016Z","iopub.execute_input":"2024-11-11T15:24:20.284388Z","iopub.status.idle":"2024-11-11T15:24:20.291282Z","shell.execute_reply.started":"2024-11-11T15:24:20.284354Z","shell.execute_reply":"2024-11-11T15:24:20.290169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels['patient_id'].isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-11-11T15:24:20.292662Z","iopub.execute_input":"2024-11-11T15:24:20.292979Z","iopub.status.idle":"2024-11-11T15:24:20.302655Z","shell.execute_reply.started":"2024-11-11T15:24:20.292952Z","shell.execute_reply":"2024-11-11T15:24:20.301819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels['bowel_healthy'].isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-11-11T15:24:20.304029Z","iopub.execute_input":"2024-11-11T15:24:20.304337Z","iopub.status.idle":"2024-11-11T15:24:20.31249Z","shell.execute_reply.started":"2024-11-11T15:24:20.304294Z","shell.execute_reply":"2024-11-11T15:24:20.311572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels['bowel_injury'].isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-11-11T15:24:20.313546Z","iopub.execute_input":"2024-11-11T15:24:20.313881Z","iopub.status.idle":"2024-11-11T15:24:20.322119Z","shell.execute_reply.started":"2024-11-11T15:24:20.313843Z","shell.execute_reply":"2024-11-11T15:24:20.32119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels['extravasation_healthy'].isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-11-11T15:24:20.323104Z","iopub.execute_input":"2024-11-11T15:24:20.323442Z","iopub.status.idle":"2024-11-11T15:24:20.331542Z","shell.execute_reply.started":"2024-11-11T15:24:20.323409Z","shell.execute_reply":"2024-11-11T15:24:20.33066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels['extravasation_injury'].isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-11-11T15:24:20.332699Z","iopub.execute_input":"2024-11-11T15:24:20.333012Z","iopub.status.idle":"2024-11-11T15:24:20.340608Z","shell.execute_reply.started":"2024-11-11T15:24:20.332982Z","shell.execute_reply":"2024-11-11T15:24:20.339644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels['kidney_healthy'].isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-11-11T15:24:20.341766Z","iopub.execute_input":"2024-11-11T15:24:20.342027Z","iopub.status.idle":"2024-11-11T15:24:20.349336Z","shell.execute_reply.started":"2024-11-11T15:24:20.342004Z","shell.execute_reply":"2024-11-11T15:24:20.348369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels['kidney_low'].isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-11-11T15:24:20.354236Z","iopub.execute_input":"2024-11-11T15:24:20.354564Z","iopub.status.idle":"2024-11-11T15:24:20.361377Z","shell.execute_reply.started":"2024-11-11T15:24:20.354535Z","shell.execute_reply":"2024-11-11T15:24:20.360469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels['kidney_high'].isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-11-11T15:24:20.362533Z","iopub.execute_input":"2024-11-11T15:24:20.362839Z","iopub.status.idle":"2024-11-11T15:24:20.370659Z","shell.execute_reply.started":"2024-11-11T15:24:20.362811Z","shell.execute_reply":"2024-11-11T15:24:20.369791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels['liver_healthy'].isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-11-11T15:24:20.371784Z","iopub.execute_input":"2024-11-11T15:24:20.372055Z","iopub.status.idle":"2024-11-11T15:24:20.380561Z","shell.execute_reply.started":"2024-11-11T15:24:20.372032Z","shell.execute_reply":"2024-11-11T15:24:20.379682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels['liver_low'].isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-11-11T15:24:20.381721Z","iopub.execute_input":"2024-11-11T15:24:20.382017Z","iopub.status.idle":"2024-11-11T15:24:20.389757Z","shell.execute_reply.started":"2024-11-11T15:24:20.381994Z","shell.execute_reply":"2024-11-11T15:24:20.388799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels['liver_high'].isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-11-11T15:24:20.390895Z","iopub.execute_input":"2024-11-11T15:24:20.39117Z","iopub.status.idle":"2024-11-11T15:24:20.398806Z","shell.execute_reply.started":"2024-11-11T15:24:20.391146Z","shell.execute_reply":"2024-11-11T15:24:20.397875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels['spleen_healthy'].isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-11-11T15:24:20.39998Z","iopub.execute_input":"2024-11-11T15:24:20.400252Z","iopub.status.idle":"2024-11-11T15:24:20.409042Z","shell.execute_reply.started":"2024-11-11T15:24:20.400228Z","shell.execute_reply":"2024-11-11T15:24:20.408017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels['spleen_low'].isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-11-11T15:24:20.410289Z","iopub.execute_input":"2024-11-11T15:24:20.410609Z","iopub.status.idle":"2024-11-11T15:24:20.417609Z","shell.execute_reply.started":"2024-11-11T15:24:20.410582Z","shell.execute_reply":"2024-11-11T15:24:20.416794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels['spleen_high'].isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-11-11T15:24:20.418882Z","iopub.execute_input":"2024-11-11T15:24:20.41918Z","iopub.status.idle":"2024-11-11T15:24:20.426712Z","shell.execute_reply.started":"2024-11-11T15:24:20.419155Z","shell.execute_reply":"2024-11-11T15:24:20.425696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_labels['any_injury'].isna().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-11-11T15:24:20.427972Z","iopub.execute_input":"2024-11-11T15:24:20.428243Z","iopub.status.idle":"2024-11-11T15:24:20.436023Z","shell.execute_reply.started":"2024-11-11T15:24:20.428219Z","shell.execute_reply":"2024-11-11T15:24:20.435223Z"},"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-11-11T15:24:20.437163Z","iopub.execute_input":"2024-11-11T15:24:20.437438Z","iopub.status.idle":"2024-11-11T15:24:20.445565Z","shell.execute_reply.started":"2024-11-11T15:24:20.437416Z","shell.execute_reply":"2024-11-11T15:24:20.444752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"****LR********","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import backend as K\n\n# Define sensitivity and specificity functions outside of the model function\ndef calculate_sensitivity(y_true, y_pred):\n    y_true = tf.cast(y_true, tf.float32)\n    y_pred = tf.cast(y_pred, tf.float32)\n    \n    true_positive = tf.reduce_sum(y_true * y_pred)\n    false_negative = tf.reduce_sum(y_true * (1 - y_pred))\n    sensitivity = true_positive / (true_positive + false_negative + tf.keras.backend.epsilon())\n    return sensitivity\n\ndef calculate_specificity(y_true, y_pred):\n    y_true = tf.cast(y_true, tf.float32)\n    y_pred = tf.cast(y_pred, tf.float32)\n    \n    true_negative = tf.reduce_sum((1 - y_true) * (1 - y_pred))\n    false_positive = tf.reduce_sum((1 - y_true) * y_pred)\n    specificity = true_negative / (true_negative + false_positive + tf.keras.backend.epsilon())\n    return specificity\n\n# Create the model function\ndef create_model(input_shape=(512, 512, 3), decay_steps=1000, warmup_steps=10):\n    # Base model: ResNet50\n    base_model = tf.keras.applications.ResNet50(\n        weights=\"imagenet\", \n        include_top=False, \n        input_shape=input_shape\n    )\n    \n    x = base_model.output\n    x = tf.keras.layers.GlobalAveragePooling2D()(x)\n    x = tf.keras.layers.Dropout(0.5)(x)\n    \n    # Shared dense layers for different outputs\n    shared_dense = tf.keras.layers.Dense(32, activation='silu', kernel_regularizer=regularizers.l1_l2(0.01, 0.01))\n\n    # Separate outputs for each organ\n    x_bowel = shared_dense(x)\n    x_extra = shared_dense(x)\n    x_liver = shared_dense(x)\n    x_kidney = shared_dense(x)\n    x_spleen = shared_dense(x)\n\n    # Define heads (outputs)\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 schedule for learning rate\n    cosine_decay = tf.keras.optimizers.schedules.CosineDecay(\n        initial_learning_rate=1e-4,\n        decay_steps=decay_steps,  # Number of steps for the decay\n        alpha=0.1  # Minimum learning rate value\n    )\n\n    # Optimizer\n    optimizer = tf.keras.optimizers.Adam(learning_rate=cosine_decay)\n\n    # Loss functions for each output\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 for each output\n    metrics = [\n        [tf.keras.metrics.BinaryAccuracy(name=\"bowel_binary_accuracy\"), calculate_sensitivity, calculate_specificity],\n        [tf.keras.metrics.BinaryAccuracy(name=\"extra_binary_accuracy\"), calculate_sensitivity, calculate_specificity],\n        [tf.keras.metrics.CategoricalAccuracy(name=\"liver_cat_accuracy\"), calculate_sensitivity, calculate_specificity],\n        [tf.keras.metrics.CategoricalAccuracy(name=\"kidney_cat_accuracy\"), calculate_sensitivity, calculate_specificity],\n        [tf.keras.metrics.CategoricalAccuracy(name=\"spleen_cat_accuracy\"), calculate_sensitivity, calculate_specificity],\n    ]\n    \n    # Enable mixed precision (for improved performance on supported hardware)\n    from tensorflow.keras.mixed_precision import Policy, set_global_policy\n    policy = Policy('mixed_float16')\n    set_global_policy(policy)\n    \n    # Compile the model\n    model.compile(optimizer=optimizer, loss=loss, metrics=metrics)\n\n    return model\n","metadata":{"execution":{"iopub.status.busy":"2024-11-11T15:24:20.446866Z","iopub.execute_input":"2024-11-11T15:24:20.447134Z","iopub.status.idle":"2024-11-11T15:24:20.465971Z","shell.execute_reply.started":"2024-11-11T15:24:20.447111Z","shell.execute_reply":"2024-11-11T15:24:20.465043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**K-FOLD**","metadata":{}},{"cell_type":"code","source":"# In ra một số giá trị của training_labels để kiểm tra\nprint(training_labels.head())\nprint(training_labels.index)\n\n# Augmenting and loading data\ndatagen = tf.keras.preprocessing.image.ImageDataGenerator(\n    rotation_range=20,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest'\n)\n\ntrain_img_paths = [os.path.join(train_img, folder) for folder in os.listdir(train_img)]\nimages = []\nlabels = []\n\nfor path in train_img_paths:\n    folder = os.listdir(path)[0]  # Lấy tên folder của bệnh nhân\n    file = os.listdir(os.path.join(path, folder))[0]  # Lấy file hình ảnh đầu tiên trong folder\n    image = cv2.imread(os.path.join(path, folder, file), cv2.IMREAD_COLOR)  # Đọc hình ảnh\n    \n    # Kiểm tra xem folder ID có tồn tại trong training_labels không\n    try:\n        label = np.asarray(training_labels.loc[int(folder)])  # Lấy nhãn từ DataFrame\n    except KeyError:\n        continue  \n    images.append(image)  # Thêm hình ảnh vào danh sách\n    labels.append(label)  # Thêm nhãn vào danh sách\n\nimages = np.asarray(images)  # Chuyển danh sách hình ảnh thành mảng NumPy\nlabels = np.asarray(labels)  # Chuyển danh sách nhãn thành mảng NumPy\n\n\n\n# K-fold cross-validation setup\nkfold = StratifiedKFold(n_splits=4, shuffle=True, random_state=42)\noutput_names = [\"bowel\", \"extra\", \"liver\", \"kidney\", \"spleen\"]\n\nfor fold, (train_idx, val_idx) in enumerate(kfold.split(images, np.argmax(labels, axis=1))):\n    print(f\"\\nTraining Fold {fold + 1}...\")\n    \n    X_train, X_val = images[train_idx], images[val_idx]\n    y_train, y_val = labels[train_idx], labels[val_idx]\n    \n    # Continue with model training...\n\n","metadata":{"execution":{"iopub.status.busy":"2024-11-11T15:24:20.467259Z","iopub.execute_input":"2024-11-11T15:24:20.468094Z","iopub.status.idle":"2024-11-11T15:24:21.826804Z","shell.execute_reply.started":"2024-11-11T15:24:20.468069Z","shell.execute_reply":"2024-11-11T15:24:21.825782Z"},"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-11-11T15:24:21.827826Z","iopub.execute_input":"2024-11-11T15:24:21.828085Z","iopub.status.idle":"2024-11-11T15:24:21.83327Z","shell.execute_reply.started":"2024-11-11T15:24:21.828063Z","shell.execute_reply":"2024-11-11T15:24:21.832257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_model()","metadata":{"execution":{"iopub.status.busy":"2024-11-11T15:24:21.834831Z","iopub.execute_input":"2024-11-11T15:24:21.835201Z","iopub.status.idle":"2024-11-11T15:24:23.018654Z","shell.execute_reply.started":"2024-11-11T15:24:21.83517Z","shell.execute_reply":"2024-11-11T15:24:23.017653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-11-11T15:24:23.019966Z","iopub.execute_input":"2024-11-11T15:24:23.020286Z","iopub.status.idle":"2024-11-11T15:24:23.256074Z","shell.execute_reply.started":"2024-11-11T15:24:23.020261Z","shell.execute_reply":"2024-11-11T15:24:23.255215Z"},"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-11-11T15:24:23.257074Z","iopub.execute_input":"2024-11-11T15:24:23.257342Z","iopub.status.idle":"2024-11-11T15:24:26.136994Z","shell.execute_reply.started":"2024-11-11T15:24:23.257301Z","shell.execute_reply":"2024-11-11T15:24:26.135682Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"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-11-11T15:24:26.138214Z","iopub.execute_input":"2024-11-11T15:24:26.138517Z","iopub.status.idle":"2024-11-11T15:24:26.156363Z","shell.execute_reply.started":"2024-11-11T15:24:26.13849Z","shell.execute_reply":"2024-11-11T15:24:26.155492Z"},"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-11-11T15:24:26.157568Z","iopub.execute_input":"2024-11-11T15:24:26.157943Z","iopub.status.idle":"2024-11-11T15:24:28.266175Z","shell.execute_reply.started":"2024-11-11T15:24:26.157904Z","shell.execute_reply":"2024-11-11T15:24:28.26523Z"},"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-11-11T15:24:28.267284Z","iopub.execute_input":"2024-11-11T15:24:28.267563Z","iopub.status.idle":"2024-11-11T15:24:29.741796Z","shell.execute_reply.started":"2024-11-11T15:24:28.26754Z","shell.execute_reply":"2024-11-11T15:24:29.740617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(images.shape)\nprint(labels.shape)","metadata":{"execution":{"iopub.status.busy":"2024-11-11T15:24:29.743243Z","iopub.execute_input":"2024-11-11T15:24:29.743643Z","iopub.status.idle":"2024-11-11T15:24:29.748936Z","shell.execute_reply.started":"2024-11-11T15:24:29.743608Z","shell.execute_reply":"2024-11-11T15:24:29.74806Z"},"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-11-11T15:24:29.750173Z","iopub.execute_input":"2024-11-11T15:24:29.75051Z","iopub.status.idle":"2024-11-11T15:24:32.793451Z","shell.execute_reply.started":"2024-11-11T15:24:29.750484Z","shell.execute_reply":"2024-11-11T15:24:32.792528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**BATH SIZE**","metadata":{}},{"cell_type":"code","source":"# Augumenting the training dataset\n# Create an instance of ImageDataGenerator\ndatagen = tf.keras.preprocessing.image.ImageDataGenerator(\n    rotation_range=40,   # Randomly rotate images by up to 20 degrees\n    width_shift_range=0.2,   # Randomly shift images horizontally by up to 5% of the width\n    height_shift_range=0.2,  # Randomly shift images vertically by up to 5% of the height\n    shear_range=0.2,   # Shear transformations\n    zoom_range=0.2,    # 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-11-11T15:24:32.794767Z","iopub.execute_input":"2024-11-11T15:24:32.795116Z","iopub.status.idle":"2024-11-11T15:24:33.438782Z","shell.execute_reply.started":"2024-11-11T15:24:32.795083Z","shell.execute_reply":"2024-11-11T15:24:33.437952Z"},"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-11-11T15:24:33.439887Z","iopub.execute_input":"2024-11-11T15:24:33.440216Z","iopub.status.idle":"2024-11-11T15:24:33.444628Z","shell.execute_reply.started":"2024-11-11T15:24:33.44019Z","shell.execute_reply":"2024-11-11T15:24:33.443727Z"},"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-11-11T15:24:33.445912Z","iopub.execute_input":"2024-11-11T15:24:33.446256Z","iopub.status.idle":"2024-11-11T15:24:33.509051Z","shell.execute_reply.started":"2024-11-11T15:24:33.446226Z","shell.execute_reply":"2024-11-11T15:24:33.508123Z"},"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-11-11T15:24:33.516948Z","iopub.execute_input":"2024-11-11T15:24:33.517239Z","iopub.status.idle":"2024-11-11T15:24:35.077234Z","shell.execute_reply.started":"2024-11-11T15:24:33.517213Z","shell.execute_reply":"2024-11-11T15:24:35.07639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(40,20))\nfor i in range(5):\n    plt.subplot(1,5,i+1)\n    plt.imshow(augmented_images[i])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-11T15:24:35.078387Z","iopub.execute_input":"2024-11-11T15:24:35.078652Z","iopub.status.idle":"2024-11-11T15:24:36.238297Z","shell.execute_reply.started":"2024-11-11T15:24:35.078629Z","shell.execute_reply":"2024-11-11T15:24:36.237268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(augmented_labels)","metadata":{"execution":{"iopub.status.busy":"2024-11-11T15:24:36.239491Z","iopub.execute_input":"2024-11-11T15:24:36.239773Z","iopub.status.idle":"2024-11-11T15:24:36.258988Z","shell.execute_reply.started":"2024-11-11T15:24:36.239748Z","shell.execute_reply":"2024-11-11T15:24:36.257962Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"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-11-11T15:24:36.26038Z","iopub.execute_input":"2024-11-11T15:24:36.260701Z","iopub.status.idle":"2024-11-11T15:24:36.275924Z","shell.execute_reply.started":"2024-11-11T15:24:36.260676Z","shell.execute_reply":"2024-11-11T15:24:36.275035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(y_val)","metadata":{"execution":{"iopub.status.busy":"2024-11-11T15:24:36.277336Z","iopub.execute_input":"2024-11-11T15:24:36.27777Z","iopub.status.idle":"2024-11-11T15:24:36.292565Z","shell.execute_reply.started":"2024-11-11T15:24:36.277734Z","shell.execute_reply":"2024-11-11T15:24:36.29165Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"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-11-11T15:24:36.293677Z","iopub.execute_input":"2024-11-11T15:24:36.293951Z","iopub.status.idle":"2024-11-11T15:24:36.299942Z","shell.execute_reply.started":"2024-11-11T15:24:36.293928Z","shell.execute_reply":"2024-11-11T15:24:36.298973Z"},"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-11-11T15:24:36.301226Z","iopub.execute_input":"2024-11-11T15:24:36.301998Z","iopub.status.idle":"2024-11-11T15:24:36.307108Z","shell.execute_reply.started":"2024-11-11T15:24:36.301973Z","shell.execute_reply":"2024-11-11T15:24:36.306358Z"},"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-11-11T15:24:36.308241Z","iopub.execute_input":"2024-11-11T15:24:36.308577Z","iopub.status.idle":"2024-11-11T15:24:36.315353Z","shell.execute_reply.started":"2024-11-11T15:24:36.308548Z","shell.execute_reply":"2024-11-11T15:24:36.314507Z"},"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-11-11T15:24:36.316479Z","iopub.execute_input":"2024-11-11T15:24:36.316802Z","iopub.status.idle":"2024-11-11T15:24:36.323257Z","shell.execute_reply.started":"2024-11-11T15:24:36.316768Z","shell.execute_reply":"2024-11-11T15:24:36.322562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**BATH SIZE**","metadata":{}},{"cell_type":"code","source":"batch_size = 32\n# If using tf.data.Dataset for example:\ntrain_data = tf.data.Dataset.from_tensor_slices((X_train, y_train)).batch(batch_size)\nval_data = tf.data.Dataset.from_tensor_slices((X_val, y_val)).batch(batch_size)\n\n# Or, if using ImageDataGenerator:\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n# Set up your data generators\ntrain_datagen = ImageDataGenerator(rescale=1.0/255.0)\nval_datagen = ImageDataGenerator(rescale=1.0/255.0)\n\ntrain_data = train_datagen.flow(X_train, y_train, batch_size=batch_size)\nval_data = val_datagen.flow(X_val, y_val, batch_size=batch_size)\n","metadata":{"execution":{"iopub.status.busy":"2024-11-11T15:24:36.324363Z","iopub.execute_input":"2024-11-11T15:24:36.32476Z","iopub.status.idle":"2024-11-11T15:24:36.960418Z","shell.execute_reply.started":"2024-11-11T15:24:36.32473Z","shell.execute_reply":"2024-11-11T15:24:36.959596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import ReduceLROnPlateau\nbatch_size = 32\nnum_epoch = 10\n# Define the ReduceLROnPlateau callback\nreduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=5, min_lr=1e-4)\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=[reduce_lr, 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-11-11T15:24:36.96162Z","iopub.execute_input":"2024-11-11T15:24:36.961972Z","iopub.status.idle":"2024-11-11T15:30:51.625671Z","shell.execute_reply.started":"2024-11-11T15:24:36.961939Z","shell.execute_reply":"2024-11-11T15:30:51.624811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history.history.keys()","metadata":{"execution":{"iopub.status.busy":"2024-11-11T15:30:51.627423Z","iopub.execute_input":"2024-11-11T15:30:51.627719Z","iopub.status.idle":"2024-11-11T15:30:51.634013Z","shell.execute_reply.started":"2024-11-11T15:30:51.627694Z","shell.execute_reply":"2024-11-11T15:30:51.633069Z"},"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-11-11T15:30:51.635202Z","iopub.execute_input":"2024-11-11T15:30:51.63551Z","iopub.status.idle":"2024-11-11T15:30:51.641647Z","shell.execute_reply.started":"2024-11-11T15:30:51.635476Z","shell.execute_reply":"2024-11-11T15:30:51.64083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n\n# Initialize variables to accumulate the sums for all metrics\ntotal_accuracy_sum = 0\nf1_score_sum = 0\nsensitivity_sum = 0\nspecificity_sum = 0\n\n# Define the organs and their corresponding accuracy metric names\norgan_names = ['bowel', 'extra', 'kidney', 'liver', 'spleen']\n\n# Determine number of epochs from 'val_bowel_bowel_binary_accuracy' or any other validation metric\nnum_epoch = len(history.history['val_bowel_bowel_binary_accuracy'])\n\n# Loop through each epoch to accumulate values\nfor epoch in range(num_epoch):\n    accuracies = []\n    f1_values = []\n    sensitivity_values = []\n    specificity_values = []\n\n    # Loop through each organ to collect the metrics dynamically\n    for organ in organ_names:\n        # Accuracy metrics\n        if f'val_{organ}_{organ}_binary_accuracy' in history.history:\n            accuracies.append(history.history[f'val_{organ}_{organ}_binary_accuracy'][epoch])\n\n        # F1 Score metrics (if available)\n        if f'val_{organ}_{organ}_cat_accuracy' in history.history:\n            f1_values.append(history.history[f'val_{organ}_{organ}_cat_accuracy'][epoch])\n\n        # Sensitivity metrics\n        if f'val_{organ}_calculate_sensitivity' in history.history:\n            sensitivity_values.append(history.history[f'val_{organ}_calculate_sensitivity'][epoch])\n\n        # Specificity metrics\n        if f'val_{organ}_calculate_specificity' in history.history:\n            specificity_values.append(history.history[f'val_{organ}_calculate_specificity'][epoch])\n\n    if accuracies:\n        total_accuracy = np.mean(accuracies)\n        total_accuracy_sum += total_accuracy\n\n    if f1_values:\n        f1_avg = np.mean(f1_values)\n        f1_score_sum += f1_avg\n\n    if sensitivity_values:\n        sensitivity_avg = np.mean(sensitivity_values)\n        sensitivity_sum += sensitivity_avg\n\n    if specificity_values:\n        specificity_avg = np.mean(specificity_values)\n        specificity_sum += specificity_avg\n\n# Calculate the final average for each metric\naverage_total_accuracy = total_accuracy_sum / num_epoch\naverage_f1_score = f1_score_sum / num_epoch\naverage_sensitivity = sensitivity_sum / num_epoch\naverage_specificity = specificity_sum / num_epoch\n\n# Print the final summary\nprint(\"\\n=== Summary ===\")\nprint(f\"Overall average Total Accuracy: {average_total_accuracy:.4f}\")\nprint(f\"Overall average F1 Score: {average_f1_score:.4f}\")\nprint(f\"Overall average Sensitivity: {average_sensitivity:.4f}\")\nprint(f\"Overall average Specificity: {average_specificity:.4f}\")\n","metadata":{"execution":{"iopub.status.busy":"2024-11-11T15:30:51.642888Z","iopub.execute_input":"2024-11-11T15:30:51.643173Z","iopub.status.idle":"2024-11-11T15:30:51.656964Z","shell.execute_reply.started":"2024-11-11T15:30:51.64315Z","shell.execute_reply":"2024-11-11T15:30:51.656043Z"},"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-11-11T15:30:51.657962Z","iopub.execute_input":"2024-11-11T15:30:51.658215Z","iopub.status.idle":"2024-11-11T15:30:51.668031Z","shell.execute_reply.started":"2024-11-11T15:30:51.658193Z","shell.execute_reply":"2024-11-11T15:30:51.667148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Loop through all keys in history.history\nfor i in history.history.keys():\n    # Check if the key ends with 'accuracy' and does not start with 'val_'\n    if i.endswith(\"accuracy\") and not i.startswith(\"val_\"):\n        plt.plot(history.history[i], label=i)\n\n# Add labels and legend\nplt.xlabel(\"Epochs\")\nplt.ylabel(\"Accuracy\")\nplt.legend(loc=(1.05, 0.0))  # Adjust the position of the legend\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-11-11T15:30:51.669096Z","iopub.execute_input":"2024-11-11T15:30:51.669389Z","iopub.status.idle":"2024-11-11T15:30:51.944509Z","shell.execute_reply.started":"2024-11-11T15:30:51.669364Z","shell.execute_reply":"2024-11-11T15:30:51.943648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Tạo một lưới 5x1 cho các subplot\nfig, axes = plt.subplots(5, 1, figsize=(6, 18))  # Điều chỉnh kích thước để phù hợp\n\n# Lặp qua các loại chính xác và sử dụng khóa đúng\nfor i, name in enumerate([\n    \"bowel_bowel_binary_accuracy\", \n    \"extra_extra_binary_accuracy\", \n    \"kidney_kidney_cat_accuracy\", \n    \"liver_liver_cat_accuracy\", \n    \"spleen_spleen_cat_accuracy\"\n]):\n    # Tạo các tên khóa cho tập huấn luyện và tập xác thực\n    train_key = name\n    val_key = 'val_' + name\n    \n    if train_key in history.history and val_key in history.history:\n        # Vẽ độ chính xác trong quá trình huấn luyện\n        axes[i].plot(history.history[train_key], label='Training ' + name.split('_')[0], color='blue')\n        # Vẽ độ chính xác trong quá trình xác thực\n        axes[i].plot(history.history[val_key], label='Validation ' + name.split('_')[0], color='orange')\n    \n    # Thiết lập tiêu đề và nhãn cho mỗi biểu đồ\n    axes[i].set_title(f\"{name.split('_')[0].capitalize()} Accuracy\")\n    axes[i].set_xlabel('Epoch')\n    axes[i].set_ylabel('Accuracy')\n    axes[i].legend()\n\n# Điều chỉnh khoảng cách giữa các biểu đồ\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-11-11T15:30:51.945751Z","iopub.execute_input":"2024-11-11T15:30:51.946104Z","iopub.status.idle":"2024-11-11T15:30:53.186234Z","shell.execute_reply.started":"2024-11-11T15:30:51.946068Z","shell.execute_reply":"2024-11-11T15:30:53.185385Z"},"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-11-11T15:30:53.187595Z","iopub.execute_input":"2024-11-11T15:30:53.188022Z","iopub.status.idle":"2024-11-11T15:30:53.432671Z","shell.execute_reply.started":"2024-11-11T15:30:53.18799Z","shell.execute_reply":"2024-11-11T15:30:53.431784Z"},"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-11-11T15:30:53.433742Z","iopub.execute_input":"2024-11-11T15:30:53.434001Z","iopub.status.idle":"2024-11-11T15:30:59.970628Z","shell.execute_reply.started":"2024-11-11T15:30:53.433978Z","shell.execute_reply":"2024-11-11T15:30:59.969705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output_names = [\"bowel\", \"extra\", \"liver\", \"kidney\", \"spleen\"]","metadata":{"execution":{"iopub.status.busy":"2024-11-11T15:30:59.971674Z","iopub.execute_input":"2024-11-11T15:30:59.971958Z","iopub.status.idle":"2024-11-11T15:30:59.976417Z","shell.execute_reply.started":"2024-11-11T15:30:59.971933Z","shell.execute_reply":"2024-11-11T15:30:59.975328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_prob = model.predict(augmented_images)","metadata":{"execution":{"iopub.status.busy":"2024-11-11T15:30:59.977452Z","iopub.execute_input":"2024-11-11T15:30:59.977723Z","iopub.status.idle":"2024-11-11T15:31:06.104063Z","shell.execute_reply.started":"2024-11-11T15:30:59.9777Z","shell.execute_reply":"2024-11-11T15:31:06.103085Z"},"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-11-11T15:31:06.105495Z","iopub.execute_input":"2024-11-11T15:31:06.105777Z","iopub.status.idle":"2024-11-11T15:31:06.115574Z","shell.execute_reply.started":"2024-11-11T15:31:06.105754Z","shell.execute_reply":"2024-11-11T15:31:06.114743Z"},"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-11-11T15:31:06.11718Z","iopub.execute_input":"2024-11-11T15:31:06.117477Z","iopub.status.idle":"2024-11-11T15:31:07.40213Z","shell.execute_reply.started":"2024-11-11T15:31:06.117454Z","shell.execute_reply":"2024-11-11T15:31:07.401228Z"},"trusted":true},"execution_count":null,"outputs":[]}]}