Skip to content

Commit 2368d20

Browse files
committed
Initial commit
0 parents  commit 2368d20

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+8745
-0
lines changed

LICENSE

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Mask R-CNN
2+
3+
The MIT License (MIT)
4+
5+
Copyright (c) 2017 Matterport, Inc.
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.

README.md

+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
# Mask R-CNN for Object Detection and Segmentation
2+
3+
This is an implementation of [Mask R-CNN](https://arxiv.org/abs/1703.06870) on Python 3, Keras, and TensorFlow. The model generates bounding boxes and segmentation masks for each instance of an object in the image. It's based on Feature Pyramid Network (FPN) and a ResNet101 backbone.
4+
5+
![Instance Segmentation Sample](assets/street.png)
6+
7+
The repository includes:
8+
* Source code of Mask R-CNN built on FPN and ResNet101.
9+
* Training code for MS COCO
10+
* Pre-trained weights for MS COCO
11+
* Jupyter notebooks to visualize the detection pipeline at every step
12+
* ParallelModel class for multi-GPU training
13+
* Evaluation on MS COCO metrics (AP)
14+
* Example of training on your own dataset
15+
16+
17+
The code is documented and designed to be easy to extend. If you use it in your research, please consider referencing this repository. If you work on 3D vision, you might find our recently released [Matterport3D](https://matterport.com/blog/2017/09/20/announcing-matterport3d-research-dataset/) dataset useful as well.
18+
This dataset was created from 3D-reconstructed spaces captured by our customers who agreed to make them publicly available for academic use. You can see more examples [here](https://matterport.com/gallery/).
19+
20+
21+
# Getting Started
22+
* [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.
23+
It includes code to run object detection and instance segmentation on arbitrary images.
24+
25+
* [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.
26+
27+
* ([model.py](model.py), [utils.py](utils.py), [config.py](config.py)): These files contain the main Mask RCNN implementation.
28+
29+
30+
* [inspect_data.ipynb](/inspect_data.ipynb). This notebook visualizes the different pre-processing steps
31+
to prepare the training data.
32+
33+
* [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.
34+
35+
* [inspect_weights.ipynb](/inspect_weights.ipynb)
36+
This notebooks inspects the weights of a trained model and looks for anomalies and odd patterns.
37+
38+
39+
# Step by Step Detection
40+
To help with debugging and understanding the model, there are 3 notebooks
41+
([inspect_data.ipynb](inspect_data.ipynb), [inspect_model.ipynb](inspect_model.ipynb),
42+
[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:
43+
44+
45+
46+
## 1. Anchor sorting and filtering
47+
Visualizes every step of the first stage Region Proposal Network and displays positive and negative anchors along with anchor box refinement.
48+
![](assets/detection_anchors.png)
49+
50+
## 2. Bounding Box Refinement
51+
This is an example of final detection boxes (dotted lines) and the refinement applied to them (solid lines) in the second stage.
52+
![](assets/detection_refinement.png)
53+
54+
## 3. Mask Generation
55+
Examples of generated masks. These then get scaled and placed on the image in the right location.
56+
57+
![](assets/detection_masks.png)
58+
59+
## 4.Layer activations
60+
Often it's useful to inspect the activations at different layers to look for signs of trouble (all zeros or random noise).
61+
62+
![](assets/detection_activations.png)
63+
64+
## 5. Weight Histograms
65+
Another useful debugging tool is to inspect the weight histograms. These are included in the inspect_weights.ipynb notebook.
66+
67+
![](assets/detection_histograms.png)
68+
69+
## 6. Logging to TensorBoard
70+
TensorBoard is another great debugging and visualization tool. The model is configured to log losses and save weights at the end of every epoch.
71+
72+
![](assets/detection_tensorboard.png)
73+
74+
## 6. Composing the different pieces into a final result
75+
76+
![](assets/detection_final.png)
77+
78+
79+
# Training on MS COCO
80+
We're providing pre-trained weights for MS COCO to make it easier to start. You can
81+
use those weights as a starting point to train your own variation on the network.
82+
Training and evaluation code is in coco.py. You can import this
83+
module in Jupyter notebook (see the provided notebooks for examples) or you
84+
can run it directly from the command line as such:
85+
86+
```
87+
# Train a new model starting from pre-trained COCO weights
88+
python3 coco.py train --dataset=/path/to/coco/ --model=coco
89+
90+
# Train a new model starting from ImageNet weights
91+
python3 coco.py train --dataset=/path/to/coco/ --model=imagenet
92+
93+
# Continue training a model that you had trained earlier
94+
python3 coco.py train --dataset=/path/to/coco/ --model=/path/to/weights.h5
95+
96+
# Continue training the last model you trained. This will find
97+
# the last trained weights in the model directory.
98+
python3 coco.py train --dataset=/path/to/coco/ --model=last
99+
```
100+
101+
You can also run the COCO evaluation code with:
102+
```
103+
# Run COCO evaluation on the last trained model
104+
python3 coco.py evaluate --dataset=/path/to/coco/ --model=last
105+
```
106+
107+
The training schedule, learning rate, and other parameters should be set in coco.py.
108+
109+
110+
# Training on Your Own Dataset
111+
To train the model on your own dataset you'll need to sub-class two classes:
112+
113+
```Config```
114+
This class contains the default configuration. Subclass it and modify the attributes you need to change.
115+
116+
```Dataset```
117+
This class provides a consistent way to work with any dataset.
118+
It allows you to use new datasets for training without having to change
119+
the code of the model. It also supports loading multiple datasets at the
120+
same time, which is useful if the objects you want to detect are not
121+
all available in one dataset.
122+
123+
The ```Dataset``` class itself is the base class. To use it, create a new
124+
class that inherits from it and adds functions specific to your dataset.
125+
See the base `Dataset` class in utils.py and examples of extending it in train_shapes.ipynb and coco.py.
126+
127+
## Differences from the Official Paper
128+
This implementation follows the Mask RCNN paper for the most part, but there are a few cases where we deviated in favor of code simplicity and generalization. These are some of the differences we're aware of. If you encounter other differences, please do let us know.
129+
130+
* **Image Resizing:** To support training multiple images per batch we resize all images to the same size. For example, 1024x1024px on MS COCO. We preserve the aspect ratio, so if an image is not square we pad it with zeros. In the paper the resizing is done such that the smallest side is 800px and the largest is trimmed at 1000px.
131+
* **Bounding Boxes**: Some datasets provide bounding boxes and some provide masks only. To support training on multiple datasets we opted to ignore the bounding boxes that come with the dataset and generate them on the fly instead. We pick the smallest box that encapsulates all the pixels of the mask as the bounding box. This simplifies the implementation and also makes it easy to apply certain image augmentations that would otherwise be really hard to apply to bounding boxes, such as image rotation.
132+
133+
To validate this approach, we compared our computed bounding boxes to those provided by the COCO dataset.
134+
We found that ~2% of bounding boxes differed by 1px or more, ~0.05% differed by 5px or more,
135+
and only 0.01% differed by 10px or more.
136+
137+
* **Learning Rate:** The paper uses a learning rate of 0.02, but we found that to be
138+
too high, and often causes the weights to explode, especially when using a small batch
139+
size. It might be related to differences between how Caffe and TensorFlow compute
140+
gradients (sum vs mean across batches and GPUs). Or, maybe the official model uses gradient
141+
clipping to avoid this issue. We do use gradient clipping, but don't set it too aggressively.
142+
We found that smaller learning rates converge faster anyway so we go with that.
143+
144+
* **Anchor Strides:** The lowest level of the pyramid has a stride of 4px relative to the image, so anchors are created at every 4 pixel intervals. To reduce computation and memory load we adopt an anchor stride of 2, which cuts the number of anchors by 4 and doesn't have a significant effect on accuracy.
145+
146+
## Contributing
147+
Contributions to this repository are welcome. Examples of things you can contribute:
148+
* Speed Improvements. Like re-writing some Python code in TensorFlow or Cython.
149+
* Training on other datasets.
150+
* Accuracy Improvements.
151+
* Visualizations and examples.
152+
153+
You can also [join our team](https://matterport.com/careers/) and help us build even more projects like this one.
154+
155+
156+
## Requirements
157+
* Python 3.4+
158+
* TensorFlow 1.3+
159+
* Keras 2.0.8+
160+
* Jupyter Notebook
161+
* Numpy, skimage, scipy
162+
163+
If you use Docker, the model has been verified to work on
164+
[this Docker container](https://hub.docker.com/r/waleedka/modern-deep-learning/).
165+
166+
## Installation
167+
1. Clone this repository
168+
2. Download pre-trained COCO weights from the releases section of this repository.
169+
170+
## More Examples
171+
![Sheep](assets/sheep.png)
172+
![Donuts](assets/donuts.png)

assets/detection_activations.png

69.1 KB
Loading

assets/detection_anchors.png

747 KB
Loading

assets/detection_final.png

887 KB
Loading

assets/detection_histograms.png

13.4 KB
Loading

assets/detection_masks.png

9.75 KB
Loading

assets/detection_refinement.png

703 KB
Loading

assets/detection_tensorboard.png

43.1 KB
Loading

assets/donuts.png

871 KB
Loading

assets/sheep.png

929 KB
Loading

assets/street.png

917 KB
Loading

0 commit comments

Comments
 (0)