{"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":"code","source":"!pip install ../input/sartorius-segmentation-mask-rcnn-tf-requirements/grpcio-1.32.0-cp37-cp37m-manylinux2014_x86_64.whl\n!pip install ../input/sartorius-segmentation-mask-rcnn-tf-requirements/gast-0.3.3-py2.py3-none-any.whl\n!pip install ../input/sartorius-segmentation-mask-rcnn-tf-requirements/six-1.15.0-py2.py3-none-any.whl\n!pip install ../input/sartorius-segmentation-mask-rcnn-tf-requirements/tensorflow_estimator-2.4.0-py2.py3-none-any.whl\n!pip install ../input/sartorius-segmentation-mask-rcnn-tf-requirements/h5py-2.10.0-cp37-cp37m-manylinux1_x86_64.whl\n!pip install ../input/sartorius-segmentation-mask-rcnn-tf-requirements/typing_extensions-3.7.4.3-py3-none-any.whl\n!pip install ../input/sartorius-segmentation-mask-rcnn-tf-requirements/tensorflow-2.4.0-cp37-cp37m-manylinux2010_x86_64.whl\n!pip install ../input/sartorius-segmentation-mask-rcnn-tf-requirements/Keras-2.4.0-py2.py3-none-any.whl\n!pip install ../input/leekunhee-mask-rcnn/Mask_RCNN-master","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-11-02T21:36:52.091546Z","iopub.execute_input":"2021-11-02T21:36:52.09186Z","iopub.status.idle":"2021-11-02T21:41:51.22473Z","shell.execute_reply.started":"2021-11-02T21:36:52.091784Z","shell.execute_reply":"2021-11-02T21:41:51.223897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<h2>Training Notebook:</h2>**\nhttps://www.kaggle.com/susnato/sartorius-segmentation-mask-rcnn-tf-train\n**<h1><font color='red'>If You Find This Useful Then Please upvote</h1>**","metadata":{}},{"cell_type":"markdown","source":"**<h2>Imports</h2>**","metadata":{}},{"cell_type":"code","source":"import os\nimport cv2\nimport shutil\nimport pandas as pd\nimport numpy as np\nfrom numpy import zeros, asarray\nimport matplotlib.pyplot as plt \n%matplotlib inline\n\nfrom mrcnn import utils\nimport mrcnn.config\nimport mrcnn.model as modellib\nimport keras\nimport tensorflow as tf\n\n\nseed=42\nnp.random.seed(seed)\ntf.random.set_seed(seed)\nos.environ['PYTHONHASHSEED'] = str(seed)\n\nos.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'\n\ntest_fol = '../input/sartorius-cell-instance-segmentation/test'\nsub = pd.read_csv('../input/sartorius-cell-instance-segmentation/sample_submission.csv')\ntf.__version__, keras.__version__, tf.keras.__version__","metadata":{"execution":{"iopub.status.busy":"2021-11-02T21:41:51.226792Z","iopub.execute_input":"2021-11-02T21:41:51.227006Z","iopub.status.idle":"2021-11-02T21:41:55.050752Z","shell.execute_reply.started":"2021-11-02T21:41:51.226981Z","shell.execute_reply":"2021-11-02T21:41:55.050018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<h2>Load Test Dataset</h2>**","metadata":{}},{"cell_type":"code","source":"# Model Configuration\nclass KaggleSartoriusConfig(mrcnn.config.Config):\n    NAME = \"kaggle_Sartorius_cfg\"\n    GPU_COUNT = 1\n    IMAGES_PER_GPU = 1 \n    NUM_CLASSES = 2\n    USE_MINI_MASK = False\n    \nkaggle_sartorius_model_config = KaggleSartoriusConfig()\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)","metadata":{"execution":{"iopub.status.busy":"2021-11-02T21:41:55.052223Z","iopub.execute_input":"2021-11-02T21:41:55.052721Z","iopub.status.idle":"2021-11-02T21:41:55.060443Z","shell.execute_reply.started":"2021-11-02T21:41:55.052683Z","shell.execute_reply":"2021-11-02T21:41:55.059822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<h2>Load Model</h2>**","metadata":{}},{"cell_type":"code","source":"# Build the Mask R-CNN Model Architecture\nmodel = mrcnn.model.MaskRCNN(mode='inference', \n                             model_dir='./infer__mask_rcnn/', \n                             config=kaggle_sartorius_model_config)\n#load trained-weights from the Train Notebook\n'''\n    WE ARE LOADING THE WEIGHTS OF EPOCH 5\n'''\nmodel.load_weights(filepath='../input/epochs-710/mask_rcnn_kaggle_sartorius_cfg_0010.h5', \n                   by_name=True)","metadata":{"execution":{"iopub.status.busy":"2021-11-02T21:41:55.061937Z","iopub.execute_input":"2021-11-02T21:41:55.066204Z","iopub.status.idle":"2021-11-02T21:42:10.225222Z","shell.execute_reply.started":"2021-11-02T21:41:55.066165Z","shell.execute_reply":"2021-11-02T21:42:10.224475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<h2>Fix Overlaps</h2>**","metadata":{}},{"cell_type":"code","source":"#import cupy as cp\nimport gc\n\ndef fix_overlap(msk):\n    \"\"\"\n    Args:\n        mask: multi-channel mask, each channel is an instance of cell, shape:(520,704,None)\n    Returns:\n        multi-channel mask with non-overlapping values, shape:(520,704,None)\n    \"\"\"\n    msk = np.array(msk)\n    msk = np.pad(msk, [[0,0],[0,0],[1,0]])\n    ins_len = msk.shape[-1]\n    msk = np.argmax(msk,axis=-1)\n    msk = tf.keras.utils.to_categorical(msk, num_classes=ins_len)\n    msk = msk[...,1:]\n    msk = msk[...,np.any(msk, axis=(0,1))]\n    return msk\n\ndef check_overlap(msk):\n    msk = msk.astype(np.bool).astype(np.uint8) # binary mask\n    return np.any(np.sum(msk, axis=-1)>1) # only one channgel will contain value\n","metadata":{"execution":{"iopub.status.busy":"2021-11-02T21:42:10.226701Z","iopub.execute_input":"2021-11-02T21:42:10.226953Z","iopub.status.idle":"2021-11-02T21:42:10.234641Z","shell.execute_reply.started":"2021-11-02T21:42:10.226918Z","shell.execute_reply":"2021-11-02T21:42:10.233969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<h2>Prediction</h2>**","metadata":{}},{"cell_type":"code","source":"%%time\nimport skimage\n\nimage_names, pred_annots = [],[]\nfor s in sub['id']:\n    img_name, prd=[],[]\n    image = skimage.io.imread(os.path.join(test_fol, s)+'.png')\n    if image.ndim != 3:\n        image = skimage.color.gray2rgb(image)\n    if image.shape[-1] == 4:\n        image = image[..., :3]\n    results = model.detect([image])[0]['masks']\n    print(image.shape, results.shape)\n    if results.shape[-1]==0:\n        img_name.append(s)\n        prd.append('')\n    else:\n        if check_overlap(results): # if mask instances have overlap then fix it\n            print(\"Overlap Found!\")\n            results = fix_overlap(results)\n        for r in range(results.shape[2]):\n            pred_mask = results[:, :, r]\n            pred_mask = pred_mask.astype(np.uint8)\n            \n            img_name.append(s)\n            prd.append(rle_encode(pred_mask))\n        \n    image_names.extend(img_name)\n    pred_annots.extend(prd)","metadata":{"execution":{"iopub.status.busy":"2021-11-02T21:53:37.572653Z","iopub.execute_input":"2021-11-02T21:53:37.572941Z","iopub.status.idle":"2021-11-02T21:53:38.825052Z","shell.execute_reply.started":"2021-11-02T21:53:37.57291Z","shell.execute_reply":"2021-11-02T21:53:38.824143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n**<h2>Submission</h2>**","metadata":{}},{"cell_type":"code","source":"assert len(os.listdir('../input/sartorius-cell-instance-segmentation/test'))==len(np.unique(image_names))\n\npd.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":"2021-11-02T21:42:20.435366Z","iopub.execute_input":"2021-11-02T21:42:20.435656Z","iopub.status.idle":"2021-11-02T21:42:20.796515Z","shell.execute_reply.started":"2021-11-02T21:42:20.435621Z","shell.execute_reply":"2021-11-02T21:42:20.795503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}