{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":6927,"databundleVersionId":45059,"sourceType":"competition"}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport os\nimport torch\nimport torch.nn.functional as F\nimport torchvision.transforms.functional as TF\nimport matplotlib.pyplot as plt\nfrom torch import nn\nimport cv2 as cv\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision import transforms\nfrom torch.utils.data import random_split\nfrom tqdm.auto import tqdm\nimport imageio","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-25T18:33:52.400083Z","iopub.execute_input":"2023-12-25T18:33:52.400452Z","iopub.status.idle":"2023-12-25T18:33:56.527758Z","shell.execute_reply.started":"2023-12-25T18:33:52.400421Z","shell.execute_reply":"2023-12-25T18:33:56.526914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n# Unzip Files\nwith zipfile.ZipFile('/kaggle/input/carvana-image-masking-challenge/train.zip', 'r') as zip_ref:\n    zip_ref.extractall('/kaggle/working/')\n\nwith zipfile.ZipFile('/kaggle/input/carvana-image-masking-challenge/train_masks.zip', 'r') as zip_ref:\n    zip_ref.extractall('/kaggle/working/')","metadata":{"execution":{"iopub.status.busy":"2023-12-25T18:33:56.529263Z","iopub.execute_input":"2023-12-25T18:33:56.529672Z","iopub.status.idle":"2023-12-25T18:34:04.832921Z","shell.execute_reply.started":"2023-12-25T18:33:56.529643Z","shell.execute_reply":"2023-12-25T18:34:04.831881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('/kaggle/working')","metadata":{"execution":{"iopub.status.busy":"2023-12-25T18:34:04.834545Z","iopub.execute_input":"2023-12-25T18:34:04.834920Z","iopub.status.idle":"2023-12-25T18:34:04.844375Z","shell.execute_reply.started":"2023-12-25T18:34:04.834875Z","shell.execute_reply":"2023-12-25T18:34:04.843345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"frames_path = '/kaggle/working/train'\nmasks_path = '/kaggle/working/train_masks'\nlen(os.listdir(frames_path)), len(os.listdir(masks_path))","metadata":{"execution":{"iopub.status.busy":"2023-12-25T18:34:04.847832Z","iopub.execute_input":"2023-12-25T18:34:04.848622Z","iopub.status.idle":"2023-12-25T18:34:04.862499Z","shell.execute_reply.started":"2023-12-25T18:34:04.848596Z","shell.execute_reply":"2023-12-25T18:34:04.861728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MakeDataset(Dataset):\n    def __init__(self, frames_path, masks_path, transform=None):\n        self.frames_path = frames_path\n        self.masks_path = masks_path\n        self.transform = transform\n\n        self.frame_names = sorted(os.listdir(frames_path))\n        self.mask_names = sorted(os.listdir(masks_path))\n\n    def __len__(self):\n        return len(self.frame_names)\n\n    def __getitem__(self, idx):\n        frame_path = os.path.join(self.frames_path, self.frame_names[idx])\n        mask_path = os.path.join(self.masks_path, self.mask_names[idx])\n\n        frame = cv.imread(frame_path)\n        gif_reader = imageio.get_reader(mask_path)\n        single_frame = gif_reader.get_data(0)\n        mask = cv.cvtColor(single_frame, cv.COLOR_RGB2BGR)\n        mask = mask[:,:,0]\n        mask[mask==255.0]=1.0\n\n        frame = (frame).astype('float32')\n        mask = (mask).astype('float32')\n\n        if (self.transform):\n            augmented = self.transform(image=frame, mask=mask)\n            frame = augmented['image']\n            mask = augmented['mask']\n\n        width = 256\n        height = 256\n        dimensions = (width, height)\n\n        frame = cv.resize(frame, dimensions, interpolation=cv.INTER_AREA)\n        mask = cv.resize(mask, dimensions, interpolation=cv.INTER_AREA)\n\n        frame = transforms.ToTensor()(frame)\n        mask = transforms.ToTensor()(mask)\n\n        return frame, mask","metadata":{"execution":{"iopub.status.busy":"2023-12-25T18:34:04.863551Z","iopub.execute_input":"2023-12-25T18:34:04.864180Z","iopub.status.idle":"2023-12-25T18:34:04.875955Z","shell.execute_reply.started":"2023-12-25T18:34:04.864154Z","shell.execute_reply":"2023-12-25T18:34:04.875038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = MakeDataset(frames_path=frames_path,\n                      masks_path=masks_path)","metadata":{"execution":{"iopub.status.busy":"2023-12-25T18:34:04.877355Z","iopub.execute_input":"2023-12-25T18:34:04.878131Z","iopub.status.idle":"2023-12-25T18:34:04.895745Z","shell.execute_reply.started":"2023-12-25T18:34:04.878098Z","shell.execute_reply":"2023-12-25T18:34:04.894897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"split = random_split(dataset, [0.8,0.2])","metadata":{"execution":{"iopub.status.busy":"2023-12-25T18:34:04.896966Z","iopub.execute_input":"2023-12-25T18:34:04.897782Z","iopub.status.idle":"2023-12-25T18:34:04.921374Z","shell.execute_reply.started":"2023-12-25T18:34:04.897748Z","shell.execute_reply":"2023-12-25T18:34:04.920606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 4\ntrain_dataloader = DataLoader(split[0],\n                        batch_size=BATCH_SIZE,\n                        shuffle=True)\nval_dataloader = DataLoader(split[1],\n                        batch_size=BATCH_SIZE,\n                        shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-12-25T18:34:04.922825Z","iopub.execute_input":"2023-12-25T18:34:04.923168Z","iopub.status.idle":"2023-12-25T18:34:04.928439Z","shell.execute_reply.started":"2023-12-25T18:34:04.923137Z","shell.execute_reply":"2023-12-25T18:34:04.927464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_features_batch, train_labels_batch = next(iter(train_dataloader))\n\nprint(train_features_batch.shape, train_labels_batch.shape)\nplt.subplot(1,2,1)\nplt.imshow(train_features_batch[0,0], cmap='gray')\nplt.subplot(1,2,2)\nplt.imshow(train_labels_batch[0,0], cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2023-12-25T18:34:04.930061Z","iopub.execute_input":"2023-12-25T18:34:04.930446Z","iopub.status.idle":"2023-12-25T18:34:05.726837Z","shell.execute_reply.started":"2023-12-25T18:34:04.930392Z","shell.execute_reply":"2023-12-25T18:34:05.725877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_features_batch.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-25T18:34:05.731068Z","iopub.execute_input":"2023-12-25T18:34:05.731838Z","iopub.status.idle":"2023-12-25T18:34:05.737500Z","shell.execute_reply.started":"2023-12-25T18:34:05.731797Z","shell.execute_reply":"2023-12-25T18:34:05.736615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DoubleConv(nn.Module):\n    def __init__(self, in_channels, out_channels):\n        super(DoubleConv, self).__init__()\n        self.block = nn.Sequential(\n            nn.Conv2d(in_channels, out_channels, kernel_size = 3, stride = 1, padding = 1, bias=False),\n            nn.BatchNorm2d(out_channels),\n            nn.ReLU(inplace=True),\n\n            nn.Conv2d(out_channels, out_channels, kernel_size = 3,stride = 1, padding = 1, bias=False),\n            nn.BatchNorm2d(out_channels),\n            nn.ReLU(inplace=True)\n        )\n        \n    def forward(self, x):\n        return self.block(x)","metadata":{"execution":{"iopub.status.busy":"2023-12-25T18:34:05.738689Z","iopub.execute_input":"2023-12-25T18:34:05.739075Z","iopub.status.idle":"2023-12-25T18:34:05.747789Z","shell.execute_reply.started":"2023-12-25T18:34:05.739046Z","shell.execute_reply":"2023-12-25T18:34:05.746666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torchvision.transforms as T\ndef copy_and_crop(down_layer, up_layer):\n    b, ch, h, w = up_layer.shape\n    crop = T.CenterCrop((h, w))(down_layer)\n    return crop","metadata":{"execution":{"iopub.status.busy":"2023-12-25T18:34:05.748980Z","iopub.execute_input":"2023-12-25T18:34:05.749339Z","iopub.status.idle":"2023-12-25T18:34:05.757320Z","shell.execute_reply.started":"2023-12-25T18:34:05.749307Z","shell.execute_reply":"2023-12-25T18:34:05.756384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class UNet(nn.Module):\n    def __init__(self, in_channels, out_channels):\n        super(UNet, self).__init__()\n        \n        self.encoder = nn.ModuleList([\n            DoubleConv(in_channels, 64),\n            DoubleConv(64, 128),\n            DoubleConv(128, 256),\n            DoubleConv(256, 512)\n        ])\n        \n        self.pool = nn.MaxPool2d(kernel_size=2, stride=2)\n        self.bottle_neck = DoubleConv(512, 1024)\n        \n        self.up_samples = nn.ModuleList([\n            nn.ConvTranspose2d(1024, 512, kernel_size=2, stride=2),\n            nn.ConvTranspose2d(512, 256, kernel_size=2, stride=2),\n            nn.ConvTranspose2d(256, 128, kernel_size=2, stride=2),\n            nn.ConvTranspose2d(128, 64, kernel_size=2, stride=2)\n        ])\n        \n        self.decoder = nn.ModuleList([\n            DoubleConv(1024, 512),\n            DoubleConv(512, 256),\n            DoubleConv(256, 128),\n            DoubleConv(128, 64)\n        ])\n        \n        self.final_layer = nn.Conv2d(64, out_channels, 1, 1)\n        \n    def forward(self, x):\n        skip_connections = []\n        \n        for layer in self.encoder:\n            x = layer(x)\n            skip_connections.append(x)\n            x = self.pool(x)\n        \n        x = self.bottle_neck(x)\n        \n        for ind, layer in enumerate(self.decoder):\n            x = self.up_samples[ind](x)\n            y = copy_and_crop(skip_connections.pop(), x)\n            x = layer(torch.cat([y, x], dim=1))\n        \n        x = self.final_layer(x)\n        \n        return x","metadata":{"execution":{"iopub.status.busy":"2023-12-25T18:34:05.758462Z","iopub.execute_input":"2023-12-25T18:34:05.758749Z","iopub.status.idle":"2023-12-25T18:34:05.770011Z","shell.execute_reply.started":"2023-12-25T18:34:05.758726Z","shell.execute_reply":"2023-12-25T18:34:05.769029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def dice_score(preds, targets):\n    preds = F.sigmoid(preds)\n    preds = (preds > 0.5).float()\n    score = (2. * (preds * targets).sum()) / (preds + targets).sum()\n    return torch.mean(score).item()","metadata":{"execution":{"iopub.status.busy":"2023-12-25T18:34:05.771299Z","iopub.execute_input":"2023-12-25T18:34:05.771994Z","iopub.status.idle":"2023-12-25T18:34:05.782315Z","shell.execute_reply.started":"2023-12-25T18:34:05.771962Z","shell.execute_reply":"2023-12-25T18:34:05.781449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = 'cuda' if torch.cuda.is_available() else 'cpu'\nunet_model = UNet(3,1).to(device)\nif torch.cuda.device_count() > 1:\n    unet_model = nn.DataParallel(unet_model, device_ids=[i for i in range(torch.cuda.device_count())])\nloss_fn = nn.BCEWithLogitsLoss()\noptimizer = torch.optim.Adam(params=unet_model.parameters(),\n                            lr=1e-3)","metadata":{"execution":{"iopub.status.busy":"2023-12-25T18:34:18.113778Z","iopub.execute_input":"2023-12-25T18:34:18.114090Z","iopub.status.idle":"2023-12-25T18:34:18.395666Z","shell.execute_reply.started":"2023-12-25T18:34:18.114064Z","shell.execute_reply":"2023-12-25T18:34:18.394694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 20\nfor epoch in tqdm(range(epochs)):\n    dice = 0\n    val_dice = 0\n    for batch, (X, y) in tqdm(enumerate(train_dataloader)):\n        unet_model.train()\n        X = X.to(device)\n        y = y.to(device)\n\n        y_pred = unet_model(X).to(device)\n\n\n        score = dice_score(y_pred, y)\n        loss = loss_fn(y_pred, y)\n        dice += score\n\n        optimizer.zero_grad()\n\n        loss.backward()\n\n        optimizer.step()\n        \n    unet_model.eval()  \n    print('Train Finished | Proceeding to test')\n    with torch.inference_mode():\n        for(X_val,y_val) in val_dataloader:\n            X_val = X_val.to(device)\n            y_val = y_val.to(device)\n            y_val_pred = unet_model(X_val)\n            val_dice+=dice_score(y_val_pred,y_val) \n\n    dice/=(len(train_dataloader))\n    val_dice/=len(val_dataloader)\n    \n    print(f'Epoch: {epoch+1} | Train Dice Score: {dice} | Val Dice Score: {val_dice}')","metadata":{"execution":{"iopub.status.busy":"2023-12-25T18:34:23.170120Z","iopub.execute_input":"2023-12-25T18:34:23.170492Z","iopub.status.idle":"2023-12-25T21:11:12.536276Z","shell.execute_reply.started":"2023-12-25T18:34:23.170462Z","shell.execute_reply":"2023-12-25T21:11:12.535330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with torch.inference_mode():\n    X_test, y_test = next(iter(val_dataloader))\n    X_test = X_test.to(device)\n    y_pred = unet_model(X_test)\n    y_pred = torch.nn.functional.sigmoid(y_pred)\n    X_test = X_test.to('cpu').numpy()\n    y_test = y_test.numpy()\n    y_pred = y_pred.to('cpu').numpy()","metadata":{"execution":{"iopub.status.busy":"2023-12-25T21:22:44.494105Z","iopub.execute_input":"2023-12-25T21:22:44.494527Z","iopub.status.idle":"2023-12-25T21:22:44.856812Z","shell.execute_reply.started":"2023-12-25T21:22:44.494495Z","shell.execute_reply":"2023-12-25T21:22:44.855790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.subplot(1,2,1)\nplt.imshow(y_test[0,0], cmap='gray')\nplt.subplot(1,2,2)\nplt.imshow(y_pred[0,0], cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2023-12-25T21:22:46.623677Z","iopub.execute_input":"2023-12-25T21:22:46.624045Z","iopub.status.idle":"2023-12-25T21:22:47.009301Z","shell.execute_reply.started":"2023-12-25T21:22:46.624017Z","shell.execute_reply":"2023-12-25T21:22:47.008382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(y_test[0][0], cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2023-12-25T21:11:12.576383Z","iopub.status.idle":"2023-12-25T21:11:12.576771Z","shell.execute_reply.started":"2023-12-25T21:11:12.576586Z","shell.execute_reply":"2023-12-25T21:11:12.576610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_features_batch, train_labels_batch = next(iter(train_dataloader))\n\nprint(train_features_batch.shape, train_labels_batch.shape)\nplt.subplot(1,2,1)\nplt.imshow(train_features_batch[0,0], cmap='gray')\nplt.subplot(1,2,2)\nplt.imshow(train_labels_batch[0,0], cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2023-12-25T21:11:12.577764Z","iopub.status.idle":"2023-12-25T21:11:12.578171Z","shell.execute_reply.started":"2023-12-25T21:11:12.577959Z","shell.execute_reply":"2023-12-25T21:11:12.577981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with zipfile.ZipFile('/kaggle/input/carvana-image-masking-challenge/test.zip', 'r') as zip_ref:\n    zip_ref.extractall('/kaggle/working/')","metadata":{"execution":{"iopub.status.busy":"2023-12-25T21:27:40.224541Z","iopub.execute_input":"2023-12-25T21:27:40.224934Z","iopub.status.idle":"2023-12-25T21:30:29.587186Z","shell.execute_reply.started":"2023-12-25T21:27:40.224905Z","shell.execute_reply":"2023-12-25T21:30:29.586144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CarvanaTestDataset(Dataset):\n    def __init__(self, image_dir, transform=None):\n        self.image_dir = image_dir\n        self.transform = transform\n        self.images = sorted(os.listdir(image_dir))\n\n    def __len__(self):\n        return len(self.images)\n\n    def __getitem__(self, index):\n        img_name = self.images[index]\n        img_path = os.path.join(self.image_dir, self.images[index])\n        \n        image = cv.imread(img_path)[0]\n        image = cv.cvtColor(image, cv.COLOR_GRAY2BGR)\n        width = 256\n        height = 256\n        dimensions = (width, height)\n\n        image = cv.resize(image, dimensions, interpolation=cv.INTER_AREA)\n        image = image.astype('float32')\n        \n        image = transforms.ToTensor()(image)\n\n        if self.transform is not None:\n            augmentations = self.transform(image=image)\n            image = augmentations['image']\n\n        return img_name, image","metadata":{"execution":{"iopub.status.busy":"2023-12-25T21:46:21.755423Z","iopub.execute_input":"2023-12-25T21:46:21.756174Z","iopub.status.idle":"2023-12-25T21:46:21.764687Z","shell.execute_reply.started":"2023-12-25T21:46:21.756144Z","shell.execute_reply":"2023-12-25T21:46:21.763699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_DIR = '/kaggle/working/test'\ntest_set = CarvanaTestDataset(image_dir=TEST_DIR)   ","metadata":{"execution":{"iopub.status.busy":"2023-12-25T21:46:22.853722Z","iopub.execute_input":"2023-12-25T21:46:22.854079Z","iopub.status.idle":"2023-12-25T21:46:22.963965Z","shell.execute_reply.started":"2023-12-25T21:46:22.854049Z","shell.execute_reply":"2023-12-25T21:46:22.963020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loader = DataLoader(test_set, batch_size=BATCH_SIZE, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-12-25T21:46:23.823799Z","iopub.execute_input":"2023-12-25T21:46:23.824159Z","iopub.status.idle":"2023-12-25T21:46:23.834216Z","shell.execute_reply.started":"2023-12-25T21:46:23.824129Z","shell.execute_reply":"2023-12-25T21:46:23.833185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = next(iter(test_loader))[1]\nimg = img.to(device)\nmask = unet_model(img)\nmask = torch.sigmoid(mask)\nmask = (mask > 0.5).float() \n\na = img[0].to('cpu').numpy()\nb = mask[0].to('cpu').numpy()\n# a = np.transpose(a[0], (1, 2, 0))\nmasked = cv.bitwise_and(a,b)\nplt.imshow(a[0])","metadata":{"execution":{"iopub.status.busy":"2023-12-25T22:14:40.953794Z","iopub.execute_input":"2023-12-25T22:14:40.954167Z","iopub.status.idle":"2023-12-25T22:14:41.388461Z","shell.execute_reply.started":"2023-12-25T22:14:40.954136Z","shell.execute_reply":"2023-12-25T22:14:41.387439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_encode(img):\n    '''\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    '''\n    pixels = img.flatten()\n    pixels[0] = 0\n    pixels[-1] = 0\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 2\n    runs[1::2] -= runs[:-1:2]\n    \n    return ' '.join(str(x) for x in runs)","metadata":{"execution":{"iopub.status.busy":"2023-12-25T21:46:54.014444Z","iopub.execute_input":"2023-12-25T21:46:54.015291Z","iopub.status.idle":"2023-12-25T21:46:54.021379Z","shell.execute_reply.started":"2023-12-25T21:46:54.015256Z","shell.execute_reply":"2023-12-25T21:46:54.020453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pathlib import Path\n\ndef save_model(model, save_dir, model_name):\n    directory_path = Path(save_dir)\n\n    if not directory_path.exists():\n        directory_path.mkdir(parents=True, exist_ok=True)\n        print(f'Directory {directory_path} did not exist, so created')\n    path = save_dir + '/' + model_name\n    torch.save(model,path)\n    print('Model saved successfully')","metadata":{"execution":{"iopub.status.busy":"2023-12-25T21:55:42.914034Z","iopub.execute_input":"2023-12-25T21:55:42.914762Z","iopub.status.idle":"2023-12-25T21:55:42.920934Z","shell.execute_reply.started":"2023-12-25T21:55:42.914723Z","shell.execute_reply":"2023-12-25T21:55:42.919606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"save_model(unet_model, '/kaggle/working', 'unet_carvana1')","metadata":{"execution":{"iopub.status.busy":"2023-12-25T21:58:22.594367Z","iopub.execute_input":"2023-12-25T21:58:22.595640Z","iopub.status.idle":"2023-12-25T21:58:22.771869Z","shell.execute_reply.started":"2023-12-25T21:58:22.595585Z","shell.execute_reply":"2023-12-25T21:58:22.770987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unet_model.eval()\n\nall_predictions = []\nfor img_names, x in tqdm(test_loader):\n    x = x.to(device)\n    with torch.inference_mode():\n        preds = torch.sigmoid(unet_model(x))\n        preds = (preds > 0.5).float()   \n    preds = TF.resize(preds, size=(1280, 1918), interpolation=TF.InterpolationMode.NEAREST)\n    \n    for idx in range(len(img_names)):\n        encoding = rle_encode(preds[idx].squeeze().cpu())\n        all_predictions.append([img_names[idx], encoding])","metadata":{"execution":{"iopub.status.busy":"2023-12-25T21:46:54.943799Z","iopub.execute_input":"2023-12-25T21:46:54.944515Z","iopub.status.idle":"2023-12-25T21:49:29.277851Z","shell.execute_reply.started":"2023-12-25T21:46:54.944482Z","shell.execute_reply":"2023-12-25T21:49:29.276573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}