Title: Keras Mask R-CNN - PyImageSearch

URL Source: https://pyimagesearch.com/2019/06/10/keras-mask-r-cnn/

Published Time: 2019-06-10T14:00:53+00:00

Markdown Content:
![Image 1](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20600'%3E%3C/svg%3E)

In this tutorial, you will learn how to use Keras and Mask R-CNN to perform instance segmentation (both with and without a GPU).

For developing and fine-tuning Mask R-CNN models, having access to rich, diverse datasets is critical. These datasets provide the foundational training material for the model, influencing its ability to accurately segment and recognize objects.

[Roboflow](https://roboflow.com/?ref=pyimagesearch) has free tools for each stage of the computer vision pipeline that will streamline your workflows and supercharge your productivity.

Sign up or Log in to your [Roboflow account](https://roboflow.com/?ref=pyimagesearch) to access state of the art dataset libaries and revolutionize your computer vision pipeline.

You can start by choosing your own datasets or using our [PyimageSearch’s assorted library of useful datasets](https://universe.roboflow.com/pyimagesearch?ref=pyimagesearch).

Bring data in any of 40+ formats to [Roboflow](https://roboflow.com/?ref=pyimagesearch), train using any state-of-the-art model architectures, deploy across multiple platforms (API, NVIDIA, browser, iOS, etc), and connect to applications or 3rd party tools.

With a few images, you can train a working computer vision model in an afternoon. For example, bring data into [Roboflow](https://roboflow.com/?ref=pyimagesearch) from anywhere via API, label images with the cloud-hosted image annotation tool, kickoff a hosted model training with one-click, and deploy the model via a hosted API endpoint. This process can be executed in a code-centric way, in the cloud-based UI, or any mix of the two.

Over 250,000 developers and machine learning engineers from companies such as Cardinal Health, Walmart, USG, Rivian, Intel, and Medtronic build computer vision pipelines with Roboflow. Get started today, no credit card required.

Using Mask R-CNN we can perform both:

1.  **Object detection,** giving us the _(x, y)_\-bounding box coordinates of for each object in an image.
2.  **Instance segmentation,** enabling us to obtain a **pixel-wise mask** for each individual object in an image.

An example of instance segmentation via Mask R-CNN can be seen in the image at the top of this tutorial — notice how we not only have the **bounding box** of the objects in the image, but we also have **pixel-wise masks** for each object as well, enabling us to **segment each individual object** (something that object detection alone does not give us).

Instance segmentation, along with Mask R-CNN, powers some of the recent advances in the “magic” we see in computer vision, including self-driving cars, robotics, and more.

In the remainder of this tutorial, you will learn how to use Mask R-CNN with Keras, including how to perform instance segmentation on your own images.

**To learn more about Keras and Mask R-CNN, _just keep reading!_**

![Image 2](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20128%20128'%3E%3C/svg%3E)

#### Looking for the source code to this post?

[Jump Right To The Downloads Section](#download-the-code)

In the first part of this tutorial, we’ll briefly review the Mask R-CNN architecture. From there, we’ll review our directory structure for this project and then install Keras + Mask R-CNN on our system.

I’ll then show you how to implement Mask R-CNN and Keras using Python.

Finally, we’ll apply Mask R-CNN to our own images and examine the results.

I’ll also share resources on how to train a Mask R-CNN model on your own custom dataset.

### The History of Mask R-CNN

[![Image 3](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20327'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2018/11/mask_rcnn_arch.png)

**Figure 1:** The Mask R-CNN architecture by He et al. enables object detection and pixel-wise instance segmentation. This blog post uses Keras to work with a Mask R-CNN model trained on the COCO dataset.

The Mask R-CNN model for _instance segmentation_ has evolved from three preceding architectures for _object detection:_

*   **R-CNN:** An input image is presented to the network, Selective Search is run on the image, and then the output regions from Selective Search are used for feature extraction and classification using a pre-trained CNN.
*   **Fast R-CNN:** Still uses the Selective Search algorithm to obtain region proposals, but adds the **Region of Interest (ROI) Pooling module**. Extracts a fixed-size window from the feature map and uses the features to obtain the final class label and bounding box. The benefit is that the network is now _end-to-end trainable_.
*   **Faster R-CNN:** Introduces the Regional Proposal Network (RPN) that bakes the region proposal _directly_ into the architecture, alleviating the need for the Selective Search algorithm.

The Mask R-CNN algorithm builds on the previous Faster R-CNN, enabling the network to not only perform _object detection_ but _pixel-wise instance segmentation as well!_

I’ve covered Mask R-CNN in-depth inside both:

1.  The _“What is Mask R-CNN?”_ section of the [_Mask R-CNN with OpenCV_](https://pyimagesearch.com/2018/11/19/mask-r-cnn-with-opencv/) post.
2.  My book, **[_Deep Learning for Computer Vision with Python_](https://pyimagesearch.com/deep-learning-computer-vision-python-book/)**.

Please refer to those resources for more in-depth details on how the architecture works, including the ROI Align module and how it facilitates instance segmentation.

### Project structure

Go ahead and use the _**“Downloads”**_ section of today’s blog post to download the code and pre-trained model. Let’s inspect our Keras Mask R-CNN project structure:

$ tree --dirsfirst
.
├── images
│   ├── 30th\_birthday.jpg
│   ├── couch.jpg
│   ├── page\_az.jpg
│   └── ybor\_city.jpg
├── coco\_labels.txt
├── mask\_rcnn\_coco.h5
└── maskrcnn\_predict.py

1 directory, 7 files

Our project consists of a testing `images/` directory as well as three files:

*   `coco_labels.txt` : Comprised of a line-by-line listing of 81 class labels. The first label is the “background” class, so typically we say there are 80 classes.
*   `mask_rcnn_coco.h5` : Our pre-trained Mask R-CNN model weights file which will be loaded from disk.
*   `maskrcnn_predict.py` : The Mask R-CNN demo script loads the labels and model/weights. From there, an inference is made on a testing image provided via a command line argument. You may test with one of our own images or any in the `images/` directory included with the _**“Downloads”**_.

Before we review today’s script, we’ll install Keras + Mask R-CNN and then we’ll briefly review the COCO dataset.

### Installing Keras Mask R-CNN

The Keras + Mask R-CNN installation process is quote straightforward with pip, git, and `setup.py` . I recommend you install these packages in a dedicated virtual environment for today’s project so you don’t complicate your system’s package tree.

First, install the required Python packages:

$ pip install numpy scipy
$ pip install pillow scikit-image matplotlib imutils
$ pip install "IPython\[all\]"
$ pip install tensorflow # or tensorflow-gpu
$ pip install keras h5py

Be sure to install `tensorflow-gpu` if you have a GPU, CUDA, and cuDNN installed in your machine.

From there, go ahead and install OpenCV, either [via pip](https://pyimagesearch.com/2018/09/19/pip-install-opencv/) or [compiling from source](https://pyimagesearch.com/opencv-tutorials-resources-guides/):

$ pip install opencv-contrib-python

Next, we’ll install the [Matterport implementation of Mask R-CNN](https://github.com/matterport/Mask_RCNN) in Keras:

$ git clone https://github.com/matterport/Mask\_RCNN.git
$ cd Mask\_RCNN
$ python setup.py install

Finally, fire up a Python interpreter in your virtual environment to verify that Mask R-CNN + Keras and OpenCV have been successfully installed:

$ python
>>> import mrcnn
>>> import cv2
>>>

Provided that there are no import errors, your environment is now ready for today’s blog post.

### Mask R-CNN and COCO

The Mask R-CNN model we’ll be using here today is **pre-trained on the COCO dataset**.

This dataset includes a total of 80 classes (plus one background class) that you can detect and segment from an input image (with the first class being the background class). I have included the labels file named `coco_labels.txt` in the **_“Downloads”_** associated with this post, but out of convenience, I have included them here for you:

1.  BG
2.  person
3.  bicycle
4.  car
5.  motorcycle
6.  airplane
7.  bus
8.  train
9.  truck
10.  boat
11.  traffic light
12.  fire hydrant
13.  stop sign
14.  parking meter
15.  bench
16.  bird
17.  cat
18.  dog
19.  horse
20.  sheep
21.  cow
22.  elephant
23.  bear
24.  zebra
25.  giraffe
26.  backpack
27.  umbrella

28.  handbag
29.  tie
30.  suitcase
31.  frisbee
32.  skis
33.  snowboard
34.  sports ball
35.  kite
36.  baseball bat
37.  baseball glove
38.  skateboard
39.  surfboard
40.  tennis racket
41.  bottle
42.  wine glass
43.  cup
44.  fork
45.  knife
46.  spoon
47.  bowl
48.  banana
49.  apple
50.  sandwich
51.  orange
52.  broccoli
53.  carrot
54.  hot dog

55.  pizza
56.  donut
57.  cake
58.  chair
59.  couch
60.  potted plant
61.  bed
62.  dining table
63.  toilet
64.  tv
65.  laptop
66.  mouse
67.  remote
68.  keyboard
69.  cell phone
70.  microwave
71.  oven
72.  toaster
73.  sink
74.  refrigerator
75.  book
76.  clock
77.  vase
78.  scissors
79.  teddy bear
80.  hair drier
81.  toothbrush

In the next section, we’ll learn how to use Keras and Mask R-CNN to detect and segment each of these classes.

### Implementing Mask R-CNN with Keras and Python

Let’s get started implementing Mask R-CNN segmentation script.

Open up the `maskrcnn_predict.py` and insert the following code:

\# import the necessary packages
from mrcnn.config import Config
from mrcnn import model as modellib
from mrcnn import visualize
import numpy as np
import colorsys
import argparse
import imutils
import random
import cv2
import os

**Lines 2-11** import our required packages.

The `mrcnn` imports are from Matterport’s implementation of Mask R-CNN. From `mrcnn` , we’ll use `Config` to create a custom subclass for our configuration, `modellib` to load our model, and `visualize` to draw our masks.

Let’s go ahead and [parse our command line arguments](https://pyimagesearch.com/2018/03/12/python-argparse-command-line-arguments/):

\# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add\_argument("-w", "--weights", required=True,
	help="path to Mask R-CNN model weights pre-trained on COCO")
ap.add\_argument("-l", "--labels", required=True,
	help="path to class labels file")
ap.add\_argument("-i", "--image", required=True,
	help="path to input image to apply Mask R-CNN to")
args = vars(ap.parse\_args())

Our script requires three command line arguments:

*   `--weights` : The path to our Mask R-CNN model weights pre-trained on COCO.
*   `--labels` : The path to our COCO class labels text file.
*   `--image` : Our input image path. We’ll be performing instance segmentation on the image provided via the command line.

Using the second argument, let’s go ahead and load our `CLASS_NAMES` and `COLORS` for each:

\# load the class label names from disk, one label per line
CLASS\_NAMES = open(args\["labels"\]).read().strip().split("\\n")

# generate random (but visually distinct) colors for each class label
# (thanks to Matterport Mask R-CNN for the method!)
hsv = \[(i / len(CLASS\_NAMES), 1, 1.0) for i in range(len(CLASS\_NAMES))\]
COLORS = list(map(lambda c: colorsys.hsv\_to\_rgb(\*c), hsv))
random.seed(42)
random.shuffle(COLORS)

**Line 24** loads the COCO class label names directly from the text file into a list.

From there, **Lines 28-31** generate random, distinct `COLORS` for each class label. The method comes from [Matterport’s Mask R-CNN implementation on GitHub](https://github.com/matterport/Mask_RCNN/blob/master/mrcnn/visualize.py#L59).

Let’s go ahead and construct our `SimpleConfig` class:

class SimpleConfig(Config):
	# give the configuration a recognizable name
	NAME = "coco\_inference"

	# set the number of GPUs to use along with the number of images
	# per GPU
	GPU\_COUNT = 1
	IMAGES\_PER\_GPU = 1

	# number of classes (we would normally add +1 for the background
	# but the background class is \*already\* included in the class
	# names)
	NUM\_CLASSES = len(CLASS\_NAMES)

Our `SimpleConfig` class inherits from Matterport’s Mask R-CNN `Config` (**Line 33**).

The configuration is given a `NAME` (**Line 35**).

From there we set the `GPU_COUNT` and `IMAGES_PER_GPU` (i.e., batch). If you have a GPU and `tensorflow-gpu` installed then Keras + Mask R-CNN will automatically use your GPU. If not, your CPU will be used instead.

_**Note:** I performed today’s experiment on a machine using a single Titan X GPU, so I set my `GPU_COUNT = 1` . While my 12GB GPU could technically handle more than one image at a time (either during training or during prediction as in this script), I decided to set `IMAGES_PER_GPU = 1` as most readers will not have a GPU with as much memory. Feel free to increase this value if your GPU can handle it._

Our `NUM_CLASSES` is then set equal to the length of the `CLASS_NAMES` list (**Line 45**).

Next, we’ll initialize our config and load our model:

\# initialize the inference configuration
config = SimpleConfig()

# initialize the Mask R-CNN model for inference and then load the
# weights
print("\[INFO\] loading Mask R-CNN model...")
model = modellib.MaskRCNN(mode="inference", config=config,
	model\_dir=os.getcwd())
model.load\_weights(args\["weights"\], by\_name=True)

**Line 48** instantiates our `config` .

Then, using our `config` , **Lines 53-55** load our Mask R-CNN `model` pre-trained on the COCO dataset.

Let’s go ahead and **perform instance segmentation:**

\# load the input image, convert it from BGR to RGB channel
# ordering, and resize the image
image = cv2.imread(args\["image"\])
image = cv2.cvtColor(image, cv2.COLOR\_BGR2RGB)
image = imutils.resize(image, width=512)

# perform a forward pass of the network to obtain the results
print("\[INFO\] making predictions with Mask R-CNN...")
r = model.detect(\[image\], verbose=1)\[0\]

**Lines 59-61** load and preprocess our `image` . Our model expects images in RGB format so we use `cv2.cvtColor` to swap the color channels (in contrast OpenCV’s default BGR color channel ordering).

**Line 65** then performs a forward pass of the `image` through the network to make both object detection and pixel-wise mask predictions.

The remaining two code blocks will process the results so that we can visualize the objects’ bounding boxes and masks using OpenCV:

\# loop over of the detected object's bounding boxes and masks
for i in range(0, r\["rois"\].shape\[0\]):
	# extract the class ID and mask for the current detection, then
	# grab the color to visualize the mask (in BGR format)
	classID = r\["class\_ids"\]\[i\]
	mask = r\["masks"\]\[:, :, i\]
	color = COLORS\[classID\]\[::-1\]

	# visualize the pixel-wise mask of the object
	image = visualize.apply\_mask(image, mask, color, alpha=0.5)

In order to visualize the results, we begin by looping over object detections (**Line 68**). Inside the loop, we:

*   Grab the unique `classID` integer (**Line 71**).
*   Extract the `mask` for the current detection (**Line 72**).
*   Determine the `color` used to `visualize` the mask (**Line 73**).
*   **Apply/draw our predicted pixel-wise mask** on the object using a semi-transparent `alpha` channel (**Line 76**).

From here, we’ll draw bounding boxes and class label + score texts for each object in the image:

\# convert the image back to BGR so we can use OpenCV's drawing
# functions
image = cv2.cvtColor(image, cv2.COLOR\_RGB2BGR)

# loop over the predicted scores and class labels
for i in range(0, len(r\["scores"\])):
	# extract the bounding box information, class ID, label, predicted
	# probability, and visualization color
	(startY, startX, endY, endX) = r\["rois"\]\[i\]
	classID = r\["class\_ids"\]\[i\]
	label = CLASS\_NAMES\[classID\]
	score = r\["scores"\]\[i\]
	color = \[int(c) for c in np.array(COLORS\[classID\]) \* 255\]

	# draw the bounding box, class label, and score of the object
	cv2.rectangle(image, (startX, startY), (endX, endY), color, 2)
	text = "{}: {:.3f}".format(label, score)
	y = startY - 10 if startY - 10 > 10 else startY + 10
	cv2.putText(image, text, (startX, y), cv2.FONT\_HERSHEY\_SIMPLEX,
		0.6, color, 2)

# show the output image
cv2.imshow("Output", image)
cv2.waitKey()

**Line 80** converts our `image` back to BGR (OpenCV’s default color channel ordering).

On **Line 83** we begin looping over objects. Inside the loop, we:

*   Extract the bounding box coordinates, `classID` , `label` , and `score` (**Lines 86-89**).
*   Compute the `color` for the bounding box and text (**Line 90**).
*   Draw each bounding box (**Line 93**).
*   Concatenate the class/probability `text` (**Line 94**) and then draw it at the top of the `image` (**Lines 95-97**).

Once the process is complete, the resulting output `image` is displayed to the screen until a key is pressed (**Lines 100-101**).

### Mask R-CNN and Keras results

Now that our Mask R-CNN script has been implemented, let’s give it a try.

Make sure you have used the **_“Downloads”_** section of this tutorial to download the source code.

You will need to know the concept of command line arguments to run the code. If it is unfamiliar to you, read up on [argparse and command line arguments](https://pyimagesearch.com/2018/03/12/python-argparse-command-line-arguments/) before you try to execute the code.

When you’re ready, open up a terminal and execute the following command:

$ python maskrcnn\_predict.py --weights mask\_rcnn\_coco.h5 --labels coco\_labels.txt \\
	--image images/30th\_birthday.jpg
Using TensorFlow backend.
\[INFO\] loading Mask R-CNN model...
\[INFO\] making predictions with Mask R-CNN...
Processing 1 images
image                    shape: (682, 512, 3)         min:    0.00000  max:  255.00000  uint8
molded\_images            shape: (1, 1024, 1024, 3)    min: -123.70000  max:  151.10000  float64
image\_metas              shape: (1, 93)               min:    0.00000  max: 1024.00000  float64
anchors                  shape: (1, 261888, 4)        min:   -0.35390  max:    1.29134  float32

[![Image 4](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20424%20600'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2019/06/keras_mask_rcnn_30th_birthday.jpg)

**Figure 2:** The Mask R-CNN model trained on COCO created a pixel-wise map of the Jurassic Park jeep (truck), my friend, and me while we celebrated my 30th birthday.

For my 30th birthday, my wife found a person to drive us around Philadelphia in a replica _Jurassic Park_ jeep — here my best friend and I are outside The Academy of Natural Sciences.

Notice how not only bounding boxes are produced for each object (i.e., both people and the jeep), _but also pixel-wise masks as well!_

Let’s give another image a try:

$ python maskrcnn\_predict.py --weights mask\_rcnn\_coco.h5 --labels coco\_labels.txt \\
	--image images/couch.jpg
Using TensorFlow backend.
\[INFO\] loading Mask R-CNN model...
\[INFO\] making predictions with Mask R-CNN...
Processing 1 images
image                    shape: (682, 512, 3)         min:    0.00000  max:  255.00000  uint8
molded\_images            shape: (1, 1024, 1024, 3)    min: -123.70000  max:  151.10000  float64
image\_metas              shape: (1, 93)               min:    0.00000  max: 1024.00000  float64
anchors                  shape: (1, 261888, 4)        min:   -0.35390  max:    1.29134  float32

[![Image 5](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20424%20600'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2019/06/keras_mask_rcnn_couch.jpg)

**Figure 3:** My dog, Janie, has been segmented from the couch and chair using a Keras and Mask R-CNN deep learning model.

Here is a super adorable photo of my dog, Janie, laying on the couch:

1.  Despite the _vast majority_ of the couch not being visible, the Mask R-CNN is _still_ able to label it as such.
2.  The Mask R-CNN is correctly able to label the dog in the image.
3.  And even though my coffee cup is _barely visible_, Mask R-CNN is able to label the cup as well (if you look really closely you’ll see that my coffee cup is a _Jurassic Park_ mug!)

The only part of the image that Mask R-CNN is not able to correctly label is the back part of the couch which it mistakes as a chair — looking at the image closely, you can see how Mask R-CNN made the mistake (the region does look quite chair-like versus being part of the couch).

Here’s another example of using Keras + Mask R-CNN for instance segmentation:

$ python maskrcnn\_predict.py --weights mask\_rcnn\_coco.h5 --labels coco\_labels.txt \\
	--image images/page\_az.jpg
Using TensorFlow backend.
\[INFO\] loading Mask R-CNN model...
\[INFO\] making predictions with Mask R-CNN...
Processing 1 images
image                    shape: (682, 512, 3)         min:    0.00000  max:  255.00000  uint8
molded\_images            shape: (1, 1024, 1024, 3)    min: -123.70000  max:  149.10000  float64
image\_metas              shape: (1, 93)               min:    0.00000  max: 1024.00000  float64
anchors                  shape: (1, 261888, 4)        min:   -0.35390  max:    1.29134  float32

[![Image 6](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20424%20600'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2019/06/keras_mask_rcnn_page_az.jpg)

**Figure 4:** A Mask R-CNN segmented image (created with Keras, TensorFlow, and Matterport’s Mask R-CNN implementation). This picture is of me in Page, AZ.

A few years ago, my wife and I made a trip out to Page, AZ (this particular photo was taken just outside Horseshoe Bend) — you can see how the Mask R-CNN has not only detected me but also constructed a pixel-wise mask for my body.

Let’s apply Mask R-CNN to one final image:

$ python maskrcnn\_predict.py --weights mask\_rcnn\_coco.h5 --labels coco\_labels.txt \\
	--image images/ybor\_city.jpg
Using TensorFlow backend.
\[INFO\] loading Mask R-CNN model...
\[INFO\] making predictions with Mask R-CNN...
Processing 1 images
image                    shape: (688, 512, 3)         min:    5.00000  max:  255.00000  uint8
molded\_images            shape: (1, 1024, 1024, 3)    min: -123.70000  max:  151.10000  float64
image\_metas              shape: (1, 93)               min:    0.00000  max: 1024.00000  float64
anchors                  shape: (1, 261888, 4)        min:   -0.35390  max:    1.29134  float32

[![Image 7](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20421%20600'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2019/06/keras_mask_rcnn_ybor_city.jpg)

**Figure 5:** Keras + Mask R-CNN with Python of a picture from Ybor City.

One of my favorite cities to visit in the United States is Ybor City — there’s just something I like about the area (and perhaps it’s that the roosters are a protected in thee city and free to roam around).

Here you can see me and such a rooster — notice how each of us is correctly labeled and segmented by the Mask R-CNN. You’ll also notice that the Mask R-CNN model was able to localize each of the individual cars _and_ label the bus!

### Can Mask R-CNN run in real-time?

At this point you’re probably wondering if it’s possible to run Keras + Mask R-CNN in real-time, right?

As you know from _“The History of Mask R-CNN?”_ section above, Mask R-CNN is based on the Faster R-CNN object detectors.

**Faster R-CNNs are _incredibly_ computationally expensive,** and when you add instance segmentation on top of object detection, the model only becomes _more_ computationally expensive, therefore:

*   **On a CPU,** a Mask R-CNN cannot run in real-time.
*   **But on a GPU,** Mask R-CNN can get up to 5-8 FPS.

**If you would like to run Mask R-CNN in semi-real-time, you will need a GPU.**

### What's next? We recommend [PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend).

**Course information:**  
84 total classes • 114+ hours of on-demand code walkthrough videos • Last updated: February 2024  
★★★★★ 4.84 (128 Ratings) • 16,000+ Students Enrolled

**I strongly believe that if you had the right teacher you could _master_ computer vision and deep learning.**

Do you think learning computer vision and deep learning has to be time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a degree in computer science?

That’s _not_ the case.

All you need to master computer vision and deep learning is for someone to explain things to you in _simple, intuitive_ terms. _And that’s exactly what I do_. My mission is to change education and how complex Artificial Intelligence topics are taught.

If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here you’ll learn how to _successfully_ and _confidently_ apply computer vision to your work, research, and projects. Join me in computer vision mastery.

**Inside PyImageSearch University you'll find:**

*   ✓ **84 courses** on essential computer vision, deep learning, and OpenCV topics
*   ✓ **84 Certificates** of Completion
*   ✓ **114+ hours** of on-demand video
*   ✓ **Brand new courses released _regularly_**, ensuring you can keep up with state-of-the-art techniques
*   ✓ **Pre-configured Jupyter Notebooks in Google Colab**
*   ✓ Run all code examples in your web browser — works on Windows, macOS, and Linux (no dev environment configuration required!)
*   ✓ Access to **centralized code repos for _all_ 536+ tutorials** on PyImageSearch
*   ✓ **Easy one-click downloads** for code, datasets, pre-trained models, etc.
*   ✓ **Access** on mobile, laptop, desktop, etc.

[Click here to join PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend)

Summary
-------

In this tutorial, you learned how to use Keras + Mask R-CNN to perform **instance segmentation**.

Unlike **object detection**, which only gives you the bounding box _(x, y)_\-coordinates for an object in an image, instance segmentation takes it a step further, _**yielding pixel-wise masks for each object**_.

Using instance segmentation we can actually _segment_ an object from an image.

To perform instance segmentation we used the Matterport Keras + Mask R-CNN implementation.

We then created a Python script that:

1.  Constructed a configuration class for Mask R-CNN (both with and without a GPU).
2.  Loaded the Keras + Mask R-CNN architecture from disk
3.  Preprocessed our input image
4.  Detected objects/masks in the image
5.  Visualized the results

**If you are interested in how to:**

1.  Label and annotate your own custom image dataset
2.  And then train a Mask R-CNN model on top of your annotated dataset…

…then you’ll want to take a look at my book, **[_Deep Learning for Computer Vision with Python_](https://pyimagesearch.com/deep-learning-computer-vision-python-book/)**, where I cover Mask R-CNN and annotation in detail.

I hope you enjoyed today’s post!

**To download the source code (including the pre-trained Keras + Mask R- CNN model), _just enter your email address in the form below!_ I’ll be sure to let you know when future tutorials are published here on PyImageSearch.**

![Image 8](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20410%20567'%3E%3C/svg%3E)

#### Download the Source Code and FREE 17-page Resource Guide

Enter your email address below to get a .zip of the code and a **FREE 17-page Resource Guide on Computer Vision, OpenCV, and Deep Learning.** Inside you'll find my hand-picked tutorials, books, courses, and libraries to help you master CV and DL!