{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":20009,"databundleVersionId":1097631,"sourceType":"competition"}],"dockerImageVersionId":29926,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# MASK RCNN\n","metadata":{}},{"cell_type":"markdown","source":"### We need to downgrade to TF 1.x in order to use MaskRCNN model","metadata":{}},{"cell_type":"code","source":"!pip install tensorflow==1.15","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-08-26T06:26:59.554730Z","iopub.execute_input":"2024-08-26T06:26:59.555215Z","iopub.status.idle":"2024-08-26T06:27:07.799105Z","shell.execute_reply.started":"2024-08-26T06:26:59.555173Z","shell.execute_reply":"2024-08-26T06:27:07.797407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow.compat.v1 as tf\ntf.disable_v2_behavior()\nimport tensorflow_hub as hub","metadata":{"execution":{"iopub.status.busy":"2024-08-26T06:27:07.802701Z","iopub.execute_input":"2024-08-26T06:27:07.803294Z","iopub.status.idle":"2024-08-26T06:27:07.809452Z","shell.execute_reply.started":"2024-08-26T06:27:07.803233Z","shell.execute_reply":"2024-08-26T06:27:07.808231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(tf.__version__)","metadata":{"execution":{"iopub.status.busy":"2024-08-26T06:27:07.811460Z","iopub.execute_input":"2024-08-26T06:27:07.811928Z","iopub.status.idle":"2024-08-26T06:27:07.824143Z","shell.execute_reply.started":"2024-08-26T06:27:07.811876Z","shell.execute_reply":"2024-08-26T06:27:07.822623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np \nimport pandas as pd \n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n    #for filename in filenames:\n        #print(os.path.join(dirname, filename))\n\n# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-08-26T06:27:07.825820Z","iopub.execute_input":"2024-08-26T06:27:07.826218Z","iopub.status.idle":"2024-08-26T06:27:07.837359Z","shell.execute_reply.started":"2024-08-26T06:27:07.826180Z","shell.execute_reply":"2024-08-26T06:27:07.836013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\nfrom pathlib import Path\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport itertools\n%matplotlib inline\nimport cv2\nfrom collections import defaultdict\nfrom IPython.display import SVG\nfrom tqdm.notebook import tqdm\nfrom PIL import Image","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2024-08-26T06:27:07.842152Z","iopub.execute_input":"2024-08-26T06:27:07.842639Z","iopub.status.idle":"2024-08-26T06:27:07.858160Z","shell.execute_reply.started":"2024-08-26T06:27:07.842583Z","shell.execute_reply":"2024-08-26T06:27:07.857021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#import tensorflow as tf\nfrom tensorflow.python.client import device_lib\nprint(device_lib.list_local_devices())","metadata":{"execution":{"iopub.status.busy":"2024-08-26T06:27:07.861872Z","iopub.execute_input":"2024-08-26T06:27:07.862389Z","iopub.status.idle":"2024-08-26T06:27:07.873205Z","shell.execute_reply.started":"2024-08-26T06:27:07.862339Z","shell.execute_reply":"2024-08-26T06:27:07.871785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://www.github.com/matterport/Mask_RCNN.git\nos.chdir('Mask_RCNN')","metadata":{"execution":{"iopub.status.busy":"2024-08-26T06:27:07.875523Z","iopub.execute_input":"2024-08-26T06:27:07.875998Z","iopub.status.idle":"2024-08-26T06:27:17.826497Z","shell.execute_reply.started":"2024-08-26T06:27:07.875950Z","shell.execute_reply":"2024-08-26T06:27:17.825051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget --quiet https://github.com/matterport/Mask_RCNN/releases/download/v2.0/mask_rcnn_coco.h5\n!ls -lh mask_rcnn_coco.h5","metadata":{"execution":{"iopub.status.busy":"2024-08-26T06:27:17.828482Z","iopub.execute_input":"2024-08-26T06:27:17.828856Z","iopub.status.idle":"2024-08-26T06:27:24.185183Z","shell.execute_reply.started":"2024-08-26T06:27:17.828814Z","shell.execute_reply":"2024-08-26T06:27:24.183634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\n# Root directory of the project\n#ROOT_DIR = os.path.abspath(\"../../\")\nROOT_DIR = ('../kaggle/working/')\n# Import Mask RCNN\nsys.path.append(ROOT_DIR)  # To find local version of the library\nfrom mrcnn.config import Config\nfrom mrcnn import utils\nimport mrcnn.model as modellib\nfrom mrcnn import visualize\nfrom mrcnn.model import log\n\n\n# Directory to save logs and trained model\n#MODEL_DIR = os.path.join(ROOT_DIR, \"../output/kaggle/working/logs\")\nMODEL_DIR = ('../kaggle/working/Mask_RCNN')\n\n# Local path to trained weights file\n#COCO_MODEL_PATH = os.path.join(MODEL_DIR, \"mask_rcnn_coco.h5\")\n# Download COCO trained weights from Releases if needed\n#if not os.path.exists(COCO_MODEL_PATH):\n#    utils.download_trained_weights(COCO_MODEL_PATH)","metadata":{"execution":{"iopub.status.busy":"2024-08-26T06:27:24.187667Z","iopub.execute_input":"2024-08-26T06:27:24.188085Z","iopub.status.idle":"2024-08-26T06:27:24.195788Z","shell.execute_reply.started":"2024-08-26T06:27:24.188041Z","shell.execute_reply":"2024-08-26T06:27:24.194567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DetectorConfig(Config):\n   \n    # Configuration name  \n    NAME = 'instances'\n    \n    # We have one GPU available, but can put multiple images on it\n    # Batch size is 8 (GPUs * images/GPU)\n    GPU_COUNT = 1\n    IMAGES_PER_GPU = 8\n    \n    BACKBONE = 'resnet101' #'resnet101' would be another option\n    \n    NUM_CLASSES = 2  # background + pneumonia classes -->semantic segmentation\n    \n    IMAGE_MIN_DIM = 256\n    IMAGE_MAX_DIM = 256\n    TRAIN_ROIS_PER_IMAGE = 32 \n    MAX_GT_INSTANCES = 4 \n    DETECTION_MAX_INSTANCES = 3\n    DETECTION_MIN_CONFIDENCE = 0.78  \n    DETECTION_NMS_THRESHOLD = 0.01 \n    STEPS_PER_EPOCH = 200 \n    \nconfig = DetectorConfig()\nconfig.display()","metadata":{"execution":{"iopub.status.busy":"2024-08-26T06:27:24.197711Z","iopub.execute_input":"2024-08-26T06:27:24.198202Z","iopub.status.idle":"2024-08-26T06:27:24.221073Z","shell.execute_reply.started":"2024-08-26T06:27:24.198161Z","shell.execute_reply":"2024-08-26T06:27:24.220130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sys.path.append(os.path.join('Mask_RCNN'))  # To find local version of the library\nfrom mrcnn.config import Config\nfrom mrcnn import utils\nimport mrcnn.model as modellib\nfrom mrcnn import visualize\nfrom mrcnn.model import log\n \n#!cd coco_weight && wget --quiet https://github.com/matterport/Mask_RCNN/releases/download/v2.0/mask_rcnn_coco.h5\n#!ls -lh mask_rcnn_coco.h5\nCOCO_WEIGHTS_PATH = \"/kaggle/working/Mask_RCNN/mask_rcnn_coco.h5\"","metadata":{"execution":{"iopub.status.busy":"2024-08-26T06:27:24.222559Z","iopub.execute_input":"2024-08-26T06:27:24.222898Z","iopub.status.idle":"2024-08-26T06:27:24.233248Z","shell.execute_reply.started":"2024-08-26T06:27:24.222862Z","shell.execute_reply":"2024-08-26T06:27:24.232007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MODEL_DIR\nCOCO_WEIGHTS_PATH","metadata":{"execution":{"iopub.status.busy":"2024-08-26T06:27:24.235085Z","iopub.execute_input":"2024-08-26T06:27:24.235563Z","iopub.status.idle":"2024-08-26T06:27:24.247602Z","shell.execute_reply.started":"2024-08-26T06:27:24.235525Z","shell.execute_reply":"2024-08-26T06:27:24.246545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create model in training mode\nmodel = modellib.MaskRCNN(mode='training', config=config, model_dir=MODEL_DIR)\nmodel.load_weights(COCO_WEIGHTS_PATH, by_name=True, exclude=[\n    \"mrcnn_class_logits\", \"mrcnn_bbox_fc\",\n    \"mrcnn_bbox\", \"mrcnn_mask\"])","metadata":{"execution":{"iopub.status.busy":"2024-08-26T06:27:24.249376Z","iopub.execute_input":"2024-08-26T06:27:24.249816Z","iopub.status.idle":"2024-08-26T06:27:54.782740Z","shell.execute_reply.started":"2024-08-26T06:27:24.249778Z","shell.execute_reply":"2024-08-26T06:27:54.781598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path = '/kaggle/input/open-images-instance-segmentation-rvc-2020/test/'\nfile_list = os.listdir(image_path)\ntype(file_list)\ntest_df = pd.DataFrame(file_list)\ntest_df.rename(columns = {0:'image_ids'}, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2024-08-26T06:27:54.784727Z","iopub.execute_input":"2024-08-26T06:27:54.785241Z","iopub.status.idle":"2024-08-26T06:27:54.855869Z","shell.execute_reply.started":"2024-08-26T06:27:54.785190Z","shell.execute_reply":"2024-08-26T06:27:54.854831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = plt.imread( image_path + '00001a21632de752.jpg')\nplt.imshow(img)\nIMG_SHAPE = img.shape\nprint (IMG_SHAPE)","metadata":{"execution":{"iopub.status.busy":"2024-08-26T06:27:54.857474Z","iopub.execute_input":"2024-08-26T06:27:54.857945Z","iopub.status.idle":"2024-08-26T06:27:55.180203Z","shell.execute_reply.started":"2024-08-26T06:27:54.857899Z","shell.execute_reply":"2024-08-26T06:27:55.179043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def display_images(flnames):\n    f, ax = plt.subplots(3,4, figsize=(20,40))\n    image_path = '/kaggle/input/open-images-instance-segmentation-rvc-2020/test/'\n    #for img in path.iterdir():\n\n    for i, fl in enumerate(flnames):\n        ax[i//4,i%4].set_axis_off()\n        img = cv2.imread( image_path+fl)\n        ax[i//4,i%4].imshow(img)\n        ax[i//4,i%4].set_title(f'{fl}')\n        f.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2024-08-26T06:27:55.181794Z","iopub.execute_input":"2024-08-26T06:27:55.182281Z","iopub.status.idle":"2024-08-26T06:27:55.192220Z","shell.execute_reply.started":"2024-08-26T06:27:55.182233Z","shell.execute_reply":"2024-08-26T06:27:55.190868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_images(list(test_df['image_ids'][30:42]))","metadata":{"execution":{"iopub.status.busy":"2024-08-26T06:27:55.193797Z","iopub.execute_input":"2024-08-26T06:27:55.194320Z","iopub.status.idle":"2024-08-26T06:27:59.146629Z","shell.execute_reply.started":"2024-08-26T06:27:55.194267Z","shell.execute_reply":"2024-08-26T06:27:59.144394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nfig, axes = plt.subplots(10, figsize=(20,20))\nfor i in range(10):\n    img = cv2.imread( image_path+test_df['image_ids'][i])\n    axes[i].imshow(img)\n''' ","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-08-26T06:27:59.148480Z","iopub.execute_input":"2024-08-26T06:27:59.148892Z","iopub.status.idle":"2024-08-26T06:27:59.155513Z","shell.execute_reply.started":"2024-08-26T06:27:59.148854Z","shell.execute_reply":"2024-08-26T06:27:59.154566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resize_image(image):\n    img = cv2.imread(image_path)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    img = cv2.resize(img, (self.IMAGE_SIZE, self.IMAGE_SIZE), interpolation=cv2.INTER_AREA)  \n    return img","metadata":{"execution":{"iopub.status.busy":"2024-08-26T06:27:59.157005Z","iopub.execute_input":"2024-08-26T06:27:59.157413Z","iopub.status.idle":"2024-08-26T06:27:59.586056Z","shell.execute_reply.started":"2024-08-26T06:27:59.157375Z","shell.execute_reply":"2024-08-26T06:27:59.584999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head(5)","metadata":{"execution":{"iopub.status.busy":"2024-08-26T06:27:59.587790Z","iopub.execute_input":"2024-08-26T06:27:59.588233Z","iopub.status.idle":"2024-08-26T06:27:59.604007Z","shell.execute_reply.started":"2024-08-26T06:27:59.588195Z","shell.execute_reply":"2024-08-26T06:27:59.602831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.to_csv('/kaggle/working/test_df.csv')","metadata":{"execution":{"iopub.status.busy":"2024-08-26T06:27:59.605519Z","iopub.execute_input":"2024-08-26T06:27:59.605978Z","iopub.status.idle":"2024-08-26T06:27:59.820445Z","shell.execute_reply.started":"2024-08-26T06:27:59.605931Z","shell.execute_reply":"2024-08-26T06:27:59.819235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# example of inference with a pre-trained coco model\nfrom keras.preprocessing.image import load_img\nfrom keras.preprocessing.image import img_to_array\nfrom mrcnn.visualize import display_instances\nfrom mrcnn.config import Config\nfrom mrcnn.model import MaskRCNN\n \n# define 81 classes that the coco model knows about\n\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']\n\n#class_names = ['car','Car'] \n# define the test configuration\nclass ImgConfig(Config):\n     NAME = \"images\"\n     GPU_COUNT = 1\n     IMAGES_PER_GPU = 1\n     NUM_CLASSES = 1 + 80\n\n# define the model\nrcnn = MaskRCNN(mode='inference', model_dir=MODEL_DIR, config=ImgConfig())\n# load coco model weights\nrcnn.load_weights(COCO_WEIGHTS_PATH, by_name=True)","metadata":{"execution":{"iopub.status.busy":"2024-08-26T06:27:59.822539Z","iopub.execute_input":"2024-08-26T06:27:59.823060Z","iopub.status.idle":"2024-08-26T06:28:27.334511Z","shell.execute_reply.started":"2024-08-26T06:27:59.823010Z","shell.execute_reply":"2024-08-26T06:28:27.333152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load image\nimg = load_img(image_path + test_df['image_ids'][15])\nimg = img_to_array(img)\n# make prediction\nresults = rcnn.detect([img], verbose=0)\n# get dictionary for first prediction\nr = results[0]\n# show photo with bounding boxes, masks, class labels and scores\n#if (class_names == 'car'):\ndisplay_instances(img, r['rois'], r['masks'], r['class_ids'], class_names, r['scores'])","metadata":{"execution":{"iopub.status.busy":"2024-08-26T06:28:27.336160Z","iopub.execute_input":"2024-08-26T06:28:27.336483Z","iopub.status.idle":"2024-08-26T06:28:56.956583Z","shell.execute_reply.started":"2024-08-26T06:28:27.336452Z","shell.execute_reply":"2024-08-26T06:28:56.954681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Lets try more images","metadata":{}},{"cell_type":"code","source":"# load image\nimg = load_img(image_path + test_df['image_ids'][19])\nimg = img_to_array(img)\n# make prediction\nresults = rcnn.detect([img], verbose=0)\n# get dictionary for first prediction\nr = results[0]\n# show photo with bounding boxes, masks, class labels and scores\n#if (class_names == 'car'):\ndisplay_instances(img, r['rois'], r['masks'], r['class_ids'], class_names, r['scores'])","metadata":{"execution":{"iopub.status.busy":"2024-08-26T06:28:56.958315Z","iopub.execute_input":"2024-08-26T06:28:56.958773Z","iopub.status.idle":"2024-08-26T06:29:05.817456Z","shell.execute_reply.started":"2024-08-26T06:28:56.958732Z","shell.execute_reply":"2024-08-26T06:29:05.815758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load image\nimg = load_img(image_path + test_df['image_ids'][260])\nimg = img_to_array(img)\n# make prediction\nresults = rcnn.detect([img], verbose=0)\n# get dictionary for first prediction\nr = results[0]\n# show photo with bounding boxes, masks, class labels and scores\n#if (class_names == 'car'):\ndisplay_instances(img, r['rois'], r['masks'], r['class_ids'], class_names, r['scores'])","metadata":{"execution":{"iopub.status.busy":"2024-08-26T06:29:05.819288Z","iopub.execute_input":"2024-08-26T06:29:05.819736Z","iopub.status.idle":"2024-08-26T06:29:18.148982Z","shell.execute_reply.started":"2024-08-26T06:29:05.819686Z","shell.execute_reply":"2024-08-26T06:29:18.147232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load image\nimg = load_img(image_path + test_df['image_ids'][92])\nimg = img_to_array(img)\n# make prediction\nresults = rcnn.detect([img], verbose=0)\n# get dictionary for first prediction\nr = results[0]\n# show photo with bounding boxes, masks, class labels and scores\n#if (class_names == 'car'):\ndisplay_instances(img, r['rois'], r['masks'], r['class_ids'], class_names, r['scores'])","metadata":{"execution":{"iopub.status.busy":"2024-08-26T06:29:18.150789Z","iopub.execute_input":"2024-08-26T06:29:18.151346Z","iopub.status.idle":"2024-08-26T06:29:26.133565Z","shell.execute_reply.started":"2024-08-26T06:29:18.151294Z","shell.execute_reply":"2024-08-26T06:29:26.132195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}