diff --git a/README.md b/README.md index 44a9a968cb..e73b798b86 100644 --- a/README.md +++ b/README.md @@ -26,27 +26,27 @@ The goal is to improve OpenStreetMap by adding high quality baseball, soccer, te [![Mask RCNN on 4K Video](assets/4k_video.gif)](https://www.youtube.com/watch?v=OOT3UIXZztE) # Getting Started -* [demo.ipynb](/demo.ipynb) Is the easiest way to start. It shows an example of using a model pre-trained on MS COCO to segment objects in your own images. +* [demo.ipynb](samples/demo.ipynb) Is the easiest way to start. It shows an example of using a model pre-trained on MS COCO to segment objects in your own images. It includes code to run object detection and instance segmentation on arbitrary images. -* [train_shapes.ipynb](train_shapes.ipynb) shows how to train Mask R-CNN on your own dataset. This notebook introduces a toy dataset (Shapes) to demonstrate training on a new dataset. +* [train_shapes.ipynb](samples/train_shapes.ipynb) shows how to train Mask R-CNN on your own dataset. This notebook introduces a toy dataset (Shapes) to demonstrate training on a new dataset. -* ([model.py](model.py), [utils.py](utils.py), [config.py](config.py)): These files contain the main Mask RCNN implementation. +* ([model.py](mrcnn/model.py), [utils.py](mrcnn/utils.py), [config.py](mrcnn/config.py)): These files contain the main Mask RCNN implementation. -* [inspect_data.ipynb](/inspect_data.ipynb). This notebook visualizes the different pre-processing steps +* [inspect_data.ipynb](samples/inspect_data.ipynb). This notebook visualizes the different pre-processing steps to prepare the training data. -* [inspect_model.ipynb](/inspect_model.ipynb) This notebook goes in depth into the steps performed to detect and segment objects. It provides visualizations of every step of the pipeline. +* [inspect_model.ipynb](samples/inspect_model.ipynb) This notebook goes in depth into the steps performed to detect and segment objects. It provides visualizations of every step of the pipeline. -* [inspect_weights.ipynb](/inspect_weights.ipynb) +* [inspect_weights.ipynb](samples/inspect_weights.ipynb) This notebooks inspects the weights of a trained model and looks for anomalies and odd patterns. # Step by Step Detection To help with debugging and understanding the model, there are 3 notebooks -([inspect_data.ipynb](inspect_data.ipynb), [inspect_model.ipynb](inspect_model.ipynb), -[inspect_weights.ipynb](inspect_weights.ipynb)) that provide a lot of visualizations and allow running the model step by step to inspect the output at each point. Here are a few examples: +([inspect_data.ipynb](samples/inspect_data.ipynb), [inspect_model.ipynb](samples/inspect_model.ipynb), +[inspect_weights.ipynb](samples/inspect_weights.ipynb)) that provide a lot of visualizations and allow running the model step by step to inspect the output at each point. Here are a few examples: diff --git a/coco.py b/coco.py index 92bea8e5ca..2379a6f904 100644 --- a/coco.py +++ b/coco.py @@ -46,9 +46,8 @@ import urllib.request import shutil -from config import Config -import utils -import model as modellib +from mrcnn.config import Config +from mrcnn import model as modellib, utils # Root directory of the project ROOT_DIR = os.getcwd() diff --git a/mrcnn/__init__.py b/mrcnn/__init__.py new file mode 100644 index 0000000000..2691a1f062 --- /dev/null +++ b/mrcnn/__init__.py @@ -0,0 +1,18 @@ +import os +import logging + +import matplotlib +import numpy as np + +# in case you are running on machine without display, e.g. server +if os.environ.get('DISPLAY', '') == '' \ + and matplotlib.rcParams['backend'] != 'agg': + logging.warning('No display found. Using non-interactive Agg backend') + # https://matplotlib.org/faq/usage_faq.html + matplotlib.use('Agg') + +# parse the numpy versions +np_version = [int(i) for i in np.version.full_version.split('.')] +# comparing strings does not work for version lower 1.10 +if np_version >= [1, 14]: + np.set_printoptions(legacy='1.13') diff --git a/config.py b/mrcnn/config.py similarity index 100% rename from config.py rename to mrcnn/config.py diff --git a/model.py b/mrcnn/model.py similarity index 99% rename from model.py rename to mrcnn/model.py index 0034af2226..4e11cb0fd3 100644 --- a/model.py +++ b/mrcnn/model.py @@ -8,13 +8,8 @@ """ import os -import sys -import glob import random -import math import datetime -import itertools -import json import re import logging from collections import OrderedDict @@ -25,11 +20,10 @@ import keras import keras.backend as K import keras.layers as KL -import keras.initializers as KI import keras.engine as KE import keras.models as KM -import utils +from mrcnn import utils # Requires TensorFlow 1.3+ and Keras 2.0.8+. from distutils.version import LooseVersion @@ -1986,7 +1980,7 @@ def build(self, mode, config): mrcnn_class_logits, mrcnn_class, mrcnn_bbox, mrcnn_mask, rpn_rois, output_rois, rpn_class_loss, rpn_bbox_loss, class_loss, bbox_loss, mask_loss] - model = KM.Model(inputs, outputs, name='mask_rcnn') + model = KM.Model(inputs, outputs, name='mrcnn') else: # Network Heads # Proposal classifier and BBox regressor heads @@ -2012,11 +2006,11 @@ def build(self, mode, config): model = KM.Model([input_image, input_image_meta], [detections, mrcnn_class, mrcnn_bbox, mrcnn_mask, rpn_rois, rpn_class, rpn_bbox], - name='mask_rcnn') + name='mrcnn') # Add multi-GPU support. if config.GPU_COUNT > 1: - from parallel_model import ParallelModel + from mrcnn.parallel_model import ParallelModel model = ParallelModel(model, config.GPU_COUNT) return model @@ -2039,7 +2033,7 @@ def find_last(self): dir_name = os.path.join(self.model_dir, dir_names[-1]) # Find the last checkpoint checkpoints = next(os.walk(dir_name))[2] - checkpoints = filter(lambda f: f.startswith("mask_rcnn"), checkpoints) + checkpoints = filter(lambda f: f.startswith("mrcnn"), checkpoints) checkpoints = sorted(checkpoints) if not checkpoints: return dir_name, None diff --git a/parallel_model.py b/mrcnn/parallel_model.py similarity index 100% rename from parallel_model.py rename to mrcnn/parallel_model.py diff --git a/shapes.py b/mrcnn/shapes.py similarity index 99% rename from shapes.py rename to mrcnn/shapes.py index ae0f3be5c7..2a44f3cc4f 100644 --- a/shapes.py +++ b/mrcnn/shapes.py @@ -14,8 +14,8 @@ import numpy as np import cv2 -from config import Config -import utils +from mrcnn.config import Config +from mrcnn import utils class ShapesConfig(Config): diff --git a/utils.py b/mrcnn/utils.py similarity index 100% rename from utils.py rename to mrcnn/utils.py diff --git a/visualize.py b/mrcnn/visualize.py similarity index 99% rename from visualize.py rename to mrcnn/visualize.py index 5ea3e1443e..9785efe133 100644 --- a/visualize.py +++ b/mrcnn/visualize.py @@ -18,7 +18,7 @@ from matplotlib.patches import Polygon import IPython.display -import utils +from mrcnn import utils ############################################################ diff --git a/samples/__init__.py b/samples/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/samples/balloon/__init__.py b/samples/balloon/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/samples/balloon/balloon.py b/samples/balloon/balloon.py index afd3a27f93..3b3373459d 100644 --- a/samples/balloon/balloon.py +++ b/samples/balloon/balloon.py @@ -42,9 +42,8 @@ # Import Mask RCNN sys.path.append(ROOT_DIR) -from config import Config -import utils -import model as modellib +from mrcnn.config import Config +from mrcnn import model as modellib, utils # Path to trained weights file COCO_WEIGHTS_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5") diff --git a/samples/balloon/inspect_balloon_data.ipynb b/samples/balloon/inspect_balloon_data.ipynb index c2c77df7e3..21d941dfef 100644 --- a/samples/balloon/inspect_balloon_data.ipynb +++ b/samples/balloon/inspect_balloon_data.ipynb @@ -49,13 +49,13 @@ "\n", "# Import Mask RCNN\n", "sys.path.append(ROOT_DIR)\n", - "import utils\n", - "import visualize\n", - "from visualize import display_images\n", - "import model as modellib\n", - "from model import log\n", + "import mrcnn.utils\n", + "import mrcnn.visualize\n", + "from mrcnn.visualize import display_images\n", + "import mrcnn.model as modellib\n", + "from mrcnn.model import log\n", "\n", - "import balloon\n", + "from samples.balloon import balloon\n", "\n", "%matplotlib inline " ] diff --git a/samples/balloon/inspect_balloon_model.ipynb b/samples/balloon/inspect_balloon_model.ipynb index 6aa2cfa3e3..b9d90a4c9a 100644 --- a/samples/balloon/inspect_balloon_model.ipynb +++ b/samples/balloon/inspect_balloon_model.ipynb @@ -45,13 +45,13 @@ "\n", "# Import Mask RCNN\n", "sys.path.append(ROOT_DIR)\n", - "import utils\n", - "import visualize\n", - "from visualize import display_images\n", - "import model as modellib\n", - "from model import log\n", + "import mrcnn.utils\n", + "import mrcnn.visualize\n", + "from mrcnn.visualize import display_images\n", + "import mrcnn.model as modellib\n", + "from mrcnn.model import log\n", "\n", - "import balloon\n", + "from samples.balloon import balloon\n", "\n", "%matplotlib inline \n", "\n", diff --git a/demo.ipynb b/samples/demo.ipynb similarity index 99% rename from demo.ipynb rename to samples/demo.ipynb index b72047d4cb..34631425ee 100644 --- a/demo.ipynb +++ b/samples/demo.ipynb @@ -33,9 +33,9 @@ "import matplotlib.pyplot as plt\n", "\n", "import coco\n", - "import utils\n", - "import model as modellib\n", - "import visualize\n", + "import mrcnn.utils\n", + "import mrcnn.model as modellib\n", + "import mrcnn.visualize\n", "\n", "%matplotlib inline \n", "\n", diff --git a/inspect_data.ipynb b/samples/inspect_data.ipynb similarity index 99% rename from inspect_data.ipynb rename to samples/inspect_data.ipynb index 3b549256fa..4b6ba85006 100644 --- a/inspect_data.ipynb +++ b/samples/inspect_data.ipynb @@ -39,11 +39,11 @@ "import matplotlib.lines as lines\n", "from matplotlib.patches import Polygon\n", "\n", - "import utils\n", - "import visualize\n", - "from visualize import display_images\n", - "import model as modellib\n", - "from model import log\n", + "import mrcnn.utils\n", + "import mrcnn.visualize\n", + "from mrcnn.visualize import display_images\n", + "import mrcnn.model as modellib\n", + "from mrcnn.model import log\n", "\n", "%matplotlib inline \n", "\n", diff --git a/inspect_model.ipynb b/samples/inspect_model.ipynb similarity index 99% rename from inspect_model.ipynb rename to samples/inspect_model.ipynb index 1445411829..6caf307a61 100644 --- a/inspect_model.ipynb +++ b/samples/inspect_model.ipynb @@ -35,11 +35,11 @@ "import matplotlib.pyplot as plt\n", "import matplotlib.patches as patches\n", "\n", - "import utils\n", - "import visualize\n", - "from visualize import display_images\n", - "import model as modellib\n", - "from model import log\n", + "import mrcnn.utils\n", + "import mrcnn.visualize\n", + "from mrcnn.visualize import display_images\n", + "import mrcnn.model as modellib\n", + "from mrcnn.model import log\n", "\n", "%matplotlib inline \n", "\n", diff --git a/inspect_weights.ipynb b/samples/inspect_weights.ipynb similarity index 99% rename from inspect_weights.ipynb rename to samples/inspect_weights.ipynb index 912fae84c8..8e80422d84 100644 --- a/inspect_weights.ipynb +++ b/samples/inspect_weights.ipynb @@ -31,10 +31,10 @@ "import matplotlib.pyplot as plt\n", "import keras\n", "\n", - "import utils\n", - "import model as modellib\n", - "import visualize\n", - "from model import log\n", + "import mrcnn.utils\n", + "import mrcnn.model as modellib\n", + "import mrcnn.visualize\n", + "from mrcnn.model import log\n", "\n", "%matplotlib inline \n", "\n", @@ -179,7 +179,7 @@ ] }, "metadata": {}, - "output_type": "display_data" + "output_type": "execute_result" } ], "source": [ diff --git a/train_shapes.ipynb b/samples/train_shapes.ipynb similarity index 99% rename from train_shapes.ipynb rename to samples/train_shapes.ipynb index be44d624f9..f1760d6fb0 100644 --- a/train_shapes.ipynb +++ b/samples/train_shapes.ipynb @@ -37,11 +37,11 @@ "import matplotlib\n", "import matplotlib.pyplot as plt\n", "\n", - "from config import Config\n", - "import utils\n", - "import model as modellib\n", - "import visualize\n", - "from model import log\n", + "from mrcnn.config import Config\n", + "import mrcnn.utils\n", + "import mrcnn.model as modellib\n", + "import mrcnn.visualize\n", + "from mrcnn.model import log\n", "\n", "%matplotlib inline \n", "\n",