{"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":6158831,"sourceType":"datasetVersion","datasetId":3532986}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from fastai.vision.all import * \nimport cv2","metadata":{"execution":{"iopub.status.busy":"2023-12-20T03:23:42.990713Z","iopub.execute_input":"2023-12-20T03:23:42.991050Z","iopub.status.idle":"2023-12-20T03:23:51.311057Z","shell.execute_reply.started":"2023-12-20T03:23:42.991020Z","shell.execute_reply":"2023-12-20T03:23:51.310173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path('/kaggle/input/blood-vessel-segmentation')\nPath.BASE_PATH = path\npath.ls()","metadata":{"execution":{"iopub.status.busy":"2023-12-20T03:23:51.312761Z","iopub.execute_input":"2023-12-20T03:23:51.313068Z","iopub.status.idle":"2023-12-20T03:23:51.321218Z","shell.execute_reply.started":"2023-12-20T03:23:51.313041Z","shell.execute_reply":"2023-12-20T03:23:51.320272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files = get_image_files(path/'train')\nfiles","metadata":{"execution":{"iopub.status.busy":"2023-12-20T03:26:20.742174Z","iopub.execute_input":"2023-12-20T03:26:20.743131Z","iopub.status.idle":"2023-12-20T03:26:23.514259Z","shell.execute_reply.started":"2023-12-20T03:26:20.743091Z","shell.execute_reply":"2023-12-20T03:26:23.513322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = sorted([img for img in files if img.parent.name != 'labels'])[:500]\nlabels = sorted([img for img in files if img.parent.name == 'labels'])[:500]","metadata":{"execution":{"iopub.status.busy":"2023-12-20T03:27:35.775743Z","iopub.execute_input":"2023-12-20T03:27:35.776870Z","iopub.status.idle":"2023-12-20T03:27:35.933637Z","shell.execute_reply.started":"2023-12-20T03:27:35.776829Z","shell.execute_reply":"2023-12-20T03:27:35.932622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_image(fname):\n    img = np.array(Image.open(fname))\n    img = np.interp(img, [np.min(img), np.max(img)], [0,255])\n    return img\n\ndef load_msk(path):\n    msk = cv2.imread(path, cv2.IMREAD_UNCHANGED)\n    msk = msk.astype('float32')\n    msk/=255.0\n    return msk","metadata":{"execution":{"iopub.status.busy":"2023-12-20T03:27:36.109696Z","iopub.execute_input":"2023-12-20T03:27:36.110711Z","iopub.status.idle":"2023-12-20T03:27:36.117574Z","shell.execute_reply.started":"2023-12-20T03:27:36.110669Z","shell.execute_reply":"2023-12-20T03:27:36.116497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def label_func(img):\n    mask_path = '/'+'/'.join(img.parts[1:6])+'/labels/'+img.name\n    mask = load_msk(mask_path)\n    return mask","metadata":{"execution":{"iopub.status.busy":"2023-12-20T03:27:36.504271Z","iopub.execute_input":"2023-12-20T03:27:36.504681Z","iopub.status.idle":"2023-12-20T03:27:36.512866Z","shell.execute_reply.started":"2023-12-20T03:27:36.504650Z","shell.execute_reply":"2023-12-20T03:27:36.511754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dblock = DataBlock(blocks=(ImageBlock, MaskBlock),\n                   splitter=RandomSplitter(0.2),\n                   get_x=load_image,\n                   get_y=label_func,\n                   item_tfms=Resize(224),\n                   batch_tfms=[*aug_transforms(), Normalize.from_stats(*imagenet_stats)])","metadata":{"execution":{"iopub.status.busy":"2023-12-20T03:27:36.977687Z","iopub.execute_input":"2023-12-20T03:27:36.978622Z","iopub.status.idle":"2023-12-20T03:27:36.987959Z","shell.execute_reply.started":"2023-12-20T03:27:36.978582Z","shell.execute_reply":"2023-12-20T03:27:36.986865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"codes = {1: 'background', 2:'blood-vessel'}","metadata":{"execution":{"iopub.status.busy":"2023-12-20T03:27:37.812372Z","iopub.execute_input":"2023-12-20T03:27:37.812756Z","iopub.status.idle":"2023-12-20T03:27:37.817424Z","shell.execute_reply.started":"2023-12-20T03:27:37.812726Z","shell.execute_reply":"2023-12-20T03:27:37.816391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = dblock.dataloaders(images, codes=codes, bs=4)\ndls.c = 2","metadata":{"execution":{"iopub.status.busy":"2023-12-20T03:27:38.563767Z","iopub.execute_input":"2023-12-20T03:27:38.564700Z","iopub.status.idle":"2023-12-20T03:27:38.774618Z","shell.execute_reply.started":"2023-12-20T03:27:38.564669Z","shell.execute_reply":"2023-12-20T03:27:38.773511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2023-12-20T03:27:39.285893Z","iopub.execute_input":"2023-12-20T03:27:39.286293Z","iopub.status.idle":"2023-12-20T03:27:40.018351Z","shell.execute_reply.started":"2023-12-20T03:27:39.286262Z","shell.execute_reply":"2023-12-20T03:27:40.017437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = unet_learner(dls, resnet34, metrics=Dice())\nlearn.fine_tune(5)","metadata":{"execution":{"iopub.status.busy":"2023-12-20T03:27:40.258930Z","iopub.execute_input":"2023-12-20T03:27:40.259310Z","iopub.status.idle":"2023-12-20T03:29:31.866686Z","shell.execute_reply.started":"2023-12-20T03:27:40.259281Z","shell.execute_reply":"2023-12-20T03:29:31.865510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.export()","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:29:26.189196Z","iopub.execute_input":"2023-12-10T04:29:26.189927Z","iopub.status.idle":"2023-12-10T04:29:26.646000Z","shell.execute_reply.started":"2023-12-10T04:29:26.189896Z","shell.execute_reply":"2023-12-10T04:29:26.645115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn_inf = load_learner('export.pkl')","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:36:23.412708Z","iopub.execute_input":"2023-12-10T04:36:23.413088Z","iopub.status.idle":"2023-12-10T04:36:23.601888Z","shell.execute_reply.started":"2023-12-10T04:36:23.413059Z","shell.execute_reply":"2023-12-10T04:36:23.600776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv(path/'sample_submission.csv')\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:36:24.132840Z","iopub.execute_input":"2023-12-10T04:36:24.133879Z","iopub.status.idle":"2023-12-10T04:36:24.147748Z","shell.execute_reply.started":"2023-12-10T04:36:24.133841Z","shell.execute_reply":"2023-12-10T04:36:24.146769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_encode(img):\n    '''\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    '''\n    pixels = img.flatten()\n    pixels = np.concatenate([[0], pixels, [0]])\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    rle = ' '.join(str(x) for x in runs)\n    if rle=='':\n        rle = '1 0'\n    return rle","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:36:24.591056Z","iopub.execute_input":"2023-12-10T04:36:24.591496Z","iopub.status.idle":"2023-12-10T04:36:24.599638Z","shell.execute_reply.started":"2023-12-10T04:36:24.591461Z","shell.execute_reply":"2023-12-10T04:36:24.598551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rles = []\nfor id, row in submission.iterrows():\n    id = row['id']\n    img_path = os.path.join(path,'test', id[:-5], 'images', id[-4:]+'.tif')\n    mask, _, _ = learn_inf.predict(img_path)\n    rle = rle_encode(mask)\n    rles.append(rle)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:36:25.135629Z","iopub.execute_input":"2023-12-10T04:36:25.136486Z","iopub.status.idle":"2023-12-10T04:36:28.067466Z","shell.execute_reply.started":"2023-12-10T04:36:25.136452Z","shell.execute_reply":"2023-12-10T04:36:28.066700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['rle'] = rles\nsubmission.to_csv('submission.csv', index=False)\nsubmission","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:36:28.069215Z","iopub.execute_input":"2023-12-10T04:36:28.069798Z","iopub.status.idle":"2023-12-10T04:36:28.080635Z","shell.execute_reply.started":"2023-12-10T04:36:28.069766Z","shell.execute_reply":"2023-12-10T04:36:28.079869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}