{"metadata":{"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":30892,"databundleVersionId":2715462,"sourceType":"competition"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.10.12"},"papermill":{"default_parameters":{},"duration":7774.718192,"end_time":"2023-11-13T09:21:28.081859","environment_variables":{},"exception":true,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2023-11-13T07:11:53.363667","version":"2.4.0"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install torchsummary\n!pip install torchgeometry","metadata":{"execution":{"iopub.execute_input":"2023-11-17T04:56:33.515782Z","iopub.status.busy":"2023-11-17T04:56:33.515145Z","iopub.status.idle":"2023-11-17T04:56:57.466276Z","shell.execute_reply":"2023-11-17T04:56:57.465140Z","shell.execute_reply.started":"2023-11-17T04:56:33.515740Z"},"papermill":{"duration":24.514406,"end_time":"2023-11-13T07:12:21.159477","exception":false,"start_time":"2023-11-13T07:11:56.645071","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchsummary import summary\nfrom torchgeometry.losses import one_hot\nimport os\nimport pandas as pd\nimport numpy as np\nfrom PIL import Image\nimport cv2\nimport time\nimport imageio\nimport matplotlib.pyplot as plt\nimport time\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.optim as optim\nfrom torch.optim import lr_scheduler\nfrom torch import Tensor\nfrom torch.utils.data import Dataset, DataLoader, random_split\nfrom torchvision.transforms import Resize, PILToTensor, ToPILImage, Compose, InterpolationMode\nfrom collections import OrderedDict\nimport wandb\nimport albumentations as A\nfrom albumentations.pytorch.transforms import ToTensorV2","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.execute_input":"2023-11-17T04:56:57.469657Z","iopub.status.busy":"2023-11-17T04:56:57.468850Z","iopub.status.idle":"2023-11-17T04:57:03.255439Z","shell.execute_reply":"2023-11-17T04:57:03.254673Z","shell.execute_reply.started":"2023-11-17T04:56:57.469605Z"},"papermill":{"duration":5.358224,"end_time":"2023-11-13T07:12:26.531689","exception":false,"start_time":"2023-11-13T07:12:21.173465","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!nvidia-smi -L","metadata":{"execution":{"iopub.execute_input":"2023-11-17T04:57:03.256831Z","iopub.status.busy":"2023-11-17T04:57:03.256430Z","iopub.status.idle":"2023-11-17T04:57:04.264313Z","shell.execute_reply":"2023-11-17T04:57:04.263302Z","shell.execute_reply.started":"2023-11-17T04:57:03.256807Z"},"papermill":{"duration":0.958271,"end_time":"2023-11-13T07:12:27.503887","exception":false,"start_time":"2023-11-13T07:12:26.545616","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\ndevice","metadata":{"execution":{"iopub.execute_input":"2023-11-17T04:57:04.267139Z","iopub.status.busy":"2023-11-17T04:57:04.266835Z","iopub.status.idle":"2023-11-17T04:57:04.298549Z","shell.execute_reply":"2023-11-17T04:57:04.297737Z","shell.execute_reply.started":"2023-11-17T04:57:04.267113Z"},"papermill":{"duration":0.082489,"end_time":"2023-11-13T07:12:27.600110","exception":false,"start_time":"2023-11-13T07:12:27.517621","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Parameters","metadata":{"papermill":{"duration":0.013953,"end_time":"2023-11-13T07:12:27.627781","exception":false,"start_time":"2023-11-13T07:12:27.613828","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# Number of class in the data set (3: neoplastic, non neoplastic, background)\nnum_classes = 3\n\n# Number of epoch\nepochs = 50\n\n# Hyperparameters for training \nlearning_rate = 2e-3\nbatch_size = 8\ndisplay_step = 2\n\n# Model path\ncheckpoint_path = '/kaggle/working/unet_model.pth'\npretrained_path = \"/kaggle/input/unet-checkpoint/unet_model.pth\"\n# Initialize lists to keep track of loss and accuracy\nloss_epoch_array = []\ntrain_accuracy = []\ntest_accuracy = []\nvalid_accuracy = []","metadata":{"execution":{"iopub.execute_input":"2023-11-17T04:57:04.300239Z","iopub.status.busy":"2023-11-17T04:57:04.299982Z","iopub.status.idle":"2023-11-17T04:57:04.311595Z","shell.execute_reply":"2023-11-17T04:57:04.310891Z","shell.execute_reply.started":"2023-11-17T04:57:04.300218Z"},"papermill":{"duration":0.021712,"end_time":"2023-11-13T07:12:27.662876","exception":false,"start_time":"2023-11-13T07:12:27.641164","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dataloader","metadata":{"papermill":{"duration":0.013261,"end_time":"2023-11-13T07:12:27.690633","exception":false,"start_time":"2023-11-13T07:12:27.677372","status":"completed"},"tags":[]}},{"cell_type":"code","source":"class UNetDataClass(Dataset):\n    def __init__(self, images_path, masks_path):\n        super(UNetDataClass, self).__init__()\n        \n        images_list = os.listdir(images_path)\n        masks_list = os.listdir(masks_path)\n        \n        images_list = [images_path + image_name for image_name in images_list]\n        masks_list = [masks_path + mask_name for mask_name in masks_list]\n        \n        self.images_list = images_list\n        self.masks_list = masks_list\n        self.transform = A.Compose([\n#                             A.Resize(384, 384),\n                            A.HorizontalFlip(p=0.5),\n                            A.VerticalFlip(p=0.5),\n                            A.RandomGamma(gamma_limit=(70, 130), eps=None, always_apply=False, p=0.2),\n                            A.RGBShift(p=0.3, r_shift_limit=10, g_shift_limit=10, b_shift_limit=10),\n                            A.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),\n                            ToTensorV2(),\n                    ])\n#         self.transform = Compose([Resize((384, 384), interpolation=InterpolationMode.BILINEAR),\n#                      PILToTensor()])\n    \n    def read_mask(self, mask_path):\n        image = cv2.imread(mask_path)\n        image = cv2.resize(image, (384, 384))\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)\n        # lower boundary RED color range values; Hue (0 - 10)\n        lower1 = np.array([0, 100, 20])\n        upper1 = np.array([10, 255, 255])\n        # upper boundary RED color range values; Hue (160 - 180)\n        lower2 = np.array([160,100,20])\n        upper2 = np.array([179,255,255])\n        lower_mask = cv2.inRange(image, lower1, upper1)\n        upper_mask = cv2.inRange(image, lower2, upper2)\n        red_mask = lower_mask + upper_mask;\n        red_mask[red_mask != 0] = 2\n        # boundary RED color range values; Hue (36 - 70)\n        green_mask = cv2.inRange(image, (36, 25, 25), (70, 255,255))\n        green_mask[green_mask != 0] = 1\n        full_mask = cv2.bitwise_or(red_mask, green_mask)\n        full_mask = full_mask.astype(np.uint8)\n        return full_mask\n\n    def __getitem__(self, index):\n        img_path = self.images_list[index]\n        mask_path = self.masks_list[index]\n        # Open image and mask\n        image = cv2.imread(img_path)\n        image = cv2.resize(image, (384, 384)) \n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        label = self.read_mask(mask_path)\n        # Normalize\n        transformed = self.transform(image=image, mask = label)\n        data = transformed[\"image\"]\n        label = transformed[\"mask\"]\n        \n#         label = torch.Tensor.float(label)\n#         label = label.permute(2,0,1)\n# #         label = torch.where(label>0.65, 1.0, 0.0)\n        \n# #         label[:, :, 2] = 0.0001\n#         label = torch.argmax(label, 0).type(torch.int64)\n\n        return data, label\n\n    \n    def __len__(self):\n        return len(self.images_list)","metadata":{"execution":{"iopub.execute_input":"2023-11-17T04:57:04.313007Z","iopub.status.busy":"2023-11-17T04:57:04.312711Z","iopub.status.idle":"2023-11-17T04:57:04.328659Z","shell.execute_reply":"2023-11-17T04:57:04.327800Z","shell.execute_reply.started":"2023-11-17T04:57:04.312984Z"},"papermill":{"duration":0.025507,"end_time":"2023-11-13T07:12:27.763903","exception":false,"start_time":"2023-11-13T07:12:27.738396","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_path = \"/kaggle/input/bkai-igh-neopolyp/train/train/\"\nmasks_path =  \"/kaggle/input/bkai-igh-neopolyp/train_gt/train_gt/\"","metadata":{"execution":{"iopub.execute_input":"2023-11-17T04:57:04.330068Z","iopub.status.busy":"2023-11-17T04:57:04.329760Z","iopub.status.idle":"2023-11-17T04:57:04.340409Z","shell.execute_reply":"2023-11-17T04:57:04.339673Z","shell.execute_reply.started":"2023-11-17T04:57:04.330045Z"},"papermill":{"duration":0.019707,"end_time":"2023-11-13T07:12:27.797020","exception":false,"start_time":"2023-11-13T07:12:27.777313","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unet_dataset = UNetDataClass(images_path, masks_path)","metadata":{"execution":{"iopub.execute_input":"2023-11-17T04:57:04.342161Z","iopub.status.busy":"2023-11-17T04:57:04.341525Z","iopub.status.idle":"2023-11-17T04:57:04.453968Z","shell.execute_reply":"2023-11-17T04:57:04.453175Z","shell.execute_reply.started":"2023-11-17T04:57:04.342128Z"},"papermill":{"duration":0.299707,"end_time":"2023-11-13T07:12:28.110322","exception":false,"start_time":"2023-11-13T07:12:27.810615","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_size = 0.8\nvalid_size = 0.2","metadata":{"execution":{"iopub.execute_input":"2023-11-17T04:57:04.455239Z","iopub.status.busy":"2023-11-17T04:57:04.454998Z","iopub.status.idle":"2023-11-17T04:57:04.459141Z","shell.execute_reply":"2023-11-17T04:57:04.458311Z","shell.execute_reply.started":"2023-11-17T04:57:04.455219Z"},"papermill":{"duration":0.021116,"end_time":"2023-11-13T07:12:28.187054","exception":false,"start_time":"2023-11-13T07:12:28.165938","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set, valid_set = random_split(unet_dataset, \n                                    [int(train_size * len(unet_dataset)) , \n                                     int(valid_size * len(unet_dataset))])","metadata":{"execution":{"iopub.execute_input":"2023-11-17T04:57:04.463107Z","iopub.status.busy":"2023-11-17T04:57:04.462826Z","iopub.status.idle":"2023-11-17T04:57:04.479638Z","shell.execute_reply":"2023-11-17T04:57:04.478820Z","shell.execute_reply.started":"2023-11-17T04:57:04.463069Z"},"papermill":{"duration":0.031141,"end_time":"2023-11-13T07:12:28.231741","exception":false,"start_time":"2023-11-13T07:12:28.200600","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataloader = DataLoader(train_set, batch_size=batch_size, shuffle=True)\nvalid_dataloader = DataLoader(valid_set, batch_size=batch_size, shuffle=True)","metadata":{"execution":{"iopub.execute_input":"2023-11-17T04:57:04.480985Z","iopub.status.busy":"2023-11-17T04:57:04.480726Z","iopub.status.idle":"2023-11-17T04:57:04.485647Z","shell.execute_reply":"2023-11-17T04:57:04.484829Z","shell.execute_reply.started":"2023-11-17T04:57:04.480964Z"},"papermill":{"duration":0.020623,"end_time":"2023-11-13T07:12:28.265904","exception":false,"start_time":"2023-11-13T07:12:28.245281","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"item = unet_dataset.__getitem__(1)\nprint(item[1], item[1].shape)","metadata":{"execution":{"iopub.execute_input":"2023-11-17T04:57:04.487384Z","iopub.status.busy":"2023-11-17T04:57:04.486995Z","iopub.status.idle":"2023-11-17T04:57:04.613811Z","shell.execute_reply":"2023-11-17T04:57:04.612986Z","shell.execute_reply.started":"2023-11-17T04:57:04.487353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{"papermill":{"duration":0.013405,"end_time":"2023-11-13T07:12:28.293069","exception":false,"start_time":"2023-11-13T07:12:28.279664","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"**Encoder Block**","metadata":{"papermill":{"duration":0.013187,"end_time":"2023-11-13T07:12:28.319611","exception":false,"start_time":"2023-11-13T07:12:28.306424","status":"completed"},"tags":[]}},{"cell_type":"code","source":"class encoder_block(nn.Module):\n    def __init__(self, in_channels, out_channels):\n        super(encoder_block, self).__init__()\n        \n        self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding='same')\n        self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding='same')\n        self.conv3 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding='same')\n        \n        self.bn1 = nn.BatchNorm2d(out_channels)\n        self.bn2 = nn.BatchNorm2d(out_channels)\n        self.bn3 = nn.BatchNorm2d(out_channels)\n        \n        self.relu = nn.ReLU()\n        self.dropout = nn.Dropout(p=0.3)\n        self.max_pool = nn.MaxPool2d(kernel_size=2, stride=2)\n        \n    def forward(self, x):\n        x = self.conv1(x)\n        x = self.bn1(x)\n        x = self.relu(x)\n        \n        x = self.dropout(x)\n        \n        x = self.conv2(x)\n        x = self.bn2(x)\n        x = self.relu(x)\n        \n        x = self.conv3(x)\n        x = self.bn3(x)\n        x = self.relu(x)\n        \n        next_layer = self.max_pool(x)\n        skip_layer = x\n        \n        return next_layer, skip_layer","metadata":{"execution":{"iopub.execute_input":"2023-11-17T04:57:04.615409Z","iopub.status.busy":"2023-11-17T04:57:04.615142Z","iopub.status.idle":"2023-11-17T04:57:04.623460Z","shell.execute_reply":"2023-11-17T04:57:04.622598Z","shell.execute_reply.started":"2023-11-17T04:57:04.615386Z"},"papermill":{"duration":0.024183,"end_time":"2023-11-13T07:12:28.357222","exception":false,"start_time":"2023-11-13T07:12:28.333039","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Decoder block**","metadata":{"papermill":{"duration":0.013371,"end_time":"2023-11-13T07:12:28.384103","exception":false,"start_time":"2023-11-13T07:12:28.370732","status":"completed"},"tags":[]}},{"cell_type":"code","source":"class decoder_block(nn.Module):\n    def __init__(self, in_channels, out_channels):\n        super(decoder_block, self).__init__()\n        \n        self.transpose_conv = nn.ConvTranspose2d(in_channels, out_channels, kernel_size=2, stride=2)\n        \n        self.conv1 = nn.Conv2d(2 * out_channels, out_channels, kernel_size=3, stride=1, padding='same')\n        self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding='same')\n        self.conv3 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding='same')\n        \n        self.bn1 = nn.BatchNorm2d(out_channels)\n        self.bn2 = nn.BatchNorm2d(out_channels)\n        self.bn3 = nn.BatchNorm2d(out_channels)\n        \n        self.relu = nn.ReLU() \n        self.dropout = nn.Dropout(p=0.3)\n    \n    def forward(self, x, skip_layer):\n        x = self.transpose_conv(x)\n        x = torch.cat([x, skip_layer], axis=1)\n        \n        x = self.conv1(x)\n        x = self.bn1(x)\n        x = self.relu(x)\n        \n        x = self.dropout(x)\n        \n        x = self.conv2(x)\n        x = self.bn2(x)\n        x = self.relu(x)\n        \n        x = self.conv3(x)\n        x = self.bn3(x)\n        x = self.relu(x)\n        \n        return x","metadata":{"execution":{"iopub.execute_input":"2023-11-17T04:57:04.624810Z","iopub.status.busy":"2023-11-17T04:57:04.624528Z","iopub.status.idle":"2023-11-17T04:57:04.635978Z","shell.execute_reply":"2023-11-17T04:57:04.635138Z","shell.execute_reply.started":"2023-11-17T04:57:04.624788Z"},"papermill":{"duration":0.024252,"end_time":"2023-11-13T07:12:28.421900","exception":false,"start_time":"2023-11-13T07:12:28.397648","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Bottle neck**","metadata":{"papermill":{"duration":0.013422,"end_time":"2023-11-13T07:12:28.448754","exception":false,"start_time":"2023-11-13T07:12:28.435332","status":"completed"},"tags":[]}},{"cell_type":"code","source":"class bottleneck_block(nn.Module):\n    def __init__(self, in_channels, out_channels):\n        super(bottleneck_block, self).__init__()\n        \n        self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding='same')\n        self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding='same')\n        self.conv3 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding='same')\n        \n        self.bn1 = nn.BatchNorm2d(out_channels)\n        self.bn2 = nn.BatchNorm2d(out_channels)\n        self.bn3 = nn.BatchNorm2d(out_channels)\n        \n        self.relu = nn.ReLU()\n        self.dropout = nn.Dropout(p=0.3)\n        \n    def forward(self, x):\n        x = self.conv1(x)\n        x = self.bn1(x)\n        x = self.relu(x)\n        \n        x = self.dropout(x)\n        \n        x = self.conv2(x)\n        x = self.bn2(x)\n        x = self.relu(x)\n        \n        x = self.conv3(x)\n        x = self.bn3(x)\n        x = self.relu(x)\n        return x","metadata":{"execution":{"iopub.execute_input":"2023-11-17T04:57:04.637194Z","iopub.status.busy":"2023-11-17T04:57:04.636940Z","iopub.status.idle":"2023-11-17T04:57:04.646426Z","shell.execute_reply":"2023-11-17T04:57:04.645556Z","shell.execute_reply.started":"2023-11-17T04:57:04.637172Z"},"papermill":{"duration":0.023287,"end_time":"2023-11-13T07:12:28.485664","exception":false,"start_time":"2023-11-13T07:12:28.462377","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Unet model**","metadata":{"papermill":{"duration":0.013331,"end_time":"2023-11-13T07:12:28.512584","exception":false,"start_time":"2023-11-13T07:12:28.499253","status":"completed"},"tags":[]}},{"cell_type":"code","source":"## UNet model\nclass UNet(nn.Module):\n    def __init__(self, n_class=3):\n        super(UNet, self).__init__()\n        # Encoder blocks\n        self.enc1 = encoder_block(3, 64)\n        self.enc2 = encoder_block(64, 128)\n        self.enc3 = encoder_block(128, 256)\n        self.enc4 = encoder_block(256, 512)\n    \n        # Bottleneck block\n        self.bottleneck = bottleneck_block(512, 1024)\n        \n        # Decoder blocks\n        self.dec1 = decoder_block(1024, 512)\n        self.dec2 = decoder_block(512, 256)\n        self.dec3 = decoder_block(256, 128)\n        self.dec4 = decoder_block(128, 64)\n        \n        # 1x1 convolution\n        self.out = nn.Conv2d(64, n_class, kernel_size=1, padding='same')\n        \n    def forward(self, image):\n        n1, s1 = self.enc1(image)\n        n2, s2 = self.enc2(n1)\n        n3, s3 = self.enc3(n2)\n        n4, s4 = self.enc4(n3)\n        \n        n5 = self.bottleneck(n4)\n        \n        n6 = self.dec1(n5, s4)\n        n7 = self.dec2(n6, s3)\n        n8 = self.dec3(n7, s2)\n        n9 = self.dec4(n8, s1)\n        \n        \n        output = self.out(n9)\n        \n        return output\n\n","metadata":{"execution":{"iopub.execute_input":"2023-11-17T04:57:04.647715Z","iopub.status.busy":"2023-11-17T04:57:04.647458Z","iopub.status.idle":"2023-11-17T04:57:04.659448Z","shell.execute_reply":"2023-11-17T04:57:04.658736Z","shell.execute_reply.started":"2023-11-17T04:57:04.647690Z"},"papermill":{"duration":0.026209,"end_time":"2023-11-13T07:12:28.552346","exception":false,"start_time":"2023-11-13T07:12:28.526137","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loss function","metadata":{"papermill":{"duration":0.013249,"end_time":"2023-11-13T07:12:28.579095","exception":false,"start_time":"2023-11-13T07:12:28.565846","status":"completed"},"tags":[]}},{"cell_type":"code","source":"class CEDiceLoss(nn.Module):\n    def __init__(self, weights) -> None:\n        super(CEDiceLoss, self).__init__()\n        self.eps: float = 1e-6\n        self.weights: torch.Tensor = weights\n\n    def forward(\n            self,\n            input: torch.Tensor,\n            target: torch.Tensor) -> torch.Tensor:\n        if not torch.is_tensor(input):\n            raise TypeError(\"Input type is not a torch.Tensor. Got {}\"\n                            .format(type(input)))\n        if not len(input.shape) == 4:\n            raise ValueError(\"Invalid input shape, we expect BxNxHxW. Got: {}\"\n                             .format(input.shape))\n        if not input.shape[-2:] == target.shape[-2:]:\n            raise ValueError(\"input and target shapes must be the same. Got: {}\"\n                             .format(input.shape, input.shape))\n        if not input.device == target.device:\n            raise ValueError(\n                \"input and target must be in the same device. Got: {}\" .format(\n                    input.device, target.device))\n        if not self.weights.shape[1] == input.shape[1]:\n            raise ValueError(\"The number of weights must equal the number of classes\")\n        if not torch.sum(self.weights).item() == 1:\n            raise ValueError(\"The sum of all weights must equal 1\")\n            \n        # cross entropy loss\n        celoss = nn.CrossEntropyLoss(self.weights)(input, target)\n        \n        # compute softmax over the classes axis\n        input_soft = F.softmax(input, dim=1)\n\n        # create the labels one hot tensor\n        target_one_hot = one_hot(target, num_classes=input.shape[1],\n                                 device=input.device, dtype=input.dtype)\n\n        # compute the actual dice score\n        dims = (2, 3)\n        intersection = torch.sum(input_soft * target_one_hot, dims)\n        cardinality = torch.sum(input_soft + target_one_hot, dims)\n\n        dice_score = 2. * intersection / (cardinality + self.eps)\n        \n        dice_score = torch.sum(dice_score * self.weights, dim=1)\n        \n        return torch.mean(1. - dice_score) + celoss","metadata":{"execution":{"iopub.execute_input":"2023-11-17T04:57:04.661011Z","iopub.status.busy":"2023-11-17T04:57:04.660690Z","iopub.status.idle":"2023-11-17T04:57:04.672755Z","shell.execute_reply":"2023-11-17T04:57:04.671891Z","shell.execute_reply.started":"2023-11-17T04:57:04.660983Z"},"papermill":{"duration":0.027653,"end_time":"2023-11-13T07:12:28.620311","exception":false,"start_time":"2023-11-13T07:12:28.592658","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training","metadata":{"papermill":{"duration":0.013344,"end_time":"2023-11-13T07:12:28.647307","exception":false,"start_time":"2023-11-13T07:12:28.633963","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"**Initialize weights**","metadata":{"papermill":{"duration":0.014389,"end_time":"2023-11-13T07:12:28.675129","exception":false,"start_time":"2023-11-13T07:12:28.660740","status":"completed"},"tags":[]}},{"cell_type":"code","source":"def weights_init(model):\n    if isinstance(model, nn.Linear):\n        # Xavier Distribution\n        torch.nn.init.xavier_uniform_(model.weight)","metadata":{"execution":{"iopub.execute_input":"2023-11-17T04:57:04.674032Z","iopub.status.busy":"2023-11-17T04:57:04.673754Z","iopub.status.idle":"2023-11-17T04:57:04.684705Z","shell.execute_reply":"2023-11-17T04:57:04.684017Z","shell.execute_reply.started":"2023-11-17T04:57:04.674010Z"},"papermill":{"duration":0.020816,"end_time":"2023-11-13T07:12:28.709398","exception":false,"start_time":"2023-11-13T07:12:28.688582","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def save_model(model, optimizer, path):\n    checkpoint = {\n        \"model\": model.state_dict(),\n        \"optimizer\": optimizer.state_dict(),\n    }\n    torch.save(checkpoint, path)\n\ndef load_model(model, optimizer, path):\n    checkpoint = torch.load(path)\n    model.load_state_dict(checkpoint[\"model\"])\n    optimizer.load_state_dict(checkpoint['optimizer'])\n    return model, optimizer","metadata":{"execution":{"iopub.execute_input":"2023-11-17T04:57:04.686039Z","iopub.status.busy":"2023-11-17T04:57:04.685757Z","iopub.status.idle":"2023-11-17T04:57:04.694463Z","shell.execute_reply":"2023-11-17T04:57:04.693797Z","shell.execute_reply.started":"2023-11-17T04:57:04.686017Z"},"papermill":{"duration":0.021664,"end_time":"2023-11-13T07:12:28.744590","exception":false,"start_time":"2023-11-13T07:12:28.722926","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Train model**","metadata":{"papermill":{"duration":0.01333,"end_time":"2023-11-13T07:12:28.771998","exception":false,"start_time":"2023-11-13T07:12:28.758668","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# Train function for each epoch\ndef train(train_dataloader, valid_dataloader,learing_rate_scheduler, epoch, display_step):\n    print(f\"Start epoch #{epoch+1}, learning rate for this epoch: {learing_rate_scheduler.get_last_lr()}\")\n    start_time = time.time()\n    train_loss_epoch = 0\n    test_loss_epoch = 0\n    last_loss = 999999999\n    model.train()\n    for i, (data,targets) in enumerate(train_dataloader):\n        \n        # Load data into GPU\n        data, targets = data.to(device), targets.to(device)\n\n        optimizer.zero_grad()\n        outputs = model(data)\n\n        # Backpropagation, compute gradients\n        loss = loss_function(outputs, targets.long())\n        loss.backward()\n\n        # Apply gradients\n        optimizer.step()\n        \n        # Save loss\n        train_loss_epoch += loss.item()\n        if (i+1) % display_step == 0:\n#             accuracy = float(test(test_loader))\n            print('Train Epoch: {} [{}/{} ({}%)]\\tLoss: {:.4f}'.format(\n                epoch + 1, (i+1) * len(data), len(train_dataloader.dataset), 100 * (i+1) * len(data) / len(train_dataloader.dataset), \n                loss.item()))\n            print(f\"Done step #{len(data)*(i+1)}, time for this displayable step: {time.time()-start_time}s\")\n                  \n    print(f\"Done epoch #{epoch+1}, time for this epoch: {time.time()-start_time}s\")\n    train_loss_epoch/= (i + 1)\n    \n    # Evaluate the validation set\n    model.eval()\n    with torch.no_grad():\n        for data, target in valid_dataloader:\n            data, target = data.to(device), target.to(device)\n            test_output = model(data)\n            test_loss = loss_function(test_output, target.long())\n            test_loss_epoch += test_loss.item()\n            \n    test_loss_epoch/= (i+1)\n    \n    return train_loss_epoch , test_loss_epoch","metadata":{"execution":{"iopub.execute_input":"2023-11-17T04:57:04.695691Z","iopub.status.busy":"2023-11-17T04:57:04.695435Z","iopub.status.idle":"2023-11-17T04:57:04.706045Z","shell.execute_reply":"2023-11-17T04:57:04.705156Z","shell.execute_reply.started":"2023-11-17T04:57:04.695666Z"},"papermill":{"duration":0.025915,"end_time":"2023-11-13T07:12:28.811424","exception":false,"start_time":"2023-11-13T07:12:28.785509","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Test function\ndef test(dataloader):\n    test_loss = 0\n    correct = 0\n    with torch.no_grad():\n        for i, (data, targets) in enumerate(dataloader):\n            data, targets = data.to(device), targets.to(device)\n            outputs = model(data)\n            _, pred = torch.max(outputs, 1)\n            test_loss += targets.size(0)\n            correct += torch.sum(pred == targets).item()\n    return 100.0 * correct / test_loss","metadata":{"execution":{"iopub.execute_input":"2023-11-17T04:57:04.707341Z","iopub.status.busy":"2023-11-17T04:57:04.707111Z","iopub.status.idle":"2023-11-17T04:57:04.718195Z","shell.execute_reply":"2023-11-17T04:57:04.717273Z","shell.execute_reply.started":"2023-11-17T04:57:04.707321Z"},"papermill":{"duration":0.021642,"end_time":"2023-11-13T07:12:28.846750","exception":false,"start_time":"2023-11-13T07:12:28.825108","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = UNet()\nmodel.apply(weights_init)\n# model = nn.DataParallel(model)\n# checkpoint = torch.load(pretrained_path)\n\n# new_state_dict = OrderedDict()\n# for k, v in checkpoint['model'].items():\n#     name = k[7:] # remove `module.`\n#     new_state_dict[name] = v\n# # load params\n# model.load_state_dict(new_state_dict)\n# model = nn.DataParallel(model)\nmodel.to(device)","metadata":{"execution":{"iopub.execute_input":"2023-11-17T04:57:04.721491Z","iopub.status.busy":"2023-11-17T04:57:04.721201Z","iopub.status.idle":"2023-11-17T04:57:07.957029Z","shell.execute_reply":"2023-11-17T04:57:07.956086Z","shell.execute_reply.started":"2023-11-17T04:57:04.721468Z"},"papermill":{"duration":4.022563,"end_time":"2023-11-13T07:12:32.882726","exception":false,"start_time":"2023-11-13T07:12:28.860163","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.014071,"end_time":"2023-11-13T07:12:32.911241","exception":false,"start_time":"2023-11-13T07:12:32.897170","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weights = torch.Tensor([[0.05,0.55,0.4]]).cuda()\nloss_function = CEDiceLoss(weights)\n\n# Define the optimizer (Adam optimizer)\noptimizer = optim.Adam(params=model.parameters(), lr=learning_rate)\n# optimizer.load_state_dict(checkpoint['optimizer'])\n\n# Learning rate scheduler\nlearing_rate_scheduler = lr_scheduler.StepLR(optimizer, step_size=4, gamma=0.6)","metadata":{"execution":{"iopub.execute_input":"2023-11-17T04:57:07.958290Z","iopub.status.busy":"2023-11-17T04:57:07.958030Z","iopub.status.idle":"2023-11-17T04:57:07.964735Z","shell.execute_reply":"2023-11-17T04:57:07.963663Z","shell.execute_reply.started":"2023-11-17T04:57:07.958268Z"},"papermill":{"duration":0.023208,"end_time":"2023-11-13T07:12:32.948265","exception":false,"start_time":"2023-11-13T07:12:32.925057","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"save_model(model, optimizer, checkpoint_path)","metadata":{"execution":{"iopub.execute_input":"2023-11-17T04:57:07.966321Z","iopub.status.busy":"2023-11-17T04:57:07.966036Z","iopub.status.idle":"2023-11-17T04:57:08.176481Z","shell.execute_reply":"2023-11-17T04:57:08.175488Z","shell.execute_reply.started":"2023-11-17T04:57:07.966297Z"},"papermill":{"duration":0.92582,"end_time":"2023-11-13T07:12:33.887741","exception":false,"start_time":"2023-11-13T07:12:32.961921","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wandb.login(\n    # set the wandb project where this run will be logged\n#     project= \"PolypSegment\", \n    key = \"1ff5940e3cf4364120a870b0c3c0e2f91facd329\",\n)\nwandb.init(\n    project = \"PolypSegment\"\n)\n# Training loop\ntrain_loss_array = []\ntest_loss_array = []\nlast_loss = 9999999999999\nfor epoch in range(epochs):\n    train_loss_epoch = 0\n    test_loss_epoch = 0\n    (train_loss_epoch, test_loss_epoch) = train(train_dataloader, \n                                              valid_dataloader, \n                                              learing_rate_scheduler, epoch, display_step)\n    \n    if test_loss_epoch < last_loss:\n        save_model(model, optimizer, checkpoint_path)\n        last_loss = test_loss_epoch\n        \n    learing_rate_scheduler.step()\n    train_loss_array.append(train_loss_epoch)\n    test_loss_array.append(test_loss_epoch)\n    wandb.log({\"Train loss\": train_loss_epoch, \"Valid loss\": test_loss_epoch})\n#     train_accuracy.append(test(train_loader))\n#     valid_accuracy.append(test(test_loader))\n#     print(\"Epoch {}: loss: {:.4f}, train accuracy: {:.4f}, valid accuracy:{:.4f}\".format(epoch + 1, \n#                                         train_loss_array[-1], train_accuracy[-1], valid_accuracy[-1]))","metadata":{"execution":{"iopub.execute_input":"2023-11-17T04:57:08.178054Z","iopub.status.busy":"2023-11-17T04:57:08.177755Z","iopub.status.idle":"2023-11-17T05:41:30.309103Z","shell.execute_reply":"2023-11-17T05:41:30.307894Z","shell.execute_reply.started":"2023-11-17T04:57:08.178030Z"},"papermill":{"duration":7726.212208,"end_time":"2023-11-13T09:21:20.114357","exception":false,"start_time":"2023-11-13T07:12:33.902149","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.cuda.empty_cache()","metadata":{"execution":{"iopub.execute_input":"2023-11-17T05:41:30.312215Z","iopub.status.busy":"2023-11-17T05:41:30.311450Z","iopub.status.idle":"2023-11-17T05:41:30.510008Z","shell.execute_reply":"2023-11-17T05:41:30.508930Z","shell.execute_reply.started":"2023-11-17T05:41:30.312185Z"},"papermill":{"duration":0.03199,"end_time":"2023-11-13T09:21:20.171938","exception":false,"start_time":"2023-11-13T09:21:20.139948","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.024586,"end_time":"2023-11-13T09:21:20.221336","exception":false,"start_time":"2023-11-13T09:21:20.196750","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plot the learning cure","metadata":{"papermill":{"duration":0.024534,"end_time":"2023-11-13T09:21:20.270588","exception":false,"start_time":"2023-11-13T09:21:20.246054","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# load_model(model, checkpoint)","metadata":{"execution":{"iopub.execute_input":"2023-11-17T05:41:30.511525Z","iopub.status.busy":"2023-11-17T05:41:30.511243Z","iopub.status.idle":"2023-11-17T05:41:30.533354Z","shell.execute_reply":"2023-11-17T05:41:30.532433Z","shell.execute_reply.started":"2023-11-17T05:41:30.511501Z"},"papermill":{"duration":0.031504,"end_time":"2023-11-13T09:21:20.326709","exception":false,"start_time":"2023-11-13T09:21:20.295205","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.rcParams['figure.dpi'] = 90\nplt.rcParams['figure.figsize'] = (6, 4)\nepochs_array = range(epochs)","metadata":{"execution":{"iopub.execute_input":"2023-11-17T05:41:30.540037Z","iopub.status.busy":"2023-11-17T05:41:30.539297Z","iopub.status.idle":"2023-11-17T05:41:30.545602Z","shell.execute_reply":"2023-11-17T05:41:30.544543Z","shell.execute_reply.started":"2023-11-17T05:41:30.540011Z"},"papermill":{"duration":0.031761,"end_time":"2023-11-13T09:21:20.383049","exception":false,"start_time":"2023-11-13T09:21:20.351288","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot Training and Test loss\nplt.plot(epochs_array, train_loss_array, 'g', label='Training loss')\nplt.plot(epochs_array, test_loss_array, 'b', label='Test loss')\nplt.title('Training and Test loss')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2023-11-17T05:41:30.547181Z","iopub.status.busy":"2023-11-17T05:41:30.546868Z","iopub.status.idle":"2023-11-17T05:41:30.863642Z","shell.execute_reply":"2023-11-17T05:41:30.862613Z","shell.execute_reply.started":"2023-11-17T05:41:30.547157Z"},"papermill":{"duration":0.324591,"end_time":"2023-11-13T09:21:20.732601","exception":false,"start_time":"2023-11-13T09:21:20.408010","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Infer**","metadata":{"papermill":{"duration":0.025171,"end_time":"2023-11-13T09:21:20.783810","exception":false,"start_time":"2023-11-13T09:21:20.758639","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# from torch.jit import load\n# model = UNet()\n# optimizer = optim.Adam(params=model.parameters(), lr=learning_rate)\n\n# checkpoint = torch.load(pretrained_path)","metadata":{"execution":{"iopub.execute_input":"2023-11-17T05:41:30.867315Z","iopub.status.busy":"2023-11-17T05:41:30.867036Z","iopub.status.idle":"2023-11-17T05:41:30.872959Z","shell.execute_reply":"2023-11-17T05:41:30.871907Z","shell.execute_reply.started":"2023-11-17T05:41:30.867291Z"},"papermill":{"duration":0.032171,"end_time":"2023-11-13T09:21:20.841338","exception":false,"start_time":"2023-11-13T09:21:20.809167","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# optimizer.load_state_dict(checkpoint['optimizer'])","metadata":{"execution":{"iopub.execute_input":"2023-11-17T05:41:30.874589Z","iopub.status.busy":"2023-11-17T05:41:30.874301Z","iopub.status.idle":"2023-11-17T05:41:30.883059Z","shell.execute_reply":"2023-11-17T05:41:30.882010Z","shell.execute_reply.started":"2023-11-17T05:41:30.874560Z"},"papermill":{"duration":0.031681,"end_time":"2023-11-13T09:21:20.898418","exception":false,"start_time":"2023-11-13T09:21:20.866737","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from collections import OrderedDict\n# new_state_dict = OrderedDict()\n# for k, v in checkpoint['model'].items():\n#     name = k[7:] # remove `module.`\n#     new_state_dict[name] = v\n# # load params\n# model.load_state_dict(new_state_dict)","metadata":{"execution":{"iopub.execute_input":"2023-11-17T05:41:30.884349Z","iopub.status.busy":"2023-11-17T05:41:30.884005Z","iopub.status.idle":"2023-11-17T05:41:30.893454Z","shell.execute_reply":"2023-11-17T05:41:30.892402Z","shell.execute_reply.started":"2023-11-17T05:41:30.884315Z"},"papermill":{"duration":0.032386,"end_time":"2023-11-13T09:21:20.956384","exception":false,"start_time":"2023-11-13T09:21:20.923998","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.025216,"end_time":"2023-11-13T09:21:21.007264","exception":false,"start_time":"2023-11-13T09:21:20.982048","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Visualize results**","metadata":{"papermill":{"duration":0.025228,"end_time":"2023-11-13T09:21:21.058135","exception":false,"start_time":"2023-11-13T09:21:21.032907","status":"completed"},"tags":[]}},{"cell_type":"code","source":"for i, (data, label) in enumerate(train_dataloader):\n    img = data\n    mask = label\n    break","metadata":{"execution":{"iopub.execute_input":"2023-11-17T05:41:30.895774Z","iopub.status.busy":"2023-11-17T05:41:30.894890Z","iopub.status.idle":"2023-11-17T05:41:31.161986Z","shell.execute_reply":"2023-11-17T05:41:31.160938Z","shell.execute_reply.started":"2023-11-17T05:41:30.895738Z"},"papermill":{"duration":0.156265,"end_time":"2023-11-13T09:21:21.239924","exception":false,"start_time":"2023-11-13T09:21:21.083659","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fig, arr = plt.subplots(4, 3, figsize=(16, 12))\n# arr[0][0].set_title('Image')\n# arr[0][1].set_title('Segmentation')\n# arr[0][2].set_title('Predict')\n\n# model.eval()\n# with torch.no_grad():\n#     z = img.to(device)\n#     predict = model(z)\n\n# def display_image_grid(images_filenames, images_directory, masks_directory, predicted_masks=None):\n#     cols = 3 if predicted_masks else 2\n#     rows = len(images_filenames)\n#     figure, ax = plt.subplots(nrows=rows, ncols=cols, figsize=(10, 24))\n#     for i, image_filename in enumerate(images_filenames):\n#         image = cv2.imread(os.path.join(images_directory, image_filename))\n#         image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n\n#         mask = cv2.imread(os.path.join(masks_directory, image_filename.replace(\".jpeg\", \".png\")), cv2.IMREAD_UNCHANGED,)\n#         mask = preprocess_mask(mask)\n#         ax[i, 0].imshow(image)\n#         ax[i, 1].imshow(mask, interpolation=\"nearest\")\n\n#         ax[i, 0].set_title(\"Image\")\n#         ax[i, 1].set_title(\"Ground truth mask\")\n\n#         ax[i, 0].set_axis_off()\n#         ax[i, 1].set_axis_off()\n\n#         if predicted_masks:\n#             predicted_mask = predicted_masks[i]\n#             ax[i, 2].imshow(predicted_mask, interpolation=\"nearest\")\n#             ax[i, 2].set_title(\"Predicted mask\")\n#             ax[i, 2].set_axis_off()\n#     plt.tight_layout()\n#     plt.show()\n\n# display_image_grid(, images_directory, masks_directory,predicted_masks = predict)    \n# # for i in range(2):\n# #     arr[i][0].imshow(img[i].permute(1, 2, 0));\n    \n# #     arr[i][1].imshow(F.one_hot(mask[i]).float())\n    \n# #     arr[i][2].imshow(F.one_hot(torch.argmax(predict[i], 0).cpu()).float())","metadata":{"execution":{"iopub.execute_input":"2023-11-17T05:51:40.967253Z","iopub.status.busy":"2023-11-17T05:51:40.966396Z","iopub.status.idle":"2023-11-17T05:51:43.119001Z","shell.execute_reply":"2023-11-17T05:51:43.116925Z","shell.execute_reply.started":"2023-11-17T05:51:40.967218Z"},"papermill":{"duration":3.94406,"end_time":"2023-11-13T09:21:25.210255","exception":true,"start_time":"2023-11-13T09:21:21.266195","status":"failed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Create submission**","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]}},{"cell_type":"code","source":"transform = A.Compose([A.Resize(384, 384),\n                       A.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),\n                     ToTensorV2(),])","metadata":{"execution":{"iopub.execute_input":"2023-11-17T05:42:52.995531Z","iopub.status.busy":"2023-11-17T05:42:52.994612Z","iopub.status.idle":"2023-11-17T05:42:53.002584Z","shell.execute_reply":"2023-11-17T05:42:53.001680Z","shell.execute_reply.started":"2023-11-17T05:42:52.995484Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class UNetTestDataClass(Dataset):\n    def __init__(self, images_path, transform):\n        super(UNetTestDataClass, self).__init__()\n        \n        images_list = os.listdir(images_path)\n        images_list = [images_path+i for i in images_list]\n        \n        self.images_list = images_list\n        self.transform = transform\n        \n    def __getitem__(self, index):\n        img_path = self.images_list[index]\n        data = Image.open(img_path)\n        h = data.size[1]\n        w = data.size[0]\n        data = self.transform(image = np.array(data)) \n        data = data['image']\n        return data, img_path, h, w\n    \n    def __len__(self):\n        return len(self.images_list)","metadata":{"execution":{"iopub.execute_input":"2023-11-17T05:42:54.431102Z","iopub.status.busy":"2023-11-17T05:42:54.430406Z","iopub.status.idle":"2023-11-17T05:42:54.440145Z","shell.execute_reply":"2023-11-17T05:42:54.439124Z","shell.execute_reply.started":"2023-11-17T05:42:54.431047Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '/kaggle/input/bkai-igh-neopolyp/test/test/'\nunet_test_dataset = UNetTestDataClass(path, transform)\ntest_dataloader = DataLoader(unet_test_dataset, batch_size=8, shuffle=True)","metadata":{"execution":{"iopub.execute_input":"2023-11-17T05:42:56.485020Z","iopub.status.busy":"2023-11-17T05:42:56.484430Z","iopub.status.idle":"2023-11-17T05:42:56.539273Z","shell.execute_reply":"2023-11-17T05:42:56.538318Z","shell.execute_reply.started":"2023-11-17T05:42:56.484986Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, (data, path, h, w) in enumerate(test_dataloader):\n    img = data\n    break","metadata":{"execution":{"iopub.execute_input":"2023-11-17T05:42:57.645460Z","iopub.status.busy":"2023-11-17T05:42:57.644500Z","iopub.status.idle":"2023-11-17T05:42:57.829043Z","shell.execute_reply":"2023-11-17T05:42:57.824997Z","shell.execute_reply.started":"2023-11-17T05:42:57.645424Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fig, arr = plt.subplots(5, 2, figsize=(16, 12))\n# arr[0][0].set_title('Image');\n# arr[0][1].set_title('Predict');\n\n# model.eval()\n# with torch.no_grad():\n#     z = img.to(device)\n#     predict = model(z)\n\n# for i in range(5):\n#     arr[i][0].imshow(img[i].permute(1, 2, 0));\n#     arr[i][1].imshow(F.one_hot(torch.argmax(predict[i], 0).cpu()).float())","metadata":{"execution":{"iopub.execute_input":"2023-11-17T05:42:58.724637Z","iopub.status.busy":"2023-11-17T05:42:58.724239Z","iopub.status.idle":"2023-11-17T05:43:01.309411Z","shell.execute_reply":"2023-11-17T05:43:01.307876Z","shell.execute_reply.started":"2023-11-17T05:42:58.724594Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.eval()\nif not os.path.isdir(\"/kaggle/working/predicted_masks\"):\n    os.mkdir(\"/kaggle/working/predicted_masks\")\nfor _, (img, path, H, W) in enumerate(test_dataloader):\n    a = path\n    b = img\n    h = H\n    w = W\n    \n    with torch.no_grad():\n        z = b.to(device)\n        predicted_mask = model(z)\n    for i in range(len(a)):\n        image_id = a[i].split('/')[-1].split('.')[0]\n        filename = image_id + \".png\"\n        mask2img = Resize((h[i].item(), w[i].item()), interpolation=InterpolationMode.NEAREST)(ToPILImage()(F.one_hot(torch.argmax(predicted_mask[i], 0)).permute(2, 0, 1).float()))\n        mask2img.save(os.path.join(\"/kaggle/working/predicted_masks/\", filename))","metadata":{"execution":{"iopub.execute_input":"2023-11-17T05:43:19.414465Z","iopub.status.busy":"2023-11-17T05:43:19.414083Z","iopub.status.idle":"2023-11-17T05:43:35.587505Z","shell.execute_reply":"2023-11-17T05:43:35.586459Z","shell.execute_reply.started":"2023-11-17T05:43:19.414433Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_to_string(runs):\n    return ' '.join(str(x) for x in runs)\n\ndef rle_encode_one_mask(mask):\n    pixels = mask.flatten()\n    pixels[pixels > 0] = 255\n    use_padding = False\n    if pixels[0] or pixels[-1]:\n        use_padding = True\n        pixel_padded = np.zeros([len(pixels) + 2], dtype=pixels.dtype)\n        pixel_padded[1:-1] = pixels\n        pixels = pixel_padded\n    \n    rle = np.where(pixels[1:] != pixels[:-1])[0] + 2\n    if use_padding:\n        rle = rle - 1\n    rle[1::2] = rle[1::2] - rle[:-1:2]\n    return rle_to_string(rle)\n\ndef mask2string(dir):\n    ## mask --> string\n    strings = []\n    ids = []\n    ws, hs = [[] for i in range(2)]\n    for image_id in os.listdir(dir):\n        id = image_id.split('.')[0]\n        path = os.path.join(dir, image_id)\n        print(path)\n        img = cv2.imread(path)[:,:,::-1]\n        h, w = img.shape[0], img.shape[1]\n        for channel in range(2):\n            ws.append(w)\n            hs.append(h)\n            ids.append(f'{id}_{channel}')\n            string = rle_encode_one_mask(img[:,:,channel])\n            strings.append(string)\n    r = {\n        'ids': ids,\n        'strings': strings,\n    }\n    return r\n\n\nMASK_DIR_PATH = '/kaggle/working/predicted_masks' # change this to the path to your output mask folder\ndir = MASK_DIR_PATH\nres = mask2string(dir)\ndf = pd.DataFrame(columns=['Id', 'Expected'])\ndf['Id'] = res['ids']\ndf['Expected'] = res['strings']\ndf.to_csv(r'output.csv', index=False)","metadata":{"execution":{"iopub.execute_input":"2023-11-17T05:43:35.589280Z","iopub.status.busy":"2023-11-17T05:43:35.588992Z","iopub.status.idle":"2023-11-17T05:43:38.645727Z","shell.execute_reply":"2023-11-17T05:43:38.643466Z","shell.execute_reply.started":"2023-11-17T05:43:35.589256Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]}]}