{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"markdown","source":"This kernel contains:\n* How to create submission.csv"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","collapsed":true,"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":false},"cell_type":"markdown","source":"This kernel does NOT contains:\n* How to train\n* How to understand/download/use dataset\n* EDA"},{"metadata":{},"cell_type":"markdown","source":"Mask R-CNN  \nhttps://github.com/matterport/Mask_RCNN  \nhttps://github.com/matterport/Mask_RCNN/blob/master/samples/demo.ipynb"},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\nimport os\nimport sys\nfrom tqdm import tqdm\nfrom pathlib import Path\nimport tensorflow as tf\nimport skimage.io\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# prepare mask_rcnn"},{"metadata":{"trusted":true},"cell_type":"code","source":"# https://www.kaggle.com/pednoi/training-mask-r-cnn-to-be-a-fashionista-lb-0-07\n\n!git clone https://www.github.com/matterport/Mask_RCNN.git\nos.chdir('Mask_RCNN')\n\n!rm -rf .git # to prevent an error when the kernel is committed\n!rm -rf images assets # to prevent displaying images at the bottom of a kernel","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"DATA_DIR = Path('/kaggle/input')\nROOT_DIR = Path('/kaggle/working')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sys.path.append(ROOT_DIR/'Mask_RCNN')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install pycocotools","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from mrcnn.config import Config\nfrom mrcnn import utils\nimport mrcnn.model as modellib\nfrom mrcnn import visualize\nfrom mrcnn.model import log","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!wget https://github.com/matterport/Mask_RCNN/releases/download/v2.0/mask_rcnn_coco.h5","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"COCO_MODEL_PATH = 'mask_rcnn_coco.h5'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Import COCO config\nsys.path.append(os.path.join(ROOT_DIR, \"Mask_RCNN/samples/coco/\"))  # To find local version\nimport coco","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class InferenceConfig(coco.CocoConfig):\n    # Set batch size to 1 since we'll be running inference on\n    # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU\n    GPU_COUNT = 1\n    IMAGES_PER_GPU = 1\n    IMAGE_MIN_DIM = 256\n    IMAGE_MAX_DIM = 256\n    \nconfig = InferenceConfig()\nconfig.display()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Create model object in inference mode.\nmodel = modellib.MaskRCNN(mode=\"inference\", config=config, model_dir=ROOT_DIR)\n\n# Load weights trained on MS-COCO\nmodel.load_weights(COCO_MODEL_PATH, by_name=True)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# inference"},{"metadata":{"trusted":true},"cell_type":"code","source":"# COCO Class names\n# Index of the class in the list is its ID. For example, to get ID of\n# the teddy bear class, use: class_names.index('teddy bear')\nclass_names = ['BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane',\n               'bus', 'train', 'truck', 'boat', 'traffic light',\n               'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird',\n               'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear',\n               'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie',\n               'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',\n               'kite', 'baseball bat', 'baseball glove', 'skateboard',\n               'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup',\n               'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',\n               'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',\n               'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed',\n               'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote',\n               'keyboard', 'cell phone', 'microwave', 'oven', 'toaster',\n               'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors',\n               'teddy bear', 'hair drier', 'toothbrush']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"IMAGE_DIR = \"/kaggle/input/test/\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.chdir('/kaggle/')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.listdir(\"./input\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!wget https://storage.googleapis.com/openimages/challenge_2019/challenge-2019-classes-description-segmentable.csv","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class_lookup_df = pd.read_csv(\"./challenge-2019-classes-description-segmentable.csv\", header=None)\nempty_submission_df = pd.read_csv(\"input/sample_empty_submission.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# we have to convert coco classes to this competition's one.\n\nclass_lookup_df.columns = [\"encoded_label\",\"label\"]\nclass_lookup_df['label'] = class_lookup_df['label'].str.lower()\nclass_lookup_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"empty_submission_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_image = \"80155d58d0ee19bd.jpg\"\nimage = skimage.io.imread(os.path.join(IMAGE_DIR, sample_image))\nresults = model.detect([image], verbose=1)\n\n# Visualize results\nr = results[0]\nprint( class_names[r['class_ids'][0]])\n\nvisualize.display_instances(image, r['rois'], r['masks'], r['class_ids'], class_names, r['scores'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"r['masks'].shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.imshow(r['masks'][:,:,0])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"See:  \nhttps://www.kaggle.com/c/open-images-2019-instance-segmentation/overview/evaluation"},{"metadata":{"trusted":true},"cell_type":"code","source":"import base64\nimport numpy as np\nfrom pycocotools import _mask as coco_mask\nimport typing as t\nimport zlib\n\ndef encode_binary_mask(mask: np.ndarray) -> t.Text:\n    \"\"\"Converts a binary mask into OID challenge encoding ascii text.\"\"\"\n\n    # check input mask --\n    if mask.dtype != np.bool:\n        raise ValueError(\"encode_binary_mask expects a binary mask, received dtype == %s\" % mask.dtype)\n\n    mask = np.squeeze(mask)\n    if len(mask.shape) != 2:\n        raise ValueError(\"encode_binary_mask expects a 2d mask, received shape == %s\" % mask.shape)\n\n    # convert input mask to expected COCO API input --\n    mask_to_encode = mask.reshape(mask.shape[0], mask.shape[1], 1)\n    mask_to_encode = mask_to_encode.astype(np.uint8)\n    mask_to_encode = np.asfortranarray(mask_to_encode)\n\n    # RLE encode mask --\n    encoded_mask = coco_mask.encode(mask_to_encode)[0][\"counts\"]\n\n    # compress and base64 encoding --\n    binary_str = zlib.compress(encoded_mask, zlib.Z_BEST_COMPRESSION)\n    base64_str = base64.b64encode(binary_str)\n    return base64_str","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ImageID_list = []\nImageWidth_list = []\nImageHeight_list = []\nPredictionString_list = []\n\nfor num, row in tqdm(empty_submission_df.iterrows(), total=len(empty_submission_df)):\n    filename = row[\"ImageID\"] + \".jpg\"\n   \n    image = skimage.io.imread(os.path.join(IMAGE_DIR, filename))\n    results = model.detect([image])\n    r = results[0]\n    \n    height = image.shape[0]\n    width  = image.shape[1]\n        \n    PredictionString = \"\"\n    \n    for i in range(len(r[\"class_ids\"])):        \n        class_id = r[\"class_ids\"][i]\n        roi = r[\"rois\"][i]\n        mask = r[\"masks\"][:,:,i]\n        confidence = r[\"scores\"][i]\n        \n        encoded_mask = encode_binary_mask(mask)\n        \n        labelname = class_names[r['class_ids'][0]]\n        if class_lookup_df[class_lookup_df[\"label\"] == labelname].shape[0] == 0:\n            # no match label\n            continue\n        \n        encoded_label = class_lookup_df[class_lookup_df[\"label\"] == labelname][\"encoded_label\"].item()\n\n        PredictionString += encoded_label \n        PredictionString += \" \"\n        PredictionString += str(confidence)\n        PredictionString += \" \"\n        PredictionString += encoded_mask.decode()\n        PredictionString += \" \"\n        \n    ImageID_list.append(row[\"ImageID\"])\n    ImageWidth_list.append(width)\n    ImageHeight_list.append(height)\n    PredictionString_list.append(PredictionString)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"results=pd.DataFrame({\"ImageID\":ImageID_list,\n                      \"ImageWidth\":ImageWidth_list,\n                      \"ImageHeight\":ImageHeight_list,\n                      \"PredictionString\":PredictionString_list\n                     })","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"results.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"results.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.chdir('/kaggle/working')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"results.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}