{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":13451,"databundleVersionId":1188070,"sourceType":"competition"}],"dockerImageVersionId":30804,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom sklearn.metrics import f1_score, precision_score, recall_score, jaccard_score\nfrom skimage.transform import resize\nfrom tensorflow.keras.applications import DenseNet121\nfrom tensorflow.keras.layers import Dense, Dropout, GlobalAveragePooling2D\nfrom tensorflow.keras.models import Sequential\nimport matplotlib.pyplot as plt\nimport pydicom\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T00:19:52.429571Z","iopub.execute_input":"2024-12-10T00:19:52.430347Z","iopub.status.idle":"2024-12-10T00:20:09.676647Z","shell.execute_reply.started":"2024-12-10T00:19:52.430304Z","shell.execute_reply":"2024-12-10T00:20:09.675384Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Dataset paths\ndataset_path = \"../input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/\"\ntrain_images_dir = os.path.join(dataset_path, \"stage_2_train/\")\ntrain_csv = os.path.join(dataset_path, \"stage_2_train.csv\")\n\n# Load and preprocess train.csv\ntrain = pd.read_csv(train_csv)\ntrain['id'] = train['ID'].apply(lambda st: \"ID_\" + st.split('_')[1])\ntrain['subtype'] = train['ID'].apply(lambda st: st.split('_')[2])\ntrain = train[[\"id\", \"subtype\", \"Label\"]]\n\n# Identify and remove duplicate entries\nduplicates = train[train.duplicated(subset=['id', 'subtype'], keep=False)]\nprint(f\"Number of duplicate rows: {len(duplicates)}\")\n\nif not duplicates.empty:\n    print(\"Removing duplicate entries...\")\n    train = train.drop_duplicates(subset=['id', 'subtype'])\n    print(\"Duplicates removed.\")\n\n# Check for missing labels\nmissing_labels = train[train['Label'].isnull()]\nprint(f\"Number of missing labels: {len(missing_labels)}\")\n\nif not missing_labels.empty:\n    print(\"Filling missing labels with 0...\")\n    train['Label'] = train['Label'].fillna(0)\n    print(\"Missing labels filled.\")\n\n# Pivot the data\ntrain_pivot = train.pivot(index='id', columns='subtype', values='Label').reset_index()\nprint(\"Pivoted DataFrame shape:\", train_pivot.shape)\nprint(train_pivot.head())\n\n# Add file paths\ntrain_pivot['file_path'] = train_pivot['id'].apply(lambda x: os.path.join(train_images_dir, f\"{x}.dcm\"))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T00:20:09.678645Z","iopub.execute_input":"2024-12-10T00:20:09.679291Z","iopub.status.idle":"2024-12-10T00:20:29.690803Z","shell.execute_reply.started":"2024-12-10T00:20:09.679251Z","shell.execute_reply":"2024-12-10T00:20:29.689585Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Use a smaller subset for testing\nsubset = train_pivot.sample(n=1000, random_state=42)\n\n# Preprocess DICOM images\ndef preprocess_dicom(file_path, label, target_size=(224, 224)):\n    try:\n        dicom = pydicom.dcmread(file_path)\n        img = dicom.pixel_array\n        img = resize(img, target_size, mode=\"constant\", anti_aliasing=True)\n        img = np.expand_dims(img, axis=-1)  # Add channel dimension\n        img = np.repeat(img, 3, axis=-1)   # Convert grayscale to RGB\n        img = img / np.max(img)           # Normalize pixel values\n        return img, label\n    except Exception as e:\n        print(f\"Error processing {file_path}: {e}\")\n        return None, None\n\n# Process the subset and create image-label pairs\nimages, labels = [], []\nfor _, row in subset.iterrows():\n    img, lbl = preprocess_dicom(\n        row['file_path'],\n        row[[\"epidural\", \"intraparenchymal\", \"intraventricular\", \"subarachnoid\", \"subdural\", \"any\"]].values\n    )\n    if img is not None:\n        images.append(img)\n        labels.append(lbl)\n\n# Convert to numpy arrays\nimages = np.array(images)\nlabels = np.array(labels)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T00:20:29.691964Z","iopub.execute_input":"2024-12-10T00:20:29.692357Z","iopub.status.idle":"2024-12-10T00:21:07.392061Z","shell.execute_reply.started":"2024-12-10T00:20:29.69232Z","shell.execute_reply":"2024-12-10T00:21:07.390674Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n# Split into training and validation sets\nX_train, X_val, y_train, y_val = train_test_split(images, labels, test_size=0.2, random_state=42)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T00:21:07.394786Z","iopub.execute_input":"2024-12-10T00:21:07.395154Z","iopub.status.idle":"2024-12-10T00:21:07.711026Z","shell.execute_reply.started":"2024-12-10T00:21:07.39512Z","shell.execute_reply":"2024-12-10T00:21:07.709945Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define the DenseNet121 model **************************************************************************************************************************************\ndef create_densenet_model(input_shape=(224, 224, 3), num_classes=6):\n    base_model = DenseNet121(weights=\"imagenet\", include_top=False, input_shape=input_shape)\n    base_model.trainable = False  # Freeze base model\n    model = Sequential([\n        base_model,\n        GlobalAveragePooling2D(),\n        Dense(128, activation=\"relu\"),\n        Dropout(0.5),\n        Dense(num_classes, activation=\"sigmoid\")  # Multi-label classification\n    ])\n    model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),\n                  loss=\"binary_crossentropy\", metrics=[\"accuracy\"])\n    return model\n\n# Create and compile the model\nmodel = create_densenet_model()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T00:21:07.712731Z","iopub.execute_input":"2024-12-10T00:21:07.713078Z","iopub.status.idle":"2024-12-10T00:21:10.834454Z","shell.execute_reply.started":"2024-12-10T00:21:07.713047Z","shell.execute_reply":"2024-12-10T00:21:10.833321Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Convert labels to float32\nlabels = np.array(labels, dtype=np.float32)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T00:21:10.835898Z","iopub.execute_input":"2024-12-10T00:21:10.83634Z","iopub.status.idle":"2024-12-10T00:21:10.841782Z","shell.execute_reply.started":"2024-12-10T00:21:10.836294Z","shell.execute_reply":"2024-12-10T00:21:10.840386Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"X_train shape:\", X_train.shape)\nprint(\"y_train shape:\", y_train.shape)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T00:21:10.843634Z","iopub.execute_input":"2024-12-10T00:21:10.844102Z","iopub.status.idle":"2024-12-10T00:21:10.855848Z","shell.execute_reply.started":"2024-12-10T00:21:10.844054Z","shell.execute_reply":"2024-12-10T00:21:10.854252Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Ensure labels are float32\ny_train = np.array(y_train, dtype=np.float32)\ny_val = np.array(y_val, dtype=np.float32)\n\n# Train the model\nhistory = model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=3, batch_size=32)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T00:22:35.586209Z","iopub.execute_input":"2024-12-10T00:22:35.586591Z","iopub.status.idle":"2024-12-10T00:28:55.434233Z","shell.execute_reply.started":"2024-12-10T00:22:35.586559Z","shell.execute_reply":"2024-12-10T00:28:55.433015Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Check data shapes and types\nprint(\"X_train shape:\", X_train.shape, \"dtype:\", X_train.dtype)\nprint(\"y_train shape:\", y_train.shape, \"dtype:\", y_train.dtype)\nprint(\"X_val shape:\", X_val.shape, \"dtype:\", X_val.dtype)\nprint(\"y_val shape:\", y_val.shape, \"dtype:\", y_val.dtype)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T21:56:29.386994Z","iopub.execute_input":"2024-12-09T21:56:29.387423Z","iopub.status.idle":"2024-12-09T21:56:29.393997Z","shell.execute_reply.started":"2024-12-09T21:56:29.387387Z","shell.execute_reply":"2024-12-09T21:56:29.392912Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Predict on validation data\ndensenet_y_pred = model.predict(X_val)\ndensenet_y_pred_binary = (densenet_y_pred > 0.5).astype(int)\n\n# Calculate evaluation metrics\nDenseNet121_f1 = f1_score(y_val, densenet_y_pred_binary, average='weighted', zero_division=1)\nDenseNet121_precision = precision_score(y_val, densenet_y_pred_binary, average='weighted', zero_division=1)\nDenseNet121_recall = recall_score(y_val, densenet_y_pred_binary, average='weighted', zero_division=1)\nDenseNet121_iou = jaccard_score(y_val, densenet_y_pred_binary, average='weighted', zero_division=1)\nDenseNet121_dice = 2 * (DenseNet121_precision * DenseNet121_recall) / (DenseNet121_precision + DenseNet121_recall)\n\n# Sensitivity (Recall per class)\nfrom sklearn.metrics import classification_report\nDenseNet121_report = classification_report(\n    y_val, \n    densenet_y_pred_binary, \n    target_names=['epidural', 'intraparenchymal', 'intraventricular', 'subarachnoid', 'subdural', 'any'], \n    zero_division=1\n)\nprint(DenseNet121_report)\n\n# Store training and validation metrics\nDenseNet121_training_accuracy = history.history['accuracy'][-1]\nDenseNet121_training_loss = history.history['loss'][-1]\nDenseNet121_validation_accuracy = history.history['val_accuracy'][-1]\nDenseNet121_validation_loss = history.history['val_loss'][-1]\n\n# Print metrics using new variables\nprint(f\"DenseNet121 Training Accuracy: {DenseNet121_training_accuracy:.4f}\")\nprint(f\"DenseNet121 Training Loss: {DenseNet121_training_loss:.4f}\")\nprint(f\"DenseNet121 Validation Accuracy: {DenseNet121_validation_accuracy:.4f}\")\nprint(f\"DenseNet121 Validation Loss: {DenseNet121_validation_loss:.4f}\")\nprint(f\"DenseNet121 F1 Score: {DenseNet121_f1:.4f}\")\nprint(f\"DenseNet121 Precision: {DenseNet121_precision:.4f}\")\nprint(f\"DenseNet121 Recall (Sensitivity): {DenseNet121_recall:.4f}\")\nprint(f\"DenseNet121 IoU: {DenseNet121_iou:.4f}\")\nprint(f\"DenseNet121 Dice Coefficient: {DenseNet121_dice:.4f}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T21:56:56.853073Z","iopub.execute_input":"2024-12-09T21:56:56.853883Z","iopub.status.idle":"2024-12-09T21:57:18.254284Z","shell.execute_reply.started":"2024-12-09T21:56:56.853837Z","shell.execute_reply":"2024-12-09T21:57:18.25309Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.applications import ResNet50V2 \n\n# Define the ResNet50V2 model ****************************************************************************************************************************\ndef create_resnet50v2_model(input_shape=(224, 224, 3), num_classes=6):\n    base_model = ResNet50V2(weights=\"imagenet\", include_top=False, input_shape=input_shape)\n    base_model.trainable = False  # Freeze base model\n    model = Sequential([\n        base_model,\n        GlobalAveragePooling2D(),\n        Dense(128, activation=\"relu\"),\n        Dropout(0.5),\n        Dense(num_classes, activation=\"sigmoid\")  # Multi-label classification\n    ])\n    model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),\n                  loss=\"binary_crossentropy\", metrics=[\"accuracy\"])\n    return model\n\n# Create and compile the model\nresnet_model = create_resnet50v2_model()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T19:15:05.544896Z","iopub.execute_input":"2024-12-09T19:15:05.545387Z","iopub.status.idle":"2024-12-09T19:15:07.7286Z","shell.execute_reply.started":"2024-12-09T19:15:05.545348Z","shell.execute_reply":"2024-12-09T19:15:07.72698Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Train the ResNet50V2 model\nresnet_history = resnet_model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=3, batch_size=32)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T18:36:00.606772Z","iopub.execute_input":"2024-12-09T18:36:00.607174Z","iopub.status.idle":"2024-12-09T18:42:17.104552Z","shell.execute_reply.started":"2024-12-09T18:36:00.607137Z","shell.execute_reply":"2024-12-09T18:42:17.103243Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\n\n# Predict on validation data\nresnet_y_pred = resnet_model.predict(X_val)\nresnet_y_pred_binary = (resnet_y_pred > 0.5).astype(int)\n\n# Calculate evaluation metrics\nresnet_f1 = f1_score(y_val, resnet_y_pred_binary, average='weighted', zero_division=1)\nresnet_precision = precision_score(y_val, resnet_y_pred_binary, average='weighted', zero_division=1)\nresnet_recall = recall_score(y_val, resnet_y_pred_binary, average='weighted', zero_division=1)\nresnet_iou = jaccard_score(y_val, resnet_y_pred_binary, average='weighted', zero_division=1)\n\n# Safeguard for Dice Coefficient\nif resnet_precision + resnet_recall > 0:\n    resnet_dice = 2 * (resnet_precision * resnet_recall) / (resnet_precision + resnet_recall)\nelse:\n    resnet_dice = 0  # Fallback value\n\n# Sensitivity (Recall per class) with safe division\nresnet_conf_matrix = confusion_matrix(y_val.argmax(axis=1), resnet_y_pred_binary.argmax(axis=1))\nresnet_sensitivity = np.divide(\n    np.diag(resnet_conf_matrix),\n    np.sum(resnet_conf_matrix, axis=1),\n    out=np.zeros_like(np.diag(resnet_conf_matrix), dtype=np.float32),\n    where=np.sum(resnet_conf_matrix, axis=1) != 0\n)\n\n# Training Accuracy (Fixed)\nresnet_accuracy = resnet_history.history['accuracy'][-1]\n\n# Print metrics\nprint(f\"ResNet50V2 Training Accuracy: {resnet_accuracy:.4f}\")\nprint(f\"ResNet50V2 Training Loss: {resnet_history.history['loss'][-1]:.4f}\")\nprint(f\"ResNet50V2 Validation Accuracy: {resnet_history.history['val_accuracy'][-1]:.4f}\")\nprint(f\"ResNet50V2 Validation Loss: {resnet_history.history['val_loss'][-1]:.4f}\")\nprint(f\"ResNet50V2 F1 Score: {resnet_f1:.4f}\")\nprint(f\"ResNet50V2 Precision: {resnet_precision:.4f}\")\nprint(f\"ResNet50V2 Recall (Sensitivity): {resnet_recall:.4f}\")\nprint(f\"ResNet50V2 IoU: {resnet_iou:.4f}\")\nprint(f\"ResNet50V2 Dice Coefficient: {resnet_dice:.4f}\")\nprint(f\"ResNet50V2 Sensitivity (per class): {resnet_sensitivity}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T19:16:47.923365Z","iopub.execute_input":"2024-12-09T19:16:47.92383Z","iopub.status.idle":"2024-12-09T19:17:05.347954Z","shell.execute_reply.started":"2024-12-09T19:16:47.923792Z","shell.execute_reply":"2024-12-09T19:17:05.346833Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.applications import InceptionV3\n\n# Define the InceptionV3 model *******************************************************************************************************************************************\ndef create_inceptionv3_model(input_shape=(224, 224, 3), num_classes=6):\n    base_model = InceptionV3(weights=\"imagenet\", include_top=False, input_shape=input_shape)\n    base_model.trainable = False  # Freeze base model\n    model = Sequential([\n        base_model,\n        GlobalAveragePooling2D(),\n        Dense(128, activation=\"relu\"),\n        Dropout(0.5),\n        Dense(num_classes, activation=\"sigmoid\")  # Multi-label classification\n    ])\n    model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),\n                  loss=\"binary_crossentropy\", metrics=[\"accuracy\"])\n    return model\n\n# Create and compile the model\ninception_model = create_inceptionv3_model()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T19:54:31.540286Z","iopub.execute_input":"2024-12-09T19:54:31.540691Z","iopub.status.idle":"2024-12-09T19:54:33.498704Z","shell.execute_reply.started":"2024-12-09T19:54:31.54065Z","shell.execute_reply":"2024-12-09T19:54:33.497868Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Ensure features are float32\nX_train = X_train.astype('float32')\nX_val = X_val.astype('float32')\n\n# Ensure labels are float32\ny_train = y_train.astype('float32')\ny_val = y_val.astype('float32')\n\n# Check shapes and data types\nprint(\"X_train shape:\", X_train.shape, \"dtype:\", X_train.dtype)\nprint(\"X_val shape:\", X_val.shape, \"dtype:\", X_val.dtype)\nprint(\"y_train shape:\", y_train.shape, \"dtype:\", y_train.dtype)\nprint(\"y_val shape:\", y_val.shape, \"dtype:\", y_val.dtype)\n\n# Train the InceptionV3 model\ninception_history = inception_model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=3, batch_size=32)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T19:59:29.626267Z","iopub.execute_input":"2024-12-09T19:59:29.626882Z","iopub.status.idle":"2024-12-09T20:02:29.685254Z","shell.execute_reply.started":"2024-12-09T19:59:29.62683Z","shell.execute_reply":"2024-12-09T20:02:29.683896Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\n\n# Predict on validation data\ninception_y_pred = inception_model.predict(X_val)\ninception_y_pred_binary = (inception_y_pred > 0.5).astype(int)\n\n# Calculate evaluation metrics\ninception_f1 = f1_score(y_val, inception_y_pred_binary, average='weighted', zero_division=1)\ninception_precision = precision_score(y_val, inception_y_pred_binary, average='weighted', zero_division=1)\ninception_recall = recall_score(y_val, inception_y_pred_binary, average='weighted', zero_division=1)\ninception_iou = jaccard_score(y_val, inception_y_pred_binary, average='weighted', zero_division=1)\ninception_dice = 2 * (inception_precision * inception_recall) / (inception_precision + inception_recall)\n\n# Sensitivity (Recall per class) with safe division\ninception_conf_matrix = confusion_matrix(y_val.argmax(axis=1), inception_y_pred_binary.argmax(axis=1))\ninception_sensitivity = np.divide(\n    np.diag(inception_conf_matrix),\n    np.sum(inception_conf_matrix, axis=1),\n    out=np.zeros_like(np.diag(inception_conf_matrix), dtype=np.float32),\n    where=np.sum(inception_conf_matrix, axis=1) != 0\n)\n\n# Print metrics\nprint(f\"InceptionV3 Training Accuracy: {inception_history.history['accuracy'][-1]:.4f}\")\nprint(f\"InceptionV3 Training Loss: {inception_history.history['loss'][-1]:.4f}\")\nprint(f\"InceptionV3 Validation Accuracy: {inception_history.history['val_accuracy'][-1]:.4f}\")\nprint(f\"InceptionV3 Validation Loss: {inception_history.history['val_loss'][-1]:.4f}\")\nprint(f\"InceptionV3 F1 Score: {inception_f1:.4f}\")\nprint(f\"InceptionV3 Precision: {inception_precision:.4f}\")\nprint(f\"InceptionV3 Recall (Sensitivity): {inception_recall:.4f}\")\nprint(f\"InceptionV3 IoU: {inception_iou:.4f}\")\nprint(f\"InceptionV3 Dice Coefficient: {inception_dice:.4f}\")\nprint(f\"InceptionV3 Sensitivity (per class): {inception_sensitivity}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T20:10:51.655873Z","iopub.execute_input":"2024-12-09T20:10:51.656368Z","iopub.status.idle":"2024-12-09T20:11:02.164167Z","shell.execute_reply.started":"2024-12-09T20:10:51.656331Z","shell.execute_reply":"2024-12-09T20:11:02.162664Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.applications import VGG16\n\n# Define the VGG16 model ***************************************************************************************************************************************\ndef create_vgg16_model(input_shape=(224, 224, 3), num_classes=6):\n    base_model = VGG16(weights=\"imagenet\", include_top=False, input_shape=input_shape)\n    base_model.trainable = False  # Freeze base model\n    model = Sequential([\n        base_model,\n        GlobalAveragePooling2D(),\n        Dense(128, activation=\"relu\"),\n        Dropout(0.5),\n        Dense(num_classes, activation=\"sigmoid\")  # Multi-label classification\n    ])\n    model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),\n                  loss=\"binary_crossentropy\", metrics=[\"accuracy\"])\n    return model\n\n# Create and compile the model\nvgg16_model = create_vgg16_model()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T20:11:19.786392Z","iopub.execute_input":"2024-12-09T20:11:19.786944Z","iopub.status.idle":"2024-12-09T20:11:23.932091Z","shell.execute_reply.started":"2024-12-09T20:11:19.786894Z","shell.execute_reply":"2024-12-09T20:11:23.930891Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Train the VGG16 model\nvgg16_history = vgg16_model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=3, batch_size=32)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T20:11:35.296484Z","iopub.execute_input":"2024-12-09T20:11:35.296937Z","iopub.status.idle":"2024-12-09T20:24:40.011596Z","shell.execute_reply.started":"2024-12-09T20:11:35.296898Z","shell.execute_reply":"2024-12-09T20:24:40.010012Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Predict on validation data\nvgg16_y_pred = vgg16_model.predict(X_val)\nvgg16_y_pred_binary = (vgg16_y_pred > 0.5).astype(int)\n\n# Calculate evaluation metrics\nvgg16_f1 = f1_score(y_val, vgg16_y_pred_binary, average='weighted', zero_division=1)\nvgg16_precision = precision_score(y_val, vgg16_y_pred_binary, average='weighted', zero_division=1)\nvgg16_recall = recall_score(y_val, vgg16_y_pred_binary, average='weighted', zero_division=1)\nvgg16_iou = jaccard_score(y_val, vgg16_y_pred_binary, average='weighted', zero_division=1)\nvgg16_dice = 2 * (vgg16_precision * vgg16_recall) / (vgg16_precision + vgg16_recall)\n\n# Sensitivity (Recall per class) with safe division\nvgg16_conf_matrix = confusion_matrix(y_val.argmax(axis=1), vgg16_y_pred_binary.argmax(axis=1))\nvgg16_sensitivity = np.divide(\n    np.diag(vgg16_conf_matrix),\n    np.sum(vgg16_conf_matrix, axis=1),\n    out=np.zeros_like(np.diag(vgg16_conf_matrix), dtype=np.float32),\n    where=np.sum(vgg16_conf_matrix, axis=1) != 0\n)\n\n# Print metrics\nprint(f\"VGG16 Training Accuracy: {vgg16_history.history['accuracy'][-1]:.4f}\")\nprint(f\"VGG16 Training Loss: {vgg16_history.history['loss'][-1]:.4f}\")\nprint(f\"VGG16 Validation Accuracy: {vgg16_history.history['val_accuracy'][-1]:.4f}\")\nprint(f\"VGG16 Validation Loss: {vgg16_history.history['val_loss'][-1]:.4f}\")\nprint(f\"VGG16 F1 Score: {vgg16_f1:.4f}\")\nprint(f\"VGG16 Precision: {vgg16_precision:.4f}\")\nprint(f\"VGG16 Recall (Sensitivity): {vgg16_recall:.4f}\")\nprint(f\"VGG16 IoU: {vgg16_iou:.4f}\")\nprint(f\"VGG16 Dice Coefficient: {vgg16_dice:.4f}\")\nprint(f\"VGG16 Sensitivity (per class): {vgg16_sensitivity}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T20:25:26.245853Z","iopub.execute_input":"2024-12-09T20:25:26.246303Z","iopub.status.idle":"2024-12-09T20:26:16.387178Z","shell.execute_reply.started":"2024-12-09T20:25:26.246267Z","shell.execute_reply":"2024-12-09T20:26:16.384969Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.applications import EfficientNetB0\n\n# Define the EfficientNetB0 model ******************************************************************************************************************************\ndef create_efficientnet_model(input_shape=(224, 224, 3), num_classes=6):\n    base_model = EfficientNetB0(weights=\"imagenet\", include_top=False, input_shape=input_shape)\n    base_model.trainable = False  # Freeze base model\n    model = Sequential([\n        base_model,\n        GlobalAveragePooling2D(),\n        Dense(128, activation=\"relu\"),\n        Dropout(0.5),\n        Dense(num_classes, activation=\"sigmoid\")  # Multi-label classification\n    ])\n    model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),\n                  loss=\"binary_crossentropy\", metrics=[\"accuracy\"])\n    return model\n\n# Create and compile the model\nefficientnet_model = create_efficientnet_model()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T20:27:06.148574Z","iopub.execute_input":"2024-12-09T20:27:06.149052Z","iopub.status.idle":"2024-12-09T20:27:10.151186Z","shell.execute_reply.started":"2024-12-09T20:27:06.149009Z","shell.execute_reply":"2024-12-09T20:27:10.150088Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Train the EfficientNet model\nefficientnet_history = efficientnet_model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=3, batch_size=32)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T20:27:24.938239Z","iopub.execute_input":"2024-12-09T20:27:24.938648Z","iopub.status.idle":"2024-12-09T20:29:33.084111Z","shell.execute_reply.started":"2024-12-09T20:27:24.938611Z","shell.execute_reply":"2024-12-09T20:29:33.082771Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Predict on validation data\nefficientnet_y_pred = efficientnet_model.predict(X_val)\nefficientnet_y_pred_binary = (efficientnet_y_pred > 0.5).astype(int)\n\n# Calculate evaluation metrics\nefficientnet_f1 = f1_score(y_val, efficientnet_y_pred_binary, average='weighted', zero_division=1)\nefficientnet_precision = precision_score(y_val, efficientnet_y_pred_binary, average='weighted', zero_division=1)\nefficientnet_recall = recall_score(y_val, efficientnet_y_pred_binary, average='weighted', zero_division=1)\nefficientnet_iou = jaccard_score(y_val, efficientnet_y_pred_binary, average='weighted', zero_division=1)\nefficientnet_dice = 2 * (efficientnet_precision * efficientnet_recall) / (efficientnet_precision + efficientnet_recall)\n\n# Sensitivity (Recall per class) with safe division\nefficientnet_conf_matrix = confusion_matrix(y_val.argmax(axis=1), efficientnet_y_pred_binary.argmax(axis=1))\nefficientnet_sensitivity = np.divide(\n    np.diag(efficientnet_conf_matrix),\n    np.sum(efficientnet_conf_matrix, axis=1),\n    out=np.zeros_like(np.diag(efficientnet_conf_matrix), dtype=np.float32),\n    where=np.sum(efficientnet_conf_matrix, axis=1) != 0\n)\n\n# Print metrics\nprint(f\"EfficientNet Training Accuracy: {efficientnet_history.history['accuracy'][-1]:.4f}\")\nprint(f\"EfficientNet Training Loss: {efficientnet_history.history['loss'][-1]:.4f}\")\nprint(f\"EfficientNet Validation Accuracy: {efficientnet_history.history['val_accuracy'][-1]:.4f}\")\nprint(f\"EfficientNet Validation Loss: {efficientnet_history.history['val_loss'][-1]:.4f}\")\nprint(f\"EfficientNet F1 Score: {efficientnet_f1:.4f}\")\nprint(f\"EfficientNet Precision: {efficientnet_precision:.4f}\")\nprint(f\"EfficientNet Recall (Sensitivity): {efficientnet_recall:.4f}\")\nprint(f\"EfficientNet IoU: {efficientnet_iou:.4f}\")\nprint(f\"EfficientNet Dice Coefficient: {efficientnet_dice:.4f}\")\nprint(f\"EfficientNet Sensitivity (per class): {efficientnet_sensitivity}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T20:29:53.700251Z","iopub.execute_input":"2024-12-09T20:29:53.700695Z","iopub.status.idle":"2024-12-09T20:30:06.089664Z","shell.execute_reply.started":"2024-12-09T20:29:53.700642Z","shell.execute_reply":"2024-12-09T20:30:06.088408Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.applications import Xception\n\n# Define the Xception model *************************************************************************************************************************************\ndef create_xception_model(input_shape=(224, 224, 3), num_classes=6):\n    base_model = Xception(weights=\"imagenet\", include_top=False, input_shape=input_shape)\n    base_model.trainable = False  # Freeze base model\n    model = Sequential([\n        base_model,\n        GlobalAveragePooling2D(),\n        Dense(128, activation=\"relu\"),\n        Dropout(0.5),\n        Dense(num_classes, activation=\"sigmoid\")  # Multi-label classification\n    ])\n    model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),\n                  loss=\"binary_crossentropy\", metrics=[\"accuracy\"])\n    return model\n\n# Create and compile the model\nxception_model = create_xception_model()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T20:31:56.021878Z","iopub.execute_input":"2024-12-09T20:31:56.022373Z","iopub.status.idle":"2024-12-09T20:32:02.06431Z","shell.execute_reply.started":"2024-12-09T20:31:56.022326Z","shell.execute_reply":"2024-12-09T20:32:02.062814Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Train the Xception model\nxception_history = xception_model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=3, batch_size=32)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T20:32:53.442797Z","iopub.execute_input":"2024-12-09T20:32:53.443245Z","iopub.status.idle":"2024-12-09T20:38:02.147932Z","shell.execute_reply.started":"2024-12-09T20:32:53.443212Z","shell.execute_reply":"2024-12-09T20:38:02.146577Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Predict on validation data\nxception_y_pred = xception_model.predict(X_val)\nxception_y_pred_binary = (xception_y_pred > 0.5).astype(int)\n\n# Calculate evaluation metrics\nxception_f1 = f1_score(y_val, xception_y_pred_binary, average='weighted', zero_division=1)\nxception_precision = precision_score(y_val, xception_y_pred_binary, average='weighted', zero_division=1)\nxception_recall = recall_score(y_val, xception_y_pred_binary, average='weighted', zero_division=1)\nxception_iou = jaccard_score(y_val, xception_y_pred_binary, average='weighted', zero_division=1)\nxception_dice = 2 * (xception_precision * xception_recall) / (xception_precision + xception_recall)\n\n# Sensitivity (Recall per class) with safe division\nxception_conf_matrix = confusion_matrix(y_val.argmax(axis=1), xception_y_pred_binary.argmax(axis=1))\nxception_sensitivity = np.divide(\n    np.diag(xception_conf_matrix),\n    np.sum(xception_conf_matrix, axis=1),\n    out=np.zeros_like(np.diag(xception_conf_matrix), dtype=np.float32),\n    where=np.sum(xception_conf_matrix, axis=1) != 0\n)\n\n# Print metrics\nprint(f\"Xception Training Accuracy: {xception_history.history['accuracy'][-1]:.4f}\")\nprint(f\"Xception Training Loss: {xception_history.history['loss'][-1]:.4f}\")\nprint(f\"Xception Validation Accuracy: {xception_history.history['val_accuracy'][-1]:.4f}\")\nprint(f\"Xception Validation Loss: {xception_history.history['val_loss'][-1]:.4f}\")\nprint(f\"Xception F1 Score: {xception_f1:.4f}\")\nprint(f\"Xception Precision: {xception_precision:.4f}\")\nprint(f\"Xception Recall (Sensitivity): {xception_recall:.4f}\")\nprint(f\"Xception IoU: {xception_iou:.4f}\")\nprint(f\"Xception Dice Coefficient: {xception_dice:.4f}\")\nprint(f\"Xception Sensitivity (per class): {xception_sensitivity}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T20:39:01.543951Z","iopub.execute_input":"2024-12-09T20:39:01.544436Z","iopub.status.idle":"2024-12-09T20:39:23.54788Z","shell.execute_reply.started":"2024-12-09T20:39:01.544399Z","shell.execute_reply":"2024-12-09T20:39:23.546446Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plot accuracy and loss trends for Xception\nplt.figure(figsize=(12, 5))\n\n# Accuracy\nplt.subplot(1, 2, 1)\nplt.plot(xception_history.history['accuracy'], label='Training Accuracy')\nplt.plot(xception_history.history['val_accuracy'], label='Validation Accuracy')\nplt.title('Xception Accuracy Trends')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend()\n\n# Loss\nplt.subplot(1, 2, 2)\nplt.plot(xception_history.history['loss'], label='Training Loss')\nplt.plot(xception_history.history['val_loss'], label='Validation Loss')\nplt.title('Xception Loss Trends')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\n\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T22:25:03.21274Z","iopub.execute_input":"2024-12-09T22:25:03.214103Z","iopub.status.idle":"2024-12-09T22:25:03.730643Z","shell.execute_reply.started":"2024-12-09T22:25:03.214056Z","shell.execute_reply":"2024-12-09T22:25:03.729417Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.layers import Conv2D, BatchNormalization, Activation, Add, Multiply, Input\nfrom tensorflow.keras.models import Model\n\n# Attention block (simplified for MA-Net) *************************************************************************************************************************\ndef attention_block(x, g, inter_channel):\n    theta_x = Conv2D(inter_channel, kernel_size=(1, 1), strides=(1, 1))(x)\n    phi_g = Conv2D(inter_channel, kernel_size=(1, 1), strides=(1, 1))(g)\n    f = Activation('relu')(Add()([theta_x, phi_g]))\n    psi = Conv2D(1, kernel_size=(1, 1), strides=(1, 1), activation='sigmoid')(f)\n    return Multiply()([x, psi])\n\n# Define MA-Net architecture\ndef create_ma_net(input_shape=(224, 224, 3), num_classes=6):\n    inputs = Input(shape=input_shape)\n    \n    # Initial Conv Block\n    x = Conv2D(64, (3, 3), padding='same')(inputs)\n    x = BatchNormalization()(x)\n    x = Activation('relu')(x)\n    \n    # Multi-scale attention\n    g = Conv2D(64, (1, 1))(x)  # Global context\n    attention1 = attention_block(x, g, inter_channel=32)\n    attention2 = attention_block(x, g, inter_channel=64)\n    \n    # Combine attention outputs\n    x = Add()([attention1, attention2])\n    x = BatchNormalization()(x)\n    x = Activation('relu')(x)\n    \n    # Global Average Pooling\n    x = GlobalAveragePooling2D()(x)\n    \n    # Fully connected layers\n    x = Dense(128, activation='relu')(x)\n    x = Dropout(0.5)(x)\n    outputs = Dense(num_classes, activation='sigmoid')(x)  # Multi-label classification\n    \n    model = Model(inputs, outputs)\n    model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),\n                  loss=\"binary_crossentropy\", metrics=[\"accuracy\"])\n    return model\n\n# Create and compile the MA-Net model\nma_net_model = create_ma_net()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T20:39:50.488212Z","iopub.execute_input":"2024-12-09T20:39:50.488638Z","iopub.status.idle":"2024-12-09T20:39:50.60552Z","shell.execute_reply.started":"2024-12-09T20:39:50.48859Z","shell.execute_reply":"2024-12-09T20:39:50.604553Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Train the MA-Net model\nma_net_history = ma_net_model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=3, batch_size=32)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T20:39:57.698979Z","iopub.execute_input":"2024-12-09T20:39:57.700134Z","iopub.status.idle":"2024-12-09T20:52:29.879295Z","shell.execute_reply.started":"2024-12-09T20:39:57.700091Z","shell.execute_reply":"2024-12-09T20:52:29.877983Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Predict on validation data\nma_net_y_pred = ma_net_model.predict(X_val)\nma_net_y_pred_binary = (ma_net_y_pred > 0.5).astype(int)\n\n# Calculate evaluation metrics\nma_net_f1 = f1_score(y_val, ma_net_y_pred_binary, average='weighted', zero_division=1)\nma_net_precision = precision_score(y_val, ma_net_y_pred_binary, average='weighted', zero_division=1)\nma_net_recall = recall_score(y_val, ma_net_y_pred_binary, average='weighted', zero_division=1)\nma_net_iou = jaccard_score(y_val, ma_net_y_pred_binary, average='weighted', zero_division=1)\nma_net_dice = 2 * (ma_net_precision * ma_net_recall) / (ma_net_precision + ma_net_recall)\n\n# Sensitivity (Recall per class) with safe division\nma_net_conf_matrix = confusion_matrix(y_val.argmax(axis=1), ma_net_y_pred_binary.argmax(axis=1))\nma_net_sensitivity = np.divide(\n    np.diag(ma_net_conf_matrix),\n    np.sum(ma_net_conf_matrix, axis=1),\n    out=np.zeros_like(np.diag(ma_net_conf_matrix), dtype=np.float32),\n    where=np.sum(ma_net_conf_matrix, axis=1) != 0\n)\n\n# Print metrics\nprint(f\"MA-Net Training Accuracy: {ma_net_history.history['accuracy'][-1]:.4f}\")\nprint(f\"MA-Net Training Loss: {ma_net_history.history['loss'][-1]:.4f}\")\nprint(f\"MA-Net Validation Accuracy: {ma_net_history.history['val_accuracy'][-1]:.4f}\")\nprint(f\"MA-Net Validation Loss: {ma_net_history.history['val_loss'][-1]:.4f}\")\nprint(f\"MA-Net F1 Score: {ma_net_f1:.4f}\")\nprint(f\"MA-Net Precision: {ma_net_precision:.4f}\")\nprint(f\"MA-Net Recall (Sensitivity): {ma_net_recall:.4f}\")\nprint(f\"MA-Net IoU: {ma_net_iou:.4f}\")\nprint(f\"MA-Net Dice Coefficient: {ma_net_dice:.4f}\")\nprint(f\"MA-Net Sensitivity (per class): {ma_net_sensitivity}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T20:54:27.814491Z","iopub.execute_input":"2024-12-09T20:54:27.816735Z","iopub.status.idle":"2024-12-09T20:54:39.821417Z","shell.execute_reply.started":"2024-12-09T20:54:27.816536Z","shell.execute_reply":"2024-12-09T20:54:39.820151Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plot accuracy and loss trends for MA-Net\nplt.figure(figsize=(12, 5))\n\n# Accuracy\nplt.subplot(1, 2, 1)\nplt.plot(ma_net_history.history['accuracy'], label='Training Accuracy')\nplt.plot(ma_net_history.history['val_accuracy'], label='Validation Accuracy')\nplt.title('MA-Net Accuracy Trends')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend()\n\n# Loss\nplt.subplot(1, 2, 2)\nplt.plot(ma_net_history.history['loss'], label='Training Loss')\nplt.plot(ma_net_history.history['val_loss'], label='Validation Loss')\nplt.title('MA-Net Loss Trends')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\n\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T22:23:56.228379Z","iopub.execute_input":"2024-12-09T22:23:56.228923Z","iopub.status.idle":"2024-12-09T22:23:56.747058Z","shell.execute_reply.started":"2024-12-09T22:23:56.228879Z","shell.execute_reply":"2024-12-09T22:23:56.746009Z"}},"outputs":[],"execution_count":null}]}