{"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":{"execution":{"iopub.status.busy":"2021-11-23T16:58:55.734412Z","iopub.execute_input":"2021-11-23T16:58:55.734706Z","iopub.status.idle":"2021-11-23T17:01:09.497978Z","shell.execute_reply.started":"2021-11-23T16:58:55.734673Z","shell.execute_reply":"2021-11-23T17:01:09.496963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport keras\nimport shutil\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom mrcnn import config, utils\nimport matplotlib.pyplot as plt \nfrom numpy import zeros, asarray\nfrom mrcnn import model as modellib\nfrom tqdm import tqdm\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2021-11-23T17:01:12.829271Z","iopub.execute_input":"2021-11-23T17:01:12.830046Z","iopub.status.idle":"2021-11-23T17:01:17.107557Z","shell.execute_reply.started":"2021-11-23T17:01:12.829984Z","shell.execute_reply":"2021-11-23T17:01:17.106698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CustomConfig(config.Config):\n  IMAGES_PER_GPU=1\n  NUM_CLASSES=2\n  NAME='CustomConfig'\n  USE_MINI_MASK=False\nsartorius_model_cfg=CustomConfig()\n#sartorius_model_cfg.display()","metadata":{"execution":{"iopub.status.busy":"2021-11-23T17:01:17.108927Z","iopub.execute_input":"2021-11-23T17:01:17.109291Z","iopub.status.idle":"2021-11-23T17:01:17.114398Z","shell.execute_reply.started":"2021-11-23T17:01:17.109256Z","shell.execute_reply":"2021-11-23T17:01:17.113630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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[1::2]-runs[0::2]\n    #print(runs)\n    return ' '.join(str(i) for i in runs)","metadata":{"execution":{"iopub.status.busy":"2021-11-23T17:01:18.381548Z","iopub.execute_input":"2021-11-23T17:01:18.382242Z","iopub.status.idle":"2021-11-23T17:01:18.389868Z","shell.execute_reply.started":"2021-11-23T17:01:18.382199Z","shell.execute_reply":"2021-11-23T17:01:18.388971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs('./infer_model',exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2021-11-23T17:01:20.453785Z","iopub.execute_input":"2021-11-23T17:01:20.454081Z","iopub.status.idle":"2021-11-23T17:01:20.458650Z","shell.execute_reply.started":"2021-11-23T17:01:20.454050Z","shell.execute_reply":"2021-11-23T17:01:20.457737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"infer_model=modellib.MaskRCNN(mode='inference',config=sartorius_model_cfg,model_dir='./infer_model')","metadata":{"execution":{"iopub.status.busy":"2021-11-23T17:01:20.821154Z","iopub.execute_input":"2021-11-23T17:01:20.822024Z","iopub.status.idle":"2021-11-23T17:01:28.386627Z","shell.execute_reply.started":"2021-11-23T17:01:20.821976Z","shell.execute_reply":"2021-11-23T17:01:28.385827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"infer_model.load_weights(filepath='../input/maskrcnnweights/mask_rcnn_customconfig_0005.h5',by_name=True)","metadata":{"execution":{"iopub.status.busy":"2021-11-23T17:01:28.388532Z","iopub.execute_input":"2021-11-23T17:01:28.389161Z","iopub.status.idle":"2021-11-23T17:01:39.338258Z","shell.execute_reply.started":"2021-11-23T17:01:28.389121Z","shell.execute_reply":"2021-11-23T17:01:39.337264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def check_overlap(msk):\n    msk=msk.astype(np.bool).astype(np.uint8)\n    return np.any(np.sum(msk,axis=-1)>1)\n\ndef fix_overlap(msk):\n    msk=msk.astype(int)\n    msk=np.pad(msk,((0,0),(0,0),(1,0)))#adding dummy background along the depth axis\n    msk=np.argmax(msk,axis=-1)#removing the overlapping mask\n    msk=tf.keras.utils.to_categorical(msk,num_classes=msk.shape[-1])#converting back to multichannel instances\n    msk=msk[:,:,1:]#removing background mask\n    msk=msk.reshape(520*704,msk.shape[-1])\n    msk=msk[:,np.any(msk,axis=0)]#removing zero masks\n    #msk=msk[...,np.any(msk,axis=(0,1))]\n    msk=msk.reshape(520,704,msk.shape[1])\n    \n    return msk","metadata":{"execution":{"iopub.status.busy":"2021-11-23T17:01:55.006713Z","iopub.execute_input":"2021-11-23T17:01:55.007133Z","iopub.status.idle":"2021-11-23T17:01:55.015239Z","shell.execute_reply.started":"2021-11-23T17:01:55.007104Z","shell.execute_reply":"2021-11-23T17:01:55.014340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_names=[]\npred_masks_annot=[]\nfor img in tqdm(os.listdir('../input/sartorius-cell-instance-segmentation/test')):\n    img_name=[]\n    pred_mask_annot=[]\n    img_arr=cv2.imread('../input/sartorius-cell-instance-segmentation/test/'+img)\n    if len(img_arr.shape)!=3:\n        img_arr=cv2.cvtColor(img_arr,cv2.COLOR_GRAY2RGB)\n    if img_arr.shape[-1]==4:\n        img_arr=img_arr[:,:,:3]\n    results=infer_model.detect([img_arr])[0]['masks']\n    print(results.shape,img_arr.shape)\n    if results.shape[-1]==0:\n        img_name.append(img.split('.')[0])\n        pred_mask_annot.append('')\n        print('No mask Found')\n    else:\n        if check_overlap(results):#checking overlaps \n            print('Overlap Found')\n            results=fix_overlap(results)\n            \n        print('Shape after removing overlaps ',results.shape)    \n        for depth in range(results.shape[-1]):\n            pred_mask=results[:,:,depth]\n            #print(pred_mask)\n            pred_mask=pred_mask.astype(np.uint8)\n            \n            img_name.append(img.split('.')[0])\n            pred_mask_annot.append(rle_encode(pred_mask))\n            \n    img_names.extend(img_name)\n    pred_masks_annot.extend(pred_mask_annot)","metadata":{"execution":{"iopub.status.busy":"2021-11-23T17:02:03.329800Z","iopub.execute_input":"2021-11-23T17:02:03.330108Z","iopub.status.idle":"2021-11-23T17:02:29.584524Z","shell.execute_reply.started":"2021-11-23T17:02:03.330078Z","shell.execute_reply":"2021-11-23T17:02:29.583683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_df=pd.DataFrame({'id':img_names,'predicted':pred_masks_annot}).sort_values(by='id')","metadata":{"execution":{"iopub.status.busy":"2021-11-23T17:02:41.448352Z","iopub.execute_input":"2021-11-23T17:02:41.448665Z","iopub.status.idle":"2021-11-23T17:02:41.471314Z","shell.execute_reply.started":"2021-11-23T17:02:41.448620Z","shell.execute_reply":"2021-11-23T17:02:41.470607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_df","metadata":{"execution":{"iopub.status.busy":"2021-11-23T17:02:41.721585Z","iopub.execute_input":"2021-11-23T17:02:41.722229Z","iopub.status.idle":"2021-11-23T17:02:41.744631Z","shell.execute_reply.started":"2021-11-23T17:02:41.722188Z","shell.execute_reply":"2021-11-23T17:02:41.744094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.rmtree('./infer_model')","metadata":{"execution":{"iopub.status.busy":"2021-11-23T15:22:37.436805Z","iopub.execute_input":"2021-11-23T15:22:37.437026Z","iopub.status.idle":"2021-11-23T15:22:37.440829Z","shell.execute_reply.started":"2021-11-23T15:22:37.436995Z","shell.execute_reply":"2021-11-23T15:22:37.440055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_df.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-11-23T15:22:37.442237Z","iopub.execute_input":"2021-11-23T15:22:37.442726Z","iopub.status.idle":"2021-11-23T15:22:37.452244Z","shell.execute_reply.started":"2021-11-23T15:22:37.442693Z","shell.execute_reply":"2021-11-23T15:22:37.451555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}