{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Installing pre trained unet model and its dependancies","metadata":{}},{"cell_type":"code","source":"!pip install segmentation_models_pytorch\n!pip -q install cairosvg==2.5.2\n!pip -q install reportlab==3.5.65\n!pip -q install cssutils==2.2.0","metadata":{"execution":{"iopub.status.busy":"2023-06-03T10:17:16.767716Z","iopub.execute_input":"2023-06-03T10:17:16.768852Z","iopub.status.idle":"2023-06-03T10:18:41.421350Z","shell.execute_reply.started":"2023-06-03T10:17:16.768807Z","shell.execute_reply":"2023-06-03T10:18:41.420394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# imports and cuda","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nimport cv2\nfrom torch.utils.data import Dataset,DataLoader\nfrom torchvision import transforms\nimport torch\nfrom matplotlib import pyplot as plt\nimport segmentation_models_pytorch as smp\nfrom segmentation_models_pytorch import utils\nimport gc\nimport os\nimport enum\nimport pickle\n%matplotlib inline\ntorch.cuda.is_available()","metadata":{"execution":{"iopub.status.busy":"2023-06-03T10:18:41.424132Z","iopub.execute_input":"2023-06-03T10:18:41.424590Z","iopub.status.idle":"2023-06-03T10:18:47.425729Z","shell.execute_reply.started":"2023-06-03T10:18:41.424522Z","shell.execute_reply":"2023-06-03T10:18:47.424453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")","metadata":{"execution":{"iopub.status.busy":"2023-06-02T10:42:41.921473Z","iopub.execute_input":"2023-06-02T10:42:41.921881Z","iopub.status.idle":"2023-06-02T10:42:41.928575Z","shell.execute_reply.started":"2023-06-02T10:42:41.921849Z","shell.execute_reply":"2023-06-02T10:42:41.927202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# dirs","metadata":{}},{"cell_type":"code","source":"DATA_DIR = \"/kaggle/input/airbus-ship-detection/\"\nTEST_DIR = DATA_DIR + \"test_v2/\"\nTRAIN_DIR = DATA_DIR + \"train_v2/\"\nSAMPLE_CSV = DATA_DIR + \"sample_submission_v2.csv\"\nTRAIN_LABELS_CSV = DATA_DIR + \"train_ship_segmentations_v2.csv\"\nWORKING_DIR = \"/kaggle/working/\"","metadata":{"execution":{"iopub.status.busy":"2023-06-03T10:18:47.427176Z","iopub.execute_input":"2023-06-03T10:18:47.427896Z","iopub.status.idle":"2023-06-03T10:18:47.433095Z","shell.execute_reply.started":"2023-06-03T10:18:47.427854Z","shell.execute_reply":"2023-06-03T10:18:47.432168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# model constants","metadata":{}},{"cell_type":"code","source":"ENCODER = 'resnet18'\nENCODER_WEIGHTS = 'imagenet'\nCLASSES = ['ships']\nACTIVATION = 'sigmoid'\n#DEVICE = 'cuda'\nIOU_THRESHOLD = 0.5\nLEARNING_RATE = 0.0001\nENCODER_DEPTH = 5\nDECODER_USE_BATCH_NORM = True\n\nMODEL_FILENAME = 'pickle_model.eth'","metadata":{"execution":{"iopub.status.busy":"2023-06-03T10:18:47.435737Z","iopub.execute_input":"2023-06-03T10:18:47.436298Z","iopub.status.idle":"2023-06-03T10:18:47.448953Z","shell.execute_reply.started":"2023-06-03T10:18:47.436270Z","shell.execute_reply":"2023-06-03T10:18:47.447999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# hyperparamters","metadata":{}},{"cell_type":"code","source":"MAX_IMAGES_LEN_TRAIN = None\nNUM_EPOCH = 1\nNUM_WORKERS = 0\nSHUFFLE = True\nBATCH_SIZE = 32\nTHRESHOLD = 0.5","metadata":{"execution":{"iopub.status.busy":"2023-06-03T10:18:47.450514Z","iopub.execute_input":"2023-06-03T10:18:47.450850Z","iopub.status.idle":"2023-06-03T10:18:47.460013Z","shell.execute_reply.started":"2023-06-03T10:18:47.450823Z","shell.execute_reply":"2023-06-03T10:18:47.458909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# model","metadata":{}},{"cell_type":"code","source":"unet = smp.Unet(encoder_depth = ENCODER_DEPTH,\n    encoder_name=ENCODER, \n    encoder_weights=ENCODER_WEIGHTS,\n    decoder_use_batchnorm=DECODER_USE_BATCH_NORM,\n    classes=len(CLASSES), \n    activation=ACTIVATION,\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-03T10:18:47.461262Z","iopub.execute_input":"2023-06-03T10:18:47.461527Z","iopub.status.idle":"2023-06-03T10:18:48.327392Z","shell.execute_reply.started":"2023-06-03T10:18:47.461504Z","shell.execute_reply":"2023-06-03T10:18:48.326617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"model metrics,optimizer,loss, preprocessing","metadata":{}},{"cell_type":"code","source":"preprocessing_fn = smp.encoders.get_preprocessing_fn(ENCODER, pretrained=ENCODER_WEIGHTS)\nloss = utils.losses.DiceLoss()\nmetrics = [\n    utils.metrics.IoU(threshold=IOU_THRESHOLD),\n]\n\noptimizer = torch.optim.Adam([ \n    dict(params=unet.parameters(), lr=LEARNING_RATE),\n])","metadata":{"execution":{"iopub.status.busy":"2023-06-03T10:18:48.328654Z","iopub.execute_input":"2023-06-03T10:18:48.329245Z","iopub.status.idle":"2023-06-03T10:18:48.335084Z","shell.execute_reply.started":"2023-06-03T10:18:48.329215Z","shell.execute_reply":"2023-06-03T10:18:48.334172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Rle encode and decode for submission and processing csv data","metadata":{}},{"cell_type":"code","source":"# ref: https://www.kaggle.com/paulorzp/run-length-encode-and-decode\n\ndef 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 = np.concatenate([[0], pixels, [0]])\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    return ' '.join(str(x) for x in runs)\n\n\n\ndef rle_decode(mask_rle, shape=(768, 768)):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (height,width) of array to return\n    Returns numpy array, 1 - mask, 0 - background\n    '''\n    if not isinstance(mask_rle, str):\n        img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n        return img.reshape(shape).T\n\n    s = mask_rle.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    return img.reshape(shape).T","metadata":{"execution":{"iopub.status.busy":"2023-06-03T10:18:48.336210Z","iopub.execute_input":"2023-06-03T10:18:48.336472Z","iopub.status.idle":"2023-06-03T10:18:48.350497Z","shell.execute_reply.started":"2023-06-03T10:18:48.336449Z","shell.execute_reply":"2023-06-03T10:18:48.349574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Custom dataset","metadata":{}},{"cell_type":"code","source":"\nclass DatasetType(enum.Enum):\n    TRAIN = 0,\n    VALIDATION = 1,\n    TEST = 2\n\nclass ShipsDataset(Dataset):\n    \n    def __init__(self,\n                 images_pathes,\n                 images_ids,\n                 annotation_file=None, \n                 dataset_type:DatasetType=DatasetType.TRAIN,\n                 train_val_size=(80,20),\n                 input_shape=(224,224),\n                 image_shape=(768,768),\n                 transform=None,\n                 max_len=None):\n        \n        self.dataset_type = dataset_type\n        self.train_val_size = train_val_size\n        self.input_shape = input_shape\n        self.image_shape = image_shape\n        self.transform = transform\n        self.max_len = max_len\n        self.paths = images_pathes\n        self.images_ids = images_ids\n        if self.dataset_type == DatasetType.TRAIN or self.dataset_type == DatasetType.VALIDATION:\n            self.labels = pd.read_csv(annotation_file)\n            self.masks = self._get_masks()\n        \n    def _to_train_val(self,lst):\n        if self.dataset_type == DatasetType.TRAIN:\n            load = int(len(lst) * self.train_val_size[0] / 100)\n            lst = lst[:load]\n        if self.dataset_type is DatasetType.VALIDATION:\n            load = int(len(lst) * self.train_val_size[1] / 100)\n            lst = lst[-load:]\n        return lst\n    \n    def _get_masks(self):    \n        return self._to_train_val(self.labels[~self.labels['EncodedPixels'].isna()])\n    \n    def __len__(self):\n        return len(self._to_train_val(self.paths))\n    \n    def __getitem__(self, idx):\n        path = self.paths[idx]\n        ImageId = self.images_ids[idx]\n\n        image = cv2.imread(path)\n        image = cv2.resize(image,self.input_shape)\n        image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)\n        image = image.astype(np.float32)\n        if self.transform:\n            image = self.transform(image)\n            #image = image.numpy()\n            #image = image.transpose([2, 0, 1])\n    \n        if self.dataset_type == DatasetType.TEST:\n            image = preprocessing_fn(image)\n            image = image.transpose([2, 0, 1]).astype(np.float32)\n            return image,ImageId\n        \n        img_masks = self.labels.loc[self.labels['ImageId'] == ImageId, 'EncodedPixels'].tolist()\n        decode = None\n        all_masks = np.zeros(self.image_shape)\n        for img_mask in img_masks:\n            if isinstance(img_mask,str):   \n                decode = rle_decode(img_mask, self.image_shape)\n                all_masks += decode\n        all_masks = cv2.resize(all_masks, self.input_shape)\n        image = preprocessing_fn(image)\n        image = image.transpose([2, 0, 1]).astype(np.float32)\n        return  image, all_masks\n","metadata":{"execution":{"iopub.status.busy":"2023-06-03T10:18:48.351974Z","iopub.execute_input":"2023-06-03T10:18:48.352275Z","iopub.status.idle":"2023-06-03T10:18:48.368324Z","shell.execute_reply.started":"2023-06-03T10:18:48.352251Z","shell.execute_reply":"2023-06-03T10:18:48.367595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_PATHES = [i for i in os.walk(TRAIN_DIR)][0][2]","metadata":{"execution":{"iopub.status.busy":"2023-06-03T10:18:48.371244Z","iopub.execute_input":"2023-06-03T10:18:48.372006Z","iopub.status.idle":"2023-06-03T10:22:18.659824Z","shell.execute_reply.started":"2023-06-03T10:18:48.371977Z","shell.execute_reply":"2023-06-03T10:22:18.658754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_PATHES = [i for i in os.walk(TEST_DIR)][0][2]","metadata":{"execution":{"iopub.status.busy":"2023-06-03T10:22:18.661338Z","iopub.execute_input":"2023-06-03T10:22:18.661733Z","iopub.status.idle":"2023-06-03T10:22:35.675285Z","shell.execute_reply.started":"2023-06-03T10:22:18.661699Z","shell.execute_reply":"2023-06-03T10:22:35.674493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FULL_TRAIN_PATHES = [TRAIN_DIR + i for i in TRAIN_PATHES]\nFULL_TEST_PATHES = [TEST_DIR + i for i in TEST_PATHES]","metadata":{"execution":{"iopub.status.busy":"2023-06-03T10:22:35.676513Z","iopub.execute_input":"2023-06-03T10:22:35.677537Z","iopub.status.idle":"2023-06-03T10:22:35.723009Z","shell.execute_reply.started":"2023-06-03T10:22:35.677502Z","shell.execute_reply":"2023-06-03T10:22:35.721800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(TRAIN_PATHES)\nTRAIN_PATHES = TRAIN_PATHES[:50000]\nFULL_TRAIN_PATHES = FULL_TRAIN_PATHES[:50000]","metadata":{"execution":{"iopub.status.busy":"2023-06-03T10:26:17.313491Z","iopub.execute_input":"2023-06-03T10:26:17.314385Z","iopub.status.idle":"2023-06-03T10:26:17.320465Z","shell.execute_reply.started":"2023-06-03T10:26:17.314346Z","shell.execute_reply":"2023-06-03T10:26:17.319317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"","metadata":{}},{"cell_type":"code","source":"train_dataset = ShipsDataset(images_ids=TRAIN_PATHES,\n                             images_pathes=FULL_TRAIN_PATHES,\n                             annotation_file=TRAIN_LABELS_CSV, \n                             dataset_type=DatasetType.TRAIN,\n                             transform =None)","metadata":{"execution":{"iopub.status.busy":"2023-06-03T10:26:18.912689Z","iopub.execute_input":"2023-06-03T10:26:18.913625Z","iopub.status.idle":"2023-06-03T10:26:19.493426Z","shell.execute_reply.started":"2023-06-03T10:26:18.913576Z","shell.execute_reply":"2023-06-03T10:26:19.492576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_dataset = ShipsDataset(images_ids=TRAIN_PATHES,\n                           images_pathes=FULL_TRAIN_PATHES,\n                           annotation_file=TRAIN_LABELS_CSV, \n                           dataset_type=DatasetType.VALIDATION,\n                           transform =None)","metadata":{"execution":{"iopub.status.busy":"2023-06-03T10:26:20.336023Z","iopub.execute_input":"2023-06-03T10:26:20.336391Z","iopub.status.idle":"2023-06-03T10:26:20.921983Z","shell.execute_reply.started":"2023-06-03T10:26:20.336363Z","shell.execute_reply":"2023-06-03T10:26:20.921113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(train_dataset),len(val_dataset))","metadata":{"execution":{"iopub.status.busy":"2023-06-03T10:26:21.994203Z","iopub.execute_input":"2023-06-03T10:26:21.995121Z","iopub.status.idle":"2023-06-03T10:26:22.000759Z","shell.execute_reply.started":"2023-06-03T10:26:21.995084Z","shell.execute_reply":"2023-06-03T10:26:21.999923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader = torch.utils.data.DataLoader(train_dataset, \n                                           batch_size=BATCH_SIZE, \n                                           shuffle=SHUFFLE,\n                                           num_workers=NUM_WORKERS)\nval_loader = torch.utils.data.DataLoader(val_dataset,\n                                         batch_size=BATCH_SIZE,\n                                         shuffle=SHUFFLE, \n                                         num_workers=NUM_WORKERS)","metadata":{"execution":{"iopub.status.busy":"2023-06-03T10:26:23.384562Z","iopub.execute_input":"2023-06-03T10:26:23.386735Z","iopub.status.idle":"2023-06-03T10:26:23.424509Z","shell.execute_reply.started":"2023-06-03T10:26:23.386699Z","shell.execute_reply":"2023-06-03T10:26:23.423383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# checking correctness of data and mask in custom dataset","metadata":{}},{"cell_type":"code","source":"image,mask = train_dataset[10]\nfig,axes = plt.subplots(1,2,figsize=(10,10))\n\naxes[0].imshow(image.T,cmap=\"gray\")\naxes[1].imshow(mask.T,cmap=\"gray\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-03T10:22:37.628716Z","iopub.execute_input":"2023-06-03T10:22:37.629060Z","iopub.status.idle":"2023-06-03T10:22:38.115831Z","shell.execute_reply.started":"2023-06-03T10:22:37.629025Z","shell.execute_reply":"2023-06-03T10:22:38.114777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_epoch = smp.utils.train.TrainEpoch(\n    unet, \n    loss=loss, \n    metrics=metrics, \n    optimizer=optimizer,\n    #device=DEVICE,\n    verbose=True,\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-06-03T10:26:28.736009Z","iopub.execute_input":"2023-06-03T10:26:28.737121Z","iopub.status.idle":"2023-06-03T10:26:28.744684Z","shell.execute_reply.started":"2023-06-03T10:26:28.737073Z","shell.execute_reply":"2023-06-03T10:26:28.743743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_epoch = smp.utils.train.ValidEpoch(\n    unet,\n    #device=DEVICE,\n    loss=loss,\n    metrics=metrics,\n    verbose=True\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-03T10:26:30.216432Z","iopub.execute_input":"2023-06-03T10:26:30.216859Z","iopub.status.idle":"2023-06-03T10:26:30.223283Z","shell.execute_reply.started":"2023-06-03T10:26:30.216827Z","shell.execute_reply":"2023-06-03T10:26:30.222466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training with validation for iou score of at least 0.5","metadata":{}},{"cell_type":"code","source":"last_iou_score = 0\n\n\nsaved_model = [i for i in os.walk(WORKING_DIR)][0][2][1:]\nif len(saved_model) > 0:\n    unet.load_state_dict(torch.load(WORKING_DIR + saved_model[0]))\ntry:\n    for i in range(NUM_EPOCH):\n        print('\\nTrain Epoch: {}'.format(i))\n        train_logs = train_epoch.run(train_loader)\n        val_logs = valid_epoch.run(val_loader)\n        val_iou = val_logs['iou_score']\n        if THRESHOLD < val_iou:\n            torch.save(unet.state_dict(), WORKING_DIR + \"airbus-ship-unet-model.eth\")\n            last_iou_score = val_iou\n            break\nexcept KeyboardInterrupt:\n    torch.save(unet.state_dict(), WORKING_DIR + \"airbus-ship-unet-model.eth\")","metadata":{"execution":{"iopub.status.busy":"2023-06-03T10:26:32.071286Z","iopub.execute_input":"2023-06-03T10:26:32.071682Z","iopub.status.idle":"2023-06-03T15:04:25.506337Z","shell.execute_reply.started":"2023-06-03T10:26:32.071649Z","shell.execute_reply":"2023-06-03T15:04:25.503181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualing Validation Data","metadata":{}},{"cell_type":"code","source":"val_predications = []\nnum_val_samples = 6\nwith torch.no_grad():\n    for i,(images,masks) in enumerate(val_loader):\n#         images = images.to(device)\n        output = unet(images)\n        for idx in range(BATCH_SIZE):\n            image = images[idx]\n            mask = masks[idx]\n            pred_mask = output[idx]\n            pred_mask[pred_mask >= THRESHOLD] = 1\n            pred_mask[pred_mask < THRESHOLD] = 0\n            val_predications.append((image.cpu(),mask.cpu(),pred_mask.cpu()))\n        break\nval_predications =val_predications[num_val_samples:]\nfig,axs = plt.subplots(num_val_samples,3,figsize=(8,25))\nfor i in range(num_val_samples):\n    axs[i,0].imshow(val_predications[i][0].T)\n    axs[i,1].imshow(val_predications[i][1].T,cmap=\"gray\")\n    axs[i,2].imshow(val_predications[i][2].T,cmap=\"gray\")\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-06-03T15:04:25.513672Z","iopub.execute_input":"2023-06-03T15:04:25.514725Z","iopub.status.idle":"2023-06-03T15:04:31.669724Z","shell.execute_reply.started":"2023-06-03T15:04:25.514532Z","shell.execute_reply":"2023-06-03T15:04:31.668851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test and submission","metadata":{}},{"cell_type":"code","source":"test_num_samples = 6\ntest_sample_names = []\ntest_sample_predictions = []","metadata":{"execution":{"iopub.status.busy":"2023-06-03T15:59:58.250947Z","iopub.execute_input":"2023-06-03T15:59:58.251402Z","iopub.status.idle":"2023-06-03T15:59:58.256291Z","shell.execute_reply.started":"2023-06-03T15:59:58.251369Z","shell.execute_reply":"2023-06-03T15:59:58.255256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = ShipsDataset(images_ids=TEST_PATHES,images_pathes=FULL_TEST_PATHES, dataset_type=DatasetType.TEST,transform =None)\ntest_loader = torch.utils.data.DataLoader(test_dataset,batch_size=BATCH_SIZE, shuffle=SHUFFLE, num_workers=NUM_WORKERS)\nprint(len(test_dataset))","metadata":{"execution":{"iopub.status.busy":"2023-06-03T16:00:03.787960Z","iopub.execute_input":"2023-06-03T16:00:03.788392Z","iopub.status.idle":"2023-06-03T16:00:03.794690Z","shell.execute_reply.started":"2023-06-03T16:00:03.788356Z","shell.execute_reply":"2023-06-03T16:00:03.793828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predications = []\ntry:\n    with torch.no_grad():\n        for i,(images,ImagesIds) in enumerate(test_loader):\n            #images = images.to(device)\n            output = unet(images)\n            shape = output.shape\n            batch_size = shape[0]\n            for idx in range(batch_size):\n                try:\n                    pred_mask = output[idx]\n                except Exception as e:\n                    print(output.shape)\n                    print(idx)\n                    print(e)\n                pred_mask = pred_mask.cpu().squeeze().numpy()\n                resized_pred_mask = cv2.resize(pred_mask, (768, 768))\n                resized_pred_mask[resized_pred_mask >= THRESHOLD] = 1\n                resized_pred_mask[resized_pred_mask < THRESHOLD] = 0\n#                 print(i, idx,resized_pred_mask.shape,np.max(resized_pred_mask))\n                encoded_pixels = rle_encode(resized_pred_mask)\n                if encoded_pixels != \"\":\n                    predications.append({\"ImageId\":ImagesIds[idx],\"EncodedPixels\":encoded_pixels})\n                if i == 0:\n                    test_sample_names.append(ImagesIds[idx])\n                    test_sample_predictions.append(resized_pred_mask)\nexcept KeyboardInterrupt:\n    pass","metadata":{"execution":{"iopub.status.busy":"2023-06-03T16:45:00.153747Z","iopub.execute_input":"2023-06-03T16:45:00.154170Z","iopub.status.idle":"2023-06-03T16:45:08.467741Z","shell.execute_reply.started":"2023-06-03T16:45:00.154136Z","shell.execute_reply":"2023-06-03T16:45:08.466821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test prediction","metadata":{}},{"cell_type":"code","source":"sample_images = [cv2.imread(TEST_DIR + test_sample_names[i]) for i in range(test_num_samples)]\ntest_pred = test_sample_predictions[:test_num_samples]","metadata":{"execution":{"iopub.status.busy":"2023-06-03T15:32:01.331725Z","iopub.status.idle":"2023-06-03T15:32:01.332280Z","shell.execute_reply.started":"2023-06-03T15:32:01.332008Z","shell.execute_reply":"2023-06-03T15:32:01.332034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,axs = plt.subplots(test_num_samples,2,figsize=(8,25))\nfor i in range(test_num_samples):\n    axs[i,0].imshow(sample_images[i])\n    axs[i,1].imshow(test_pred[i],cmap=\"gray\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-03T15:32:01.333589Z","iopub.status.idle":"2023-06-03T15:32:01.334284Z","shell.execute_reply.started":"2023-06-03T15:32:01.334090Z","shell.execute_reply":"2023-06-03T15:32:01.334110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Creating submission file","metadata":{}},{"cell_type":"code","source":"submission = pd.DataFrame(predications)\nprint(len(submission))\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-03T15:32:01.335742Z","iopub.status.idle":"2023-06-03T15:32:01.337045Z","shell.execute_reply.started":"2023-06-03T15:32:01.336768Z","shell.execute_reply":"2023-06-03T15:32:01.336794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-06-02T08:48:01.924915Z","iopub.execute_input":"2023-06-02T08:48:01.925847Z","iopub.status.idle":"2023-06-02T08:48:01.941040Z","shell.execute_reply.started":"2023-06-02T08:48:01.925814Z","shell.execute_reply":"2023-06-02T08:48:01.940151Z"},"trusted":true},"execution_count":null,"outputs":[]}]}