Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Advanced Computer Vision with TensorFlow/Week 3 - Image Segmentation/Copy of C3_W3_Lab_3_Mask-RCNN-ImageSegmentation.ipynb
Views: 13370
Ungraded Lab: Mask R-CNN Image Segmentation Demo
In this lab, you will see how to use a Mask R-CNN model from Tensorflow Hub for object detection and instance segmentation. This means that aside from the bounding boxes, the model is also able to predict segmentation masks for each instance of a class in the image. You have already encountered most of the commands here when you worked with the Object Dection API and you will see how you can use it with instance segmentation models. Let's begin!
Note: You should use a TPU runtime for this colab because of the processing requirements for this model. We have already enabled it for you but if you'll be using it in another colab, you can change the runtime from Runtime --> Change runtime type
then select TPU
.
Installation
As mentioned, you will be using the Tensorflow 2 Object Detection API. You can do that by cloning the Tensorflow Model Garden and installing the object detection packages just like you did in Week 2.
Import libraries
Utilities
For convenience, you will use a function to convert an image to a numpy array. You can pass in a relative path to an image (e.g. to a local directory) or a URL. You can see this in the TEST_IMAGES
dictionary below. Some paths point to test images that come with the API package (e.g. Beach
) while others are URLs that point to images online (e.g. Street
).
Load the Model
Tensorflow Hub provides a Mask-RCNN model that is built with the Object Detection API. You can read about the details here. Let's first load the model and see how to use it for inference in the next section.
Inference
You will use the model you just loaded to do instance segmentation on an image. First, choose one of the test images you specified earlier and load it into a numpy array.
You can run inference by simply passing the numpy array of a single image to the model. Take note that this model does not support batching. As you've seen in the notebooks in Week 2, this will output a dictionary containing the results. These are described in the Outputs
section of the documentation
Visualizing the results
You can now plot the results on the original image. First, you need to create the category_index
dictionary that will contain the class IDs and names. The model was trained on the COCO2017 dataset and the API package has the labels saved in a different format (i.e. mscoco_label_map.pbtxt
). You can use the create_category_index_from_labelmap internal utility function to convert this to the required dictionary format.
Next, you will preprocess the masks then finally plot the results.
The result dictionary contains a
detection_masks
key containing segmentation masks for each box. That will be converted first to masks that will overlay to the full image size.You will also select mask pixel values that are above a certain threshold. We picked a value of
0.6
but feel free to modify this and see what results you will get. If you pick something lower, then you'll most likely notice mask pixels that are outside the object.As you've seen before, you can use
visualize_boxes_and_labels_on_image_array()
to plot the results on the image. The difference this time is the parameterinstance_masks
and you will pass in the reframed detection boxes to see the segmentation masks on the image.
You can see how all these are handled in the code below.