{"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":"## <font color='gold'/>**Install and Import**","metadata":{}},{"cell_type":"code","source":"!pip install \\\n    --requirement ../input/cellpose-wheels/requirements.txt \\\n    --no-index \\\n    --find-links ../input/cellpose-wheels/wheels","metadata":{"execution":{"iopub.status.busy":"2022-08-18T16:40:48.901363Z","iopub.execute_input":"2022-08-18T16:40:48.901735Z","iopub.status.idle":"2022-08-18T16:41:58.564564Z","shell.execute_reply.started":"2022-08-18T16:40:48.901641Z","shell.execute_reply":"2022-08-18T16:41:58.563598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport glob\nimport imageio\nimport matplotlib.pyplot as plt\nfrom cellpose import models\nfrom tqdm import tqdm\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-08-18T16:41:58.567069Z","iopub.execute_input":"2022-08-18T16:41:58.567373Z","iopub.status.idle":"2022-08-18T16:42:05.827030Z","shell.execute_reply.started":"2022-08-18T16:41:58.567333Z","shell.execute_reply":"2022-08-18T16:42:05.826167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## <font color='gold'/>**Utility functions**","metadata":{}},{"cell_type":"code","source":"# Run Length Encode an instance\ndef 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    return ' '.join(str(x) for x in runs)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T04:04:51.228714Z","iopub.execute_input":"2022-08-12T04:04:51.229120Z","iopub.status.idle":"2022-08-12T04:04:51.235284Z","shell.execute_reply.started":"2022-08-12T04:04:51.229086Z","shell.execute_reply":"2022-08-12T04:04:51.234625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Init model\ncp_model_path = '../input/sartorius-weight/cellpose_221908_mosaic_astro_cort_2'\nsz_model_path = '../input/sartorius-weight/cellpose_1700_size.npy'\n\nmodel = models.CellposeModel(gpu=True, pretrained_model=cp_model_path)\n#sz_model = models.SizeModel(cp_model=model, pretrained_size=sz_model_path)\n\n# Model configuration\nchannels = [0, 0]  # segment grayscale image","metadata":{"execution":{"iopub.status.busy":"2022-08-18T16:42:10.467091Z","iopub.execute_input":"2022-08-18T16:42:10.467369Z","iopub.status.idle":"2022-08-18T16:42:14.179252Z","shell.execute_reply.started":"2022-08-18T16:42:10.467337Z","shell.execute_reply":"2022-08-18T16:42:14.177453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dir = glob.glob('../input/sartorius-cell-instance-segmentation/test/*.png')\nimage_names, pred_annots = [], []\n\nfor file_name in tqdm(test_dir):\n    img = imageio.imread(file_name)\n    img_name = file_name.split('/')[-1].split('.')[0]\n    #predicted_diam, _ = sz_model.eval(img, channels=channels)\n    masks, flows, _ = model.eval(img, diameter=19, channels=channels, augment=True)\n    \n    for i in range(1, masks.max() + 1):\n        pred_annot = rle_encode(masks == i)\n        image_names.append(img_name)\n        pred_annots.append(pred_annot)","metadata":{"execution":{"iopub.status.busy":"2022-06-13T15:48:39.889481Z","iopub.execute_input":"2022-06-13T15:48:39.889743Z","iopub.status.idle":"2022-06-13T15:48:46.500361Z","shell.execute_reply.started":"2022-06-13T15:48:39.889687Z","shell.execute_reply":"2022-06-13T15:48:46.499673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display result\nfrom cellpose import plot\n\nfig = plt.figure(figsize=(24, 8))\nplot.show_segmentation(fig, img, masks, flows[0], channels=channels)\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-06-13T15:48:46.501509Z","iopub.execute_input":"2022-06-13T15:48:46.502283Z","iopub.status.idle":"2022-06-13T15:48:47.640673Z","shell.execute_reply.started":"2022-06-13T15:48:46.502243Z","shell.execute_reply":"2022-06-13T15:48:47.639791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame({'id': image_names, 'predicted': pred_annots}).sort_values(['id']).to_csv('submission.csv', index=False)\npd.read_csv('submission.csv').head(20)","metadata":{"execution":{"iopub.status.busy":"2022-06-13T15:48:47.642018Z","iopub.execute_input":"2022-06-13T15:48:47.642499Z","iopub.status.idle":"2022-06-13T15:48:47.688679Z","shell.execute_reply.started":"2022-06-13T15:48:47.642462Z","shell.execute_reply":"2022-06-13T15:48:47.687942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}