{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":14774,"databundleVersionId":875431,"sourceType":"competition"},{"sourceId":46865,"sourceType":"datasetVersion","datasetId":34835},{"sourceId":848739,"sourceType":"datasetVersion","datasetId":251095},{"sourceId":2037383,"sourceType":"datasetVersion","datasetId":1220153},{"sourceId":2812287,"sourceType":"datasetVersion","datasetId":1719146},{"sourceId":3242401,"sourceType":"datasetVersion","datasetId":1965297},{"sourceId":5203002,"sourceType":"datasetVersion","datasetId":3025918},{"sourceId":18900850,"sourceType":"kernelVersion"},{"sourceId":222017548,"sourceType":"kernelVersion"},{"sourceId":222155719,"sourceType":"kernelVersion"},{"sourceId":228351077,"sourceType":"kernelVersion"},{"sourceId":228562817,"sourceType":"kernelVersion"}],"dockerImageVersionId":30919,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport torch\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom torchvision import datasets, transforms\nfrom torch.utils.data import DataLoader, random_split\nfrom PIL import Image\n\n# Set the seed for all libraries\nseed = 42\ntorch.manual_seed(seed)\ntorch.cuda.manual_seed_all(seed)  # For multi-GPU setup\nnp.random.seed(seed)\n\n# Set deterministic behavior for cuDNN\ntorch.backends.cudnn.deterministic = True\ntorch.backends.cudnn.benchmark = False\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:48:54.539842Z","iopub.execute_input":"2025-03-22T20:48:54.540195Z","iopub.status.idle":"2025-03-22T20:49:02.298442Z","shell.execute_reply.started":"2025-03-22T20:48:54.540165Z","shell.execute_reply":"2025-03-22T20:49:02.297529Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torchvision.models as models\nfrom sklearn.metrics import f1_score, cohen_kappa_score,confusion_matrix","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:49:02.299479Z","iopub.execute_input":"2025-03-22T20:49:02.299902Z","iopub.status.idle":"2025-03-22T20:49:02.442474Z","shell.execute_reply.started":"2025-03-22T20:49:02.299879Z","shell.execute_reply":"2025-03-22T20:49:02.441737Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\nimport torch.nn as nn","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:49:02.443809Z","iopub.execute_input":"2025-03-22T20:49:02.444039Z","iopub.status.idle":"2025-03-22T20:49:02.447483Z","shell.execute_reply.started":"2025-03-22T20:49:02.444021Z","shell.execute_reply":"2025-03-22T20:49:02.446665Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torchvision.models as models\n\nclass ResNetModel(nn.Module):\n    def __init__(self, pretrained=True):\n        super(ResNetModel, self).__init__()\n        self.resnet = models.resnet18(pretrained=pretrained)  # Load ResNet18\n        \n        # Remove the original FC layer\n        num_ftrs = self.resnet.fc.in_features\n        self.resnet.fc = nn.Identity()  # Remove the last layer\n        \n        # Add custom classifier layers\n        self.classifier = nn.Sequential(\n            nn.Linear(num_ftrs, 256),  # First Dense layer\n            nn.ReLU(),\n            nn.Dropout(0.5),  # Dropout to prevent overfitting\n            nn.Linear(256, 128),  # Second Dense layer\n            nn.ReLU(),\n            nn.Dropout(0.5),\n            nn.Linear(128, 1)   # Output layer for binary classification\n        )\n\n    def forward(self, x):\n        x = self.resnet(x)  # Extract features from ResNet\n        x = self.classifier(x)  # Pass through classifier\n        return x","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:49:02.448485Z","iopub.execute_input":"2025-03-22T20:49:02.448769Z","iopub.status.idle":"2025-03-22T20:49:02.459026Z","shell.execute_reply.started":"2025-03-22T20:49:02.448749Z","shell.execute_reply":"2025-03-22T20:49:02.458257Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n\nmodel = ResNetModel(pretrained=False).to(device)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:49:02.459921Z","iopub.execute_input":"2025-03-22T20:49:02.460187Z","iopub.status.idle":"2025-03-22T20:49:03.062045Z","shell.execute_reply.started":"2025-03-22T20:49:02.460168Z","shell.execute_reply":"2025-03-22T20:49:03.061106Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch.optim as optim\n\n# optimizer = optim.Adam(model.parameters(), lr=0.0001)\n\n# # Handle class imbalance\n# class_weights = len(train_data) / (2.0 * train_data['binary_label'].value_counts().to_numpy())\n# pos_weight = torch.tensor(np.float64(class_weights[1] / class_weights[0])).to(device)\n\n# criterion = nn.BCEWithLogitsLoss(pos_weight=pos_weight)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:49:03.062863Z","iopub.execute_input":"2025-03-22T20:49:03.0631Z","iopub.status.idle":"2025-03-22T20:49:03.066672Z","shell.execute_reply.started":"2025-03-22T20:49:03.06308Z","shell.execute_reply":"2025-03-22T20:49:03.065696Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Messidor1_Data","metadata":{}},{"cell_type":"code","source":"import os\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport torchvision.transforms as transforms\nimport torchvision.models as models\nfrom torch.utils.data import DataLoader, Dataset\nfrom PIL import Image\nimport numpy as np\nfrom sklearn.metrics import cohen_kappa_score, accuracy_score,classification_report\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:49:03.067362Z","iopub.execute_input":"2025-03-22T20:49:03.067577Z","iopub.status.idle":"2025-03-22T20:49:03.08461Z","shell.execute_reply.started":"2025-03-22T20:49:03.067558Z","shell.execute_reply":"2025-03-22T20:49:03.083795Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from PIL import Image, ImageChops\n\ndef crop_to_object(img):\n\n\n    # Convert to grayscale\n    gray_im = img.convert(\"L\")\n\n    # Set your threshold value\n    threshold = 10  # Adjust this value as needed\n\n    # Create a binary image: pixels > threshold become 255 (white), else 0 (black)\n    binary_im = gray_im.point(lambda x: 255 if x > threshold else 0)\n\n    # Get the bounding box of the white regions (non-background)\n    bbox = binary_im.getbbox()\n\n    if bbox:\n        # Crop the image to the bounding box and save it\n        cropped_img = img.crop(bbox)\n        #cropped_img.save(output_path)\n        #print(f\"Cropped image saved as '{output_path}'\")\n        return cropped_img\n        \n# Demo usage:\n# if __name__ == \"__main__\":\n#     input_image_path = \"retina_image.png\"   # Path to your retina image\n#     output_image_path = \"cropped_retina.png\"  # Desired path for the cropped image\n\n#     # Crop the image and show the result if available\n#     cropped = crop_to_object(input_image_path, output_image_path)\n#     if cropped:\n#         cropped.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:49:03.086975Z","iopub.execute_input":"2025-03-22T20:49:03.087197Z","iopub.status.idle":"2025-03-22T20:49:03.098087Z","shell.execute_reply.started":"2025-03-22T20:49:03.087179Z","shell.execute_reply":"2025-03-22T20:49:03.097176Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nfrom torch.utils.data import Dataset\nfrom PIL import Image\n\nclass MessidorDataset(Dataset):\n    def __init__(self, root_dir, transform=None):\n        self.root_dir = root_dir\n        self.transform = transform\n        self.image_paths = []\n        self.labels = []\n\n        # Iterate over folders (0, 1, 2, 3) and collect valid image paths\n        for label in [0, 3]:  # Include only 0, 3\n            folder_path = os.path.join(root_dir, str(label))\n            for filename in os.listdir(folder_path):\n                if filename.endswith(\".tif\"):\n                    self.image_paths.append(os.path.join(folder_path, filename))\n\n                    # Assign binary labels: 0 (negative), 1 (positive)\n                    binary_label = 0 if label == 0 else 1\n                    self.labels.append(binary_label)\n\n    def __len__(self):\n        return len(self.image_paths)\n\n    def __getitem__(self, idx):\n        img_path = self.image_paths[idx]\n        label = self.labels[idx]\n\n        # Open the image and apply transformations\n        image = Image.open(img_path).convert(\"RGB\")\n        image = crop_to_object(image)\n        if self.transform:\n            image = self.transform(image)\n\n        return image, label","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:49:03.099791Z","iopub.execute_input":"2025-03-22T20:49:03.100114Z","iopub.status.idle":"2025-03-22T20:49:03.109659Z","shell.execute_reply.started":"2025-03-22T20:49:03.100079Z","shell.execute_reply":"2025-03-22T20:49:03.108895Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"crop_to_object(Image.open(\"/kaggle/input/messifor2/messidor2/IMAGES/20051020_43808_0100_PP.png\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:49:03.11067Z","iopub.execute_input":"2025-03-22T20:49:03.110962Z","iopub.status.idle":"2025-03-22T20:49:03.615959Z","shell.execute_reply.started":"2025-03-22T20:49:03.11091Z","shell.execute_reply":"2025-03-22T20:49:03.61494Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ============================\n# Data Transformations & Dataloaders\n# ============================\ntransform = transforms.Compose([\n    transforms.Resize((224, 224)),  # Resizing for ResNet\n    transforms.ToTensor(),\n    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])\n])\n\ntest_dir = \"/kaggle/input/messidor1-data/P_Data/Test\"\n\ntest_dataset = MessidorDataset(test_dir, transform=transform)\n\ntest_loader = DataLoader(test_dataset, batch_size=16, shuffle=False)\n\nprint(f\"Testing samples: {len(test_dataset)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:49:03.616753Z","iopub.execute_input":"2025-03-22T20:49:03.6171Z","iopub.status.idle":"2025-03-22T20:49:03.655086Z","shell.execute_reply.started":"2025-03-22T20:49:03.617068Z","shell.execute_reply":"2025-03-22T20:49:03.654096Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ============================\n# Evaluation Function (QWK & Accuracy)\n# ============================\ndef evaluate(model, test_loader):\n    model.eval()\n    y_true = []\n    y_pred = []\n\n    with torch.no_grad():\n        for images, labels in test_loader:\n            images, labels = images.to(device), labels.to(device)\n\n            outputs = model(images)\n            preds = (torch.sigmoid(outputs) > 0.5).float()\n\n            y_true.extend(labels.cpu().numpy())\n            y_pred.extend(preds.cpu().numpy())\n\n    # Compute Accuracy & Quadratic Weighted Kappa (QWK)\n    accuracy = accuracy_score(y_true, y_pred)\n\n    print(f\"✅ Test Accuracy: {accuracy:.4f}\")\n    \n    print(classification_report(y_true,y_pred))\n    print(confusion_matrix(y_true,y_pred))\n    return y_true ,y_pred\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:49:03.656073Z","iopub.execute_input":"2025-03-22T20:49:03.656404Z","iopub.status.idle":"2025-03-22T20:49:03.664712Z","shell.execute_reply.started":"2025-03-22T20:49:03.656373Z","shell.execute_reply":"2025-03-22T20:49:03.663481Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.load_state_dict(torch.load(\"/kaggle/input/eyepacs-aptos-resnet-model/EyePacs_APTOS_best_resnet_model.pth\"))\nmodel.eval()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:49:03.665895Z","iopub.execute_input":"2025-03-22T20:49:03.666326Z","iopub.status.idle":"2025-03-22T20:49:04.322388Z","shell.execute_reply.started":"2025-03-22T20:49:03.666285Z","shell.execute_reply":"2025-03-22T20:49:04.321412Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ============================\n# Run Evaluation\n# ============================\ny_true ,y_pred = evaluate(model, test_loader)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:49:04.323205Z","iopub.execute_input":"2025-03-22T20:49:04.323442Z","iopub.status.idle":"2025-03-22T20:49:33.787235Z","shell.execute_reply.started":"2025-03-22T20:49:04.323423Z","shell.execute_reply":"2025-03-22T20:49:33.785606Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import confusion_matrix\n\n# Compute confusion matrix\ncm = confusion_matrix(y_true, y_pred)\n\n# Plot confusion matrix\nplt.figure(figsize=(6, 5))\nsns.heatmap(cm, annot=True, fmt=\"d\", cmap=\"Blues\", xticklabels=[\"Class 0\", \"Class 1\"], yticklabels=[\"Class 0\", \"Class 1\"])\nplt.xlabel(\"Predicted Label\")\nplt.ylabel(\"True Label\")\nplt.title(\"Confusion Matrix\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:49:33.78827Z","iopub.execute_input":"2025-03-22T20:49:33.788659Z","iopub.status.idle":"2025-03-22T20:49:34.033459Z","shell.execute_reply.started":"2025-03-22T20:49:33.788613Z","shell.execute_reply":"2025-03-22T20:49:34.032445Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\n\n# Convert predictions to numpy for histogram\ny_pred = np.array(y_pred)\n\nplt.figure(figsize=(6, 5))\nplt.hist(y_pred, bins=[-0.5, 0.5, 1.5], edgecolor='black', alpha=0.7, color='blue')\nplt.xticks([0, 1], labels=[\"No DR\", \"DR\"])\nplt.xlabel(\"Predicted Class\")\nplt.ylabel(\"Count\")\nplt.title(\"Distribution of Predictions\")\nplt.grid(axis=\"y\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:49:34.034606Z","iopub.execute_input":"2025-03-22T20:49:34.034963Z","iopub.status.idle":"2025-03-22T20:49:34.180647Z","shell.execute_reply.started":"2025-03-22T20:49:34.03493Z","shell.execute_reply":"2025-03-22T20:49:34.179783Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import precision_recall_curve\n\n# Compute precision-recall curve\nprecision, recall, _ = precision_recall_curve(y_true, y_pred)\n\nplt.figure(figsize=(6, 5))\nplt.plot(recall, precision, marker=\".\")\nplt.xlabel(\"Recall\")\nplt.ylabel(\"Precision\")\nplt.title(\"Precision-Recall Curve\")\nplt.grid()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:49:34.181533Z","iopub.execute_input":"2025-03-22T20:49:34.18184Z","iopub.status.idle":"2025-03-22T20:49:34.38625Z","shell.execute_reply.started":"2025-03-22T20:49:34.181808Z","shell.execute_reply":"2025-03-22T20:49:34.385035Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import roc_curve, auc\n\n# Compute ROC curve\nfpr, tpr, _ = roc_curve(y_true, y_pred)\nroc_auc = auc(fpr, tpr)\n\nplt.figure(figsize=(6, 5))\nplt.plot(fpr, tpr, color=\"blue\", lw=2, label=f\"AUC = {roc_auc:.2f}\")\nplt.plot([0, 1], [0, 1], color=\"gray\", linestyle=\"--\")\nplt.xlabel(\"False Positive Rate\")\nplt.ylabel(\"True Positive Rate\")\nplt.title(\"Receiver Operating Characteristic (ROC) Curve\")\nplt.legend(loc=\"lower right\")\nplt.grid()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:49:34.387119Z","iopub.execute_input":"2025-03-22T20:49:34.387406Z","iopub.status.idle":"2025-03-22T20:49:34.621015Z","shell.execute_reply.started":"2025-03-22T20:49:34.387376Z","shell.execute_reply":"2025-03-22T20:49:34.619934Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# IDRiD: Diabetic Retinopathy – Grading","metadata":{}},{"cell_type":"code","source":"# Define dataset paths\nimage_dir = \"/kaggle/input/idrid-dataset/Imagenes/Imagenes\"\ncsv_file = \"/kaggle/input/idrid-dataset/idrid_labels.csv\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:49:34.622175Z","iopub.execute_input":"2025-03-22T20:49:34.622568Z","iopub.status.idle":"2025-03-22T20:49:34.62711Z","shell.execute_reply.started":"2025-03-22T20:49:34.622525Z","shell.execute_reply":"2025-03-22T20:49:34.626142Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define image transformations\ntransform = transforms.Compose([\n    transforms.Resize((224, 224)),  # Resize images to match model input\n    transforms.ToTensor(),\n    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:49:34.628361Z","iopub.execute_input":"2025-03-22T20:49:34.628739Z","iopub.status.idle":"2025-03-22T20:49:34.640791Z","shell.execute_reply.started":"2025-03-22T20:49:34.628702Z","shell.execute_reply":"2025-03-22T20:49:34.639737Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define dataset class\nclass IDRiDDataset(Dataset):\n    def __init__(self, image_dir, csv_file, transform=None):\n        self.image_dir = image_dir\n        self.transform = transform\n        self.df = pd.read_csv(csv_file)\n\n        # Ensure column names are correctly read\n        self.df.columns = self.df.columns.str.strip()\n\n    def __len__(self):\n        return len(self.df)\n\n    def __getitem__(self, idx):\n        # Extract image ID and label\n        img_id = self.df.iloc[idx][\"id_code\"]\n\n        if self.df.iloc[idx][\"diagnosis\"] == 0:\n            label = 0\n        else:\n            label = 1\n        #label = self.df.iloc[idx][\"diagnosis\"]\n\n        # Load image\n        img_path = os.path.join(self.image_dir, img_id + \".jpg\")\n        image = Image.open(img_path).convert(\"RGB\")\n\n        # Apply transformations\n        if self.transform:\n            image = self.transform(image)\n\n        return image, label","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:49:34.64179Z","iopub.execute_input":"2025-03-22T20:49:34.642117Z","iopub.status.idle":"2025-03-22T20:49:34.655561Z","shell.execute_reply.started":"2025-03-22T20:49:34.642086Z","shell.execute_reply":"2025-03-22T20:49:34.654701Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Create dataset and dataloader\nbatch_size = 64\ntest_dataset = IDRiDDataset(image_dir=image_dir, csv_file=csv_file, transform=transform)\ntest_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False, num_workers=2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:49:34.656547Z","iopub.execute_input":"2025-03-22T20:49:34.656842Z","iopub.status.idle":"2025-03-22T20:49:34.692689Z","shell.execute_reply.started":"2025-03-22T20:49:34.656816Z","shell.execute_reply":"2025-03-22T20:49:34.691661Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load trained model\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel.to(device)\nmodel.eval()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:49:34.699074Z","iopub.execute_input":"2025-03-22T20:49:34.699438Z","iopub.status.idle":"2025-03-22T20:49:34.710553Z","shell.execute_reply.started":"2025-03-22T20:49:34.699407Z","shell.execute_reply":"2025-03-22T20:49:34.709632Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch.nn.functional as F\n\n# Evaluation function\ndef evaluate_model(model, dataloader):\n    all_preds = []\n    all_labels = []\n\n    with torch.no_grad():\n        for images, labels in dataloader:\n            images, labels = images.to(device), labels.to(device)\n            \n            # Forward pass\n            outputs = model(images)\n            preds = (torch.sigmoid(outputs) > 0.5).float()\n\n            # Store predictions and true labels\n            all_preds.extend(preds.cpu().numpy())\n            all_labels.extend(labels.cpu().numpy())\n\n    # Compute evaluation metrics\n    accuracy = accuracy_score(all_labels, all_preds)\n    \n\n    print(f\"Accuracy: {accuracy:.4f}\")\n    \n    print(classification_report(all_labels, all_preds))\n\n    print(confusion_matrix(all_labels, all_preds))\n    return all_labels, all_preds\n\n# Run evaluation\nall_labels, all_preds = evaluate_model(model, test_loader)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:49:34.712433Z","iopub.execute_input":"2025-03-22T20:49:34.712704Z","iopub.status.idle":"2025-03-22T20:50:23.343588Z","shell.execute_reply.started":"2025-03-22T20:49:34.712684Z","shell.execute_reply":"2025-03-22T20:50:23.342569Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import confusion_matrix\n\n# Compute confusion matrix\ncm = confusion_matrix(all_labels, all_preds)\n\n# Plot confusion matrix\nplt.figure(figsize=(6, 5))\nsns.heatmap(cm, annot=True, fmt=\"d\", cmap=\"Blues\", xticklabels=[\"Class 0\", \"Class 1\"], yticklabels=[\"Class 0\", \"Class 1\"])\nplt.xlabel(\"Predicted Label\")\nplt.ylabel(\"True Label\")\nplt.title(\"Confusion Matrix\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:50:23.344713Z","iopub.execute_input":"2025-03-22T20:50:23.345004Z","iopub.status.idle":"2025-03-22T20:50:23.550802Z","shell.execute_reply.started":"2025-03-22T20:50:23.344977Z","shell.execute_reply":"2025-03-22T20:50:23.549846Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\n\n# Convert predictions to numpy for histogram\nall_preds = np.array(all_preds)\n\nplt.figure(figsize=(6, 5))\nplt.hist(all_preds, bins=[-0.5, 0.5, 1.5], edgecolor='black', alpha=0.7, color='blue')\nplt.xticks([0, 1], labels=[\"No DR\", \"DR\"])\nplt.xlabel(\"Predicted Class\")\nplt.ylabel(\"Count\")\nplt.title(\"Distribution of Predictions\")\nplt.grid(axis=\"y\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:50:23.551954Z","iopub.execute_input":"2025-03-22T20:50:23.552416Z","iopub.status.idle":"2025-03-22T20:50:23.706981Z","shell.execute_reply.started":"2025-03-22T20:50:23.552358Z","shell.execute_reply":"2025-03-22T20:50:23.705952Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import precision_recall_curve\n\n# Compute precision-recall curve\nprecision, recall, _ = precision_recall_curve(all_labels, all_preds)\n\nplt.figure(figsize=(6, 5))\nplt.plot(recall, precision, marker=\".\")\nplt.xlabel(\"Recall\")\nplt.ylabel(\"Precision\")\nplt.title(\"Precision-Recall Curve\")\nplt.grid()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:50:23.70809Z","iopub.execute_input":"2025-03-22T20:50:23.708397Z","iopub.status.idle":"2025-03-22T20:50:23.917923Z","shell.execute_reply.started":"2025-03-22T20:50:23.708363Z","shell.execute_reply":"2025-03-22T20:50:23.916801Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import roc_curve, auc\n\n# Compute ROC curve\nfpr, tpr, _ = roc_curve(all_labels, all_preds)\nroc_auc = auc(fpr, tpr)\n\nplt.figure(figsize=(6, 5))\nplt.plot(fpr, tpr, color=\"blue\", lw=2, label=f\"AUC = {roc_auc:.2f}\")\nplt.plot([0, 1], [0, 1], color=\"gray\", linestyle=\"--\")\nplt.xlabel(\"False Positive Rate\")\nplt.ylabel(\"True Positive Rate\")\nplt.title(\"Receiver Operating Characteristic (ROC) Curve\")\nplt.legend(loc=\"lower right\")\nplt.grid()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:50:23.918978Z","iopub.execute_input":"2025-03-22T20:50:23.919299Z","iopub.status.idle":"2025-03-22T20:50:24.139277Z","shell.execute_reply.started":"2025-03-22T20:50:23.919274Z","shell.execute_reply":"2025-03-22T20:50:24.138005Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Aptos\n","metadata":{}},{"cell_type":"code","source":"# Define dataset paths\nimage_dir = \"/kaggle/input/aptos2019-blindness-detection/train_images\"\ncsv_file = \"/kaggle/input/aptos2019-blindness-detection/train.csv\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:50:24.140191Z","iopub.execute_input":"2025-03-22T20:50:24.14044Z","iopub.status.idle":"2025-03-22T20:50:24.144349Z","shell.execute_reply.started":"2025-03-22T20:50:24.14042Z","shell.execute_reply":"2025-03-22T20:50:24.143453Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define image transformations\ntransform = transforms.Compose([\n    transforms.Resize((224, 224)),  # Resize images to match model input\n    transforms.ToTensor(),\n    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:50:24.145136Z","iopub.execute_input":"2025-03-22T20:50:24.145405Z","iopub.status.idle":"2025-03-22T20:50:24.15661Z","shell.execute_reply.started":"2025-03-22T20:50:24.145382Z","shell.execute_reply":"2025-03-22T20:50:24.155766Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define dataset class\nclass APTOSDataset(Dataset):\n    def __init__(self, image_dir, csv_file, transform=None):\n        self.image_dir = image_dir\n        self.transform = transform\n        self.df = pd.read_csv(csv_file)\n\n        # Ensure column names are correctly read\n        self.df.columns = self.df.columns.str.strip()\n\n    def __len__(self):\n        return len(self.df)\n\n    def __getitem__(self, idx):\n        # Extract image ID and label\n        img_id = self.df.iloc[idx][\"id_code\"]\n\n        if self.df.iloc[idx][\"diagnosis\"] == 0:\n            label = 0\n        else:\n            label = 1\n        #label = self.df.iloc[idx][\"diagnosis\"]\n\n        # Load image\n        img_path = os.path.join(self.image_dir, img_id + \".png\")\n        image = Image.open(img_path).convert(\"RGB\")\n\n        # Apply transformations\n        if self.transform:\n            image = self.transform(image)\n\n        return image, label","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:50:24.157776Z","iopub.execute_input":"2025-03-22T20:50:24.158017Z","iopub.status.idle":"2025-03-22T20:50:24.168785Z","shell.execute_reply.started":"2025-03-22T20:50:24.15799Z","shell.execute_reply":"2025-03-22T20:50:24.167822Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Create dataset and dataloader\nbatch_size = 64\ntest_dataset = APTOSDataset(image_dir=image_dir, csv_file=csv_file, transform=transform)\ntest_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False, num_workers=2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:50:24.169766Z","iopub.execute_input":"2025-03-22T20:50:24.170165Z","iopub.status.idle":"2025-03-22T20:50:24.199568Z","shell.execute_reply.started":"2025-03-22T20:50:24.170137Z","shell.execute_reply":"2025-03-22T20:50:24.198746Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Run evaluation\nall_labels, all_preds = evaluate_model(model, test_loader)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:50:24.200425Z","iopub.execute_input":"2025-03-22T20:50:24.200731Z","iopub.status.idle":"2025-03-22T20:55:32.390338Z","shell.execute_reply.started":"2025-03-22T20:50:24.200692Z","shell.execute_reply":"2025-03-22T20:55:32.389162Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import confusion_matrix\n\n# Compute confusion matrix\ncm = confusion_matrix(all_labels, all_preds)\n\n# Plot confusion matrix\nplt.figure(figsize=(6, 5))\nsns.heatmap(cm, annot=True, fmt=\"d\", cmap=\"Blues\", xticklabels=[\"Class 0\", \"Class 1\"], yticklabels=[\"Class 0\", \"Class 1\"])\nplt.xlabel(\"Predicted Label\")\nplt.ylabel(\"True Label\")\nplt.title(\"Confusion Matrix\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:55:32.391583Z","iopub.execute_input":"2025-03-22T20:55:32.391956Z","iopub.status.idle":"2025-03-22T20:55:32.596738Z","shell.execute_reply.started":"2025-03-22T20:55:32.391927Z","shell.execute_reply":"2025-03-22T20:55:32.595903Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\n\n# Convert predictions to numpy for histogram\nall_preds = np.array(all_preds)\n\nplt.figure(figsize=(6, 5))\nplt.hist(all_preds, bins=[-0.5, 0.5, 1.5], edgecolor='black', alpha=0.7, color='blue')\nplt.xticks([0, 1], labels=[\"No DR\", \"DR\"])\nplt.xlabel(\"Predicted Class\")\nplt.ylabel(\"Count\")\nplt.title(\"Distribution of Predictions\")\nplt.grid(axis=\"y\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:55:32.597513Z","iopub.execute_input":"2025-03-22T20:55:32.597731Z","iopub.status.idle":"2025-03-22T20:55:32.750801Z","shell.execute_reply.started":"2025-03-22T20:55:32.597714Z","shell.execute_reply":"2025-03-22T20:55:32.749867Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import precision_recall_curve\n\n# Compute precision-recall curve\nprecision, recall, _ = precision_recall_curve(all_labels, all_preds)\n\nplt.figure(figsize=(6, 5))\nplt.plot(recall, precision, marker=\".\")\nplt.xlabel(\"Recall\")\nplt.ylabel(\"Precision\")\nplt.title(\"Precision-Recall Curve\")\nplt.grid()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:55:32.751525Z","iopub.execute_input":"2025-03-22T20:55:32.75184Z","iopub.status.idle":"2025-03-22T20:55:32.919707Z","shell.execute_reply.started":"2025-03-22T20:55:32.75182Z","shell.execute_reply":"2025-03-22T20:55:32.918911Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import roc_curve, auc\n\n# Compute ROC curve\nfpr, tpr, _ = roc_curve(all_labels, all_preds)\nroc_auc = auc(fpr, tpr)\n\nplt.figure(figsize=(6, 5))\nplt.plot(fpr, tpr, color=\"blue\", lw=2, label=f\"AUC = {roc_auc:.2f}\")\nplt.plot([0, 1], [0, 1], color=\"gray\", linestyle=\"--\")\nplt.xlabel(\"False Positive Rate\")\nplt.ylabel(\"True Positive Rate\")\nplt.title(\"Receiver Operating Characteristic (ROC) Curve\")\nplt.legend(loc=\"lower right\")\nplt.grid()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:55:32.920528Z","iopub.execute_input":"2025-03-22T20:55:32.920793Z","iopub.status.idle":"2025-03-22T20:55:33.108748Z","shell.execute_reply.started":"2025-03-22T20:55:32.920774Z","shell.execute_reply":"2025-03-22T20:55:33.107865Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Messidor2","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport torch\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision import transforms\nfrom PIL import Image\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.optim as optim\nfrom sklearn.metrics import confusion_matrix, classification_report\n\n# Define image transformations\ntransform = transforms.Compose([\n    transforms.Resize((224, 224)),  # Resize to match model input\n    transforms.ToTensor(),\n    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])\n])\n\n# Correct paths\ncsv_path = \"/kaggle/input/messidor2-dr-grades/messidor_data.csv\"\nimg_folder = \"/kaggle/input/messifor2/messidor2/IMAGES\"\n\n# Load CSV file\nMessidor2_data = pd.read_csv(csv_path)\n\n# Ensure correct data types and drop missing labels\nMessidor2_data = Messidor2_data.dropna(subset=[\"adjudicated_dr_grade\"])  # Drop NaNs\nMessidor2_data[\"adjudicated_dr_grade\"] = Messidor2_data[\"adjudicated_dr_grade\"].astype(int)  # Ensure integer labels\n\n# Convert to binary classification (0: No DR, 1-4: Has DR)\nMessidor2_data[\"binary_label\"] = Messidor2_data[\"adjudicated_dr_grade\"].apply(lambda x: 0 if x == 0 else 1)\n\nclass MessidorDataset(Dataset):\n    def __init__(self, dataframe, root_dir, transform=None):\n        self.data = dataframe\n        self.root_dir = root_dir\n        self.transform = transform\n\n        # Ensure image_id column has \".png\" extension\n        self.data[\"image_id\"] = self.data[\"image_id\"].astype(str).apply(lambda x: x if x.endswith(\".png\") else x + \".png\")\n\n        # Get valid image files in the folder\n        self.valid_images = set(os.listdir(root_dir))\n\n        # Filter dataset to include only existing images\n        self.data = self.data[self.data[\"image_id\"].isin(self.valid_images)].reset_index(drop=True)\n\n    def __len__(self):\n        return len(self.data)\n\n    def __getitem__(self, idx):\n        img_name = self.data.iloc[idx][\"image_id\"]\n        img_path = os.path.join(self.root_dir, img_name)\n\n        # Load image\n        image = Image.open(img_path).convert(\"RGB\")\n\n        # Extract label as binary classification\n        label = int(self.data.iloc[idx][\"binary_label\"])\n\n        # Apply transformations\n        if self.transform:\n            image = self.transform(image)\n\n        return image, torch.tensor(label, dtype=torch.float)  # BCEWithLogitsLoss expects float labels\n\n# Create dataset and dataloader\ntest_dataset = MessidorDataset(Messidor2_data, img_folder, transform=transform)\ntest_loader = DataLoader(test_dataset, batch_size=32, shuffle=False)\n\nprint(f\"Total images found: {len(test_dataset)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:55:33.109403Z","iopub.execute_input":"2025-03-22T20:55:33.109628Z","iopub.status.idle":"2025-03-22T20:55:33.218021Z","shell.execute_reply.started":"2025-03-22T20:55:33.109609Z","shell.execute_reply":"2025-03-22T20:55:33.21732Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load trained model\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel.load_state_dict(torch.load(\"/kaggle/input/eyepacs-resnet/EyePacs_best_resnet_model.pth\", map_location=device))\nmodel.to(device)\nmodel.eval()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:55:33.218903Z","iopub.execute_input":"2025-03-22T20:55:33.219226Z","iopub.status.idle":"2025-03-22T20:55:33.840213Z","shell.execute_reply.started":"2025-03-22T20:55:33.219204Z","shell.execute_reply":"2025-03-22T20:55:33.83947Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Evaluate model\ndef evaluate_binary_model(model, dataloader):\n    model.eval()\n    all_labels, all_preds = [], []\n\n    with torch.no_grad():\n        for images, labels in dataloader:\n            images, labels = images.to(device), labels.to(device)\n\n            outputs = model(images)\n            probs = torch.sigmoid(outputs)  # Convert to probability\n            preds = (probs > 0.5).float()  # Convert to 0 or 1\n\n            all_labels.extend(labels.cpu().numpy())\n            all_preds.extend(preds.cpu().numpy())\n\n    return all_labels, all_preds\n\n# Run evaluation\nall_labels, all_preds = evaluate_binary_model(model, test_loader)\n\n# Compute binary confusion matrix\ncm = confusion_matrix(all_labels, all_preds)\nprint(\"Confusion Matrix:\\n\", cm)\n\n# Classification Report\nprint(\"Classification Report:\\n\", classification_report(all_labels, all_preds, target_names=[\"No DR\", \"Has DR\"]))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:55:33.841031Z","iopub.execute_input":"2025-03-22T20:55:33.841355Z","iopub.status.idle":"2025-03-22T20:57:28.581463Z","shell.execute_reply.started":"2025-03-22T20:55:33.841332Z","shell.execute_reply":"2025-03-22T20:57:28.58052Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import seaborn as sns\n\n# Plot Confusion Matrix\ndef plot_confusion_matrix(y_true, y_pred):\n    cm = confusion_matrix(y_true, y_pred)\n    plt.figure(figsize=(6, 5))\n    sns.heatmap(cm, annot=True, fmt=\"d\", cmap=\"Blues\", xticklabels=[\"Negative (0)\", \"Positive (1)\"], yticklabels=[\"Negative (0)\", \"Positive (1)\"])\n    plt.xlabel(\"Predicted Label\")\n    plt.ylabel(\"True Label\")\n    plt.title(\"Confusion Matrix\")\n    plt.show()\n\n# Accuracy Bar Plot\ndef plot_accuracy(accuracy):\n    plt.figure(figsize=(5, 5))\n    plt.bar([\"Accuracy\"], [accuracy], color=\"skyblue\")\n    plt.ylim(0, 1)\n    plt.ylabel(\"Score\")\n    plt.title(\"Model Accuracy\")\n    plt.show()\n\n# Compute Accuracy for Plot\naccuracy = accuracy_score(all_labels, all_preds)\n\n# Show Plots\nplot_confusion_matrix(all_labels, all_preds)\nplot_accuracy(accuracy)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:57:28.582407Z","iopub.execute_input":"2025-03-22T20:57:28.582776Z","iopub.status.idle":"2025-03-22T20:57:28.933624Z","shell.execute_reply.started":"2025-03-22T20:57:28.582742Z","shell.execute_reply":"2025-03-22T20:57:28.932552Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\n\n# Convert predictions to numpy for histogram\nall_preds = np.array(all_preds)\n\nplt.figure(figsize=(6, 5))\nplt.hist(all_preds, bins=[-0.5, 0.5, 1.5], edgecolor='black', alpha=0.7, color='blue')\nplt.xticks([0, 1], labels=[\"No DR\", \"DR\"])\nplt.xlabel(\"Predicted Class\")\nplt.ylabel(\"Count\")\nplt.title(\"Distribution of Predictions\")\nplt.grid(axis=\"y\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:57:28.934599Z","iopub.execute_input":"2025-03-22T20:57:28.934876Z","iopub.status.idle":"2025-03-22T20:57:29.111938Z","shell.execute_reply.started":"2025-03-22T20:57:28.934854Z","shell.execute_reply":"2025-03-22T20:57:29.110863Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import precision_recall_curve\n\n# Compute precision-recall curve\nprecision, recall, _ = precision_recall_curve(all_labels, all_preds)\n\nplt.figure(figsize=(6, 5))\nplt.plot(recall, precision, marker=\".\")\nplt.xlabel(\"Recall\")\nplt.ylabel(\"Precision\")\nplt.title(\"Precision-Recall Curve\")\nplt.grid()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:57:29.112993Z","iopub.execute_input":"2025-03-22T20:57:29.113342Z","iopub.status.idle":"2025-03-22T20:57:29.538671Z","shell.execute_reply.started":"2025-03-22T20:57:29.113305Z","shell.execute_reply":"2025-03-22T20:57:29.537636Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import roc_curve, auc\n\n# Compute ROC curve\nfpr, tpr, _ = roc_curve(all_labels, all_preds)\nroc_auc = auc(fpr, tpr)\n\nplt.figure(figsize=(6, 5))\nplt.plot(fpr, tpr, color=\"blue\", lw=2, label=f\"AUC = {roc_auc:.2f}\")\nplt.plot([0, 1], [0, 1], color=\"gray\", linestyle=\"--\")\nplt.xlabel(\"False Positive Rate\")\nplt.ylabel(\"True Positive Rate\")\nplt.title(\"Receiver Operating Characteristic (ROC) Curve\")\nplt.legend(loc=\"lower right\")\nplt.grid()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:57:29.539843Z","iopub.execute_input":"2025-03-22T20:57:29.540222Z","iopub.status.idle":"2025-03-22T20:57:29.737042Z","shell.execute_reply.started":"2025-03-22T20:57:29.540188Z","shell.execute_reply":"2025-03-22T20:57:29.736069Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# DDR","metadata":{}},{"cell_type":"code","source":"import pandas as pd\n\n# Set path to DDR dataset\nDDR_DIR = \"/kaggle/input/idriddiseasegrading/DDR-dataset/DR_grading\"\nTEST_CSV = f\"{DDR_DIR}/test.csv\"\n\n# Load test.csv\ndf_test = pd.read_csv(TEST_CSV)\n\n# Convert multi-class labels to binary labels\ndf_test['binary_label'] = df_test['Retinopathy grade'].apply(lambda x: 0 if x == 0 else 1)\n\n# Display dataset preview\nprint(df_test.head())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:57:29.737917Z","iopub.execute_input":"2025-03-22T20:57:29.738198Z","iopub.status.idle":"2025-03-22T20:57:29.778766Z","shell.execute_reply.started":"2025-03-22T20:57:29.738176Z","shell.execute_reply":"2025-03-22T20:57:29.777998Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nfrom PIL import Image\nimport torch\nfrom torch.utils.data import Dataset\nimport torchvision.transforms as transforms\n\nclass DDRDataset(Dataset):\n    def __init__(self, df, img_dir, transform=None):\n        self.df = df\n        self.img_dir = img_dir\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.df)\n\n    def __getitem__(self, idx):\n        # Get image file name and corresponding label\n        img_id = str(self.df.iloc[idx]['Image name'])\n        img_path = os.path.join(self.img_dir, img_id)\n        label = int(self.df.iloc[idx]['binary_label'])\n\n        # Load and preprocess image\n        image = Image.open(img_path).convert(\"RGB\")\n        if self.transform:\n            image = self.transform(image)\n\n        return image, label\n\n# Define transformation (resize to match model input)\ntest_transforms = transforms.Compose([\n    transforms.Resize((224, 224)),  # Adjust based on your model input size\n    transforms.ToTensor(),\n    transforms.Normalize(mean=[0.5], std=[0.5])\n])\n\n# Set path to DDR test images\nDDR_TEST_IMG_DIR = f\"{DDR_DIR}/test\"\n\n# Create dataset\ntest_dataset = DDRDataset(df_test, DDR_TEST_IMG_DIR, transform=test_transforms)\n\n# Create DataLoader\ntest_loader = torch.utils.data.DataLoader(test_dataset, batch_size=32, shuffle=False)\n\nprint(\"DDR Test DataLoader Ready!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:57:29.779531Z","iopub.execute_input":"2025-03-22T20:57:29.779757Z","iopub.status.idle":"2025-03-22T20:57:29.788593Z","shell.execute_reply.started":"2025-03-22T20:57:29.779736Z","shell.execute_reply":"2025-03-22T20:57:29.787846Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\nimport torch.nn.functional as F\n\n# Evaluate model\ncorrect = 0\ntotal = 0\nall_labels = []\nall_preds = []\n\nwith torch.no_grad():\n    for images, labels in test_loader:\n        images, labels = images.to(device), labels.to(device)\n\n        # Get predictions\n        outputs = model(images)\n        probs = torch.sigmoid(outputs)  # Convert to probability\n        preds = (probs > 0.5).float()   # Convert to 0 or 1\n\n        # Store results\n        all_labels.extend(labels.cpu().numpy())\n        all_preds.extend(preds.cpu().numpy())\n\n        # Calculate accuracy\n        correct += (preds == labels).sum().item()\n        total += labels.size(0)\n\n# Compute final accuracy\naccuracy = correct / total * 100\nprint(f\"Model Accuracy on DDR Test Data: {accuracy:.2f}%\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T20:57:29.789719Z","iopub.execute_input":"2025-03-22T20:57:29.790121Z","iopub.status.idle":"2025-03-22T21:03:55.843811Z","shell.execute_reply.started":"2025-03-22T20:57:29.790055Z","shell.execute_reply":"2025-03-22T21:03:55.842834Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Classification Report\nprint(\"Classification Report:\\n\", classification_report(all_labels, all_preds, target_names=[\"No DR\", \"Has DR\"]))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T21:03:55.844939Z","iopub.execute_input":"2025-03-22T21:03:55.845334Z","iopub.status.idle":"2025-03-22T21:03:55.872128Z","shell.execute_reply.started":"2025-03-22T21:03:55.845297Z","shell.execute_reply":"2025-03-22T21:03:55.871198Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import seaborn as sns\n\n# Plot Confusion Matrix\ndef plot_confusion_matrix(y_true, y_pred):\n    cm = confusion_matrix(y_true, y_pred)\n    plt.figure(figsize=(6, 5))\n    sns.heatmap(cm, annot=True, fmt=\"d\", cmap=\"Blues\", xticklabels=[\"Negative (0)\", \"Positive (1)\"], yticklabels=[\"Negative (0)\", \"Positive (1)\"])\n    plt.xlabel(\"Predicted Label\")\n    plt.ylabel(\"True Label\")\n    plt.title(\"Confusion Matrix\")\n    plt.show()\n\n# Accuracy Bar Plot\ndef plot_accuracy(accuracy):\n    plt.figure(figsize=(5, 5))\n    plt.bar([\"Accuracy\"], [accuracy], color=\"skyblue\")\n    plt.ylim(0, 1)\n    plt.ylabel(\"Score\")\n    plt.title(\"Model Accuracy\")\n    plt.show()\n\n# Compute Accuracy for Plot\naccuracy = accuracy_score(all_labels, all_preds)\n\n# Show Plots\nplot_confusion_matrix(all_labels, all_preds)\nplot_accuracy(accuracy)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T21:03:55.873084Z","iopub.execute_input":"2025-03-22T21:03:55.873349Z","iopub.status.idle":"2025-03-22T21:03:56.195445Z","shell.execute_reply.started":"2025-03-22T21:03:55.873327Z","shell.execute_reply":"2025-03-22T21:03:56.194543Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\n\n# Convert predictions to numpy for histogram\nall_preds = np.array(all_preds)\n\nplt.figure(figsize=(6, 5))\nplt.hist(all_preds, bins=[-0.5, 0.5, 1.5], edgecolor='black', alpha=0.7, color='blue')\nplt.xticks([0, 1], labels=[\"No DR\", \"DR\"])\nplt.xlabel(\"Predicted Class\")\nplt.ylabel(\"Count\")\nplt.title(\"Distribution of Predictions\")\nplt.grid(axis=\"y\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T21:03:56.196259Z","iopub.execute_input":"2025-03-22T21:03:56.196515Z","iopub.status.idle":"2025-03-22T21:03:56.340569Z","shell.execute_reply.started":"2025-03-22T21:03:56.196494Z","shell.execute_reply":"2025-03-22T21:03:56.33982Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import precision_recall_curve\n\n# Compute precision-recall curve\nprecision, recall, _ = precision_recall_curve(all_labels, all_preds)\n\nplt.figure(figsize=(6, 5))\nplt.plot(recall, precision, marker=\".\")\nplt.xlabel(\"Recall\")\nplt.ylabel(\"Precision\")\nplt.title(\"Precision-Recall Curve\")\nplt.grid()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T21:03:56.341354Z","iopub.execute_input":"2025-03-22T21:03:56.341639Z","iopub.status.idle":"2025-03-22T21:03:56.511251Z","shell.execute_reply.started":"2025-03-22T21:03:56.341598Z","shell.execute_reply":"2025-03-22T21:03:56.510365Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import roc_curve, auc\n\n# Compute ROC curve\nfpr, tpr, _ = roc_curve(all_labels, all_preds)\nroc_auc = auc(fpr, tpr)\n\nplt.figure(figsize=(6, 5))\nplt.plot(fpr, tpr, color=\"blue\", lw=2, label=f\"AUC = {roc_auc:.2f}\")\nplt.plot([0, 1], [0, 1], color=\"gray\", linestyle=\"--\")\nplt.xlabel(\"False Positive Rate\")\nplt.ylabel(\"True Positive Rate\")\nplt.title(\"Receiver Operating Characteristic (ROC) Curve\")\nplt.legend(loc=\"lower right\")\nplt.grid()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T21:03:56.512238Z","iopub.execute_input":"2025-03-22T21:03:56.512557Z","iopub.status.idle":"2025-03-22T21:03:56.696203Z","shell.execute_reply.started":"2025-03-22T21:03:56.512526Z","shell.execute_reply":"2025-03-22T21:03:56.695423Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"- APTOS\n- EyePacs\n\n1- split Aptos train 0.8 val 0.1 test 0.1\n2- split Eyepacs train 0.8 val 0.1 test 0.1\n\n3- balance Eyepacs train - down sampling- \n\n4- concat datasets\n\n5- augmentation\n\n6- evaluation","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T21:03:56.697113Z","iopub.execute_input":"2025-03-22T21:03:56.697341Z","iopub.status.idle":"2025-03-22T21:03:56.70316Z","shell.execute_reply.started":"2025-03-22T21:03:56.697323Z","shell.execute_reply":"2025-03-22T21:03:56.701977Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#TODO\nEyePacs + DDR \n\nevalute it Aptos ,idrid, messidor 1, 2 ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T21:40:24.941396Z","iopub.status.idle":"2025-03-22T21:40:24.941666Z","shell.execute_reply":"2025-03-22T21:40:24.94156Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# TODO FineTuning\n\n- Remove the Classifier from the (last trained models - resnet original)\n- Transfer learning over messidor 2 , DDR\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T21:41:34.159039Z","iopub.execute_input":"2025-03-22T21:41:34.159347Z","iopub.status.idle":"2025-03-22T21:41:34.163148Z","shell.execute_reply.started":"2025-03-22T21:41:34.159326Z","shell.execute_reply":"2025-03-22T21:41:34.162205Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# TODO Big Dataset Training\n\n- Download DataSets - Done -\n- Image preprocessing -  -\n- ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T23:17:37.133837Z","iopub.execute_input":"2025-03-22T23:17:37.134249Z","iopub.status.idle":"2025-03-22T23:17:37.140231Z","shell.execute_reply.started":"2025-03-22T23:17:37.134217Z","shell.execute_reply":"2025-03-22T23:17:37.139045Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}