{"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":7317236,"sourceType":"datasetVersion","datasetId":4243245},{"sourceId":7340169,"sourceType":"datasetVersion","datasetId":4250312},{"sourceId":7351547,"sourceType":"datasetVersion","datasetId":4265818},{"sourceId":7380587,"sourceType":"datasetVersion","datasetId":4289156},{"sourceId":7401067,"sourceType":"datasetVersion","datasetId":4303540},{"sourceId":7402160,"sourceType":"datasetVersion","datasetId":4249424},{"sourceId":7425532,"sourceType":"datasetVersion","datasetId":4320571},{"sourceId":7431367,"sourceType":"datasetVersion","datasetId":4324557},{"sourceId":7456345,"sourceType":"datasetVersion","datasetId":4340212},{"sourceId":7482783,"sourceType":"datasetVersion","datasetId":4355949},{"sourceId":7503739,"sourceType":"datasetVersion","datasetId":4369711},{"sourceId":7507237,"sourceType":"datasetVersion","datasetId":4372109},{"sourceId":7510845,"sourceType":"datasetVersion","datasetId":4374506},{"sourceId":7518289,"sourceType":"datasetVersion","datasetId":4379404},{"sourceId":150248402,"sourceType":"kernelVersion"}],"dockerImageVersionId":30588,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"### Inference: \nThis notebook 0.859  \n\nVersion 9\n\nmodel:\n/kaggle/input/sn-hoa-8e-5-27-rot0-5/se_resnext50_32x4d_26_loss0.10_score0.90_val_loss0.12_val_score0.88_midd_1024.pt.  From dataset «sn-hoa-8e-5-27-rot0-5» Version 1 name of version \"Version 1 - Initial release\"\n\nidia 0:        \n         th_percentile = 0.00143                            (-> 0.859)\n\n\n# Train: \nhttps://www.kaggle.com/nguynvnthtr/the-training-image-is-1024-x-1024  \n\nversion 1\n\nidia  : image_size = 1024\n\nidia 1:\n\n        x_index= (x.shape[1]-self.image_size)//2 \n        \n        y_index= (x.shape[2]-self.image_size)//2   (-> 0.628)\n        \nidia 2:\n     \n        p_augm = 0.05  #probability of augmentaton changed to 0.05    (-> 0.686)\n        \n\n    \nidia 3:\n\n        in_chans = 1  #window for moov label changed to 1    (-> 0.800)\n        \nidia 4:\n \n        Epoch = 27                                           (-> 0.856)\n\n\nidia 5:\n\n        lr = 8e-5","metadata":{}},{"cell_type":"markdown","source":"# All get from:","metadata":{}},{"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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# config","metadata":{}},{"cell_type":"code","source":"# model_path_i = 0 # 7 #5 #in_chans_1__25     3 in_chans_1__20 2 \n# class CFG:\n# # ============== model CFG =============\n#     model_name = 'Unet++'\n#     backbone = 'se_resnext50_32x4d'#'mobileone_s4'\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 # 4\n#     drop_egde_pixel= 0 #64 # 16 #32\n    \n#     target_size = 1\n#     chopping_percentile=1e-3\n#     # ============== fold =============\n#     valid_id = 1\n#     batch=4 #128\n#     th_percentile = 0.0014109 #0.0014109#0.00149#0.0014109 #0.00143 #0.00145 #0.00146 #0.00149 #0.00145 # 0.0014 #0.00175 #0.0021\n#     # 先用126算了\n    \n#     axis_w = [0.328800989, 0.336629584, 0.334569427]\n#     model_path=[\"/kaggle/input/unetpp-1-3/best_93.pt\"] \n\n\n\n# Defining variables to specify model paths\nmodel_path_i = 0\n# model_path_i9 = 1\n\n# Configuration class containing various model and training parameters\nclass CFG:\n    # Model configuration\n    model_name = 'Unet++'\n    backbone = 'se_resnext50_32x4d'\n    in_chans = 3\n    image_size = 1024\n    input_size = 1024\n    tile_size = image_size\n    stride = tile_size // 4\n    drop_egde_pixel = 0\n    target_size = 3\n    chopping_percentile = 1e-3\n\n    # Fold and validation configuration\n    valid_id = 1\n    batch = 8\n    th_percentile = 0.00149\n#     axis_w = [0.328800989, 0.336629584, 0.334569427]\n#     axis_second_model = 0\n\n    # Model paths for different folds\n    model_path = [\n#         \"/kaggle/input/sn-hoa-8e-5-27-rot0-5/se_resnext50_32x4d_40_loss0.14_score0.86_val_loss0.20_val_score0.80_midd_1024.pt\",  # 8e-5-27-rot0-5\n#         \"/kaggle/input/sennet-kidney-1-and-3/model_real_23.pt\" # 31 8e 05\n        \"/kaggle/input/channel3-unetpp/best_channel3_den1.pt\"\n    ]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"code","source":"\n# class CustomModel(nn.Module):\n#     def __init__(self, CFG, weight=None):\n#         super().__init__()\n#         # self.model = smp.FPN(  #FPN 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.encoder_depth = 4\n#         self.decoder_channels =[512, 256, 128, 64]\n#         self.model = smp.UnetPlusPlus(\n#             encoder_name=CFG.backbone, #self.model_name,\n#             encoder_weights=weight, #\"imagenet\",\n#             in_channels=CFG.in_chans,\n#             classes=CFG.target_size,\n#             encoder_depth=self.encoder_depth,\n#             decoder_channels=self.decoder_channels,\n#             activation =None, #\"sigmoid\",\n#         )\n#         self.batch=CFG.batch\n        \n\n\n# 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\nclass CustomModel(nn.Module):\n    def __init__(self, CFG, weight=None):\n        super().__init__()\n        self.model = smp.UnetPlusPlus(  #tPlusPlus FPN 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        # output = output.squeeze(-1)\n        return output # [:,0]#.sigmoid()\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=x.reshape(4,shape[0],*shape[1:])\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 # (B,3,1024,1024)\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()\n\n# def build_model2(weight=None):\n#     load_dotenv()\n\n#     print('model_name', CFG.model_name)\n#     print('backbone', CFG.backbone)\n\n#     model = CustomModel2(CFG, weight)\n\n#     return model.cuda()\n\n# # Custom model class definition\n# class CustomModel2(nn.Module):\n#     def __init__(self, CFG, weight=None):\n#         super().__init__()\n        \n#         # Storing configuration parameters within the model\n#         self.CFG = CFG\n        \n#         # Creating an instance of the Unet model from segmentation_models_pytorch\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        \n#         # Setting batch size from the configuration\n#         self.batch = CFG.batch\n\n#     def forward_(self, image):\n#         # Forward pass through the model\n#         output = self.model(image)\n#         # Extracting the first channel from the output\n#         return output[:, 0]\n\n#     def forward(self, x: tc.Tensor):\n#         # Converting input tensor to float32\n#         x = x.to(tc.float32)\n        \n#         # Normalizing the input tensor using a custom function 'norm_with_clip'\n#         x = norm_with_clip(x.reshape(-1, *x.shape[2:])).reshape(x.shape)\n        \n#         # Interpolating the input tensor if input size is not equal to image size\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#         # Performing data augmentation by rotating the input tensor\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        \n#         # Using autocast for mixed precision training and no_grad to disable gradient computation\n#         with autocast():\n#             with tc.no_grad():\n#                 # Forward pass for each rotated batch\n#                 x = [self.forward_(x[i * self.batch:(i + 1) * self.batch]) for i in range(x.shape[0] // self.batch + 1)]\n#                 # Concatenating the results along the batch dimension\n#                 x = tc.cat(x, dim=0)\n        \n#         # Applying sigmoid activation and reshaping the tensor\n#         x = x.sigmoid()\n#         x = x.reshape(4, shape[0], *shape[2:])\n        \n#         # Rotating the tensor back to the original orientation\n#         x = [tc.rot90(x[i], k=-i, dims=(-2, -1)) for i in range(4)]\n#         # Stacking along a new dimension and taking the mean\n#         x = tc.stack(x, dim=0).mean(0)\n        \n#         # Interpolating the output tensor if input size is not equal to image size\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        \n#         return x","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Size","metadata":{}},{"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  \n\n\n\ndef to_original_3D(im_pred, img, image_size=1024):\n    # 初始化一个空列表来存储处理后的切片\n    outputs = []\n\n    # 遍历所有 Z 轴上的切片\n    for i in range(im_pred.size(0)):  # im_pred.size(0) 是 Z 轴上的切片数量\n        # 对当前切片应用 to_original 函数进行尺寸还原\n        restored_slice = to_original(im_pred[i], img, image_size=image_size)\n\n        # 将处理后的切片添加到列表中\n        outputs.append(restored_slice)\n\n    # 将列表中的所有切片堆叠成一个新的三维张量\n    # 假设 to_original 函数的输出是一个二维张量 (X_原来, Y_原来)\n    # 使用 torch.stack 并指定堆叠维度为 0，以还原成 (Z, X_原来, Y_原来) 形状\n    output_tensor = tc.stack(outputs, dim=0)\n\n    return output_tensor\n","metadata":{"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 = 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\n\ndef load_data(path,s):\n    data_loader=Data_loader(path,s)\n    data_loader=DataLoader(data_loader, batch_size=16, num_workers=4)\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        self.x=x\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)+self.in_chan-1):\n            ids.append(self.get_mark(index))\n        return ids\n\n#填充太大了，我需要减少计算量\n# def 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\n\n\ndef add_edge_min(x: tc.Tensor, tile_size: int = 1024, stride: int = 256):\n    # x = (C, H, W)\n    # 目标：确保填充后的尺寸至少为 tile_size，并且是 stride 的倍数\n\n    # 计算需要填充到的目标尺寸，确保它是 stride 的倍数\n    target_h = max(tile_size, ((x.shape[1] - tile_size + stride - 1) // stride) * stride + tile_size)\n    target_w = max(tile_size, ((x.shape[2] - tile_size + stride - 1) // stride) * stride + tile_size)\n\n    # 计算每个维度需要填充的大小\n    pad_h = max(0, target_h - x.shape[1])\n    pad_w = max(0, target_w - x.shape[2])\n\n    # 计算填充值\n    mean_ = int(x.to(tc.float32).mean())\n\n    # 对高度进行填充\n    pad_top = pad_h // 2\n    pad_bottom = pad_h - pad_top\n    x = tc.cat([tc.ones([x.shape[0], pad_top, x.shape[2]], dtype=x.dtype, device=x.device) * mean_, x, tc.ones([x.shape[0], pad_bottom, x.shape[2]], dtype=x.dtype, device=x.device) * mean_], dim=1)\n\n    # 对宽度进行填充\n    pad_left = pad_w // 2\n    pad_right = pad_w - pad_left\n    x = tc.cat([tc.ones([x.shape[0], x.shape[1], pad_left], dtype=x.dtype, device=x.device) * mean_, x, tc.ones([x.shape[0], x.shape[1], pad_right], dtype=x.dtype, device=x.device) * mean_], dim=2)\n\n    return x, (pad_top, pad_bottom, pad_left, pad_right)\n","metadata":{"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[ model_path_i ],\"cpu\"))\nmodel.eval()\nmodel=DataParallel(model)\n\n# # Building and loading the 512 x 512 Image Model\n# model9 = build_model2()  # Creating an instance of the model\n# model9.load_state_dict(tc.load(CFG.model_path[model_path_i9], \"cpu\"))  # Loading the pre-trained weights\n# model9.eval()  # Setting the model to evaluation mode\n# model9 = DataParallel(model9)  # Using DataParallel for parallel processing","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_output(debug=False):\n    outputs=[]\n    \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]:#0,1,2\n            debug_count=0\n            if axis==0:\n                x_=x\n                labels_=labels.to(\"cuda:0\")\n            elif axis==1:\n                x_=x.permute(1,2,0)\n                labels_=labels.permute(1,2,0).to(\"cuda:0\")\n            elif axis==2:\n                x_=x.permute(2,0,1)\n                labels_=labels.permute(2,0,1).to(\"cuda:0\")\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            \n            img,_=add_edge_min(dataset.x[0][None], tile_size = 1024, stride= 256)\n            shape=img.shape[-2:]\n            x1_list = np.arange(0, shape[0]-CFG.tile_size+1, CFG.stride)\n            y1_list = np.arange(0, shape[1]-CFG.tile_size+1, CFG.stride)\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#                 if index<2150:\n#                     continue\n                #img=(1,C,H,W)\n                img=img.to(\"cuda:0\")\n#                 img=add_edge(img[0],CFG.tile_size//2)[None]\n                img, (pad_top, pad_bottom, pad_left, pad_right) =add_edge_min(img[0], tile_size = 1024, stride= 256)            \n                img=img[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                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#                 y_preds = (0.25 * y_preds + 0.75 * model9.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[:, pad_top:-pad_bottom if pad_bottom > 0 else None, pad_left:-pad_right if pad_right > 0 else None]\n                #[...,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)\n#                 labels_[index] += (mask_pred[0] * 255 * CFG.axis_w[axis]).to(tc.uint8)\n                labels_[index:index+CFG.target_size] += (mask_pred * 255 / 3/ CFG.target_size).to(tc.uint8)  # 直接在 GPU 上累加\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                if debug:\n                    debug_count+=1\n                    plt.subplot(121)\n                    plt.imshow(img[0,CFG.in_chans//2,pad_top:-pad_bottom if pad_bottom > 0 else None, pad_left:-pad_right if pad_right > 0 else None].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                        \n            if axis==0: \n                labels=labels_.to(tc.uint8).cpu()\n            elif axis==1:\n                labels=labels_.to(tc.uint8).permute(2,0,1).cpu()\n            elif axis==2:\n                labels=labels_.to(tc.uint8).permute(1,2,0).cpu()\n                \n        outputs[0].append(labels)\n        outputs[1].extend(mark)\n    return outputs","metadata":{"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\nimg=cv2.imread(\"/kaggle/input/blood-vessel-segmentation/test/kidney_5/images/0001.tif\",cv2.IMREAD_GRAYSCALE)\nfor i in range(len(output)):\n    output[i]=to_original_3D(output[i], img, image_size=1024)# 提高到这里，这样的TH先验可以不收填充的干扰 #主要是这里只有一个，到时候记得想想怎么改为遍历的\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]\n# TH=126\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    #mask_pred2 = to_original ( mask_pred, img, image_size = 1024 )\n    #mask_pred =  mask_pred.copy()\n    \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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}