{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":14774,"databundleVersionId":875431,"sourceType":"competition"}],"dockerImageVersionId":30805,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<a id=\"1\"></a>\n# <div style=\"text-align:center; border-radius:30px 30px; padding:7px; color:white; margin:0; font-size:110%; font-family:Pacifico; background-color:#5b81d4; overflow:hidden\"><b> APTOS 2019 Blindness Detection </b></div>\n<h2><center>Detect diabetic retinopathy to stop blindness before it's too late</center></h2>\n<center><img src=\"https://raw.githubusercontent.com/dimitreOliveira/MachineLearning/master/Kaggle/APTOS%202019%20Blindness%20Detection/aux_img.png\"></center>\n\nIn this notebook, we address the urgent issue of diabetic retinopathy detection, a major cause of blindness among working-aged adults. Aravind Eye Hospital in India aims to improve healthcare access in rural areas, where technicians capture retinal images, but trained doctors are needed to manually review them. While effective, this method is time-consuming and limits scalability.\n\nOur goal is to build a machine learning model that automates the detection of diabetic retinopathy, enabling faster and more accurate diagnoses. By leveraging state-of-the-art deep learning models, we aim to improve disease detection and expand this technology's potential to identify other conditions, like glaucoma and macular degeneration.\n\nModels Utilized\nWe combine the strengths of multiple cutting-edge models for effective image classification:\n\n**ResNetV2_50**: A deep convolutional network that specializes in feature extraction, allowing it to detect subtle features in retinal images with high precision.\n\n**DeiT_base_patch16** (Vision Transformer): A transformer-based architecture that excels at capturing global dependencies within images, making it ideal for complex medical images.\n\n**FastViT_s12**: A faster version of Vision Transformers, optimized for speed without sacrificing accuracy, ensuring quick inference times in real-time diagnostic settings.\n\n**SwinV2_base_window16_256**: A Shifted Window Transformer capable of handling images at multiple scales, providing fine-grained detail for more accurate detection of retinopathy.\n","metadata":{"editable":false}},{"cell_type":"markdown","source":"<a id=\"1\"></a>\n# <div style=\"text-align:center; border-radius:30px 30px; padding:7px; color:white; margin:0; font-size:110%; font-family:Pacifico; background-color:#5b81d4; overflow:hidden\"><b> Table of Contents </b></div>","metadata":{"editable":false}},{"cell_type":"markdown","source":"- <a href=\"#libraries\">1. Importing Required Libraries</a>\n- <a href=\"#EDA\">2. EDA</a>\n- <a href=\"#Transformation\">3. Data Splitting and Transformation </a>\n- <a href=\"#Tuning\">4. Fine Tuning The Models </a>\n    - <a href=\"#1\">4.1. Train and validation function   </a> \n    - <a href=\"#2\">4.2. Fine tuning ResNetV2_50 </a> \n    - <a href=\"#3\">4.3. Fine tuning  DeiT_base_patch16 </a> \n    - <a href=\"#4\">4.4. Fine tuning FastViT_s12</a> \n    - <a href=\"#5\">4.5. Fine tuning SwinV2_base_window16_256</a> \n- <a href=\"#6\">5. Evalution</a>\n    - <a href=\"#7\">5.1. Evalution ResNetV2_50 </a> \n    - <a href=\"#8\">5.2.Evalution DeiT_base_patch16 </a> \n    - <a href=\"#9\">5.3. Evalution FastViT_s12</a> \n    - <a href=\"#10\">5.4. Evalution SwinV2_base_window16_256</a> ","metadata":{"editable":false}},{"cell_type":"markdown","source":"<a id=\"libraries\"></a>\n# <div style=\"text-align:center; border-radius:30px 30px; padding:7px; color:white; margin:0; font-size:110%; font-family:Pacifico; background-color:#5b81d4; overflow:hidden\"><b> Importing Required Libraries </b></div>","metadata":{"editable":false}},{"cell_type":"code","source":"import torch\nfrom torch.utils.data import Dataset, DataLoader\nfrom PIL import Image\nimport pandas as pd\nimport os\nimport timm\nimport random\nimport time\nfrom collections import OrderedDict\nfrom torch.cuda import amp\nimport numpy as np\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torchvision import transforms as T\nfrom torchvision.io import read_image\nimport cv2\nimport seaborn as sns\nfrom tqdm import tqdm\nprint(torch.__version__)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:52:21.26488Z","iopub.execute_input":"2024-12-09T05:52:21.26532Z","iopub.status.idle":"2024-12-09T05:52:27.931169Z","shell.execute_reply.started":"2024-12-09T05:52:21.265268Z","shell.execute_reply":"2024-12-09T05:52:27.930305Z"},"editable":false},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id=\"EDA\"></a>\n# <div style=\"text-align:center; border-radius:30px 30px; padding:7px; color:white; margin:0; font-size:110%; font-family:Pacifico; background-color:#5b81d4; overflow:hidden\"><b> EDA </b></div>","metadata":{"editable":false}},{"cell_type":"code","source":"train = pd.read_csv('../input/aptos2019-blindness-detection/train.csv')\ntest = pd.read_csv('../input/aptos2019-blindness-detection/test.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:52:31.601761Z","iopub.execute_input":"2024-12-09T05:52:31.602096Z","iopub.status.idle":"2024-12-09T05:52:31.616996Z","shell.execute_reply.started":"2024-12-09T05:52:31.602067Z","shell.execute_reply":"2024-12-09T05:52:31.616396Z"},"editable":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print('Number of train samples: ', train.shape[0])\nprint('Number of test samples: ', test.shape[0])\ndisplay(train.head())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:52:32.582759Z","iopub.execute_input":"2024-12-09T05:52:32.5831Z","iopub.status.idle":"2024-12-09T05:52:32.59829Z","shell.execute_reply.started":"2024-12-09T05:52:32.583071Z","shell.execute_reply":"2024-12-09T05:52:32.597378Z"},"editable":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:52:33.462444Z","iopub.execute_input":"2024-12-09T05:52:33.462769Z","iopub.status.idle":"2024-12-09T05:52:33.466834Z","shell.execute_reply.started":"2024-12-09T05:52:33.462741Z","shell.execute_reply":"2024-12-09T05:52:33.465901Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"f, ax = plt.subplots(figsize=(14, 8.7))\nax = sns.countplot(x=\"diagnosis\", data=train, palette=\"GnBu_d\")\nsns.despine()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:52:33.989053Z","iopub.execute_input":"2024-12-09T05:52:33.989636Z","iopub.status.idle":"2024-12-09T05:52:34.239707Z","shell.execute_reply.started":"2024-12-09T05:52:33.989604Z","shell.execute_reply":"2024-12-09T05:52:34.238829Z"},"editable":false},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Now let's see some of the images\n\nThe images have different sizes, they may need resizing or some padding.","metadata":{"editable":false}},{"cell_type":"code","source":"# Setting the style for the plot\nsns.set_style(\"white\")\n\n# Mapping class labels to their corresponding categories\nlevel_to_category = {\n    0: \"No_DR\",\n    1: \"Mild\",\n    2: \"Moderate\",\n    3: \"Severe\",\n    4: \"Proliferate_DR\"\n}\n\n# Plotting the first 15 images along with their labels\ncount = 1\nplt.figure(figsize=[20, 20])\n\nfor img_name in train['id_code'][:15]:  # Assuming 'train' contains the dataset\n    img = cv2.imread(f\"../input/aptos2019-blindness-detection/train_images/{img_name}.png\")[..., [2, 1, 0]]  # Reading the image\n    \n    # Getting the label (class) for the image\n    label = train[train['id_code'] == img_name]['diagnosis'].values[0]  # Assuming 'diagnosis' is the label column\n    \n    # Setting up the subplot with image and label\n    plt.subplot(5, 5, count)\n    plt.imshow(img)\n    plt.title(f\"Image {count}: {level_to_category[label]}\")  # Display the class label\n    count += 1\n\n# Display the plot\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:52:46.821995Z","iopub.execute_input":"2024-12-09T05:52:46.822245Z","iopub.status.idle":"2024-12-09T05:52:57.851131Z","shell.execute_reply.started":"2024-12-09T05:52:46.822221Z","shell.execute_reply":"2024-12-09T05:52:57.849715Z"},"editable":false},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id=\"Transformation\"></a>\n# <div style=\"text-align:center; border-radius:30px 30px; padding:7px; color:white; margin:0; font-size:110%; font-family:Pacifico; background-color:#5b81d4; overflow:hidden\"><b> Data Splitting and Transformation </b></div>","metadata":{"editable":false}},{"cell_type":"code","source":"DATA_DIR = \"../input/aptos2019-blindness-detection/\"\nTRAIN_DIR = \"../input/aptos2019-blindness-detection/train_images\"\nCSV_PATH = \"../input/aptos2019-blindness-detection/train.csv\"\nMODEL_PATH = \"./kaggle/working/\"\nTRAIN_SPLIT = 0.8\nLEARNING_RATE = 1e-4\nTRAIN_BATCH_SIZE = 32\nVALID_BATCH_SIZE = 32\nNUM_WORKERS = 2\nUSE_AMP = True\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:53:00.643507Z","iopub.execute_input":"2024-12-09T05:53:00.644217Z","iopub.status.idle":"2024-12-09T05:53:00.648433Z","shell.execute_reply.started":"2024-12-09T05:53:00.644182Z","shell.execute_reply":"2024-12-09T05:53:00.647612Z"},"editable":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class RetinopathyDataset(Dataset):\n    def __init__(self, image_dir, csv_file, transforms=None):\n        self.data = pd.read_csv(csv_file)\n        self.transforms = transforms\n        self.image_dir = image_dir\n\n    def __len__(self):\n        return len(self.data)\n\n    def __getitem__(self, idx):\n        # img_name = os.path.join('../input/aptos2019-blindness-detection/train_images',\n        #                         self.data.loc[idx, 'id_code'] + '.png')\n\n        img_name = os.path.join(self.image_dir, self.data.loc[idx, 'id_code'] + '.png')\n\n        tensor_image = read_image(img_name)\n        label = torch.tensor(self.data.loc[idx, 'diagnosis'], dtype=torch.long)\n\n        if self.transforms is not None:\n            tensor_image = self.transforms(tensor_image)\n\n        return (tensor_image, label)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:53:01.386648Z","iopub.execute_input":"2024-12-09T05:53:01.387022Z","iopub.status.idle":"2024-12-09T05:53:01.393138Z","shell.execute_reply.started":"2024-12-09T05:53:01.386988Z","shell.execute_reply":"2024-12-09T05:53:01.392178Z"},"editable":false},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Helper Functions and Utilities for Training and Evaluation\n\n","metadata":{"editable":false}},{"cell_type":"code","source":"@torch.no_grad()\ndef accuracy(output, target, topk=(1,)):\n    \"\"\"Computes the accuracy over the k top predictions for the specified values of k\"\"\"\n    maxk = max(topk)\n    batch_size = target.size(0)\n    _, pred = output.topk(maxk, 1, True, True)\n    pred = pred.t()\n    correct = pred.eq(target.reshape(1, -1).expand_as(pred))\n    return [correct[:k].reshape(-1).float().sum(0) * 100. / batch_size for k in topk]\n\n\ndef set_debug_apis(state: bool = False):\n    torch.autograd.profiler.profile(enabled=state)\n    torch.autograd.profiler.emit_nvtx(enabled=state)\n    torch.autograd.set_detect_anomaly(mode=state)\n\n\ndef seed_everything(seed):\n    \"\"\"\n    Makes code deterministic using a given seed.\n    Internally sets all seeds of torch, numpy and random.\n    \"\"\"\n    random.seed(seed)\n    os.environ[\"PYTHONHASHSEED\"] = str(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = True\n\n\ndef print_size_of_model(model):\n    torch.save(model.state_dict(), \"temp.p\")\n    print(\"Size (MB):\", os.path.getsize(\"temp.p\") / 1e6)\n    os.remove(\"temp.p\")\n\n\nclass AverageMeter(object):\n    \"\"\"Computes and stores the average and current value\"\"\"\n\n    def __init__(self):\n        self.reset()\n\n    def reset(self):\n        self.val = 0\n        self.avg = 0\n        self.sum = 0\n        self.count = 0\n\n    def update(self, val, n=1):\n        self.val = val\n        self.sum += val * n\n        self.count += n\n        self.avg = self.sum / self.count","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:53:03.368687Z","iopub.execute_input":"2024-12-09T05:53:03.369005Z","iopub.status.idle":"2024-12-09T05:53:03.37879Z","shell.execute_reply.started":"2024-12-09T05:53:03.368979Z","shell.execute_reply":"2024-12-09T05:53:03.377834Z"},"editable":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(torch.cuda.is_available())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:53:05.764619Z","iopub.execute_input":"2024-12-09T05:53:05.765334Z","iopub.status.idle":"2024-12-09T05:53:05.814078Z","shell.execute_reply.started":"2024-12-09T05:53:05.7653Z","shell.execute_reply":"2024-12-09T05:53:05.813203Z"},"editable":false},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id=\"Tuning\"></a>\n# <div style=\"text-align:center; border-radius:30px 30px; padding:7px; color:white; margin:0; font-size:110%; font-family:Pacifico; background-color:#5b81d4; overflow:hidden\"><b> Fine Tuning The Models </b></div>","metadata":{"editable":false}},{"cell_type":"markdown","source":"<a id=\"1\"></a>\r\n### <div style=\"text-align:center; border-radius:15px; padding:5px; color:white; margin:0; font-size:100%; font-family:Arial; background-color:#f39c12;\"><b>Training function and validations</b></div>\r\n","metadata":{"editable":false}},{"cell_type":"code","source":"def train_step(model: nn.Module, train_loader, criterion,\n               device: str, optimizer,\n               scheduler=None, num_batches: int = None,\n               log_interval: int = 100,\n               scaler=None,):\n    \"\"\"\n    Performs one step of training. Calculates loss, forward pass, computes gradient and returns metrics.\n    Args:\n        model : A pytorch CNN Model.\n        train_loader : Train loader.\n        criterion : Loss function to be optimized.\n        device : \"cuda\" or \"cpu\"\n        optimizer : Torch optimizer to train.\n        scheduler : Learning rate scheduler.\n        num_batches : (optional) Integer To limit training to certain number of batches.\n        log_interval : (optional) Defualt 100. Integer to Log after specified batch ids in every batch.\n        scaler: (optional)  Pass tor\n        \n        \n        ch.cuda.amp.GradScaler() for fp16 precision Training.\n    \"\"\"\n\n    model = model.to(device)\n    start_train_step = time.time()\n    metrics = OrderedDict()\n    model.train()\n    last_idx = len(train_loader) - 1\n    batch_time_m = AverageMeter()\n    # data_time_m = AverageMeter()\n    losses_m = AverageMeter()\n    top1_m = AverageMeter()\n    top5_m = AverageMeter()\n    cnt = 0\n    batch_start = time.time()\n    # num_updates = epoch * len(loader)\n\n    for batch_idx, (inputs, target) in enumerate(train_loader):\n        last_batch = batch_idx == last_idx\n        # data_time_m.update(time.time() - batch_start)\n        inputs = inputs.to(device)\n        target = target.to(device)\n\n        # zero the parameter gradients\n        optimizer.zero_grad()\n\n        if scaler is not None:\n            with amp.autocast():\n                output = model(inputs)\n                loss = criterion(output, target)\n                # Scale the loss using Grad Scaler\n            scaler.scale(loss).backward()\n            # Step using scaler.step()\n            scaler.step(optimizer)\n            # Update for next iteration\n            scaler.update()\n\n        else:\n            output = model(inputs)\n            loss = criterion(output, target)\n            loss.backward()\n            optimizer.step()\n\n        if scheduler is not None:\n            scheduler.step()\n\n        cnt += 1\n        acc1, acc5 = accuracy(output, target, topk=(1, 5))\n\n        top1_m.update(acc1.item(), output.size(0))\n        top5_m.update(acc5.item(), output.size(0))\n        losses_m.update(loss.item(), inputs.size(0))\n\n        batch_time_m.update(time.time() - batch_start)\n        batch_start = time.time()\n        if last_batch or batch_idx % log_interval == 0:  # If we reach the log intervel\n            print(\n                \"Batch Train Time: {batch_time.val:.3f} ({batch_time.avg:.3f})  \"\n                \"Loss: {loss.val:>7.4f} ({loss.avg:>6.4f})  \"\n                \"Top 1 Accuracy: {top1.val:>7.4f} ({top1.avg:>7.4f})  \"\n                \"Top 5 Accuracy: {top5.val:>7.4f} ({top5.avg:>7.4f})\".format(\n                    batch_time=batch_time_m, loss=losses_m, top1=top1_m, top5=top5_m))\n\n        if num_batches is not None:\n            if cnt >= num_batches:\n                end_train_step = time.time()\n                metrics[\"loss\"] = losses_m.avg\n                metrics[\"top1\"] = top1_m.avg\n                metrics[\"top5\"] = top5_m.avg\n                print(f\"Done till {num_batches} train batches\")\n                print(f\"Time taken for train step = {end_train_step - start_train_step} sec\")\n                return metrics\n\n    metrics[\"loss\"] = losses_m.avg\n    metrics[\"top1\"] = top1_m.avg\n    metrics[\"top5\"] = top5_m.avg\n    end_train_step = time.time()\n    print(f\"Time taken for train step = {end_train_step - start_train_step} sec\")\n    return metrics","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:53:09.606784Z","iopub.execute_input":"2024-12-09T05:53:09.607122Z","iopub.status.idle":"2024-12-09T05:53:09.618591Z","shell.execute_reply.started":"2024-12-09T05:53:09.607093Z","shell.execute_reply":"2024-12-09T05:53:09.617683Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"@torch.no_grad()\ndef val_step(model: nn.Module, val_loader, criterion,\n             device: str, num_batches=None,\n             log_interval: int = 100):\n\n    \"\"\"\n    Performs one step of validation. Calculates loss, forward pass and returns metrics.\n    Args:\n        model : A pytorch CNN Model.\n        val_loader : Validation loader.\n        criterion : Loss function to be optimized.\n        device : \"cuda\" or \"cpu\"\n        num_batches : (optional) Integer To limit validation to certain number of batches.\n        log_interval : (optional) Defualt 100. Integer to Log after specified batch ids in every batch.\n    \"\"\"\n\n    model = model.to(device)\n    start_val_step = time.time()\n    last_idx = len(val_loader) - 1\n    batch_time_m = AverageMeter()\n    # data_time_m = AverageMeter()\n    losses_m = AverageMeter()\n    top1_m = AverageMeter()\n    top5_m = AverageMeter()\n    cnt = 0\n    model.eval()\n    batch_start = time.time()\n    metrics = OrderedDict()\n\n    for batch_idx, (inputs, target) in enumerate(val_loader):\n        last_batch = batch_idx == last_idx\n        inputs = inputs.to(device)\n        target = target.to(device)\n\n        output = model(inputs)\n        loss = criterion(output, target)\n        cnt += 1\n        acc1, acc5 = accuracy(output, target, topk=(1, 5))\n        reduced_loss = loss.data\n\n        losses_m.update(reduced_loss.item(), inputs.size(0))\n        top1_m.update(acc1.item(), output.size(0))\n        top5_m.update(acc5.item(), output.size(0))\n        batch_time_m.update(time.time() - batch_start)\n\n        batch_start = time.time()\n\n        if (last_batch or batch_idx % log_interval == 0):  # If we reach the log intervel\n            print(\n                \"Batch Inference Time: {batch_time.val:.3f} ({batch_time.avg:.3f})  \"\n                \"Loss: {loss.val:>7.4f} ({loss.avg:>6.4f})  \"\n                \"Top 1 Accuracy: {top1.val:>7.4f} ({top1.avg:>7.4f})  \"\n                \"Top 5 Accuracy: {top5.val:>7.4f} ({top5.avg:>7.4f})\".format(\n                    batch_time=batch_time_m, loss=losses_m, top1=top1_m, top5=top5_m))\n\n        if num_batches is not None:\n            if cnt >= num_batches:\n                end_val_step = time.time()\n                metrics[\"loss\"] = losses_m.avg\n                metrics[\"top1\"] = top1_m.avg\n                metrics[\"top5\"] = top5_m.avg\n                print(f\"Done till {num_batches} validation batches\")\n                print(f\"Time taken for validation step = {end_val_step - start_val_step} sec\")\n                return metrics\n\n    metrics[\"loss\"] = losses_m.avg\n    metrics[\"top1\"] = top1_m.avg\n    metrics[\"top5\"] = top5_m.avg\n    print(\"Finished the validation epoch\")\n\n    end_val_step = time.time()\n    print(f\"Time taken for validation step = {end_val_step - start_val_step} sec\")\n    return metrics","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:53:11.901905Z","iopub.execute_input":"2024-12-09T05:53:11.902521Z","iopub.status.idle":"2024-12-09T05:53:11.911892Z","shell.execute_reply.started":"2024-12-09T05:53:11.902475Z","shell.execute_reply":"2024-12-09T05:53:11.911026Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id=\"2\"></a>\n### <div style=\"text-align:center; border-radius:15px; padding:5px; color:white; margin:0; font-size:100%; font-family:Arial; background-color:#f39c12;\"><b> Fine tuning ResNetV2_50 </b></div>\n","metadata":{"editable":false}},{"cell_type":"code","source":"EPOCHS = 10\nIMG_WIDTH= 224\nIMG_HEIGHT = 224\nMODEL_NAME= \"resnetv2_50\"\nMODEL_SAVE = \"/kaggle/working/ResnetMetrics.Csv\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:53:14.406378Z","iopub.execute_input":"2024-12-09T05:53:14.407011Z","iopub.status.idle":"2024-12-09T05:53:14.410815Z","shell.execute_reply.started":"2024-12-09T05:53:14.40698Z","shell.execute_reply":"2024-12-09T05:53:14.409894Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"seed_everything(42)\nset_debug_apis(False)\n\ntrain_trasforms_ResNetV2_50 = T.Compose([\n    T.ConvertImageDtype(torch.float32),\n    T.Resize((IMG_WIDTH, IMG_HEIGHT)),\n    T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])\n])\n\nfull_dataset = RetinopathyDataset(TRAIN_DIR, CSV_PATH, transforms=train_trasforms_ResNetV2_50)\n\ntrain_size = int(TRAIN_SPLIT * len(full_dataset))\ntest_size = len(full_dataset) - train_size\n\ntrain_dataset, val_dataset = torch.utils.data.random_split(full_dataset, [train_size, test_size])\n\ntrain_loader = DataLoader(full_dataset, batch_size=TRAIN_BATCH_SIZE,\n                          shuffle=False, num_workers=NUM_WORKERS, drop_last=True, pin_memory=False)\n\nval_loader = DataLoader(val_dataset, batch_size=VALID_BATCH_SIZE, shuffle=False,\n                        num_workers=NUM_WORKERS, drop_last=True, pin_memory=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:53:17.368825Z","iopub.execute_input":"2024-12-09T05:53:17.369645Z","iopub.status.idle":"2024-12-09T05:53:17.4093Z","shell.execute_reply.started":"2024-12-09T05:53:17.36961Z","shell.execute_reply":"2024-12-09T05:53:17.408663Z"},"editable":false,"jupyter":{"source_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model= timm.create_model(MODEL_NAME, pretrained=True, num_classes=5)\n\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.AdamW(model.parameters(), lr=LEARNING_RATE)\n\nif torch.cuda.is_available():\n    device = \"cuda\"\nelse:\n    device = \"cpu\"\n\nif USE_AMP:\n    from torch.cuda import amp\n    scaler = amp.GradScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:53:21.877648Z","iopub.execute_input":"2024-12-09T05:53:21.878329Z","iopub.status.idle":"2024-12-09T05:53:23.214828Z","shell.execute_reply.started":"2024-12-09T05:53:21.878297Z","shell.execute_reply":"2024-12-09T05:53:23.213961Z"},"editable":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport time\nfrom tqdm import tqdm\n\n# Initialize lists to store metrics\ntrain_loss = []\ntrain_top1_acc = []\nval_loss = []\nval_top1_acc = []\n\n# Record start time\nstart_time = time.time()\n\nfor epoch in tqdm(range(EPOCHS)):\n    train_metrics = train_step(model, train_loader, criterion, device, optimizer, scaler=scaler)\n    train_loss.append(train_metrics[\"loss\"])\n    print(f\"Training loss = {train_metrics['loss']}\")\n    train_top1_acc.append(train_metrics[\"top1\"])\n\n    val_metrics = val_step(model,val_loader, criterion, device)\n    val_loss.append(val_metrics[\"loss\"])\n    print(f\"Validation loss = {val_metrics['loss']}\")\n    val_top1_acc.append(val_metrics[\"top1\"])\n    \n    # Save model checkpoint\n    checkpoint_path = f\"{MODEL_NAME}_{epoch}.pt\"\n    torch.save(model.state_dict(), checkpoint_path)\n\n# Record end time\nend_time = time.time()\n\n# Calculate total training time\ntotal_time = end_time - start_time\nprint(f\"Total training time: {total_time:.2f} seconds\")\n\n# Create a DataFrame to store the metrics\nmetrics_df = pd.DataFrame({\n    \"epoch\": range(1, EPOCHS + 1),\n    \"train_loss\": train_loss,\n    \"train_top1_acc\": train_top1_acc,\n    \"val_loss\": val_loss,\n    \"val_top1_acc\": val_top1_acc,\n})\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:53:23.364731Z","iopub.execute_input":"2024-12-09T05:53:23.365322Z","iopub.status.idle":"2024-12-09T06:59:08.906915Z","shell.execute_reply.started":"2024-12-09T05:53:23.365286Z","shell.execute_reply":"2024-12-09T06:59:08.905704Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Save the DataFrame to CSV\nmetrics_df.to_csv(MODEL_SAVE, index=False)\nprint(f\"Metrics saved to {MODEL_SAVE}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T07:07:03.237424Z","iopub.execute_input":"2024-12-09T07:07:03.237777Z","iopub.status.idle":"2024-12-09T07:07:03.24739Z","shell.execute_reply.started":"2024-12-09T07:07:03.237746Z","shell.execute_reply":"2024-12-09T07:07:03.246428Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id=\"7\"></a>\n### <div style=\"text-align:center; border-radius:15px; padding:5px; color:white; margin:0; font-size:100%; font-family:Arial; background-color:#f39c12;\"><b>  Evalution  ResNetV2_50 </b></div>\n","metadata":{"editable":false}},{"cell_type":"code","source":"metrics_path = f\"{MODEL_NAME}_metrics.csv\"  # Path to metrics CSV\nmetrics_df = pd.read_csv(MODEL_SAVE)\n\n# Set Seaborn style for better aesthetics\nsns.set_theme(style=\"whitegrid\")\n\n# Create a figure for visualizations\nplt.figure(figsize=(12, 8))\n\n# Plot training and validation loss\nplt.subplot(2, 1, 1)\nsns.lineplot(x='epoch', y='train_loss', data=metrics_df, label='Train Loss', color='blue', marker=\"o\")\nsns.lineplot(x='epoch', y='val_loss', data=metrics_df, label='Validation Loss', color='orange', marker=\"o\")\nplt.title(\"Training and Validation Loss per Epoch\", fontsize=14, fontweight='bold')\nplt.xlabel(\"Epoch\", fontsize=12)\nplt.ylabel(\"Loss\", fontsize=12)\nplt.legend()\nplt.grid(alpha=0.3)\n\n# Plot training and validation accuracy\nplt.subplot(2, 1, 2)\nsns.lineplot(x='epoch', y='train_top1_acc', data=metrics_df, label='Train Accuracy', color='green', marker=\"o\")\nsns.lineplot(x='epoch', y='val_top1_acc', data=metrics_df, label='Validation Accuracy', color='red', marker=\"o\")\nplt.title(\"Training and Validation Accuracy per Epoch\", fontsize=14, fontweight='bold')\nplt.xlabel(\"Epoch\", fontsize=12)\nplt.ylabel(\"Accuracy\", fontsize=12)\nplt.legend()\nplt.grid(alpha=0.3)\n\n# Adjust spacing between plots\nplt.tight_layout()\n\n# Save the visualization as a file (optional)\noutput_plot_path = f\"{MODEL_NAME}_training_visualization.png\"\nplt.savefig(output_plot_path, dpi=300)\nprint(f\"Visualization saved to {output_plot_path}\")\n\n# Show the plots\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T07:07:07.303067Z","iopub.execute_input":"2024-12-09T07:07:07.303882Z","iopub.status.idle":"2024-12-09T07:07:08.998677Z","shell.execute_reply.started":"2024-12-09T07:07:07.303836Z","shell.execute_reply":"2024-12-09T07:07:08.997798Z"},"editable":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load your model from checkpoint\ndef load_model_from_checkpoint(checkpoint_path, model_class, device):\n    model.load_state_dict(torch.load(checkpoint_path, map_location=device))\n    model.to(device)\n    model.eval()  # Set to evaluation mode\n    return model\n","metadata":{"trusted":true,"editable":false,"execution":{"iopub.status.busy":"2024-12-09T07:07:09.03947Z","iopub.execute_input":"2024-12-09T07:07:09.039929Z","iopub.status.idle":"2024-12-09T07:07:09.044097Z","shell.execute_reply.started":"2024-12-09T07:07:09.039901Z","shell.execute_reply":"2024-12-09T07:07:09.043215Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Predict on validation data\ndef predict_on_validation(model, dataloader, device):\n    model.eval()  # Set the model to evaluation mode\n    y_true = []\n    y_pred = []\n\n    with torch.no_grad():  # No need to compute gradients for validation\n        for inputs, labels in val_loader:  # Assuming dataloader is a dictionary with 'val' key\n            inputs = inputs.to(device)\n            labels = labels.to(device)\n            outputs = model(inputs)\n            _, preds = torch.max(outputs, 1)\n\n            # Collect true and predicted labels\n            y_true.extend(labels.cpu().numpy())\n            y_pred.extend(preds.cpu().numpy())\n\n    return np.array(y_true), np.array(y_pred)","metadata":{"trusted":true,"editable":false,"execution":{"iopub.status.busy":"2024-12-09T07:07:12.231529Z","iopub.execute_input":"2024-12-09T07:07:12.232205Z","iopub.status.idle":"2024-12-09T07:07:12.237532Z","shell.execute_reply.started":"2024-12-09T07:07:12.232174Z","shell.execute_reply":"2024-12-09T07:07:12.236743Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define paths and device\ncheckpoint_path = \"/kaggle/working/resnetv2_50_9.pt\"\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel_class=5\n# Initialize and load the model (replace YourModel with your actual model class)\nmodel = load_model_from_checkpoint(checkpoint_path, model_class, device)\n\n# Assuming `dataloaders` is a dictionary with 'val' key containing the validation dataloader\n# Predict on validation set\ny_true, y_pred = predict_on_validation(model, val_loader, device)\n","metadata":{"trusted":true,"editable":false,"execution":{"iopub.status.busy":"2024-12-09T07:07:15.144321Z","iopub.execute_input":"2024-12-09T07:07:15.144704Z","iopub.status.idle":"2024-12-09T07:08:20.247941Z","shell.execute_reply.started":"2024-12-09T07:07:15.144674Z","shell.execute_reply":"2024-12-09T07:08:20.24679Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T07:08:20.250035Z","iopub.execute_input":"2024-12-09T07:08:20.250389Z","iopub.status.idle":"2024-12-09T07:08:20.391911Z","shell.execute_reply.started":"2024-12-09T07:08:20.250314Z","shell.execute_reply":"2024-12-09T07:08:20.391264Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Compute confusion matrix\ncm = confusion_matrix(y_true, y_pred)\n\n# Plot confusion matrix using seaborn\nplt.figure(figsize=(8, 6))\nsns.heatmap(cm, annot=True, fmt='d', cmap='Blues', \n            xticklabels=np.arange(cm.shape[0]), yticklabels=np.arange(cm.shape[0]))\nplt.xlabel('Predicted')\nplt.ylabel('True')\nplt.title('Confusion Matrix')\nplt.show()\n","metadata":{"trusted":true,"editable":false,"execution":{"iopub.status.busy":"2024-12-09T07:08:20.392805Z","iopub.execute_input":"2024-12-09T07:08:20.393038Z","iopub.status.idle":"2024-12-09T07:08:20.742111Z","shell.execute_reply.started":"2024-12-09T07:08:20.393014Z","shell.execute_reply":"2024-12-09T07:08:20.741284Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Calculate errors per class (misclassifications)\nclass_errors = {}\nnum_classes = cm.shape[0]\nfor i in range(num_classes):\n    total = np.sum(cm[i, :])  # Total number of instances of class i\n    incorrect = total - cm[i, i]  # Misclassified instances of class i\n    error_rate = incorrect / total if total != 0 else 0\n    class_errors[i] = error_rate\n\n# Print class errors\nfor class_id, error_rate in class_errors.items():\n    print(f\"Class {class_id}: Error Rate = {error_rate:.4f}\")\n","metadata":{"trusted":true,"editable":false,"execution":{"iopub.status.busy":"2024-12-09T07:08:20.74379Z","iopub.execute_input":"2024-12-09T07:08:20.744071Z","iopub.status.idle":"2024-12-09T07:08:20.749763Z","shell.execute_reply.started":"2024-12-09T07:08:20.744044Z","shell.execute_reply":"2024-12-09T07:08:20.74877Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Create a DataFrame for the error rates\nerror_data = pd.DataFrame(list(class_errors.items()), columns=['Class', 'Error Rate'])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T07:09:20.75429Z","iopub.execute_input":"2024-12-09T07:09:20.754673Z","iopub.status.idle":"2024-12-09T07:09:20.75975Z","shell.execute_reply.started":"2024-12-09T07:09:20.754642Z","shell.execute_reply":"2024-12-09T07:09:20.758754Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plot only the table, fitting the entire figure\nfig, ax = plt.subplots(figsize=(8, 4))  # Adjust size as needed\nax.axis('off')  # Turn off axis for the table\n\n# Create and display the table, making it fill the figure\ntable = ax.table(cellText=error_data.values, colLabels=error_data.columns, cellLoc='center', loc='center')\ntable.auto_set_font_size(False)\ntable.set_fontsize(12)\ntable.auto_set_column_width(col=list(range(len(error_data.columns))))\ntable.scale(20, 5)  # Scale table size (adjust the values as needed)\n\nplt.tight_layout()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T07:09:34.13918Z","iopub.execute_input":"2024-12-09T07:09:34.13985Z","iopub.status.idle":"2024-12-09T07:09:34.503013Z","shell.execute_reply.started":"2024-12-09T07:09:34.139815Z","shell.execute_reply":"2024-12-09T07:09:34.501661Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true,"editable":false},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id=\"3\"></a>\n### <div style=\"text-align:center; border-radius:15px; padding:5px; color:white; margin:0; font-size:100%; font-family:Arial; background-color:#f39c12;\"><b> Fine Tuning  DeiT_base_patch16 </b></div>\n","metadata":{"editable":false}},{"cell_type":"code","source":"IMG_WIDTH= 224\nIMG_HEIGHT= 224\nMODEL_NAME=\"deit_base_patch16_224\"\nMODEL_SAVE=  \"/kaggle/working/deitMetrics.Csv\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:37:51.775049Z","iopub.execute_input":"2024-12-09T05:37:51.775374Z","iopub.status.idle":"2024-12-09T05:37:51.780616Z","shell.execute_reply.started":"2024-12-09T05:37:51.775338Z","shell.execute_reply":"2024-12-09T05:37:51.779682Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"seed_everything(42)\nset_debug_apis(False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:37:53.734954Z","iopub.execute_input":"2024-12-09T05:37:53.735296Z","iopub.status.idle":"2024-12-09T05:37:53.740898Z","shell.execute_reply.started":"2024-12-09T05:37:53.735267Z","shell.execute_reply":"2024-12-09T05:37:53.740043Z"},"editable":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_trasforms_DeiT_base_patch16= T.Compose([\n    T.ConvertImageDtype(torch.float32),\n    T.Resize((IMG_WIDTH, IMG_HEIGHT)),\n    T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])\n])\n\nfull_dataset = RetinopathyDataset(TRAIN_DIR, CSV_PATH, transforms=train_trasforms_DeiT_base_patch16)\n\ntrain_size = int(TRAIN_SPLIT * len(full_dataset))\ntest_size = len(full_dataset) - train_size\n\ntrain_dataset, val_dataset = torch.utils.data.random_split(full_dataset, [train_size, test_size])\n\ntrain_loader = DataLoader(full_dataset, batch_size=TRAIN_BATCH_SIZE,\n                          shuffle=False, num_workers=NUM_WORKERS, drop_last=True, pin_memory=False)\n\nval_loader = DataLoader(val_dataset, batch_size=VALID_BATCH_SIZE, shuffle=False,\n                        num_workers=NUM_WORKERS, drop_last=True, pin_memory=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:37:55.496952Z","iopub.execute_input":"2024-12-09T05:37:55.49731Z","iopub.status.idle":"2024-12-09T05:37:55.508955Z","shell.execute_reply.started":"2024-12-09T05:37:55.497281Z","shell.execute_reply":"2024-12-09T05:37:55.508007Z"},"editable":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model= timm.create_model(MODEL_NAME, pretrained=True, num_classes=5)\n\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.AdamW(model.parameters(), lr=LEARNING_RATE)\n\nif torch.cuda.is_available():\n    device = \"cuda\"\nelse:\n    device = \"cpu\"\n\nif USE_AMP:\n    from torch.cuda import amp\n    scaler = amp.GradScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:37:57.667018Z","iopub.execute_input":"2024-12-09T05:37:57.667861Z","iopub.status.idle":"2024-12-09T05:37:59.375434Z","shell.execute_reply.started":"2024-12-09T05:37:57.667826Z","shell.execute_reply":"2024-12-09T05:37:59.374484Z"},"editable":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Initialize lists to store metrics\ntrain_loss = []\ntrain_top1_acc = []\nval_loss = []\nval_top1_acc = []\n\n# Record start time\nstart_time = time.time()\n\nfor epoch in tqdm(range(EPOCHS)):\n    train_metrics = train_step(model, train_loader, criterion, device, optimizer, scaler=scaler)\n    train_loss.append(train_metrics[\"loss\"])\n    print(f\"Training loss = {train_metrics['loss']}\")\n    train_top1_acc.append(train_metrics[\"top1\"])\n\n    val_metrics = val_step(model, val_loader, criterion, device)\n    val_loss.append(val_metrics[\"loss\"])\n    print(f\"Validation loss = {val_metrics['loss']}\")\n    val_top1_acc.append(val_metrics[\"top1\"])\n    \n    # Save model checkpoint\n    checkpoint_path = f\"{MODEL_NAME}_{epoch}.pt\"\n    torch.save(model.state_dict(), checkpoint_path)\n\n# Record end time\nend_time = time.time()\n\n# Calculate total training time\ntotal_time = end_time - start_time\nprint(f\"Total training time: {total_time:.2f} seconds\")\n\n# Create a DataFrame to store the metrics\nmetrics_df = pd.DataFrame({\n    \"epoch\": range(1, EPOCHS + 1),\n    \"train_loss\": train_loss,\n    \"train_top1_acc\": train_top1_acc,\n    \"val_loss\": val_loss,\n    \"val_top1_acc\": val_top1_acc,\n})\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T05:38:00.766679Z","iopub.execute_input":"2024-12-09T05:38:00.767064Z","iopub.status.idle":"2024-12-09T05:51:43.472077Z","shell.execute_reply.started":"2024-12-09T05:38:00.767033Z","shell.execute_reply":"2024-12-09T05:51:43.470809Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Save the DataFrame to CSV\nmetrics_df.to_csv(MODEL_SAVE, index=False)\nprint(f\"Metrics saved to {MODEL_SAVE}\")\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id=\"8\"></a>\n### <div style=\"text-align:center; border-radius:15px; padding:5px; color:white; margin:0; font-size:100%; font-family:Arial; background-color:#f39c12;\"><b>  Evalution  DeiT_base_patch16 </b></div>\n","metadata":{"editable":false}},{"cell_type":"code","source":"metrics_path = f\"{MODEL_NAME}_metrics.csv\"  # Path to metrics CSV\nmetrics_df = pd.read_csv(MODEL_SAVE)\n\n# Set Seaborn style for better aesthetics\nsns.set_theme(style=\"whitegrid\")\n\n# Create a figure for visualizations\nplt.figure(figsize=(12, 8))\n\n# Plot training and validation loss\nplt.subplot(2, 1, 1)\nsns.lineplot(x='epoch', y='train_loss', data=metrics_df, label='Train Loss', color='blue', marker=\"o\")\nsns.lineplot(x='epoch', y='val_loss', data=metrics_df, label='Validation Loss', color='orange', marker=\"o\")\nplt.title(\"Training and Validation Loss per Epoch\", fontsize=14, fontweight='bold')\nplt.xlabel(\"Epoch\", fontsize=12)\nplt.ylabel(\"Loss\", fontsize=12)\nplt.legend()\nplt.grid(alpha=0.3)\n\n# Plot training and validation accuracy\nplt.subplot(2, 1, 2)\nsns.lineplot(x='epoch', y='train_top1_acc', data=metrics_df, label='Train Accuracy', color='green', marker=\"o\")\nsns.lineplot(x='epoch', y='val_top1_acc', data=metrics_df, label='Validation Accuracy', color='red', marker=\"o\")\nplt.title(\"Training and Validation Accuracy per Epoch\", fontsize=14, fontweight='bold')\nplt.xlabel(\"Epoch\", fontsize=12)\nplt.ylabel(\"Accuracy\", fontsize=12)\nplt.legend()\nplt.grid(alpha=0.3)\n\n# Adjust spacing between plots\nplt.tight_layout()\n\n# Save the visualization as a file (optional)\noutput_plot_path = f\"{MODEL_NAME}_training_visualization.png\"\nplt.savefig(output_plot_path, dpi=300)\nprint(f\"Visualization saved to {output_plot_path}\")\n\n# Show the plots\nplt.show()","metadata":{"trusted":true,"editable":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load your model from checkpoint\ndef load_model_from_checkpoint(checkpoint_path, model_class, device):\n    model.load_state_dict(torch.load(checkpoint_path, map_location=device))\n    model.to(device)\n    model.eval()  # Set to evaluation mode\n    return model\n","metadata":{"trusted":true,"editable":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Predict on validation data\ndef predict_on_validation(model, dataloader, device):\n    model.eval()  # Set the model to evaluation mode\n    y_true = []\n    y_pred = []\n\n    with torch.no_grad():  # No need to compute gradients for validation\n        for inputs, labels in val_loader:  # Assuming dataloader is a dictionary with 'val' key\n            inputs = inputs.to(device)\n            labels = labels.to(device)\n            outputs = model(inputs)\n            _, preds = torch.max(outputs, 1)\n\n            # Collect true and predicted labels\n            y_true.extend(labels.cpu().numpy())\n            y_pred.extend(preds.cpu().numpy())\n\n    return np.array(y_true), np.array(y_pred)","metadata":{"trusted":true,"editable":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define paths and device\ncheckpoint_path = \"/kaggle/working/deit_base_patch16_224_9.pt\"\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel_class=5\n# Initialize and load the model (replace YourModel with your actual model class)\nmodel = load_model_from_checkpoint(checkpoint_path, model_class, device)\n\n# Assuming `dataloaders` is a dictionary with 'val' key containing the validation dataloader\n# Predict on validation set\ny_true, y_pred = predict_on_validation(model, val_loader, device)\n","metadata":{"trusted":true,"editable":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Compute confusion matrix\ncm = confusion_matrix(y_true, y_pred)\n\n# Plot confusion matrix using seaborn\nplt.figure(figsize=(8, 6))\nsns.heatmap(cm, annot=True, fmt='d', cmap='Blues', \n            xticklabels=np.arange(cm.shape[0]), yticklabels=np.arange(cm.shape[0]))\nplt.xlabel('Predicted')\nplt.ylabel('True')\nplt.title('Confusion Matrix')\nplt.show()\n","metadata":{"trusted":true,"editable":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Calculate errors per class (misclassifications)\nclass_errors = {}\nnum_classes = cm.shape[0]\nfor i in range(num_classes):\n    total = np.sum(cm[i, :])  # Total number of instances of class i\n    incorrect = total - cm[i, i]  # Misclassified instances of class i\n    error_rate = incorrect / total if total != 0 else 0\n    class_errors[i] = error_rate\n\n# Print class errors\nfor class_id, error_rate in class_errors.items():\n    print(f\"Class {class_id}: Error Rate = {error_rate:.4f}\")\n","metadata":{"trusted":true,"editable":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plot only the table, fitting the entire figure\nfig, ax = plt.subplots(figsize=(8, 4))  # Adjust size as needed\nax.axis('off')  # Turn off axis for the table\n\n# Create and display the table, making it fill the figure\ntable = ax.table(cellText=error_data.values, colLabels=error_data.columns, cellLoc='center', loc='center')\ntable.auto_set_font_size(False)\ntable.set_fontsize(12)\ntable.auto_set_column_width(col=list(range(len(error_data.columns))))\ntable.scale(20, 5)  # Scale table size (adjust the values as needed)\n\nplt.tight_layout()\nplt.show()\n","metadata":{"trusted":true,"editable":false},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id=\"4\"></a>\n### <div style=\"text-align:center; border-radius:15px; padding:5px; color:white; margin:0; font-size:100%; font-family:Arial; background-color:#f39c12;\"><b> Fine Tuning  FastViT_s12 </b></div>\n","metadata":{"editable":false}},{"cell_type":"code","source":"IMG_WIDTH= 256\nIMG_HEIGHT = 256\nMODEL_NAME = \"fastvit_s12\"\nMODEL_SAVE = MODEL_PATH + MODEL_NAME","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T18:36:48.743914Z","iopub.execute_input":"2024-12-08T18:36:48.744904Z","iopub.status.idle":"2024-12-08T18:36:48.749366Z","shell.execute_reply.started":"2024-12-08T18:36:48.74485Z","shell.execute_reply":"2024-12-08T18:36:48.748513Z"},"editable":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"seed_everything(42)\nset_debug_apis(False)\n\ntrain_trasforms_ResNetV2_50 = T.Compose([\n    T.ConvertImageDtype(torch.float32),\n    T.Resize((IMG_WIDTH, IMG_HEIGHT)),\n    T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])\n])\nfull_dataset = RetinopathyDataset(TRAIN_DIR, CSV_PATH, transforms=train_trasforms_ResNetV2_50)\n\ntrain_size = int(TRAIN_SPLIT * len(full_dataset))\ntest_size = len(full_dataset) - train_size\n\ntrain_dataset, val_dataset = torch.utils.data.random_split(full_dataset, [train_size, test_size])\n\ntrain_loader = DataLoader(full_dataset, batch_size=TRAIN_BATCH_SIZE,\n                          shuffle=False, num_workers=NUM_WORKERS, drop_last=True, pin_memory=False)\n\nval_loader = DataLoader(val_dataset, batch_size=VALID_BATCH_SIZE, shuffle=False,\n                        num_workers=NUM_WORKERS, drop_last=True, pin_memory=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T18:36:49.329279Z","iopub.execute_input":"2024-12-08T18:36:49.329636Z","iopub.status.idle":"2024-12-08T18:36:49.342544Z","shell.execute_reply.started":"2024-12-08T18:36:49.329604Z","shell.execute_reply":"2024-12-08T18:36:49.341625Z"},"editable":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model= timm.create_model(MODEL_NAME, pretrained=True, num_classes=5)\n\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.AdamW(model.parameters(), lr=LEARNING_RATE)\n\nif torch.cuda.is_available():\n    device = \"cuda\"\nelse:\n    device = \"cpu\"\n\nif USE_AMP:\n    from torch.cuda import amp\n    scaler = amp.GradScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T18:36:49.847909Z","iopub.execute_input":"2024-12-08T18:36:49.849008Z","iopub.status.idle":"2024-12-08T18:36:50.864302Z","shell.execute_reply.started":"2024-12-08T18:36:49.848957Z","shell.execute_reply":"2024-12-08T18:36:50.863367Z"},"editable":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Initialize lists to store metrics\ntrain_loss = []\ntrain_top1_acc = []\nval_loss = []\nval_top1_acc = []\n\n# Record start time\nstart_time = time.time()\n\nfor epoch in tqdm(range(EPOCHS)):\n    train_metrics = train_step(model, train_loader, criterion, device, optimizer, scaler=scaler)\n    train_loss.append(train_metrics[\"loss\"])\n    print(f\"Training loss = {train_metrics['loss']}\")\n    train_top1_acc.append(train_metrics[\"top1\"])\n\n    val_metrics = val_step(model_mobilenetv3, val_loader, criterion, device)\n    val_loss.append(val_metrics[\"loss\"])\n    print(f\"Validation loss = {val_metrics['loss']}\")\n    val_top1_acc.append(val_metrics[\"top1\"])\n    \n    # Save model checkpoint\n    checkpoint_path = f\"{MODEL_NAME}_{epoch}.pt\"\n    torch.save(model.state_dict(), checkpoint_path)\n\n# Record end time\nend_time = time.time()\n\n# Calculate total training time\ntotal_time = end_time - start_time\nprint(f\"Total training time: {total_time:.2f} seconds\")\n\n# Create a DataFrame to store the metrics\nmetrics_df = pd.DataFrame({\n    \"epoch\": range(1, EPOCHS + 1),\n    \"train_loss\": train_loss,\n    \"train_top1_acc\": train_top1_acc,\n    \"val_loss\": val_loss,\n    \"val_top1_acc\": val_top1_acc,\n})\n\n# Save the DataFrame to CSV\nmetrics_df.to_csv(MODEL_SAVE, index=False)\nprint(f\"Metrics saved to {metrics_path}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T18:36:53.366959Z","iopub.execute_input":"2024-12-08T18:36:53.367591Z","iopub.status.idle":"2024-12-08T18:37:02.463859Z","shell.execute_reply.started":"2024-12-08T18:36:53.367555Z","shell.execute_reply":"2024-12-08T18:37:02.462485Z"},"editable":false},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id=\"9\"></a>\n### <div style=\"text-align:center; border-radius:15px; padding:5px; color:white; margin:0; font-size:100%; font-family:Arial; background-color:#f39c12;\"><b>  Evalution  FastViT_s12 </b></div>\n","metadata":{"editable":false}},{"cell_type":"code","source":"metrics_path = f\"{MODEL_NAME}_metrics.csv\"  # Path to metrics CSV\nmetrics_df = pd.read_csv(MODEL_SAVE)\n\n# Set Seaborn style for better aesthetics\nsns.set_theme(style=\"whitegrid\")\n\n# Create a figure for visualizations\nplt.figure(figsize=(12, 8))\n\n# Plot training and validation loss\nplt.subplot(2, 1, 1)\nsns.lineplot(x='epoch', y='train_loss', data=metrics_df, label='Train Loss', color='blue', marker=\"o\")\nsns.lineplot(x='epoch', y='val_loss', data=metrics_df, label='Validation Loss', color='orange', marker=\"o\")\nplt.title(\"Training and Validation Loss per Epoch\", fontsize=14, fontweight='bold')\nplt.xlabel(\"Epoch\", fontsize=12)\nplt.ylabel(\"Loss\", fontsize=12)\nplt.legend()\nplt.grid(alpha=0.3)\n\n# Plot training and validation accuracy\nplt.subplot(2, 1, 2)\nsns.lineplot(x='epoch', y='train_top1_acc', data=metrics_df, label='Train Accuracy', color='green', marker=\"o\")\nsns.lineplot(x='epoch', y='val_top1_acc', data=metrics_df, label='Validation Accuracy', color='red', marker=\"o\")\nplt.title(\"Training and Validation Accuracy per Epoch\", fontsize=14, fontweight='bold')\nplt.xlabel(\"Epoch\", fontsize=12)\nplt.ylabel(\"Accuracy\", fontsize=12)\nplt.legend()\nplt.grid(alpha=0.3)\n\n# Adjust spacing between plots\nplt.tight_layout()\n\n# Save the visualization as a file (optional)\noutput_plot_path = f\"{MODEL_NAME}_training_visualization.png\"\nplt.savefig(output_plot_path, dpi=300)\nprint(f\"Visualization saved to {output_plot_path}\")\n\n# Show the plots\nplt.show()","metadata":{"trusted":true,"editable":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load your model from checkpoint\ndef load_model_from_checkpoint(checkpoint_path, model_class, device):\n    model.load_state_dict(torch.load(checkpoint_path, map_location=device))\n    model.to(device)\n    model.eval()  # Set to evaluation mode\n    return model\n","metadata":{"trusted":true,"editable":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Predict on validation data\ndef predict_on_validation(model, dataloader, device):\n    model.eval()  # Set the model to evaluation mode\n    y_true = []\n    y_pred = []\n\n    with torch.no_grad():  # No need to compute gradients for validation\n        for inputs, labels in val_loader:  # Assuming dataloader is a dictionary with 'val' key\n            inputs = inputs.to(device)\n            labels = labels.to(device)\n            outputs = model(inputs)\n            _, preds = torch.max(outputs, 1)\n\n            # Collect true and predicted labels\n            y_true.extend(labels.cpu().numpy())\n            y_pred.extend(preds.cpu().numpy())\n\n    return np.array(y_true), np.array(y_pred)","metadata":{"trusted":true,"editable":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define paths and device\ncheckpoint_path = \"/kaggle/working/fastvit_s12_9.pt\"\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel_class=5\n# Initialize and load the model (replace YourModel with your actual model class)\nmodel = load_model_from_checkpoint(checkpoint_path, model_class, device)\n\n# Assuming `dataloaders` is a dictionary with 'val' key containing the validation dataloader\n# Predict on validation set\ny_true, y_pred = predict_on_validation(model, val_loader, device)\n","metadata":{"trusted":true,"editable":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Compute confusion matrix\ncm = confusion_matrix(y_true, y_pred)\n\n# Plot confusion matrix using seaborn\nplt.figure(figsize=(8, 6))\nsns.heatmap(cm, annot=True, fmt='d', cmap='Blues', \n            xticklabels=np.arange(cm.shape[0]), yticklabels=np.arange(cm.shape[0]))\nplt.xlabel('Predicted')\nplt.ylabel('True')\nplt.title('Confusion Matrix')\nplt.show()\n","metadata":{"trusted":true,"editable":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Calculate errors per class (misclassifications)\nclass_errors = {}\nnum_classes = cm.shape[0]\nfor i in range(num_classes):\n    total = np.sum(cm[i, :])  # Total number of instances of class i\n    incorrect = total - cm[i, i]  # Misclassified instances of class i\n    error_rate = incorrect / total if total != 0 else 0\n    class_errors[i] = error_rate\n\n# Print class errors\nfor class_id, error_rate in class_errors.items():\n    print(f\"Class {class_id}: Error Rate = {error_rate:.4f}\")\n","metadata":{"trusted":true,"editable":false},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id=\"5\"></a>\n### <div style=\"text-align:center; border-radius:15px; padding:5px; color:white; margin:0; font-size:100%; font-family:Arial; background-color:#f39c12;\"><b> Fine Tuning  SwinV2_base_window16_256 </b></div>\n","metadata":{"editable":false}},{"cell_type":"code","source":"IMG_WIDTH= 256\nIMG_HEIGHT= 256\nMODEL_NAME= \"swinv2_base_window16_256\"\nMODEL_SAVE= MODEL_PATH + MODEL_NAME","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T18:38:15.264396Z","iopub.execute_input":"2024-12-08T18:38:15.265269Z","iopub.status.idle":"2024-12-08T18:38:15.269564Z","shell.execute_reply.started":"2024-12-08T18:38:15.265228Z","shell.execute_reply":"2024-12-08T18:38:15.26864Z"},"editable":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"seed_everything(42)\nset_debug_apis(False)\ntrain_trasforms_DeiT_base_patch16= T.Compose([\n    T.ConvertImageDtype(torch.float32),\n    T.Resize((IMG_WIDTH, IMG_HEIGHT)),\n    T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])\n])\n\nfull_dataset = RetinopathyDataset(TRAIN_DIR, CSV_PATH, transforms=train_trasforms_DeiT_base_patch16)\n\ntrain_size = int(TRAIN_SPLIT * len(full_dataset))\ntest_size = len(full_dataset) - train_size\n\ntrain_dataset, val_dataset = torch.utils.data.random_split(full_dataset, [train_size, test_size])\n\ntrain_loader = DataLoader(full_dataset, batch_size=TRAIN_BATCH_SIZE,\n                          shuffle=False, num_workers=NUM_WORKERS, drop_last=True, pin_memory=False)\n\nval_loader = DataLoader(val_dataset, batch_size=VALID_BATCH_SIZE, shuffle=False,\n                        num_workers=NUM_WORKERS, drop_last=True, pin_memory=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T18:38:16.829026Z","iopub.execute_input":"2024-12-08T18:38:16.829818Z","iopub.status.idle":"2024-12-08T18:38:16.844593Z","shell.execute_reply.started":"2024-12-08T18:38:16.829781Z","shell.execute_reply":"2024-12-08T18:38:16.843738Z"},"editable":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model= timm.create_model(MODEL_NAME, pretrained=True, num_classes=5)\n\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.AdamW(model.parameters(), lr=LEARNING_RATE)\n\nif torch.cuda.is_available():\n    device = \"cuda\"\nelse:\n    device = \"cpu\"\n\nif USE_AMP:\n    from torch.cuda import amp\n    scaler = amp.GradScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T18:38:17.2182Z","iopub.execute_input":"2024-12-08T18:38:17.218572Z","iopub.status.idle":"2024-12-08T18:38:20.437772Z","shell.execute_reply.started":"2024-12-08T18:38:17.21854Z","shell.execute_reply":"2024-12-08T18:38:20.436678Z"},"editable":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Initialize lists to store metrics\ntrain_loss = []\ntrain_top1_acc = []\nval_loss = []\nval_top1_acc = []\n\n# Record start time\nstart_time = time.time()\n\nfor epoch in tqdm(range(EPOCHS)):\n    train_metrics = train_step(model, train_loader, criterion, device, optimizer, scaler=scaler)\n    train_loss.append(train_metrics[\"loss\"])\n    print(f\"Training loss = {train_metrics['loss']}\")\n    train_top1_acc.append(train_metrics[\"top1\"])\n\n    val_metrics = val_step(model_mobilenetv3, val_loader, criterion, device)\n    val_loss.append(val_metrics[\"loss\"])\n    print(f\"Validation loss = {val_metrics['loss']}\")\n    val_top1_acc.append(val_metrics[\"top1\"])\n    \n    # Save model checkpoint\n    checkpoint_path = f\"{MODEL_NAME}_{epoch}.pt\"\n    torch.save(model.state_dict(), checkpoint_path)\n\n# Record end time\nend_time = time.time()\n\n# Calculate total training time\ntotal_time = end_time - start_time\nprint(f\"Total training time: {total_time:.2f} seconds\")\n\n# Create a DataFrame to store the metrics\nmetrics_df = pd.DataFrame({\n    \"epoch\": range(1, EPOCHS + 1),\n    \"train_loss\": train_loss,\n    \"train_top1_acc\": train_top1_acc,\n    \"val_loss\": val_loss,\n    \"val_top1_acc\": val_top1_acc,\n})\n\n# Save the DataFrame to CSV\nmetrics_df.to_csv(MODEL_SAVE, index=False)\nprint(f\"Metrics saved to {metrics_path}\")\n","metadata":{"trusted":true,"editable":false},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id=\"10\"></a>\n### <div style=\"text-align:center; border-radius:15px; padding:5px; color:white; margin:0; font-size:100%; font-family:Arial; background-color:#f39c12;\"><b>  Evalution  SwinV2_base_window16_256 </b></div>\n","metadata":{"editable":false}},{"cell_type":"code","source":"metrics_path = f\"{MODEL_NAME}_metrics.csv\"  # Path to metrics CSV\nmetrics_df = pd.read_csv(MODEL_SAVE)\n\n# Set Seaborn style for better aesthetics\nsns.set_theme(style=\"whitegrid\")\n\n# Create a figure for visualizations\nplt.figure(figsize=(12, 8))\n\n# Plot training and validation loss\nplt.subplot(2, 1, 1)\nsns.lineplot(x='epoch', y='train_loss', data=metrics_df, label='Train Loss', color='blue', marker=\"o\")\nsns.lineplot(x='epoch', y='val_loss', data=metrics_df, label='Validation Loss', color='orange', marker=\"o\")\nplt.title(\"Training and Validation Loss per Epoch\", fontsize=14, fontweight='bold')\nplt.xlabel(\"Epoch\", fontsize=12)\nplt.ylabel(\"Loss\", fontsize=12)\nplt.legend()\nplt.grid(alpha=0.3)\n\n# Plot training and validation accuracy\nplt.subplot(2, 1, 2)\nsns.lineplot(x='epoch', y='train_top1_acc', data=metrics_df, label='Train Accuracy', color='green', marker=\"o\")\nsns.lineplot(x='epoch', y='val_top1_acc', data=metrics_df, label='Validation Accuracy', color='red', marker=\"o\")\nplt.title(\"Training and Validation Accuracy per Epoch\", fontsize=14, fontweight='bold')\nplt.xlabel(\"Epoch\", fontsize=12)\nplt.ylabel(\"Accuracy\", fontsize=12)\nplt.legend()\nplt.grid(alpha=0.3)\n\n# Adjust spacing between plots\nplt.tight_layout()\n\n# Save the visualization as a file (optional)\noutput_plot_path = f\"{MODEL_NAME}_training_visualization.png\"\nplt.savefig(output_plot_path, dpi=300)\nprint(f\"Visualization saved to {output_plot_path}\")\n\n# Show the plots\nplt.show()","metadata":{"trusted":true,"editable":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load your model from checkpoint\ndef load_model_from_checkpoint(checkpoint_path, model_class, device):\n    model.load_state_dict(torch.load(checkpoint_path, map_location=device))\n    model.to(device)\n    model.eval()  # Set to evaluation mode\n    return model\n","metadata":{"trusted":true,"editable":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Predict on validation data\ndef predict_on_validation(model, dataloader, device):\n    model.eval()  # Set the model to evaluation mode\n    y_true = []\n    y_pred = []\n\n    with torch.no_grad():  # No need to compute gradients for validation\n        for inputs, labels in val_loader:  # Assuming dataloader is a dictionary with 'val' key\n            inputs = inputs.to(device)\n            labels = labels.to(device)\n            outputs = model(inputs)\n            _, preds = torch.max(outputs, 1)\n\n            # Collect true and predicted labels\n            y_true.extend(labels.cpu().numpy())\n            y_pred.extend(preds.cpu().numpy())\n\n    return np.array(y_true), np.array(y_pred)","metadata":{"trusted":true,"editable":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define paths and device\ncheckpoint_path = \"/kaggle/working/swinv2_base_window16_256_9.pt\"\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel_class=5\n# Initialize and load the model (replace YourModel with your actual model class)\nmodel = load_model_from_checkpoint(checkpoint_path, model_class, device)\n\n# Assuming `dataloaders` is a dictionary with 'val' key containing the validation dataloader\n# Predict on validation set\ny_true, y_pred = predict_on_validation(model, val_loader, device)\n","metadata":{"trusted":true,"editable":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Compute confusion matrix\ncm = confusion_matrix(y_true, y_pred)\n\n# Plot confusion matrix using seaborn\nplt.figure(figsize=(8, 6))\nsns.heatmap(cm, annot=True, fmt='d', cmap='Blues', \n            xticklabels=np.arange(cm.shape[0]), yticklabels=np.arange(cm.shape[0]))\nplt.xlabel('Predicted')\nplt.ylabel('True')\nplt.title('Confusion Matrix')\nplt.show()\n","metadata":{"trusted":true,"editable":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Calculate errors per class (misclassifications)\nclass_errors = {}\nnum_classes = cm.shape[0]\nfor i in range(num_classes):\n    total = np.sum(cm[i, :])  # Total number of instances of class i\n    incorrect = total - cm[i, i]  # Misclassified instances of class i\n    error_rate = incorrect / total if total != 0 else 0\n    class_errors[i] = error_rate\n\n# Print class errors\nfor class_id, error_rate in class_errors.items():\n    print(f\"Class {class_id}: Error Rate = {error_rate:.4f}\")\n","metadata":{"trusted":true,"editable":false},"outputs":[],"execution_count":null}]}