{"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":222155719,"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-09T21:49:57.271303Z","iopub.execute_input":"2025-03-09T21:49:57.271622Z","iopub.status.idle":"2025-03-09T21:49:57.277986Z","shell.execute_reply.started":"2025-03-09T21:49:57.271601Z","shell.execute_reply":"2025-03-09T21:49:57.27734Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Global Variables\nbatch_size = 16\nimbalance_approach = True","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-09T21:49:57.577439Z","iopub.execute_input":"2025-03-09T21:49:57.577669Z","iopub.status.idle":"2025-03-09T21:49:57.580884Z","shell.execute_reply.started":"2025-03-09T21:49:57.57765Z","shell.execute_reply":"2025-03-09T21:49:57.580117Z"}},"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-09T21:49:57.743364Z","iopub.execute_input":"2025-03-09T21:49:57.743581Z","iopub.status.idle":"2025-03-09T21:49:57.746971Z","shell.execute_reply.started":"2025-03-09T21:49:57.743554Z","shell.execute_reply":"2025-03-09T21:49:57.746208Z"}},"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-09T21:49:57.969401Z","iopub.execute_input":"2025-03-09T21:49:57.969612Z","iopub.status.idle":"2025-03-09T21:49:57.973512Z","shell.execute_reply.started":"2025-03-09T21:49:57.969594Z","shell.execute_reply":"2025-03-09T21:49:57.972565Z"}},"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-09T21:49:58.244501Z","iopub.execute_input":"2025-03-09T21:49:58.24474Z","iopub.status.idle":"2025-03-09T21:49:58.24933Z","shell.execute_reply.started":"2025-03-09T21:49:58.244721Z","shell.execute_reply":"2025-03-09T21:49:58.24852Z"}},"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-09T21:50:00.364675Z","iopub.execute_input":"2025-03-09T21:50:00.364987Z","iopub.status.idle":"2025-03-09T21:50:00.603018Z","shell.execute_reply.started":"2025-03-09T21:50:00.364963Z","shell.execute_reply":"2025-03-09T21:50:00.602103Z"}},"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-09T21:50:00.623656Z","iopub.execute_input":"2025-03-09T21:50:00.623898Z","iopub.status.idle":"2025-03-09T21:50:00.627709Z","shell.execute_reply.started":"2025-03-09T21:50:00.623878Z","shell.execute_reply":"2025-03-09T21:50:00.62678Z"}},"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-09T21:56:39.040438Z","iopub.execute_input":"2025-03-09T21:56:39.040732Z","iopub.status.idle":"2025-03-09T21:56:39.045001Z","shell.execute_reply.started":"2025-03-09T21:56:39.04071Z","shell.execute_reply":"2025-03-09T21:56:39.043849Z"}},"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-09T21:56:39.400715Z","iopub.execute_input":"2025-03-09T21:56:39.40107Z","iopub.status.idle":"2025-03-09T21:56:39.407119Z","shell.execute_reply.started":"2025-03-09T21:56:39.401019Z","shell.execute_reply":"2025-03-09T21:56:39.406293Z"}},"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-09T21:56:41.581346Z","iopub.execute_input":"2025-03-09T21:56:41.581655Z","iopub.status.idle":"2025-03-09T21:56:41.587556Z","shell.execute_reply.started":"2025-03-09T21:56:41.58163Z","shell.execute_reply":"2025-03-09T21:56:41.586685Z"}},"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-09T21:56:41.86846Z","iopub.execute_input":"2025-03-09T21:56:41.868673Z","iopub.status.idle":"2025-03-09T21:56:42.20514Z","shell.execute_reply.started":"2025-03-09T21:56:41.868655Z","shell.execute_reply":"2025-03-09T21:56:42.20407Z"}},"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-09T21:59:08.924921Z","iopub.execute_input":"2025-03-09T21:59:08.925257Z","iopub.status.idle":"2025-03-09T21:59:08.936276Z","shell.execute_reply.started":"2025-03-09T21:59:08.925235Z","shell.execute_reply":"2025-03-09T21:59:08.935518Z"}},"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-09T21:59:09.714771Z","iopub.execute_input":"2025-03-09T21:59:09.715094Z","iopub.status.idle":"2025-03-09T21:59:09.720413Z","shell.execute_reply.started":"2025-03-09T21:59:09.715057Z","shell.execute_reply":"2025-03-09T21:59:09.719488Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.load_state_dict(torch.load(\"/kaggle/input/aptos-resnet-02/best_resnet_model_qwk.pth\"))\nmodel.eval()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-09T21:59:10.617383Z","iopub.execute_input":"2025-03-09T21:59:10.617688Z","iopub.status.idle":"2025-03-09T21:59:10.670591Z","shell.execute_reply.started":"2025-03-09T21:59:10.617663Z","shell.execute_reply":"2025-03-09T21:59:10.669715Z"}},"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-09T21:59:10.867139Z","iopub.execute_input":"2025-03-09T21:59:10.867388Z","iopub.status.idle":"2025-03-09T21:59:25.324548Z","shell.execute_reply.started":"2025-03-09T21:59:10.867369Z","shell.execute_reply":"2025-03-09T21:59:25.323626Z"}},"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-09T21:38:47.477973Z","iopub.execute_input":"2025-03-09T21:38:47.478293Z","iopub.status.idle":"2025-03-09T21:38:47.481829Z","shell.execute_reply.started":"2025-03-09T21:38:47.47827Z","shell.execute_reply":"2025-03-09T21:38:47.480937Z"}},"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-09T21:38:48.042Z","iopub.execute_input":"2025-03-09T21:38:48.042264Z","iopub.status.idle":"2025-03-09T21:38:48.046327Z","shell.execute_reply.started":"2025-03-09T21:38:48.042244Z","shell.execute_reply":"2025-03-09T21:38:48.045463Z"}},"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-09T21:38:48.517351Z","iopub.execute_input":"2025-03-09T21:38:48.517632Z","iopub.status.idle":"2025-03-09T21:38:48.523077Z","shell.execute_reply.started":"2025-03-09T21:38:48.517611Z","shell.execute_reply":"2025-03-09T21:38:48.522261Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Create dataset and dataloader\nbatch_size = 16\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-09T21:38:55.246557Z","iopub.execute_input":"2025-03-09T21:38:55.246841Z","iopub.status.idle":"2025-03-09T21:38:55.258931Z","shell.execute_reply.started":"2025-03-09T21:38:55.24682Z","shell.execute_reply":"2025-03-09T21:38:55.258103Z"}},"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-09T21:38:57.040222Z","iopub.execute_input":"2025-03-09T21:38:57.040502Z","iopub.status.idle":"2025-03-09T21:38:57.048919Z","shell.execute_reply.started":"2025-03-09T21:38:57.040482Z","shell.execute_reply":"2025-03-09T21:38:57.048051Z"}},"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-09T21:39:57.499805Z","iopub.execute_input":"2025-03-09T21:39:57.500146Z","iopub.status.idle":"2025-03-09T21:40:38.824641Z","shell.execute_reply.started":"2025-03-09T21:39:57.500122Z","shell.execute_reply":"2025-03-09T21:40:38.82335Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**messidor2_dataset**","metadata":{}},{"cell_type":"code","source":"# Define dataset class\nclass Messidor2Dataset(Dataset):\n    def __init__(self, root_dir, csv_file, transform=None):\n        self.root_dir = root_dir\n        self.df = pd.read_csv(csv_file)\n        self.df.columns = self.df.columns.str.strip()  # Remove leading/trailing spaces in column names\n        self.image_files = self.df[\"left\"].dropna().tolist()  # Only use 'left' column\n        self.labels = [self.extract_label(f) for f in self.image_files]\n        self.transform = transform\n\n    def extract_label(self, filename):\n        try:\n            return int(filename.split(\"_\")[-2])  # Modify if label format is different\n        except ValueError:\n            return 0  # Default label if extraction fails\n\n    def __len__(self):\n        return len(self.image_files)\n\n    def __getitem__(self, idx):\n        img_name = os.path.join(self.root_dir, self.image_files[idx])\n        image = Image.open(img_name).convert(\"RGB\")\n        label = self.labels[idx]\n\n        if self.transform:\n            image = self.transform(image)\n\n        return image, label","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-08T23:18:36.392516Z","iopub.execute_input":"2025-03-08T23:18:36.392937Z","iopub.status.idle":"2025-03-08T23:18:36.399537Z","shell.execute_reply.started":"2025-03-08T23:18:36.392904Z","shell.execute_reply":"2025-03-08T23:18:36.398533Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define image transformations\ntransform = transforms.Compose([\n    transforms.Resize((256, 256)),\n    transforms.ToTensor(),\n    transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-08T23:18:36.401269Z","iopub.execute_input":"2025-03-08T23:18:36.401531Z","iopub.status.idle":"2025-03-08T23:18:36.416391Z","shell.execute_reply.started":"2025-03-08T23:18:36.401508Z","shell.execute_reply":"2025-03-08T23:18:36.415658Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Paths\ndataset_path = \"/kaggle/input/messifor2/messidor2/IMAGES\"\ncsv_file = \"/kaggle/input/messifor2/messidor-2.csv\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-08T23:18:36.417381Z","iopub.execute_input":"2025-03-08T23:18:36.417671Z","iopub.status.idle":"2025-03-08T23:18:36.427348Z","shell.execute_reply.started":"2025-03-08T23:18:36.417636Z","shell.execute_reply":"2025-03-08T23:18:36.426616Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Create dataset and DataLoader\nbatch_size = 32\nmessidor2_dataset = Messidor2Dataset(root_dir=dataset_path, csv_file=csv_file, transform=transform)\ntest_loader = DataLoader(messidor2_dataset, batch_size=batch_size, shuffle=False, num_workers=2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-08T23:18:36.428454Z","iopub.execute_input":"2025-03-08T23:18:36.428704Z","iopub.status.idle":"2025-03-08T23:18:36.493605Z","shell.execute_reply.started":"2025-03-08T23:18:36.428684Z","shell.execute_reply":"2025-03-08T23:18:36.492366Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.eval()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-08T23:18:36.49409Z","iopub.status.idle":"2025-03-08T23:18:36.494323Z","shell.execute_reply":"2025-03-08T23:18:36.494226Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Evaluation function\ndef evaluate(model, dataloader):\n    all_preds = []\n    all_labels = []\n    device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n    model.to(device)\n\n    with torch.no_grad():\n        for images, labels in dataloader:\n            images, labels = images.to(device), labels.to(device)\n            outputs = model(images)\n            preds = torch.argmax(F.softmax(outputs, dim=1), dim=1)\n            \n            all_preds.extend(preds.cpu().numpy())\n            all_labels.extend(labels.cpu().numpy())\n\n    qwk = cohen_kappa_score(all_labels, all_preds, weights=\"quadratic\")\n    print(f\"Quadratic Weighted Kappa (QWK): {qwk:.4f}\")\n\n# Run evaluation\nevaluate(model, test_loader)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-08T23:18:36.494923Z","iopub.status.idle":"2025-03-08T23:18:36.495321Z","shell.execute_reply":"2025-03-08T23:18:36.495149Z"}},"outputs":[],"execution_count":null}]}