{"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":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport os\nimport random as rand\nimport json\nimport cv2\nfrom skimage.morphology import flood","metadata":{"_uuid":"5c9ed51a-66f2-4929-b82b-d055432bf558","_cell_guid":"206282ae-c146-44aa-8670-0bdb8768c2e5","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-08-21T05:21:26.429459Z","iopub.execute_input":"2023-08-21T05:21:26.430135Z","iopub.status.idle":"2023-08-21T05:21:27.751457Z","shell.execute_reply.started":"2023-08-21T05:21:26.430080Z","shell.execute_reply":"2023-08-21T05:21:27.750496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_DIR = '/kaggle/input/hubmap-hacking-the-human-vasculature/train/'","metadata":{"_uuid":"b1a14ee8-3818-4f7b-b9fc-0859456e5a17","_cell_guid":"9dba9ca2-9127-418c-bc00-1d814578e816","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-08-21T05:21:27.753266Z","iopub.execute_input":"2023-08-21T05:21:27.753607Z","iopub.status.idle":"2023-08-21T05:21:27.763243Z","shell.execute_reply.started":"2023-08-21T05:21:27.753575Z","shell.execute_reply":"2023-08-21T05:21:27.762153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('/kaggle/input/hubmap-hacking-the-human-vasculature/polygons.jsonl', 'r') as json_file:\n    json_list = list(json_file)\n\ntiles_dicts = []\nfor json_str in json_list:\n    tiles_dicts.append(json.loads(json_str))","metadata":{"_uuid":"252da824-6af4-40f9-958b-54bae98b7fab","_cell_guid":"c91de489-9414-4998-8526-20ff0ae3ae11","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-08-21T05:21:27.765606Z","iopub.execute_input":"2023-08-21T05:21:27.765900Z","iopub.status.idle":"2023-08-21T05:21:32.477043Z","shell.execute_reply.started":"2023-08-21T05:21:27.765877Z","shell.execute_reply":"2023-08-21T05:21:32.476049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def image_gen(index)->np.array:\n    sample = tiles_dicts[index]['id'] +'.tif'\n    sample_dir = os.path.join(TRAIN_DIR, sample)\n    img_sample = cv2.imread(sample_dir)\n\n    bruh = len(tiles_dicts[index]['annotations'])\n\n    blood_vessel = np.zeros(shape=(512,512), dtype='int')\n    glomerulus = np.zeros(shape=(512,512), dtype='int')\n    unsure = np.zeros(shape=(512,512), dtype='int')\n    type_dict = {'blood_vessel':blood_vessel,'glomerulus':glomerulus,'unsure':unsure}\n    for i in range(bruh):\n        coord = tiles_dicts[index]['annotations'][i]['coordinates']\n        type_lbl = tiles_dicts[index]['annotations'][i]['type']\n        for j in coord[0]:\n            x = j[0]\n            y = j[1]\n            type_dict[type_lbl][y,x]=1\n            \n    return img_sample, ~flood(blood_vessel, (0,0), connectivity=1), ~flood(glomerulus, (0,0), connectivity=1), ~flood(unsure, (0,0), connectivity=1)","metadata":{"_uuid":"636c986b-eb9d-4b7a-835c-0f593b974943","_cell_guid":"ea343386-6d0b-42ae-84a5-46116139c1d6","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-08-21T05:21:32.479488Z","iopub.execute_input":"2023-08-21T05:21:32.479768Z","iopub.status.idle":"2023-08-21T05:21:32.493111Z","shell.execute_reply.started":"2023-08-21T05:21:32.479745Z","shell.execute_reply":"2023-08-21T05:21:32.492157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = rand.randrange(len(tiles_dicts))\nimg_sample, blood_vessel, glomerulus, unsure = image_gen(index)\n\nimg_bruh = img_sample[:,:,0] + blood_vessel*255 + glomerulus*255 + unsure*255\n\nplt.subplot(2,3,1)\nplt.axis('off')\nplt.title('IMAGE')\nplt.imshow(img_sample)\nplt.subplot(2,3,2)\nplt.axis('off')\nplt.title('Blood Vessel')\nplt.imshow(blood_vessel, cmap='gray')\nplt.subplot(2,3,3)\nplt.axis('off')\nplt.imshow(glomerulus,cmap='gray')\nplt.title('Glomerulus')\nplt.subplot(2,3,4)\nplt.axis('off')\nplt.imshow(unsure, cmap='gray')\nplt.title('Unsure')\nplt.subplot(2,3,5)\nplt.imshow(img_bruh)\nplt.show()","metadata":{"_uuid":"905fcf41-4d1c-4839-a880-18d2af59aa82","_cell_guid":"064d15e8-32c9-432a-85ff-8801d97834d9","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-08-21T05:21:32.495149Z","iopub.execute_input":"2023-08-21T05:21:32.495712Z","iopub.status.idle":"2023-08-21T05:21:33.099049Z","shell.execute_reply.started":"2023-08-21T05:21:32.495681Z","shell.execute_reply":"2023-08-21T05:21:33.098065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NUM_IMG = len(tiles_dicts)\nNUM_IMG","metadata":{"_uuid":"7b5f2998-27d6-42d5-9884-bcd8db6970c0","_cell_guid":"f7c96b6f-ee56-4546-8aa5-d02030035e5d","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-08-21T05:21:33.100245Z","iopub.execute_input":"2023-08-21T05:21:33.101478Z","iopub.status.idle":"2023-08-21T05:21:33.108237Z","shell.execute_reply.started":"2023-08-21T05:21:33.101443Z","shell.execute_reply":"2023-08-21T05:21:33.107153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch \nimport torchvision\nfrom torch import nn, utils\nfrom torch.utils import data\nfrom torch.nn import functional as F\nfrom torchvision import transforms\nfrom PIL.Image import fromarray\nfrom sklearn.preprocessing import LabelEncoder\n\ndevice = 'cuda' if torch.cuda.is_available() else 'cpu'","metadata":{"execution":{"iopub.status.busy":"2023-08-21T05:21:33.109950Z","iopub.execute_input":"2023-08-21T05:21:33.111186Z","iopub.status.idle":"2023-08-21T05:21:36.360491Z","shell.execute_reply.started":"2023-08-21T05:21:33.111000Z","shell.execute_reply":"2023-08-21T05:21:36.359486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class TissueDataset(data.Dataset):\n    def __init__(self, device=device, tar_size=(512, 512), img_dir=TRAIN_DIR, tiles_dicts=tiles_dicts):\n        self.tar_size = tar_size\n        self.img_dir = img_dir\n        self.tiles_dicts = tiles_dicts\n        self.trans = transforms.Compose([\n            transforms.Normalize((0.5,0.5,0.5), (0.5,0.5,0.5))\n        ])\n    \n    def __len__(self):\n        return len(self.tiles_dicts)\n    \n    def __getitem__(self, idx):\n        img_id = self.tiles_dicts[idx]['id'] +'.tif'\n        path = os.path.join(self.img_dir, img_id)\n        image = cv2.imread(path)\n        image = np.array(image, np.float32)/255.\n        image = np.reshape(image, (3, 512, 512))\n        \n        bruh = len(self.tiles_dicts[idx]['annotations'])\n        \n        empt = np.ones(shape=(512,512), dtype='int')\n        blood_vessel = np.zeros(shape=(512,512), dtype='int')\n        glomerulus = np.zeros(shape=(512,512), dtype='int')\n        unsure = np.zeros(shape=(512,512), dtype='int')\n        type_dict = {'blood_vessel':blood_vessel,'glomerulus':glomerulus,'unsure':unsure}\n        for i in range(bruh):\n            coord = self.tiles_dicts[idx]['annotations'][i]['coordinates']\n            type_lbl = self.tiles_dicts[idx]['annotations'][i]['type']\n            for j in coord[0]:\n                x = j[0]\n                y = j[1]\n                type_dict[type_lbl][y,x]=1\n        empt = empt-blood_vessel-glomerulus-unsure\n        target = np.zeros(shape=(512,512,4), dtype='int')\n        target[:,:,0] = empt\n        target[:,:,1] = ~flood(blood_vessel, (0,0), connectivity=1)\n        target[:,:,2] = ~flood(glomerulus, (0,0), connectivity=1)\n        target[:,:,3] = ~flood(unsure, (0,0), connectivity=1)\n        target[...,0] = target[...,0] - target[...,1] - target[...,2] - target[...,3]\n        target = torch.Tensor(target)\n        return torch.Tensor(image).to(device), target.to(device)","metadata":{"execution":{"iopub.status.busy":"2023-08-21T05:21:36.361972Z","iopub.execute_input":"2023-08-21T05:21:36.362614Z","iopub.status.idle":"2023-08-21T05:21:36.379089Z","shell.execute_reply.started":"2023-08-21T05:21:36.362586Z","shell.execute_reply":"2023-08-21T05:21:36.378112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_img(img, clr_chnl):\n    if type(img)==torch.Tensor:\n        img = img.to('cpu').numpy()\n    plt.figure()\n\n    fig, ax = plt.subplots(1, img.shape[0])\n    for i, axe in enumerate(ax):\n        axe.imshow(img[i].reshape(512,512,clr_chnl))\n        axe.axis('off')","metadata":{"execution":{"iopub.status.busy":"2023-08-21T05:21:36.380590Z","iopub.execute_input":"2023-08-21T05:21:36.381373Z","iopub.status.idle":"2023-08-21T05:21:36.391023Z","shell.execute_reply.started":"2023-08-21T05:21:36.381247Z","shell.execute_reply":"2023-08-21T05:21:36.389894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA_LOADER = TissueDataset()\nDATA = data.DataLoader(DATA_LOADER, shuffle=True, batch_size=4)\niter_data = iter(DATA)\nimg, target = iter_data.__next__()\n\nplot_img(img,3)\nplot_img(target,4)\ntarget.shape, type(target)","metadata":{"execution":{"iopub.status.busy":"2023-08-21T05:21:36.396002Z","iopub.execute_input":"2023-08-21T05:21:36.396502Z","iopub.status.idle":"2023-08-21T05:21:40.196401Z","shell.execute_reply.started":"2023-08-21T05:21:36.396475Z","shell.execute_reply":"2023-08-21T05:21:40.195364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(target[2,:,:,0].to('cpu').numpy())","metadata":{"execution":{"iopub.status.busy":"2023-08-21T05:21:40.198008Z","iopub.execute_input":"2023-08-21T05:21:40.198699Z","iopub.status.idle":"2023-08-21T05:21:40.497061Z","shell.execute_reply.started":"2023-08-21T05:21:40.198663Z","shell.execute_reply":"2023-08-21T05:21:40.496185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class U_Net(nn.Module):\n    def __init__(self, n_class):\n        super(U_Net, self).__init__()\n        self.n_class = n_class\n        self.out = nn.Conv2d(4, 4, (1,1))\n    \n    def down_block(self, x, in_chnls, out_chnls, pool=True):\n        if pool:\n            x = nn.MaxPool2d((2,2), stride=2)(x)\n        x_ = x\n        x_ = nn.Conv2d(in_chnls, out_chnls, (3,3), padding=1).cuda()(x_)\n        x = nn.Conv2d(in_chnls, out_chnls, (3,3), padding=1).cuda()(x)\n        x = nn.Conv2d(out_chnls, out_chnls, (3,3), padding=1).cuda()(x)\n        x = nn.BatchNorm2d(out_chnls).cuda()(x)\n        x = nn.ReLU().cuda()(x)\n        x = torch.add(x_, x)\n        x = nn.Dropout2d(0.2)(x)\n        return x\n    \n    def up_block(self, x, in_chnls, out_chnls, con):\n        x = nn.ConvTranspose2d(in_chnls, out_chnls, kernel_size=(2,2), stride=2).cuda()(x)\n        x = torch.cat([x, con],1)\n        x = nn.Conv2d(in_chnls, out_chnls, kernel_size=(3,3), padding=1).cuda()(x)\n        x = nn.Conv2d(out_chnls, out_chnls, kernel_size=(3,3), padding=1).cuda()(x)\n        x = nn.ReLU().cuda()(x)\n        return x\n    \n    def forward(self, img):\n        bs, ch, hi, wd = img.shape\n        xd1 = self.down_block(img, 3, 4, False)\n        xd2 = self.down_block(xd1, 4, 8)\n        xd3 = self.down_block(xd2, 8, 16)\n        xd4 = self.down_block(xd3, 16, 32)\n        \n        xu3 = self.up_block(xd4, 32, 16, xd3)\n        xu2 = self.up_block(xu3, 16, 8, xd2)\n        xu1 = self.up_block(xu2, 8, 4, xd1)\n        x = self.out(xu1)\n        x = x.permute(0,2,3,1)\n        #x = F.softmax(x)\n        return x\n\nmodel = U_Net(n_class=3).cuda()\nout = model(img)\nout.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-21T05:21:41.744727Z","iopub.execute_input":"2023-08-21T05:21:41.745089Z","iopub.status.idle":"2023-08-21T05:21:45.930194Z","shell.execute_reply.started":"2023-08-21T05:21:41.745058Z","shell.execute_reply":"2023-08-21T05:21:45.929314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# U_Net(3).parameters()","metadata":{"execution":{"iopub.status.busy":"2023-08-20T10:11:19.387867Z","iopub.execute_input":"2023-08-20T10:11:19.388227Z","iopub.status.idle":"2023-08-20T10:11:19.393217Z","shell.execute_reply.started":"2023-08-20T10:11:19.388199Z","shell.execute_reply":"2023-08-20T10:11:19.392101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class JaccardLoss(nn.Module):\n    def __init__(self):\n        super(JaccardLoss, self).__init__()\n\n    def forward(self, predicted_masks, true_masks):\n        intersection = torch.sum(predicted_masks * true_masks)\n        union = torch.sum(predicted_masks) + torch.sum(true_masks) - intersection\n        jaccard_index = intersection / union\n        jaccard_loss = 1.0 - jaccard_index\n        return jaccard_loss","metadata":{"execution":{"iopub.status.busy":"2023-08-21T05:22:46.873359Z","iopub.execute_input":"2023-08-21T05:22:46.873722Z","iopub.status.idle":"2023-08-21T05:22:46.880452Z","shell.execute_reply.started":"2023-08-21T05:22:46.873693Z","shell.execute_reply":"2023-08-21T05:22:46.879422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_DATASET = TissueDataset()\nTRAIN_LOADER = data.DataLoader(TRAIN_DATASET, batch_size=16, shuffle=True)\n\nn_epochs = 30\n\nmodel = U_Net(3).cuda()\nmodel.train()\n\nloss_fn_1 = JaccardLoss()\nloss_fn_2 = nn.CrossEntropyLoss()\noptim = torch.optim.Adam(model.parameters(), lr=0.003)\n\nfor epoch in range(n_epochs):\n    print(f'Epoch: {epoch+1}', end=' ')\n    iou_loss = 0\n    bce_loss = 0\n    for img, target in TRAIN_LOADER:\n        optim.zero_grad()\n        pred = model(img)\n        loss_1 = loss_fn_1(F.softmax(pred), target)\n        loss_2 = loss_fn_2(F.log_softmax(pred), target)\n        loss = loss_1+loss_2\n        loss.backward()\n        optim.step()\n        iou_loss+=loss_1.to('cpu').detach().numpy()\n        bce_loss+=loss_2.to('cpu').detach().numpy()\n    \n    iou_loss/=len(TRAIN_LOADER)\n    bce_loss/=len(TRAIN_LOADER)\n    print(f'|IoU Loss: {iou_loss} |BCE Loss: {bce_loss}')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.save(model.state_dict(), 'IoU_Categorical_20.pth')","metadata":{"execution":{"iopub.status.busy":"2023-08-20T17:41:00.413321Z","iopub.execute_input":"2023-08-20T17:41:00.414747Z","iopub.status.idle":"2023-08-20T17:41:00.422925Z","shell.execute_reply.started":"2023-08-20T17:41:00.414710Z","shell.execute_reply":"2023-08-20T17:41:00.421963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_img = cv2.imread('/kaggle/input/hubmap-hacking-the-human-vasculature/test/72e40acccadf.tif')\nplt.imshow(test_img)","metadata":{"execution":{"iopub.status.busy":"2023-08-20T17:50:50.448926Z","iopub.status.idle":"2023-08-20T17:50:50.449410Z","shell.execute_reply.started":"2023-08-20T17:50:50.449169Z","shell.execute_reply":"2023-08-20T17:50:50.449192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_img = np.reshape(test_img, (1,3, 512, 512))","metadata":{"execution":{"iopub.status.busy":"2023-08-20T17:50:50.451484Z","iopub.status.idle":"2023-08-20T17:50:50.451970Z","shell.execute_reply.started":"2023-08-20T17:50:50.451730Z","shell.execute_reply":"2023-08-20T17:50:50.451752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_img = torch.Tensor(test_img).cuda()\nmodel.eval()\nout = model(test_img)","metadata":{"execution":{"iopub.status.busy":"2023-08-20T17:50:50.453271Z","iopub.status.idle":"2023-08-20T17:50:50.454170Z","shell.execute_reply.started":"2023-08-20T17:50:50.453912Z","shell.execute_reply":"2023-08-20T17:50:50.453938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out = torch.squeeze(out)\nout.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-20T17:50:50.456037Z","iopub.status.idle":"2023-08-20T17:50:50.456521Z","shell.execute_reply.started":"2023-08-20T17:50:50.456259Z","shell.execute_reply":"2023-08-20T17:50:50.456280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out = out.detach().to('cpu')\nout.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-20T17:50:50.463202Z","iopub.status.idle":"2023-08-20T17:50:50.463980Z","shell.execute_reply.started":"2023-08-20T17:50:50.463734Z","shell.execute_reply":"2023-08-20T17:50:50.463756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(out[...,0])\nplt.figure()\nplt.imshow(out[...,1])\nplt.figure()\nplt.imshow(out[...,2])\nplt.figure()\nplt.imshow(out[...,3])","metadata":{"execution":{"iopub.status.busy":"2023-08-20T17:50:50.465405Z","iopub.status.idle":"2023-08-20T17:50:50.466248Z","shell.execute_reply.started":"2023-08-20T17:50:50.465990Z","shell.execute_reply":"2023-08-20T17:50:50.466023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}