{"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":"gpu","dataSources":[{"sourceId":6927,"databundleVersionId":45059,"sourceType":"competition"},{"sourceId":10870920,"sourceType":"datasetVersion","datasetId":6730020},{"sourceId":286709,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":200494,"modelId":222308}],"dockerImageVersionId":30918,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# **1. Import necessary libaries**","metadata":{}},{"cell_type":"code","source":"!pip install segmentation-models-pytorch -q","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-03-17T08:07:50.185814Z","iopub.execute_input":"2025-03-17T08:07:50.186157Z","iopub.status.idle":"2025-03-17T08:07:58.476515Z","shell.execute_reply.started":"2025-03-17T08:07:50.186130Z","shell.execute_reply":"2025-03-17T08:07:58.475702Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import segmentation_models_pytorch as smp\nimport torch as t\nfrom torch.utils.data import Dataset, DataLoader, Subset\nfrom torchvision.transforms import ToTensor\nfrom torch import optim\n\nimport os\nimport shutil\nimport zipfile\nimport gc\nimport random\n\nimport numpy as np\nimport pandas as pd\n\nfrom PIL import Image\n\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-17T08:07:58.477634Z","iopub.execute_input":"2025-03-17T08:07:58.477946Z","iopub.status.idle":"2025-03-17T08:08:08.721787Z","shell.execute_reply.started":"2025-03-17T08:07:58.477919Z","shell.execute_reply":"2025-03-17T08:08:08.720931Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Available device = \", t.cuda.is_available())\nprint(\"CUDA version = \", t.version.cuda)\nprint(\"Cudnn version = \", t.backends.cudnn.version())\nprint(\"Cudnn is enabled? = \", t.backends.cudnn.enabled)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-17T08:08:08.723465Z","iopub.execute_input":"2025-03-17T08:08:08.724018Z","iopub.status.idle":"2025-03-17T08:08:08.794601Z","shell.execute_reply.started":"2025-03-17T08:08:08.723982Z","shell.execute_reply":"2025-03-17T08:08:08.793738Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **2. Utils**","metadata":{}},{"cell_type":"markdown","source":"## **2.1 Data loaders**","metadata":{}},{"cell_type":"code","source":"if not os.path.exists('/kaggle/working/dataset/train_masks'):\n    print(\"Train mask does not exists... Extracting the data\")\n    with zipfile.ZipFile('/kaggle/input/carvana-image-masking-challenge/train_masks.zip') as z:\n        z.extractall('/kaggle/working/dataset/')\n\nif not os.path.exists('/kaggle/working/dataset/train'):\n    print(\"Train does not exists... Extracting the data\")\n    with zipfile.ZipFile('/kaggle/input/carvana-image-masking-challenge/train.zip') as z:\n        z.extractall('/kaggle/working/dataset/')\n\n# if not os.path.exists('/kaggle/working/dataset/test'):\n#     print(\"Test does not exists... Extracting the data\")\n#     with zipfile.ZipFile('/kaggle/input/carvana-image-masking-challenge/test.zip') as z:\n#         z.extractall('/kaggle/working/dataset/')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-17T08:08:08.795849Z","iopub.execute_input":"2025-03-17T08:08:08.796184Z","iopub.status.idle":"2025-03-17T08:08:16.601012Z","shell.execute_reply.started":"2025-03-17T08:08:08.796149Z","shell.execute_reply":"2025-03-17T08:08:16.600118Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img_path = '/kaggle/working/dataset/train'\nmsk_path = '/kaggle/working/dataset/train_masks'\n\nimg_lst = os.listdir(img_path)\nmsk_lst = os.listdir(msk_path)\n\nprint(f\"Total images = {len(img_lst)}, Total masks = {len(msk_lst)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-17T08:08:16.601946Z","iopub.execute_input":"2025-03-17T08:08:16.602255Z","iopub.status.idle":"2025-03-17T08:08:16.612020Z","shell.execute_reply.started":"2025-03-17T08:08:16.602225Z","shell.execute_reply":"2025-03-17T08:08:16.611170Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class load_process(Dataset):\n    def __init__(self, img_path, msk_path, bb_name):\n        self.img_path = img_path\n        self.msk_path = msk_path\n        self.process = smp.encoders.get_preprocessing_fn(bb_name)\n        self.lst_img = os.listdir(img_path)\n\n    def __len__(self):\n        return len(self.lst_img)\n\n    def __getitem__(self, index):\n        s_img = Image.open(os.path.join(self.img_path, self.lst_img[index])).convert('RGB').resize((512,512), Image.LANCZOS)\n        s_msk = Image.open(os.path.join(self.msk_path, self.lst_img[index].replace('.jpg', '_mask.gif'))).convert('L').resize((512,512), Image.LANCZOS)\n\n        img_arr = np.array(s_img)\n        msk_arr = np.array(s_msk)\n\n        return t.tensor(self.process(img_arr), dtype=t.float32).permute(2,0,1), t.tensor(msk_arr, dtype=t.int64).unsqueeze(-1).permute(2,0,1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-17T08:08:16.612721Z","iopub.execute_input":"2025-03-17T08:08:16.612984Z","iopub.status.idle":"2025-03-17T08:08:16.621725Z","shell.execute_reply.started":"2025-03-17T08:08:16.612963Z","shell.execute_reply":"2025-03-17T08:08:16.621105Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## **2.2 Plot predictions during training**","metadata":{}},{"cell_type":"code","source":"def plot_predict(bb_name, model, idx=1001):\n    processor = smp.encoders.get_preprocessing_fn(bb_name)\n    img_data = Image.open(os.path.join(img_path, img_lst[idx])).convert('RGB').resize((512,512), Image.LANCZOS)\n    arr_img = np.array(img_data)\n    prcsd = t.tensor(processor(arr_img), dtype=t.float32).permute(2,0,1).unsqueeze(0).to(DEVICE)\n    predicted = t.sigmoid(model(prcsd)).squeeze(0)\n    msk_data = Image.open(os.path.join(msk_path, img_lst[idx].replace('.jpg', '_mask.gif'))).convert('L').resize((512,512), Image.LANCZOS)\n    imgs = [prcsd.squeeze(0).permute(1,2,0).to('cpu').numpy(), np.array(msk_data), \n           (predicted.squeeze(0).to('cpu').detach().numpy() > 0.5).astype(int)]\n    headings = ['Original', 'Mask', 'Prediction']\n    plt.figure(figsize=(15,5))\n    for i in range(len(imgs)):\n        plt.subplot(1,3,i+1)\n        if i >= 1:\n            plt.imshow(imgs[i], cmap='gray')\n        plt.imshow(imgs[i], cmap='gray')\n        plt.title(headings[i])\n        plt.axis('off')\n    plt.show()\n        ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-17T08:08:16.622442Z","iopub.execute_input":"2025-03-17T08:08:16.622727Z","iopub.status.idle":"2025-03-17T08:08:16.641134Z","shell.execute_reply.started":"2025-03-17T08:08:16.622698Z","shell.execute_reply":"2025-03-17T08:08:16.640377Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## **2.3 Train function**","metadata":{}},{"cell_type":"code","source":"def train(model, data_loader, optimizer, ls_fn):\n    model.to(DEVICE)\n    model.train()\n    loss_per_batch = 0\n    progress_bar = tqdm(range(len(data_loader)), desc=\"Batch processing\", unit=\"bath\")\n    for idx, batch in enumerate(data_loader):\n        img = batch[0].to(device = DEVICE, dtype = t.float32)\n        msk = batch[1].to(device = DEVICE, dtype = t.int64)\n        # 1.Forward pass\n        x_preds = model(img)\n        # 2. Calculate the loss\n        loss = ls_fn(x_preds, msk)\n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n        progress_bar.set_postfix(loss=loss.item())\n        progress_bar.update(1)\n        loss_per_batch += loss.item()\n        \n    progress_bar.close()\n    loss_per_batch /= idx+1\n    return loss_per_batch","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-17T08:08:16.643003Z","iopub.execute_input":"2025-03-17T08:08:16.643198Z","iopub.status.idle":"2025-03-17T08:08:16.654352Z","shell.execute_reply.started":"2025-03-17T08:08:16.643180Z","shell.execute_reply":"2025-03-17T08:08:16.653545Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## **2.4 Loss function**","metadata":{}},{"cell_type":"code","source":"def loss_iou(y_pred, y_true, inf=False):\n    if not inf:\n        if not y_pred.requires_grad:\n            raise ValueError(\"y_pred should have gradient tracking\")\n    \n    device = y_pred.device\n    y_pred = t.sigmoid(y_pred)\n    y_true = t.where(y_true <= 0, t.zeros_like(y_pred, device=device), t.ones_like(y_pred, device=device))\n    intersection = t.abs((y_pred.view((-1)) * y_true.view((-1))).sum().float())\n    union = t.abs((y_pred.sum() + y_true.sum()).float())\n\n    iou = (t.abs(intersection) + 1e-5) / ((union + 1e-5) - t.abs(intersection))\n    iou_loss = 1 - iou\n    return iou_loss","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-17T08:08:16.655349Z","iopub.execute_input":"2025-03-17T08:08:16.655662Z","iopub.status.idle":"2025-03-17T08:08:16.670015Z","shell.execute_reply.started":"2025-03-17T08:08:16.655633Z","shell.execute_reply":"2025-03-17T08:08:16.669349Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **3. Model training**","metadata":{}},{"cell_type":"markdown","source":"## **3.1 resnet101 with imagenet**","metadata":{}},{"cell_type":"code","source":"DEVICE = 'cuda' if t.cuda.is_available() else 'cpu'\nprint(f\"Available device = {DEVICE}\")\nNUM_CLASS = 1\nEPOCHS = 10\nLEARNING_RATE = 1e-2\nENCODER_NAME = 'resnet101'\nENCODER_WEIGHT = 'imagenet'\nBATCH_SIZE = 8\n\nloss_dict = dict()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-17T08:08:16.670793Z","iopub.execute_input":"2025-03-17T08:08:16.671090Z","iopub.status.idle":"2025-03-17T08:08:16.683553Z","shell.execute_reply.started":"2025-03-17T08:08:16.671061Z","shell.execute_reply":"2025-03-17T08:08:16.682948Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model1 = smp.Unet(encoder_name = ENCODER_NAME, encoder_weights=ENCODER_WEIGHT, in_channel=3, classes=NUM_CLASS)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-17T08:08:31.193180Z","iopub.execute_input":"2025-03-17T08:08:31.193457Z","iopub.status.idle":"2025-03-17T08:08:33.110472Z","shell.execute_reply.started":"2025-03-17T08:08:31.193435Z","shell.execute_reply":"2025-03-17T08:08:33.109793Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ls_fn = smp.losses.DiceLoss('binary')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-17T08:08:33.111289Z","iopub.execute_input":"2025-03-17T08:08:33.111490Z","iopub.status.idle":"2025-03-17T08:08:33.115028Z","shell.execute_reply.started":"2025-03-17T08:08:33.111472Z","shell.execute_reply":"2025-03-17T08:08:33.113938Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"optimizer = optim.Adam(params = model1.parameters(), lr=LEARNING_RATE)\nresnet_data = load_process(img_path, msk_path, ENCODER_NAME)\nsubset = Subset(resnet_data, range(1000))\ntrain_data = DataLoader(subset, BATCH_SIZE, True)\n\nloss_now = []\nfor epoch in range(1, EPOCHS+1):\n    ls = train(model1, train_data, optimizer, loss_iou)\n    print(f\"{epoch} / {EPOCHS} | train_loss = {ls:.2f}\")\n    plot_predict(ENCODER_NAME, model1)\n    loss_now.append(ls)\nloss_dict[ENCODER_NAME] = loss_now","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-17T08:08:33.116094Z","iopub.execute_input":"2025-03-17T08:08:33.116319Z","iopub.status.idle":"2025-03-17T08:08:33.133703Z","shell.execute_reply.started":"2025-03-17T08:08:33.116299Z","shell.execute_reply":"2025-03-17T08:08:33.133078Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"t.save(model1.state_dict(), \"U-Net_resnet101.pt\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-12T17:17:21.922680Z","iopub.execute_input":"2025-03-12T17:17:21.923022Z","iopub.status.idle":"2025-03-12T17:17:22.237164Z","shell.execute_reply.started":"2025-03-12T17:17:21.922994Z","shell.execute_reply":"2025-03-12T17:17:22.236339Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"del model1, train_data\ngc.collect()\nt.cuda.empty_cache()\nt.cuda.reset_peak_memory_stats()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-15T18:36:46.583538Z","iopub.execute_input":"2025-03-15T18:36:46.583911Z","iopub.status.idle":"2025-03-15T18:36:47.002118Z","shell.execute_reply.started":"2025-03-15T18:36:46.583883Z","shell.execute_reply":"2025-03-15T18:36:47.000507Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## **3.2 efficientnet with imagenet**","metadata":{}},{"cell_type":"code","source":"ENCODER_NAME = 'efficientnet-b3'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-17T08:08:37.488379Z","iopub.execute_input":"2025-03-17T08:08:37.488657Z","iopub.status.idle":"2025-03-17T08:08:37.492027Z","shell.execute_reply.started":"2025-03-17T08:08:37.488635Z","shell.execute_reply":"2025-03-17T08:08:37.491172Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model2 = smp.Unet(encoder_name = ENCODER_NAME, encoder_weights=ENCODER_WEIGHT, in_channel=3, classes=NUM_CLASS)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-17T08:08:37.881240Z","iopub.execute_input":"2025-03-17T08:08:37.881488Z","iopub.status.idle":"2025-03-17T08:08:38.738593Z","shell.execute_reply.started":"2025-03-17T08:08:37.881466Z","shell.execute_reply":"2025-03-17T08:08:38.737952Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"optimizer = optim.Adam(params = model2.parameters(), lr=LEARNING_RATE)\nefficientnet_data = load_process(img_path, msk_path, ENCODER_NAME)\nsubset = Subset(efficientnet_data, range(1000))\ntrain_data = DataLoader(subset, BATCH_SIZE, True)\nloss_now = []\n\nfor epoch in range(1, EPOCHS+1):\n    ls = train(model2, train_data, optimizer, loss_iou)\n    print(f\"{epoch} / {EPOCHS} | train_loss = {ls:.2f}\")\n    plot_predict(ENCODER_NAME, model2)\n    loss_now.append(ls)\n    \nloss_dict[ENCODER_NAME] = loss_now","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-17T08:08:45.105787Z","iopub.execute_input":"2025-03-17T08:08:45.106127Z","iopub.status.idle":"2025-03-17T08:08:45.114662Z","shell.execute_reply.started":"2025-03-17T08:08:45.106099Z","shell.execute_reply":"2025-03-17T08:08:45.113931Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"t.save(model2.state_dict(), 'U-net_efficientnet.pt')","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"del model2, train_data\ngc.collect()\nt.cuda.empty_cache()\nt.cuda.reset_peak_memory_stats()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-13T08:21:02.843576Z","iopub.execute_input":"2025-03-13T08:21:02.843929Z","iopub.status.idle":"2025-03-13T08:21:03.389683Z","shell.execute_reply.started":"2025-03-13T08:21:02.843898Z","shell.execute_reply":"2025-03-13T08:21:03.388984Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## **3.3 inceptionv4 with imagenet**","metadata":{}},{"cell_type":"code","source":"ENCODER_NAME = 'inceptionv4'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-17T08:08:48.448418Z","iopub.execute_input":"2025-03-17T08:08:48.448706Z","iopub.status.idle":"2025-03-17T08:08:48.452195Z","shell.execute_reply.started":"2025-03-17T08:08:48.448682Z","shell.execute_reply":"2025-03-17T08:08:48.451363Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model3 =smp.Unet(encoder_name = ENCODER_NAME, encoder_weights=ENCODER_WEIGHT, in_channel=3, classes=NUM_CLASS)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-17T08:08:49.685990Z","iopub.execute_input":"2025-03-17T08:08:49.686278Z","iopub.status.idle":"2025-03-17T08:08:59.354184Z","shell.execute_reply.started":"2025-03-17T08:08:49.686255Z","shell.execute_reply":"2025-03-17T08:08:59.353476Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"optimizer = optim.Adam(params = model3.parameters(), lr=LEARNING_RATE)\ninceptionv4_data = load_process(img_path, msk_path, ENCODER_NAME)\nsubset = Subset(inceptionv4_data, range(1000))\ntrain_data = DataLoader(subset, BATCH_SIZE, True)\nloss_now = []\n\nfor epoch in range(1, EPOCHS+1):\n    ls = train(model3, train_data, optimizer, loss_iou)\n    print(f\"{epoch} / {EPOCHS} | train_loss = {ls:.2f}\")\n    plot_predict(ENCODER_NAME, model3)\n    loss_now.append(ls)\n    \nloss_dict[ENCODER_NAME] = loss_now","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-17T08:09:00.298057Z","iopub.execute_input":"2025-03-17T08:09:00.298334Z","iopub.status.idle":"2025-03-17T08:09:00.307808Z","shell.execute_reply.started":"2025-03-17T08:09:00.298312Z","shell.execute_reply":"2025-03-17T08:09:00.307007Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"t.save(model3.state_dict(), 'U-Net_inception.pt')","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"del model3, train_data\ngc.collect()\nt.cuda.empty_cache()\nt.cuda.reset_peak_memory_stats()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **4. Plot loss**","metadata":{}},{"cell_type":"code","source":"for i,j in loss_dict.items():\n    plt.plot(j, label=i)\n\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-15T18:37:53.179593Z","iopub.execute_input":"2025-03-15T18:37:53.179931Z","iopub.status.idle":"2025-03-15T18:37:53.334212Z","shell.execute_reply.started":"2025-03-15T18:37:53.179903Z","shell.execute_reply":"2025-03-15T18:37:53.332999Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **5. Ensemble learning with previously trained models**","metadata":{}},{"cell_type":"markdown","source":"## **5.1 Loading the model and weights**","metadata":{}},{"cell_type":"code","source":"if os.path.exists('/kaggle/working/U-Net_inception.pt'):\n    inception_path = '/kaggle/working/U-Net_inception.pt'\nelse:\n    inception_path = '/kaggle/input/instance-segmentation-carvana-image-challenge/pytorch/default/2/U-Net_inception.pt'\n    \nif os.path.exists('/kaggle/working/U-Net_resnet101.pt'):\n    resnet_path = '/kaggle/working/U-Net_resnet101.pt'\nelse:\n    resnet_path = '/kaggle/input/instance-segmentation-carvana-image-challenge/pytorch/default/2/U-Net_resnet101.pt'\n    \nif os.path.exists('/kaggle/working/U-net_efficientnet.pt'):\n    efficientnet_path = '/kaggle/working/U-net_efficientnet.pt'\nelse:\n    efficientnet_path = '/kaggle/input/instance-segmentation-carvana-image-challenge/pytorch/default/2/U-net_efficientnet.pt'\n\ninception_mdl = smp.Unet(encoder_name = 'inceptionv4', encoder_weights=ENCODER_WEIGHT, in_channel=3, classes=NUM_CLASS).to(DEVICE)\nresnet_mdl = smp.Unet(encoder_name = 'resnet101', encoder_weights=ENCODER_WEIGHT, in_channel=3, classes=NUM_CLASS).to(DEVICE)\nefficientnet_mdl = smp.Unet(encoder_name = 'efficientnet-b3', encoder_weights=ENCODER_WEIGHT, in_channel=3, classes=NUM_CLASS).to(DEVICE)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-17T08:09:05.208629Z","iopub.execute_input":"2025-03-17T08:09:05.208948Z","iopub.status.idle":"2025-03-17T08:09:07.308149Z","shell.execute_reply.started":"2025-03-17T08:09:05.208923Z","shell.execute_reply":"2025-03-17T08:09:07.307445Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"inception_mdl.load_state_dict(t.load(inception_path, weights_only=True, map_location=t.device(DEVICE)))\nresnet_mdl.load_state_dict(t.load(resnet_path, weights_only=True, map_location=t.device(DEVICE)))\nefficientnet_mdl.load_state_dict(t.load(efficientnet_path, weights_only=True, map_location=t.device(DEVICE)))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-17T08:09:08.603795Z","iopub.execute_input":"2025-03-17T08:09:08.604124Z","iopub.status.idle":"2025-03-17T08:09:12.568569Z","shell.execute_reply.started":"2025-03-17T08:09:08.604097Z","shell.execute_reply":"2025-03-17T08:09:12.567676Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## **4.2 Calculate loss with test image**","metadata":{}},{"cell_type":"code","source":"models = [inception_mdl, resnet_mdl, efficientnet_mdl]\nbackbones = ['inceptionv4', 'resnet101', 'efficientnet-b3']\ndata_set = [inceptionv4_data, resnet_data, efficientnet_data]\n\nrand_idx = random.randint(1000, 5000)\nmodel_loss = dict()\npredictions = []\nmasks = []\nfor i in range(len(models)):\n    models[i].eval()\n    img = data_set[i][rand_idx][0].to(device=DEVICE, dtype=t.float32).unsqueeze(0)\n    msk = data_set[i][rand_idx][1].to(device=DEVICE, dtype=t.int64)\n    masks.append(msk)\n    with t.inference_mode():\n        pred = models[i](img)\n        predictions.append(pred.cpu().numpy())\n        loss = loss_iou(pred, msk, True)\n    plot_predict(backbones[i], models[i], rand_idx)\n    model_loss[backbones[i]] = [loss.item()]\n    print(f\"Backbone = {backbones[i]}, Loss = {loss.item()}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-17T08:09:12.569913Z","iopub.execute_input":"2025-03-17T08:09:12.570242Z","iopub.status.idle":"2025-03-17T08:09:14.885905Z","shell.execute_reply.started":"2025-03-17T08:09:12.570216Z","shell.execute_reply":"2025-03-17T08:09:14.885217Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pd.DataFrame(model_loss)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-17T08:09:17.585157Z","iopub.execute_input":"2025-03-17T08:09:17.585482Z","iopub.status.idle":"2025-03-17T08:09:17.608304Z","shell.execute_reply.started":"2025-03-17T08:09:17.585453Z","shell.execute_reply":"2025-03-17T08:09:17.607605Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## **4.3 Ensembling the model**","metadata":{}},{"cell_type":"code","source":"weighted_summary = dict()\nfor i in range(4):\n    for j in range(4):\n        for k in range(4):\n            weights = [i/10, j/10, k/10]\n            weighted_preds = np.tensordot(weights, predictions, axes=(0,0))\n            L = loss_iou(t.from_numpy(weighted_preds), masks[0].to('cpu'), True).item()\n            weighted_summary[str(i/10)+\"_\"+str(j/10)+\"_\"+str(k/10)] = round(L, 6)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-17T08:09:20.128677Z","iopub.execute_input":"2025-03-17T08:09:20.128998Z","iopub.status.idle":"2025-03-17T08:09:20.371679Z","shell.execute_reply.started":"2025-03-17T08:09:20.128972Z","shell.execute_reply":"2025-03-17T08:09:20.370786Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"summary_wgt = np.array([])\nsummary_ls = np.array([])\nfor i,j in weighted_summary.items():\n    summary_wgt = np.append(summary_wgt, [i])\n    summary_ls = np.append(summary_ls, [j*100])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-17T08:11:15.519100Z","iopub.execute_input":"2025-03-17T08:11:15.519393Z","iopub.status.idle":"2025-03-17T08:11:15.524539Z","shell.execute_reply.started":"2025-03-17T08:11:15.519370Z","shell.execute_reply":"2025-03-17T08:11:15.523855Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_wgt = pd.DataFrame(data=summary_wgt.reshape((8,8)))\ndf_wgt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-17T08:11:16.594128Z","iopub.execute_input":"2025-03-17T08:11:16.594388Z","iopub.status.idle":"2025-03-17T08:11:16.604816Z","shell.execute_reply.started":"2025-03-17T08:11:16.594366Z","shell.execute_reply":"2025-03-17T08:11:16.604030Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"summary_ls.min(), summary_ls.max()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-17T08:12:52.047498Z","iopub.execute_input":"2025-03-17T08:12:52.047776Z","iopub.status.idle":"2025-03-17T08:12:52.053045Z","shell.execute_reply.started":"2025-03-17T08:12:52.047754Z","shell.execute_reply":"2025-03-17T08:12:52.052200Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_ls = pd.DataFrame(data=summary_ls.reshape((8,8)))\ndf_ls.style.background_gradient(cmap='Greens', low=0, high=0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-17T08:13:31.348330Z","iopub.execute_input":"2025-03-17T08:13:31.348601Z","iopub.status.idle":"2025-03-17T08:13:31.367182Z","shell.execute_reply.started":"2025-03-17T08:13:31.348579Z","shell.execute_reply":"2025-03-17T08:13:31.366338Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_wgt[(df_ls <= 2.5) & (df_ls >= 2)]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-17T08:20:37.829767Z","iopub.execute_input":"2025-03-17T08:20:37.830079Z","iopub.status.idle":"2025-03-17T08:20:37.840476Z","shell.execute_reply.started":"2025-03-17T08:20:37.830054Z","shell.execute_reply":"2025-03-17T08:20:37.839742Z"}},"outputs":[],"execution_count":null}]}