{"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 os\nimport glob\n\nimport numpy as np\nimport pandas as pd\n\nimport cv2\nimport matplotlib.pyplot as plt\n\nfrom tqdm.notebook import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-16T05:36:40.978243Z","iopub.execute_input":"2021-10-16T05:36:40.978963Z","iopub.status.idle":"2021-10-16T05:36:41.25276Z","shell.execute_reply.started":"2021-10-16T05:36:40.978841Z","shell.execute_reply":"2021-10-16T05:36:41.251914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class FLAGS:\n    \n    data_dir = '/kaggle/input/sartorius-cell-instance-segmentation'\n    save_dir = '/kaggle/working'","metadata":{"execution":{"iopub.status.busy":"2021-10-16T05:36:42.097747Z","iopub.execute_input":"2021-10-16T05:36:42.098029Z","iopub.status.idle":"2021-10-16T05:36:42.102869Z","shell.execute_reply.started":"2021-10-16T05:36:42.098Z","shell.execute_reply":"2021-10-16T05:36:42.101609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle2mask(rle, shape):\n    \n    mask = np.zeros(shape, dtype=int).flatten()\n\n    rle = np.array(rle.split(), dtype=int)\n\n    starts = rle[0::2] - 1\n    lengths = rle[1::2]\n    ends = starts + lengths\n\n    for lo, hi in zip(starts, ends):\n        mask[lo:hi] = 1\n\n    mask = mask.reshape(shape)\n    \n    return mask","metadata":{"execution":{"iopub.status.busy":"2021-10-16T05:36:42.292239Z","iopub.execute_input":"2021-10-16T05:36:42.293177Z","iopub.status.idle":"2021-10-16T05:36:42.300528Z","shell.execute_reply.started":"2021-10-16T05:36:42.293121Z","shell.execute_reply":"2021-10-16T05:36:42.299916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(f'{FLAGS.data_dir}/train.csv')\ndf = df.set_index('id')\n\nfiles = sorted(glob.glob(f'{FLAGS.data_dir}/train/*.png'))\nprint(f'Number of files: {len(files)}')\n\nfor path in tqdm(files):\n\n    image_id = path.split('/')[-1][:-4]\n    image_df = df.loc[image_id]\n    \n    annotations = image_df['annotation'].values\n    shape = tuple(image_df.iloc[0][['height', 'width']].values)\n        \n    mask = []\n    for color, rle in enumerate(annotations):\n        inst_mask = rle2mask(rle, shape)\n        mask.append(inst_mask)\n    mask = np.argmax(mask, 0).astype(np.uint16)\n    \n    save_path = f'{FLAGS.save_dir}/{image_id}.png'\n    cv2.imwrite(save_path, mask)\n    \nprint(f'Processing complete')","metadata":{"execution":{"iopub.status.busy":"2021-10-16T05:38:28.508483Z","iopub.execute_input":"2021-10-16T05:38:28.508818Z","iopub.status.idle":"2021-10-16T05:38:31.434188Z","shell.execute_reply.started":"2021-10-16T05:38:28.508779Z","shell.execute_reply":"2021-10-16T05:38:31.433259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualize Masks (sanity check)","metadata":{}},{"cell_type":"code","source":"colored_mask = []\n\nfor val in np.unique(mask)[1:]:\n    \n    color = np.random.choice(256, 3)\n    \n    mask_ = mask==val\n    mask_ = np.dstack([mask_]*3)\n    mask_ = mask_ * color\n    \n    colored_mask.append(mask_)\n    \ncolored_mask = np.array(colored_mask)\ncolored_mask = np.sum(colored_mask, 0)","metadata":{"execution":{"iopub.status.busy":"2021-10-16T05:37:39.269309Z","iopub.status.idle":"2021-10-16T05:37:39.269633Z","shell.execute_reply.started":"2021-10-16T05:37:39.269469Z","shell.execute_reply":"2021-10-16T05:37:39.269485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = cv2.imread(path, -1)\n\nplt.figure(figsize=(10,10))\nplt.imshow(image, cmap='gray')\nplt.axis(\"off\")\nplt.show()\n\nplt.figure(figsize=(10,10))\nplt.imshow(colored_mask)\nplt.axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-16T05:37:39.270601Z","iopub.status.idle":"2021-10-16T05:37:39.270922Z","shell.execute_reply.started":"2021-10-16T05:37:39.270764Z","shell.execute_reply":"2021-10-16T05:37:39.27078Z"},"trusted":true},"execution_count":null,"outputs":[]}]}