{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":15043,"databundleVersionId":862223,"sourceType":"competition"},{"sourceId":8562655,"sourceType":"datasetVersion","datasetId":5118503},{"sourceId":8562855,"sourceType":"datasetVersion","datasetId":5118686}],"dockerImageVersionId":28772,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"This kernel contains:\n* How to create submission.csv","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true}},{"cell_type":"markdown","source":"This kernel does NOT contains:\n* How to train\n* How to understand/download/use dataset\n* EDA","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0"}},{"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":{}},{"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","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:44:22.480145Z","iopub.execute_input":"2024-05-30T17:44:22.480447Z","iopub.status.idle":"2024-05-30T17:44:25.428929Z","shell.execute_reply.started":"2024-05-30T17:44:22.480399Z","shell.execute_reply":"2024-05-30T17:44:25.428225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# prepare mask_rcnn","metadata":{}},{"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","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:44:25.431091Z","iopub.execute_input":"2024-05-30T17:44:25.431456Z","iopub.status.idle":"2024-05-30T17:44:33.232358Z","shell.execute_reply.started":"2024-05-30T17:44:25.431388Z","shell.execute_reply":"2024-05-30T17:44:33.231298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA_DIR = Path('/kaggle/input')\nROOT_DIR = Path('/kaggle/working')","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:44:33.233937Z","iopub.execute_input":"2024-05-30T17:44:33.234203Z","iopub.status.idle":"2024-05-30T17:44:33.238158Z","shell.execute_reply.started":"2024-05-30T17:44:33.234159Z","shell.execute_reply":"2024-05-30T17:44:33.237422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sys.path.append(ROOT_DIR/'Mask_RCNN')","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:44:33.239722Z","iopub.execute_input":"2024-05-30T17:44:33.239985Z","iopub.status.idle":"2024-05-30T17:44:33.250989Z","shell.execute_reply.started":"2024-05-30T17:44:33.239936Z","shell.execute_reply":"2024-05-30T17:44:33.250257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pycocotools","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:44:33.254031Z","iopub.execute_input":"2024-05-30T17:44:33.254434Z","iopub.status.idle":"2024-05-30T17:45:02.730217Z","shell.execute_reply.started":"2024-05-30T17:44:33.254249Z","shell.execute_reply":"2024-05-30T17:45:02.72946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:45:02.73233Z","iopub.execute_input":"2024-05-30T17:45:02.732574Z","iopub.status.idle":"2024-05-30T17:45:02.856958Z","shell.execute_reply.started":"2024-05-30T17:45:02.732534Z","shell.execute_reply":"2024-05-30T17:45:02.856056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget https://github.com/matterport/Mask_RCNN/releases/download/v2.0/mask_rcnn_coco.h5","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:45:02.858385Z","iopub.execute_input":"2024-05-30T17:45:02.858713Z","iopub.status.idle":"2024-05-30T17:45:05.665111Z","shell.execute_reply.started":"2024-05-30T17:45:02.858653Z","shell.execute_reply":"2024-05-30T17:45:05.66415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"COCO_MODEL_PATH = 'mask_rcnn_coco.h5'","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:45:05.666774Z","iopub.execute_input":"2024-05-30T17:45:05.667049Z","iopub.status.idle":"2024-05-30T17:45:05.671061Z","shell.execute_reply.started":"2024-05-30T17:45:05.666998Z","shell.execute_reply":"2024-05-30T17:45:05.670275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:45:05.672172Z","iopub.execute_input":"2024-05-30T17:45:05.672439Z","iopub.status.idle":"2024-05-30T17:45:05.8853Z","shell.execute_reply.started":"2024-05-30T17:45:05.672392Z","shell.execute_reply":"2024-05-30T17:45:05.884705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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()","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:45:05.886761Z","iopub.execute_input":"2024-05-30T17:45:05.88709Z","iopub.status.idle":"2024-05-30T17:45:05.89634Z","shell.execute_reply.started":"2024-05-30T17:45:05.887029Z","shell.execute_reply":"2024-05-30T17:45:05.895422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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)","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:45:05.897511Z","iopub.execute_input":"2024-05-30T17:45:05.89777Z","iopub.status.idle":"2024-05-30T17:45:19.193952Z","shell.execute_reply.started":"2024-05-30T17:45:05.89772Z","shell.execute_reply":"2024-05-30T17:45:19.193064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# inference","metadata":{}},{"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']","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:45:19.195187Z","iopub.execute_input":"2024-05-30T17:45:19.19544Z","iopub.status.idle":"2024-05-30T17:45:19.20388Z","shell.execute_reply.started":"2024-05-30T17:45:19.1954Z","shell.execute_reply":"2024-05-30T17:45:19.203023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGE_DIR = \"/kaggle/input/test/\"","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:45:19.205362Z","iopub.execute_input":"2024-05-30T17:45:19.205665Z","iopub.status.idle":"2024-05-30T17:45:19.219652Z","shell.execute_reply.started":"2024-05-30T17:45:19.20561Z","shell.execute_reply":"2024-05-30T17:45:19.218836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.chdir('/kaggle/')","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:45:19.220697Z","iopub.execute_input":"2024-05-30T17:45:19.220911Z","iopub.status.idle":"2024-05-30T17:45:19.231961Z","shell.execute_reply.started":"2024-05-30T17:45:19.220874Z","shell.execute_reply":"2024-05-30T17:45:19.230713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(\"./input\")","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:45:19.233371Z","iopub.execute_input":"2024-05-30T17:45:19.23386Z","iopub.status.idle":"2024-05-30T17:45:19.24472Z","shell.execute_reply.started":"2024-05-30T17:45:19.233805Z","shell.execute_reply":"2024-05-30T17:45:19.243684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget https://storage.googleapis.com/openimages/challenge_2019/challenge-2019-classes-description-segmentable.csv","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:45:19.245937Z","iopub.execute_input":"2024-05-30T17:45:19.246423Z","iopub.status.idle":"2024-05-30T17:45:20.462654Z","shell.execute_reply.started":"2024-05-30T17:45:19.246385Z","shell.execute_reply":"2024-05-30T17:45:20.461758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_lookup_df = pd.read_csv(\"./challenge-2019-classes-description-segmentable.csv\", header=None)\n# empty_submission_df = pd.read_csv(\"input/sample_empty_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:45:52.16082Z","iopub.execute_input":"2024-05-30T17:45:52.16114Z","iopub.status.idle":"2024-05-30T17:45:52.168956Z","shell.execute_reply.started":"2024-05-30T17:45:52.161097Z","shell.execute_reply":"2024-05-30T17:45:52.168184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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()","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:45:54.431764Z","iopub.execute_input":"2024-05-30T17:45:54.432113Z","iopub.status.idle":"2024-05-30T17:45:54.451917Z","shell.execute_reply.started":"2024-05-30T17:45:54.432053Z","shell.execute_reply":"2024-05-30T17:45:54.451106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_image = \"/kaggle/input/image-to-read/do_me.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'])","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:53:02.366182Z","iopub.execute_input":"2024-05-30T17:53:02.366543Z","iopub.status.idle":"2024-05-30T17:53:03.146025Z","shell.execute_reply.started":"2024-05-30T17:53:02.366494Z","shell.execute_reply":"2024-05-30T17:53:03.145245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_image = \"/kaggle/input/hmmmmmmm/busjet.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'])","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:49:54.237892Z","iopub.execute_input":"2024-05-30T17:49:54.238291Z","iopub.status.idle":"2024-05-30T17:49:55.739255Z","shell.execute_reply.started":"2024-05-30T17:49:54.238221Z","shell.execute_reply":"2024-05-30T17:49:55.738383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r['masks'].shape","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:50:16.437747Z","iopub.execute_input":"2024-05-30T17:50:16.438066Z","iopub.status.idle":"2024-05-30T17:50:16.44388Z","shell.execute_reply.started":"2024-05-30T17:50:16.438015Z","shell.execute_reply":"2024-05-30T17:50:16.442869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(r['masks'][:,:,0])","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:50:17.241733Z","iopub.execute_input":"2024-05-30T17:50:17.242037Z","iopub.status.idle":"2024-05-30T17:50:17.471536Z","shell.execute_reply.started":"2024-05-30T17:50:17.241994Z","shell.execute_reply":"2024-05-30T17:50:17.470666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"See:  \nhttps://www.kaggle.com/c/open-images-2019-instance-segmentation/overview/evaluation","metadata":{}},{"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","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:50:19.831373Z","iopub.execute_input":"2024-05-30T17:50:19.831669Z","iopub.status.idle":"2024-05-30T17:50:19.842293Z","shell.execute_reply.started":"2024-05-30T17:50:19.831627Z","shell.execute_reply":"2024-05-30T17:50:19.841377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results=pd.DataFrame({\"ImageID\":ImageID_list,\n                      \"ImageWidth\":ImageWidth_list,\n                      \"ImageHeight\":ImageHeight_list,\n                      \"PredictionString\":PredictionString_list\n                     })","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.chdir('/kaggle/working')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results.to_csv(\"submission.csv\", index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}