{"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":"markdown","source":"Creates binary masks for all images for semantic segmentation. Also fill faulty holes in masks.","metadata":{}},{"cell_type":"code","source":"from pathlib import Path\nimport pandas as pd\nfrom tqdm.auto import tqdm\nfrom scipy import ndimage\nimport numpy as np\nimport pickle\nfrom cytoolz import partition","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","scrolled":true,"execution":{"iopub.status.busy":"2021-11-29T18:11:49.616209Z","iopub.execute_input":"2021-11-29T18:11:49.616543Z","iopub.status.idle":"2021-11-29T18:11:49.803160Z","shell.execute_reply.started":"2021-11-29T18:11:49.616491Z","shell.execute_reply":"2021-11-29T18:11:49.802523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir = Path(\"/kaggle/input/sartorius-cell-instance-segmentation\")\noutput_dir = Path(\"/kaggle/working\")","metadata":{"execution":{"iopub.status.busy":"2021-11-29T18:11:49.807473Z","iopub.execute_input":"2021-11-29T18:11:49.807781Z","iopub.status.idle":"2021-11-29T18:11:49.811207Z","shell.execute_reply.started":"2021-11-29T18:11:49.807754Z","shell.execute_reply":"2021-11-29T18:11:49.810455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"D=pd.read_csv(data_dir.joinpath(\"train.csv\"))\nD.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-29T18:11:49.812079Z","iopub.execute_input":"2021-11-29T18:11:49.812611Z","iopub.status.idle":"2021-11-29T18:11:50.141618Z","shell.execute_reply.started":"2021-11-29T18:11:49.812568Z","shell.execute_reply":"2021-11-29T18:11:50.140746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle2mask(rle, width, height):\n    result = np.zeros(width * height, dtype=np.uint8)\n    for start, length in rle:\n        start -= 1\n        result[start : start + length] = 1\n    return result.reshape(height, width)\n\n\ndef row2mask(row):\n    return rle2mask(partition(2, map(int, row.annotation.split())), row.width, row.height)\n\n\nimg_masks = {}\nmask_dir=output_dir.joinpath(\"train_masks\")\nmask_dir.mkdir(exist_ok=True)\n\nfor id_, rows in tqdm(D.groupby(\"id\")):\n    masks = [ndimage.binary_fill_holes(row2mask(row)).astype(np.uint8) for row in rows.itertuples(False)]\n    full_mask = np.max(masks, axis=0)\n    with mask_dir.joinpath(f\"mask_{id_}.pkl\").open(\"wb\") as f:\n        pickle.dump(full_mask, f)","metadata":{"execution":{"iopub.status.busy":"2021-11-29T18:11:50.143909Z","iopub.execute_input":"2021-11-29T18:11:50.144217Z","iopub.status.idle":"2021-11-29T18:14:02.245908Z","shell.execute_reply.started":"2021-11-29T18:11:50.144178Z","shell.execute_reply":"2021-11-29T18:14:02.244992Z"},"trusted":true},"execution_count":null,"outputs":[]}]}