{"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":"# Sartorius Segmentation - Detectron2 [Inference]","metadata":{"papermill":{"duration":0.012883,"end_time":"2021-10-21T08:46:26.376113","exception":false,"start_time":"2021-10-21T08:46:26.36323","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"### Hi kagglers, This is `Training` notebook using `Detectron2`.\n[Sartorius Segmentation - Detectron2 [training]](https://www.kaggle.com/ammarnassanalhajali/sartorius-segmentation-detectron2-training) \n### Please if this kernel is useful, <font color='red'>please upvote !!</font>","metadata":{"papermill":{"duration":0.011945,"end_time":"2021-10-21T08:46:26.40046","exception":false,"start_time":"2021-10-21T08:46:26.388515","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"## Other notebooks in this competition \n- [Sartorius Segmentation - Keras U-Net[Training]](https://www.kaggle.com/ammarnassanalhajali/sartorius-segmentation-keras-u-net-training)\n- [Sartorius Segmentation - Keras U-Net[Inference]](https://www.kaggle.com/ammarnassanalhajali/sartorius-segmentation-keras-u-net-inference/edit)","metadata":{"papermill":{"duration":0.011578,"end_time":"2021-10-21T08:46:26.424025","exception":false,"start_time":"2021-10-21T08:46:26.412447","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"# Detectron2 \nDetectron2 is Facebook AI Research's next generation software system that implements state-of-the-art object detection algorithms. It is a ground-up rewrite of the previous version, Detectron, and it originates from maskrcnn-benchmark\n\n","metadata":{"papermill":{"duration":0.011661,"end_time":"2021-10-21T08:46:26.447263","exception":false,"start_time":"2021-10-21T08:46:26.435602","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"## Install Detectron2 offline","metadata":{"papermill":{"duration":0.011278,"end_time":"2021-10-21T08:46:26.470179","exception":false,"start_time":"2021-10-21T08:46:26.458901","status":"completed"},"tags":[]}},{"cell_type":"code","source":"!pip install ../input/detectron-05/whls/pycocotools-2.0.2/dist/pycocotools-2.0.2.tar --no-index --find-links ../input/detectron-05/whls \n!pip install ../input/detectron-05/whls/fvcore-0.1.5.post20211019/fvcore-0.1.5.post20211019 --no-index --find-links ../input/detectron-05/whls \n!pip install ../input/detectron-05/whls/antlr4-python3-runtime-4.8/antlr4-python3-runtime-4.8 --no-index --find-links ../input/detectron-05/whls \n!pip install ../input/detectron-05/whls/detectron2-0.5/detectron2 --no-index --find-links ../input/detectron-05/whls ","metadata":{"_kg_hide-input":false,"_kg_hide-output":true,"papermill":{"duration":202.195854,"end_time":"2021-10-21T08:49:48.677674","exception":false,"start_time":"2021-10-21T08:46:26.48182","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-22T10:29:03.416993Z","iopub.execute_input":"2021-10-22T10:29:03.417551Z","iopub.status.idle":"2021-10-22T10:32:27.494269Z","shell.execute_reply.started":"2021-10-22T10:29:03.417467Z","shell.execute_reply":"2021-10-22T10:32:27.493438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# importing libraries","metadata":{"papermill":{"duration":0.030677,"end_time":"2021-10-21T08:49:48.740612","exception":false,"start_time":"2021-10-21T08:49:48.709935","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import detectron2\nfrom detectron2 import model_zoo\nfrom detectron2.engine import DefaultPredictor\nfrom detectron2.config import get_cfg\nfrom PIL import Image\nimport cv2\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom fastcore.all import *\ndetectron2.__version__","metadata":{"papermill":{"duration":1.256075,"end_time":"2021-10-21T08:49:50.027772","exception":false,"start_time":"2021-10-21T08:49:48.771697","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-22T10:32:27.496291Z","iopub.execute_input":"2021-10-22T10:32:27.496708Z","iopub.status.idle":"2021-10-22T10:32:28.659374Z","shell.execute_reply.started":"2021-10-22T10:32:27.496671Z","shell.execute_reply":"2021-10-22T10:32:28.658650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Functions","metadata":{"papermill":{"duration":0.030778,"end_time":"2021-10-21T08:49:50.089516","exception":false,"start_time":"2021-10-21T08:49:50.058738","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# From https://www.kaggle.com/stainsby/fast-tested-rle\ndef rle_decode(mask_rle, shape=(520, 704)):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (height,width) of array to return \n    Returns numpy array, 1 - mask, 0 - background\n\n    '''\n    s = mask_rle.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    return img.reshape(shape)  # Needed to align to RLE direction\n\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)\n\ndef get_masks(fn, predictor):\n    im = cv2.imread(str(fn))\n    outputs = predictor(im)\n    pred_masks = outputs['instances'].pred_masks.cpu().numpy()\n    res = []\n    used = np.zeros(im.shape[:2], dtype=int) \n    for mask in pred_masks:\n        mask = mask * (1-used)\n        used += mask\n        res.append(rle_encode(mask))\n    return res","metadata":{"papermill":{"duration":0.046363,"end_time":"2021-10-21T08:49:50.167377","exception":false,"start_time":"2021-10-21T08:49:50.121014","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-22T10:32:28.660984Z","iopub.execute_input":"2021-10-22T10:32:28.661499Z","iopub.status.idle":"2021-10-22T10:32:28.673156Z","shell.execute_reply.started":"2021-10-22T10:32:28.661459Z","shell.execute_reply":"2021-10-22T10:32:28.672253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Dir_testdata=Path('../input/sartorius-cell-instance-segmentation')\nids, masks=[],[]\ntest_image_names = (Dir_testdata/'test').ls()","metadata":{"papermill":{"duration":0.043527,"end_time":"2021-10-21T08:49:50.24159","exception":false,"start_time":"2021-10-21T08:49:50.198063","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-22T10:32:28.675396Z","iopub.execute_input":"2021-10-22T10:32:28.675732Z","iopub.status.idle":"2021-10-22T10:32:28.695512Z","shell.execute_reply.started":"2021-10-22T10:32:28.675694Z","shell.execute_reply":"2021-10-22T10:32:28.694620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Model","metadata":{"papermill":{"duration":0.030884,"end_time":"2021-10-21T08:49:50.303102","exception":false,"start_time":"2021-10-21T08:49:50.272218","status":"completed"},"tags":[]}},{"cell_type":"code","source":"cfg = get_cfg()\ncfg.merge_from_file(model_zoo.get_config_file(\"COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml\"))\ncfg.INPUT.MASK_FORMAT='bitmask'\ncfg.MODEL.WEIGHTS = \"../input/sartoriussegmentationdetectron2rasin/output/model_final.pth\" \ncfg.MODEL.ROI_HEADS.NUM_CLASSES = 3 \ncfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5\ncfg.TEST.DETECTIONS_PER_IMAGE = 1000\npredictor = DefaultPredictor(cfg)","metadata":{"papermill":{"duration":6.943331,"end_time":"2021-10-21T08:49:57.277649","exception":false,"start_time":"2021-10-21T08:49:50.334318","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-22T10:32:28.696691Z","iopub.execute_input":"2021-10-22T10:32:28.697447Z","iopub.status.idle":"2021-10-22T10:32:35.635406Z","shell.execute_reply.started":"2021-10-22T10:32:28.697407Z","shell.execute_reply":"2021-10-22T10:32:35.634666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predicting","metadata":{"papermill":{"duration":0.02825,"end_time":"2021-10-21T08:49:57.334214","exception":false,"start_time":"2021-10-21T08:49:57.305964","status":"completed"},"tags":[]}},{"cell_type":"code","source":"for fn in test_image_names:\n    encoded_masks = get_masks(fn, predictor)\n    for enc in encoded_masks:\n        ids.append(fn.stem)\n        masks.append(enc)","metadata":{"papermill":{"duration":6.169911,"end_time":"2021-10-21T08:50:03.532068","exception":false,"start_time":"2021-10-21T08:49:57.362157","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-22T10:32:35.636626Z","iopub.execute_input":"2021-10-22T10:32:35.636888Z","iopub.status.idle":"2021-10-22T10:32:41.690436Z","shell.execute_reply.started":"2021-10-22T10:32:35.636855Z","shell.execute_reply":"2021-10-22T10:32:41.689716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"id\",\",\",\"predicted\")\nprint(ids[0],\",\",masks[0])","metadata":{"papermill":{"duration":0.036639,"end_time":"2021-10-21T08:50:03.597836","exception":false,"start_time":"2021-10-21T08:50:03.561197","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-22T10:32:41.691564Z","iopub.execute_input":"2021-10-22T10:32:41.691803Z","iopub.status.idle":"2021-10-22T10:32:41.698257Z","shell.execute_reply.started":"2021-10-22T10:32:41.691772Z","shell.execute_reply":"2021-10-22T10:32:41.697475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualize predictions","metadata":{"papermill":{"duration":0.02841,"end_time":"2021-10-21T08:50:03.655051","exception":false,"start_time":"2021-10-21T08:50:03.626641","status":"completed"},"tags":[]}},{"cell_type":"code","source":"encoded_masks = get_masks(test_image_names[2], predictor)\n_, axs = plt.subplots(1,2, figsize=(40,30))\naxs[1].imshow(cv2.imread(str(test_image_names[0])))\naxs[1].axis(\"off\")\nmask = np.zeros((520, 704, 1))\nfor enc in encoded_masks:\n    mask += rle_decode(enc, shape=(520, 704, 1))\n    \nmask = mask.clip(0, 1)\naxs[0].imshow(mask)\naxs[0].axis(\"off\")","metadata":{"papermill":{"duration":1.077437,"end_time":"2021-10-21T08:50:04.761576","exception":false,"start_time":"2021-10-21T08:50:03.684139","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-22T10:32:41.699653Z","iopub.execute_input":"2021-10-22T10:32:41.700399Z","iopub.status.idle":"2021-10-22T10:32:42.781233Z","shell.execute_reply.started":"2021-10-22T10:32:41.700359Z","shell.execute_reply":"2021-10-22T10:32:42.778902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{"papermill":{"duration":0.045681,"end_time":"2021-10-21T08:50:04.853301","exception":false,"start_time":"2021-10-21T08:50:04.80762","status":"completed"},"tags":[]}},{"cell_type":"code","source":"pd.DataFrame({'id':ids, 'predicted':masks}).to_csv('submission.csv', index=False)\npd.read_csv('submission.csv').head()","metadata":{"papermill":{"duration":0.07296,"end_time":"2021-10-21T08:50:04.971828","exception":false,"start_time":"2021-10-21T08:50:04.898868","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-22T10:32:42.782350Z","iopub.execute_input":"2021-10-22T10:32:42.782659Z","iopub.status.idle":"2021-10-22T10:32:42.815442Z","shell.execute_reply.started":"2021-10-22T10:32:42.782620Z","shell.execute_reply":"2021-10-22T10:32:42.814568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# References\n* https://www.kaggle.com/slawekbiel/positive-score-with-detectron-3-3-inference","metadata":{"papermill":{"duration":0.045939,"end_time":"2021-10-21T08:50:05.065048","exception":false,"start_time":"2021-10-21T08:50:05.019109","status":"completed"},"tags":[]}}]}