{"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":"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)\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","execution":{"iopub.status.busy":"2021-12-02T05:05:09.143874Z","iopub.execute_input":"2021-12-02T05:05:09.144455Z","iopub.status.idle":"2021-12-02T05:05:11.167313Z","shell.execute_reply.started":"2021-12-02T05:05:09.144417Z","shell.execute_reply":"2021-12-02T05:05:11.166594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install torchsummary\nimport torch\nimport torch.nn as nn\nimport albumentations as A\nimport torch.optim as optim\nimport pandas as pd\nimport os\nimport glob\nimport numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\nfrom torch.utils.data import Dataset,DataLoader\nimport random\n# import torchsummary\nfrom sklearn.metrics import f1_score\nfrom tqdm import tqdm\nimport skimage.morphology","metadata":{"execution":{"iopub.status.busy":"2021-12-02T05:05:11.168734Z","iopub.execute_input":"2021-12-02T05:05:11.169204Z","iopub.status.idle":"2021-12-02T05:05:14.747616Z","shell.execute_reply.started":"2021-12-02T05:05:11.169163Z","shell.execute_reply":"2021-12-02T05:05:14.746910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# hyperparameters\nPATH = \"/kaggle/input/sartorius-cell-instance-segmentation\"\nIMAGE_HEIGHT = 520\nIMAGE_WIDTH = 704\nIMAGE_NEW_HEIGHT = 352\nIMAGE_NEW_WIDTH = 352\nLEARNING_RATE = 0.01\nEPOCHS = 250\nBATCH_SIZE = 16\nDEVICE = \"cuda\" if torch.cuda.is_available() else \"cpu\"","metadata":{"execution":{"iopub.status.busy":"2021-12-02T05:05:14.751797Z","iopub.execute_input":"2021-12-02T05:05:14.754116Z","iopub.status.idle":"2021-12-02T05:05:14.805250Z","shell.execute_reply.started":"2021-12-02T05:05:14.754066Z","shell.execute_reply":"2021-12-02T05:05:14.804499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#loading data\ndf = pd.read_csv(os.path.join(PATH, \"train.csv\"))\ndf","metadata":{"execution":{"iopub.status.busy":"2021-12-02T05:05:14.808716Z","iopub.execute_input":"2021-12-02T05:05:14.814194Z","iopub.status.idle":"2021-12-02T05:05:15.371170Z","shell.execute_reply.started":"2021-12-02T05:05:14.814151Z","shell.execute_reply":"2021-12-02T05:05:15.370452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = glob.glob(os.path.join(PATH , \"train/*\" ))\ntest_images = glob.glob(os.path.join(PATH,\"test/*\"))\nrandom.shuffle(images)\ntrain_images = images[:int(len(images)*0.95)]\nval_images = images[int(len(images)*0.95) : ]\n    ","metadata":{"execution":{"iopub.status.busy":"2021-12-02T05:05:15.372496Z","iopub.execute_input":"2021-12-02T05:05:15.372906Z","iopub.status.idle":"2021-12-02T05:05:15.382716Z","shell.execute_reply.started":"2021-12-02T05:05:15.372868Z","shell.execute_reply":"2021-12-02T05:05:15.382072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show(image,mask):\n    if(type(image) == torch.Tensor):\n        image= image.permute(1,2,0)\n        mask = mask.permute(1,2,0)\n    plt.figure(figsize=(40,40))\n    plt.subplot(1,2,1)\n    plt.imshow(image)\n    plt.subplot(1,2,2)\n    plt.imshow(mask)\n    plt.figure(figsize=(40,40))\n    plt.imshow(image)\n    plt.imshow(mask,alpha = 0.2)","metadata":{"execution":{"iopub.status.busy":"2021-12-02T05:05:15.384186Z","iopub.execute_input":"2021-12-02T05:05:15.384463Z","iopub.status.idle":"2021-12-02T05:05:15.391684Z","shell.execute_reply.started":"2021-12-02T05:05:15.384425Z","shell.execute_reply":"2021-12-02T05:05:15.390791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = A.Compose(\n    [\n    A.Resize(height = IMAGE_NEW_HEIGHT , width = IMAGE_NEW_WIDTH),\n    A.Normalize(mean=(0.485, 0.456, 0.406), std= (0.229, 0.224, 0.225), p=1),\n    A.Rotate(limit=35, p=1.0),\n    A.HorizontalFlip(p=0.5),\n    A.VerticalFlip(p=0.1),\n     ]\n)\ntransform_test = A.Compose(\n    [\n    A.Resize(height = 512 , width = 720),\n    A.Normalize(mean=(0.485, 0.456, 0.406), std= (0.229, 0.224, 0.225), p=1),\n     ]\n)\ntransform_test_reverse = A.Compose(\n    [\n    A.Resize(height = IMAGE_HEIGHT , width = IMAGE_WIDTH),\n    ]\n)","metadata":{"execution":{"iopub.status.busy":"2021-12-02T05:05:15.393247Z","iopub.execute_input":"2021-12-02T05:05:15.393599Z","iopub.status.idle":"2021-12-02T05:05:15.404240Z","shell.execute_reply.started":"2021-12-02T05:05:15.393561Z","shell.execute_reply":"2021-12-02T05:05:15.403496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Custom_Dataset(Dataset):\n    \n    def __init__(self,df,images,transform=None,train = False):\n        \n        self.images =  images\n        self.df = df\n        self.transform = transform\n        self.train = train\n    \n    def __len__(self):\n        return len(self.images)\n    \n    def __getitem__(self,index):\n        \n        path = self.images[index]    \n        img = cv2.imread(path)\n        img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)\n        self.image_id = self.images[index].split('/')[-1].split(\".\")[0]\n        \n        img_mask = self.compute()\n        img_mask = img_mask.astype('float32')\n\n        if (self.transform != None):\n            augmentation = self.transform(image = img , mask = img_mask)\n            img = augmentation[\"image\"]\n            img_mask = augmentation[\"mask\"]\n            \n            \n        img = torch.tensor(img).to(torch.float)\n        img = img.permute(2,0,1) \n       \n        img_mask = torch.tensor(img_mask).to(torch.float).unsqueeze(0)\n        return img,img_mask\n    \n    def compute(self):\n        mask = np.zeros((IMAGE_HEIGHT * IMAGE_WIDTH))\n        for i in list(df[df[\"id\"] == self.image_id][\"annotation\"]):\n            for index,j in enumerate(i.split()):\n                if(index % 2 == 0):\n                    x = int(j)\n                else:\n                    mask[x:x+int(j)] = 1\n        return mask.reshape(IMAGE_HEIGHT,IMAGE_WIDTH)","metadata":{"execution":{"iopub.status.busy":"2021-12-02T05:05:15.407391Z","iopub.execute_input":"2021-12-02T05:05:15.407585Z","iopub.status.idle":"2021-12-02T05:05:15.420606Z","shell.execute_reply.started":"2021-12-02T05:05:15.407561Z","shell.execute_reply":"2021-12-02T05:05:15.419783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dataset():\n    \n    train_data = Custom_Dataset(df,train_images,transform,True)\n    val_data = Custom_Dataset(df,val_images,transform,True)\n    \n    train_loader = DataLoader(train_data,batch_size = BATCH_SIZE, shuffle = True)\n    val_loader  = DataLoader(val_data , batch_size = len(val_data))\n    \n    return train_loader , val_loader","metadata":{"execution":{"iopub.status.busy":"2021-12-02T05:05:15.423319Z","iopub.execute_input":"2021-12-02T05:05:15.423953Z","iopub.status.idle":"2021-12-02T05:05:15.432200Z","shell.execute_reply.started":"2021-12-02T05:05:15.423902Z","shell.execute_reply":"2021-12-02T05:05:15.431104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader , val_loader = load_dataset()","metadata":{"execution":{"iopub.status.busy":"2021-12-02T05:05:15.433473Z","iopub.execute_input":"2021-12-02T05:05:15.433746Z","iopub.status.idle":"2021-12-02T05:05:15.442096Z","shell.execute_reply.started":"2021-12-02T05:05:15.433710Z","shell.execute_reply":"2021-12-02T05:05:15.441354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m,q = next(iter(train_loader))\nm.shape","metadata":{"execution":{"iopub.status.busy":"2021-12-02T05:05:15.443589Z","iopub.execute_input":"2021-12-02T05:05:15.443834Z","iopub.status.idle":"2021-12-02T05:05:16.145560Z","shell.execute_reply.started":"2021-12-02T05:05:15.443800Z","shell.execute_reply":"2021-12-02T05:05:16.144831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show(m[0],q[0])","metadata":{"execution":{"iopub.status.busy":"2021-12-02T05:05:16.146711Z","iopub.execute_input":"2021-12-02T05:05:16.146974Z","iopub.status.idle":"2021-12-02T05:05:19.816116Z","shell.execute_reply.started":"2021-12-02T05:05:16.146939Z","shell.execute_reply":"2021-12-02T05:05:19.815382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Double_Convolution(nn.Module):\n    \n    def __init__(self,in_channels , out_channels , kernel_size, padding):\n        \n        super().__init__()\n        self.double_conv = nn.Sequential(\n            nn.Conv2d(in_channels,out_channels, kernel_size , padding  = padding),\n            nn.BatchNorm2d(out_channels),\n            nn.ReLU(),\n            \n            nn.Conv2d(out_channels,out_channels, kernel_size , padding  = padding),\n            nn.BatchNorm2d(out_channels),\n            nn.ReLU()\n        )\n    \n    def forward(self,x):\n        return self.double_conv(x)","metadata":{"execution":{"iopub.status.busy":"2021-12-02T05:05:19.818175Z","iopub.execute_input":"2021-12-02T05:05:19.819207Z","iopub.status.idle":"2021-12-02T05:05:19.826422Z","shell.execute_reply.started":"2021-12-02T05:05:19.819166Z","shell.execute_reply":"2021-12-02T05:05:19.825840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class UNET(nn.Module):\n    \n    def __init__(self,in_channels,features = [64,128, 256,512,1024],kernel_size = 3 , padding = 1, kernel_size_up = 2,stride_up = 2):\n        \n        super().__init__()\n        \n        #down\n        self.down1 = Double_Convolution(in_channels, features[0] , kernel_size , padding)\n        self.down2 = Double_Convolution(features[0], features[1] , kernel_size , padding)\n        self.down3 = Double_Convolution(features[1], features[2] , kernel_size , padding)\n        self.down4 = Double_Convolution(features[2], features[3] , kernel_size , padding)\n        self.down5 = Double_Convolution(features[3], features[4] , kernel_size , padding)\n        \n        self.maxpool = nn.MaxPool2d(2)\n        \n        #up\n        \n        self.up1 = nn.ConvTranspose2d(features[4],features[3],kernel_size_up, stride =2)\n        self.same1 = Double_Convolution(features[4],features[3],kernel_size , padding)\n        \n        self.up2 = nn.ConvTranspose2d(features[3],features[2],kernel_size_up, stride = 2)\n        self.same2 = Double_Convolution(features[3],features[2],kernel_size , padding)\n        \n        self.up3 = nn.ConvTranspose2d(features[2],features[1],kernel_size_up, stride = 2)\n        self.same3 = Double_Convolution(features[2],features[1],kernel_size , padding)\n        \n        self.up4 = nn.ConvTranspose2d(features[1],features[0],kernel_size_up, stride = 2)\n        self.same4 = Double_Convolution(features[1],features[0],kernel_size , padding)\n        \n        self.out = nn.Conv2d(features[0],1,kernel_size,padding= padding)\n        \n        self.next_out = nn.Sigmoid()\n        \n    def forward(self,x):\n        \n        #going down\n        \n        d1 = self.down1(x)\n        d2 = self.maxpool(d1)\n        \n        d2 = self.down2(d2)\n        d3 = self.maxpool(d2)\n        \n        d3 = self.down3(d3)\n        d4 = self.maxpool(d3)\n        \n        d4 = self.down4(d4)\n        d5 = self.maxpool(d4)\n        \n        d5 = self.down5(d5)\n        \n        #going up\n        \n        z1 = self.up1(d5)\n      \n        z1 = torch.cat((z1 ,d4), 1)\n        z1 = self.same1(z1)\n        \n        z1 = self.up2(z1)\n        z1 = torch.cat((z1 ,d3), 1)\n        z1 = self.same2(z1)\n        \n        z1 = self.up3(z1)\n        z1 = torch.cat((z1 ,d2), 1)\n        z1 = self.same3(z1)\n        \n        z1 = self.up4(z1)\n        z1 = torch.cat((z1 ,d1), 1)\n        z1 = self.same4(z1)\n        \n        z1 = self.out(z1)\n        z1 = self.next_out(z1)\n        \n        return z1","metadata":{"execution":{"iopub.status.busy":"2021-12-02T05:05:19.827783Z","iopub.execute_input":"2021-12-02T05:05:19.828308Z","iopub.status.idle":"2021-12-02T05:05:19.851141Z","shell.execute_reply.started":"2021-12-02T05:05:19.828218Z","shell.execute_reply":"2021-12-02T05:05:19.850355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = UNET(3,padding = 1).to(device =DEVICE)\nloss_fn = nn.BCELoss()\noptimizer = optim.Adam(model.parameters(),lr = LEARNING_RATE)\n# torchsummary.summary(model,(3,256,256))","metadata":{"execution":{"iopub.status.busy":"2021-12-02T05:05:19.853280Z","iopub.execute_input":"2021-12-02T05:05:19.854470Z","iopub.status.idle":"2021-12-02T05:05:22.941630Z","shell.execute_reply.started":"2021-12-02T05:05:19.854441Z","shell.execute_reply":"2021-12-02T05:05:22.940897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_model():\n    model.train()\n    losses = []\n    for index,(data,target) in enumerate(train_loader):\n\n        data = data.to(device = DEVICE)\n        target = target.to(device = DEVICE)\n         \n        prediction = model(data)\n        loss = loss_fn(prediction,target)\n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n        losses.append(loss.item())\n        \n    return (sum(losses)/len(losses))","metadata":{"execution":{"iopub.status.busy":"2021-12-02T05:05:22.945020Z","iopub.execute_input":"2021-12-02T05:05:22.945218Z","iopub.status.idle":"2021-12-02T05:05:22.952939Z","shell.execute_reply.started":"2021-12-02T05:05:22.945193Z","shell.execute_reply":"2021-12-02T05:05:22.952197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_model()","metadata":{"execution":{"iopub.status.busy":"2021-12-02T05:05:22.954829Z","iopub.execute_input":"2021-12-02T05:05:22.955365Z","iopub.status.idle":"2021-12-02T05:06:27.896459Z","shell.execute_reply.started":"2021-12-02T05:05:22.955327Z","shell.execute_reply":"2021-12-02T05:06:27.895760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def evaluate(threshold):\n    model.eval()\n    with torch.no_grad():\n        f1s= []\n        loss = []\n        accuracies = []\n        for(x,y) in val_loader:\n            x = x.to(device = DEVICE)\n            y = y.to(device = DEVICE)\n            \n            prediction = model(x)\n            losses = loss_fn(prediction,y)\n            loss.append(losses)\n            \n            prediction  = (prediction >=threshold).int()\n            \n            prediction_new = prediction.view(-1).cpu().numpy()\n            y_new = y.view(-1).int().cpu().numpy()\n            \n            accuracies.append( ((prediction_new== y_new).sum()) /len(prediction_new))\n    \n            f1  = f1_score(y_new,prediction_new)\n            \n            union = np.where(y_new+ prediction_new,1,0).sum()\n            intersection = y_new.sum() + prediction_new.sum() - union\n            f1s.append(f1)\n            \n    return (sum(accuracies).item()/len(accuracies) , sum(f1s)/len(f1s) , (intersection/union).item() , sum(loss).item()/len(loss) ) ","metadata":{"execution":{"iopub.status.busy":"2021-12-02T05:06:27.897937Z","iopub.execute_input":"2021-12-02T05:06:27.898199Z","iopub.status.idle":"2021-12-02T05:06:27.907009Z","shell.execute_reply.started":"2021-12-02T05:06:27.898165Z","shell.execute_reply":"2021-12-02T05:06:27.906300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"evaluate(0.5)","metadata":{"execution":{"iopub.status.busy":"2021-12-02T05:06:27.908391Z","iopub.execute_input":"2021-12-02T05:06:27.908988Z","iopub.status.idle":"2021-12-02T05:06:31.095382Z","shell.execute_reply.started":"2021-12-02T05:06:27.908946Z","shell.execute_reply":"2021-12-02T05:06:31.094687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss_plot_train = []\nloss_plot_val = []\naccuracy_plot = []\nf1_plot = []\niou_plot = []","metadata":{"execution":{"iopub.status.busy":"2021-12-02T05:06:31.096535Z","iopub.execute_input":"2021-12-02T05:06:31.096960Z","iopub.status.idle":"2021-12-02T05:06:31.102241Z","shell.execute_reply.started":"2021-12-02T05:06:31.096904Z","shell.execute_reply":"2021-12-02T05:06:31.101175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def training():\n    with tqdm(total=EPOCHS, position=0, leave=True) as pbar:\n        for epoch in range(EPOCHS):\n            loss = train_model()\n            loss_plot_train.append(loss)\n            accuracy, f1,iou,l1  = evaluate(0.5)\n            accuracy_plot.append(accuracy)\n            f1_plot.append(f1)\n            pbar.update()\n            iou_plot.append(iou)\n            loss_plot_val.append(l1)\n            pbar.set_postfix(loss = (loss,l1) , acc = (accuracy,f1,iou ) )","metadata":{"execution":{"iopub.status.busy":"2021-12-02T05:06:31.104006Z","iopub.execute_input":"2021-12-02T05:06:31.104357Z","iopub.status.idle":"2021-12-02T05:06:31.112374Z","shell.execute_reply.started":"2021-12-02T05:06:31.104223Z","shell.execute_reply":"2021-12-02T05:06:31.111662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training()","metadata":{"execution":{"iopub.status.busy":"2021-12-02T05:06:31.116671Z","iopub.execute_input":"2021-12-02T05:06:31.116951Z","iopub.status.idle":"2021-12-02T05:07:46.937273Z","shell.execute_reply.started":"2021-12-02T05:06:31.116899Z","shell.execute_reply":"2021-12-02T05:07:46.935979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(loss_plot_train , label = \"train loss\")\nplt.plot(loss_plot_val, label = \"val loss\")","metadata":{"execution":{"iopub.status.busy":"2021-12-02T05:07:46.938270Z","iopub.status.idle":"2021-12-02T05:07:46.938991Z","shell.execute_reply.started":"2021-12-02T05:07:46.938716Z","shell.execute_reply":"2021-12-02T05:07:46.938743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(iou_plot)","metadata":{"execution":{"iopub.status.busy":"2021-12-02T05:07:46.940374Z","iopub.status.idle":"2021-12-02T05:07:46.941051Z","shell.execute_reply.started":"2021-12-02T05:07:46.940791Z","shell.execute_reply":"2021-12-02T05:07:46.940816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#inference\nthreshold = 0.5\ntest_id = [index.split(\"/\")[-1].split(\".\")[0] for index in test_images]\nfinal = pd.DataFrame(test_id,columns=[\"id\"])\nfinal","metadata":{"execution":{"iopub.status.busy":"2021-12-02T05:07:46.942358Z","iopub.status.idle":"2021-12-02T05:07:46.942776Z","shell.execute_reply.started":"2021-12-02T05:07:46.942557Z","shell.execute_reply":"2021-12-02T05:07:46.942579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def mask2rle(mask):\n    mask = np.array(mask)\n    pixels = mask.flatten()\n    pad = np.array([0])\n    pixels = np.concatenate([pad, pixels, pad])\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)","metadata":{"execution":{"iopub.status.busy":"2021-12-02T05:07:46.944099Z","iopub.status.idle":"2021-12-02T05:07:46.944755Z","shell.execute_reply.started":"2021-12-02T05:07:46.944505Z","shell.execute_reply":"2021-12-02T05:07:46.944533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def mask2rle(mask):\n    mask = np.array(mask)\n    pixels = mask.flatten()\n    pad = np.array([0])\n    pixels = np.concatenate([pad, pixels, pad])\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\ndef mask2rle_list(mask, cutoff=0.5, min_object_size=1.0):\n    \"\"\" Return run length encoding of mask.\n        ref: https://www.kaggle.com/raoulma/nuclei-dsb-2018-tensorflow-u-net-score-0-352\n    \"\"\"\n    # segment image and label different objects\n    lab_mask = skimage.morphology.label(mask > cutoff)\n\n    # Keep only objects that are large enough.\n    (mask_labels, mask_sizes) = np.unique(lab_mask, return_counts=True)\n    l = (mask_sizes < min_object_size).any()\n    if (mask_sizes < min_object_size).any():\n        mask_labels = mask_labels[mask_sizes < min_object_size]\n        for n in mask_labels:\n            lab_mask[lab_mask == n] = 0\n        lab_mask = skimage.morphology.label(lab_mask > cutoff)\n\n        # Loop over each object excluding the background labeled by 0.\n    for i in range(1, lab_mask.max() + 1):\n        yield mask2rle(lab_mask == i)","metadata":{"execution":{"iopub.status.busy":"2021-12-02T05:07:46.946014Z","iopub.status.idle":"2021-12-02T05:07:46.946425Z","shell.execute_reply.started":"2021-12-02T05:07:46.946198Z","shell.execute_reply":"2021-12-02T05:07:46.946220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final= {\"id\" : [] , \"predicted\" : []}","metadata":{"execution":{"iopub.status.busy":"2021-12-02T05:07:46.948143Z","iopub.status.idle":"2021-12-02T05:07:46.948556Z","shell.execute_reply.started":"2021-12-02T05:07:46.948325Z","shell.execute_reply":"2021-12-02T05:07:46.948347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for index,i in enumerate(test_images):\n    image = cv2.imread(i)\n    image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)\n    image_id = i.split('/')[-1].split(\".\")[0]\n    augmentation = transform_test(image = image)\n    image = augmentation[\"image\"]\n    image =torch.tensor(image).to(device = DEVICE).permute(2,0,1).unsqueeze(0).float()\n    prediction = model(image)\n    \n    prediction = prediction.squeeze(0).permute(1,2,0).detach().cpu().numpy()\n#     plt.imshow( (prediction>0.5 ).astype(\"int\") )\n    prediction = cv2.resize(prediction , (IMAGE_WIDTH,IMAGE_HEIGHT), interpolation = cv2.INTER_AREA)\n    prediction = (prediction>=threshold).astype(\"int\")\n#     k = mask2rle(prediction)\n#     final[\"id\"].append(image_id)\n#     final[\"predicted\"].append(k)\n    \n    m = list(mask2rle_list(prediction))\n    for i in m:\n        final[\"id\"].append(image_id)\n        final[\"predicted\"].append(i)\n    ","metadata":{"execution":{"iopub.status.busy":"2021-12-02T05:07:46.950025Z","iopub.status.idle":"2021-12-02T05:07:46.950442Z","shell.execute_reply.started":"2021-12-02T05:07:46.950209Z","shell.execute_reply":"2021-12-02T05:07:46.950231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = pd.DataFrame(final)\nresult","metadata":{"execution":{"iopub.status.busy":"2021-12-02T05:07:46.951695Z","iopub.status.idle":"2021-12-02T05:07:46.952475Z","shell.execute_reply.started":"2021-12-02T05:07:46.952203Z","shell.execute_reply":"2021-12-02T05:07:46.952238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-12-02T05:07:46.953732Z","iopub.status.idle":"2021-12-02T05:07:46.954157Z","shell.execute_reply.started":"2021-12-02T05:07:46.953934Z","shell.execute_reply":"2021-12-02T05:07:46.953956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}