Run Mask R-CNN on GPU with Pytorch (on Ubuntu)

In this tutorial we are going to see how to run the Mask R-CNN algorythm using the GPU on the Ubuntu os system.

System requirements:

  • OS Linux Ubuntu 18.04 (it might work with earlier versions as well, but I don’t guarantee that)
  • Nvidia GPU with CUDA support.
    Click here to see the list with all the GPUs which support cuda.

What is Mask R-CNN?

Mask R-CNN is a Convolutional Neural Network used in Computer vision which is able to perform Object Detection and Istance segmentation.

To describe in simple words how this algorythm works, we can divide it into three stages:

  • On the 1st stage there is a RPN (Region Proposal Network) which proposes the Regions where objects could be. This method is really useful to discard some areas and so to reduce the computetional requirements later on for the object detection.
  • On the 2nd stage these regions are passed through a Convolutional Neural Network which detects to what category the object belongs.
  • On the 3rd stage for each Object detected, is also created a Mask which exactly identifies the location of the object, so that we can have clear boundaries of the objects (not simple boxes).

How to Run Mask R-CNN on Ubuntu

Let’s now see the instructions about how to run this algorythm on Ubuntu. Let me clarify first that there is not only one way to do that, but there are many ways.

The Mask R-CNN algorythm to run needs a deep learning framework. At the moment the most common deep learning frameworks are: tensorflow, pytorch and keras. For each of them there is an implementation of the algorythm.

I choosed for this article to run it on the Pytorch framework.
For a different framework, you need of course a different implementation of the algorythm.

This is the github repository of the code we’re going to use: https://github.com/facebookresearch/maskrcnn-benchmark

Let’s now proceed with the installation.

1. Install Anaconda

  1. Get the download link of the lastest version of Anaconda here: https://www.anaconda.com/distribution/
  2. Then on the terminal type:
    wget https://repo.anaconda.com/archive/Anaconda3-2019.10-Linux-x86_64.sh

    (You have to change the link with your link for the lastest version).
    Then after the download is finish you can proceed with the installation.
    sudo bash Anaconda3-2019.10-Linux-x86_64.sh
  3. Press enter until you see the “License agreement”, then you press the spacebar until you see the question: “Do you accept the license terms?”
    yes
    and press enter.
  4. Press enter again to confirm the default location where you want to install anaconda.
  5. “Do you wish the installer to inizialize Anaconda3 by running conda init?”
    Yes
    and press enter.
  6. Give administrator permissions to use Anaconda and create virtual environments:
    sudo chown -R $USER anaconda3
    sudo chwon -R $USER sudo chown R ~/.conda

2. Install Libraries

Once we have anaconda installed we can install all the libraries necesary to run Mask R-CNN

Create an environment:

 conda create --name maskrcnn_benchmark -y
 conda activate maskrcnn_benchmark 


Pytorch:

pip install torch==1.3.0
pip install torchvision==0.4.1

Cuda Toolkit:

conda install cudatoolkit=10.0

Install Apex:

cd $INSTALL_DIR
git clone https://github.com/NVIDIA/apex.git
cd apex
python setup.py install

Install pycocotools:

cd $INSTALL_DIR
git clone https://github.com/cocodataset/cocoapi.git
cd cocoapi/PythonAPI
python setup.py build_ext install

Install cityscapesScripts:

cd $INSTALL_DIR
git clone https://github.com/mcordts/cityscapesScripts.git
cd cityscapesScripts/
python setup.py build_ext install

Install Maskrcnn-benchmark:

cd $INSTALL_DIR
git clone https://github.com/facebookresearch/maskrcnn-benchmark.git
cd maskrcnn-benchmark
python setup.py build develop

3. Run Mask R-CNN

Once we are still inside the conda environment and the folder maskrcnn-benchmark we can run the algorythm with simply two lines of code:

cd demo
python webcam.py
Blueprint

Learn to build Computer Vision Software easily and efficiently.

This is a FREE Workshop where I'm going to break down the 4 steps that are necessary to build software to detect and track any object.

Sign UP for FREE