{"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":"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\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\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":"2022-09-02T18:36:45.038367Z","iopub.execute_input":"2022-09-02T18:36:45.047237Z","iopub.status.idle":"2022-09-02T18:36:45.173463Z","shell.execute_reply.started":"2022-09-02T18:36:45.047186Z","shell.execute_reply":"2022-09-02T18:36:45.171198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from ctypes import resize\nfrom torch.utils.data import DataLoader\nfrom torch.utils.data import Dataset\nimport torch\nimport torchvision\nfrom torchvision import transforms\nimport pathlib\nimport pandas as pd\nimport numpy as np\nimport random\nimport json\nimport cv2\nimport os\nimport math\nimport glob\nfrom tqdm import tqdm\nimport pandas as pd\nimport h5py\nfrom PIL import Image","metadata":{"execution":{"iopub.status.busy":"2022-09-02T18:36:45.179555Z","iopub.execute_input":"2022-09-02T18:36:45.180092Z","iopub.status.idle":"2022-09-02T18:36:46.030519Z","shell.execute_reply.started":"2022-09-02T18:36:45.180043Z","shell.execute_reply":"2022-09-02T18:36:46.029271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle2mask(rle, size):\n    rle = np.array(list(map(int, rle.split())))\n    label = np.zeros((size*size), dtype=np.uint8)\n    for start, end in zip(rle[::2], rle[1::2]):\n        label[start:start+end] = 1\n    return label.reshape(size, size).T","metadata":{"execution":{"iopub.status.busy":"2022-09-02T18:36:46.032300Z","iopub.execute_input":"2022-09-02T18:36:46.032703Z","iopub.status.idle":"2022-09-02T18:36:46.041299Z","shell.execute_reply.started":"2022-09-02T18:36:46.032662Z","shell.execute_reply":"2022-09-02T18:36:46.039683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DataImages1(Dataset):\n    \"\"\"\n        Dataset class for training the network\n    \"\"\"\n\n    def __init__(self, phase = \"Train\"):\n\n        data_path = \"/kaggle/input/hubmap-organ-segmentation/\"\n        all_df = pd.read_csv(data_path + 'train.csv')\n\n        id_l = all_df[\"id\"].values\n        rle_l = all_df[\"rle\"].values\n        organs_l = all_df[\"organ\"].values\n        image_height_l = all_df[\"img_height\"].values\n        image_width_l = all_df[\"img_width\"].values\n        pixel_size_l = all_df[\"pixel_size\"].values\n        tissue_thickness_l = all_df[\"tissue_thickness\"].values\n        age_l = all_df[\"age\"].values\n        sex_l = all_df[\"sex\"].values\n\n        self.id_l = id_l\n        self.rle_l = rle_l\n        self.data_path = data_path\n        self.image_width_l = image_width_l\n\n        self.transform = transforms.Compose([\n                                            transforms.ToTensor(),\n                                            transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])\n\n\n\n    def __len__(self):\n        return len(self.id_l)\n    \n    def __getitem__(self, idx):\n        \n        id = self.id_l[idx]\n        mask_rle = self.rle_l[idx]\n        img_w = self.image_width_l[idx]\n\n        image_path = self.data_path + \"train_images/\" + str(id) + \".tiff\"\n        img = cv2.imread(image_path)\n        mask = rle2mask(mask_rle, img_w)\n\n        img = cv2.resize(img, (512, 512))\n        mask = cv2.resize(mask, (512, 512))\n\n        img = self.transform(Image.fromarray(img))\n        # img = img/255.0\n\n        # img = torch.from_numpy(np.array(img))\n        mask = torch.from_numpy(np.array(mask))\n\n        # img = img.permute(2,0,1)\n\n        return img, mask\n\ndef create_dataloader():\n    \"\"\"Creating Data Loaders\n    Args:\n        config (dict): Contains config params\n    \"\"\"\n\n    train_set = DataImages1(phase = \"Train\")\n    train_loader = DataLoader(\n        train_set,\n        batch_size = 2,\n        shuffle = True,\n        num_workers = 2,\n        pin_memory = False,\n        drop_last = True,\n    )\n\n    test_set = DataImages1(phase=\"Test\")\n    test_loader = DataLoader(\n        test_set,\n        batch_size = 2,\n        shuffle = False,\n        num_workers = 2,\n        pin_memory = False,\n    )\n\n    return train_loader, test_loader","metadata":{"execution":{"iopub.status.busy":"2022-09-02T18:36:46.044296Z","iopub.execute_input":"2022-09-02T18:36:46.045598Z","iopub.status.idle":"2022-09-02T18:36:46.065964Z","shell.execute_reply.started":"2022-09-02T18:36:46.045535Z","shell.execute_reply":"2022-09-02T18:36:46.064125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom functools import partial\nfrom typing import Any, Callable, List, Optional, Sequence\n\nimport torch\nfrom torch import nn, Tensor\nfrom torch.nn import functional as F\n\ntr = torch","metadata":{"execution":{"iopub.status.busy":"2022-09-02T18:36:46.067666Z","iopub.execute_input":"2022-09-02T18:36:46.069746Z","iopub.status.idle":"2022-09-02T18:36:46.085225Z","shell.execute_reply.started":"2022-09-02T18:36:46.068101Z","shell.execute_reply":"2022-09-02T18:36:46.083840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import math\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\n\n\nclass Swish(nn.Module):\n    def __init__(self, name=None):\n        super().__init__()\n        self.name = name\n\n    def forward(self, x):\n        return x * torch.sigmoid(x)\n\n\nclass Conv2dSamePadding(nn.Conv2d):\n    \"\"\"2D Convolutions with same padding\n    \"\"\"\n\n    def __init__(self, in_channels, out_channels, kernel_size, stride=1, dilation=1, groups=1, bias=True, name=None):\n        super().__init__(in_channels, out_channels, kernel_size, stride, padding=0, dilation=dilation, groups=groups,\n                         bias=bias)\n        self.stride = self.stride if len(self.stride) == 2 else [self.stride[0]] * 2\n        self.name = name\n\n    def forward(self, x):\n        input_h, input_w = x.size()[2:]\n        kernel_h, kernel_w = self.weight.size()[2:]\n        stride_h, stride_w = self.stride\n        output_h, output_w = math.ceil(input_h / stride_h), math.ceil(input_w / stride_w)\n        pad_h = max((output_h - 1) * self.stride[0] + (kernel_h - 1) * self.dilation[0] + 1 - input_h, 0)\n        pad_w = max((output_w - 1) * self.stride[1] + (kernel_w - 1) * self.dilation[1] + 1 - input_w, 0)\n        if pad_h > 0 or pad_w > 0:\n            x = F.pad(x, [pad_w // 2, pad_w - pad_w // 2, pad_h // 2, pad_h - pad_h // 2])\n        return F.conv2d(x, self.weight, self.bias, self.stride, self.padding, self.dilation, self.groups)\n\n\nclass BatchNorm2d(nn.BatchNorm2d):\n    def __init__(self, num_features, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True, name=None):\n        super().__init__(num_features, eps=eps, momentum=momentum, affine=affine,\n                         track_running_stats=track_running_stats)\n        self.name = name\n\n\ndef drop_connect(inputs, drop_connect_rate, training):\n    if not training:\n        return inputs\n    batch_size = inputs.shape[0]\n    keep_prob = 1.0 - drop_connect_rate\n    random_tensor = keep_prob\n    random_tensor += torch.rand([batch_size, 1, 1, 1], dtype=inputs.dtype, device=inputs.device)\n    binary_tensor = torch.floor(random_tensor)\n    output = inputs / keep_prob * binary_tensor\n    return output\n\n\nclass MBConvBlock(nn.Module):\n    \"\"\"Mobile Inverted Residual Bottleneck Block\n    \"\"\"\n\n    def __init__(self, block_args, global_params, idx):\n        super().__init__()\n\n        block_name = 'blocks_' + str(idx) + '_'\n\n        self.block_args = block_args\n        self.batch_norm_momentum = 1 - global_params.batch_norm_momentum\n        self.batch_norm_epsilon = global_params.batch_norm_epsilon\n        self.has_se = (self.block_args.se_ratio is not None) and (0 < self.block_args.se_ratio <= 1)\n        self.id_skip = block_args.id_skip\n\n        self.swish = Swish(block_name + '_swish')\n\n        # Expansion phase\n        in_channels = self.block_args.input_filters\n        out_channels = self.block_args.input_filters * self.block_args.expand_ratio\n        if self.block_args.expand_ratio != 1:\n            self._expand_conv = Conv2dSamePadding(in_channels=in_channels,\n                                                  out_channels=out_channels,\n                                                  kernel_size=1,\n                                                  bias=False,\n                                                  name=block_name + 'expansion_conv')\n            self._bn0 = BatchNorm2d(num_features=out_channels,\n                                    momentum=self.batch_norm_momentum,\n                                    eps=self.batch_norm_epsilon,\n                                    name=block_name + 'expansion_batch_norm')\n\n        # Depth-wise convolution phase\n        kernel_size = self.block_args.kernel_size\n        strides = self.block_args.strides\n        self._depthwise_conv = Conv2dSamePadding(in_channels=out_channels,\n                                                 out_channels=out_channels,\n                                                 groups=out_channels,\n                                                 kernel_size=kernel_size,\n                                                 stride=strides,\n                                                 bias=False,\n                                                 name=block_name + 'depthwise_conv')\n        self._bn1 = BatchNorm2d(num_features=out_channels,\n                                momentum=self.batch_norm_momentum,\n                                eps=self.batch_norm_epsilon,\n                                name=block_name + 'depthwise_batch_norm')\n\n        # Squeeze and Excitation layer\n        if self.has_se:\n            num_squeezed_channels = max(1, int(self.block_args.input_filters * self.block_args.se_ratio))\n            self._se_reduce = Conv2dSamePadding(in_channels=out_channels,\n                                                out_channels=num_squeezed_channels,\n                                                kernel_size=1,\n                                                name=block_name + 'se_reduce')\n            self._se_expand = Conv2dSamePadding(in_channels=num_squeezed_channels,\n                                                out_channels=out_channels,\n                                                kernel_size=1,\n                                                name=block_name + 'se_expand')\n\n        # Output phase\n        final_output_channels = self.block_args.output_filters\n        self._project_conv = Conv2dSamePadding(in_channels=out_channels,\n                                               out_channels=final_output_channels,\n                                               kernel_size=1,\n                                               bias=False,\n                                               name=block_name + 'output_conv')\n        self._bn2 = BatchNorm2d(num_features=final_output_channels,\n                                momentum=self.batch_norm_momentum,\n                                eps=self.batch_norm_epsilon,\n                                name=block_name + 'output_batch_norm')\n\n    def forward(self, x, drop_connect_rate=None):\n        identity = x\n        # Expansion and depth-wise convolution\n        if self.block_args.expand_ratio != 1:\n            x = self._expand_conv(x)\n            x = self._bn0(x)\n            x = self.swish(x)\n\n        x = self._depthwise_conv(x)\n        x = self._bn1(x)\n        x = self.swish(x)\n\n        # Squeeze and Excitation\n        if self.has_se:\n            x_squeezed = F.adaptive_avg_pool2d(x, 1)\n            x_squeezed = self._se_expand(self.swish(self._se_reduce(x_squeezed)))\n            x = torch.sigmoid(x_squeezed) * x\n\n        x = self._bn2(self._project_conv(x))\n\n        # Skip connection and drop connect\n        input_filters, output_filters = self.block_args.input_filters, self.block_args.output_filters\n        if self.id_skip and self.block_args.strides == 1 and input_filters == output_filters:\n            if drop_connect_rate:\n                x = drop_connect(x, drop_connect_rate=drop_connect_rate, training=self.training)\n            x = x + identity\n        return x\n\n\ndef double_conv(in_channels, out_channels):\n    return nn.Sequential(\n        nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1),\n        nn.BatchNorm2d(out_channels),\n        nn.ReLU(inplace=True),\n        nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1),\n        nn.BatchNorm2d(out_channels),\n        nn.ReLU(inplace=True)\n    )\n\n\ndef up_conv(in_channels, out_channels):\n    return nn.ConvTranspose2d(\n        in_channels, out_channels, kernel_size=2, stride=2\n    )\n\n\ndef custom_head(in_channels, out_channels):\n    return nn.Sequential(\n        nn.Dropout(),\n        nn.Linear(in_channels, 512),\n        nn.ReLU(inplace=True),\n        nn.Dropout(),\n        nn.Linear(512, out_channels)\n    )","metadata":{"execution":{"iopub.status.busy":"2022-09-02T18:36:46.088366Z","iopub.execute_input":"2022-09-02T18:36:46.089945Z","iopub.status.idle":"2022-09-02T18:36:46.141103Z","shell.execute_reply.started":"2022-09-02T18:36:46.089806Z","shell.execute_reply":"2022-09-02T18:36:46.139481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from functools import partial\nfrom typing import Any, Callable, List, Optional, Sequence\nfrom typing import Callable, List, Optional, Sequence, Tuple, Union\nimport collections\nimport torch\nfrom torch import nn, Tensor\nfrom torch.nn import functional as F\nfrom torch.hub import load_state_dict_from_url\nfrom itertools import repeat\n\n\ndef _make_ntuple(x: Any, n: int) -> Tuple[Any, ...]:\n    \"\"\"\n    Make n-tuple from input x. If x is an iterable, then we just convert it to tuple.\n    Otherwise we will make a tuple of length n, all with value of x.\n    reference: https://github.com/pytorch/pytorch/blob/master/torch/nn/modules/utils.py#L8\n    Args:\n        x (Any): input value\n        n (int): length of the resulting tuple\n    \"\"\"\n    if isinstance(x, collections.abc.Iterable):\n        return tuple(x)\n    return tuple(repeat(x, n))\n\ndef stochastic_depth(input: Tensor, p: float, mode: str, training: bool = True) -> Tensor:\n    \"\"\"\n    Implements the Stochastic Depth from `\"Deep Networks with Stochastic Depth\"\n    <https://arxiv.org/abs/1603.09382>`_ used for randomly dropping residual\n    branches of residual architectures.\n    Args:\n        input (Tensor[N, ...]): The input tensor or arbitrary dimensions with the first one\n                    being its batch i.e. a batch with ``N`` rows.\n        p (float): probability of the input to be zeroed.\n        mode (str): ``\"batch\"`` or ``\"row\"``.\n                    ``\"batch\"`` randomly zeroes the entire input, ``\"row\"`` zeroes\n                    randomly selected rows from the batch.\n        training: apply stochastic depth if is ``True``. Default: ``True``\n    Returns:\n        Tensor[N, ...]: The randomly zeroed tensor.\n    \"\"\"\n    # if not torch.jit.is_scripting() and not torch.jit.is_tracing():\n    #     _log_api_usage_once(stochastic_depth)\n    if p < 0.0 or p > 1.0:\n        raise ValueError(f\"drop probability has to be between 0 and 1, but got {p}\")\n    if mode not in [\"batch\", \"row\"]:\n        raise ValueError(f\"mode has to be either 'batch' or 'row', but got {mode}\")\n    if not training or p == 0.0:\n        return input\n\n    survival_rate = 1.0 - p\n    if mode == \"row\":\n        size = [input.shape[0]] + [1] * (input.ndim - 1)\n    else:\n        size = [1] * input.ndim\n    noise = torch.empty(size, dtype=input.dtype, device=input.device)\n    noise = noise.bernoulli_(survival_rate)\n    if survival_rate > 0.0:\n        noise.div_(survival_rate)\n    return input * noise\n\nclass StochasticDepth(nn.Module):\n    \"\"\"\n    See :func:`stochastic_depth`.\n    \"\"\"\n\n    def __init__(self, p: float, mode: str) -> None:\n        super().__init__()\n        self.p = p\n        self.mode = mode\n\n    def forward(self, input: Tensor) -> Tensor:\n        return stochastic_depth(input, self.p, self.mode, self.training)\n\n\n    def __repr__(self) -> str:\n        s = f\"{self.__class__.__name__}(p={self.p}, mode={self.mode})\"\n        return s\n\nclass Permute(torch.nn.Module):\n    \"\"\"This module returns a view of the tensor input with its dimensions permuted.\n    Args:\n        dims (List[int]): The desired ordering of dimensions\n    \"\"\"\n\n    def __init__(self, dims: List[int]):\n        super().__init__()\n        self.dims = dims\n\n    def forward(self, x: Tensor) -> Tensor:\n        return torch.permute(x, self.dims)\n\nclass FrozenBatchNorm2d(torch.nn.Module):\n    \"\"\"\n    BatchNorm2d where the batch statistics and the affine parameters are fixed\n    Args:\n        num_features (int): Number of features ``C`` from an expected input of size ``(N, C, H, W)``\n        eps (float): a value added to the denominator for numerical stability. Default: 1e-5\n    \"\"\"\n\n    def __init__(\n        self,\n        num_features: int,\n        eps: float = 1e-5,\n    ):\n        super().__init__()\n        self.eps = eps\n        self.register_buffer(\"weight\", torch.ones(num_features))\n        self.register_buffer(\"bias\", torch.zeros(num_features))\n        self.register_buffer(\"running_mean\", torch.zeros(num_features))\n        self.register_buffer(\"running_var\", torch.ones(num_features))\n\n    def _load_from_state_dict(\n        self,\n        state_dict: dict,\n        prefix: str,\n        local_metadata: dict,\n        strict: bool,\n        missing_keys: List[str],\n        unexpected_keys: List[str],\n        error_msgs: List[str],\n    ):\n        num_batches_tracked_key = prefix + \"num_batches_tracked\"\n        if num_batches_tracked_key in state_dict:\n            del state_dict[num_batches_tracked_key]\n\n        super()._load_from_state_dict(\n            state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs\n        )\n\n    def forward(self, x: Tensor) -> Tensor:\n        # move reshapes to the beginning\n        # to make it fuser-friendly\n        w = self.weight.reshape(1, -1, 1, 1)\n        b = self.bias.reshape(1, -1, 1, 1)\n        rv = self.running_var.reshape(1, -1, 1, 1)\n        rm = self.running_mean.reshape(1, -1, 1, 1)\n        scale = w * (rv + self.eps).rsqrt()\n        bias = b - rm * scale\n        return x * scale + bias\n\n\n    def __repr__(self) -> str:\n        return f\"{self.__class__.__name__}({self.weight.shape[0]}, eps={self.eps})\"\n\n\n\nclass ConvNormActivation(torch.nn.Sequential):\n    def __init__(\n        self,\n        in_channels: int,\n        out_channels: int,\n        kernel_size: Union[int, Tuple[int, ...]] = 3,\n        stride: Union[int, Tuple[int, ...]] = 1,\n        padding: Optional[Union[int, Tuple[int, ...], str]] = None,\n        groups: int = 1,\n        norm_layer: Optional[Callable[..., torch.nn.Module]] = torch.nn.BatchNorm2d,\n        activation_layer: Optional[Callable[..., torch.nn.Module]] = torch.nn.ReLU,\n        dilation: Union[int, Tuple[int, ...]] = 1,\n        inplace: Optional[bool] = True,\n        bias: Optional[bool] = None,\n        conv_layer: Callable[..., torch.nn.Module] = torch.nn.Conv2d,\n    ) -> None:\n\n        if padding is None:\n            if isinstance(kernel_size, int) and isinstance(dilation, int):\n                padding = (kernel_size - 1) // 2 * dilation\n            else:\n                _conv_dim = len(kernel_size) if isinstance(kernel_size, Sequence) else len(dilation)\n                kernel_size = _make_ntuple(kernel_size, _conv_dim)\n                dilation = _make_ntuple(dilation, _conv_dim)\n                padding = tuple((kernel_size[i] - 1) // 2 * dilation[i] for i in range(_conv_dim))\n        if bias is None:\n            bias = norm_layer is None\n\n        layers = [\n            conv_layer(\n                in_channels,\n                out_channels,\n                kernel_size,\n                stride,\n                padding,\n                dilation=dilation,\n                groups=groups,\n                bias=bias,\n            )\n        ]\n\n        if norm_layer is not None:\n            layers.append(norm_layer(out_channels))\n\n        if activation_layer is not None:\n            params = {} if inplace is None else {\"inplace\": inplace}\n            layers.append(activation_layer(**params))\n        super().__init__(*layers)\n        self.out_channels = out_channels\n\n        if self.__class__ == ConvNormActivation:\n            warnings.warn(\n                \"Don't use ConvNormActivation directly, please use Conv2dNormActivation and Conv3dNormActivation instead.\"\n            )\n\n\nclass Conv2dNormActivation(ConvNormActivation):\n    \"\"\"\n    Configurable block used for Convolution2d-Normalization-Activation blocks.\n    Args:\n        in_channels (int): Number of channels in the input image\n        out_channels (int): Number of channels produced by the Convolution-Normalization-Activation block\n        kernel_size: (int, optional): Size of the convolving kernel. Default: 3\n        stride (int, optional): Stride of the convolution. Default: 1\n        padding (int, tuple or str, optional): Padding added to all four sides of the input. Default: None, in which case it will calculated as ``padding = (kernel_size - 1) // 2 * dilation``\n        groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1\n        norm_layer (Callable[..., torch.nn.Module], optional): Norm layer that will be stacked on top of the convolution layer. If ``None`` this layer wont be used. Default: ``torch.nn.BatchNorm2d``\n        activation_layer (Callable[..., torch.nn.Module], optional): Activation function which will be stacked on top of the normalization layer (if not None), otherwise on top of the conv layer. If ``None`` this layer wont be used. Default: ``torch.nn.ReLU``\n        dilation (int): Spacing between kernel elements. Default: 1\n        inplace (bool): Parameter for the activation layer, which can optionally do the operation in-place. Default ``True``\n        bias (bool, optional): Whether to use bias in the convolution layer. By default, biases are included if ``norm_layer is None``.\n    \"\"\"\n\n    def __init__(\n        self,\n        in_channels: int,\n        out_channels: int,\n        kernel_size: Union[int, Tuple[int, int]] = 3,\n        stride: Union[int, Tuple[int, int]] = 1,\n        padding: Optional[Union[int, Tuple[int, int], str]] = None,\n        groups: int = 1,\n        norm_layer: Optional[Callable[..., torch.nn.Module]] = torch.nn.BatchNorm2d,\n        activation_layer: Optional[Callable[..., torch.nn.Module]] = torch.nn.ReLU,\n        dilation: Union[int, Tuple[int, int]] = 1,\n        inplace: Optional[bool] = True,\n        bias: Optional[bool] = None,\n    ) -> None:\n\n        super().__init__(\n            in_channels,\n            out_channels,\n            kernel_size,\n            stride,\n            padding,\n            groups,\n            norm_layer,\n            activation_layer,\n            dilation,\n            inplace,\n            bias,\n            torch.nn.Conv2d,\n        )\n\n\n\nclass LayerNorm2d(nn.LayerNorm):\n    def forward(self, x: Tensor) -> Tensor:\n        x = x.permute(0, 2, 3, 1)\n        x = F.layer_norm(x, self.normalized_shape, self.weight, self.bias, self.eps)\n        x = x.permute(0, 3, 1, 2)\n        return x\n\n\nclass CNBlock(nn.Module):\n    def __init__(\n        self,\n        dim,\n        layer_scale: float,\n        stochastic_depth_prob: float,\n        norm_layer: Optional[Callable[..., nn.Module]] = None,\n    ) -> None:\n        super().__init__()\n        if norm_layer is None:\n            norm_layer = partial(nn.LayerNorm, eps=1e-6)\n\n        self.block = nn.Sequential(\n            nn.Conv2d(dim, dim, kernel_size=7, padding=3, groups=dim, bias=True),\n            Permute([0, 2, 3, 1]),\n            norm_layer(dim),\n            nn.Linear(in_features=dim, out_features=4 * dim, bias=True),\n            nn.GELU(),\n            nn.Linear(in_features=4 * dim, out_features=dim, bias=True),\n            Permute([0, 3, 1, 2]),\n        )\n        self.layer_scale = nn.Parameter(torch.ones(dim, 1, 1) * layer_scale)\n        self.stochastic_depth = StochasticDepth(stochastic_depth_prob, \"row\")\n\n    def forward(self, input: Tensor) -> Tensor:\n        result = self.layer_scale * self.block(input)\n        result = self.stochastic_depth(result)\n        result += input\n        return result\n\n\nclass CNBlockConfig:\n    # Stores information listed at Section 3 of the ConvNeXt paper\n    def __init__(\n        self,\n        input_channels: int,\n        out_channels: Optional[int],\n        num_layers: int,\n    ) -> None:\n        self.input_channels = input_channels\n        self.out_channels = out_channels\n        self.num_layers = num_layers\n\n    def __repr__(self) -> str:\n        s = self.__class__.__name__ + \"(\"\n        s += \"input_channels={input_channels}\"\n        s += \", out_channels={out_channels}\"\n        s += \", num_layers={num_layers}\"\n        s += \")\"\n        return s.format(**self.__dict__)\n\n\nclass ConvNeXt(nn.Module):\n    def __init__(\n        self,\n        block_setting: List[CNBlockConfig],\n        stochastic_depth_prob: float = 0.0,\n        layer_scale: float = 1e-6,\n        num_classes: int = 1000,\n        block: Optional[Callable[..., nn.Module]] = None,\n        norm_layer: Optional[Callable[..., nn.Module]] = None,\n        **kwargs: Any,\n    ) -> None:\n        super().__init__()\n\n        if not block_setting:\n            raise ValueError(\"The block_setting should not be empty\")\n        elif not (isinstance(block_setting, Sequence) and all([isinstance(s, CNBlockConfig) for s in block_setting])):\n            raise TypeError(\"The block_setting should be List[CNBlockConfig]\")\n\n        if block is None:\n            block = CNBlock\n\n        if norm_layer is None:\n            norm_layer = partial(LayerNorm2d, eps=1e-6)\n\n        layers: List[nn.Module] = []\n\n        # Stem\n        firstconv_output_channels = block_setting[0].input_channels\n        # layers.append(\n        #     Conv2dNormActivation(\n        #         3,\n        #         firstconv_output_channels,\n        #         kernel_size=4,\n        #         stride=4,\n        #         padding=0,\n        #         norm_layer=norm_layer,\n        #         activation_layer=None,\n        #         bias=True,\n        #     )\n        # )\n        layers.append(\n            Conv2dNormActivation(\n                3,\n                firstconv_output_channels,\n                kernel_size=4,\n                stride=2,\n                padding=1,\n                norm_layer=norm_layer,\n                activation_layer=None,\n                bias=True,\n            )\n        )\n\n        total_stage_blocks = sum(cnf.num_layers for cnf in block_setting)\n        stage_block_id = 0\n        for cnf in block_setting:\n            # Bottlenecks\n            stage: List[nn.Module] = []\n            for _ in range(cnf.num_layers):\n                # adjust stochastic depth probability based on the depth of the stage block\n                sd_prob = stochastic_depth_prob * stage_block_id / (total_stage_blocks - 1.0)\n                stage.append(block(cnf.input_channels, layer_scale, sd_prob))\n                stage_block_id += 1\n            layers.append(nn.Sequential(*stage))\n            if cnf.out_channels is not None:\n                # Downsampling\n                layers.append(\n                    nn.Sequential(\n                        norm_layer(cnf.input_channels),\n                        nn.Conv2d(cnf.input_channels, cnf.out_channels, kernel_size=2, stride=2),\n                    )\n                )\n\n        self.features = nn.Sequential(*layers)\n        self.avgpool = nn.AdaptiveAvgPool2d(1)\n\n        lastblock = block_setting[-1]\n        lastconv_output_channels = (\n            lastblock.out_channels if lastblock.out_channels is not None else lastblock.input_channels\n        )\n        self.classifier = nn.Sequential(\n            norm_layer(lastconv_output_channels), nn.Flatten(1), nn.Linear(lastconv_output_channels, num_classes)\n        )\n\n        for m in self.modules():\n            if isinstance(m, (nn.Conv2d, nn.Linear)):\n                nn.init.trunc_normal_(m.weight, std=0.02)\n                if m.bias is not None:\n                    nn.init.zeros_(m.bias)\n\n    def _forward_impl(self, x: Tensor) -> Tensor:\n        x = self.features(x)\n        x = self.avgpool(x)\n        x = self.classifier(x)\n        return x\n\n    def forward(self, x: Tensor) -> Tensor:\n        return self._forward_impl(x)\n\n\ndef _convnext(\n    block_setting: List[CNBlockConfig],\n    stochastic_depth_prob: float,\n    weights_url,\n    progress: bool,\n    **kwargs: Any,\n):\n\n    model = ConvNeXt(block_setting, stochastic_depth_prob=stochastic_depth_prob, **kwargs)\n#     if weights_url is not None:\n#         weights =  load_state_dict_from_url(weights_url, progress=progress)\n#         model.load_state_dict(weights)\n\n    return model\n\n\n\nURL_DICT = {\n    \"tiny\": \"https://download.pytorch.org/models/convnext_tiny-983f1562.pth\",\n    \"small\": \"https://download.pytorch.org/models/convnext_small-0c510722.pth\",\n    \"base\": \"https://download.pytorch.org/models/convnext_base-6075fbad.pth\",\n    \"large\": \"https://download.pytorch.org/models/convnext_large-ea097f82.pth\"\n}\n\n\ndef convnext_tiny(pretrained = False, progress = True, **kwargs):\n\n    url = URL_DICT[\"tiny\"]\n    block_setting = [\n        CNBlockConfig(96, 192, 3),\n        CNBlockConfig(192, 384, 3),\n        CNBlockConfig(384, 768, 9),\n        CNBlockConfig(768, None, 3),\n    ]\n    stochastic_depth_prob = kwargs.pop(\"stochastic_depth_prob\", 0.1)\n    return _convnext(block_setting, stochastic_depth_prob, url, progress, **kwargs)\n\ndef convnext_large(pretrained = False, progress = True, **kwargs):\n\n    url = URL_DICT[\"large\"]\n    block_setting = [\n        CNBlockConfig(192, 384, 3),\n        CNBlockConfig(384, 768, 3),\n        CNBlockConfig(768, 1536, 27),\n        CNBlockConfig(1536, None, 3),\n    ]\n    stochastic_depth_prob = kwargs.pop(\"stochastic_depth_prob\", 0.1)\n    return _convnext(block_setting, stochastic_depth_prob, url, progress, **kwargs)","metadata":{"execution":{"iopub.status.busy":"2022-09-02T18:44:51.012511Z","iopub.execute_input":"2022-09-02T18:44:51.013533Z","iopub.status.idle":"2022-09-02T18:44:51.468361Z","shell.execute_reply.started":"2022-09-02T18:44:51.013480Z","shell.execute_reply":"2022-09-02T18:44:51.467017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_blocks_to_be_concat(model, x):\n    features = {}\n    def get_features(name):\n        def hook(model, input, output):\n            features[name] = output.detach()\n        return hook\n\n#     model[1][2].register_forward_hook(get_features('feat1'))\n#     model[3][2].register_forward_hook(get_features('feat2'))\n#     model[5][26].register_forward_hook(get_features('feat3'))\n\n    model[1][2].register_forward_hook(get_features('feat1'))\n    model[3][2].register_forward_hook(get_features('feat2'))\n    model[5][8].register_forward_hook(get_features('feat3'))\n\n\n    # make a forward pass to trigger the hooks\n    x = model(x)\n    # print (features[\"feat1\"].size())\n    # print (features[\"feat2\"].size())\n    # print (features[\"feat3\"].size())\n\n    return x, features\n\n\nclass ConvNextUnet(nn.Module):\n    def __init__(self, encoder,n_channels = 1536, out_channels=1, concat_input=True):\n        super().__init__()\n\n        self.encoder = encoder.features\n        print (self.encoder[1][2])\n        self.concat_input = concat_input\n\n        n_channels = 1536\n        self.size = [1536, 768, 384, 192]\n        self.up_conv1 = up_conv(n_channels, 768)\n        self.double_conv1 = double_conv(self.size[0], 768)\n\n        self.up_conv2 = up_conv(768, 384)\n        self.double_conv2 = double_conv(self.size[1], 384)\n\n        self.up_conv3 = up_conv(384, 192)\n        self.double_conv3 = double_conv(self.size[2], 192)\n\n        self.up_conv_input = up_conv(192, 96)\n        self.double_conv_input = double_conv(99, 32)\n\n#         n_channels = 768\n#         self.size = [768, 384, 192, 96]\n#         self.up_conv1 = up_conv(n_channels, 384)\n#         self.double_conv1 = double_conv(self.size[0], 384)\n\n#         self.up_conv2 = up_conv(384, 192)\n#         self.double_conv2 = double_conv(self.size[1], 192)\n\n#         self.up_conv3 = up_conv(192, 96)\n#         self.double_conv3 = double_conv(self.size[2], 96)\n\n#         self.up_conv_input = up_conv(96, 32)\n#         self.double_conv_input = double_conv(35, 16)\n\n#         self.final_conv = nn.Conv2d(16, 1, kernel_size=1)\n        self.final_conv = nn.Conv2d(32, 1, kernel_size=1)\n\n\n    def forward(self, x):\n        input_ = x\n        x, blocks = get_blocks_to_be_concat(self.encoder, x)\n\n        x = self.up_conv1(x)\n        x = torch.cat([x, blocks[\"feat3\"]], dim=1)\n        x = self.double_conv1(x)\n\n        x = self.up_conv2(x)\n        x = torch.cat([x, blocks[\"feat2\"]], dim=1)\n        x = self.double_conv2(x)\n\n        x = self.up_conv3(x)\n        x = torch.cat([x, blocks[\"feat1\"]], dim=1)\n        x = self.double_conv3(x)\n\n        x = self.up_conv_input(x)\n        x = torch.cat([x, input_], dim=1)\n        x = self.double_conv_input(x)\n\n        x = self.final_conv(x)\n\n        return x\n\n\n\ndef get_encoder():\n    model = convnext_large(pretrained = False)\n    return model\n\ndef create_convnext_unet():\n    encoder = get_encoder()\n    model = ConvNextUnet(encoder)\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-09-02T18:44:53.568403Z","iopub.execute_input":"2022-09-02T18:44:53.568911Z","iopub.status.idle":"2022-09-02T18:44:53.598118Z","shell.execute_reply.started":"2022-09-02T18:44:53.568870Z","shell.execute_reply":"2022-09-02T18:44:53.596904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_efficient_net():\n    # unet = get_efficientunet_b7(out_channels=1, concat_input=True, pretrained=True).cuda()\n    unet = create_convnext_unet()\n    return unet","metadata":{"execution":{"iopub.status.busy":"2022-09-02T18:44:54.376498Z","iopub.execute_input":"2022-09-02T18:44:54.376918Z","iopub.status.idle":"2022-09-02T18:44:54.382869Z","shell.execute_reply.started":"2022-09-02T18:44:54.376884Z","shell.execute_reply":"2022-09-02T18:44:54.381422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_optimizer(model):\n\n    base_lr = 1e-4\n    no_weight_decay_on_bn = False\n    params_to_update = []\n    for name, param in model.named_parameters():\n        if param.requires_grad:\n            params_to_update.append(param)\n    optimizer = torch.optim.AdamW(params_to_update,lr = base_lr)\n    return optimizer","metadata":{"execution":{"iopub.status.busy":"2022-09-02T18:44:55.907527Z","iopub.execute_input":"2022-09-02T18:44:55.907950Z","iopub.status.idle":"2022-09-02T18:44:55.915785Z","shell.execute_reply.started":"2022-09-02T18:44:55.907915Z","shell.execute_reply":"2022-09-02T18:44:55.913796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CreateLoss(nn.Module):\n    def __init__(self):\n        super(CreateLoss, self).__init__()\n        self.loss_criterion = nn.BCEWithLogitsLoss()\n\n    def forward(self, res_target, masks_target):\n        res_target = res_target.squeeze(1)\n        loss_target = self.loss_criterion(res_target, masks_target)\n        return loss_target","metadata":{"execution":{"iopub.status.busy":"2022-09-02T18:37:31.461478Z","iopub.execute_input":"2022-09-02T18:37:31.462387Z","iopub.status.idle":"2022-09-02T18:37:31.471156Z","shell.execute_reply.started":"2022-09-02T18:37:31.462337Z","shell.execute_reply":"2022-09-02T18:37:31.469751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class AverageMeter:\n    \"\"\"\n    Class for calculating average\n    \"\"\"\n    def __init__(self):\n        self.reset()\n\n    def reset(self):\n        self.val = 0\n        self.avg = 0\n        self.sum = 0\n        self.count = 0\n\n    def update(self, val, num):\n        self.val = val\n        self.sum += val * num\n        self.count += num\n        self.avg = self.sum / self.count","metadata":{"execution":{"iopub.status.busy":"2022-09-02T18:37:31.910920Z","iopub.execute_input":"2022-09-02T18:37:31.911722Z","iopub.status.idle":"2022-09-02T18:37:31.919875Z","shell.execute_reply.started":"2022-09-02T18:37:31.911671Z","shell.execute_reply":"2022-09-02T18:37:31.918704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def set_seeds():\n    print (\"Setting up seeds!\")\n    random.seed(0)\n    np.random.seed(0)\n    torch.manual_seed(0)\n    torch.cuda.manual_seed(0)\n\ndef setup_cudnn(cudnn_benchmark, cudnn_deterministic):\n    print (\"Setting up cudnn!\")\n    torch.backends.cudnn.benchmark = cudnn_benchmark\n    torch.backends.cudnn.deterministic = cudnn_deterministic\n\ndef MakeDir(path):\n    if (not os.path.exists(path)):\n        os.mkdir(path)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-02T18:37:33.557489Z","iopub.execute_input":"2022-09-02T18:37:33.558981Z","iopub.status.idle":"2022-09-02T18:37:33.569738Z","shell.execute_reply.started":"2022-09-02T18:37:33.558911Z","shell.execute_reply":"2022-09-02T18:37:33.568264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train(epoch,model,optimizer,scheduler,loss_function,train_loader):\n    print (\"Training!\")\n    use_cuda = True\n    model.train()\n    device = torch.device(\"cuda\" if (use_cuda and torch.cuda.is_available()) else \"cpu\")\n    Tensor = torch.cuda.FloatTensor if (use_cuda and torch.cuda.is_available()) else torch.FloatTensor\n    \n    Train_Loss_meter = AverageMeter()\n    for step, sample in enumerate(train_loader):\n        images, masks = sample\n        images = images.to(device).float()\n        masks = masks.to(device).float()\n        optimizer.zero_grad()\n        res = model(images)\n        print (res.size())\n        loss = loss_function(res, masks)\n\n        print (\"Loss : \" + str(loss))\n        loss.backward()\n        optimizer.step()\n\n        num = res.size(0)\n        Train_Loss_meter.update(loss.item(), num)\n\n    print (\"Epoch : \" + str(epoch) + \" :: TrainLoss : \" + str(Train_Loss_meter.avg))\n","metadata":{"execution":{"iopub.status.busy":"2022-09-01T16:25:42.389356Z","iopub.execute_input":"2022-09-01T16:25:42.391194Z","iopub.status.idle":"2022-09-01T16:25:42.402526Z","shell.execute_reply.started":"2022-09-01T16:25:42.391127Z","shell.execute_reply":"2022-09-01T16:25:42.400967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def validate(epoch,model,loss_function,train_loader):\n    print (\"Validating!\")\n    model.eval()\n    use_cuda = True\n    device = torch.device(\"cuda\" if (use_cuda and torch.cuda.is_available()) else \"cpu\")\n    Tensor = torch.cuda.FloatTensor if (use_cuda and torch.cuda.is_available()) else torch.FloatTensor\n    \n    Train_Loss_meter = AverageMeter()\n\n    l = []\n    for step, sample in enumerate(train_loader):\n        images, masks = sample\n        images = images.to(device).float()\n        masks = masks.to(device).float()\n        res = model(images)\n        loss = loss_function(res, masks)\n        num = res.size(0)\n        Train_Loss_meter.update(loss.item(), num)\n\n    print (\"Epoch : \" + str(epoch) + \" :: TrainLoss : \" + str(Train_Loss_meter.avg))","metadata":{"execution":{"iopub.status.busy":"2022-09-01T16:25:43.186640Z","iopub.execute_input":"2022-09-01T16:25:43.188173Z","iopub.status.idle":"2022-09-01T16:25:43.199418Z","shell.execute_reply.started":"2022-09-01T16:25:43.188109Z","shell.execute_reply":"2022-09-01T16:25:43.197698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!nvidia-smi","metadata":{"execution":{"iopub.status.busy":"2022-09-01T14:54:56.569833Z","iopub.execute_input":"2022-09-01T14:54:56.574233Z","iopub.status.idle":"2022-09-01T14:54:57.989021Z","shell.execute_reply.started":"2022-09-01T14:54:56.574191Z","shell.execute_reply":"2022-09-01T14:54:57.987270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# use_cuda = True\n# cuda_device_id = 0\n# cudnn_benchmark = False\n# cudnn_deterministic = False\n# device = torch.device(\"cuda\" if (use_cuda and torch.cuda.is_available()) else \"cpu\")\n# working_dir = \"/kaggle/working/\"\n# print(f\"The current working directory is {working_dir}\")\n# MakeDir(working_dir + \"/checkpoint_save/\")\n# if (use_cuda):\n#     torch.cuda.set_device(cuda_device_id)\n# set_seeds()\n# setup_cudnn(cudnn_benchmark, cudnn_deterministic)","metadata":{"execution":{"iopub.status.busy":"2022-09-01T14:54:57.992182Z","iopub.execute_input":"2022-09-01T14:54:57.992717Z","iopub.status.idle":"2022-09-01T14:54:58.099152Z","shell.execute_reply.started":"2022-09-01T14:54:57.992664Z","shell.execute_reply":"2022-09-01T14:54:58.097374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model = create_efficient_net()\n# model = model.to(device)","metadata":{"execution":{"iopub.status.busy":"2022-09-01T14:54:58.101948Z","iopub.execute_input":"2022-09-01T14:54:58.102556Z","iopub.status.idle":"2022-09-01T14:55:14.921850Z","shell.execute_reply.started":"2022-09-01T14:54:58.102511Z","shell.execute_reply":"2022-09-01T14:55:14.920367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_scheduler(optimizer):\n    milestones = [40,60,90]\n    lr_decay = 0.1\n    scheduler = torch.optim.lr_scheduler.MultiStepLR(\n        optimizer,\n        milestones=milestones,\n        gamma=lr_decay)\n    return scheduler","metadata":{"execution":{"iopub.status.busy":"2022-09-01T14:55:14.925210Z","iopub.execute_input":"2022-09-01T14:55:14.925665Z","iopub.status.idle":"2022-09-01T14:55:14.933812Z","shell.execute_reply.started":"2022-09-01T14:55:14.925620Z","shell.execute_reply":"2022-09-01T14:55:14.932046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# loss_function = CreateLoss()\n# optimizer = create_optimizer(model)\n# scheduler = create_scheduler(optimizer)","metadata":{"execution":{"iopub.status.busy":"2022-09-01T14:55:14.935987Z","iopub.execute_input":"2022-09-01T14:55:14.936802Z","iopub.status.idle":"2022-09-01T14:55:14.950078Z","shell.execute_reply.started":"2022-09-01T14:55:14.936758Z","shell.execute_reply":"2022-09-01T14:55:14.948393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install fvcore","metadata":{"execution":{"iopub.status.busy":"2022-09-01T14:55:14.952602Z","iopub.execute_input":"2022-09-01T14:55:14.953254Z","iopub.status.idle":"2022-09-01T14:55:35.578076Z","shell.execute_reply.started":"2022-09-01T14:55:14.953210Z","shell.execute_reply":"2022-09-01T14:55:35.576366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from fvcore.common.checkpoint import Checkpointer\n# checkpointer = Checkpointer(model,\n#                         optimizer=optimizer,\n#                         scheduler=scheduler,\n#                         save_dir= working_dir + \"/checkpoint_save/\",\n#                         save_to_disk=True)","metadata":{"execution":{"iopub.status.busy":"2022-09-01T14:55:35.580918Z","iopub.execute_input":"2022-09-01T14:55:35.582781Z","iopub.status.idle":"2022-09-01T14:55:35.620909Z","shell.execute_reply.started":"2022-09-01T14:55:35.582689Z","shell.execute_reply":"2022-09-01T14:55:35.619523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_loader, test_loader = create_dataloader()","metadata":{"execution":{"iopub.status.busy":"2022-09-01T14:55:35.622790Z","iopub.execute_input":"2022-09-01T14:55:35.623495Z","iopub.status.idle":"2022-09-01T14:55:36.113669Z","shell.execute_reply.started":"2022-09-01T14:55:35.623440Z","shell.execute_reply":"2022-09-01T14:55:36.112086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Starting Training\n# # -----------------------------------------\n# print (\"Starting Training!\")\n# epochs = 25\n# val_period = 1\n# checkpoint_period = 5\n# print (\"Number of epochs : \" + str(epochs))\n# print (\"Validation Period : \" + str(val_period))\n# print (\"Checkpoint Period : \" + str(checkpoint_period))\n# # input(\"Press Enter to Start Training Model ?\")\n# for epoch in range(1, epochs + 1):\n#     print (\"Epoch Number => \" + str(epoch))\n#     train(epoch,model,optimizer,scheduler,loss_function,train_loader)\n#     scheduler.step()\n# #     if epoch % val_period == 0:\n# #         validate(epoch, model, loss_function, test_loader)\n#     if (epoch % checkpoint_period == 0 or epoch == epochs):\n#         checkpoint_config = {'epoch': epoch}\n#         checkpointer.save(f\"checkpoint_{epoch:04d}\", **checkpoint_config)\n#     print (\"------------------------------------\")","metadata":{"execution":{"iopub.status.busy":"2022-09-02T18:41:51.480324Z","iopub.execute_input":"2022-09-02T18:41:51.480754Z","iopub.status.idle":"2022-09-02T18:41:51.487010Z","shell.execute_reply.started":"2022-09-02T18:41:51.480719Z","shell.execute_reply":"2022-09-02T18:41:51.485650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !ls","metadata":{"execution":{"iopub.status.busy":"2022-09-01T16:06:55.022182Z","iopub.execute_input":"2022-09-01T16:06:55.023565Z","iopub.status.idle":"2022-09-01T16:06:56.508113Z","shell.execute_reply.started":"2022-09-01T16:06:55.023515Z","shell.execute_reply":"2022-09-01T16:06:56.506291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !cd checkpoint_save","metadata":{"execution":{"iopub.status.busy":"2022-09-01T16:07:10.335750Z","iopub.execute_input":"2022-09-01T16:07:10.336259Z","iopub.status.idle":"2022-09-01T16:07:11.835876Z","shell.execute_reply.started":"2022-09-01T16:07:10.336225Z","shell.execute_reply":"2022-09-01T16:07:11.833980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !ls","metadata":{"execution":{"iopub.status.busy":"2022-09-01T16:09:08.463775Z","iopub.execute_input":"2022-09-01T16:09:08.464373Z","iopub.status.idle":"2022-09-01T16:09:10.076764Z","shell.execute_reply.started":"2022-09-01T16:09:08.464308Z","shell.execute_reply":"2022-09-01T16:09:10.075016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls","metadata":{"execution":{"iopub.status.busy":"2022-09-02T18:39:11.482580Z","iopub.execute_input":"2022-09-02T18:39:11.484079Z","iopub.status.idle":"2022-09-02T18:39:12.778832Z","shell.execute_reply.started":"2022-09-02T18:39:11.484007Z","shell.execute_reply":"2022-09-02T18:39:12.777371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"use_cuda = True\ndevice_id = 0\n# model_path = \"./checkpoint_save/checkpoint_0020.pth\"\nmodel_path = \"/kaggle/input/hubmap-convnext-checkpoint/checkpoint_0030.pth\"","metadata":{"execution":{"iopub.status.busy":"2022-09-02T18:45:06.239973Z","iopub.execute_input":"2022-09-02T18:45:06.240676Z","iopub.status.idle":"2022-09-02T18:45:06.246922Z","shell.execute_reply.started":"2022-09-02T18:45:06.240632Z","shell.execute_reply":"2022-09-02T18:45:06.245341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda\" if (use_cuda and torch.cuda.is_available()) else \"cpu\")\n\nif (use_cuda):\n    torch.cuda.set_device(device_id)\n\nmodel = create_efficient_net()\nmodel = model.to(device)\n# model.load_state_dict(torch.load(model_path)[\"model\"])","metadata":{"execution":{"iopub.status.busy":"2022-09-02T18:45:07.116857Z","iopub.execute_input":"2022-09-02T18:45:07.117305Z","iopub.status.idle":"2022-09-02T18:45:15.918166Z","shell.execute_reply.started":"2022-09-02T18:45:07.117267Z","shell.execute_reply":"2022-09-02T18:45:15.917015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_state_dict(torch.load(model_path)[\"model\"])","metadata":{"execution":{"iopub.status.busy":"2022-09-02T18:45:29.555276Z","iopub.execute_input":"2022-09-02T18:45:29.556435Z","iopub.status.idle":"2022-09-02T18:45:51.870411Z","shell.execute_reply.started":"2022-09-02T18:45:29.556385Z","shell.execute_reply":"2022-09-02T18:45:51.869198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_df_test = pd.read_csv(\"/kaggle/input/hubmap-organ-segmentation/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-09-02T18:45:56.918188Z","iopub.execute_input":"2022-09-02T18:45:56.918861Z","iopub.status.idle":"2022-09-02T18:45:56.937412Z","shell.execute_reply.started":"2022-09-02T18:45:56.918799Z","shell.execute_reply":"2022-09-02T18:45:56.936004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_df_test.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-09-02T18:45:57.905034Z","iopub.execute_input":"2022-09-02T18:45:57.905878Z","iopub.status.idle":"2022-09-02T18:45:57.928577Z","shell.execute_reply.started":"2022-09-02T18:45:57.905836Z","shell.execute_reply":"2022-09-02T18:45:57.927209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_l = all_df_test[\"id\"].values\nimage_width_l = all_df_test[\"img_width\"].values","metadata":{"execution":{"iopub.status.busy":"2022-09-02T18:46:02.985819Z","iopub.execute_input":"2022-09-02T18:46:02.986521Z","iopub.status.idle":"2022-09-02T18:46:03.001347Z","shell.execute_reply.started":"2022-09-02T18:46:02.986470Z","shell.execute_reply":"2022-09-02T18:46:02.999601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_l","metadata":{"execution":{"iopub.status.busy":"2022-09-02T18:46:05.195558Z","iopub.execute_input":"2022-09-02T18:46:05.196441Z","iopub.status.idle":"2022-09-02T18:46:05.204530Z","shell.execute_reply.started":"2022-09-02T18:46:05.196400Z","shell.execute_reply":"2022-09-02T18:46:05.203149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#https://www.kaggle.com/bguberfain/memory-aware-rle-encoding\n#with transposed mask\ndef 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-09-02T18:46:06.278019Z","iopub.execute_input":"2022-09-02T18:46:06.278659Z","iopub.status.idle":"2022-09-02T18:46:06.293304Z","shell.execute_reply.started":"2022-09-02T18:46:06.278599Z","shell.execute_reply":"2022-09-02T18:46:06.291606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"names = []\npreds = []\nthreshold = 0.5\nfor idx in tqdm(range(len(id_l))):\n    id = id_l[idx]\n    img_w = image_width_l[idx]\n\n    image_path = \"/kaggle/input/hubmap-organ-segmentation/test_images/\" + str(id) + \".tiff\"\n    img = cv2.imread(image_path)\n    img = cv2.resize(img, (512, 512))\n    img = img/255.0\n    img = torch.from_numpy(np.array(img))\n    img = img.permute(2,0,1)\n    img = img.unsqueeze(0)\n    img = img.to(device).float()\n    pred_mask = model(img)\n    pred_mask = pred_mask.squeeze(1)\n    pred_mask = pred_mask.cpu().data.numpy()\n    pred_mask = cv2.resize(pred_mask, (img_w, img_w))    \n    pred_mask = pred_mask > threshold\n    rle = rle_encode_less_memory(pred_mask)\n    names.append(id)\n    preds.append(rle)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-02T18:46:11.165201Z","iopub.execute_input":"2022-09-02T18:46:11.166412Z","iopub.status.idle":"2022-09-02T18:47:08.332564Z","shell.execute_reply.started":"2022-09-02T18:46:11.166368Z","shell.execute_reply":"2022-09-02T18:47:08.331000Z"},"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-09-02T18:48:15.274703Z","iopub.execute_input":"2022-09-02T18:48:15.275704Z","iopub.status.idle":"2022-09-02T18:48:15.318915Z","shell.execute_reply.started":"2022-09-02T18:48:15.275659Z","shell.execute_reply":"2022-09-02T18:48:15.317738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-02T18:48:15.939301Z","iopub.execute_input":"2022-09-02T18:48:15.940062Z","iopub.status.idle":"2022-09-02T18:48:15.962888Z","shell.execute_reply.started":"2022-09-02T18:48:15.940020Z","shell.execute_reply":"2022-09-02T18:48:15.961680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}