{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":61446,"databundleVersionId":6962461,"sourceType":"competition"},{"sourceId":154339316,"sourceType":"kernelVersion"}],"dockerImageVersionId":30616,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from fastai.vision.all import *\nimport cv2","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-09T04:51:58.405423Z","iopub.execute_input":"2023-12-09T04:51:58.405802Z","iopub.status.idle":"2023-12-09T04:51:58.579382Z","shell.execute_reply.started":"2023-12-09T04:51:58.405773Z","shell.execute_reply":"2023-12-09T04:51:58.578602Z"},"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-09T04:51:58.753854Z","iopub.execute_input":"2023-12-09T04:51:58.754543Z","iopub.status.idle":"2023-12-09T04:51:58.762422Z","shell.execute_reply.started":"2023-12-09T04:51:58.754506Z","shell.execute_reply":"2023-12-09T04:51:58.761488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files = get_image_files(path/'train')\nfiles","metadata":{"execution":{"iopub.status.busy":"2023-12-09T04:51:59.193251Z","iopub.execute_input":"2023-12-09T04:51:59.193555Z","iopub.status.idle":"2023-12-09T04:52:01.380076Z","shell.execute_reply.started":"2023-12-09T04:51:59.193529Z","shell.execute_reply":"2023-12-09T04:52:01.378995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = [img for img in files if img.parent.name != 'labels']\nlabels = [img for img in files if img.parent.name == 'labels']","metadata":{"execution":{"iopub.status.busy":"2023-12-09T04:52:01.381600Z","iopub.execute_input":"2023-12-09T04:52:01.382241Z","iopub.status.idle":"2023-12-09T04:52:01.435096Z","shell.execute_reply.started":"2023-12-09T04:52:01.382213Z","shell.execute_reply":"2023-12-09T04:52:01.434205Z"},"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-09T04:52:01.435999Z","iopub.execute_input":"2023-12-09T04:52:01.436291Z","iopub.status.idle":"2023-12-09T04:52:01.441922Z","shell.execute_reply.started":"2023-12-09T04:52:01.436266Z","shell.execute_reply":"2023-12-09T04:52:01.440879Z"},"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-09T04:52:01.444451Z","iopub.execute_input":"2023-12-09T04:52:01.445104Z","iopub.status.idle":"2023-12-09T04:52:01.450970Z","shell.execute_reply.started":"2023-12-09T04:52:01.444993Z","shell.execute_reply":"2023-12-09T04:52:01.449930Z"},"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))","metadata":{"execution":{"iopub.status.busy":"2023-12-09T04:52:01.452360Z","iopub.execute_input":"2023-12-09T04:52:01.452993Z","iopub.status.idle":"2023-12-09T04:52:01.460568Z","shell.execute_reply.started":"2023-12-09T04:52:01.452958Z","shell.execute_reply":"2023-12-09T04:52:01.459491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"codes = {1: 'background', 2:'blood-vessel'}","metadata":{"execution":{"iopub.status.busy":"2023-12-09T04:52:01.461749Z","iopub.execute_input":"2023-12-09T04:52:01.462080Z","iopub.status.idle":"2023-12-09T04:52:01.470645Z","shell.execute_reply.started":"2023-12-09T04:52:01.462047Z","shell.execute_reply":"2023-12-09T04:52:01.469688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"samples = images[:200]\ndls = dblock.dataloaders(images, codes=codes, bs=16)\ndls.c = 2","metadata":{"execution":{"iopub.status.busy":"2023-12-09T04:52:01.731601Z","iopub.execute_input":"2023-12-09T04:52:01.732329Z","iopub.status.idle":"2023-12-09T04:52:04.986756Z","shell.execute_reply.started":"2023-12-09T04:52:01.732294Z","shell.execute_reply":"2023-12-09T04:52:04.985793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn_inf = load_learner('/kaggle/input/sennet-fastai-starter/export.pkl')","metadata":{"execution":{"iopub.status.busy":"2023-12-09T04:52:04.994254Z","iopub.execute_input":"2023-12-09T04:52:04.994532Z","iopub.status.idle":"2023-12-09T04:52:06.825351Z","shell.execute_reply.started":"2023-12-09T04:52:04.994508Z","shell.execute_reply":"2023-12-09T04:52:06.824374Z"},"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-09T04:52:09.279656Z","iopub.execute_input":"2023-12-09T04:52:09.280010Z","iopub.status.idle":"2023-12-09T04:52:09.309883Z","shell.execute_reply.started":"2023-12-09T04:52:09.279979Z","shell.execute_reply":"2023-12-09T04:52:09.309067Z"},"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-09T04:52:13.100006Z","iopub.execute_input":"2023-12-09T04:52:13.101129Z","iopub.status.idle":"2023-12-09T04:52:13.107341Z","shell.execute_reply.started":"2023-12-09T04:52:13.101093Z","shell.execute_reply":"2023-12-09T04:52:13.106274Z"},"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-09T04:52:13.584523Z","iopub.execute_input":"2023-12-09T04:52:13.584854Z","iopub.status.idle":"2023-12-09T04:52:16.227579Z","shell.execute_reply.started":"2023-12-09T04:52:13.584826Z","shell.execute_reply":"2023-12-09T04:52:16.225309Z"},"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-09T04:52:16.230106Z","iopub.execute_input":"2023-12-09T04:52:16.230836Z","iopub.status.idle":"2023-12-09T04:52:16.245509Z","shell.execute_reply.started":"2023-12-09T04:52:16.230774Z","shell.execute_reply":"2023-12-09T04:52:16.244615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}