{"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":"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install --user  /kaggle/input/d/saworz/pycocotools-206/wheels/pycocotools-2.0.6-cp310-cp310-linux_x86_64.whl\nimport pycocotools","metadata":{"execution":{"iopub.status.busy":"2023-07-11T02:42:36.178119Z","iopub.execute_input":"2023-07-11T02:42:36.178440Z","iopub.status.idle":"2023-07-11T02:43:07.295790Z","shell.execute_reply.started":"2023-07-11T02:42:36.178407Z","shell.execute_reply":"2023-07-11T02:43:07.294581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folder = '/kaggle/input/hubmap-hacking-the-human-vasculature/'","metadata":{"execution":{"iopub.status.busy":"2023-07-11T02:46:05.552533Z","iopub.execute_input":"2023-07-11T02:46:05.552938Z","iopub.status.idle":"2023-07-11T02:46:05.557852Z","shell.execute_reply.started":"2023-07-11T02:46:05.552908Z","shell.execute_reply":"2023-07-11T02:46:05.556927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nfrom torch.utils.data import Dataset\nimport json\nimport cv2\nimport os\nimport numpy as np \nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-07-11T02:46:06.289429Z","iopub.execute_input":"2023-07-11T02:46:06.290116Z","iopub.status.idle":"2023-07-11T02:46:06.320479Z","shell.execute_reply.started":"2023-07-11T02:46:06.290083Z","shell.execute_reply":"2023-07-11T02:46:06.319575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torchvision.transforms as transforms\ntransform = transforms.Compose([\n    transforms.RandomHorizontalFlip(),\n])","metadata":{"execution":{"iopub.status.busy":"2023-07-11T02:46:06.791235Z","iopub.execute_input":"2023-07-11T02:46:06.791638Z","iopub.status.idle":"2023-07-11T02:46:06.911666Z","shell.execute_reply.started":"2023-07-11T02:46:06.791606Z","shell.execute_reply":"2023-07-11T02:46:06.910685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ImageHuBMAPDataset(Dataset):\n    def __init__(self, image_dir, labels_file, transform=None):\n        self.image_dir = image_dir\n        self.transform = transform\n        with open(labels_file, 'r') as json_file:\n            self.json_labels = [json.loads(line) for line in json_file]\n\n    def __len__(self):\n        return len(self.json_labels)\n\n    def __getitem__(self, idx):\n        image_path = os.path.join(self.image_dir, f\"{self.json_labels[idx]['id']}.tif\")\n        image =  cv2.imread(image_path)\n        image = np.transpose(np.array(image).astype(np.float32), axes=[2, 0, 1]) / 255.0\n        \n        mask0 = np.ones((512, 512), dtype=np.int32) # Background\n        mask1 = np.zeros((512, 512), dtype=np.int32) # WSI\n        mask = np.stack([mask0, mask1])\n        for annot in self.json_labels[idx]['annotations']:\n            cords = annot['coordinates']\n            if annot['type'] == \"blood_vessel\":\n                for cd in cords:\n                    rr, cc = np.array([i[1] for i in cd]), np.asarray([i[0] for i in cd])\n                    mask[1, rr, cc] = 1\n                    mask[0, rr, cc] = 0\n                # For each polygon\n            lines = np.array(cords)\n            lines = lines.reshape(-1,1,2)\n#             cv2.fillPoly(mask[1],[lines],1)\n#             cv2.fillPoly(mask[0],[lines],0)\n\n\n        mask = np.array(mask).astype(np.float32)\n        image = torch.tensor(image)\n        mask = torch.tensor(mask)\n        if self.transform:\n            both = torch.cat([image, mask], dim=0)\n            both = self.transform(both)\n            (image, mask) = torch.split(both,3,0)\n        \n        return image, mask[0][None,:]# (3, 512, 512) (2, 512, 512)","metadata":{"execution":{"iopub.status.busy":"2023-07-11T02:46:07.994967Z","iopub.execute_input":"2023-07-11T02:46:07.995323Z","iopub.status.idle":"2023-07-11T02:46:08.008043Z","shell.execute_reply.started":"2023-07-11T02:46:07.995295Z","shell.execute_reply":"2023-07-11T02:46:08.007065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = ImageHuBMAPDataset(folder + 'train', folder + 'polygons.jsonl',transform=transform)\n# Calculate the number of samples for train and validation\ndataset_size = len(dataset)\nval_size = int(0.01 * dataset_size)\ntrain_size = dataset_size - val_size\n# Split the dataset into train and validation sets\ntrain_dataset, val_dataset = torch.utils.data.random_split(dataset, [train_size, val_size])\nbatch_size = 1\ntrain_dataloader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True)\nval_dataloader = torch.utils.data.DataLoader(val_dataset, batch_size=batch_size, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2023-07-11T02:46:09.072037Z","iopub.execute_input":"2023-07-11T02:46:09.073209Z","iopub.status.idle":"2023-07-11T02:46:14.704495Z","shell.execute_reply.started":"2023-07-11T02:46:09.073145Z","shell.execute_reply":"2023-07-11T02:46:14.703491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.imshow(dataset[0][0].permute(1,2,0))\n# plt.imshow(dataset[0][1][0],alpha=0.5)","metadata":{"execution":{"iopub.status.busy":"2023-07-11T02:46:14.706909Z","iopub.execute_input":"2023-07-11T02:46:14.707299Z","iopub.status.idle":"2023-07-11T02:46:14.711173Z","shell.execute_reply.started":"2023-07-11T02:46:14.707264Z","shell.execute_reply":"2023-07-11T02:46:14.710266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dataset[0][1].shape","metadata":{"execution":{"iopub.status.busy":"2023-07-11T02:46:14.712574Z","iopub.execute_input":"2023-07-11T02:46:14.713142Z","iopub.status.idle":"2023-07-11T02:46:14.725358Z","shell.execute_reply.started":"2023-07-11T02:46:14.713106Z","shell.execute_reply":"2023-07-11T02:46:14.724454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DownBlock(nn.Module):\n    def __init__(self, in_channel, out_channel):\n        super(DownBlock,self).__init__()\n        self.conv1 = nn.Conv2d(in_channel, out_channel, kernel_size = (3,3), stride = 1, padding = 1)\n        self.normal1 = nn.BatchNorm2d(out_channel)\n        self.relu1 = nn.ReLU()\n        self.conv2 = nn.Conv2d(out_channel, out_channel, kernel_size = (3,3), stride = 1, padding = 1)\n        self.normal2 = nn.BatchNorm2d(out_channel)\n        self.relu2 = nn.ReLU()      \n        self.maxpool = nn.MaxPool2d(kernel_size = 2, stride = 2)\n    def forward(self,x):\n        x = self.relu1(self.normal1(self.conv1(x)))\n        return self.maxpool(self.relu2(self.normal2(self.conv2(x))))","metadata":{"execution":{"iopub.status.busy":"2023-07-11T02:46:14.729363Z","iopub.execute_input":"2023-07-11T02:46:14.729865Z","iopub.status.idle":"2023-07-11T02:46:14.738576Z","shell.execute_reply.started":"2023-07-11T02:46:14.729831Z","shell.execute_reply":"2023-07-11T02:46:14.737644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# x = torch.randn((1,3,512,512))\n# f = DownBlock(3,96)\n# f(x).shape","metadata":{"execution":{"iopub.status.busy":"2023-07-11T02:46:14.739744Z","iopub.execute_input":"2023-07-11T02:46:14.740298Z","iopub.status.idle":"2023-07-11T02:46:14.748492Z","shell.execute_reply.started":"2023-07-11T02:46:14.740267Z","shell.execute_reply":"2023-07-11T02:46:14.747586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"config = {\n    'batchsize': 1,\n    'device': torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\"),\n    'num_epochs': 5,\n}\nmobilenetv2 = None#torch.hub.load('pytorch/vision:v0.10.0', 'mobilenet_v2',pretrained=True).to(config['device'])\n\n# activation = {}\n# def get_activation(name):\n#     def hook(model, input, output):\n#         activation[name] = output.detach()\n#     return hook\n# x = torch.randn((2,3,512,512)).to(config['device'])\n    \n# for i in [2, 4, 7, 14]:\n#     mobilenetv2.features[i].conv[0].register_forward_hook(get_activation('l%d'%i))    \n# mobilenetv2.features[17].register_forward_hook(get_activation('l17'))    \n# out = mobilenetv2(x)\n# for i in [2, 4, 7, 14]:\n#     print(activation['l%d'%i].shape)\n# print(activation['l17'].shape)\n\nclass ResBlock(nn.Module):\n    def __init__(self, in_channel, filters):\n        super(ResBlock,self).__init__()\n        self.conv = nn.ConvTranspose2d(in_channel, filters, kernel_size=4, stride=2, padding=1)\n        self.normal = nn.BatchNorm2d(filters)\n        self.dropout = nn.Dropout(0.5)\n        self.relu6 = nn.ReLU()\n    def forward(self,x):\n        x = self.conv(x)\n        x = self.normal(x)\n        x = self.dropout(x)\n        x = self.relu6(x)\n        return x\nclass AttenBlock(nn.Module):\n    def __init__(self, embed_dim, num_heads):\n        super().__init__()\n        self.mha = nn.MultiheadAttention(embed_dim, num_heads,batch_first=True)\n    def forward(self,x):\n        xshape = x.shape\n        x = x.view([x.size(0), x.size(1),-1])\n        x = self.mha(x,x,x)[0]\n        return x.view(xshape)\n\nclass unet(nn.Module):\n    def __init__(self, output_channels: int, mobilenet = False):\n        super(unet,self).__init__()\n        self.up_stack = nn.ModuleList([ResBlock(320,512), ResBlock(1088,256), ResBlock(448,128), ResBlock(272,64)])\n        self.last = nn.ConvTranspose2d(160,output_channels,kernel_size=3, stride=2, padding=1, output_padding=1)\n        self.att1 = AttenBlock(256, 32)\n        self.att2 = AttenBlock(1024, 32)\n        self.mobilenet = mobilenet\n        if mobilenet == False:\n            self.downblock = nn.ModuleList([DownBlock(3,96), DownBlock(96,144), DownBlock(144,192),DownBlock(192,576),DownBlock(576,320)])\n    def forward(self, x):\n        if self.mobilenet == True:\n            with torch.no_grad():\n                _ = mobilenetv2(x)\n                skips = [activation['l%d'%i] for i in [2, 4, 7, 14, 17]]\n        else:\n            skips = []\n            for m in self.downblock:\n                x = m(x)\n                skips.append(x)\n        x = self.att1(skips[-1])\n        \n        skips = reversed(skips[:-1])\n        mm = 0\n        for up, skip in zip(self.up_stack, skips):\n            if mm == 0:\n                x = self.att2(up(x))\n            else:\n                x = up(x)\n            mm += 1\n            x = torch.concat([x, skip], dim=1)\n        return torch.sigmoid(self.last(x))\n    ","metadata":{"execution":{"iopub.status.busy":"2023-07-11T02:46:14.750071Z","iopub.execute_input":"2023-07-11T02:46:14.750415Z","iopub.status.idle":"2023-07-11T02:46:14.769557Z","shell.execute_reply.started":"2023-07-11T02:46:14.750386Z","shell.execute_reply":"2023-07-11T02:46:14.768487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# f = torch.nn.MultiheadAttention(embed_dim = 256, num_heads = 32,batch_first=True)","metadata":{"execution":{"iopub.status.busy":"2023-07-11T02:46:14.771115Z","iopub.execute_input":"2023-07-11T02:46:14.771465Z","iopub.status.idle":"2023-07-11T02:46:14.782466Z","shell.execute_reply.started":"2023-07-11T02:46:14.771434Z","shell.execute_reply":"2023-07-11T02:46:14.781579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# xx = torch.randn((2,320,16,16))\n# xx=xx.view([xx.size(0), xx.size(1),-1])\n# out, _ = f(xx,xx,xx)","metadata":{"execution":{"iopub.status.busy":"2023-07-11T02:46:14.784055Z","iopub.execute_input":"2023-07-11T02:46:14.784396Z","iopub.status.idle":"2023-07-11T02:46:14.792329Z","shell.execute_reply.started":"2023-07-11T02:46:14.784365Z","shell.execute_reply":"2023-07-11T02:46:14.791337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# x = torch.randn((1,3,512,512))","metadata":{"execution":{"iopub.status.busy":"2023-07-11T02:46:14.793586Z","iopub.execute_input":"2023-07-11T02:46:14.794167Z","iopub.status.idle":"2023-07-11T02:46:14.802016Z","shell.execute_reply.started":"2023-07-11T02:46:14.794135Z","shell.execute_reply":"2023-07-11T02:46:14.801121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model = unet(1).to(config['device'])","metadata":{"execution":{"iopub.status.busy":"2023-07-11T02:46:14.805317Z","iopub.execute_input":"2023-07-11T02:46:14.805682Z","iopub.status.idle":"2023-07-11T02:46:14.811473Z","shell.execute_reply.started":"2023-07-11T02:46:14.805658Z","shell.execute_reply":"2023-07-11T02:46:14.810560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from collections import defaultdict\nfrom collections import OrderedDict\nmodel = unet(1,mobilenet=False)\n\n# model= nn.DataParallel(model)\nmodel = model.to(config['device'])\noptimizer = torch.optim.Adam(model.parameters())\ncriterion = torch.nn.BCELoss()","metadata":{"execution":{"iopub.status.busy":"2023-07-11T02:46:14.812880Z","iopub.execute_input":"2023-07-11T02:46:14.814154Z","iopub.status.idle":"2023-07-11T02:46:15.020968Z","shell.execute_reply.started":"2023-07-11T02:46:14.814119Z","shell.execute_reply":"2023-07-11T02:46:15.019888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# losses = []\n# val_losses = []\n# from tqdm import tqdm\n# for epoch in range(1):#config['num_epochs']):\n#   for i, (image, label) in enumerate(tqdm(train_dataloader)):\n#     label_p = model(image.to(config['device']))\n#     loss = criterion(label_p,label.to(config['device']))\n#     losses.append(loss.item())\n#     optimizer.zero_grad()\n#     loss.backward()\n#     optimizer.step()\n#     del image, label_p, label\n#     del loss\n#     torch.cuda.empty_cache()\n#   for ii, (image_v, label_v) in enumerate(tqdm(val_dataloader)):\n#     with torch.no_grad():\n#       label_p_v = model(image_v.to(config['device']))\n#       val_loss = criterion(label_p_v,label_v.to(config['device']))\n#       val_losses.append(val_loss.item())\n#       del image_v, label_p_v, label_v\n#       del val_loss\n#       torch.cuda.empty_cache()\n# #   print(loss.item(),val_loss.item())\n#   print(losses[-1], val_losses[-1])\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-07-11T02:46:15.266175Z","iopub.execute_input":"2023-07-11T02:46:15.266563Z","iopub.status.idle":"2023-07-11T02:46:15.272071Z","shell.execute_reply.started":"2023-07-11T02:46:15.266509Z","shell.execute_reply":"2023-07-11T02:46:15.270995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# image, mask = dataset[0]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# image = image[None,:]\n# mask = mask[None,:]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model(image.cuda()).detach().cpu()[0][0]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.imshow(model(image.cuda()).detach().cpu()[0][0]>=0.98,cmap='viridis')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.imshow(torch.argmax(model(image.cuda()),dim=1).detach().cpu()[0])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.imshow(mask[0][0])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for image_path in os.listdir(folder + 'test'):\n#     image_path = folder + 'test/' + image_path\n#     image =  cv2.imread(image_path)\n#     image = np.transpose(np.array(image).astype(np.float32), axes=[2, 0, 1]) / 255.0\n#     with torch.no_grad():\n#         pred = model(torch.tensor(image)[None,:].to(config['device']))\n# # test_dataloader = torch.utils.data.DataLoader(dataset_test, batch_size=1, shuffle=False)\n\n# # for image, mask in tqdm(test_dataloader):\n# #     image = image.to(config['device'])\n# #     with torch.no_grad():\n# #         out = model(image)\n# # out = np.where(out.permute(0, 2, 3, 1).to('cpu').numpy() >= 0.9, 1.0, 0.0)\n# # fig, ax = plt.subplots(1, 2, figsize=(20, 10))\n# # ax0 = ax[0].imshow(image[0,:,:,:].permute(1, 2, 0).to('cpu'), cmap='viridis')\n# # # ax[1].imshow(np.round(out[0,:,:,:].permute(1, 2, 0).to('cpu')))\n# # ax1 = ax[1].imshow(out[0,:,:,:], cmap='viridis')\n# # fig.colorbar(ax0, ax=ax[0])\n# # fig.colorbar(ax1, ax=ax[1])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.imshow(pred[0][0].detach().cpu()>0.975)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import os\n# os.listdir(folder+'test')\nimport torch.nn as nn","metadata":{"execution":{"iopub.status.busy":"2023-07-11T02:43:17.025238Z","iopub.execute_input":"2023-07-11T02:43:17.025851Z","iopub.status.idle":"2023-07-11T02:43:17.030876Z","shell.execute_reply.started":"2023-07-11T02:43:17.025818Z","shell.execute_reply":"2023-07-11T02:43:17.029795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class simplemodel(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.conv1 = nn.Conv2d(3, 1, kernel_size = (3,3), stride = 1, padding = 1)\n    def forward(self,x):\n        return torch.sigmoid(self.conv1(x))","metadata":{"execution":{"iopub.status.busy":"2023-07-11T02:43:17.250105Z","iopub.execute_input":"2023-07-11T02:43:17.250457Z","iopub.status.idle":"2023-07-11T02:43:17.255907Z","shell.execute_reply.started":"2023-07-11T02:43:17.250428Z","shell.execute_reply":"2023-07-11T02:43:17.254987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = simplemodel()\nmodel.to('cuda')","metadata":{"execution":{"iopub.status.busy":"2023-07-11T02:50:37.822310Z","iopub.execute_input":"2023-07-11T02:50:37.822735Z","iopub.status.idle":"2023-07-11T02:50:37.831493Z","shell.execute_reply.started":"2023-07-11T02:50:37.822696Z","shell.execute_reply":"2023-07-11T02:50:37.830501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import torch\n# x = torch.randn((1,3,512,512)).to('cuda')\n# y = torch.rand((1,1,512,512)).to('cuda')\n# criterion = torch.nn.BCELoss()\n# criterion(y,model(x))","metadata":{"execution":{"iopub.status.busy":"2023-07-11T02:50:38.358117Z","iopub.execute_input":"2023-07-11T02:50:38.358473Z","iopub.status.idle":"2023-07-11T02:50:38.362960Z","shell.execute_reply.started":"2023-07-11T02:50:38.358444Z","shell.execute_reply":"2023-07-11T02:50:38.361663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"losses = []\nval_losses = []\nfrom tqdm import tqdm\nfor epoch in range(1):#config['num_epochs']):\n  for i, (image, label) in enumerate(tqdm(train_dataloader)):\n    image = image.to('cuda')\n    label = label.to('cuda')\n    label_p = model(image)\n    loss = criterion(label_p,label)\n#     losses.append(loss.item())\n    optimizer.zero_grad()\n    loss.backward()\n    optimizer.step()","metadata":{"execution":{"iopub.status.busy":"2023-07-11T02:50:39.596020Z","iopub.execute_input":"2023-07-11T02:50:39.596380Z","iopub.status.idle":"2023-07-11T02:50:44.509001Z","shell.execute_reply.started":"2023-07-11T02:50:39.596352Z","shell.execute_reply":"2023-07-11T02:50:44.507199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import base64\nimport numpy as np\nfrom pycocotools import _mask as coco_mask\nimport typing as t\nimport zlib\n\n\ndef encode_binary_mask(mask: np.ndarray) -> t.Text:\n  \"\"\"Converts a binary mask into OID challenge encoding ascii text.\"\"\"\n\n  # check input mask --\n#   if mask.dtype != np.bool:\n#     raise ValueError(\n#         \"encode_binary_mask expects a binary mask, received dtype == %s\" %\n#         mask.dtype)\n\n  mask = np.squeeze(mask)\n#   if len(mask.shape) != 2:\n#     raise ValueError(\n#         \"encode_binary_mask expects a 2d mask, received shape == %s\" %\n#         mask.shape)\n\n  # convert input mask to expected COCO API input --\n  mask_to_encode = mask.reshape(mask.shape[0], mask.shape[1], 1)\n  mask_to_encode = mask_to_encode.astype(np.uint8)\n  mask_to_encode = np.asfortranarray(mask_to_encode)\n\n  # RLE encode mask --\n  encoded_mask = coco_mask.encode(mask_to_encode)[0][\"counts\"]\n\n  # compress and base64 encoding --\n  binary_str = zlib.compress(encoded_mask, zlib.Z_BEST_COMPRESSION)\n  base64_str = base64.b64encode(binary_str)\n  return base64_str","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#submission code from https://www.kaggle.com/code/saworz/hubmap-how-to-make-first-dummy-submission\ndef get_pred_string(objs):\n\n    string = \"\"\n    for i, item in enumerate(objs):\n        mask = np.zeros((512,512), np.bool8)\n        mask[item>0] = 1\n        encoded_mask = encode_binary_mask(mask).decode(\"utf-8\")\n\n        if i == 0:\n            string += f\"0 1.0 {encoded_mask}\"\n        else:\n            string += f\" 0 1.0 {encoded_mask}\"\n        \n    return string","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"config={'device':'cuda'}","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_path = \"/kaggle/input/hubmap-hacking-the-human-vasculature/test/\"\nimport pandas as pd\n\nsubmission = pd.DataFrame()\nids = []\nh = []\nw = []\npred_strings = []\n    \nfor img_id in os.listdir(test_path):\n    curr_img = cv2.imread(test_path + img_id)\n\n    image = np.transpose(np.array(curr_img).astype(np.float32), axes=[2, 0, 1]) / 255.0\n    img = torch.tensor(image)[None,:].to(config['device'])\n    with torch.no_grad():\n        pred = model(img)\n    objs = ((pred.detach().cpu()>0.98)[0][0])\n    ## Get id, height, width\n    height, width, channels = curr_img.shape\n    ids.append(img_id.split(\".\")[0])\n    h.append(height)\n    w.append(width)\n    del img\n    del pred\n    torch.cuda.empty_cache()\n    \n    ## Get prediction_string\n    pred_strings.append(get_pred_string(objs))\n\nsubmission[\"id\"] = ids\nsubmission[\"height\"] = h\nsubmission[\"width\"] = w\nsubmission[\"prediction_string\"] = pred_strings\nsubmission.set_index(\"id\", inplace=True)\nsubmission","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}