{"metadata":{"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":61446,"databundleVersionId":6962461,"sourceType":"competition"},{"sourceId":7187369,"sourceType":"datasetVersion","datasetId":4087873},{"sourceId":7312958,"sourceType":"datasetVersion","datasetId":4229452},{"sourceId":7317236,"sourceType":"datasetVersion","datasetId":4243245},{"sourceId":7402160,"sourceType":"datasetVersion","datasetId":4249424},{"sourceId":7462566,"sourceType":"datasetVersion","datasetId":4274471},{"sourceId":7535010,"sourceType":"datasetVersion","datasetId":4305091},{"sourceId":150248402,"sourceType":"kernelVersion"},{"sourceId":156694315,"sourceType":"kernelVersion"}],"dockerImageVersionId":30636,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.10.12"},"papermill":{"default_parameters":{},"duration":1768.453118,"end_time":"2024-01-12T02:15:23.097132","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2024-01-12T01:45:54.644014","version":"2.4.0"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# All get from:","metadata":{"papermill":{"duration":0.005475,"end_time":"2024-01-12T01:45:59.631557","exception":false,"start_time":"2024-01-12T01:45:59.626082","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"# Import","metadata":{"papermill":{"duration":0.00496,"end_time":"2024-01-12T01:45:59.652681","exception":false,"start_time":"2024-01-12T01:45:59.647721","status":"completed"},"tags":[]}},{"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\nimport gc","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"_kg_hide-output":true,"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":33.620518,"end_time":"2024-01-12T01:46:33.278620","exception":false,"start_time":"2024-01-12T01:45:59.658102","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-03T13:06:39.448034Z","iopub.execute_input":"2024-02-03T13:06:39.448410Z","iopub.status.idle":"2024-02-03T13:07:02.406008Z","shell.execute_reply.started":"2024-02-03T13:06:39.448381Z","shell.execute_reply":"2024-02-03T13:07:02.405164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# config","metadata":{"papermill":{"duration":0.007745,"end_time":"2024-01-12T01:46:33.294761","exception":false,"start_time":"2024-01-12T01:46:33.287016","status":"completed"},"tags":[]}},{"cell_type":"code","source":"model_path_i = -1 # 7 #5 #in_chans_1__25     3 in_chans_1__20 2 \nclass CFG:\n# ============== model CFG =============\n    model_name = 'Unet'\n    backbone = 'tu-gcresnext50ts'\n\n    in_chans = 1 #5 # 65\n    #============== _ CFG =============\n    image_size = 1024 #512\n    input_size= 1024 #512\n    tile_size = image_size\n    stride = tile_size // 4\n    drop_egde_pixel= 0 # 16 #32\n    \n    target_size = 1\n    chopping_percentile=1e-3\n    # ============== fold =============\n    valid_id = 1\n    batch=16 #128\n    th_percentile = 0.00143 #0.00145 #0.00146 #0.00149 #0.00145 # 0.0014 #0.00175 #0.0021\n    \n    #axis_w = [0.3353333 ,0.3323333,0.3323333 ]\n    model_path=[\"/kaggle/input/tu-gcresnext50ts/2d/tu-gcresnext50ts_50_loss0.0460_score0.8965_val_loss0.1686_val_score0.8924_midd_1024.pt\",\n                \"/kaggle/input/tu-gcresnext50ts/mixup_1c/tu-gcresnext50ts_155_loss0.2500_score0.6543_val_loss0.1121_val_score0.9025_midd_1024.pt\",\n               \"/kaggle/input/tu-gcresnext50ts/mixup_1c/tu-gcresnext50ts_310_loss0.2445_score0.6541_val_loss0.1670_val_score0.8632_midd_1024.pt\",\n               \"/kaggle/input/tu-gcresnext50ts/tu-gcresnext50ts_380_loss0.1685_score0.7487_val_loss0.1646_val_score0.8759_midd_1024.pt\",\n               \"/kaggle/input/tu-gcresnext50ts/tu-gcresnext50ts_375_loss0.1731_score0.7428_val_loss0.1550_val_score0.8810_midd_1024.pt\",\n               \"/kaggle/input/tu-gcresnext50ts/tu-gcresnext50ts_385_loss0.1475_score0.7667_val_loss0.1486_val_score0.8778_midd_1024.pt\",\n               \"/kaggle/input/tu-gcresnext50ts/tu-gcresnext50ts_390_loss0.1527_score0.7570_val_loss0.1495_val_score0.8766_midd_1024.pt\",\n               \"/kaggle/input/tu-gcresnext50ts/tu-gc-mixup-2/tu-gcresnext50ts_450_loss0.1563_score0.7649_val_loss0.1411_val_score0.8882_midd_1024.pt\",\n               \"/kaggle/input/tu-gcresnext50ts/tu-gc-mixup-2/tu-gcresnext50ts_400_loss0.1639_score0.7511_val_loss0.1505_val_score0.8870_midd_1024.pt\",\n               \"/kaggle/input/tu-gcresnext50ts/tu-gc-mixup-2/tu-gcresnext50ts_350_loss0.1653_score0.7539_val_loss0.1376_val_score0.8748_midd_1024.pt\",\n               \"/kaggle/input/tu-gcresnext50ts/tu-gc-mixup-2/tu-gcresnext50ts_500_loss0.1670_score0.7506_val_loss0.1520_val_score0.8841_midd_1024.pt\",\n               \"/kaggle/input/tu-gcresnext50ts/tu-gc-mixup-2/tu-gcresnext50ts_520_loss0.1605_score0.7601_val_loss0.1378_val_score0.8933_midd_1024.pt\",\n               \"/kaggle/input/tu-gcresnext50ts/tu-gc-mixup-2/tu-gcresnext50ts_550_loss0.1520_score0.7687_val_loss0.1480_val_score0.8742_midd_1024.pt\",\n               \"/kaggle/input/tu-gcresnext50ts/tu-gc-mixup-2/tu-gcresnext50ts_600_loss0.1652_score0.7511_val_loss0.1160_val_score0.8877_midd_1024.pt\",\n               \"/kaggle/input/tu-gcresnext50ts/tu-gc-mixup-2/tu-gcresnext50ts_630_loss0.1569_score0.7642_val_loss0.1283_val_score0.8917_midd_1024.pt\",\n               \"/kaggle/input/tu-gcresnext50ts/scale-tu-gc/tu-gcresnext50ts_80_loss0.1583_score0.7585_val_loss0.1137_val_score0.8988_midd_1024.pt\",\n               \"/kaggle/input/tu-gcresnext50ts/scale-tu-gc/tu-gcresnext50ts_146_loss0.1513_score0.7666_val_loss0.1252_val_score0.8924_midd_1024.pt\",\n               \"/kaggle/input/tu-gcresnext50ts/tu-gc-ema/tu-gcresnext50ts_300_loss0.0998_score0.8266_val_loss0.1598_val_score0.0179_midd_1024.pt\",\n               \"/kaggle/input/tu-gcresnext50ts/tu-gc-ema/tu-gcresnext50ts_350_loss0.1104_score0.8089_val_loss0.1596_val_score0.0179_midd_1024.pt\",\n               \"/kaggle/input/tu-gcresnext50ts/tu-gc-ema/tu-gcresnext50ts_250_loss0.0979_score0.8328_val_loss0.1668_val_score0.0179_midd_1024.pt\"]#  31 8e 05  \n    print(model_path[model_path_i])","metadata":{"papermill":{"duration":0.020775,"end_time":"2024-01-12T01:46:33.323493","exception":false,"start_time":"2024-01-12T01:46:33.302718","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-03T13:08:52.314220Z","iopub.execute_input":"2024-02-03T13:08:52.315105Z","iopub.status.idle":"2024-02-03T13:08:52.324368Z","shell.execute_reply.started":"2024-02-03T13:08:52.315070Z","shell.execute_reply":"2024-02-03T13:08:52.323341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{"papermill":{"duration":0.007905,"end_time":"2024-01-12T01:46:33.339800","exception":false,"start_time":"2024-01-12T01:46:33.331895","status":"completed"},"tags":[]}},{"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":{"papermill":{"duration":0.026601,"end_time":"2024-01-12T01:46:33.374657","exception":false,"start_time":"2024-01-12T01:46:33.348056","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-03T13:07:02.418507Z","iopub.execute_input":"2024-02-03T13:07:02.419081Z","iopub.status.idle":"2024-02-03T13:07:02.434072Z","shell.execute_reply.started":"2024-02-03T13:07:02.419047Z","shell.execute_reply":"2024-02-03T13:07:02.433240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Size","metadata":{"papermill":{"duration":0.007651,"end_time":"2024-01-12T01:46:33.390399","exception":false,"start_time":"2024-01-12T01:46:33.382748","status":"completed"},"tags":[]}},{"cell_type":"code","source":"def to_1024(img , image_size = 1024):\n    if image_size > img.shape[1]:\n       img = np.rot90(img)\n       start1 = (CFG.image_size - img.shape[0])//2 \n       top =     img[0                    : start1,   0: img.shape[1] ]\n       bottom  = img[img.shape[0] -start1 : img.shape[0],   0 : img.shape[1] ]\n       img_result = np.concatenate((top,img,bottom ),axis=0)\n       img_result = np.rot90(img_result)\n       img_result = np.rot90(img_result)\n       img_result = np.rot90(img_result)\n    else :\n       img_result = img\n    return img_result\n\ndef to_1024_no_rot(img, image_size = 1024):\n    if image_size > img.shape[0]:  \n       start1 = ( image_size - img.shape[0])//2\n       top =     img[0                    : start1,   0: img.shape[1] ]\n       bottom  = img[img.shape[0] -start1 : img.shape[0],   0 : img.shape[1] ]\n       img_result = np.concatenate((top,img,bottom ),axis=0)\n    else: \n       img_result = img\n    return img_result\n\ndef to_1024_1024(img  , image_size = 1024 ):\n     img_result = to_1024(img, image_size )\n     return img_result\n    \ndef to_original ( im_after, img, image_size = 1024 ):\n    top_ = 0\n    left_ = 0\n    if (im_after.shape[0] > img.shape[0]):\n             top_  = ( image_size - img.shape[0])//2 \n    if    (im_after.shape[1] > img.shape[1]) :\n             left_  = ( image_size - img.shape[1])//2  \n    if (top_>0)or (left_>0) :\n             img_result = im_after[top_  : img.shape[0] + top_,   left_: img.shape[1] + left_ ]\n    else:\n             img_result = im_after\n    return img_result  ","metadata":{"papermill":{"duration":0.024845,"end_time":"2024-01-12T01:46:33.422971","exception":false,"start_time":"2024-01-12T01:46:33.398126","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-03T13:07:02.436329Z","iopub.execute_input":"2024-02-03T13:07:02.436690Z","iopub.status.idle":"2024-02-03T13:07:02.450072Z","shell.execute_reply.started":"2024-02-03T13:07:02.436657Z","shell.execute_reply":"2024-02-03T13:07:02.449122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Functions","metadata":{"papermill":{"duration":0.007493,"end_time":"2024-01-12T01:46:33.438395","exception":false,"start_time":"2024-01-12T01:46:33.430902","status":"completed"},"tags":[]}},{"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        ims = img.shape\n\n        img = to_1024_1024(img , image_size = CFG.image_size )\n        \n        img=tc.from_numpy(img.copy())\n        if self.bool:\n            img=img.to(tc.bool)\n        else:\n            img=img.to(tc.uint8)\n        return img, ims\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, ims in tqdm(data_loader):\n        data.append(x)\n        ims = (ims[0][0].item(), ims[1][0].item())\n        print('load data', ims)\n    x=tc.cat(data,dim=0)\n    del data\n    gc.collect()\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, ims\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":{"papermill":{"duration":0.039087,"end_time":"2024-01-12T01:46:33.485315","exception":false,"start_time":"2024-01-12T01:46:33.446228","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-03T13:07:02.451459Z","iopub.execute_input":"2024-02-03T13:07:02.452163Z","iopub.status.idle":"2024-02-03T13:07:02.618703Z","shell.execute_reply.started":"2024-02-03T13:07:02.452127Z","shell.execute_reply":"2024-02-03T13:07:02.617891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Build model(s)","metadata":{"papermill":{"duration":0.007582,"end_time":"2024-01-12T01:46:33.500858","exception":false,"start_time":"2024-01-12T01:46:33.493276","status":"completed"},"tags":[]}},{"cell_type":"code","source":"model=build_model()\nmodel=DataParallel(model)\nmodel.load_state_dict(tc.load(CFG.model_path[ model_path_i ],\"cpu\"))\nmodel.eval()\nprint(CFG.model_path[ model_path_i])","metadata":{"papermill":{"duration":2.373251,"end_time":"2024-01-12T01:46:35.882041","exception":false,"start_time":"2024-01-12T01:46:33.508790","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-03T13:09:02.999025Z","iopub.execute_input":"2024-02-03T13:09:02.999752Z","iopub.status.idle":"2024-02-03T13:09:04.387635Z","shell.execute_reply.started":"2024-02-03T13:09:02.999718Z","shell.execute_reply":"2024-02-03T13:09:04.386666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_output(debug=False):\n    outputs=[]\n    tc.cuda.empty_cache()\n    gc.collect()\n    if debug:\n        paths=[\"/kaggle/input/blood-vessel-segmentation/test/kidney_6\"]\n    else:\n        paths=glob(\"/kaggle/input/blood-vessel-segmentation/test/*\")\n    outputs=[[],[],[]]\n    for path in paths:\n        x, ims =load_data(path,\"/images/\")\n        print('skrrrr', ims)\n        labels=tc.zeros_like(x,dtype=tc.uint8)\n        mark=Pipeline_Dataset(x,path).get_marks()\n        ims = [ims for item in mark]\n        print(ims)\n        print(mark)\n        \n        aug_list = [0,1,2]\n        for axis in aug_list:\n            print('axis', axis)\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            print(x.shape)\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=1)\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                print(\"axis, index\", axis, index)\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 /len(aug_list)).to(tc.uint8).cpu()\n                tc.cuda.empty_cache()\n                gc.collect()\n                if debug:\n                    debug_count+=1\n                    print(debug_count)\n                    if debug_count>100:\n                        break\n            tc.cuda.empty_cache()\n            gc.collect()                \n        outputs[0].append(labels)\n        outputs[1].extend(mark)\n        outputs[2].extend(ims)\n    return outputs","metadata":{"papermill":{"duration":0.031711,"end_time":"2024-01-12T01:46:35.922370","exception":false,"start_time":"2024-01-12T01:46:35.890659","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-03T13:07:04.265507Z","iopub.execute_input":"2024-02-03T13:07:04.265778Z","iopub.status.idle":"2024-02-03T13:07:04.286487Z","shell.execute_reply.started":"2024-02-03T13:07:04.265754Z","shell.execute_reply":"2024-02-03T13:07:04.285629Z"},"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\nimg=cv2.imread(\"/kaggle/input/blood-vessel-segmentation/test/kidney_5/images/0001.tif\",cv2.IMREAD_GRAYSCALE)\nprint('skrrr11111111111111', img.shape)\noutput,ids,ims_list =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\", TH)\n\n\n####################################\nsubmission_df=[]\ndebug_count=0\nfor index in range(len(ids)):\n    id=ids[index]\n    ims = ims_list[index]\n    img = np.zeros(ims)\n    print('np size', img.shape)\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    mask_pred2 = to_original ( mask_pred, img, image_size = 1024 )\n    mask_pred =  mask_pred2.copy()\n    \n    ####################################\n    if not is_submit:\n        debug_count+=1\n        print(debug_count)\n#         plt.subplot(121)\n#         plt.imshow(mask_pred)\n#         plt.show()\n        if debug_count>100:\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)\n#1051 1 1187 2 1201 1 1963 1 7851 1 25901 1 268...","metadata":{"papermill":{"duration":1723.506231,"end_time":"2024-01-12T02:15:19.436805","exception":false,"start_time":"2024-01-12T01:46:35.930574","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-03T13:09:10.671137Z","iopub.execute_input":"2024-02-03T13:09:10.672025Z","iopub.status.idle":"2024-02-03T13:09:20.288084Z","shell.execute_reply.started":"2024-02-03T13:09:10.671987Z","shell.execute_reply":"2024-02-03T13:09:20.287040Z"},"trusted":true},"execution_count":null,"outputs":[]}]}