{"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":7205982,"sourceType":"datasetVersion","datasetId":4168795},{"sourceId":7289394,"sourceType":"datasetVersion","datasetId":4227408},{"sourceId":7322246,"sourceType":"datasetVersion","datasetId":4249424,"isSourceIdPinned":true},{"sourceId":7452621,"sourceType":"datasetVersion","datasetId":4337737},{"sourceId":7453020,"sourceType":"datasetVersion","datasetId":4337511},{"sourceId":7489733,"sourceType":"datasetVersion","datasetId":4360587,"isSourceIdPinned":true},{"sourceId":7535762,"sourceType":"datasetVersion","datasetId":4271821,"isSourceIdPinned":true},{"sourceId":7557499,"sourceType":"datasetVersion","datasetId":4392661,"isSourceIdPinned":true},{"sourceId":150248402,"sourceType":"kernelVersion"},{"sourceId":156694315,"sourceType":"kernelVersion"},{"sourceId":157632090,"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\nimport torch\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\nimport albumentations as A\nimport gc\nimport sys\n\nfrom torch.utils.data import Dataset, DataLoader\nimport torch.nn.functional as F\nfrom torch.nn.parallel import DataParallel\nfrom dotenv import load_dotenv\n\n\nimport timm\nprint('timm', timm.__version__)\nfrom timm.models.resnet import *","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-02-05T03:48:29.631686Z","iopub.execute_input":"2024-02-05T03:48:29.631952Z","iopub.status.idle":"2024-02-05T03:48:36.637922Z","shell.execute_reply.started":"2024-02-05T03:48:29.631927Z","shell.execute_reply":"2024-02-05T03:48:36.636833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python -m pip install --no-index --find-links=/kaggle/input/pip-download-for-segmentation-models-pytorch segmentation-models-pytorch\n\nimport segmentation_models_pytorch as smp","metadata":{"execution":{"iopub.status.busy":"2024-02-05T03:48:36.640109Z","iopub.execute_input":"2024-02-05T03:48:36.640495Z","iopub.status.idle":"2024-02-05T03:48:55.512176Z","shell.execute_reply.started":"2024-02-05T03:48:36.640461Z","shell.execute_reply":"2024-02-05T03:48:55.511099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# config","metadata":{}},{"cell_type":"code","source":"#!pip3 install /kaggle/input/packegs/einops-0.7.0-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2024-02-05T03:48:55.513729Z","iopub.execute_input":"2024-02-05T03:48:55.514167Z","iopub.status.idle":"2024-02-05T03:48:55.518780Z","shell.execute_reply.started":"2024-02-05T03:48:55.514130Z","shell.execute_reply":"2024-02-05T03:48:55.517733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from einops import rearrange","metadata":{"execution":{"iopub.status.busy":"2024-02-05T03:48:55.520140Z","iopub.execute_input":"2024-02-05T03:48:55.520507Z","iopub.status.idle":"2024-02-05T03:48:55.529197Z","shell.execute_reply.started":"2024-02-05T03:48:55.520473Z","shell.execute_reply":"2024-02-05T03:48:55.528408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#sys.path.append('/kaggle/input/models')\nsys.path.append('/kaggle/input/helper')","metadata":{"execution":{"iopub.status.busy":"2024-02-05T03:48:55.531830Z","iopub.execute_input":"2024-02-05T03:48:55.532138Z","iopub.status.idle":"2024-02-05T03:48:55.537271Z","shell.execute_reply.started":"2024-02-05T03:48:55.532114Z","shell.execute_reply":"2024-02-05T03:48:55.536421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from helper import *","metadata":{"execution":{"iopub.status.busy":"2024-02-05T03:48:55.538240Z","iopub.execute_input":"2024-02-05T03:48:55.538533Z","iopub.status.idle":"2024-02-05T03:48:55.579423Z","shell.execute_reply.started":"2024-02-05T03:48:55.538509Z","shell.execute_reply":"2024-02-05T03:48:55.578434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CFG:\n# ============== model CFG =============\n\n    model_name = 'Unet'\n    backbone = 'se_resnext50_32x4d'\n\n    in_chans = 1 # 65\n    #============== _ CFG =============\n    image_size = 1024\n    input_size= 1024\n    tile_size = image_size\n    stride = tile_size // 2\n    drop_egde_pixel=0\n    \n    target_size = 1\n    chopping_percentile=1e-3\n    # ============== fold =============\n    valid_id = 1\n    batch=2\n    th_percentile = 0.001525\n    model_path=[\"/kaggle/input/trained-model-44/2.5D_unet_epoch_37_loss_0.64_score_0.81_val_loss_0.68_val_score_0.86.pth\"]\n    model1_path = [\"/kaggle/input/sn-hoa-8e-5-27-rot0-5/se_resnext50_32x4d_30_loss0.10_score0.90_val_loss0.13_val_score0.88_midd_1024.pt\"]","metadata":{"execution":{"iopub.status.busy":"2024-02-05T04:08:22.152024Z","iopub.execute_input":"2024-02-05T04:08:22.152728Z","iopub.status.idle":"2024-02-05T04:08:22.158487Z","shell.execute_reply.started":"2024-02-05T04:08:22.152693Z","shell.execute_reply":"2024-02-05T04:08:22.157559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"code","source":"CFG.stride","metadata":{"execution":{"iopub.status.busy":"2024-02-05T03:48:55.595599Z","iopub.execute_input":"2024-02-05T03:48:55.595933Z","iopub.status.idle":"2024-02-05T03:48:55.604024Z","shell.execute_reply.started":"2024-02-05T03:48:55.595899Z","shell.execute_reply":"2024-02-05T03:48:55.603314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class GatingSignal2D(nn.Module):\n    def __init__(self, in_channels, out_channels, batch_norm=False):\n        super(GatingSignal2D, self).__init__()\n\n        self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=1, padding=0)\n        self.batch_norm = nn.BatchNorm2d(out_channels) if batch_norm else None\n\n    def forward(self, inputs):\n        x = self.conv(inputs)\n        if self.batch_norm is not None:\n            x = self.batch_norm(x)\n        x = F.relu(x)\n        return x\n    \n    \n    \nclass AttentionBlock2D(nn.Module):\n    def __init__(self, skip_channel, gate_channel, inter_shape):\n        super(AttentionBlock2D, self).__init__()\n\n        self.theta_x = nn.Conv2d(skip_channel, inter_shape, kernel_size=1, stride=2)\n        self.phi_g = GatingSignal2D(in_channels=gate_channel, out_channels=inter_shape)\n        \n        #self.upsample_g = nn.ConvTranspose3d(inter_shape, inter_shape, kernel_size=3, stride=2, padding=1, output_padding=1)  # Initialize but configure later\n        self.conv_concat = nn.Conv2d(inter_shape, inter_shape, kernel_size=1, padding=0)\n        \n        self.psi = nn.Conv2d(inter_shape, 1, kernel_size=1, padding=0)\n        self.upsample_psi = nn.Upsample(scale_factor=(2, 2), mode='bilinear')\n        self.conv_y = nn.Conv2d(skip_channel, skip_channel, kernel_size=1, padding=0)\n\n    def forward(self, skip, gating):\n        \n        shape_x = skip.size()\n        shape_g = gating.size()\n        \n        theta_x = self.theta_x(skip)\n        phi_g = self.phi_g(gating)\n        concat_xg = phi_g + theta_x\n        \n        act_xg = F.relu(self.conv_concat(concat_xg))\n        psi = self.psi(act_xg)\n        \n        sigmoid_xg = tc.sigmoid(psi)\n        upsample_psi = self.upsample_psi(sigmoid_xg)\n\n        upsample_psi = upsample_psi.repeat(1, shape_x[1], 1, 1)\n        y = upsample_psi * skip\n\n        result = self.conv_y(y)\n        return result\n","metadata":{"execution":{"iopub.status.busy":"2024-02-05T03:48:55.604911Z","iopub.execute_input":"2024-02-05T03:48:55.605160Z","iopub.status.idle":"2024-02-05T03:48:55.620167Z","shell.execute_reply.started":"2024-02-05T03:48:55.605138Z","shell.execute_reply":"2024-02-05T03:48:55.619224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MyDecoderBlock2d(nn.Module):\n    def __init__(\n        self,\n        in_channel,\n        skip_channel,\n        out_channel,\n    ):\n        super().__init__()\n        self.conv1 = nn.Sequential(\n            nn.Conv2d(in_channel + skip_channel, out_channel, kernel_size=3, padding=1, bias=False),\n            nn.BatchNorm2d(out_channel, eps=1e-4),\n            nn.ReLU(inplace=True),\n        )\n\n        self.in_channel = in_channel\n        self.skip_channel = skip_channel\n        self.out_channel = out_channel\n\n        self.upsample = nn.ConvTranspose2d(in_channel, in_channel, kernel_size=3, stride=2, padding=1, output_padding=1)\n        self.attention1 = AttentionBlock2D(skip_channel=skip_channel, gate_channel=in_channel, inter_shape=out_channel)\n        \n        self.conv2 = nn.Sequential(\n            nn.Conv2d(out_channel, out_channel, kernel_size=3, padding=1, bias=False),\n            nn.BatchNorm2d(out_channel, eps=1e-4),\n            nn.ReLU(inplace=True),\n        )\n        \n        self.attention2 = AttentionBlock2D(skip_channel=out_channel, gate_channel=out_channel, inter_shape=out_channel)\n\n    def forward(self, x, skip=None):\n        #x = F.interpolate(x, scale_factor=2, mode='nearest')\n        if skip is not None:\n            attn1 = self.attention1(skip, x)\n            x = self.upsample(x)\n            x = tc.cat([x, attn1], dim=1)\n        \n        x = self.conv1(x)\n        x = self.conv2(x)\n        \n        #x = self.attention2(attn1, x)\n        return x\n\nclass MyUnetDecoder2d(nn.Module):\n    def __init__(self,\n                 in_channel,\n                 skip_channel,\n                 out_channel,\n                 ):\n        super().__init__()\n        self.center = nn.Identity()\n\n        i_channel = [in_channel, ] + out_channel[:-1]\n        s_channel = skip_channel\n        o_channel = out_channel\n        self.in_channel = in_channel\n        self.skip_channel = skip_channel\n        self.out_channel = out_channel\n        block = [\n            MyDecoderBlock2d(i, s, o)\n            for i, s, o in zip(i_channel, s_channel, o_channel)\n        ]\n        self.block = nn.ModuleList(block)\n\n    def forward(self, feature, skip):\n        d = self.center(feature)\n        decode = []\n        for i, block in enumerate(self.block):\n            #print('block skip',i, d.shape, skip[i].shape if skip[i] is not None else 'none')\n            #print(block.conv1[0])\n            #print('')\n            \n            s = skip[i]\n            d = block(d, s)\n            decode.append(d)\n        last = d\n        return last, decode\n\n#--------------------------------------","metadata":{"execution":{"iopub.status.busy":"2024-02-05T03:48:55.621595Z","iopub.execute_input":"2024-02-05T03:48:55.622334Z","iopub.status.idle":"2024-02-05T03:48:55.636479Z","shell.execute_reply.started":"2024-02-05T03:48:55.622301Z","shell.execute_reply":"2024-02-05T03:48:55.635634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Encode(nn.Module):\n    def __init__(self, in_channels1, in_channels2, out_channels, kernel_size=3, stride=1, padding=1):\n        super(Encode, self).__init__()\n        \n        # Concatenate along the channel axis\n        \n        self.concat = nn.ModuleList(\n            [nn.Conv2d(in_channels1[i] + in_channels2[i], out_channels[i], kernel_size=1, stride=1, padding=0) for i in range(len(in_channels1))])\n        \n        # Convolution, Batch Normalization, and ReLU\n        self.conv_bn_relu = nn.ModuleList([nn.Sequential(\n            nn.Conv2d(out_channels[i], out_channels[i], kernel_size, stride, padding),\n            nn.BatchNorm2d(out_channels[i]),\n            nn.ReLU(inplace=True)) for i in range(len(out_channels))]\n        )\n\n    def forward(self, x1, x2):\n        \n        output = []\n        # Concatenate along the channel axis\n        for i in range(len(x1)):\n            concat_result = torch.cat((x1[i], x2[i]), dim=1)\n            output.append(self.conv_bn_relu[i](self.concat[i](concat_result)))\n\n        return output","metadata":{"execution":{"iopub.status.busy":"2024-02-05T03:48:55.637523Z","iopub.execute_input":"2024-02-05T03:48:55.637796Z","iopub.status.idle":"2024-02-05T03:48:55.648758Z","shell.execute_reply.started":"2024-02-05T03:48:55.637756Z","shell.execute_reply":"2024-02-05T03:48:55.647906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\n\"\"\"class Net(nn.Module):\n    def __init__(self, ):\n        super().__init__()\n        \n        \n        encoder_dim1 = [32,64,64,128,256,512]\n        encoder_dim2 = [32,64,256,512,1024,2048]\n        decoder_dim = [256, 128, 64, 64, 32 ]\n\n        self.encoder1 = smp.encoders.get_encoder('tu-maxvit_nano_rw_256', 32,weights=None)\n        self.encoder2 = smp.encoders.get_encoder('tu-seresnext26d_32x4d', 32,weights=None)\n        \n        self.add_conv1 = nn.Sequential(\n            nn.Conv2d(CFG.in_chans, 32, kernel_size=3, padding=1, bias=False),\n            nn.BatchNorm2d(32, eps=1e-4),\n            nn.ReLU(inplace=True),\n            nn.Conv2d(32, 32, kernel_size=3, padding=1, stride=2, bias=False),\n            nn.BatchNorm2d(32, eps=1e-4),\n            nn.ReLU(inplace=True),\n            nn.Conv2d(32, 32, kernel_size=3, padding=1, bias=False),\n            nn.BatchNorm2d(32, eps=1e-4),\n            nn.ReLU(inplace=True),\n        )\n\n        self.last_up = nn.Sequential(\n            nn.ConvTranspose2d(32, 32, kernel_size=3, padding=1, stride=2, output_padding=1, bias=False),\n            nn.BatchNorm2d(32, eps=1e-4),\n            nn.ReLU(inplace=True),\n            nn.Conv2d(32,32,kernel_size=3,padding=1),\n            nn.BatchNorm2d(32, eps=1e-4),\n            nn.ReLU(inplace=True)\n        )\n        \n        self.encode = Encode(encoder_dim1,encoder_dim2,encoder_dim1)\n        \n        self.decoder = MyUnetDecoder3d(\n             in_channel  = encoder_dim1[-1],\n            skip_channel= encoder_dim1[:-1][::-1] + [32],\n            out_channel = decoder_dim,\n        )\n\n        #self.decoder = smp.decoders.unet.decoder.UnetDecoder(\n        #    encoder_channels=encoder_dim1,\n        #    decoder_channels=decoder_dim,\n        #    attention_type='scse',\n        #    center=True\n        #)\n        \n        \n        self.vessel = nn.Conv2d(decoder_dim[-1], CFG.target_size, kernel_size=1)\n        self.act1 = nn.ReLU()\n\n    def forward(self, subvolume):\n        xx = subvolume\n        B, C, H, W = xx.shape\n\n        x = xx\n        #x = xx.expand(-1, 1, -1, -1, -1)\n\n        encode1 = []\n        encode2 = []\n        xx = self.add_conv1(xx)\n\n        \n        encode1 = self.encoder1(xx)\n        encode2 = self.encoder2(xx)\n        \n        encode = self.encode(encode1,encode2)\n        \n        \n        #for i in range(len(encode)):\n        #    print(f'layer {i} shape : {encode[i].shape}')\n        \n        #last = self.decoder(*encode)\n        last, decode = self.decoder(\n        feature=encode[-1], skip=encode[:-1][::-1]\n        )\n        last = self.last_up(last)\n        vessel = self.vessel(last)\n        vessel = vessel\n        return vessel\"\"\"\nlol='lol'","metadata":{"execution":{"iopub.status.busy":"2024-02-05T03:54:11.658595Z","iopub.execute_input":"2024-02-05T03:54:11.659199Z","iopub.status.idle":"2024-02-05T03:54:11.665915Z","shell.execute_reply.started":"2024-02-05T03:54:11.659165Z","shell.execute_reply":"2024-02-05T03:54:11.665048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nclass Net(nn.Module):\n    def __init__(self, ):\n        super().__init__()\n        \n        \n        encoder_dim1 = [32,64,64,128,256,512]\n        encoder_dim2 = [32,64,256,512,1024,2048]\n        decoder_dim = [256, 128, 64, 64, 32 ]\n        encoder_dim = [32,64,64,128,256,512]\n        \n        \n        self.encoder1 = smp.encoders.get_encoder('tu-maxvit_nano_rw_256', 32,weights=None)\n        self.encoder2 = smp.encoders.get_encoder('tu-seresnext26d_32x4d', 32,weights=None)\n        \n        self.add_conv1 = nn.Sequential(\n            nn.Conv2d(CFG.in_chans, 32, kernel_size=3, padding=1, bias=False),\n            nn.BatchNorm2d(32, eps=1e-4),\n            nn.ReLU(inplace=True),\n            nn.Conv2d(32, 32, kernel_size=3, padding=1, stride=2, bias=False),\n            nn.BatchNorm2d(32, eps=1e-4),\n            nn.ReLU(inplace=True),\n            nn.Conv2d(32, 32, kernel_size=3, padding=1, bias=False),\n            nn.BatchNorm2d(32, eps=1e-4),\n            nn.ReLU(inplace=True),\n        )\n\n        self.last_up = nn.Sequential(\n            nn.ConvTranspose2d(32, 32, kernel_size=3, padding=1, stride=2, output_padding=1, bias=False),\n            nn.BatchNorm2d(32, eps=1e-4),\n            nn.ReLU(inplace=True),\n            nn.Conv2d(32,32,kernel_size=3,padding=1),\n            nn.BatchNorm2d(32, eps=1e-4),\n            nn.ReLU(inplace=True)\n        )\n        \n        self.encode = Encode(encoder_dim1,encoder_dim2,encoder_dim)\n        \n        #self.decoder = MyUnetDecoder2d(\n        #    in_channel  = encoder_dim[-1],\n        #    skip_channel= encoder_dim[:-1][::-1] + [32],\n        #    out_channel = decoder_dim,\n        #)\n\n        self.decoder = smp.decoders.unet.decoder.UnetDecoder(\n            encoder_channels=encoder_dim,\n            decoder_channels=decoder_dim,\n            attention_type='scse',\n            center=True\n        )\n        \n        \n        self.vessel = nn.Conv2d(decoder_dim[-1], CFG.target_size, kernel_size=1)\n        self.act1 = nn.ReLU()\n\n    def forward(self, subvolume):\n        xx = subvolume\n        B, C, H, W = xx.shape\n\n        x = xx\n        #x = xx.expand(-1, 1, -1, -1, -1)\n\n        encode1 = []\n        encode2 = []\n        xx = self.add_conv1(xx)\n\n        \n        encode1 = self.encoder1(xx)\n        encode2 = self.encoder2(xx)\n        \n        encode = self.encode(encode1,encode2)\n        \n        \n        #for i in range(len(encode)):\n        #    print(f'layer {i} shape : {encode[i].shape}')\n        \n        last = self.decoder(*encode)\n        #last, decode = self.decoder(\n        #feature=encode[-1], skip=encode[:-1][::-1]\n        #)\n        last = self.last_up(last)\n        vessel = self.vessel(last)\n        vessel = vessel\n        return vessel","metadata":{"execution":{"iopub.status.busy":"2024-02-05T04:05:48.168855Z","iopub.execute_input":"2024-02-05T04:05:48.169615Z","iopub.status.idle":"2024-02-05T04:05:48.184593Z","shell.execute_reply.started":"2024-02-05T04:05:48.169582Z","shell.execute_reply":"2024-02-05T04:05:48.183677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model(weight=None):\n    \n    \n    model = Net()\n    #model =  model.to('cuda')\n    model = nn.DataParallel(model)\n\n    stat_dict = tc.load(CFG.model_path[0],map_location=(tc.device('cpu')))\n    model.load_state_dict(stat_dict)\n    return model","metadata":{"execution":{"iopub.status.busy":"2024-02-05T04:05:50.006751Z","iopub.execute_input":"2024-02-05T04:05:50.007472Z","iopub.status.idle":"2024-02-05T04:05:50.012628Z","shell.execute_reply.started":"2024-02-05T04:05:50.007437Z","shell.execute_reply":"2024-02-05T04:05:50.011597Z"},"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":"class CustomModel(nn.Module):\n    def __init__(self, CFG, weight=None):\n        super().__init__()\n        self.CFG = CFG\n        self.model1 = get_model()\n        \n        \n        self.model = smp.Unet(\n            encoder_name=CFG.backbone, \n            encoder_weights=None,\n            in_channels=CFG.in_chans,\n            classes=CFG.target_size,\n            activation=None,\n        )\n        \n        self.weights = [0.50,0.50]\n        stat_dict = tc.load(CFG.model1_path[0],\"cpu\")\n        stat_dict = {k.replace('model.',''): v for k, v in stat_dict.items()}\n        self.model.load_state_dict(stat_dict)\n        self.model = self.model.to('cuda')\n        self.model = nn.DataParallel(self.model)\n        \n        self.batch=CFG.batch\n\n    def forward_(self, image):\n        output1 = self.model(image)\n        output2 = self.model1(image)\n        weighted_output = self.weights[0] * output1 + self.weights[1] * output2\n        return weighted_output\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) if x[i * self.batch: (i + 1) * self.batch].shape[0] > 0]\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":"2024-02-05T04:13:19.210158Z","iopub.execute_input":"2024-02-05T04:13:19.211076Z","iopub.status.idle":"2024-02-05T04:13:19.229266Z","shell.execute_reply.started":"2024-02-05T04:13:19.211040Z","shell.execute_reply":"2024-02-05T04:13:19.228404Z"},"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":"2024-02-05T04:13:20.200276Z","iopub.execute_input":"2024-02-05T04:13:20.201041Z","iopub.status.idle":"2024-02-05T04:13:20.227525Z","shell.execute_reply.started":"2024-02-05T04:13:20.201011Z","shell.execute_reply":"2024-02-05T04:13:20.226671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def choose_biggest_object(mask, threshold):\n    mask = ((mask > threshold) * 255).astype(np.uint8)\n    num_label, label, stats, centroid = cv2.connectedComponentsWithStats(mask, connectivity=8)\n    max_label = -1\n    max_area = -1\n    for l in range(1, num_label):\n        if stats[l, cv2.CC_STAT_AREA] >= max_area:\n            max_area = stats[l, cv2.CC_STAT_AREA]\n            max_label = l\n    processed = (label==max_label).astype(np.uint8)\n    return processed\n\n\ndef remove_small_objects(mask, min_size, threshold):\n    mask = ((mask > threshold) * 255).astype(np.uint8)\n    # find all connected components (labels)\n    num_label, label, stats, centroid = cv2.connectedComponentsWithStats(mask, connectivity=8)\n    # create a mask where small objects are removed\n    processed = np.zeros_like(mask)\n    for l in range(1, num_label):\n        if stats[l, cv2.CC_STAT_AREA] >= min_size:\n            processed[label == l] = 1\n    return processed","metadata":{"execution":{"iopub.status.busy":"2024-02-05T04:13:20.293086Z","iopub.execute_input":"2024-02-05T04:13:20.293380Z","iopub.status.idle":"2024-02-05T04:13:20.301263Z","shell.execute_reply.started":"2024-02-05T04:13:20.293344Z","shell.execute_reply":"2024-02-05T04:13:20.300384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Build model(s)","metadata":{}},{"cell_type":"code","source":"model=build_model()\nmodel=model.to('cuda')\n#model.load_state_dict(tc.load(CFG.model_path[0],\"cuda\"))\nmodel.eval()\n#model=nn.DataParallel(model)\nlol='lol'","metadata":{"execution":{"iopub.status.busy":"2024-02-05T04:13:20.523084Z","iopub.execute_input":"2024-02-05T04:13:20.523941Z","iopub.status.idle":"2024-02-05T04:13:22.979350Z","shell.execute_reply.started":"2024-02-05T04:13:20.523911Z","shell.execute_reply":"2024-02-05T04:13:22.978496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def print_model_parameters(model):\n    for name, param in model.named_parameters():\n        print(f\"Parameter: {name}, Shape: {param.shape}, Device: {param.device}\")\n","metadata":{"execution":{"iopub.status.busy":"2024-02-05T04:13:22.981342Z","iopub.execute_input":"2024-02-05T04:13:22.982146Z","iopub.status.idle":"2024-02-05T04:13:22.986646Z","shell.execute_reply.started":"2024-02-05T04:13:22.982108Z","shell.execute_reply":"2024-02-05T04:13:22.985770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#del model\ntc.cuda.empty_cache()\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-02-05T04:13:22.987817Z","iopub.execute_input":"2024-02-05T04:13:22.988160Z","iopub.status.idle":"2024-02-05T04:13:23.259626Z","shell.execute_reply.started":"2024-02-05T04:13:22.988128Z","shell.execute_reply":"2024-02-05T04:13:23.258301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\ndef 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        gc.collect()\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=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                #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                print(len(chip))\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(),cmap='gray')\n                    plt.subplot(122)\n                    plt.imshow(mask_pred[0].cpu().detach().numpy(),cmap='gray')\n                    plt.show()\n                    if debug_count>3:\n                        break\n        outputs[0].append(labels)\n        outputs[1].extend(mark)\n    return outputs\n\n","metadata":{"execution":{"iopub.status.busy":"2024-02-05T04:13:23.262355Z","iopub.execute_input":"2024-02-05T04:13:23.262799Z","iopub.status.idle":"2024-02-05T04:13:23.285030Z","shell.execute_reply.started":"2024-02-05T04:13:23.262750Z","shell.execute_reply":"2024-02-05T04:13:23.283868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","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\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\nprint(TH)","metadata":{"execution":{"iopub.status.busy":"2024-02-05T04:13:37.078484Z","iopub.execute_input":"2024-02-05T04:13:37.079328Z","iopub.status.idle":"2024-02-05T04:13:52.842022Z","shell.execute_reply.started":"2024-02-05T04:13:37.079283Z","shell.execute_reply":"2024-02-05T04:13:52.841027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"####################################\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,cmap='gray')\n        plt.show()\n        debug_count+=1\n        if debug_count>6:\n            break\n        \n    #mask_pred = remove_small_objects(mask_pred, min_size=20, threshold=0.1)\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":"2024-02-05T04:14:22.633497Z","iopub.execute_input":"2024-02-05T04:14:22.634238Z","iopub.status.idle":"2024-02-05T04:14:22.875276Z","shell.execute_reply.started":"2024-02-05T04:14:22.634201Z","shell.execute_reply":"2024-02-05T04:14:22.874254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#submission_df = pd.read_csv('/kaggle/working/submission.csv')","metadata":{"execution":{"iopub.status.busy":"2024-02-03T04:51:48.024408Z","iopub.execute_input":"2024-02-03T04:51:48.025126Z","iopub.status.idle":"2024-02-03T04:51:48.029311Z","shell.execute_reply.started":"2024-02-03T04:51:48.025076Z","shell.execute_reply":"2024-02-03T04:51:48.028435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_decode(mask_rle, shape):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (height,width) of array to return \n    Returns numpy array, 1 - mask, 0 - background\n\n    '''\n    s = mask_rle.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    return img.reshape(shape)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-02-03T04:51:48.030602Z","iopub.execute_input":"2024-02-03T04:51:48.030922Z","iopub.status.idle":"2024-02-03T04:51:48.043257Z","shell.execute_reply.started":"2024-02-03T04:51:48.030875Z","shell.execute_reply":"2024-02-03T04:51:48.042251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#j=rle_decode(submission_df['rle'][3],img.shape)\n#plt.figure(figsize=(15,30))\n#plt.imshow(j,cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2024-02-03T04:51:48.044573Z","iopub.execute_input":"2024-02-03T04:51:48.044866Z","iopub.status.idle":"2024-02-03T04:51:48.053837Z","shell.execute_reply.started":"2024-02-03T04:51:48.044839Z","shell.execute_reply":"2024-02-03T04:51:48.052868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#img=cv2.imread('/kaggle/input/blood-vessel-segmentation/train/kidney_2/labels/0003.tif',cv2.IMREAD_GRAYSCALE)\n#plt.imshow(img,cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2024-02-03T04:51:48.055006Z","iopub.execute_input":"2024-02-03T04:51:48.055306Z","iopub.status.idle":"2024-02-03T04:51:48.064235Z","shell.execute_reply.started":"2024-02-03T04:51:48.055281Z","shell.execute_reply":"2024-02-03T04:51:48.063341Z"},"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":[]}]}