{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Mask R-CNN Demo\n", "\n", "A quick intro to using the pre-trained model to detect and segment objects." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "scrolled": true }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\hideoshiraishi\\anaconda3\\envs\\mask_rcnn\\lib\\site-packages\\h5py\\__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n", " from ._conv import register_converters as _register_converters\n", "Using TensorFlow backend.\n" ] } ], "source": [ "import os\n", "import sys\n", "import random\n", "import math\n", "import numpy as np\n", "import skimage.io\n", "import matplotlib\n", "import matplotlib.pyplot as plt\n", "\n", "# Root directory of the project\n", "ROOT_DIR = os.path.abspath(\"../\")\n", "\n", "# Import Mask RCNN\n", "sys.path.append(ROOT_DIR) # To find local version of the library\n", "from mrcnn import utils\n", "import mrcnn.model as modellib\n", "from mrcnn import visualize\n", "# Import COCO config\n", "sys.path.append(os.path.join(ROOT_DIR, \"samples/coco/\")) # To find local version\n", "import coco\n", "\n", "%matplotlib inline \n", "\n", "# Directory to save logs and trained model\n", "MODEL_DIR = os.path.join(ROOT_DIR, \"logs\")\n", "\n", "# Local path to trained weights file\n", "# COCO_MODEL_PATH = os.path.join(ROOT_DIR, \"mask_rcnn_coco.h5\")\n", "# Pre-trained file from Mstfakts/Building-Detection-MaskRCNN\n", "COCO_MODEL_PATH = os.path.join(ROOT_DIR, \"mask_rcnn_spacenet_0151.h5\")\n", "\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)\n", "\n", "# Directory of images to run detection on\n", "IMAGE_DIR = os.path.join(ROOT_DIR, \"images\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Configurations\n", "\n", "We'll be using a model trained on the MS-COCO dataset. The configurations of this model are in the ```CocoConfig``` class in ```coco.py```.\n", "\n", "For inferencing, modify the configurations a bit to fit the task. To do so, sub-class the ```CocoConfig``` class and override the attributes you need to change." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Configurations:\n", "BACKBONE resnet101\n", "BACKBONE_STRIDES [4, 8, 16, 32, 64]\n", "BATCH_SIZE 1\n", "BBOX_STD_DEV [0.1 0.1 0.2 0.2]\n", "COMPUTE_BACKBONE_SHAPE None\n", "DETECTION_MAX_INSTANCES 100\n", "DETECTION_MIN_CONFIDENCE 0.7\n", "DETECTION_NMS_THRESHOLD 0.3\n", "FPN_CLASSIF_FC_LAYERS_SIZE 1024\n", "GPU_COUNT 1\n", "GRADIENT_CLIP_NORM 5.0\n", "IMAGES_PER_GPU 1\n", "IMAGE_CHANNEL_COUNT 3\n", "IMAGE_MAX_DIM 1024\n", "IMAGE_META_SIZE 93\n", "IMAGE_MIN_DIM 800\n", "IMAGE_MIN_SCALE 0\n", "IMAGE_RESIZE_MODE square\n", "IMAGE_SHAPE [1024 1024 3]\n", "LEARNING_MOMENTUM 0.9\n", "LEARNING_RATE 0.001\n", "LOSS_WEIGHTS {'rpn_class_loss': 1.0, 'rpn_bbox_loss': 1.0, 'mrcnn_class_loss': 1.0, 'mrcnn_bbox_loss': 1.0, 'mrcnn_mask_loss': 1.0}\n", "MASK_POOL_SIZE 14\n", "MASK_SHAPE [28, 28]\n", "MAX_GT_INSTANCES 100\n", "MEAN_PIXEL [123.7 116.8 103.9]\n", "MINI_MASK_SHAPE (56, 56)\n", "NAME coco\n", "NUM_CLASSES 81\n", "POOL_SIZE 7\n", "POST_NMS_ROIS_INFERENCE 1000\n", "POST_NMS_ROIS_TRAINING 2000\n", "PRE_NMS_LIMIT 6000\n", "ROI_POSITIVE_RATIO 0.33\n", "RPN_ANCHOR_RATIOS [0.5, 1, 2]\n", "RPN_ANCHOR_SCALES (32, 64, 128, 256, 512)\n", "RPN_ANCHOR_STRIDE 1\n", "RPN_BBOX_STD_DEV [0.1 0.1 0.2 0.2]\n", "RPN_NMS_THRESHOLD 0.7\n", "RPN_TRAIN_ANCHORS_PER_IMAGE 256\n", "STEPS_PER_EPOCH 1000\n", "TOP_DOWN_PYRAMID_SIZE 256\n", "TRAIN_BN False\n", "TRAIN_ROIS_PER_IMAGE 200\n", "USE_MINI_MASK True\n", "USE_RPN_ROIS True\n", "VALIDATION_STEPS 50\n", "WEIGHT_DECAY 0.0001\n", "\n", "\n" ] } ], "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", "\n", "config = InferenceConfig()\n", "config.display()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create Model and Load Trained Weights" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "scrolled": false }, "outputs": [ { "ename": "ValueError", "evalue": "Dimension 1 in both shapes must be equal, but are 324 and 8 for 'Assign_376' (op: 'Assign') with input shapes: [1024,324], [1024,8].", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mInvalidArgumentError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m~\\anaconda3\\envs\\mask_rcnn\\lib\\site-packages\\tensorflow\\python\\framework\\common_shapes.py\u001b[0m in \u001b[0;36m_call_cpp_shape_fn_impl\u001b[1;34m(op, input_tensors_needed, input_tensors_as_shapes_needed, require_shape_fn)\u001b[0m\n\u001b[0;32m 653\u001b[0m \u001b[0mgraph_def_version\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mnode_def_str\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0minput_shapes\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0minput_tensors\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 654\u001b[1;33m input_tensors_as_shapes, status)\n\u001b[0m\u001b[0;32m 655\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0merrors\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mInvalidArgumentError\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0merr\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32m~\\anaconda3\\envs\\mask_rcnn\\lib\\contextlib.py\u001b[0m in \u001b[0;36m__exit__\u001b[1;34m(self, type, value, traceback)\u001b[0m\n\u001b[0;32m 87\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 88\u001b[1;33m \u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgen\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 89\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mStopIteration\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32m~\\anaconda3\\envs\\mask_rcnn\\lib\\site-packages\\tensorflow\\python\\framework\\errors_impl.py\u001b[0m in \u001b[0;36mraise_exception_on_not_ok_status\u001b[1;34m()\u001b[0m\n\u001b[0;32m 465\u001b[0m \u001b[0mcompat\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mas_text\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpywrap_tensorflow\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mTF_Message\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstatus\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 466\u001b[1;33m pywrap_tensorflow.TF_GetCode(status))\n\u001b[0m\u001b[0;32m 467\u001b[0m \u001b[1;32mfinally\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mInvalidArgumentError\u001b[0m: Dimension 1 in both shapes must be equal, but are 324 and 8 for 'Assign_376' (op: 'Assign') with input shapes: [1024,324], [1024,8].", "\nDuring handling of the above exception, another exception occurred:\n", "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;31m# Load weights trained on MS-COCO\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mmodel\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mload_weights\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mCOCO_MODEL_PATH\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mby_name\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;32m~\\anaconda3\\envs\\mask_rcnn\\lib\\site-packages\\mask_rcnn-2.1-py3.6.egg\\mrcnn\\model.py\u001b[0m in \u001b[0;36mload_weights\u001b[1;34m(self, filepath, by_name, exclude)\u001b[0m\n\u001b[0;32m 2128\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2129\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mby_name\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 2130\u001b[1;33m \u001b[0msaving\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mload_weights_from_hdf5_group_by_name\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mf\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlayers\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2131\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2132\u001b[0m \u001b[0msaving\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mload_weights_from_hdf5_group\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mf\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlayers\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32m~\\anaconda3\\envs\\mask_rcnn\\lib\\site-packages\\keras\\engine\\topology.py\u001b[0m in \u001b[0;36mload_weights_from_hdf5_group_by_name\u001b[1;34m(f, layers)\u001b[0m\n\u001b[0;32m 3156\u001b[0m weight_value_tuples.append((symbolic_weights[i],\n\u001b[0;32m 3157\u001b[0m weight_values[i]))\n\u001b[1;32m-> 3158\u001b[1;33m \u001b[0mK\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbatch_set_value\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mweight_value_tuples\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;32m~\\anaconda3\\envs\\mask_rcnn\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py\u001b[0m in \u001b[0;36mbatch_set_value\u001b[1;34m(tuples)\u001b[0m\n\u001b[0;32m 2186\u001b[0m assign_placeholder = tf.placeholder(tf_dtype,\n\u001b[0;32m 2187\u001b[0m shape=value.shape)\n\u001b[1;32m-> 2188\u001b[1;33m \u001b[0massign_op\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0massign\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0massign_placeholder\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2189\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_assign_placeholder\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0massign_placeholder\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2190\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_assign_op\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0massign_op\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32m~\\anaconda3\\envs\\mask_rcnn\\lib\\site-packages\\tensorflow\\python\\ops\\variables.py\u001b[0m in \u001b[0;36massign\u001b[1;34m(self, value, use_locking)\u001b[0m\n\u001b[0;32m 525\u001b[0m \u001b[0mthe\u001b[0m \u001b[0massignment\u001b[0m \u001b[0mhas\u001b[0m \u001b[0mcompleted\u001b[0m\u001b[1;33m.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 526\u001b[0m \"\"\"\n\u001b[1;32m--> 527\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mstate_ops\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0massign\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_variable\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0muse_locking\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0muse_locking\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 528\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 529\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0massign_add\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdelta\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0muse_locking\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mFalse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32m~\\anaconda3\\envs\\mask_rcnn\\lib\\site-packages\\tensorflow\\python\\ops\\state_ops.py\u001b[0m in \u001b[0;36massign\u001b[1;34m(ref, value, validate_shape, use_locking, name)\u001b[0m\n\u001b[0;32m 272\u001b[0m return gen_state_ops.assign(\n\u001b[0;32m 273\u001b[0m \u001b[0mref\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0muse_locking\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0muse_locking\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mname\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 274\u001b[1;33m validate_shape=validate_shape)\n\u001b[0m\u001b[0;32m 275\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mref\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0massign\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mvalue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32m~\\anaconda3\\envs\\mask_rcnn\\lib\\site-packages\\tensorflow\\python\\ops\\gen_state_ops.py\u001b[0m in \u001b[0;36massign\u001b[1;34m(ref, value, validate_shape, use_locking, name)\u001b[0m\n\u001b[0;32m 41\u001b[0m result = _op_def_lib.apply_op(\"Assign\", ref=ref, value=value,\n\u001b[0;32m 42\u001b[0m \u001b[0mvalidate_shape\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mvalidate_shape\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 43\u001b[1;33m use_locking=use_locking, name=name)\n\u001b[0m\u001b[0;32m 44\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mresult\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 45\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32m~\\anaconda3\\envs\\mask_rcnn\\lib\\site-packages\\tensorflow\\python\\framework\\op_def_library.py\u001b[0m in \u001b[0;36mapply_op\u001b[1;34m(self, op_type_name, name, **keywords)\u001b[0m\n\u001b[0;32m 765\u001b[0m op = g.create_op(op_type_name, inputs, output_types, name=scope,\n\u001b[0;32m 766\u001b[0m \u001b[0minput_types\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0minput_types\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mattrs\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mattr_protos\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 767\u001b[1;33m op_def=op_def)\n\u001b[0m\u001b[0;32m 768\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0moutput_structure\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 769\u001b[0m \u001b[0moutputs\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mop\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0moutputs\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32m~\\anaconda3\\envs\\mask_rcnn\\lib\\site-packages\\tensorflow\\python\\framework\\ops.py\u001b[0m in \u001b[0;36mcreate_op\u001b[1;34m(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_shapes, compute_device)\u001b[0m\n\u001b[0;32m 2630\u001b[0m original_op=self._default_original_op, op_def=op_def)\n\u001b[0;32m 2631\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mcompute_shapes\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 2632\u001b[1;33m \u001b[0mset_shapes_for_outputs\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mret\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2633\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_add_op\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mret\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2634\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_record_op_seen_by_control_dependencies\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mret\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32m~\\anaconda3\\envs\\mask_rcnn\\lib\\site-packages\\tensorflow\\python\\framework\\ops.py\u001b[0m in \u001b[0;36mset_shapes_for_outputs\u001b[1;34m(op)\u001b[0m\n\u001b[0;32m 1909\u001b[0m \u001b[0mshape_func\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_call_cpp_shape_fn_and_require_op\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1910\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1911\u001b[1;33m \u001b[0mshapes\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mshape_func\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mop\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 1912\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mshapes\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1913\u001b[0m raise RuntimeError(\n", "\u001b[1;32m~\\anaconda3\\envs\\mask_rcnn\\lib\\site-packages\\tensorflow\\python\\framework\\ops.py\u001b[0m in \u001b[0;36mcall_with_requiring\u001b[1;34m(op)\u001b[0m\n\u001b[0;32m 1859\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1860\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mcall_with_requiring\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mop\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1861\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mcall_cpp_shape_fn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mop\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mrequire_shape_fn\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 1862\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1863\u001b[0m \u001b[0m_call_cpp_shape_fn_and_require_op\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcall_with_requiring\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32m~\\anaconda3\\envs\\mask_rcnn\\lib\\site-packages\\tensorflow\\python\\framework\\common_shapes.py\u001b[0m in \u001b[0;36mcall_cpp_shape_fn\u001b[1;34m(op, require_shape_fn)\u001b[0m\n\u001b[0;32m 593\u001b[0m res = _call_cpp_shape_fn_impl(op, input_tensors_needed,\n\u001b[0;32m 594\u001b[0m \u001b[0minput_tensors_as_shapes_needed\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 595\u001b[1;33m require_shape_fn)\n\u001b[0m\u001b[0;32m 596\u001b[0m \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0misinstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mres\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdict\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 597\u001b[0m \u001b[1;31m# Handles the case where _call_cpp_shape_fn_impl calls unknown_shape(op).\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32m~\\anaconda3\\envs\\mask_rcnn\\lib\\site-packages\\tensorflow\\python\\framework\\common_shapes.py\u001b[0m in \u001b[0;36m_call_cpp_shape_fn_impl\u001b[1;34m(op, input_tensors_needed, input_tensors_as_shapes_needed, require_shape_fn)\u001b[0m\n\u001b[0;32m 657\u001b[0m \u001b[0mmissing_shape_fn\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 658\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 659\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0merr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmessage\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 660\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 661\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mmissing_shape_fn\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mValueError\u001b[0m: Dimension 1 in both shapes must be equal, but are 324 and 8 for 'Assign_376' (op: 'Assign') with input shapes: [1024,324], [1024,8]." ] } ], "source": [ "# Create model object in inference mode.\n", "model = modellib.MaskRCNN(mode=\"inference\", model_dir=MODEL_DIR, config=config)\n", "\n", "# Load weights trained on MS-COCO\n", "model.load_weights(COCO_MODEL_PATH, by_name=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Class Names\n", "\n", "The model classifies objects and returns class IDs, which are integer value that identify each class. Some datasets assign integer values to their classes and some don't. For example, in the MS-COCO dataset, the 'person' class is 1 and 'teddy bear' is 88. The IDs are often sequential, but not always. The COCO dataset, for example, has classes associated with class IDs 70 and 72, but not 71.\n", "\n", "To improve consistency, and to support training on data from multiple sources at the same time, our ```Dataset``` class assigns it's own sequential integer IDs to each class. For example, if you load the COCO dataset using our ```Dataset``` class, the 'person' class would get class ID = 1 (just like COCO) and the 'teddy bear' class is 78 (different from COCO). Keep that in mind when mapping class IDs to class names.\n", "\n", "To get the list of class names, you'd load the dataset and then use the ```class_names``` property like this.\n", "```\n", "# Load COCO dataset\n", "dataset = coco.CocoDataset()\n", "dataset.load_coco(COCO_DIR, \"train\")\n", "dataset.prepare()\n", "\n", "# Print class names\n", "print(dataset.class_names)\n", "```\n", "\n", "We don't want to require you to download the COCO dataset just to run this demo, so we're including the list of class names below. The index of the class name in the list represent its ID (first class is 0, second is 1, third is 2, ...etc.)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "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')\n", "\n", "# class_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']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Run Object Detection" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "# Load a random image from the images folder\n", "file_names = next(os.walk(IMAGE_DIR))[2]\n", "image = skimage.io.imread(os.path.join(IMAGE_DIR, random.choice(file_names)))\n", "\n", "# Run detection\n", "results = model.detect([image], verbose=1)\n", "\n", "# Visualize results\n", "r = results[0]\n", "visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'], class_names, r['scores'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.13" } }, "nbformat": 4, "nbformat_minor": 2 }