{"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":2812287,"sourceType":"datasetVersion","datasetId":1719146},{"sourceId":3242401,"sourceType":"datasetVersion","datasetId":1965297},{"sourceId":5203002,"sourceType":"datasetVersion","datasetId":3025918},{"sourceId":222017548,"sourceType":"kernelVersion"},{"sourceId":222155719,"sourceType":"kernelVersion"},{"sourceId":227642656,"sourceType":"kernelVersion"}],"dockerImageVersionId":30919,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"**ApTos**","metadata":{}},{"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-09T23:30:09.196352Z","iopub.execute_input":"2025-03-09T23:30:09.196671Z","iopub.status.idle":"2025-03-09T23:30:16.889287Z","shell.execute_reply.started":"2025-03-09T23:30:09.196641Z","shell.execute_reply":"2025-03-09T23:30:16.888402Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Global Variables\nbatch_size = 64\nimbalance_approach = True","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-09T23:30:16.890460Z","iopub.execute_input":"2025-03-09T23:30:16.890936Z","iopub.status.idle":"2025-03-09T23:30:16.894441Z","shell.execute_reply.started":"2025-03-09T23:30:16.890911Z","shell.execute_reply":"2025-03-09T23:30:16.893600Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nfrom sklearn.model_selection import train_test_split\n\n# # Map non-class 0 to a single binary class\n# labels_df['binary_label'] = labels_df['diagnosis'].apply(lambda x: 0 if x == '0' else 1)\n\n# # Split into Class 0 and Other Classes\n# class0_data = labels_df[labels_df['binary_label'] == 0]\n# other_data = labels_df[labels_df['binary_label'] == 1]\n\n# print(len(class0_data),len(other_data))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-09T23:30:16.896440Z","iopub.execute_input":"2025-03-09T23:30:16.897190Z","iopub.status.idle":"2025-03-09T23:30:17.072084Z","shell.execute_reply.started":"2025-03-09T23:30:16.897163Z","shell.execute_reply":"2025-03-09T23:30:17.071349Z"}},"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","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-09T23:30:17.073307Z","iopub.execute_input":"2025-03-09T23:30:17.073526Z","iopub.status.idle":"2025-03-09T23:30:17.077339Z","shell.execute_reply.started":"2025-03-09T23:30:17.073507Z","shell.execute_reply":"2025-03-09T23:30:17.076475Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\nimport torch.nn as nn","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-09T23:30:23.889278Z","iopub.execute_input":"2025-03-09T23:30:23.889624Z","iopub.status.idle":"2025-03-09T23:30:23.900839Z","shell.execute_reply.started":"2025-03-09T23:30:23.889591Z","shell.execute_reply":"2025-03-09T23:30:23.900008Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class 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        # Modify the final fully connected layer for binary classification\n        num_ftrs = self.resnet.fc.in_features\n        self.resnet.fc = nn.Linear(num_ftrs, 1)\n\n    def forward(self, x):\n        return self.resnet(x)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-09T23:30:23.901998Z","iopub.execute_input":"2025-03-09T23:30:23.902311Z","iopub.status.idle":"2025-03-09T23:30:23.910518Z","shell.execute_reply.started":"2025-03-09T23:30:23.902289Z","shell.execute_reply":"2025-03-09T23:30:23.909766Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n\nmodel = ResNetModel(pretrained=True).to(device)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-09T23:30:23.913328Z","iopub.execute_input":"2025-03-09T23:30:23.913546Z","iopub.status.idle":"2025-03-09T23:30:25.288414Z","shell.execute_reply.started":"2025-03-09T23:30:23.913510Z","shell.execute_reply":"2025-03-09T23:30:25.287756Z"}},"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-09T23:30:25.289936Z","iopub.execute_input":"2025-03-09T23:30:25.290183Z","iopub.status.idle":"2025-03-09T23:30:25.294014Z","shell.execute_reply.started":"2025-03-09T23:30:25.290162Z","shell.execute_reply":"2025-03-09T23:30:25.293177Z"}},"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-09T23:30:25.294829Z","iopub.execute_input":"2025-03-09T23:30:25.295101Z","iopub.status.idle":"2025-03-09T23:30:25.317454Z","shell.execute_reply.started":"2025-03-09T23:30:25.295068Z","shell.execute_reply":"2025-03-09T23:30:25.316757Z"}},"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-09T23:30:25.318106Z","iopub.execute_input":"2025-03-09T23:30:25.318286Z","iopub.status.idle":"2025-03-09T23:30:25.334496Z","shell.execute_reply.started":"2025-03-09T23:30:25.318270Z","shell.execute_reply":"2025-03-09T23:30:25.333653Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ============================\n# Custom Dataset for Messidor1 (Handles .tif images)\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 image paths\n        for label in range(4):  # Labels: 0, 1, 2, 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                    self.labels.append(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        if self.labels[idx] == 0:\n            label = 0\n        else:\n            label = 1\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-09T23:30:25.335378Z","iopub.execute_input":"2025-03-09T23:30:25.335680Z","iopub.status.idle":"2025-03-09T23:30:25.349231Z","shell.execute_reply.started":"2025-03-09T23:30:25.335641Z","shell.execute_reply":"2025-03-09T23:30:25.348364Z"}},"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-09T23:30:25.350085Z","iopub.execute_input":"2025-03-09T23:30:25.350313Z","iopub.status.idle":"2025-03-09T23:30:25.852985Z","shell.execute_reply.started":"2025-03-09T23:30:25.350293Z","shell.execute_reply":"2025-03-09T23:30:25.851841Z"}},"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-09T23:30:25.853784Z","iopub.execute_input":"2025-03-09T23:30:25.854190Z","iopub.status.idle":"2025-03-09T23:30:25.895314Z","shell.execute_reply.started":"2025-03-09T23:30:25.854153Z","shell.execute_reply":"2025-03-09T23:30:25.894437Z"}},"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            _, predicted = torch.max(outputs, 1)\n\n            y_true.extend(labels.cpu().numpy())\n            y_pred.extend(predicted.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    return y_true ,y_pred\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-09T23:30:25.896163Z","iopub.execute_input":"2025-03-09T23:30:25.896365Z","iopub.status.idle":"2025-03-09T23:30:25.901755Z","shell.execute_reply.started":"2025-03-09T23:30:25.896348Z","shell.execute_reply":"2025-03-09T23:30:25.900708Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.load_state_dict(torch.load(\"/kaggle/input/eyepacs-resnet/EyePacs_best_resnet_model_qwk.pth\"))\nmodel.eval()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-09T23:30:25.903046Z","iopub.execute_input":"2025-03-09T23:30:25.903335Z","iopub.status.idle":"2025-03-09T23:30:26.391415Z","shell.execute_reply.started":"2025-03-09T23:30:25.903307Z","shell.execute_reply":"2025-03-09T23:30:26.390559Z"}},"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-09T23:30:26.392290Z","iopub.execute_input":"2025-03-09T23:30:26.392540Z","iopub.status.idle":"2025-03-09T23:31:04.242010Z","shell.execute_reply.started":"2025-03-09T23:30:26.392518Z","shell.execute_reply":"2025-03-09T23:31:04.240924Z"}},"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-09T23:31:04.243098Z","iopub.execute_input":"2025-03-09T23:31:04.243443Z","iopub.status.idle":"2025-03-09T23:31:04.247404Z","shell.execute_reply.started":"2025-03-09T23:31:04.243418Z","shell.execute_reply":"2025-03-09T23:31:04.246433Z"}},"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-09T23:31:04.248345Z","iopub.execute_input":"2025-03-09T23:31:04.248585Z","iopub.status.idle":"2025-03-09T23:31:04.266475Z","shell.execute_reply.started":"2025-03-09T23:31:04.248564Z","shell.execute_reply":"2025-03-09T23:31:04.265731Z"}},"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-09T23:31:04.267282Z","iopub.execute_input":"2025-03-09T23:31:04.267553Z","iopub.status.idle":"2025-03-09T23:31:04.282803Z","shell.execute_reply.started":"2025-03-09T23:31:04.267517Z","shell.execute_reply":"2025-03-09T23:31:04.281885Z"}},"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-09T23:31:04.283695Z","iopub.execute_input":"2025-03-09T23:31:04.283993Z","iopub.status.idle":"2025-03-09T23:31:04.312548Z","shell.execute_reply.started":"2025-03-09T23:31:04.283963Z","shell.execute_reply":"2025-03-09T23:31:04.311948Z"}},"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-09T23:31:04.313369Z","iopub.execute_input":"2025-03-09T23:31:04.313626Z","iopub.status.idle":"2025-03-09T23:31:04.325708Z","shell.execute_reply.started":"2025-03-09T23:31:04.313606Z","shell.execute_reply":"2025-03-09T23:31:04.324926Z"}},"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.argmax(F.softmax(outputs, dim=1), dim=1)\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    qwk = cohen_kappa_score(all_labels, all_preds, weights=\"quadratic\")\n\n    print(f\"Accuracy: {accuracy:.4f}\")\n    print(f\"Quadratic Weighted Kappa (QWK): {qwk:.4f}\")\n    print(classification_report(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-09T23:31:04.328285Z","iopub.execute_input":"2025-03-09T23:31:04.328514Z","iopub.status.idle":"2025-03-09T23:31:45.532911Z","shell.execute_reply.started":"2025-03-09T23:31:04.328495Z","shell.execute_reply":"2025-03-09T23:31:45.531651Z"}},"outputs":[],"execution_count":null}]}