{"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":61446,"databundleVersionId":6962461,"sourceType":"competition"},{"sourceId":7187369,"sourceType":"datasetVersion","datasetId":4087873},{"sourceId":150248402,"sourceType":"kernelVersion"},{"sourceId":156694315,"sourceType":"kernelVersion"}],"dockerImageVersionId":30588,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"**This code is base on [2.5d segmentaion baseline [inference]](https://www.kaggle.com/code/tanakar/2-5d-segmentaion-baseline-inference)**\nIf you think my code is useful,please upvote it ^w^.\n* Version2:\n1. *     updata normalization method\n2. *     image_size = 512\n3. *     useing 3d TTA\n4. *     se_resnext50_32x4d\n\n* Version3:\n1. *     updata normalization method\n\n* This version is correspond with [2.5d segmentaion baseline [training]](https://www.kaggle.com/code/yoyobar/2-5d-cutting-model-baseline-training) version6\n","metadata":{}},{"cell_type":"markdown","source":"# Import","metadata":{}},{"cell_type":"code","source":"import torch as tc \nimport torch.nn as nn  \nimport numpy as np\nfrom tqdm import tqdm\nfrom torch.cuda.amp import autocast\nimport cv2\nimport os,sys\nfrom glob import glob\nimport matplotlib.pyplot as plt\nimport pandas as pd\n!python -m pip install --no-index --find-links=/kaggle/input/pip-download-for-segmentation-models-pytorch segmentation-models-pytorch\nimport segmentation_models_pytorch as smp\nfrom torch.utils.data import Dataset, DataLoader\nfrom torch.nn.parallel import DataParallel\nfrom dotenv import load_dotenv\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-12-27T11:33:05.540442Z","iopub.execute_input":"2023-12-27T11:33:05.541375Z","iopub.status.idle":"2023-12-27T11:33:17.485094Z","shell.execute_reply.started":"2023-12-27T11:33:05.541338Z","shell.execute_reply":"2023-12-27T11:33:17.483939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# config","metadata":{}},{"cell_type":"code","source":"class CFG:\n# ============== model CFG =============\n    model_name = 'Unet'\n    backbone = 'se_resnext50_32x4d'\n\n    in_chans = 5 # 65\n    #============== _ CFG =============\n    image_size = 512\n    input_size=512\n    tile_size = image_size\n    stride = tile_size // 4\n    drop_egde_pixel=32\n    \n    target_size = 1\n    chopping_percentile=1e-3\n    # ============== fold =============\n    valid_id = 1\n    batch=128\n    th_percentile = 0.0021\n    model_path=[\"/kaggle/input/2-5d-cutting-model-baseline-training/se_resnext50_32x4d_19_loss0.12_score0.79_val_loss0.25_val_score0.79.pt\"]\n","metadata":{"execution":{"iopub.status.busy":"2023-12-27T11:33:17.487076Z","iopub.execute_input":"2023-12-27T11:33:17.487395Z","iopub.status.idle":"2023-12-27T11:33:17.493745Z","shell.execute_reply.started":"2023-12-27T11:33:17.487366Z","shell.execute_reply":"2023-12-27T11:33:17.492854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"code","source":"class CustomModel(nn.Module):\n    def __init__(self, CFG, weight=None):\n        super().__init__()\n        self.CFG = CFG\n        self.model = smp.Unet(\n            encoder_name=CFG.backbone, \n            encoder_weights=weight,\n            in_channels=CFG.in_chans,\n            classes=CFG.target_size,\n            activation=None,\n        )\n        self.batch=CFG.batch\n\n    def forward_(self, image):\n        output = self.model(image)\n        return output[:,0]\n    \n    def forward(self,x:tc.Tensor):\n        #x.shape=(batch,c,h,w)\n        x=x.to(tc.float32)\n        x=norm_with_clip(x.reshape(-1,*x.shape[2:])).reshape(x.shape)\n        \n        if CFG.input_size!=CFG.image_size:\n            x=nn.functional.interpolate(x,size=(CFG.input_size,CFG.input_size),mode='bilinear',align_corners=True)\n        \n        shape=x.shape\n        x=[tc.rot90(x,k=i,dims=(-2,-1)) for i in range(4)]\n        x=tc.cat(x,dim=0)\n        with autocast():\n            with tc.no_grad():\n                x=[self.forward_(x[i*self.batch:(i+1)*self.batch]) for i in range(x.shape[0]//self.batch+1)]\n                # batch=64,64...48\n                x=tc.cat(x,dim=0)\n        x=x.sigmoid()\n        x=x.reshape(4,shape[0],*shape[2:])\n        x=[tc.rot90(x[i],k=-i,dims=(-2,-1)) for i in range(4)]\n        x=tc.stack(x,dim=0).mean(0)\n        \n        if CFG.input_size!=CFG.image_size:\n            x=nn.functional.interpolate(x[None],size=(CFG.image_size,CFG.image_size),mode='bilinear',align_corners=True)[0]\n        return x\n\n\ndef build_model(weight=None):\n    load_dotenv()\n\n    print('model_name', CFG.model_name)\n    print('backbone', CFG.backbone)\n\n    model = CustomModel(CFG, weight)\n\n    return model.cuda()","metadata":{"execution":{"iopub.status.busy":"2023-12-27T11:33:17.495122Z","iopub.execute_input":"2023-12-27T11:33:17.495479Z","iopub.status.idle":"2023-12-27T11:33:17.511174Z","shell.execute_reply.started":"2023-12-27T11:33:17.495447Z","shell.execute_reply":"2023-12-27T11:33:17.510154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Functions","metadata":{}},{"cell_type":"code","source":"def rle_encode(mask):\n    pixel = mask.flatten()\n    pixel = np.concatenate([[0], pixel, [0]])\n    run = np.where(pixel[1:] != pixel[:-1])[0] + 1\n    run[1::2] -= run[::2]\n    rle = ' '.join(str(r) for r in run)\n    if rle == '':\n        rle = '1 0'\n    return rle\n\ndef min_max_normalization(x:tc.Tensor)->tc.Tensor:\n    \"\"\"input.shape=(batch,f1,...)\"\"\"\n    shape=x.shape\n    if x.ndim>2:\n        x=x.reshape(x.shape[0],-1)\n    \n    min_=x.min(dim=-1,keepdim=True)[0]\n    max_=x.max(dim=-1,keepdim=True)[0]\n    if min_.mean()==0 and max_.mean()==1:\n        return x.reshape(shape)\n    \n    x=(x-min_)/(max_-min_+1e-9)\n    return x.reshape(shape)\n\ndef norm_with_clip(x:tc.Tensor,smooth=1e-5):\n    dim=list(range(1,x.ndim))\n    mean=x.mean(dim=dim,keepdim=True)\n    std=x.std(dim=dim,keepdim=True)\n    x=(x-mean)/(std+smooth)\n    x[x>5]=(x[x>5]-5)*1e-3 +5\n    x[x<-3]=(x[x<-3]+3)*1e-3-3\n    return x\n\nclass Data_loader(Dataset):\n    def __init__(self,path,s=\"/images/\"):\n        self.paths=glob(path+f\"{s}*.tif\")\n        self.paths.sort()\n        self.bool=s==\"/labels/\"\n    \n    def __len__(self):\n        return len(self.paths)\n    \n    def __getitem__(self,index):\n        img=cv2.imread(self.paths[index],cv2.IMREAD_GRAYSCALE)\n        img=tc.from_numpy(img)\n        if self.bool:\n            img=img.to(tc.bool)\n        else:\n            img=img.to(tc.uint8)\n        return img\n\ndef load_data(path,s):\n    data_loader=Data_loader(path,s)\n    data_loader=DataLoader(data_loader, batch_size=16, num_workers=2)\n    data=[]\n    for x in tqdm(data_loader):\n        data.append(x)\n    x=tc.cat(data,dim=0)\n    ########################################################################\n    TH=x.reshape(-1).numpy()\n    index = -int(len(TH) * CFG.chopping_percentile)\n    TH:int = np.partition(TH, index)[index]\n    x[x>TH]=int(TH)\n    ########################################################################\n    TH=x.reshape(-1).numpy()\n    index = -int(len(TH) * CFG.chopping_percentile)\n    TH:int = np.partition(TH, -index)[-index]\n    x[x<TH]=int(TH)\n    ########################################################################\n    #x=(min_max_normalization(x.to(tc.float16))*255).to(tc.uint8)\n    return x\n\nclass Pipeline_Dataset(Dataset):\n    def __init__(self,x,path):\n        self.img_paths  = glob(path+\"/images/*\")\n        self.img_paths.sort()\n        self.in_chan = CFG.in_chans\n        z=tc.zeros(self.in_chan//2,*x.shape[1:],dtype=x.dtype)\n        self.x=tc.cat((z,x,z),dim=0)\n        \n    def __len__(self):\n        return self.x.shape[0]-self.in_chan+1\n    \n    def __getitem__(self, index):\n        x  = self.x[index:index+self.in_chan]\n        return x,index\n    \n    def get_mark(self,index):\n        id=self.img_paths[index].split(\"/\")[-3:]\n        id.pop(1)\n        id=\"_\".join(id)\n        return id[:-4]\n    \n    def get_marks(self):\n        ids=[]\n        for index in range(len(self)):\n            ids.append(self.get_mark(index))\n        return ids\n\ndef add_edge(x:tc.Tensor,edge:int):\n    #x=(C,H,W)\n    #output=(C,H+2*edge,W+2*edge)\n    mean_=int(x.to(tc.float32).mean())\n    x=tc.cat([x,tc.ones([x.shape[0],edge,x.shape[2]],dtype=x.dtype,device=x.device)*mean_],dim=1)\n    x=tc.cat([x,tc.ones([x.shape[0],x.shape[1],edge],dtype=x.dtype,device=x.device)*mean_],dim=2)\n    x=tc.cat([tc.ones([x.shape[0],edge,x.shape[2]],dtype=x.dtype,device=x.device)*mean_,x],dim=1)\n    x=tc.cat([tc.ones([x.shape[0],x.shape[1],edge],dtype=x.dtype,device=x.device)*mean_,x],dim=2)\n    return x","metadata":{"execution":{"iopub.status.busy":"2023-12-27T11:33:17.513262Z","iopub.execute_input":"2023-12-27T11:33:17.513522Z","iopub.status.idle":"2023-12-27T11:33:17.540545Z","shell.execute_reply.started":"2023-12-27T11:33:17.513499Z","shell.execute_reply":"2023-12-27T11:33:17.539595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Build model(s)","metadata":{}},{"cell_type":"code","source":"model=build_model()\nmodel.load_state_dict(tc.load(CFG.model_path[0],\"cpu\"))\nmodel.eval()\nmodel=DataParallel(model)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T11:33:17.541526Z","iopub.execute_input":"2023-12-27T11:33:17.541778Z","iopub.status.idle":"2023-12-27T11:33:18.084421Z","shell.execute_reply.started":"2023-12-27T11:33:17.541755Z","shell.execute_reply":"2023-12-27T11:33:18.083503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_output(debug=False):\n    outputs=[]\n    if debug:\n        paths=[\"/kaggle/input/blood-vessel-segmentation/train/kidney_2\"]\n    else:\n        paths=glob(\"/kaggle/input/blood-vessel-segmentation/test/*\")\n    outputs=[[],[]]\n    for path in paths:\n        x=load_data(path,\"/images/\")\n        labels=tc.zeros_like(x,dtype=tc.uint8)\n        mark=Pipeline_Dataset(x,path).get_marks()\n        for axis in [0,1,2]:\n            debug_count=0\n            if axis==0:\n                x_=x\n                labels_=labels\n            elif axis==1:\n                x_=x.permute(1,2,0)\n                labels_=labels.permute(1,2,0)\n            elif axis==2:\n                x_=x.permute(2,0,1)\n                labels_=labels.permute(2,0,1)\n            if x.shape[0]==3 and axis!=0:\n                break\n            dataset=Pipeline_Dataset(x_,path)\n            dataloader=DataLoader(dataset,batch_size=1,shuffle=False,num_workers=2)\n            shape=dataset.x.shape[-2:]\n            x1_list = np.arange(0, shape[0]+CFG.tile_size-CFG.tile_size+1, CFG.stride)\n            y1_list = np.arange(0, shape[1]+CFG.tile_size-CFG.tile_size+1, CFG.stride)\n            for img,index in tqdm(dataloader):\n                #img=(1,C,H,W)\n                img=img.to(\"cuda:0\")\n                img=add_edge(img[0],CFG.tile_size//2)[None]\n\n                mask_pred = tc.zeros_like(img[:,0],dtype=tc.float32,device=img.device)\n                mask_count = tc.zeros_like(img[:,0],dtype=tc.float32,device=img.device)\n\n                indexs=[]\n                chip=[]\n                for y1 in y1_list:\n                    for x1 in x1_list:\n                        x2 = x1 + CFG.tile_size\n                        y2 = y1 + CFG.tile_size\n                        indexs.append([x1+CFG.drop_egde_pixel,x2-CFG.drop_egde_pixel,\n                                       y1+CFG.drop_egde_pixel,y2-CFG.drop_egde_pixel])\n                        chip.append(img[...,x1:x2,y1:y2])\n\n                y_preds = model.forward(tc.cat(chip)).to(device=0)\n\n                if CFG.drop_egde_pixel:\n                    y_preds=y_preds[...,CFG.drop_egde_pixel:-CFG.drop_egde_pixel,\n                                        CFG.drop_egde_pixel:-CFG.drop_egde_pixel]\n                for i,(x1,x2,y1,y2) in enumerate(indexs):\n                    mask_pred[...,x1:x2, y1:y2] += y_preds[i]\n                    mask_count[...,x1:x2, y1:y2] += 1\n\n                mask_pred /= mask_count\n\n                #Rrecover\n                mask_pred=mask_pred[...,CFG.tile_size//2:-CFG.tile_size//2,CFG.tile_size//2:-CFG.tile_size//2]\n                \n                labels_[index]+=(mask_pred[0]*255/3).to(tc.uint8).cpu()\n                if debug:\n                    debug_count+=1\n                    plt.subplot(121)\n                    plt.imshow(img[0,CFG.in_chans//2].cpu().detach().numpy())\n                    plt.subplot(122)\n                    plt.imshow(mask_pred[0].cpu().detach().numpy())\n                    plt.show()\n                    if debug_count>3:\n                        break\n        outputs[0].append(labels)\n        outputs[1].extend(mark)\n    return outputs","metadata":{"execution":{"iopub.status.busy":"2023-12-27T11:33:18.085673Z","iopub.execute_input":"2023-12-27T11:33:18.085986Z","iopub.status.idle":"2023-12-27T11:33:18.106261Z","shell.execute_reply.started":"2023-12-27T11:33:18.085960Z","shell.execute_reply":"2023-12-27T11:33:18.105196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"is_submit=len(glob(\"/kaggle/input/blood-vessel-segmentation/test/kidney_5/images/*.tif\"))!=3\n#is_submit=True\noutput,ids=get_output(not is_submit)\n\n\n####################################\nTH=[x.flatten().numpy() for x in output]\nTH=np.concatenate(TH)\nindex = -int(len(TH) * CFG.th_percentile)\nTH:int = np.partition(TH, index)[index]\nprint(TH)\n\n####################################\nsubmission_df=[]\ndebug_count=0\nfor index in range(len(ids)):\n    id=ids[index]\n    i=0\n    for x in output:\n        if index>=len(x):\n            index-=len(x)\n            i+=1\n        else:\n            break\n    mask_pred=(output[i][index]>TH).numpy()\n    ####################################\n    if not is_submit:\n        plt.subplot(121)\n        plt.imshow(mask_pred)\n        plt.show()\n        debug_count+=1\n        if debug_count>6:\n            break\n        \n    rle = rle_encode(mask_pred)\n    \n    submission_df.append(\n        pd.DataFrame(data={\n            'id'  : id,\n            'rle' : rle,\n        },index=[0])\n    )\n\nsubmission_df =pd.concat(submission_df)\nsubmission_df.to_csv('submission.csv', index=False)\nsubmission_df.head(6)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T11:33:18.107531Z","iopub.execute_input":"2023-12-27T11:33:18.108202Z","iopub.status.idle":"2023-12-27T11:36:20.192303Z","shell.execute_reply.started":"2023-12-27T11:33:18.108167Z","shell.execute_reply":"2023-12-27T11:36:20.191145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}