{"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":"gpu","dataSources":[{"sourceId":61446,"databundleVersionId":6962461,"sourceType":"competition"},{"sourceId":7526523,"sourceType":"datasetVersion","datasetId":4383869},{"sourceId":7568399,"sourceType":"datasetVersion","datasetId":4406186},{"sourceId":7568690,"sourceType":"datasetVersion","datasetId":4406324},{"sourceId":7573166,"sourceType":"datasetVersion","datasetId":4408848}],"dockerImageVersionId":30626,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-02-06T07:44:15.295227Z","iopub.execute_input":"2024-02-06T07:44:15.296057Z","iopub.status.idle":"2024-02-06T07:44:15.301034Z","shell.execute_reply.started":"2024-02-06T07:44:15.296023Z","shell.execute_reply":"2024-02-06T07:44:15.300047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torchvision.transforms as transforms\nimport matplotlib.pyplot as plt\nimport SimpleITK as sitk\nimport cv2\nimport os\nimport numpy as np\nimport pandas as pd\nimport cv2\nfrom glob import glob\nfrom tqdm import tqdm\nimport torch\nimport torch.nn as nn\nfrom torch.utils.data import Dataset, DataLoader\nimport albumentations as A\nimport torchvision.transforms as transforms\nfrom torch.cuda.amp import autocast","metadata":{"execution":{"iopub.status.busy":"2024-02-06T07:44:15.335437Z","iopub.execute_input":"2024-02-06T07:44:15.335754Z","iopub.status.idle":"2024-02-06T07:44:15.341896Z","shell.execute_reply.started":"2024-02-06T07:44:15.335726Z","shell.execute_reply":"2024-02-06T07:44:15.341092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Squeeze_Excitation(nn.Module):\n    def __init__(self, channel, r=8):\n        super().__init__()\n\n        self.pool = nn.AdaptiveAvgPool2d(1)\n        self.net = nn.Sequential(\n            nn.Linear(channel, channel // r, bias=False),\n            nn.ReLU(inplace=True),\n            nn.Linear(channel // r, channel, bias=False),\n            nn.Sigmoid(),\n        )\n\n    def forward(self, inputs):\n        b, c, _, _ = inputs.shape\n        x = self.pool(inputs).view(b, c)\n        x = self.net(x).view(b, c, 1, 1)\n        x = inputs * x\n        return x\n\nclass Stem_Block(nn.Module):\n    def __init__(self, in_c, out_c, stride):\n        super().__init__()\n\n        self.c1 = nn.Sequential(\n            nn.Conv2d(in_c, out_c, kernel_size=3, stride=stride, padding=1),\n            nn.BatchNorm2d(out_c),\n            nn.ReLU(inplace=True),\n            nn.Conv2d(out_c, out_c, kernel_size=3, padding=1),\n        )\n\n        self.c2 = nn.Sequential(\n            nn.Conv2d(in_c, out_c, kernel_size=1, stride=stride, padding=0),\n            nn.BatchNorm2d(out_c),\n        )\n\n        self.attn = Squeeze_Excitation(out_c)\n\n    def forward(self, inputs):\n        x = self.c1(inputs)\n        s = self.c2(inputs)\n        y = self.attn(x + s)\n        return y\n\nclass DownBlock(nn.Module):\n    expansion = 1\n\n    def __init__(self, inplanes, planes, stride=1, kernel = 3,pad = 1,downsample=None):\n        super().__init__()\n        self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=kernel, stride=stride,\n                     padding=pad, bias=False)\n        self.bn1 = nn.BatchNorm2d(planes)\n        self.relu = nn.ReLU(inplace=True)\n        self.conv2 = nn.Conv2d(planes, planes, kernel_size=kernel, stride=1,\n                     padding=pad, bias=False)\n        self.bn2 = nn.BatchNorm2d(planes)\n        self.downsample = downsample\n        self.stride = stride\n        # self.dp =  nn.Dropout2d(0.2)\n    def forward(self, x):\n        identity = x\n\n        out = self.conv1(x)\n        # out = self.dp(out)\n        out = self.bn1(out)\n        out = self.relu(out)\n        out = self.conv2(out)\n        # out = self.dp(out)\n        out = self.bn2(out)\n        if self.downsample is not None:\n            identity = self.downsample(x)\n\n        out += identity\n        out = self.relu(out)\n        return out\n\nclass UpConv(nn.Module):\n\n    def __init__(self, in_channels, out_channels):\n        super(UpConv, self).__init__()\n\n        self.up = nn.Sequential(\n            nn.ConvTranspose2d(in_channels,out_channels,kernel_size=2,stride=2,padding=0,bias=False),\n            nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=False),\n            #nn.Dropout2d(p=0.2),\n            nn.BatchNorm2d(out_channels),\n            nn.ReLU(inplace=True)\n        )\n\n    def forward(self, x):\n        x = self.up(x)\n        return x\n\nclass ASPP(nn.Module):\n    def __init__(self, in_c, out_c, rate=[1, 6, 12, 18]):\n        super().__init__()\n\n        self.c1 = nn.Sequential(\n            nn.Conv2d(in_c, out_c, kernel_size=3, dilation=rate[0], padding=rate[0]),\n            nn.BatchNorm2d(out_c)\n        )\n\n        self.c2 = nn.Sequential(\n            nn.Conv2d(in_c, out_c, kernel_size=3, dilation=rate[1], padding=rate[1]),\n            nn.BatchNorm2d(out_c)\n        )\n\n        self.c3 = nn.Sequential(\n            nn.Conv2d(in_c, out_c, kernel_size=3, dilation=rate[2], padding=rate[2]),\n            nn.BatchNorm2d(out_c)\n        )\n\n        self.c4 = nn.Sequential(\n            nn.Conv2d(in_c, out_c, kernel_size=3, dilation=rate[3], padding=rate[3]),\n            nn.BatchNorm2d(out_c)\n        )\n\n        self.c5 = nn.Sequential(#nn.AdaptiveAvgPool2d((1, 1)),\n                                nn.Conv2d(out_c, out_c//2, 1, stride=1, bias=False),\n                                nn.BatchNorm2d(out_c//2),\n                                nn.ReLU(inplace=True),\n                                )\n\n\n    def forward(self, inputs):\n        x1 = self.c1(inputs)\n        x2 = self.c2(inputs)\n        x3 = self.c3(inputs)\n        x4 = self.c4(inputs)\n        x = x1 + x2 + x3 + x4\n        y = self.c5(x)\n        return y\n\nclass AttentionBlock(nn.Module):\n    \"\"\"Attention block with learnable parameters\"\"\"\n\n    def __init__(self, F_g, F_l, n_coefficients):\n        \"\"\"\n        :param F_g: number of feature maps (channels) in previous layer\n        :param F_l: number of feature maps in corresponding encoder layer, transferred via skip connection\n        :param n_coefficients: number of learnable multi-dimensional attention coefficients\n        \"\"\"\n        super(AttentionBlock, self).__init__()\n\n        self.W_gate = nn.Sequential(\n            nn.Conv2d(F_g, n_coefficients, kernel_size=1, stride=1, padding='same', bias=True),\n            nn.BatchNorm2d(n_coefficients),\n        )\n\n        self.W_x = nn.Sequential(\n            nn.Conv2d(F_l, n_coefficients, kernel_size=1, stride=1, padding='same', bias=True),\n            nn.BatchNorm2d(n_coefficients),\n\n        )\n\n        self.psi = nn.Sequential(\n            nn.Conv2d(n_coefficients, 1, kernel_size=1, stride=1, padding='same', bias=True),\n            nn.BatchNorm2d(1),\n            nn.Sigmoid()\n        )\n\n        self.relu = nn.ReLU(inplace=True)\n\n    def forward(self, gate, skip_connection):\n        \"\"\"\n        :param gate: gating signal from previous layer\n        :param skip_connection: activation from corresponding encoder layer\n        :return: output activations\n        \"\"\"\n        g1 = self.W_gate(gate)\n        x1 = self.W_x(skip_connection)\n        psi = self.relu(g1 + x1)\n        psi = self.psi(psi)\n        out = skip_connection * psi\n        return out\n\n\nclass ResNet1(nn.Module):\n\n    def __init__(self,num_class=2,blocks=9,in_channels=1,inplanes=32):\n        super().__init__()\n        self.inplanes = inplanes\n        self.num_class = num_class\n\n        self.conv1 = nn.Sequential(nn.Conv2d(in_channels, inplanes, kernel_size=3, stride=1, padding='same',\n                                             bias=False),\n                                   nn.BatchNorm2d(inplanes),\n                                   nn.ReLU(inplace=True),\n                                   # nn.MaxPool2d((2, 2))\n                                   )\n        self.se1 = Squeeze_Excitation(inplanes)\n        self.layer1 = self._make_layer(DownBlock, inplanes*2, 3,stride=2)\n        self.se2 = Squeeze_Excitation(inplanes*2)\n        self.layer2 = self._make_layer(DownBlock, inplanes*4, 4, stride=2)\n        self.se3 = Squeeze_Excitation(inplanes * 4)\n        self.layer3 = self._make_layer(DownBlock, inplanes*8, 7, stride=2)\n        self.se4 = Squeeze_Excitation(inplanes * 8)\n        self.layer4 = self._make_layer(DownBlock, inplanes * 16, 9, stride=2)\n        self.se5 = Squeeze_Excitation(inplanes * 16)\n        self.layeri1 = self._make_layer(DownBlock, inplanes * 32, 3, stride=2)\n\n\n        self.Upo2 = UpConv(inplanes * 32, inplanes * 16)\n        self.layero2 = self._make_layer(DownBlock, inplanes * 16, 2)\n        self.Atto2 = AttentionBlock(F_g=inplanes * 16, F_l=inplanes * 16, n_coefficients=inplanes * 8)\n\n        self.Up1 = UpConv(inplanes * 16, inplanes * 8)\n        self.layer5 = self._make_layer(DownBlock, inplanes * 8, 2)\n        self.Att5 = AttentionBlock(F_g=inplanes * 8, F_l=inplanes * 8, n_coefficients=inplanes * 4)\n\n        self.Up2 = UpConv(inplanes*8,inplanes*4)\n        self.layer6 = self._make_layer(DownBlock, inplanes*4, 2)\n        self.Att4 = AttentionBlock(F_g=inplanes*4, F_l=inplanes*4, n_coefficients=inplanes*2)\n\n        self.Up3 = UpConv(inplanes*4,inplanes*2)\n        self.layer7 = self._make_layer(DownBlock, inplanes*2, 2)\n        self.Att3 = AttentionBlock(F_g=inplanes*2, F_l=inplanes*2, n_coefficients=inplanes)\n\n        # self.layer8 = self._make_layer(DownBlock, inplanes*2, 3)\n        self.Up4 = UpConv(inplanes*2,inplanes)\n        #self.aspp2 = ASPP(inplanes, inplanes*2)\n        self.Conv1= nn.Conv2d(inplanes,num_class,kernel_size=1,stride=1,padding=0)\n\n\n    def _make_layer(self, block, planes, blocks, stride=1,kernel=3,pad=1):\n        downsample = None\n\n        if stride != 1 or self.inplanes != planes:\n            downsample = nn.Sequential(\n                nn.Conv2d(self.inplanes, planes, 1, stride,bias=False),\n                nn.BatchNorm2d(planes),\n            )\n\n        layers = []\n        layers.append(block(self.inplanes, planes, stride, kernel, pad,downsample))\n\n        self.inplanes = planes\n\n        for _ in range(1, blocks):\n            layers.append(block(self.inplanes, planes))\n\n        return nn.Sequential(*layers)\n\n\n    def forward(self, x):\n        x = self.conv1(x)           # 224x224\n        x = self.se1(x)\n        x1 = self.layer1(x) # 56x56\n        x1 = self.se2(x1)\n        x2 = self.layer2(x1)          # 28x28\n        x2 = self.se3(x2)\n        x3 = self.layer3(x2)          # 14x14\n        x3 = self.se4(x3)\n        x4 = self.layer4(x3)  # 7x7\n        x4 = self.se5(x4)\n        x5 = self.layeri1(x4)  # 14x14\n\n        u5 = self.Upo2(x5)\n        a5 = self.Atto2(gate=u5, skip_connection=x4)\n        u5 = torch.cat((a5, u5), dim=1)\n        u5 = self.layero2(u5)\n\n        u4 = self.Up1(u5)\n        a4 = self.Att5(gate=u4, skip_connection=x3)\n        u4 = torch.cat((a4, u4), dim=1)\n        u4 = self.layer5(u4)\n\n        u3 = self.Up2(u4)\n        a3 = self.Att4(gate = u3,skip_connection = x2)\n        u3 = torch.cat((a3, u3), dim=1)\n        u3 = self.layer6(u3)\n\n        u2 = self.Up3(u3)\n        a2 = self.Att3(gate = u2,skip_connection = x1)\n        u2 = torch.cat((a2, u2), dim=1)\n        f1 = self.layer7(u2)\n        # f1 = self.layer8(u2)\n\n        f1 = self.Up4(f1)\n        #f1=self.aspp2(f1)\n        f1 = self.Conv1(f1)\n\n        return f1","metadata":{"execution":{"iopub.status.busy":"2024-02-06T07:44:15.373640Z","iopub.execute_input":"2024-02-06T07:44:15.373927Z","iopub.status.idle":"2024-02-06T07:44:15.423796Z","shell.execute_reply.started":"2024-02-06T07:44:15.373902Z","shell.execute_reply":"2024-02-06T07:44:15.422753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\ndef read_image(path):\n    img = sitk.GetArrayFromImage(sitk.ReadImage(path))\n    img = img.astype('float32')\n    mx = np.max(img)\n    if mx:\n        img /= mx\n    return img\n\ndef patch(img, window, step):\n    s1 = img.shape\n    if ((s1[0] - window[0]) // (step)) * (step) < s1[0]:\n        x = (s1[0] - window[0]) // (step) + 2\n    else:\n        x = (s1[0] - window[0]) // (step) + 1\n    if ((s1[1] - window[1]) // (step)) * (step) < s1[1]:\n        y = (s1[1] - window[1]) // (step) + 2\n    else:\n        y = (s1[1] - window[1]) // (step) + 1\n\n    patches = np.zeros((x, y, window[0], window[1]))\n    for i in range(x):\n        for j in range(y):\n            #             print(step*j,window[1]+step*j)\n            if i == (x - 1) and j != (y - 1):\n                patches[i][j] = img[-window[0]:, step * j:window[1] + step * j]\n\n            elif i != (x - 1) and j == (y - 1):\n                patches[i][j] = img[step * i:window[0] + step * i, -window[1]:]\n\n            elif i != (x - 1) and j != (y - 1):\n                patches[i][j] = img[step * i:window[0] + step * i, step * j:window[1] + step * j]\n            elif i == (x - 1) and j == (y - 1):\n                patches[i][j] = img[-window[0]:, -window[1]:]\n    return patches\n\ndef unpatch(patch, shape, step):\n    img = np.zeros(shape)\n    x = patch.shape[0]\n    y = patch.shape[1]\n    window = patch.shape[2:]\n    for i in range(x):\n        for j in range(y):\n            if i == (x - 1) and j != (y - 1):\n                img[-window[0]:, step * j:window[1] + step * j] = patch[i][j]\n\n            elif i != (x - 1) and j == (y - 1):\n                img[step * i:window[0] + step * i, -window[1]:] = patch[i][j]\n\n            elif i != (x - 1) and j != (y - 1):\n                img[step * i:window[0] + step * i, step * j:window[1] + step * j] = patch[i][j]\n            elif i == (x - 1) and j == (y - 1):\n                img[-window[0]:, -window[1]:] = patch[i][j]\n    return img\n\n\ndef rle_encode(img):\n    '''\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    '''\n    pixels = img.flatten()\n    pixels = np.concatenate([[0], pixels, [0]])\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    rle = ' '.join(str(x) for x in runs)\n    if rle=='':\n        rle = '1 0'\n    return rle\n","metadata":{"execution":{"iopub.status.busy":"2024-02-06T07:44:15.426040Z","iopub.execute_input":"2024-02-06T07:44:15.426446Z","iopub.status.idle":"2024-02-06T07:44:15.445467Z","shell.execute_reply.started":"2024-02-06T07:44:15.426404Z","shell.execute_reply":"2024-02-06T07:44:15.444538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# DATASET_FOLDER = \"/kaggle/input/blood-vessel-segmentation\"\n# image_files = glob(os.path.join(DATASET_FOLDER, \"test\", \"*\", \"*\", \"*.tif\"))\n# print(f\"found images: {len(image_files)}\")","metadata":{"execution":{"iopub.status.busy":"2024-02-06T07:44:15.446645Z","iopub.execute_input":"2024-02-06T07:44:15.447009Z","iopub.status.idle":"2024-02-06T07:44:15.461483Z","shell.execute_reply.started":"2024-02-06T07:44:15.446975Z","shell.execute_reply":"2024-02-06T07:44:15.460635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir = \\\n    '/kaggle/input/blood-vessel-segmentation'\nimage_files = []\nvalid_folder = sorted(glob(f'{data_dir}/test/*'))\n\nfor image_folder in valid_folder:\n    file = sorted(glob(f'{image_folder}/images/*.tif'))\n    image_files.append(file)","metadata":{"execution":{"iopub.status.busy":"2024-02-06T07:44:15.464292Z","iopub.execute_input":"2024-02-06T07:44:15.464706Z","iopub.status.idle":"2024-02-06T07:44:15.475459Z","shell.execute_reply.started":"2024-02-06T07:44:15.464654Z","shell.execute_reply":"2024-02-06T07:44:15.474609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# image_files = load_train('/kaggle/input/blood-vessel-segmentation/train/kidney_1_dense')[1]","metadata":{"execution":{"iopub.status.busy":"2024-02-06T07:44:15.476541Z","iopub.execute_input":"2024-02-06T07:44:15.476803Z","iopub.status.idle":"2024-02-06T07:44:15.485102Z","shell.execute_reply.started":"2024-02-06T07:44:15.476761Z","shell.execute_reply":"2024-02-06T07:44:15.483968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device =torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\ngen = ResNet1(num_class=3)\ngen.load_state_dict(torch.load(\"/kaggle/input/model0207/sen_modelf2.pt\"))\ngen = gen.to(device)\n\ndef norm_with_clip(x,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\ndef tta(in_img):\n    transforms1 = transforms.Compose([  # transforms.ToPILImage(),\n        transforms.ToTensor()])\n    transforms2 = transforms.Compose([\n        transforms.ToTensor(),\n        transforms.RandomHorizontalFlip(p=1)])\n    transforms3 = transforms.Compose([  # transforms.ToPILImage(),\n        transforms.ToTensor(),\n        transforms.RandomVerticalFlip(p=1)])\n    \n\n    in_img1 = transforms1(in_img)\n    in_img1 = in_img1.reshape((1,) + in_img1.shape)\n\n    in_img2 = transforms2(in_img)\n    in_img2 = in_img2.reshape((1,) + in_img2.shape)\n\n    in_img3 = transforms3(in_img)\n    in_img3 = in_img3.reshape((1,) + in_img3.shape)\n    \n    \n#     in_img4,in_img5,in_img6 = torch.rot90(in_img1,dims=[2,3]),torch.rot90(in_img2,dims=[2,3]),torch.rot90(in_img3,dims=[2,3])\n#     print(in_img1.shape,in_img4.shape,in_img5.shape,in_img6.shape)\n#     in_img4 = torch.rot90(in_img1,dims=[2,3])\n    in_img4 = torch.rot90(in_img1,dims=[2,3])\n    in_img5 = torch.rot90(in_img1,k=2,dims=[2,3])\n    \n    return torch.cat((in_img1, in_img2, in_img3,in_img4,in_img5))\n    \n","metadata":{"execution":{"iopub.status.busy":"2024-02-06T07:44:15.486387Z","iopub.execute_input":"2024-02-06T07:44:15.486773Z","iopub.status.idle":"2024-02-06T07:44:21.926148Z","shell.execute_reply.started":"2024-02-06T07:44:15.486745Z","shell.execute_reply":"2024-02-06T07:44:21.925348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rles = []\nids = []\nv_flip = transforms.RandomVerticalFlip(p=1)\nh_flip = transforms.RandomHorizontalFlip(p=1)\naxis0=0\naxis1=0\naxis2=0\ndef sub(xxx):\n    with ((torch.no_grad())):\n        gen.eval()\n        for image_file in image_files:\n            for idxx in tqdm(image_file):\n                image = read_image(idxx)\n                s = image.shape\n                #image = image.reshape((1,) + image.shape)\n                patches = patch(image, (800, 800), step=640)\n                preds1 = np.zeros_like(patches)\n\n                for x in range(patches.shape[0]):\n                    for y in range(patches.shape[1]):\n                        in_img = tta(patches[x][y])\n                        # in_img = in_img.reshape((1,)+in_img.shape)\n                        in_img = in_img.to(device,dtype=torch.float32)\n                        in_img= norm_with_clip(in_img.reshape(-1, *in_img.shape[2:])).reshape(in_img.shape)\n                        with autocast():\n                            with torch.no_grad():\n                                pred = gen(in_img)\n\n                        pred = (pred[0] + h_flip(pred[1]) + v_flip(pred[2])+\n                                                   torch.rot90(pred[3],k=-1,dims=[1,2])+torch.rot90(pred[4],k=-2,dims=[1,2])) / 5\n\n                        preds1[x][y]= torch.argmax(pred,dim=0).cpu().numpy()\n\n                output = np.uint8(unpatch(preds1, s, 640))\n                output[output!=1]=0\n                rle = rle_encode(output)\n                rles.append(rle)\n\n            for p_img in tqdm(image_file):\n                path_ = p_img.split(os.path.sep)\n                # parse the submission ID\n                dataset = path_[-3]\n                slice_id, _ = os.path.splitext(path_[-1])\n                ids.append(f\"{dataset}_{slice_id}\")\n\n    submission = pd.DataFrame.from_dict({\n        \"id\": ids,\n        \"rle\": rles\n    })\n    return submission","metadata":{"execution":{"iopub.status.busy":"2024-02-06T07:44:21.927493Z","iopub.execute_input":"2024-02-06T07:44:21.927859Z","iopub.status.idle":"2024-02-06T07:44:21.942538Z","shell.execute_reply.started":"2024-02-06T07:44:21.927824Z","shell.execute_reply":"2024-02-06T07:44:21.941642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if len(image_files[0])==3 or len(image_files)==2:\n    submission = sub(1)\nelse:\n    submission = sub(3)","metadata":{"execution":{"iopub.status.busy":"2024-02-06T07:44:21.943472Z","iopub.execute_input":"2024-02-06T07:44:21.943747Z","iopub.status.idle":"2024-02-06T07:44:33.417709Z","shell.execute_reply.started":"2024-02-06T07:44:21.943721Z","shell.execute_reply":"2024-02-06T07:44:33.416813Z"},"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":"submission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-02-06T07:44:33.418926Z","iopub.execute_input":"2024-02-06T07:44:33.419216Z","iopub.status.idle":"2024-02-06T07:44:33.425076Z","shell.execute_reply.started":"2024-02-06T07:44:33.419190Z","shell.execute_reply":"2024-02-06T07:44:33.424180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# a = np.array([i.numpy() for i in submission[0]])\n# b = np.array([i.numpy() for i in submission[1]])\n# c = np.array([i.numpy() for i in submission[2]])","metadata":{"execution":{"iopub.status.busy":"2024-02-06T07:44:33.428440Z","iopub.execute_input":"2024-02-06T07:44:33.428925Z","iopub.status.idle":"2024-02-06T07:44:33.439174Z","shell.execute_reply.started":"2024-02-06T07:44:33.428887Z","shell.execute_reply":"2024-02-06T07:44:33.438225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2024-02-06T07:44:33.440191Z","iopub.execute_input":"2024-02-06T07:44:33.440587Z","iopub.status.idle":"2024-02-06T07:44:33.454812Z","shell.execute_reply.started":"2024-02-06T07:44:33.440539Z","shell.execute_reply":"2024-02-06T07:44:33.453840Z"},"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# rles = []\n# ids = []\n# v_flip = transforms.RandomVerticalFlip(p=1)\n# h_flip = transforms.RandomHorizontalFlip(p=1)\n# axis1=1\n# axis2=1\n# def sub(xxx):\n#     with ((torch.no_grad())):\n#         gen.eval()\n#         for image_file in image_files:\n#             img_3d = np.array([read_image(i) for i in image_file])\n\n#             s = img_3d[0].shape\n#             img_sum = torch.zeros((1,2,)+s)\n#             for j in range(xxx):\n#                 if j == 0:\n#                     img_3d = img_3d\n#                 elif j == 1:\n#                     img_3d = np.swapaxes(img_3d, 0, 1)\n#                 elif j == 2:\n#                     img_3d = np.swapaxes(img_3d, 0, 1)\n#                     img_3d = np.swapaxes(img_3d, 0, 2)\n\n#                 for img_2d in tqdm(img_3d):\n#                     patches = patch(img_2d, (512, 512), step=511)\n#                     ps= patches.shape\n#                     preds = torch.zeros((ps[0],ps[1],2,ps[2],ps[3]))\n\n#                     for x in range(patches.shape[0]):\n#                         for y in range(patches.shape[1]):\n\n#                             in_all = tta(patches[x][y])\n\n#                             pred1 = gen(in_all.to(device, dtype=torch.float32))\n\n#                             preds[x][y] = (pred1[0].cpu() + h_flip(pred1[1]).cpu() + v_flip(pred1[2]).cpu()) / 3\n\n#                     output = unpatch(preds,((2,)+s), 511)\n# #                     output = torch.unsqueeze(output,dim=0)\n#                     if j == 0:\n#                         img_sum = torch.cat((img_sum,torch.unsqueeze(output,dim=0)),dim=0)\n#                         print(img_sum.shape)\n#                     elif j == 1:\n#                         img_sum[:,:,axis1,:] +=output\n#                         axis1+=1\n#                     elif j == 2:\n#                         img_sum[:,:,:,axis2] +=output\n#                         axis2+=1\n\n            \n#             for i in range(1,img_sum.shape[0]):\n#             #     pred = ((nn.Sigmoid()(pred)) >= 0.5).double()\n#                 rle = rle_encode(np.uint8((torch.argmax(img_sum[i]/3),dim=0).numpy()))\n#                 rles.append(rle)\n\n#             for p_img in tqdm(image_file):\n#                 path_ = p_img.split(os.path.sep)\n#                 # parse the submission ID\n#                 dataset = path_[-3]\n#                 slice_id, _ = os.path.splitext(path_[-1])\n#                 ids.append(f\"{dataset}_{slice_id}\")\n\n#     submission = pd.DataFrame.from_dict({\n#         \"id\": ids,\n#         \"rle\": rles\n#     })\n#     return submission","metadata":{"execution":{"iopub.status.busy":"2024-02-06T07:44:33.456109Z","iopub.execute_input":"2024-02-06T07:44:33.456371Z","iopub.status.idle":"2024-02-06T07:44:33.465910Z","shell.execute_reply.started":"2024-02-06T07:44:33.456346Z","shell.execute_reply":"2024-02-06T07:44:33.465086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# rles = []\n# ids = []\n# v_flip = transforms.RandomVerticalFlip(p=1)\n# h_flip = transforms.RandomHorizontalFlip(p=1)\n# axis0=0\n# axis1=0\n# axis2=0\n# def sub(xxx):\n#     with ((torch.no_grad())):\n#         gen.eval()\n#         for image_file in [image_files[0]]:\n#             img_3d = image_norm(np.array([read_image(i)[395:907,197:710] for i in image_file[:512]]))\n#             s = img_3d.shape\n#             img_sum = torch.zeros((s[0],2,)+s[1:])\n#             img_sum1=[]\n#             img_sum2 = []\n#             img_sum3=[]\n#             for j in range(xxx):\n#                 if j == 0:\n#                     img_3d = img_3d\n#                 elif j == 1:\n#                     img_3d = np.swapaxes(img_3d, 0, 1)\n#                 elif j == 2:\n#                     img_3d = np.swapaxes(img_3d, 0, 1)\n#                     img_3d = np.swapaxes(img_3d, 0, 2)\n                    \n#                 s = img_3d.shape\n#                 for idxx in tqdm(range(img_3d.shape[0])):\n#                     patches = patch(img_3d[idxx], (256, 256), step=224)\n#                     ps= patches.shape\n#                     preds = torch.zeros((ps[0],ps[1],2,ps[2],ps[3]))\n\n#                     for x in range(patches.shape[0]):\n#                         for y in range(patches.shape[1]):\n\n#                             in_all = tta(patches[x][y])\n\n#                             pred1 = gen(in_all.to(device, dtype=torch.float32))\n\n#                             # pred2 = h_flip(gen(in_img2.to(device, dtype=torch.float32))).cpu()\n#                             # pred3 = v_flip(gen(in_img3.to(device, dtype=torch.float32))).cpu()\n\n#                             preds[x][y] = (pred1[0].cpu() + h_flip(pred1[1]).cpu() + v_flip(pred1[2]).cpu()) / 3\n\n#                     output = unpatch(preds,(2,s[1],s[2]), 224)\n# #                     output = torch.unsqueeze(output,dim=0)\n# #                     if j == 0:\n#                         img_sum1.append(output)\n\n#                     elif j == 1:\n#                         img_sum2.append(output)\n                        \n#                     elif j == 2:\n#                         img_sum3.append(output)\n\n#                     if j == 0:\n#                         img_sum[idxx] +=output\n#                         if idxx+1==img_3d.shape[0]:\n#                             img_sum=img_sum.permute(2,1,0,3)\n#                     elif j == 1:\n#                         img_sum[idxx] +=output\n#                         if idxx+1==img_3d.shape[0]:\n#                             img_sum=img_sum.permute(2,1,0,3)\n#                             img_sum=img_sum.permute(3,1,2,0)\n# #                     elif j == 2:\n# #                         img_sum[idxx] +=output\n# #                         if idxx+1==img_3d.shape[0]:\n# #                             img_sum=img_sum.permute(3,1,2,0)\n\n\n#             for i in range(1,img_sum.shape[0]):\n#             #     pred = ((nn.Sigmoid()(pred)) >= 0.5).double()\n#                 rle = rle_encode(np.uint8((torch.argmax(img_sum[i]/3),dim=0).numpy()))\n#                 rles.append(rle)\n\n#             for p_img in tqdm(image_file):\n#                 path_ = p_img.split(os.path.sep)\n#                 # parse the submission ID\n#                 dataset = path_[-3]\n#                 slice_id, _ = os.path.splitext(path_[-1])\n#                 ids.append(f\"{dataset}_{slice_id}\")\n\n#     submission = pd.DataFrame.from_dict({\n#         \"id\": ids,\n#         \"rle\": rles\n#     })\n#     return img_sum1,img_sum2,img_sum3#np.uint8((torch.argmax(img_sum,dim=1).numpy()))","metadata":{"execution":{"iopub.status.busy":"2024-02-06T07:44:33.467214Z","iopub.execute_input":"2024-02-06T07:44:33.467731Z","iopub.status.idle":"2024-02-06T07:44:33.482720Z","shell.execute_reply.started":"2024-02-06T07:44:33.467696Z","shell.execute_reply":"2024-02-06T07:44:33.481813Z"},"trusted":true},"execution_count":null,"outputs":[]}]}