{"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":"# THE TRAINING NOTEBOOK (PART 1 CAN BE FOUND HERE)\n# [Sartorius-Cell-Segmentation-DeepLabv3-Training](https://www.kaggle.com/albertozorzetto/sartorius-cell-segmentation-deeplabv3-training)","metadata":{}},{"cell_type":"markdown","source":"`█▀▀▄░░░░░░░░░░░▄▀▀█\n░█░░░▀▄░▄▄▄▄▄░▄▀░░░█\n░░▀▄░░░▀░░░░░▀░░░▄▀\n░░░░▌░▄▄░░░▄▄░▐▀▀\n░░░▐░░█▄░░░▄█░░▌▄▄▀▀▀▀█\n░░░▌▄▄▀▀░▄░▀▀▄▄▐░░░░░░█\n▄▀▀▐▀▀░▄▄▄▄▄░▀▀▌▄▄▄░░░█\n█░░░▀▄░█░░░█░▄▀░░░░█▀▀▀\n░▀▄░░▀░░▀▀▀░░▀░░░▄█▀\n░░░█░░░░░░░░░░░▄▀▄░▀▄\n░░░█░░░░░░░░░▄▀█░░█░░█\n░░░█░░░░░░░░░░░█▄█░░▄▀\n░░░█░░░░░░░░░░░████▀\n░░░▀▄▄▀▀▄▄▀▀▄▄▄█▀\n`","metadata":{}},{"cell_type":"code","source":"!pip install segmentation-models-pytorch \n!pip install sewar ","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-12-19T23:07:10.669027Z","iopub.execute_input":"2021-12-19T23:07:10.669627Z","iopub.status.idle":"2021-12-19T23:07:28.677903Z","shell.execute_reply.started":"2021-12-19T23:07:10.669571Z","shell.execute_reply":"2021-12-19T23:07:28.676546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sewar\nimport segmentation_models_pytorch  as smp\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport torch\nimport torchvision\nimport cv2\nfrom tqdm import tqdm\nfrom torch.nn import functional as F\nimport torch.backends.cudnn as cudnn\nfrom torch.optim.lr_scheduler import ReduceLROnPlateau\nimport gc\nimport torch.nn as nn\nfrom torch.utils.data import Dataset\nfrom torch.utils.data import DataLoader\n\nfrom albumentations import (HorizontalFlip, VerticalFlip, \n                            ShiftScaleRotate, Normalize, Resize, \n                            Compose, GaussNoise)\nfrom albumentations.pytorch import ToTensorV2\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-19T23:07:28.681511Z","iopub.execute_input":"2021-12-19T23:07:28.681961Z","iopub.status.idle":"2021-12-19T23:07:28.692656Z","shell.execute_reply.started":"2021-12-19T23:07:28.681909Z","shell.execute_reply":"2021-12-19T23:07:28.691244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('../input/sartorius-cell-instance-segmentation/train.csv')\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2021-12-19T23:07:28.694564Z","iopub.execute_input":"2021-12-19T23:07:28.694820Z","iopub.status.idle":"2021-12-19T23:07:29.100244Z","shell.execute_reply.started":"2021-12-19T23:07:28.694788Z","shell.execute_reply":"2021-12-19T23:07:29.098869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df = pd.read_csv('../input/sartorius-cell-instance-segmentation/sample_submission.csv')\nsub_df","metadata":{"execution":{"iopub.status.busy":"2021-12-19T23:07:29.103363Z","iopub.execute_input":"2021-12-19T23:07:29.103755Z","iopub.status.idle":"2021-12-19T23:07:29.120117Z","shell.execute_reply.started":"2021-12-19T23:07:29.103704Z","shell.execute_reply":"2021-12-19T23:07:29.119050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_IMGS_PATH = \"../input/sartorius-cell-instance-segmentation/test/\"\nTRAIN_IMGS_PATH = \"../input/sartorius-cell-instance-segmentation/train/\"\n\nIMGS_WIDTH = 704\nIMGS_HEIGHT = 520\n\nRESNET_MEAN = (0.485, 0.456, 0.406)\nRESNET_STD = (0.229, 0.224, 0.225)\n\nTARGET_IMGS_HEIGHT=512\nTARGET_IMGS_WIDTH=512\n\nDEVICE = \"cuda\" if torch.cuda.is_available() else \"cpu\"\nprint(\"Using : \",DEVICE)","metadata":{"execution":{"iopub.status.busy":"2021-12-19T23:07:29.122364Z","iopub.execute_input":"2021-12-19T23:07:29.122624Z","iopub.status.idle":"2021-12-19T23:07:29.130574Z","shell.execute_reply.started":"2021-12-19T23:07:29.122596Z","shell.execute_reply":"2021-12-19T23:07:29.129721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_decode(x,shape,color=1):\n    \n    out = np.zeros((shape[0]*shape[1],shape[2]))\n    x=[int(i) for i in x.split(\" \")]\n    for i in range(0,len(x),2):\n        out[ x[i]:(x[i]+x[i+1]) ]=color\n\n    return np.reshape(out,shape)","metadata":{"execution":{"iopub.status.busy":"2021-12-19T23:07:29.131808Z","iopub.execute_input":"2021-12-19T23:07:29.132097Z","iopub.status.idle":"2021-12-19T23:07:29.146045Z","shell.execute_reply.started":"2021-12-19T23:07:29.132053Z","shell.execute_reply":"2021-12-19T23:07:29.145041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_encode(x):\n    out=[]\n    x=x.flatten()\n    for i in range(0,x.shape[0]-1):\n        if(x[i]==1):\n            count=1\n            out.append(str(i))\n            i+=1\n            while(x[i]==1):\n                count+=1\n                i+=1\n            out.append(str(count))\n    return \" \".join(out)","metadata":{"execution":{"iopub.status.busy":"2021-12-19T23:07:29.147466Z","iopub.execute_input":"2021-12-19T23:07:29.147749Z","iopub.status.idle":"2021-12-19T23:07:29.161457Z","shell.execute_reply.started":"2021-12-19T23:07:29.147714Z","shell.execute_reply":"2021-12-19T23:07:29.160677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class SartoriusCellDataset(Dataset):\n    def __init__(self,train_df,train_imgs_path,transforms):\n        self.train_df=train_df\n        self.image_ids=np.unique(train_df['id']).tolist()\n        self.train_imgs_path=train_imgs_path\n        self.transforms = transforms\n    def __len__(self):\n        return len(self.image_ids)\n    def __getitem__(self,idx):\n        \n        image = cv2.cvtColor( cv2.imread( self.train_imgs_path +  self.image_ids[idx] + \".png\"),cv2.COLOR_BGR2RGB)\n        mask = np.zeros((image.shape[0],image.shape[1],1),dtype=np.float32)\n        rle_masks=self.train_df[train_df[\"id\"]==self.image_ids[idx]]['annotation'].tolist()\n        \n        for rle_mask in rle_masks:\n            mask+=rle_decode(rle_mask,(image.shape[0],image.shape[1],1)).astype(np.float32)\n        mask = mask.clip(0, 1)\n        \n        if self.transforms:\n            aug = self.transforms(image=image,mask=mask)\n            image,mask=aug['image'],aug['mask']\n            \n        return image,mask.reshape((1,image.shape[1],image.shape[2]))","metadata":{"execution":{"iopub.status.busy":"2021-12-19T23:10:30.942777Z","iopub.execute_input":"2021-12-19T23:10:30.943072Z","iopub.status.idle":"2021-12-19T23:10:30.957357Z","shell.execute_reply.started":"2021-12-19T23:10:30.943039Z","shell.execute_reply":"2021-12-19T23:10:30.956229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transforms = Compose([Resize(TARGET_IMGS_HEIGHT,TARGET_IMGS_WIDTH),\n                    Normalize(mean=RESNET_MEAN,std=RESNET_STD),\n                    VerticalFlip(p=0.5),\n                    HorizontalFlip(p=0.5),\n                    ToTensorV2()])\ntrain_ds = SartoriusCellDataset(train_df,\n                          TRAIN_IMGS_PATH,\n                          transforms)","metadata":{"execution":{"iopub.status.busy":"2021-12-19T23:07:29.162673Z","iopub.execute_input":"2021-12-19T23:07:29.162949Z","iopub.status.idle":"2021-12-19T23:07:29.244424Z","shell.execute_reply.started":"2021-12-19T23:07:29.162913Z","shell.execute_reply":"2021-12-19T23:07:29.243253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading the model","metadata":{}},{"cell_type":"code","source":"\nmodel = smp.DeepLabV3Plus(\"resnet34\",\n                  encoder_weights=\"imagenet\", \n                )\n","metadata":{"execution":{"iopub.status.busy":"2021-12-19T23:07:29.245879Z","iopub.execute_input":"2021-12-19T23:07:29.246301Z","iopub.status.idle":"2021-12-19T23:07:29.864632Z","shell.execute_reply.started":"2021-12-19T23:07:29.246267Z","shell.execute_reply":"2021-12-19T23:07:29.863307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inference test on training data","metadata":{}},{"cell_type":"code","source":"if torch.cuda.is_available():\n    model.load_state_dict(torch.load(\"../input/deeplabv3plus-resnet34-sartorius/best_model.pth\"))\nelse:\n    model.load_state_dict(torch.load(\"../input/deeplabv3plus-resnet34-sartorius/best_model.pth\",map_location=torch.device('cpu')))\n\nmodel.to(DEVICE)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-12-19T23:07:29.867284Z","iopub.execute_input":"2021-12-19T23:07:29.867568Z","iopub.status.idle":"2021-12-19T23:07:29.990274Z","shell.execute_reply.started":"2021-12-19T23:07:29.867533Z","shell.execute_reply":"2021-12-19T23:07:29.989258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE=32\ntrain_loader=DataLoader(\n    train_ds,\n    batch_size=BATCH_SIZE,\n    shuffle=False\n)","metadata":{"execution":{"iopub.status.busy":"2021-12-19T23:07:29.992255Z","iopub.execute_input":"2021-12-19T23:07:29.992581Z","iopub.status.idle":"2021-12-19T23:07:29.998484Z","shell.execute_reply.started":"2021-12-19T23:07:29.992536Z","shell.execute_reply":"2021-12-19T23:07:29.997378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for batch_idx,batch in enumerate(train_loader):\n    images,masks=batch\n    if torch.cuda.is_available():\n        images,masks=images.cuda(),masks.cuda()\n    preds=model(images)\n    print(preds.shape)\n    fig,axs=plt.subplots(16,2,figsize=(10,80))\n    images,masks=images.cpu(),masks.cpu()\n    preds=preds.cpu().detach().numpy()\n    print(preds[0].max(),preds[0].min())\n    for i in range(16):\n        #axs[i][0].imshow(images[i].reshape(512,512,3))\n        axs[i][0].imshow(masks[i].reshape(512,512,1))\n        axs[i][0].title.set_text(\"Ground truth\")\n        #axs[i][1].imshow(images[i].reshape(512,512,3))\n        axs[i][1].imshow(preds[i].reshape(512,512,1))\n        axs[i][1].title.set_text(\"Prediction\")\n\n    plt.subplots_adjust(wspace=0.1)\n\n    plt.show()\n    break","metadata":{"execution":{"iopub.status.busy":"2021-12-19T23:07:29.999780Z","iopub.execute_input":"2021-12-19T23:07:30.000018Z","iopub.status.idle":"2021-12-19T23:08:14.221230Z","shell.execute_reply.started":"2021-12-19T23:07:29.999971Z","shell.execute_reply":"2021-12-19T23:08:14.219475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_pred_1=preds[1].reshape((512,512,1))\nplt.imshow(sample_pred_1)","metadata":{"execution":{"iopub.status.busy":"2021-12-19T23:08:14.224082Z","iopub.execute_input":"2021-12-19T23:08:14.224325Z","iopub.status.idle":"2021-12-19T23:08:14.547518Z","shell.execute_reply.started":"2021-12-19T23:08:14.224298Z","shell.execute_reply":"2021-12-19T23:08:14.545925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"thresholds=[0.05,0.1,0.5,0.7]\nfig,axs=plt.subplots(4,2,figsize=(8,16))\n\nfor idx,t in enumerate(thresholds):\n    axs[idx][0].imshow(masks[1].reshape(512,512,1))\n    axs[idx][0].title.set_text(\"Ground Truth\")\n    thresh_img=cv2.threshold(sample_pred_1,t,1,cv2.THRESH_BINARY)[1]\n    axs[idx][1].imshow(thresh_img)\n    axs[idx][1].title.set_text(\"Pred,Treshold : \"+str(t)+\",UQI score : \"+\"{:.4f}\".format(sewar.full_ref.uqi(sample_pred_1.reshape((512,512)),thresh_img)))","metadata":{"execution":{"iopub.status.busy":"2021-12-19T22:22:42.89513Z","iopub.execute_input":"2021-12-19T22:22:42.895529Z","iopub.status.idle":"2021-12-19T22:22:44.577361Z","shell.execute_reply.started":"2021-12-19T22:22:42.895477Z","shell.execute_reply":"2021-12-19T22:22:44.576097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_pred_2=preds[2].reshape((512,512,1))\nplt.imshow(sample_pred_2) ","metadata":{"execution":{"iopub.status.busy":"2021-12-19T23:08:14.549874Z","iopub.execute_input":"2021-12-19T23:08:14.550463Z","iopub.status.idle":"2021-12-19T23:08:14.849929Z","shell.execute_reply.started":"2021-12-19T23:08:14.550404Z","shell.execute_reply":"2021-12-19T23:08:14.848690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"thresholds=[0.05,0.1,0.5,0.7]\nfig,axs=plt.subplots(4,2,figsize=(8,16))\n\nfor idx,t in enumerate(thresholds):\n    axs[idx][0].imshow(masks[2].reshape(512,512,1))\n    axs[idx][0].title.set_text(\"Ground Truth\")\n    thresh_img=cv2.threshold(sample_pred_2,t,1,cv2.THRESH_BINARY)[1]\n    axs[idx][1].imshow(thresh_img)\n    axs[idx][1].title.set_text(\"Pred,Treshold : \"+str(t)+\",UQI score : \"+\"{:.4f}\".format(sewar.full_ref.uqi(sample_pred_2.reshape((512,512)),thresh_img)))","metadata":{"execution":{"iopub.status.busy":"2021-12-19T23:08:14.851340Z","iopub.execute_input":"2021-12-19T23:08:14.851653Z","iopub.status.idle":"2021-12-19T23:08:16.620965Z","shell.execute_reply.started":"2021-12-19T23:08:14.851610Z","shell.execute_reply":"2021-12-19T23:08:16.619717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_pred_3=preds[7].reshape((512,512,1))\nplt.imshow(sample_pred_3)","metadata":{"execution":{"iopub.status.busy":"2021-12-19T23:08:16.622778Z","iopub.execute_input":"2021-12-19T23:08:16.623169Z","iopub.status.idle":"2021-12-19T23:08:16.913279Z","shell.execute_reply.started":"2021-12-19T23:08:16.623117Z","shell.execute_reply":"2021-12-19T23:08:16.912173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"thresholds=[0.05,0.1,0.5,0.7,1]\nfig,axs=plt.subplots(5,2,figsize=(8,20))\n\nfor idx,t in enumerate(thresholds):\n    axs[idx][0].imshow(masks[7].reshape(512,512,1))\n    axs[idx][0].title.set_text(\"Ground Truth\")\n    thresh_img=cv2.threshold(sample_pred_3,t,1,cv2.THRESH_BINARY)[1]\n    axs[idx][1].imshow(thresh_img)\n    axs[idx][1].title.set_text(\"Pred,Treshold : \"+str(t)+\",UQI score : \"+\"{:.4f}\".format(sewar.full_ref.uqi(sample_pred_3.reshape((512,512)),thresh_img)))\nplt.subplots_adjust(hspace=0.3)","metadata":{"execution":{"iopub.status.busy":"2021-12-19T23:08:16.914621Z","iopub.execute_input":"2021-12-19T23:08:16.914850Z","iopub.status.idle":"2021-12-19T23:08:18.770840Z","shell.execute_reply.started":"2021-12-19T23:08:16.914821Z","shell.execute_reply":"2021-12-19T23:08:18.769486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Apparently 0.5 threshold is better quite often ","metadata":{}},{"cell_type":"code","source":"def preds_postprocess(mask,threshold=0.6,min_size=300):\n    mask=cv2.threshold(mask,threshold,1,cv2.THRESH_BINARY)[1]\n    n_component, component = cv2.connectedComponents(mask.astype(np.uint8))\n    predictions=[]\n    for c in range(1,n_component):\n        p = (component == c)\n        if p.sum() > min_size:\n            a_prediction = np.zeros((512, 512), np.float32)\n            a_prediction[p] = 1\n            predictions.append(a_prediction)\n    return predictions\n\npost_preds=preds_postprocess(sample_pred_1)\nprint(len(post_preds))\nprint(post_preds[0].shape)\nprint(np.unique(post_preds[0]))","metadata":{"execution":{"iopub.status.busy":"2021-12-19T23:08:18.772276Z","iopub.execute_input":"2021-12-19T23:08:18.772508Z","iopub.status.idle":"2021-12-19T23:08:18.797131Z","shell.execute_reply.started":"2021-12-19T23:08:18.772480Z","shell.execute_reply":"2021-12-19T23:08:18.796079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,axs=plt.subplots(1,3,figsize=(15,5))\naxs[0].imshow(masks[1].reshape((512,512,1)))\naxs[0].title.set_text(\"Ground Truth\")\naxs[1].imshow(sample_pred_1.reshape((512,512,1)))\naxs[1].title.set_text(\"Prediction\")\naxs[2].imshow(cv2.threshold(sample_pred_1.reshape((512,512,1)),0.5,1,cv2.THRESH_BINARY)[1])\naxs[2].title.set_text(\"Thresholded Prediction\")","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-19T23:08:18.800040Z","iopub.execute_input":"2021-12-19T23:08:18.800280Z","iopub.status.idle":"2021-12-19T23:08:19.450345Z","shell.execute_reply.started":"2021-12-19T23:08:18.800253Z","shell.execute_reply":"2021-12-19T23:08:19.449184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"RLE Test\")\nencode_sample_pred = post_preds[2]\nfig,axs=plt.subplots(1,2)\nreconstr_pred = rle_decode(rle_encode(encode_sample_pred),(512,512,1))\naxs[0].imshow(encode_sample_pred)\naxs[1].imshow(reconstr_pred)\n\naxs[0].title.set_text(\"Original Image\")\naxs[1].title.set_text(\"Reconstructed Image\")","metadata":{"execution":{"iopub.status.busy":"2021-12-19T22:24:19.357906Z","iopub.execute_input":"2021-12-19T22:24:19.359351Z","iopub.status.idle":"2021-12-19T22:24:20.941918Z","shell.execute_reply.started":"2021-12-19T22:24:19.359279Z","shell.execute_reply":"2021-12-19T22:24:20.940957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test-Time Data Augmentation (TTA) INFERENCE","metadata":{}},{"cell_type":"code","source":"def read_img(path,resize_shape=(512,512)):\n    img=cv2.cvtColor( cv2.imread(path),cv2.COLOR_BGR2RGB)\n    img=cv2.resize(img,resize_shape)\n    return img.astype(np.double)\n\ndef np_to_torch(img,img_shape=(512,512)):\n    return torch.tensor(np.expand_dims(img, axis=0).astype(np.float32)).reshape(1,3,img_shape[0],img_shape[1])\n\ndef torch_to_plt(img,shape=(512,512,1)):\n    return img.detach().numpy().astype(np.uint8).reshape(shape)","metadata":{"execution":{"iopub.status.busy":"2021-12-19T23:08:19.451860Z","iopub.execute_input":"2021-12-19T23:08:19.452134Z","iopub.status.idle":"2021-12-19T23:08:19.460340Z","shell.execute_reply.started":"2021-12-19T23:08:19.452102Z","shell.execute_reply":"2021-12-19T23:08:19.459184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_transforms = Compose([Resize(512,512),Normalize(mean=RESNET_MEAN,std=RESNET_STD), ToTensorV2()])","metadata":{"execution":{"iopub.status.busy":"2021-12-19T23:08:19.461928Z","iopub.execute_input":"2021-12-19T23:08:19.462423Z","iopub.status.idle":"2021-12-19T23:08:19.480367Z","shell.execute_reply.started":"2021-12-19T23:08:19.462385Z","shell.execute_reply":"2021-12-19T23:08:19.479332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#For some reason doesnt work ლ(╥﹏╥ლ)\nPREDS_THRESHOLD = 0.5\nmodel.cpu()\nsingle_mask_preds=[]\nfor id in sub_df['id'].tolist():\n    \n    plt.figure(figsize=(32,32))\n    \n    image = read_img(TEST_IMGS_PATH + id +\".png\").astype(np.float32)\n    \n    #Original\n    original_image=test_transforms(image=image)[\"image\"]\n    original_pred = model(torch.unsqueeze(original_image,0)).detach().numpy()\n    plt.subplot(1,8,1)\n    plt.imshow(original_pred.reshape(512,512,1))\n    \n    #Vertical flip \n    v_flip_image=test_transforms(image=np.flipud(image).astype(np.float32))[\"image\"]\n    v_flip_pred=np.flipud(model(torch.unsqueeze(v_flip_image,0)).detach().numpy())\n    plt.subplot(1,8,2)\n    plt.imshow(np.flipud(v_flip_pred.reshape(512,512,1)))\n    \n    #Horizontal flip \n    h_flip_image=test_transforms(image=np.fliplr(image).astype(np.float32))[\"image\"]\n    h_flip_pred=np.fliplr(model(torch.unsqueeze(h_flip_image,0)).detach().numpy())\n    plt.subplot(1,8,3)\n    plt.imshow(np.fliplr(h_flip_pred.reshape(512,512,1)))\n    \n    #Diagonal\n    v_h_flip_image=test_transforms(image=np.fliplr(np.flipud(image)).astype(np.float32))[\"image\"]\n    v_h_flip_pred=np.fliplr(np.flipud(model(torch.unsqueeze(v_h_flip_image,0)).detach().numpy()))\n    plt.subplot(1,8,4)\n    plt.imshow(np.fliplr(np.flipud(v_h_flip_pred.reshape(512,512,1))))\n    \n    pred = (original_pred+v_flip_pred+h_flip_pred+v_h_flip_pred) / 4\n    single_mask_preds.append(pred)\n    \nsingle_mask_preds=np.asarray(single_mask_preds)\nthresh_preds= single_mask_preds > PREDS_THRESHOLD","metadata":{"execution":{"iopub.status.busy":"2021-12-19T23:02:47.149902Z","iopub.execute_input":"2021-12-19T23:02:47.150636Z","iopub.status.idle":"2021-12-19T23:02:49.598454Z","shell.execute_reply.started":"2021-12-19T23:02:47.150585Z","shell.execute_reply":"2021-12-19T23:02:49.596925Z"},"_kg_hide-output":true,"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PREDS_THRESHOLD = 0.5\nmodel.cpu()\nmodel.eval()\nsingle_mask_preds=[]\nfor id in sub_df['id'].tolist():\n    \n    plt.figure(figsize=(32,32))\n    \n    image = read_img(TEST_IMGS_PATH + id +\".png\").astype(np.float32)\n    \n    #Original\n    image=test_transforms(image=image)[\"image\"]\n    pred = model(torch.unsqueeze(image,0)).detach().numpy()\n    plt.subplot(1,8,1)\n    plt.imshow(pred.reshape(512,512,1))\n    single_mask_preds.append(pred)\n    \nsingle_mask_preds=np.asarray(single_mask_preds)\nthresh_preds= single_mask_preds > PREDS_THRESHOLD","metadata":{"execution":{"iopub.status.busy":"2021-12-19T23:08:19.481786Z","iopub.execute_input":"2021-12-19T23:08:19.482080Z","iopub.status.idle":"2021-12-19T23:08:22.327751Z","shell.execute_reply.started":"2021-12-19T23:08:19.482041Z","shell.execute_reply":"2021-12-19T23:08:22.326540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(80,80))\nplt.subplot(1,10,1)\nplt.imshow(thresh_preds[0].reshape(512,512,1))\n\nplt.subplot(1,10,2)\n\nplt.imshow(thresh_preds[1].reshape(512,512,1))\n\nplt.subplot(1,10,3)\n\nplt.imshow(thresh_preds[2].reshape(512,512,1))","metadata":{"execution":{"iopub.status.busy":"2021-12-19T23:08:22.331434Z","iopub.execute_input":"2021-12-19T23:08:22.331707Z","iopub.status.idle":"2021-12-19T23:08:23.065311Z","shell.execute_reply.started":"2021-12-19T23:08:22.331677Z","shell.execute_reply":"2021-12-19T23:08:23.064461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Post-process and sub","metadata":{}},{"cell_type":"code","source":"def remove_overlapping_pixels(mask, other_masks):\n    for other_mask in other_masks:\n        if np.sum(np.logical_and(mask, other_mask)) > 0:\n            mask[np.logical_and(mask, other_mask)] = 0\n    return mask","metadata":{"execution":{"iopub.status.busy":"2021-12-19T23:08:23.066596Z","iopub.execute_input":"2021-12-19T23:08:23.066834Z","iopub.status.idle":"2021-12-19T23:08:23.074168Z","shell.execute_reply.started":"2021-12-19T23:08:23.066804Z","shell.execute_reply":"2021-12-19T23:08:23.072700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preds_postprocess(mask,threshold=0.5,min_size=300):\n    mask=cv2.threshold(mask,threshold,1,cv2.THRESH_BINARY)[1]\n    n_component, component = cv2.connectedComponents(mask.astype(np.uint8))\n    predictions=[]\n    for c in range(1,n_component):\n        p = (component == c)\n        if p.sum() > min_size:\n            a_prediction = np.zeros((520, 704), np.float32)\n            a_prediction[p] = 1\n            predictions.append(a_prediction)\n    return predictions","metadata":{"execution":{"iopub.status.busy":"2021-12-19T23:08:23.075606Z","iopub.execute_input":"2021-12-19T23:08:23.075863Z","iopub.status.idle":"2021-12-19T23:08:23.088712Z","shell.execute_reply.started":"2021-12-19T23:08:23.075832Z","shell.execute_reply":"2021-12-19T23:08:23.088038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub = pd.read_csv('../input/sartorius-cell-instance-segmentation/sample_submission.csv')\nsample_sub","metadata":{"execution":{"iopub.status.busy":"2021-12-19T22:25:33.429751Z","iopub.execute_input":"2021-12-19T22:25:33.430099Z","iopub.status.idle":"2021-12-19T22:25:33.447073Z","shell.execute_reply.started":"2021-12-19T22:25:33.430066Z","shell.execute_reply":"2021-12-19T22:25:33.446067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_transforms = Compose([Resize(512,512),Normalize(mean=RESNET_MEAN,std=RESNET_STD), ToTensorV2()])","metadata":{"execution":{"iopub.status.busy":"2021-12-19T23:08:23.092141Z","iopub.execute_input":"2021-12-19T23:08:23.092492Z","iopub.status.idle":"2021-12-19T23:08:23.101076Z","shell.execute_reply.started":"2021-12-19T23:08:23.092455Z","shell.execute_reply":"2021-12-19T23:08:23.100201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_list=[]\nfor idx,id in enumerate (np.unique(sample_sub['id']).tolist()):\n    \n    image=read_img(TEST_IMGS_PATH +str(id) + \".png\")\n    image=test_transforms(image=image)[\"image\"]\n    single_mask_pred=model(image.unsqueeze(0)).detach().squeeze(0).numpy().reshape(512,512)\n\n    single_mask_pred=cv2.resize(single_mask_pred,(704,520),interpolation = cv2.INTER_AREA)\n\n    img_preds=preds_postprocess(single_mask_pred)\n    masks=[]\n    for img_pred in img_preds:\n        fixed_mask=remove_overlapping_pixels(img_pred,masks)\n        masks.append(fixed_mask)\n        pred_list.append((str(id),rle_encode(fixed_mask)))\n        \nsub_df = pd.DataFrame(sub_list,columns=['id','predicted'])\nsub_df","metadata":{"execution":{"iopub.status.busy":"2021-12-19T23:08:24.525043Z","iopub.execute_input":"2021-12-19T23:08:24.525349Z","iopub.status.idle":"2021-12-19T23:10:29.258170Z","shell.execute_reply.started":"2021-12-19T23:08:24.525322Z","shell.execute_reply":"2021-12-19T23:10:29.257470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_masks(df, image_id, input_shape):\n    height, width = input_shape\n    labels = df[df[\"id\"] == image_id][\"predicted\"].tolist()\n    mask = np.zeros((height, width,1))\n    for label in labels:\n        mask += rle_decode(label, shape=(height, width,1))\n    mask = mask.clip(0, 1)\n    return mask\nfig,axs=plt.subplots(1,3,figsize=(30,30))\nfor n,id in enumerate(np.unique(sub_df['id']).tolist()):\n    sample_img=plt.imread(TEST_IMGS_PATH+id+\".png\")\n    sample_masks=build_masks(sub_df,id,input_shape=(520, 704))\n\n    masked = np.ma.masked_where(sample_masks == 0, sample_masks)\n\n    axs[n].imshow(sample_img,cmap=\"seismic\")\n    axs[n].imshow(masked,alpha=0.6,cmap=\"bone\")\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-19T23:10:29.259278Z","iopub.execute_input":"2021-12-19T23:10:29.259674Z","iopub.status.idle":"2021-12-19T23:10:30.830645Z","shell.execute_reply.started":"2021-12-19T23:10:29.259632Z","shell.execute_reply":"2021-12-19T23:10:30.829656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"`░░░░░░░░░░░░░░░░░░░░░▄▀░░▌ GUD.\n░░░░░░░░░░░░░░░░░░░▄▀▐░░░▌\n░░░░░░░░░░░░░░░░▄▀▀▒▐▒░░░▌\n░░░░░▄▀▀▄░░░▄▄▀▀▒▒▒▒▌▒▒░░▌\n░░░░▐▒░░░▀▄▀▒▒▒▒▒▒▒▒▒▒▒▒▒█\n░░░░▌▒░░░░▒▀▄▒▒▒▒▒▒▒▒▒▒▒▒▒▀▄\n░░░░▐▒░░░░░▒▒▒▒▒▒▒▒▒▌▒▐▒▒▒▒▒▀▄\n░░░░▌▀▄░░▒▒▒▒▒▒▒▒▐▒▒▒▌▒▌▒▄▄▒▒▐\n░░░▌▌▒▒▀▒▒▒▒▒▒▒▒▒▒▐▒▒▒▒▒█▄█▌▒▒▌\n░▄▀▒▐▒▒▒▒▒▒▒▒▒▒▒▄▀█▌▒▒▒▒▒▀▀▒▒▐░░░▄\n▀▒▒▒▒▌▒▒▒▒▒▒▒▄▒▐███▌▄▒▒▒▒▒▒▒▄▀▀▀▀\n▒▒▒▒▒▐▒▒▒▒▒▄▀▒▒▒▀▀▀▒▒▒▒▄█▀░░▒▌▀▀▄▄\n▒▒▒▒▒▒█▒▄▄▀▒▒▒▒▒▒▒▒▒▒▒░░▐▒▀▄▀▄░░░░▀\n▒▒▒▒▒▒▒█▒▒▒▒▒▒▒▒▒▄▒▒▒▒▄▀▒▒▒▌░░▀▄\n▒▒▒▒▒▒▒▒▀▄▒▒▒▒▒▒▒▒▀▀▀▀▒▒▒▄▀`","metadata":{}},{"cell_type":"markdown","source":"Aaaaaand , submission ᕕ( ᐛ )ᕗ","metadata":{}},{"cell_type":"code","source":"sub_df.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-12-19T23:10:30.831955Z","iopub.execute_input":"2021-12-19T23:10:30.832348Z","iopub.status.idle":"2021-12-19T23:10:30.939436Z","shell.execute_reply.started":"2021-12-19T23:10:30.832309Z","shell.execute_reply":"2021-12-19T23:10:30.938103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df","metadata":{"execution":{"iopub.status.busy":"2021-12-19T23:10:41.564696Z","iopub.execute_input":"2021-12-19T23:10:41.565223Z","iopub.status.idle":"2021-12-19T23:10:41.594060Z","shell.execute_reply.started":"2021-12-19T23:10:41.565179Z","shell.execute_reply":"2021-12-19T23:10:41.593049Z"},"trusted":true},"execution_count":null,"outputs":[]}]}