{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","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"}],"dockerImageVersionId":31040,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom PIL import Image\nimport cv2\nfrom tqdm.notebook import tqdm\nimport os\nimport numpy as np\nimport torch\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-05-14T13:27:51.814198Z","iopub.execute_input":"2025-05-14T13:27:51.814735Z","iopub.status.idle":"2025-05-14T13:27:51.821181Z","shell.execute_reply.started":"2025-05-14T13:27:51.814715Z","shell.execute_reply":"2025-05-14T13:27:51.820535Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def get_config():\n    return {'Width' : 1918,\n            'Height': 1280,\n            'W_pad_size': 2,\n            'Resize_W': 1920 // 4,\n            'Resize_H': 1280 // 4,\n            'Batch_size': 8,\n            'Num_workers': 2,\n            'Device': 'cuda' if torch.cuda.is_available() else 'cpu', \n            'Num_epochs': 20,\n           }\nconfig = get_config()\nconfig","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T13:27:55.151071Z","iopub.execute_input":"2025-05-14T13:27:55.151916Z","iopub.status.idle":"2025-05-14T13:27:55.157384Z","shell.execute_reply.started":"2025-05-14T13:27:55.151889Z","shell.execute_reply":"2025-05-14T13:27:55.156815Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Load Data","metadata":{}},{"cell_type":"markdown","source":"## Unzip","metadata":{}},{"cell_type":"code","source":"import zipfile\nimport os\n\nextract_path = '/kaggle/working'\n\n# Extract the zip file\ndef unzip(file):\n    with zipfile.ZipFile(file, 'r') as zip_ref:\n        zip_ref.extractall(extract_path)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T13:27:59.441960Z","iopub.execute_input":"2025-05-14T13:27:59.442675Z","iopub.status.idle":"2025-05-14T13:27:59.447393Z","shell.execute_reply.started":"2025-05-14T13:27:59.442645Z","shell.execute_reply":"2025-05-14T13:27:59.446644Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_mask_dir = '/kaggle/input/carvana-image-masking-challenge/train_masks.zip'\nunzip(train_mask_dir)\n\ntrain_dir = '/kaggle/input/carvana-image-masking-challenge/train.zip'\nunzip(train_dir)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T13:27:43.668460Z","iopub.status.idle":"2025-05-14T13:27:43.668761Z","shell.execute_reply.started":"2025-05-14T13:27:43.668594Z","shell.execute_reply":"2025-05-14T13:27:43.668609Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# unzip('/kaggle/input/carvana-image-masking-challenge/train_hq.zip')\n# unzip('/kaggle/input/carvana-image-masking-challenge/metadata.csv.zip')\nunzip('/kaggle/input/carvana-image-masking-challenge/sample_submission.csv.zip')\nunzip('/kaggle/input/carvana-image-masking-challenge/train_masks.csv.zip')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T13:27:43.669638Z","iopub.status.idle":"2025-05-14T13:27:43.669940Z","shell.execute_reply.started":"2025-05-14T13:27:43.669780Z","shell.execute_reply":"2025-05-14T13:27:43.669796Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pd.read_csv('/kaggle/working/train_masks.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T13:28:03.603121Z","iopub.execute_input":"2025-05-14T13:28:03.603706Z","iopub.status.idle":"2025-05-14T13:28:04.068382Z","shell.execute_reply.started":"2025-05-14T13:28:03.603684Z","shell.execute_reply":"2025-05-14T13:28:04.067741Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Reading files and Resizing","metadata":{}},{"cell_type":"code","source":"\ndef read_file(dir_):\n    files = os.listdir(dir_)\n    files.sort()\n    images, images_resized = [], []\n    for fname in tqdm(files, desc=\"Loading...\"):\n        path = os.path.join(dir_, fname)\n        with Image.open(path) as img:  # Auto-close image after use\n            # plt.imshow(img, cmap='gray')\n            # plt.show()\n            W, H = img.size\n            if W != config['Width'] or H != config['Height']:\n                print(f\"Size mismatch! Expected: {config['Width'], config['Height']}. Got : {W, H}.\")\n                return\n            img = np.array(img)\n            pad_width = ((0, 0), (0, 2)) if img.ndim == 2 else ((0, 0), (0, 2), (0, 0))  \n            img = np.pad(img, pad_width, mode='edge')           \n            # images.append(Image.fromarray(img))\n\n            # plt.imshow(img, cmap='gray')\n            # plt.show()\n            \n            img = cv2.resize(img, (config['Resize_W'], config['Resize_H']), interpolation=cv2.INTER_AREA)\n            # plt.imshow(img, cmap='gray')\n            # plt.show()\n            images_resized.append(Image.fromarray(img))\n            # break\n\n    return files, images, images_resized\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T13:28:06.702649Z","iopub.execute_input":"2025-05-14T13:28:06.703332Z","iopub.status.idle":"2025-05-14T13:28:06.709016Z","shell.execute_reply.started":"2025-05-14T13:28:06.703293Z","shell.execute_reply":"2025-05-14T13:28:06.708370Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_files, train_images, train_images_resized  = read_file('/kaggle/working/train')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T13:28:09.386843Z","iopub.execute_input":"2025-05-14T13:28:09.387521Z","iopub.status.idle":"2025-05-14T13:30:38.881141Z","shell.execute_reply.started":"2025-05-14T13:28:09.387497Z","shell.execute_reply":"2025-05-14T13:30:38.880352Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# len(train_files), len(train_images), len(train_images_resized), train_images[0].size, train_images_resized[0].size\nlen(train_files), len(train_images_resized), train_images_resized[0].size","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T13:30:38.882172Z","iopub.execute_input":"2025-05-14T13:30:38.882405Z","iopub.status.idle":"2025-05-14T13:30:38.887189Z","shell.execute_reply.started":"2025-05-14T13:30:38.882388Z","shell.execute_reply":"2025-05-14T13:30:38.886554Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(train_images_resized[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T13:30:38.888399Z","iopub.execute_input":"2025-05-14T13:30:38.888981Z","iopub.status.idle":"2025-05-14T13:30:39.124726Z","shell.execute_reply.started":"2025-05-14T13:30:38.888955Z","shell.execute_reply":"2025-05-14T13:30:39.123968Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import gc\ngc.collect()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T13:30:39.126053Z","iopub.execute_input":"2025-05-14T13:30:39.126274Z","iopub.status.idle":"2025-05-14T13:30:39.256813Z","shell.execute_reply.started":"2025-05-14T13:30:39.126255Z","shell.execute_reply":"2025-05-14T13:30:39.256108Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_mask_files, train_masks, train_masks_resized  = read_file('/kaggle/working/train_masks')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T13:30:39.257675Z","iopub.execute_input":"2025-05-14T13:30:39.257959Z","iopub.status.idle":"2025-05-14T13:31:29.547286Z","shell.execute_reply.started":"2025-05-14T13:30:39.257942Z","shell.execute_reply":"2025-05-14T13:31:29.546577Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(train_mask_files), len(train_masks_resized), train_masks_resized[0].size","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T13:31:29.548028Z","iopub.execute_input":"2025-05-14T13:31:29.548279Z","iopub.status.idle":"2025-05-14T13:31:29.553457Z","shell.execute_reply.started":"2025-05-14T13:31:29.548261Z","shell.execute_reply":"2025-05-14T13:31:29.552619Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(train_masks_resized[0], cmap='gray')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T13:31:29.554171Z","iopub.execute_input":"2025-05-14T13:31:29.554416Z","iopub.status.idle":"2025-05-14T13:31:29.737812Z","shell.execute_reply.started":"2025-05-14T13:31:29.554400Z","shell.execute_reply":"2025-05-14T13:31:29.737158Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_masks_resized[0].size","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T13:31:29.738545Z","iopub.execute_input":"2025-05-14T13:31:29.738775Z","iopub.status.idle":"2025-05-14T13:31:29.743506Z","shell.execute_reply.started":"2025-05-14T13:31:29.738750Z","shell.execute_reply":"2025-05-14T13:31:29.742906Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_files[22], train_mask_files[22]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T13:31:29.744290Z","iopub.execute_input":"2025-05-14T13:31:29.744650Z","iopub.status.idle":"2025-05-14T13:31:29.759594Z","shell.execute_reply.started":"2025-05-14T13:31:29.744628Z","shell.execute_reply":"2025-05-14T13:31:29.759050Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Data set and Data Loader","metadata":{}},{"cell_type":"markdown","source":"## Dataset Class","metadata":{}},{"cell_type":"code","source":"import torch\nfrom torch.utils.data import Dataset, DataLoader, random_split\nfrom torchvision import transforms\n\nclass SegmentationDataset(Dataset):\n    def __init__(self, images, masks, files, transform=None):\n        self.images = images\n        self.masks = masks\n        self.files = files\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.images)\n\n    def __getitem__(self, idx):\n        img = self.images[idx]\n        mask = self.masks[idx]\n\n        if self.transform:\n            img = self.transform(img)\n            mask = self.transform(mask) * 255\n\n        return img, mask, self.files[idx]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T13:31:29.761626Z","iopub.execute_input":"2025-05-14T13:31:29.761860Z","iopub.status.idle":"2025-05-14T13:31:32.852055Z","shell.execute_reply.started":"2025-05-14T13:31:29.761845Z","shell.execute_reply":"2025-05-14T13:31:32.851532Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"transform = transforms.Compose([\n    transforms.ToTensor(),  # Converts to [C, H, W] with values in [0, 1]\n])\n\nfull_dataset = SegmentationDataset(train_images_resized, train_masks_resized, train_files, transform=transform)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T13:31:32.852661Z","iopub.execute_input":"2025-05-14T13:31:32.852970Z","iopub.status.idle":"2025-05-14T13:31:32.856723Z","shell.execute_reply.started":"2025-05-14T13:31:32.852947Z","shell.execute_reply":"2025-05-14T13:31:32.855942Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_ratio = 0.1\nval_size = int(len(full_dataset) * val_ratio)\ntrain_size = len(full_dataset) - val_size\n\ntrain_dataset, val_dataset = random_split(full_dataset, [train_size, val_size])\n\n\ntrain_loader = DataLoader(train_dataset, batch_size=config['Batch_size'], shuffle=True, num_workers=config['Num_workers'], drop_last=True)\nval_loader = DataLoader(val_dataset, batch_size=config['Batch_size'], shuffle=False, num_workers=config['Num_workers'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T13:31:32.857891Z","iopub.execute_input":"2025-05-14T13:31:32.858171Z","iopub.status.idle":"2025-05-14T13:31:32.878797Z","shell.execute_reply.started":"2025-05-14T13:31:32.858150Z","shell.execute_reply":"2025-05-14T13:31:32.878226Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torchvision.models as models\nimport torch.nn.functional as F\n\nimport torch.optim as optim\nimport copy\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T13:31:32.879449Z","iopub.execute_input":"2025-05-14T13:31:32.879615Z","iopub.status.idle":"2025-05-14T13:31:32.884627Z","shell.execute_reply.started":"2025-05-14T13:31:32.879602Z","shell.execute_reply":"2025-05-14T13:31:32.884033Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class Double_Conv(nn.Module):\n    def __init__(self, in_ch, mid_ch, out_ch):\n        super().__init__()\n        self.block = nn.Sequential(\n            nn.Conv2d(in_ch, mid_ch, kernel_size=3, padding=1),\n            nn.BatchNorm2d(mid_ch),\n            nn.ReLU(inplace=True),\n            \n            nn.Conv2d(mid_ch, out_ch, kernel_size=3, padding=1),\n            nn.BatchNorm2d(out_ch),\n            nn.ReLU(inplace=True)\n        )\n\n    def forward(self, x):\n        return self.block(x)\n\nclass DecoderBlock(nn.Module):\n    def __init__(self, in_ch, mid_ch, out_ch):\n        super().__init__()\n        self.double_conv = Double_Conv(in_ch, mid_ch, mid_ch)\n        self.upsample    = nn.ConvTranspose2d(mid_ch, out_ch, kernel_size=2, stride=2)\n\n    def forward(self, x):\n        return self.upsample(self.double_conv(x))\n\nclass ResNetUNet(nn.Module):\n    def __init__(self, n_classes=1):\n        super().__init__()\n        resnet = models.resnet50(pretrained=True)        \n        # Freeze ResNet encoder\n        # for param in resnet.parameters():\n        #     param.requires_grad = False\n\n        # Encoder\n        self.base_layers = list(resnet.children())\n        self.input_layer = nn.Sequential(*self.base_layers[:3])       # conv1 + bn1 + relu\n        self.maxpool = self.base_layers[3]                            # maxpool\n        self.encoder1 = self.base_layers[4]                           # layer1\n        self.encoder2 = self.base_layers[5]                           # layer2\n        self.encoder3 = self.base_layers[6]                           # layer3\n        self.encoder4 = self.base_layers[7]                           # layer4\n\n        # Decoder\n        self.decoder5 = DecoderBlock(2048, 1024, 1024)\n        self.decoder4 = DecoderBlock(2048, 1024, 512)\n        self.decoder3 = DecoderBlock(1024, 512, 256)\n        self.decoder2 = DecoderBlock(512, 256, 64)\n        self.decoder1 = DecoderBlock(128, 64, 64)\n        \n        self.decoder_x0 = DecoderBlock(64, 64, 64)\n\n        self.double_conv = Double_Conv(128, 64, 64)\n\n        self.final_conv = nn.Conv2d(64, n_classes, kernel_size=1)\n\n    def forward(self, x):\n        x0 = self.input_layer(x)          # (B, 64, H/2, W/2)\n        x1 = self.maxpool(x0)             # (B, 64, H/4, W/4)\n        x2 = self.encoder1(x1)            # (B, 256, H/4, W/4)\n        x3 = self.encoder2(x2)            # (B, 512, H/8, W/8)\n        x4 = self.encoder3(x3)            # (B, 1024, H/16, W/16)\n        x5 = self.encoder4(x4)            # (B, 2048, H/32, W/32)\n\n        d4 = self.decoder5(x5)            # (B, 256, H/16, W/16)\n        d3 = self.decoder4(torch.cat([d4, x4], dim=1))  # + x4\n        d2 = self.decoder3(torch.cat([d3, x3], dim=1))  # + x3\n        d1 = self.decoder2(torch.cat([d2, x2], dim=1))  # + x2\n        d0 = self.decoder1(torch.cat([d1, x0], dim=1))  # + x0\n\n        out = self.double_conv(torch.cat([self.decoder_x0(x0), d0], dim=1))\n\n        out = self.final_conv(out)\n        return torch.sigmoid(out)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T15:04:51.118920Z","iopub.execute_input":"2025-05-14T15:04:51.119236Z","iopub.status.idle":"2025-05-14T15:04:51.131777Z","shell.execute_reply.started":"2025-05-14T15:04:51.119210Z","shell.execute_reply":"2025-05-14T15:04:51.131071Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = ResNetUNet(n_classes=1)\ninput_tensor = torch.randn(1, 3, 320, 480)  # B, C, H, W\noutput = model(input_tensor)\nprint(output.shape)  # [1, 1, 480, 320]\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T15:04:54.261871Z","iopub.execute_input":"2025-05-14T15:04:54.262436Z","iopub.status.idle":"2025-05-14T15:04:57.091047Z","shell.execute_reply.started":"2025-05-14T15:04:54.262412Z","shell.execute_reply":"2025-05-14T15:04:57.090225Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Training","metadata":{}},{"cell_type":"code","source":"device = config['Device']\nmodel = model.to(device)\n# criterion = nn.BCEWithLogitsLoss()  # Use sigmoid inside loss\ncriterion = nn.BCELoss()\noptimizer = optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=1e-4)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T15:04:57.092260Z","iopub.execute_input":"2025-05-14T15:04:57.092774Z","iopub.status.idle":"2025-05-14T15:04:57.225432Z","shell.execute_reply.started":"2025-05-14T15:04:57.092755Z","shell.execute_reply":"2025-05-14T15:04:57.224538Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Training fnc","metadata":{}},{"cell_type":"code","source":"def train_model(model,train_L, val_L, criterion, optimizer, num_epochs=10):\n    best_model_wts = copy.deepcopy(model.state_dict())\n    best_loss = float('inf')\n\n    train_loss_history = []\n    val_loss_history = []\n\n    for epoch in tqdm(range(num_epochs), desc='Training '):\n        print(f\"Epoch {epoch+1}/{num_epochs}\")\n\n        for phase in ['train', 'val']:\n            if phase == 'train':\n                model.train()\n                dataloader = train_L\n            else:\n                model.eval()\n                dataloader = val_L\n\n            running_loss = 0.0\n\n            for inputs, masks, _ in tqdm(dataloader, desc=phase):\n                inputs = inputs.to(device)\n                masks = masks.to(device)\n\n                optimizer.zero_grad()\n\n                with torch.set_grad_enabled(phase == 'train'):\n                    outputs = model(inputs)\n                    # outputs = outputs.squeeze(1)  # [B, H, W]\n                    # masks = masks.squeeze(1)\n                    # print(outputs.size(), masks.shape)\n                    loss = criterion(outputs, masks)\n\n                    if phase == 'train':\n                        loss.backward()\n                        optimizer.step()\n\n                running_loss += loss.item() * inputs.size(0)\n\n            epoch_loss = running_loss / len(dataloader.dataset)\n            print(f\"{phase.capitalize()} Loss: {epoch_loss:.4f}\")\n\n            if phase == 'train':\n                train_loss_history.append(epoch_loss)\n            else:\n                val_loss_history.append(epoch_loss)\n                if epoch_loss < best_loss:\n                    best_loss = epoch_loss\n                    best_model_wts = copy.deepcopy(model.state_dict())\n                    torch.save(best_model_wts, \"best_model.pth\")\n\n    print(\"Training complete. Best val loss: {:.4f}\".format(best_loss))\n    model.load_state_dict(best_model_wts)\n    return model, train_loss_history, val_loss_history\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T15:04:58.524911Z","iopub.execute_input":"2025-05-14T15:04:58.525184Z","iopub.status.idle":"2025-05-14T15:04:58.533036Z","shell.execute_reply.started":"2025-05-14T15:04:58.525165Z","shell.execute_reply":"2025-05-14T15:04:58.532238Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"trained_model, train_losses, val_losses = train_model(\n    model, train_loader, val_loader, criterion, optimizer, num_epochs=config['Num_epochs']\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T15:05:01.976566Z","iopub.execute_input":"2025-05-14T15:05:01.977074Z","iopub.status.idle":"2025-05-14T16:51:59.853124Z","shell.execute_reply.started":"2025-05-14T15:05:01.977051Z","shell.execute_reply":"2025-05-14T16:51:59.852238Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(8,5))\nplt.plot(range(1,len(train_losses)+1), train_losses, label='Train Loss')\nplt.plot(range(1,len(val_losses)+1), val_losses, label='Validation Loss')\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"Loss\")\nplt.title(\"Training & Validation Loss\")\nplt.legend()\nplt.grid(True)\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T15:00:13.352896Z","iopub.execute_input":"2025-05-14T15:00:13.353126Z","iopub.status.idle":"2025-05-14T15:00:13.543101Z","shell.execute_reply.started":"2025-05-14T15:00:13.353104Z","shell.execute_reply":"2025-05-14T15:00:13.542381Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for im, m, _ in train_loader:\n    outputs=trained_model(im.to(device))\n    out = outputs[0][0].detach().to('cpu').numpy()\n\n    plt.hist(out.reshape(-1))\n    plt.show()\n\n    out[out > .5] = 1\n    out[out < 1] = 0\n    plt.imshow(out, cmap='gray')\n    plt.show()\n    break","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T15:03:20.971211Z","iopub.execute_input":"2025-05-14T15:03:20.971523Z","iopub.status.idle":"2025-05-14T15:03:21.947080Z","shell.execute_reply.started":"2025-05-14T15:03:20.971502Z","shell.execute_reply":"2025-05-14T15:03:21.946175Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Evaluation ","metadata":{}},{"cell_type":"markdown","source":"## Dice coefficient","metadata":{}},{"cell_type":"code","source":"\ndef dice_coefficient(pred_mask, true_mask, epsilon=1e-8):\n    pred_mask = (pred_mask > 0.5).float()\n    true_mask = true_mask.float()\n\n    intersection = (pred_mask * true_mask).sum(dim=(-2, -1))\n    union = pred_mask.sum(dim=(-2, -1)) + true_mask.sum(dim=(-2, -1))\n\n    dice = (2. * intersection + epsilon) / (union + epsilon)\n\n    return dice#.mean().item()  # return average over batch if batched\n\n\ndef evaluate(mdl, loader):\n    mdl.eval()\n    dice_coeff = []\n    for im, m, _ in loader:\n        out = mdl(im.to(device))\n        out = out.detach().to('cpu')\n\n        dice_coeff.append(dice_coefficient(out, m) )\n    \n        # plt.hist(out.reshape(-1))\n        # plt.show()\n    \n        # out[out > .5] = 1\n        # out[out < 1] = 0\n        # plt.imshow(out, cmap='gray')\n        # plt.show()\n        # break","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Delete Files","metadata":{}},{"cell_type":"code","source":"import shutil\nimport os\n\n# Check if the file exists and delete it\ndef delete_file(file_path):\n    if os.path.exists(file_path):\n        os.remove(file_path)\n        print(f\"Deleted: {file_path}\")\n    else:\n        print(\"File does not exist.\")\n        \n# Check if directory exists and delete it\ndef delete_dir(dir_path):\n    if os.path.exists(dir_path) and os.path.isdir(dir_path):\n        shutil.rmtree(dir_path)\n        print(f\"Deleted directory: {dir_path}\")\n    else:\n        print(\"Directory does not exist.\")\n\n\n# List all files and folders\ndef empty_dir(dir_path):\n    items = os.listdir(dir_path)\n    print(\"Contents to delete:\")\n    for item in items:\n        print(item)\n    \n    # Delete everything\n    for item in items:\n        item_path = os.path.join(dir_path, item)\n        if os.path.isfile(item_path) or os.path.islink(item_path):\n            delete_file(item_path)\n        elif os.path.isdir(item_path):\n            delete_dir(item_path)\n\n    print(f\"\\nDirectory '{dir_path}' is now empty.\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T15:00:14.514658Z","iopub.execute_input":"2025-05-14T15:00:14.514978Z","iopub.status.idle":"2025-05-14T15:00:14.521383Z","shell.execute_reply.started":"2025-05-14T15:00:14.514937Z","shell.execute_reply":"2025-05-14T15:00:14.520658Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"file_path = '/kaggle/working'\nempty_dir(file_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T16:52:37.713671Z","iopub.execute_input":"2025-05-14T16:52:37.713944Z","iopub.status.idle":"2025-05-14T16:52:37.811298Z","shell.execute_reply.started":"2025-05-14T16:52:37.713923Z","shell.execute_reply":"2025-05-14T16:52:37.810653Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nbest_model_wts = copy.deepcopy(trained_model.state_dict())\ntorch.save(best_model_wts, \"best_model_v2_0_resnet_tune.pth\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-14T16:52:55.258303Z","iopub.execute_input":"2025-05-14T16:52:55.258623Z","iopub.status.idle":"2025-05-14T16:52:55.926641Z","shell.execute_reply.started":"2025-05-14T16:52:55.258602Z","shell.execute_reply":"2025-05-14T16:52:55.926076Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}