{"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":"# Inference example with a Cellpose model: www.cellpose.org\nThe model is based on U-Net, however rather than training it directly on bitmask targets they first convert them to \"spatial flows\" representations and train on that. This makes segmentation of dense and touching cells more reliable. For details and additional tricks they use see the paper \"Cellpose: a generalist algorithm for cellular segmentation\".\n\nTo train it I used the script provided in the cellpose repo ie: `python -m cellpose --train ...` after I converted the dataset to the input format it expects.\n\nIn inference I just submit the masks as they were returned from the model - no postprocessing","metadata":{}},{"cell_type":"code","source":"!pip install --no-index ../input/cellpose-library/offline_whls/cellpose-0.7.2-py3-none-any.whl --find-links=../input/cellpose-library/offline_whls","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-04T15:14:59.451065Z","iopub.execute_input":"2021-12-04T15:14:59.4514Z","iopub.status.idle":"2021-12-04T15:15:07.805992Z","shell.execute_reply.started":"2021-12-04T15:14:59.451348Z","shell.execute_reply":"2021-12-04T15:15:07.80508Z"},"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### I'm running it in a separate process rather than as a regular notebook because I've faced issues with numpy version not updating after the `pip install` above. If you know how to fix this please let me know.","metadata":{}},{"cell_type":"code","source":"%%writefile run.py\nimport numpy as np\nfrom cellpose import models, io, plot\nfrom pathlib import Path\nimport pandas as pd\n\ndef rle_encode(img):\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)\n\ntest_dir = Path('../input/sartorius-cell-instance-segmentation/test')\ntest_files = [fname for fname in test_dir.iterdir()]\nmodel = models.CellposeModel(gpu=True, pretrained_model='../input/cellpose-model-example/cellpose_residual_on_style_on_concatenation_off_cellpose_train_2021_12_03_14_43_12.213259')\n\nids, masks = [],[]\nfor fn in test_files:\n    preds, flows, _ = model.eval(io.imread(str(fn)), diameter=19, channels=[0,0], augment=True, resample=True)\n    for i in range (1, preds.max() + 1):\n        ids.append(fn.stem)\n        masks.append(rle_encode(preds == i))\n        \npd.DataFrame({'id':ids, 'predicted':masks}).to_csv('submission.csv', index=False)\n","metadata":{"execution":{"iopub.status.busy":"2021-12-04T15:22:55.772442Z","iopub.execute_input":"2021-12-04T15:22:55.772836Z","iopub.status.idle":"2021-12-04T15:22:55.779569Z","shell.execute_reply.started":"2021-12-04T15:22:55.772796Z","shell.execute_reply":"2021-12-04T15:22:55.778704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python run.py","metadata":{"execution":{"iopub.status.busy":"2021-12-04T15:22:56.848779Z","iopub.execute_input":"2021-12-04T15:22:56.849255Z","iopub.status.idle":"2021-12-04T15:23:20.703973Z","shell.execute_reply.started":"2021-12-04T15:22:56.849218Z","shell.execute_reply":"2021-12-04T15:23:20.703102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\npd.read_csv('submission.csv').head()","metadata":{"execution":{"iopub.status.busy":"2021-12-04T15:23:20.706032Z","iopub.execute_input":"2021-12-04T15:23:20.706253Z","iopub.status.idle":"2021-12-04T15:23:20.722469Z","shell.execute_reply.started":"2021-12-04T15:23:20.706226Z","shell.execute_reply":"2021-12-04T15:23:20.721771Z"},"trusted":true},"execution_count":null,"outputs":[]}]}