{"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-11-12T05:51:10.871872Z","iopub.execute_input":"2021-11-12T05:51:10.872569Z","iopub.status.idle":"2021-11-12T05:55:37.708335Z","shell.execute_reply.started":"2021-11-12T05:51:10.872476Z","shell.execute_reply":"2021-11-12T05:55:37.707258Z"},"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\nimport torch\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-11-12T05:56:22.030917Z","iopub.execute_input":"2021-11-12T05:56:22.031364Z","iopub.status.idle":"2021-11-12T05:56:22.049267Z","shell.execute_reply.started":"2021-11-12T05:56:22.031319Z","shell.execute_reply":"2021-11-12T05:56:22.047803Z"},"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    pred = predictor(im)\n    pred_class = torch.mode(pred['instances'].pred_classes)[0]\n    take = pred['instances'].scores >= THRESHOLDS[pred_class]\n    pred_masks = pred['instances'].pred_masks[take]\n    pred_masks = 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        if mask.sum() >= MIN_PIXELS[pred_class]: # skip predictions with small area\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-11-12T05:55:39.390418Z","iopub.execute_input":"2021-11-12T05:55:39.390868Z","iopub.status.idle":"2021-11-12T05:55:39.409181Z","shell.execute_reply.started":"2021-11-12T05:55:39.390806Z","shell.execute_reply":"2021-11-12T05:55:39.407461Z"},"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-11-12T05:55:39.412335Z","iopub.execute_input":"2021-11-12T05:55:39.412709Z","iopub.status.idle":"2021-11-12T05:55:39.425433Z","shell.execute_reply.started":"2021-11-12T05:55:39.412664Z","shell.execute_reply":"2021-11-12T05:55:39.424437Z"},"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()\n#cfg.merge_from_file(model_zoo.get_config_file(\"COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml\"))\n#cfg.merge_from_file(model_zoo.get_config_file(\"COCO-InstanceSegmentation/mask_rcnn_X_101_32x8d_FPN_3x.yaml\")) #2--57\n#cfg.merge_from_file(model_zoo.get_config_file(\"COCO-InstanceSegmentation/mask_rcnn_R_101_C4_3x.yaml\")) #3--58\ncfg.merge_from_file(model_zoo.get_config_file(\"COCO-InstanceSegmentation/mask_rcnn_R_101_FPN_3x.yaml\")) #4--59\n\n\ncfg.INPUT.MASK_FORMAT='bitmask'\ncfg.MODEL.WEIGHTS = \"../input/sartorius-segmentation-la/output/model_best.pth\"\ncfg.MODEL.ROI_HEADS.NUM_CLASSES = 3 \n#cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.40\ncfg.TEST.DETECTIONS_PER_IMAGE = 1000\npredictor = DefaultPredictor(cfg)\nTHRESHOLDS = [.18, .38, .58]\nMIN_PIXELS = [75, 150, 75]","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-11-12T05:55:39.427912Z","iopub.execute_input":"2021-11-12T05:55:39.428811Z","iopub.status.idle":"2021-11-12T05:55:48.007153Z","shell.execute_reply.started":"2021-11-12T05:55:39.428683Z","shell.execute_reply":"2021-11-12T05:55:48.006178Z"},"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-11-12T05:56:33.423923Z","iopub.execute_input":"2021-11-12T05:56:33.424217Z","iopub.status.idle":"2021-11-12T05:56:34.225707Z","shell.execute_reply.started":"2021-11-12T05:56:33.424187Z","shell.execute_reply":"2021-11-12T05:56:34.22451Z"},"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-11-12T05:56:37.825557Z","iopub.execute_input":"2021-11-12T05:56:37.826573Z","iopub.status.idle":"2021-11-12T05:56:37.837184Z","shell.execute_reply.started":"2021-11-12T05:56:37.826521Z","shell.execute_reply":"2021-11-12T05:56:37.835622Z"},"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[0], 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-11-12T05:56:41.376926Z","iopub.execute_input":"2021-11-12T05:56:41.377893Z","iopub.status.idle":"2021-11-12T05:56:42.759089Z","shell.execute_reply.started":"2021-11-12T05:56:41.377853Z","shell.execute_reply":"2021-11-12T05:56:42.758213Z"},"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-11-12T05:56:52.381918Z","iopub.execute_input":"2021-11-12T05:56:52.382212Z","iopub.status.idle":"2021-11-12T05:56:52.421989Z","shell.execute_reply.started":"2021-11-12T05:56:52.382169Z","shell.execute_reply":"2021-11-12T05:56:52.420933Z"},"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":[]}}]}