{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Baselines\n\n|                         |                                           Kaggle Train                                          |                                                Local Train                                               |                                           Kaggle Submission                                          |\n|:-----------------------:|:-----------------------------------------------------------------------------------------------:|:--------------------------------------------------------------------------------------------------------:|:----------------------------------------------------------------------------------------------------:|\n|  Efficient_Net_Unet_256 |    [url](https://www.kaggle.com/code/yuliknormanowen/hubmap-hap-efficientnet-unet-256-train)    |      [url](https://www.kaggle.com/datasets/yuliknormanowen/ucode-hubmap-efficientnet-train-local-v2)     |    [url](https://www.kaggle.com/code/yuliknormanowen/hubmap-hap-efficientnet-unet-256-submission)    |\n|  Efficient_Net_Unet_768 |    [url]( https://www.kaggle.com/code/yuliknormanowen/hubmap-hap-efficientnet-unet-768-train)   |     [url](https://www.kaggle.com/datasets/yuliknormanowen/ucode-hubmap-efficientnet-768-train-local)     |    [url](https://www.kaggle.com/code/yuliknormanowen/hubmap-hap-efficientnet-unet-768-submission)    |\n| Swin_Transformer_v1_768 | [url](https://www.kaggle.com/code/yuliknormanowen/hubmap-hap-swintransformer-v1-unet-768-train) | [url](https://www.kaggle.com/datasets/yuliknormanowen/hubmaphap-swintransformer-v1-unet-256-train-local) | [url](https://www.kaggle.com/code/yuliknormanowen/hubmap-hap-swintransformer-v1-unet-768-submission) |\n| Swin_Transformer_v2_256 | [url](https://www.kaggle.com/code/yuliknormanowen/hubmap-hap-swintransformer-v2-unet-256-train) | [url](https://www.kaggle.com/datasets/yuliknormanowen/ucode-hubmap-swintransformer-768-train-local)      | [url](https://www.kaggle.com/code/yuliknormanowen/hubmap-hap-swintransformer-v2-unet-256-submission) |","metadata":{}},{"cell_type":"code","source":"import collections.abc\nimport glob\nimport math\nimport os\nfrom itertools import repeat\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport rasterio\nimport torch\nimport gc\nimport warnings\nimport torch.nn.functional as F\nfrom matplotlib import pyplot as plt\nfrom torch import nn\nfrom tqdm.notebook import tqdm\nfrom torch.utils.data import Dataset, DataLoader\nwarnings.filterwarnings(\"ignore\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bs = 1\nsz = 768  # the size of tiles\nreduce = 4 / 3  # reduce the original images by 4 times\nDATA = '../input/hubmap-organ-segmentation/test_images/'\nMODELS = glob.glob(\"../input/hubmaphap-swintransformer-v1-unet-256-weight/*.pth\")\ndf_sample = pd.read_csv('../input/hubmap-organ-segmentation/test.csv')\ndevice = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')\nEnsemble_Weight = False\nth = 0.55\ns_th = 40  # saturation blancking threshold\np_th = 1000 * (sz // 256) ** 2  # threshold for the minimum number of pixels\nidentity = rasterio.Affine(1, 0, 0, 0, 1, 0)\n\nif len(df_sample) == 1:\n    DEBUG = True\nelse:\n    DEBUG = False","metadata":{"execution":{"iopub.status.busy":"2022-08-09T02:14:12.295186Z","iopub.execute_input":"2022-08-09T02:14:12.295701Z","iopub.status.idle":"2022-08-09T02:14:12.386619Z","shell.execute_reply.started":"2022-08-09T02:14:12.295671Z","shell.execute_reply":"2022-08-09T02:14:12.385632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class config:\n    def __init__(self, fold=0):\n        self.root_dir = \".\"\n        self.pretrain_dir = '../input/swin-tiny-small-22k-pretrained/'\n        self.is_amp = True\n        self.TRAIN = '../input/hubmap-768x768/train/'\n        self.MASKS = '../input/hubmap-768x768/masks/'\n        self.LABELS = '../input/hubmap-organ-segmentation/train.csv'\n\n        self.fold = fold\n\n        self.out_dir = self.root_dir + f'/models/fold_{self.fold}'\n\n        self.initial_checkpoint = None\n\n        self.start_lr = 5e-5  # 0.0001\n        self.batch_size = 8  # 32 #32\n\n        self.checkpoint = dict(\n\n            # configs/_base_/models/upernet_swin.py\n            basic=dict(\n                swin=dict(\n                    embed_dim=96,\n                    depths=[2, 2, 6, 2],\n                    num_heads=[3, 6, 12, 24],\n                    window_size=7,\n                    mlp_ratio=4.,\n                    qkv_bias=True,\n                    qk_scale=None,\n                    drop_rate=0.,\n                    attn_drop_rate=0.,\n                    drop_path_rate=0.3,\n                    ape=False,\n                    patch_norm=True,\n                    out_indices=(0, 1, 2, 3),\n                    use_checkpoint=False\n                ),\n\n            ),\n\n            # configs/swin/upernet_swin_tiny_patch4_window7_512x512_160k_ade20k.py\n            swin_tiny_patch4_window7_224=dict(\n                checkpoint=self.pretrain_dir + '/swin_tiny_patch4_window7_224_22k.pth',\n\n                swin=dict(\n                    embed_dim=96,\n                    depths=[2, 2, 6, 2],\n                    num_heads=[3, 6, 12, 24],\n                    window_size=7,\n                    ape=False,\n                    drop_path_rate=0.3,\n                    patch_norm=True,\n                    use_checkpoint=False,\n                ),\n                upernet=dict(\n                    in_channels=[96, 192, 384, 768],\n                ),\n            ),\n\n            # /configs/swin/upernet_swin_small_patch4_window7_512x512_160k_ade20k.py\n            swin_small_patch4_window7_224_22k=dict(\n                checkpoint=self.pretrain_dir + '/swin_small_patch4_window7_224_22k.pth',\n\n                swin=dict(\n                    embed_dim=96,\n                    depths=[2, 2, 18, 2],\n                    num_heads=[3, 6, 12, 24],\n                    window_size=7,\n                    ape=False,\n                    drop_path_rate=0.3,\n                    patch_norm=True,\n                    use_checkpoint=False\n                ),\n                upernet=dict(\n                    in_channels=[96, 192, 384, 768],\n                ),\n            ),\n        )\n","metadata":{"execution":{"iopub.status.busy":"2022-08-09T02:14:12.390222Z","iopub.execute_input":"2022-08-09T02:14:12.390557Z","iopub.status.idle":"2022-08-09T02:14:12.401397Z","shell.execute_reply.started":"2022-08-09T02:14:12.390527Z","shell.execute_reply":"2022-08-09T02:14:12.400369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def img2tensor(img, dtype: np.dtype = np.float32):\n    if img.ndim == 2: img = np.expand_dims(img, 2)\n    img = np.transpose(img, (2, 0, 1))\n    return torch.from_numpy(img.astype(dtype, copy=False))\n\n\ndef _ntuple(n):\n    def parse(x):\n        if isinstance(x, collections.abc.Iterable):\n            return x\n        return tuple(repeat(x, n))\n\n    return parse\n\n\nto_2tuple = _ntuple(2)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T02:14:12.403970Z","iopub.execute_input":"2022-08-09T02:14:12.404774Z","iopub.status.idle":"2022-08-09T02:14:12.415364Z","shell.execute_reply.started":"2022-08-09T02:14:12.404738Z","shell.execute_reply":"2022-08-09T02:14:12.414539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class TestDataset(Dataset):\n    def __init__(self, idx, sz=sz, reduce=reduce):\n        self.data = rasterio.open(os.path.join(DATA, idx + '.tiff'), transform=identity,\n                                  num_threads='all_cpus')\n        # some images have issues with their format\n        # and must be saved correctly before reading with rasterio\n        if self.data.count != 3:\n            subdatasets = self.data.subdatasets\n            self.layers = []\n            if len(subdatasets) > 0:\n                for i, subdataset in enumerate(subdatasets, 0):\n                    self.layers.append(rasterio.open(subdataset))\n        self.shape = self.data.shape\n        self.reduce = reduce\n        self.sz = int(reduce * sz)\n        self.pad0 = (self.sz - self.shape[0] % self.sz) % self.sz\n        self.pad1 = (self.sz - self.shape[1] % self.sz) % self.sz\n        self.n0max = 1\n        self.n1max = 1\n\n    def __len__(self):\n        return self.n0max * self.n1max\n\n    def __getitem__(self, idx):\n        img = self.data.read()\n\n        img = np.transpose(img, (1, 2, 0))\n\n        if self.reduce != 1:\n            img = cv2.resize(img, (int(self.sz / reduce), int(self.sz / reduce)),\n                             interpolation=cv2.INTER_AREA)\n\n        # check for empty imges\n        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)\n        h, s, v = cv2.split(hsv)\n\n        if (s > s_th).sum() <= p_th or img.sum() <= p_th:\n            # images with -1 will be skipped\n            return img2tensor(img / 255.0), -1\n        else:\n            return img2tensor(img / 255.0), idx","metadata":{"execution":{"iopub.status.busy":"2022-08-09T02:14:12.417729Z","iopub.execute_input":"2022-08-09T02:14:12.418411Z","iopub.status.idle":"2022-08-09T02:14:12.431473Z","shell.execute_reply.started":"2022-08-09T02:14:12.418360Z","shell.execute_reply":"2022-08-09T02:14:12.430540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class LayerNorm2d(nn.Module):\n    def __init__(self, dim, eps=1e-6):\n        super().__init__()\n        self.weight = nn.Parameter(torch.ones(dim))\n        self.bias = nn.Parameter(torch.zeros(dim))\n        self.eps = eps\n\n    def forward(self, x):\n        u = x.mean(1, keepdim=True)\n        s = (x - u).pow(2).mean(1, keepdim=True)\n        x = (x - u) / torch.sqrt(s + self.eps)\n        x = self.weight[:, None, None] * x + self.bias[:, None, None]\n        return x\n\n\ndef criterion_aux_loss(logit, mask):\n    mask = F.interpolate(mask, size=logit.shape[-2:], mode='nearest')\n    loss = F.binary_cross_entropy_with_logits(logit, mask)\n    return loss\n\n\nclass RGB(nn.Module):\n    IMAGE_RGB_MEAN = [0.485, 0.456, 0.406]  # [0.5, 0.5, 0.5]\n    IMAGE_RGB_STD = [0.229, 0.224, 0.225]  # [0.5, 0.5, 0.5]\n\n    def __init__(self, ):\n        super(RGB, self).__init__()\n        self.register_buffer('mean', torch.zeros(1, 3, 1, 1))\n        self.register_buffer('std', torch.ones(1, 3, 1, 1))\n        self.mean.data = torch.FloatTensor(self.IMAGE_RGB_MEAN).view(self.mean.shape)\n        self.std.data = torch.FloatTensor(self.IMAGE_RGB_STD).view(self.std.shape)\n\n    def forward(self, x):\n        x = (x - self.mean) / self.std\n        return x\n\n\nclass PatchEmbed(nn.Module):\n    r\"\"\" Image to Patch Embedding\n\n    Args:\n        patch_size (int): Patch token size. Default: 4.\n        in_chans (int): Number of input image channels. Default: 3.\n        embed_dim (int): Number of linear projection output channels. Default: 96.\n        norm_layer (nn.Module, optional): Normalization layer. Default: None\n    \"\"\"\n\n    def __init__(self,\n                 patch_size=4,\n                 in_chans=3,\n                 embed_dim=96,\n                 norm_layer=None\n                 ):\n        super().__init__()\n        patch_size = to_2tuple(patch_size)\n        self.patch_size = patch_size\n\n        self.in_chans = in_chans\n        self.embed_dim = embed_dim\n\n        self.proj = nn.Conv2d(in_chans, embed_dim, kernel_size=patch_size, stride=patch_size)\n        if norm_layer is not None:\n            self.norm = norm_layer(embed_dim)\n        else:\n            self.norm = None\n\n    def forward(self, x):\n        B, C, H, W = x.shape\n\n        # padding\n        if W % self.patch_size[1] != 0:\n            x = F.pad(x, (0, self.patch_size[1] - W % self.patch_size[1]))\n        if H % self.patch_size[0] != 0:\n            x = F.pad(x, (0, 0, 0, self.patch_size[0] - H % self.patch_size[0]))\n\n        x = self.proj(x)  # B C Wh Ww\n        if self.norm is not None:\n            Wh, Ww = x.size(2), x.size(3)\n            x = x.flatten(2).transpose(1, 2)\n            x = self.norm(x)\n            x = x.transpose(1, 2).view(-1, self.embed_dim, Wh, Ww)\n\n        return x\n\n\ndef _no_grad_trunc_normal_(tensor, mean, std, a, b):\n    # Cut & paste from PyTorch official master until it's in a few official releases - RW\n    # Method based on https://people.sc.fsu.edu/~jburkardt/presentations/truncated_normal.pdf\n    def norm_cdf(x):\n        # Computes standard normal cumulative distribution function\n        return (1. + math.erf(x / math.sqrt(2.))) / 2.\n\n    if (mean < a - 2 * std) or (mean > b + 2 * std):\n        warnings.warn(\"mean is more than 2 std from [a, b] in nn.init.trunc_normal_. \"\n                      \"The distribution of values may be incorrect.\",\n                      stacklevel=2)\n\n\ndef trunc_normal_(tensor, mean=0., std=1., a=-2., b=2.):\n    return _no_grad_trunc_normal_(tensor, mean, std, a, b)\n\n\ndef drop_path(x, drop_prob: float = 0., training: bool = False, scale_by_keep: bool = True):\n    \"\"\"Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).\n    This is the same as the DropConnect impl I created for EfficientNet, etc networks, however,\n    the original name is misleading as 'Drop Connect' is a different form of dropout in a separate paper...\n    See discussion: https://github.com/tensorflow/tpu/issues/494#issuecomment-532968956 ... I've opted for\n    changing the layer and argument names to 'drop path' rather than mix DropConnect as a layer name and use\n    'survival rate' as the argument.\n    \"\"\"\n    if drop_prob == 0. or not training:\n        return x\n    keep_prob = 1 - drop_prob\n    shape = (x.shape[0],) + (1,) * (x.ndim - 1)  # work with diff dim tensors, not just 2D ConvNets\n    random_tensor = x.new_empty(shape).bernoulli_(keep_prob)\n    if keep_prob > 0.0 and scale_by_keep:\n        random_tensor.div_(keep_prob)\n    return x * random_tensor\n\n\nclass WindowAttention(nn.Module):\n    r\"\"\" Window based multi-head self attention (W-MSA) module with relative position bias.\n    It supports both of shifted and non-shifted window.\n\n    Args:\n        dim (int): Number of input channels.\n        window_size (tuple[int]): The height and width of the window.\n        num_heads (int): Number of attention heads.\n        qkv_bias (bool, optional):  If True, add a learnable bias to query, key, value. Default: True\n        qk_scale (float | None, optional): Override default qk scale of head_dim ** -0.5 if set\n        attn_drop (float, optional): Dropout ratio of attention weight. Default: 0.0\n        proj_drop (float, optional): Dropout ratio of output. Default: 0.0\n    \"\"\"\n\n    def __init__(self, dim, window_size, num_heads, qkv_bias=True, qk_scale=None, attn_drop=0., proj_drop=0.):\n        super().__init__()\n        self.dim = dim\n        self.window_size = window_size  # Wh, Ww\n        self.num_heads = num_heads\n        head_dim = dim // num_heads\n        self.scale = qk_scale or head_dim ** (-0.5)\n\n        # define a parameter table of relative position bias\n        self.relative_position_bias_table = nn.Parameter(\n            torch.zeros((2 * window_size[0] - 1) * (2 * window_size[1] - 1), num_heads))  # 2*Wh-1 * 2*Ww-1, nH\n\n        # get pair-wise relative position index for each token inside the window\n        coords_h = torch.arange(self.window_size[0])\n        coords_w = torch.arange(self.window_size[1])\n        coords = torch.stack(torch.meshgrid([coords_h, coords_w]))  # 2, Wh, Ww\n        coords_flatten = torch.flatten(coords, 1)  # 2, Wh*Ww\n        relative_coords = coords_flatten[:, :, None] - coords_flatten[:, None, :]  # 2, Wh*Ww, Wh*Ww\n        relative_coords = relative_coords.permute(1, 2, 0).contiguous()  # Wh*Ww, Wh*Ww, 2\n        relative_coords[:, :, 0] += self.window_size[0] - 1  # shift to start from 0\n        relative_coords[:, :, 1] += self.window_size[1] - 1\n        relative_coords[:, :, 0] *= 2 * self.window_size[1] - 1\n        relative_position_index = relative_coords.sum(-1)  # Wh*Ww, Wh*Ww\n        self.register_buffer(\"relative_position_index\", relative_position_index)\n\n        self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)\n        self.proj = nn.Linear(dim, dim)\n        self.softmax = nn.Softmax(dim=-1)\n\n        self.attn_drop = nn.Dropout(attn_drop)\n        self.proj_drop = nn.Dropout(proj_drop)\n\n        trunc_normal_(self.relative_position_bias_table, std=.02)\n\n    def forward(self, x, mask=None):\n        \"\"\"\n        Args:\n            x: input features with shape of (num_windows*B, N, C)\n            mask: (0/-inf) mask with shape of (num_windows, Wh*Ww, Wh*Ww) or None\n        \"\"\"\n\n        B_, N, C = x.shape\n        qkv = self.qkv(x).reshape(B_, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4)\n        q, k, v = qkv[0], qkv[1], qkv[2]  # make torchscript happy (cannot use tensor as tuple)\n\n        q = q * self.scale\n        attn = (q @ k.transpose(-2, -1))\n\n        relative_position_bias = \\\n            self.relative_position_bias_table[self.relative_position_index.view(-1)].view(\n                self.window_size[0] * self.window_size[1], self.window_size[0] * self.window_size[1], self.num_heads)\n        # Wh*Ww,Wh*Ww,nH\n        relative_position_bias = relative_position_bias.permute(2, 0, 1).contiguous()  # nH, Wh*Ww, Wh*Ww\n\n        attn = attn + relative_position_bias.unsqueeze(0)\n\n        if mask is not None:\n            nW = mask.shape[0]\n            attn = attn.view(B_ // nW, nW, self.num_heads, N, N) + mask.unsqueeze(1).unsqueeze(0)\n            attn = attn.view(-1, self.num_heads, N, N)\n            attn = self.softmax(attn)\n        else:\n            attn = self.softmax(attn)\n\n        attn = self.attn_drop(attn)\n        x = (attn @ v).transpose(1, 2).reshape(B_, N, C)\n        x = self.proj(x)\n        x = self.proj_drop(x)\n        return x\n\n    def extra_repr(self) -> str:\n        return f'dim={self.dim}, window_size={self.window_size}, num_heads={self.num_heads}'\n\n\nclass DropPath(nn.Module):\n    \"\"\"Drop paths (Stochastic Depth) per sample  (when applied in main path of residual blocks).\n    \"\"\"\n\n    def __init__(self, drop_prob: float = 0., scale_by_keep: bool = True):\n        super(DropPath, self).__init__()\n        self.drop_prob = drop_prob\n        self.scale_by_keep = scale_by_keep\n\n    def forward(self, x):\n        return drop_path(x, self.drop_prob, self.training, self.scale_by_keep)\n\n    def extra_repr(self):\n        return f'drop_prob={round(self.drop_prob, 3):0.3f}'\n\n\nclass Mlp(nn.Module):\n    \"\"\" Multilayer perceptron.\"\"\"\n\n    def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, drop=0.):\n        super().__init__()\n        out_features = out_features or in_features\n        hidden_features = hidden_features or in_features\n        self.fc1 = nn.Linear(in_features, hidden_features)\n        self.act = act_layer()\n        self.fc2 = nn.Linear(hidden_features, out_features)\n        self.drop = nn.Dropout(drop)\n\n    def forward(self, x):\n        x = self.fc1(x)\n        x = self.act(x)\n        x = self.drop(x)\n        x = self.fc2(x)\n        x = self.drop(x)\n        return x\n\n\ndef window_partition(x, window_size):\n    \"\"\"\n    Args:\n        x: (B, H, W, C)\n        window_size (int): window size\n\n    Returns:\n        windows: (num_windows*B, window_size, window_size, C)\n    \"\"\"\n    B, H, W, C = x.shape\n    x = x.view(B, H // window_size, window_size, W // window_size, window_size, C)\n    windows = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(-1, window_size, window_size, C)\n    return windows\n\ndef window_reverse(windows, window_size, H, W):\n    \"\"\"\n    Args:\n        windows: (num_windows*B, window_size, window_size, C)\n        window_size (int): Window size\n        H (int): Height of image\n        W (int): Width of image\n\n    Returns:\n        x: (B, H, W, C)\n    \"\"\"\n    B = int(windows.shape[0] / (H * W / window_size / window_size))\n    x = windows.view(B, H // window_size, W // window_size, window_size, window_size, -1)\n    x = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(B, H, W, -1)\n    return x\n\nclass SwinTransformerBlock(nn.Module):\n    r\"\"\" Swin Transformer Block.\n\n    Args:\n        dim (int): Number of input channels.\n        input_resolution (tuple[int]): Input resulotion.\n        num_heads (int): Number of attention heads.\n        window_size (int): Window size.\n        shift_size (int): Shift size for SW-MSA.\n        mlp_ratio (float): Ratio of mlp hidden dim to embedding dim.\n        qkv_bias (bool, optional): If True, add a learnable bias to query, key, value. Default: True\n        qk_scale (float | None, optional): Override default qk scale of head_dim ** -0.5 if set.\n        drop (float, optional): Dropout rate. Default: 0.0\n        attn_drop (float, optional): Attention dropout rate. Default: 0.0\n        drop_path (float, optional): Stochastic depth rate. Default: 0.0\n        act_layer (nn.Module, optional): Activation layer. Default: nn.GELU\n        norm_layer (nn.Module, optional): Normalization layer.  Default: nn.LayerNorm\n        fused_window_process (bool, optional): If True, use one kernel to fused window shift & window partition for acceleration, similar for the reversed part. Default: False\n    \"\"\"\n\n    def __init__(self,\n                 dim,\n                 num_heads,\n                 window_size=7,\n                 shift_size=0,\n                 mlp_ratio=4.,\n                 qkv_bias=True,\n                 qk_scale=None,\n                 drop=0.,\n                 attn_drop=0.,\n                 drop_path=0.,\n                 act_layer=nn.GELU,\n                 norm_layer=nn.LayerNorm,\n                 ):\n        super().__init__()\n        self.dim = dim\n        self.num_heads = num_heads\n        self.window_size = window_size\n        self.shift_size = shift_size\n        self.mlp_ratio = mlp_ratio\n        assert 0 <= self.shift_size < self.window_size, \"shift_size must in 0-window_size\"\n\n        self.norm1 = norm_layer(dim)\n        self.attn = WindowAttention(\n            dim,\n            window_size=to_2tuple(self.window_size),\n            num_heads=num_heads,\n            qkv_bias=qkv_bias,\n            qk_scale=qk_scale,\n            attn_drop=attn_drop,\n            proj_drop=drop,\n        )\n        self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity()\n\n        self.norm2 = norm_layer(dim)\n        mlp_hidden_dim = int(dim * mlp_ratio)\n        self.mlp = Mlp(in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop)\n\n    def forward(self, x, H, W, mask_matrix):\n\n        B, L, C = x.shape\n        assert L == H * W, \"input feature has wrong size\"\n\n        shortcut = x\n        x = self.norm1(x)\n        x = x.view(B, H, W, C)\n\n        # pad feature maps to multiples of window size\n        pad_l = pad_t = 0\n        pad_r = (self.window_size - W % self.window_size) % self.window_size\n        pad_b = (self.window_size - H % self.window_size) % self.window_size\n        x = F.pad(x, (0, 0, pad_l, pad_r, pad_t, pad_b))\n        _, Hp, Wp, _ = x.shape\n\n        # cyclic shift ---\n        if self.shift_size > 0:\n            shifted_x = torch.roll(x, shifts=(-self.shift_size, -self.shift_size), dims=(1, 2))\n            attn_mask = mask_matrix\n        else:\n            shifted_x = x\n            attn_mask = None\n\n        x_windows = window_partition(shifted_x, self.window_size)  # nW*B, window_size, window_size, C\n        x_windows = x_windows.view(-1, self.window_size * self.window_size, C)  # nW*B, window_size*window_size, C\n        attn_windows = self.attn(x_windows, mask=attn_mask)  # nW*B, window_size*window_size, C\n        attn_windows = attn_windows.view(-1, self.window_size, self.window_size, C)\n\n        # reverse cyclic shift ---\n        shifted_x = window_reverse(attn_windows, self.window_size, Hp, Wp)  # B H' W' C\n        if self.shift_size > 0:\n            x = torch.roll(shifted_x, shifts=(self.shift_size, self.shift_size), dims=(1, 2))\n        else:\n            x = shifted_x\n\n        if pad_r > 0 or pad_b > 0:\n            x = x[:, :H, :W, :].contiguous()\n        x = x.view(B, H * W, C)\n\n        # FFN\n        x = shortcut + self.drop_path(x)\n        x = x + self.drop_path(self.mlp(self.norm2(x)))\n        return x\n\n    def extra_repr(self) -> str:\n        return f\"dim={self.dim}, num_heads={self.num_heads}, \" \\\n               f\"window_size={self.window_size}, shift_size={self.shift_size}, mlp_ratio={self.mlp_ratio}\"\n\n\nclass BasicLayer(nn.Module):\n    \"\"\" A basic Swin Transformer layer for one stage.\n\n    Args:\n        dim (int): Number of input channels.\n        depth (int): Number of blocks.\n        num_heads (int): Number of attention heads.\n        window_size (int): Local window size.\n        mlp_ratio (float): Ratio of mlp hidden dim to embedding dim.\n        qkv_bias (bool, optional): If True, add a learnable bias to query, key, value. Default: True\n        qk_scale (float | None, optional): Override default qk scale of head_dim ** -0.5 if set.\n        drop (float, optional): Dropout rate. Default: 0.0\n        attn_drop (float, optional): Attention dropout rate. Default: 0.0\n        drop_path (float | tuple[float], optional): Stochastic depth rate. Default: 0.0\n        norm_layer (nn.Module, optional): Normalization layer. Default: nn.LayerNorm\n        downsample (nn.Module | None, optional): Downsample layer at the end of the layer. Default: None\n        use_checkpoint (bool): Whether to use checkpointing to save memory. Default: False.\n        fused_window_process (bool, optional): If True, use one kernel to fused window shift & window partition for acceleration, similar for the reversed part. Default: False\n    \"\"\"\n\n    def __init__(self,\n                 dim,\n                 depth,\n                 num_heads,\n                 window_size,\n                 mlp_ratio=4.,\n                 qkv_bias=True,\n                 qk_scale=None,\n                 drop=0.,\n                 attn_drop=0.,\n                 drop_path=0.,\n                 norm_layer=nn.LayerNorm,\n                 downsample=None,\n                 # use_checkpoint=False,\n                 ):\n        super().__init__()\n        self.window_size = window_size\n        self.shift_size = window_size // 2\n        self.depth = depth\n\n        self.blocks = nn.ModuleList([\n            SwinTransformerBlock(\n                dim=dim,\n                num_heads=num_heads,\n                window_size=window_size,\n                shift_size=0 if (i % 2 == 0) else window_size // 2,\n                mlp_ratio=mlp_ratio,\n                qkv_bias=qkv_bias,\n                qk_scale=qk_scale,\n                drop=drop,\n                attn_drop=attn_drop,\n                drop_path=drop_path[i] if isinstance(drop_path, list) else drop_path,\n                norm_layer=norm_layer,\n            )\n            for i in range(depth)\n        ])\n        # patch merging layer\n        if downsample is not None:\n            self.downsample = downsample(dim=dim, norm_layer=norm_layer)\n        else:\n            self.downsample = None\n\n    def forward(self, x, H, W):\n        \"\"\"\n        Args:\n            x: Input feature, tensor size (B, H*W, C).\n            H, W: Spatial resolution of the input feature.\n        \"\"\"\n\n        # calculate attention mask for SW-MSA ----\n        Hp = int(np.ceil(H / self.window_size)) * self.window_size\n        Wp = int(np.ceil(W / self.window_size)) * self.window_size\n        img_mask = torch.zeros((1, Hp, Wp, 1), device=x.device)  # 1 Hp Wp 1\n        h_slices = (slice(0, -self.window_size),\n                    slice(-self.window_size, -self.shift_size),\n                    slice(-self.shift_size, None))\n        w_slices = (slice(0, -self.window_size),\n                    slice(-self.window_size, -self.shift_size),\n                    slice(-self.shift_size, None))\n        cnt = 0\n        for h in h_slices:\n            for w in w_slices:\n                img_mask[:, h, w, :] = cnt\n                cnt += 1\n\n        mask_windows = window_partition(img_mask, self.window_size)  # nW, window_size, window_size, 1\n        mask_windows = mask_windows.view(-1, self.window_size * self.window_size)\n        attn_mask = mask_windows.unsqueeze(1) - mask_windows.unsqueeze(2)\n        attn_mask = attn_mask.masked_fill(attn_mask != 0, float(-100.0)).masked_fill(attn_mask == 0, float(0.0))\n        # ------\n\n        for blk in self.blocks:\n            x = blk(x, H, W, attn_mask)\n\n        if self.downsample is not None:\n            x_down = self.downsample(x, H, W)\n            Wh, Ww = (H + 1) // 2, (W + 1) // 2\n            return x, H, W, x_down, Wh, Ww\n        else:\n            return x, H, W, x, H, W\n\n\nclass PatchMerging(nn.Module):\n    r\"\"\" Patch Merging Layer.\n\n    Args:\n        dim (int): Number of input channels.\n        norm_layer (nn.Module, optional): Normalization layer.  Default: nn.LayerNorm\n    \"\"\"\n\n    def __init__(self, dim, norm_layer=nn.LayerNorm):\n        super().__init__()\n        self.dim = dim\n        self.reduction = nn.Linear(4 * dim, 2 * dim, bias=False)\n        self.norm = norm_layer(4 * dim)\n\n    def forward(self, x, H, W):\n        \"\"\"\n        Args:\n            x: Input feature, tensor size (B, H*W, C).\n            H, W: Spatial resolution of the input feature.\n        \"\"\"\n\n        B, L, C = x.shape\n        assert L == H * W, \"input feature has wrong size\"\n\n        x = x.view(B, H, W, C)\n        # padding\n        pad_input = (H % 2 == 1) or (W % 2 == 1)\n        if pad_input:\n            x = F.pad(x, (0, 0, 0, W % 2, 0, H % 2))\n\n        x0 = x[:, 0::2, 0::2, :]  # B H/2 W/2 C\n        x1 = x[:, 1::2, 0::2, :]  # B H/2 W/2 C\n        x2 = x[:, 0::2, 1::2, :]  # B H/2 W/2 C\n        x3 = x[:, 1::2, 1::2, :]  # B H/2 W/2 C\n        x = torch.cat([x0, x1, x2, x3], -1)  # B, H/2, W/2, 4*C\n        x = x.view(B, -1, 4 * C)  # B, H/2*W/2, 4*C\n\n        x = self.norm(x)\n        x = self.reduction(x)\n\n        return x\n\n\nclass SwinTransformerV1(nn.Module):\n    def __init__(self,\n                 pretrain_img_size=224,\n                 patch_size=4,\n                 in_chans=3,\n                 embed_dim=96,\n                 depths=[2, 2, 6, 2],\n                 num_heads=[3, 6, 12, 24],\n                 window_size=7,\n                 mlp_ratio=4.,\n                 qkv_bias=True,\n                 qk_scale=None,\n                 drop_rate=0.,\n                 attn_drop_rate=0.,\n                 drop_path_rate=0.1,\n                 norm_layer=nn.LayerNorm,\n                 patch_norm=True,\n                 out_norm=nn.Identity,  # use nn.Identity, nn.BatchNorm2d, LayerNorm2d\n                 **kwargs\n                 ):\n        super().__init__()\n        self.pretrain_img_size = pretrain_img_size\n        self.num_layers = len(depths)\n        self.embed_dim = embed_dim\n        self.mlp_ratio = mlp_ratio\n\n        self.patch_embed = PatchEmbed(\n            patch_size=patch_size,\n            in_chans=in_chans,\n            embed_dim=embed_dim,\n            norm_layer=norm_layer if patch_norm else None\n        )\n        self.pos_drop = nn.Dropout(p=drop_rate)\n\n        # stochastic depth\n        dpr = np.linspace(0, drop_path_rate, sum(depths)).tolist()  # stochastic depth decay rule\n\n        # build layers\n        self.layers = nn.ModuleList()\n        for i in range(self.num_layers):\n            layer = BasicLayer(\n                dim=int(embed_dim * 2 ** i),\n                depth=depths[i],\n                num_heads=num_heads[i],\n                window_size=window_size,\n                mlp_ratio=self.mlp_ratio,\n                qkv_bias=qkv_bias, qk_scale=qk_scale,\n                drop=drop_rate,\n                attn_drop=attn_drop_rate,\n                drop_path=dpr[sum(depths[:i]):sum(depths[:i + 1])],\n                norm_layer=norm_layer,\n                downsample=PatchMerging if (i < self.num_layers - 1) else None,\n            )\n            self.layers.append(layer)\n\n        # ---\n        # add a norm layer for each output\n        self.out_norm = nn.ModuleList(\n            [out_norm(int(embed_dim * 2 ** i)) for i in range(self.num_layers)]\n        )\n\n        # ---\n        self.apply(self._init_weights)\n\n    def _init_weights(self, m):\n        if isinstance(m, nn.Linear):\n            trunc_normal_(m.weight, std=.02)\n            if isinstance(m, nn.Linear) and m.bias is not None:\n                nn.init.constant_(m.bias, 0)\n        elif isinstance(m, nn.LayerNorm):\n            nn.init.constant_(m.bias, 0)\n            nn.init.constant_(m.weight, 1.0)\n\n    def forward(self, x):\n        x = self.patch_embed(x)\n        Wh, Ww = x.size(2), x.size(3)\n\n        # positional encode?\n        x = x.flatten(2).transpose(1, 2)\n        x = self.pos_drop(x)\n\n        outs = []\n        for i in range(self.num_layers):\n            x_out, H, W, x, Wh, Ww = self.layers[i](x, Wh, Ww)\n            out = x_out.view(-1, H, W, int(self.embed_dim * 2 ** i)).permute(0, 3, 1, 2).contiguous()\n            out = self.out_norm[i](out)\n            outs.append(out)\n\n        return outs\n\n\ndef conv3x3_bn_relu(in_planes, out_planes, stride=1):\n    \"3x3 convolution + BN + relu\"\n    return nn.Sequential(\n        nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False),\n        nn.BatchNorm2d(out_planes),\n        nn.ReLU(inplace=True),\n    )\n\n\nclass UPerDecoder(nn.Module):\n    def __init__(self,\n                 in_dim=[256, 512, 1024, 2048],\n                 ppm_pool_scale=[1, 2, 3, 6],\n                 ppm_dim=512,\n                 fpn_out_dim=256\n                 ):\n        super(UPerDecoder, self).__init__()\n\n        # PPM ----\n        dim = in_dim[-1]\n        ppm_pooling = []\n        ppm_conv = []\n\n        for scale in ppm_pool_scale:\n            ppm_pooling.append(\n                nn.AdaptiveAvgPool2d(scale)\n            )\n            ppm_conv.append(\n                nn.Sequential(\n                    nn.Conv2d(dim, ppm_dim, kernel_size=1, bias=False),\n                    nn.BatchNorm2d(ppm_dim),\n                    nn.ReLU(inplace=True)\n                )\n            )\n        self.ppm_pooling = nn.ModuleList(ppm_pooling)\n        self.ppm_conv = nn.ModuleList(ppm_conv)\n        self.ppm_out = conv3x3_bn_relu(dim + len(ppm_pool_scale) * ppm_dim, fpn_out_dim, 1)\n\n        # FPN ----\n        fpn_in = []\n        for i in range(0, len(in_dim) - 1):  # skip the top layer\n            fpn_in.append(\n                nn.Sequential(\n                    nn.Conv2d(in_dim[i], fpn_out_dim, kernel_size=1, bias=False),\n                    nn.BatchNorm2d(fpn_out_dim),\n                    nn.ReLU(inplace=True)\n                )\n            )\n        self.fpn_in = nn.ModuleList(fpn_in)\n\n        fpn_out = []\n        for i in range(len(in_dim) - 1):  # skip the top layer\n            fpn_out.append(\n                conv3x3_bn_relu(fpn_out_dim, fpn_out_dim, 1),\n            )\n        self.fpn_out = nn.ModuleList(fpn_out)\n\n        self.fpn_fuse = nn.Sequential(\n            conv3x3_bn_relu(len(in_dim) * fpn_out_dim, fpn_out_dim, 1),\n        )\n\n    def forward(self, feature):\n        f = feature[-1]\n        pool_shape = f.shape[2:]\n\n        ppm_out = [f]\n        for pool, conv in zip(self.ppm_pooling, self.ppm_conv):\n            p = pool(f)\n            p = F.interpolate(p, size=pool_shape, mode='bilinear', align_corners=False)\n            p = conv(p)\n            ppm_out.append(p)\n        ppm_out = torch.cat(ppm_out, 1)\n        down = self.ppm_out(ppm_out)\n\n        fpn_out = [down]\n        for i in reversed(range(len(feature) - 1)):\n            lateral = feature[i]\n            lateral = self.fpn_in[i](lateral)  # lateral branch\n            down = F.interpolate(down, size=lateral.shape[2:], mode='bilinear', align_corners=False)  # top-down branch\n            down = down + lateral\n            fpn_out.append(self.fpn_out[i](down))\n\n        fpn_out.reverse()  # [P2 - P5]\n        fusion_shape = fpn_out[0].shape[2:]\n        fusion = [fpn_out[0]]\n        for i in range(1, len(fpn_out)):\n            fusion.append(\n                F.interpolate(fpn_out[i], fusion_shape, mode='bilinear', align_corners=False)\n            )\n        x = self.fpn_fuse(torch.cat(fusion, 1))\n\n        return x, fusion\n\n\nclass Net(nn.Module):\n\n    def load_pretrain(self):\n\n        checkpoint = self.config.checkpoint[self.arch]['checkpoint']\n        print('loading %s ...' % checkpoint)\n        checkpoint = torch.load(checkpoint, map_location=lambda storage, loc: storage)['model']\n        # if 0:\n        #     skip = ['relative_coords_table','relative_position_index']\n        #     filtered={}\n        #     for k,v in checkpoint.items():\n        #         if any([s in k for s in skip ]): continue\n        #         filtered[k]=v\n        #     checkpoint = filtered\n        print(self.encoder.load_state_dict(checkpoint, strict=False))  # True\n\n    def __init__(self, config):\n        super(Net, self).__init__()\n        self.config = config\n\n        self.rgb = RGB()\n        self.arch = 'swin_tiny_patch4_window7_224'\n\n        self.encoder = SwinTransformerV1(\n            **{**(config.checkpoint['basic']['swin']), **(config.checkpoint[self.arch]['swin']),\n               **{'out_norm': LayerNorm2d}}\n        )\n        encoder_dim = config.checkpoint[self.arch]['upernet']['in_channels']\n        # [96, 192, 384, 768]\n\n        self.decoder = UPerDecoder(\n            in_dim=encoder_dim,\n            ppm_pool_scale=[1, 2, 3, 6],\n            ppm_dim=512,\n            fpn_out_dim=256\n        )\n\n        self.logit = nn.Sequential(\n            nn.Conv2d(256, 1, kernel_size=1)\n        )\n        self.aux = nn.ModuleList([\n            nn.Conv2d(256, 1, kernel_size=1, padding=0) for i in range(4)\n        ])\n\n    def forward(self, batch):\n        x = batch\n        B, C, H, W = x.shape\n        x = self.rgb(x)\n        encoder = self.encoder(x)\n        last, decoder = self.decoder(encoder)\n        logit = self.logit(last)\n        logit = F.interpolate(logit, size=None, scale_factor=4, mode='bilinear', align_corners=False)\n\n        output = torch.sigmoid(logit)\n\n        return output","metadata":{"execution":{"iopub.status.busy":"2022-08-09T02:14:12.433748Z","iopub.execute_input":"2022-08-09T02:14:12.434041Z","iopub.status.idle":"2022-08-09T02:14:12.539072Z","shell.execute_reply.started":"2022-08-09T02:14:12.434017Z","shell.execute_reply":"2022-08-09T02:14:12.537973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Model_pred:\n    def __init__(self, models, dl, all_score, tta: bool = True, half: bool = False):\n        self.models = models\n        self.dl = dl\n        self.tta = tta\n        self.half = half\n        self.all_score = all_score\n\n    def __iter__(self):\n        count = 0\n        with torch.no_grad():\n            for x, y in iter(self.dl):\n                \n                if DEBUG:\n                    print(\"\\n Original:\")\n                    show_image = x.squeeze(0)\n                    show_image = show_image.permute(1,2,0)\n                    show_image = show_image.cpu().detach().numpy()\n                    plt.imshow(show_image)\n                    plt.show()\n\n                if (y >= 0).sum() > 0:  # exclude empty images\n                    x = x[y >= 0].to(device)\n                    y = y[y >= 0]\n                    if self.half: x = x.half()\n                    py = None\n                    for model, score in self.models:\n                        p = model(x)\n                        p *= score / self.all_score\n                        if py is None:\n                            py = p\n                        else:\n                            py += p\n                    if self.tta:\n                        # x,y,xy flips as TTA\n                        flips = [[-1], [-2], [-2, -1]]\n                        for f in flips:\n                            xf = torch.flip(x, f)\n                            for model, score in self.models:\n                                p = model(xf)\n                                p = torch.flip(p, f)\n                                py += p * score / self.all_score\n                        py /= (1 + len(flips))\n\n                    py = F.upsample(py, scale_factor=reduce, mode=\"bilinear\")\n                    py = py.permute(0, 2, 3, 1).float().cpu()\n\n                    batch_size = len(py)\n                    for i in range(batch_size):\n                        yield py[i], y[i]\n                        count += 1\n\n    def __len__(self):\n        return len(self.dl.dataset)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-09T02:14:12.540752Z","iopub.execute_input":"2022-08-09T02:14:12.541384Z","iopub.status.idle":"2022-08-09T02:14:12.555460Z","shell.execute_reply.started":"2022-08-09T02:14:12.541346Z","shell.execute_reply":"2022-08-09T02:14:12.554611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def extract_model_score(path):\n    file_name = path[path.rindex(\"/\") + 1:]\n    score = float(file_name[file_name.rindex(\"_\") + 1:file_name.rindex(\".\")])\n    return score","metadata":{"execution":{"iopub.status.busy":"2022-08-09T02:14:12.557807Z","iopub.execute_input":"2022-08-09T02:14:12.558719Z","iopub.status.idle":"2022-08-09T02:14:12.572180Z","shell.execute_reply.started":"2022-08-09T02:14:12.558681Z","shell.execute_reply":"2022-08-09T02:14:12.571192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"models = []\nths = []\nall_score = 0\nfor path in MODELS:\n    state_dict = torch.load(path,map_location=torch.device(device))\n    if Ensemble_Weight:\n        score = extract_model_score(path)\n    else:\n        score = 1\n    all_score += score\n    model = Net(config=config()).to(device)\n    model.load_state_dict(state_dict[\"state_dict\"])\n    model.float()\n    model.eval()\n    model.to(device)\n    models.append((model, score))\ndel state_dict","metadata":{"execution":{"iopub.status.busy":"2022-08-09T02:14:12.573446Z","iopub.execute_input":"2022-08-09T02:14:12.574321Z","iopub.status.idle":"2022-08-09T02:14:25.092846Z","shell.execute_reply.started":"2022-08-09T02:14:12.574277Z","shell.execute_reply":"2022-08-09T02:14:25.091824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_encode_less_memory(img):\n    #the image should be transposed\n    pixels = img.T.flatten()\n    \n    # This simplified method requires first and last pixel to be zero\n    pixels[0] = 0\n    pixels[-1] = 0\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 2\n    runs[1::2] -= runs[::2]\n    \n    return ' '.join(str(x) for x in runs)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T02:14:25.097293Z","iopub.execute_input":"2022-08-09T02:14:25.097604Z","iopub.status.idle":"2022-08-09T02:14:25.104867Z","shell.execute_reply.started":"2022-08-09T02:14:25.097575Z","shell.execute_reply":"2022-08-09T02:14:25.102756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"names, preds = [], []\n\nfor _, row in tqdm(df_sample.iterrows(), total=len(df_sample)):\n    idx = str(row['id'])\n    ds = TestDataset(idx)\n    # rasterio cannot be used with multiple workers\n    dl = DataLoader(ds, bs, num_workers=0, shuffle=False, pin_memory=True)\n    mp = Model_pred(models, dl, all_score)\n\n    # generate masks\n    mask = torch.zeros(len(dl), ds.sz, ds.sz, dtype=torch.float)\n\n    for p, i in iter(mp):\n        mask[i.item()] = p.squeeze(-1) > th\n\n    # reshape tiled masks into a single mask and crop padding\n    mask = mask.unsqueeze(0)\n    mask = mask[0][0].numpy()\n    mask = cv2.resize(mask, (2023, 2023), interpolation=cv2.INTER_CUBIC)\n\n    mask = mask > th / reduce\n    \n    if DEBUG:\n        print(\"\\nPredict:\")\n        plt.imshow(mask,cmap = 'gray')\n    \n    rle = rle_encode_less_memory(mask)\n    names.append(idx)\n    preds.append(rle)\n    del mask, ds, dl\n    gc.collect()\n","metadata":{"execution":{"iopub.status.busy":"2022-08-09T02:14:25.106525Z","iopub.execute_input":"2022-08-09T02:14:25.107249Z","iopub.status.idle":"2022-08-09T02:14:34.036890Z","shell.execute_reply.started":"2022-08-09T02:14:25.107213Z","shell.execute_reply":"2022-08-09T02:14:34.035958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame({'id':names,'rle':preds})\ndf.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T02:14:34.038492Z","iopub.execute_input":"2022-08-09T02:14:34.038860Z","iopub.status.idle":"2022-08-09T02:14:34.047735Z","shell.execute_reply.started":"2022-08-09T02:14:34.038825Z","shell.execute_reply":"2022-08-09T02:14:34.046836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T02:14:34.049254Z","iopub.execute_input":"2022-08-09T02:14:34.049923Z","iopub.status.idle":"2022-08-09T02:14:34.062733Z","shell.execute_reply.started":"2022-08-09T02:14:34.049884Z","shell.execute_reply":"2022-08-09T02:14:34.061626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# HAP Check","metadata":{}},{"cell_type":"code","source":"import cv2\nidx = \"10044_0000\"\nimg = cv2.cvtColor(cv2.imread(f\"../input/hubmap-768x768/train/{idx}.png\"), cv2.COLOR_BGR2RGB)\nmask = cv2.imread(f\"../input/hubmap-768x768/masks/{idx}.png\", cv2.IMREAD_GRAYSCALE)\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T02:14:34.063951Z","iopub.execute_input":"2022-08-09T02:14:34.064816Z","iopub.status.idle":"2022-08-09T02:14:34.386610Z","shell.execute_reply.started":"2022-08-09T02:14:34.064753Z","shell.execute_reply":"2022-08-09T02:14:34.385721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(mask,cmap = 'gray')","metadata":{"execution":{"iopub.status.busy":"2022-08-09T02:14:34.388931Z","iopub.execute_input":"2022-08-09T02:14:34.389685Z","iopub.status.idle":"2022-08-09T02:14:34.620387Z","shell.execute_reply.started":"2022-08-09T02:14:34.389637Z","shell.execute_reply":"2022-08-09T02:14:34.619451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = np.transpose(img,(2,0,1)) / 255\nimg = torch.tensor(img).unsqueeze(0).to(dtype = torch.float)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T02:14:34.621973Z","iopub.execute_input":"2022-08-09T02:14:34.622578Z","iopub.status.idle":"2022-08-09T02:14:34.641753Z","shell.execute_reply.started":"2022-08-09T02:14:34.622537Z","shell.execute_reply":"2022-08-09T02:14:34.640835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m = models[0][0]\npred = m(img.to('cuda'))","metadata":{"execution":{"iopub.status.busy":"2022-08-09T02:14:34.643413Z","iopub.execute_input":"2022-08-09T02:14:34.643782Z","iopub.status.idle":"2022-08-09T02:14:34.684516Z","shell.execute_reply.started":"2022-08-09T02:14:34.643747Z","shell.execute_reply":"2022-08-09T02:14:34.683514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = pred > 0.5\npred = pred.squeeze().squeeze().detach().cpu()\npred = pred.numpy()\nplt.imshow(pred,cmap = 'gray')","metadata":{"execution":{"iopub.status.busy":"2022-08-09T02:14:34.686165Z","iopub.execute_input":"2022-08-09T02:14:34.686582Z","iopub.status.idle":"2022-08-09T02:14:34.935715Z","shell.execute_reply.started":"2022-08-09T02:14:34.686541Z","shell.execute_reply":"2022-08-09T02:14:34.934749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# HUBMAP Check¶","metadata":{}},{"cell_type":"code","source":"import cv2\nidx = \"0486052bb_1047_overlap\"\nimg = cv2.cvtColor(cv2.imread(f\"../input/hubmap-768-resize-768-overlap-images-dataset/train/{idx}.png\"), cv2.COLOR_BGR2RGB)\nmask = cv2.imread(f\"../input/hubmap-768-resize-768-overlap-images-dataset/masks/{idx}.png\", cv2.IMREAD_GRAYSCALE)\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T02:14:34.937123Z","iopub.execute_input":"2022-08-09T02:14:34.940058Z","iopub.status.idle":"2022-08-09T02:14:35.284552Z","shell.execute_reply.started":"2022-08-09T02:14:34.940017Z","shell.execute_reply":"2022-08-09T02:14:35.283689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(mask,cmap = 'gray')","metadata":{"execution":{"iopub.status.busy":"2022-08-09T02:14:35.285976Z","iopub.execute_input":"2022-08-09T02:14:35.286983Z","iopub.status.idle":"2022-08-09T02:14:35.524925Z","shell.execute_reply.started":"2022-08-09T02:14:35.286945Z","shell.execute_reply":"2022-08-09T02:14:35.523756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = img2tensor(img / 255.0).unsqueeze(0)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T02:14:35.526381Z","iopub.execute_input":"2022-08-09T02:14:35.526716Z","iopub.status.idle":"2022-08-09T02:14:35.537262Z","shell.execute_reply.started":"2022-08-09T02:14:35.526688Z","shell.execute_reply":"2022-08-09T02:14:35.535848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m = models[0][0]\npred = m(img.to('cuda'))\npred = pred > 0.5\npred = pred.squeeze().squeeze().detach().cpu()\npred = np.array(pred, dtype=np.uint8)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T02:14:35.539218Z","iopub.execute_input":"2022-08-09T02:14:35.541378Z","iopub.status.idle":"2022-08-09T02:14:35.605467Z","shell.execute_reply.started":"2022-08-09T02:14:35.541336Z","shell.execute_reply":"2022-08-09T02:14:35.604489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(pred,cmap = 'gray')","metadata":{"execution":{"iopub.status.busy":"2022-08-09T02:14:35.606997Z","iopub.execute_input":"2022-08-09T02:14:35.607408Z","iopub.status.idle":"2022-08-09T02:14:35.847840Z","shell.execute_reply.started":"2022-08-09T02:14:35.607369Z","shell.execute_reply":"2022-08-09T02:14:35.846739Z"},"trusted":true},"execution_count":null,"outputs":[]}]}