{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.10","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":29653,"databundleVersionId":2420395,"sourceType":"competition"},{"sourceId":2542390,"sourceType":"datasetVersion","datasetId":1541666}],"dockerImageVersionId":30498,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install -q pydicom\n\nimport numpy as np\nimport pandas as pd\nimport os, cv2, pydicom\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nfrom tensorflow.keras import layers, models\nfrom tensorflow.keras.applications import VGG16, VGG19, Xception, InceptionV3, InceptionResNetV2\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom sklearn.metrics import roc_curve, auc, accuracy_score, f1_score, cohen_kappa_score, classification_report, confusion_matrix\nfrom sklearn.utils.class_weight import compute_class_weight\n\n# Load Data\nlabels_df = pd.read_csv('/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv')\ntrain_path = '/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/train/'\n\ndef load_image(patient_id, img_size=(224,224)):\n    folder = os.path.join(train_path, str(patient_id).zfill(5), \"T1w\")\n    files = sorted(os.listdir(folder)) if os.path.exists(folder) else []\n    if len(files) == 0: return None\n    path = os.path.join(folder, files[len(files)//2])\n    dcm = pydicom.dcmread(path)\n    img = cv2.resize(dcm.pixel_array, img_size) / 255.0\n    return img\n\nX, y = [], []\nfor _, row in labels_df.iterrows():\n    img = load_image(row['BraTS21ID'])\n    if img is not None:\n        X.append(img)\n        y.append(row['MGMT_value'])\n\nX = np.expand_dims(np.array(X), -1)\nX = np.repeat(X, 3, axis=-1)\ny = np.array(y)\n\n# Split\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=y, random_state=42)\nX_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.25, stratify=y_train, random_state=42)\n\n# Augmentation\naug = ImageDataGenerator(rotation_range=15, width_shift_range=0.1, height_shift_range=0.1,\n                         zoom_range=0.1, horizontal_flip=True)\naug.fit(X_train)\n\n# Model Builder\ndef build_model(base, input_shape=(224,224,3)):\n    base_model = base(weights='imagenet', include_top=False, input_shape=input_shape)\n    base_model.trainable = False\n    model = models.Sequential([\n        base_model,\n        layers.GlobalAveragePooling2D(),\n        layers.Dense(256, activation='relu'),\n        layers.Dropout(0.5),\n        layers.Dense(1, activation='sigmoid')\n    ])\n    model.compile(optimizer=tf.keras.optimizers.Adam(1e-4),\n                  loss='binary_crossentropy',\n                  metrics=['accuracy'])\n    return model\n\n# Train Models & Predict\nhistories = {}\npredictions = {}\n\nfor name, arch in {\n    'VGG16': VGG16,\n    'VGG19': VGG19,\n    'Xception': Xception,\n    'InceptionV3': InceptionV3,\n    'InceptionResNetV2': InceptionResNetV2\n}.items():\n    print(f\"\\nTraining {name}...\")\n    model = build_model(arch)\n    early_stop = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)\n    history = model.fit(\n        aug.flow(X_train, y_train, batch_size=32),\n        validation_data=(X_val, y_val),\n        epochs=30,\n        callbacks=[early_stop],\n        verbose=1\n    )\n    histories[name] = history\n    predictions[name] = model.predict(X_test).flatten()\n\n# Ensemble Predictions & Metrics\nensemble_probs = sum(predictions.values()) / len(predictions)\nensemble_preds = (ensemble_probs > 0.5).astype(int)\n\nfpr, tpr, _ = roc_curve(y_test, ensemble_probs)\nroc_auc = auc(fpr, tpr)\nacc = accuracy_score(y_test, ensemble_preds)\nf1 = f1_score(y_test, ensemble_preds)\nkappa = cohen_kappa_score(y_test, ensemble_preds)\n\n# Metric Summary\nprint(\"\\n===== Fuzzy Ensemble Evaluation Metrics =====\")\nprint(f\"Test Accuracy     : {acc:.4f}\")\nprint(f\"F1 Score          : {f1:.4f}\")\nprint(f\"Cohen’s Kappa     : {kappa:.4f}\")\nprint(f\"AUC               : {roc_auc:.4f}\")\n\nprint(\"\\nClassification Report:\\n\", classification_report(y_test, ensemble_preds))\nprint(\"Confusion Matrix:\\n\", confusion_matrix(y_test, ensemble_preds))\n\n# Training Summary Table \nresults = []\nfor name, history in histories.items():\n    results.append({\n        'Model': name,\n        'Train Accuracy': round(history.history['accuracy'][-1], 4),\n        'Validation Accuracy': round(history.history['val_accuracy'][-1], 4),\n        'Train Loss': round(history.history['loss'][-1], 4),\n        'Validation Loss': round(history.history['val_loss'][-1], 4)\n    })\n\ndf_results = pd.DataFrame(results)\nprint(\"\\n=== Training & Validation Summary ===\")\nprint(df_results.to_string(index=False))\n\n# === 10. ROC Curve ===\nplt.figure(figsize=(6, 6))\nplt.plot(fpr, tpr, label=f'Fuzzy Ensemble ROC (AUC = {roc_auc:.2f})')\nplt.plot([0, 1], [0, 1], 'k--')\nplt.xlabel('False Positive Rate')\nplt.ylabel('True Positive Rate')\nplt.title('ROC Curve - Fuzzy Ensemble (New Method II)')\nplt.legend()\nplt.grid()\nplt.show()\n\n# Accuracy/Loss Curves\nplt.figure(figsize=(14, 6))\nplt.subplot(1, 2, 1)\nfor name, history in histories.items():\n    plt.plot(history.history['val_accuracy'], label=name)\nplt.title('Validation Accuracy')\nplt.xlabel('Epoch'); plt.ylabel('Accuracy'); plt.legend()\n\nplt.subplot(1, 2, 2)\nfor name, history in histories.items():\n    plt.plot(history.history['val_loss'], label=name)\nplt.title('Validation Loss')\nplt.xlabel('Epoch'); plt.ylabel('Loss'); plt.legend()\n\nplt.suptitle('Training Curves for New Method II - 5 CNN Models')\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2025-05-07T01:08:15.469255Z","iopub.execute_input":"2025-05-07T01:08:15.469662Z","iopub.status.idle":"2025-05-07T01:13:09.204843Z","shell.execute_reply.started":"2025-05-07T01:08:15.469634Z","shell.execute_reply":"2025-05-07T01:13:09.203882Z"},"trusted":true},"outputs":[],"execution_count":null}]}