{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":61446,"databundleVersionId":6962461,"sourceType":"competition"},{"sourceId":7281813,"sourceType":"datasetVersion","datasetId":4222194}],"dockerImageVersionId":30627,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# packages to access folders and files\nimport sys, os\nfrom glob import glob\n\n# to read images\nimport cv2\nfrom PIL import Image\n\n# for data manipulation\nimport numpy as np\nimport pandas as pd\n\n# torch modules\nimport torch\n# data loading\nfrom torch.utils.data import Dataset, DataLoader\n# model building\nimport torch.nn as nn\n# preprocessing transforms for images\nfrom torchvision.transforms import ToTensor, ToPILImage, Resize, Compose, Normalize\n\n# paths necessary\nsys.path.append('/kaggle/input/blood-vessel-segmentation-third-party')\nsys.path.append('/kaggle/input/blood-vessel-segmentation-00')\n\n# dataset path\ndata_dir = \"/kaggle/input/blood-vessel-segmentation/\"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-26T05:53:45.533241Z","iopub.execute_input":"2023-12-26T05:53:45.533561Z","iopub.status.idle":"2023-12-26T05:53:49.857401Z","shell.execute_reply.started":"2023-12-26T05:53:45.533523Z","shell.execute_reply":"2023-12-26T05:53:49.856486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# list to store image paths\nX = []\n\n# list to store ids in the submission.csv file (refer the data card in the competition page)\nids = []\n\n# get list of all test folders\nvalid_folder = sorted(glob(f'{data_dir}/test/*'))\n\n# iterating throught folders\nfor image_folder in valid_folder:\n    #getting all images in each folder\n    file = sorted(glob(f'{image_folder}/images/*.tif'))\n    # appending files to \"X\"\n    X += file\n    # formatting the ids for the submission.csv file\n    ids += [(image_folder.split(\"/\")[-1] + \"_\" + x.split(\"/\")[-1][:-4]) for x in file]","metadata":{"execution":{"iopub.status.busy":"2023-12-26T05:53:49.859069Z","iopub.execute_input":"2023-12-26T05:53:49.859476Z","iopub.status.idle":"2023-12-26T05:53:49.960534Z","shell.execute_reply.started":"2023-12-26T05:53:49.859450Z","shell.execute_reply":"2023-12-26T05:53:49.959597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# rle encode function given in the Evaluation page of the competition\ndef rle_encode(mask):\n    pixel = mask.flatten()\n    pixel = np.concatenate([[0], pixel, [0]])\n    run = np.where(pixel[1:] != pixel[:-1])[0] + 1\n    run[1::2] -= run[::2]\n    rle = ' '.join(str(r) for r in run)\n    if rle == '':\n        rle = '1 0'\n    return rle","metadata":{"execution":{"iopub.status.busy":"2023-12-26T05:53:49.961837Z","iopub.execute_input":"2023-12-26T05:53:49.962607Z","iopub.status.idle":"2023-12-26T05:53:49.968820Z","shell.execute_reply.started":"2023-12-26T05:53:49.962549Z","shell.execute_reply":"2023-12-26T05:53:49.967865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Dummy model architecture\nclass DummyModel(nn.Module):\n    def __init__(self):\n        super(DummyModel, self).__init__()\n        self.conv1 = nn.Conv2d(1, 3, 3, padding=1)\n        self.conv2 = nn.Conv2d(3, 1, 3, padding=1)\n        self.relu = nn.ReLU()\n        self.sigmoid = nn.Sigmoid()\n    def forward(self, x):\n        x = self.conv1(x)\n        x = self.relu(x)\n        x = self.conv2(x)\n        x = self.sigmoid(x)\n        return x","metadata":{"execution":{"iopub.status.busy":"2023-12-26T05:53:49.970680Z","iopub.execute_input":"2023-12-26T05:53:49.970946Z","iopub.status.idle":"2023-12-26T05:53:49.979676Z","shell.execute_reply.started":"2023-12-26T05:53:49.970923Z","shell.execute_reply":"2023-12-26T05:53:49.978898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# preprocessing transforms\nimage_transform = Compose([ToTensor(), Normalize(mean = [0.5], std = [0.5])])\nlabel_transform = Compose([ToTensor()])","metadata":{"execution":{"iopub.status.busy":"2023-12-26T05:53:49.980767Z","iopub.execute_input":"2023-12-26T05:53:49.981277Z","iopub.status.idle":"2023-12-26T05:53:49.989121Z","shell.execute_reply.started":"2023-12-26T05:53:49.981247Z","shell.execute_reply":"2023-12-26T05:53:49.988301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# define model\nnet = DummyModel()\n# load weigths from custom dataset with weights\nnet.load_state_dict(torch.load(\"/kaggle/input/weights/model_weights.pth\"))\n# load model into GPU\nnet = net.cuda()\n\n# (optional - based on your architecture)\nthreshold = 0.1\n\n# list to store the rle encodings\nenc = []\n\n# iterate through the ids\nfor _ in range(len(ids)):\n    # load the image as a PIL image\n    img = np.array(Image.open(X[_]))\n    # convert into uint8 to match the preprocessing in training -> the dtype chages w.r.t to your model\n    img = np.array(img, np.uint8)\n    # preprocess image\n    img = image_transform(img)\n    \n    # ask torch to not accumulate gradients when infering ( not necesary when running for only inference,)\n    with torch.no_grad():\n        # load image into GPU before inferring\n        mask = net(img.cuda())\n        # Model outputs in shape : [1,1,H,W]. we squeeze it out to [H,W] and transfer it to cpu memory and make it a numyp array\n        mask = mask.squeeze(0).squeeze(0).cpu().numpy()\n    # (optional) based on your architecture\n    mask = np.where(mask > threshold, 1, 0)\n    \n    # get the rle_encoding for the mask\n    enc += [rle_encode(mask)]","metadata":{"execution":{"iopub.status.busy":"2023-12-26T05:54:50.544327Z","iopub.execute_input":"2023-12-26T05:54:50.545101Z","iopub.status.idle":"2023-12-26T05:54:50.645252Z","shell.execute_reply.started":"2023-12-26T05:54:50.545068Z","shell.execute_reply":"2023-12-26T05:54:50.644450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create dataframe with the required columns (refer the Data section in the competition page)\nsub = pd.DataFrame(columns = [\"id\", \"rle\"])\n\n# assign values in the columns\nsub[\"id\"] = ids\nsub[\"rle\"] = enc\n\n# write the submission.csv file\nsub.to_csv(\"submission.csv\", index = False)\nsub","metadata":{"execution":{"iopub.status.busy":"2023-12-26T05:54:51.131480Z","iopub.execute_input":"2023-12-26T05:54:51.132444Z","iopub.status.idle":"2023-12-26T05:54:51.146139Z","shell.execute_reply.started":"2023-12-26T05:54:51.132410Z","shell.execute_reply":"2023-12-26T05:54:51.145410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# make sure your notebook has internet access off before submitting","metadata":{},"execution_count":null,"outputs":[]}]}