How to Run Mask R-CNN with Pytorch using the GPU on the Ubuntu

How to Run Mask R-CNN with Pytorch using the GPU on the 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:

Let’s now proceed with the installation.

1. Install Anaconda

1 - Get the download link of the lastest version of Anaconda here: 2 - Then on the terminal type:


(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

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?”


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?


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 


pip install torch==1.3.0

pip install torchvision==0.4.1

Cuda Toolkit:

conda install cudatoolkit=10.0

Install Apex:

git clone
cd apex

python install

Install pycocotools:

git clone
cd cocoapi/PythonAPI
python build_ext install

Install cityscapesScripts:

git clone
cd cityscapesScripts/
python build_ext install

Install Maskrcnn-benchmark:

git clone
cd maskrcnn-benchmark
python 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 Pytorch ubuntu

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Basic Data Types in Python | Python Web Development For Beginners

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

How To Compare Tesla and Ford Company By Using Magic Methods in Python

Magic Methods are the special methods which gives us the ability to access built in syntactical features such as ‘<’, ‘>’, ‘==’, ‘+’ etc.. You must have worked with such methods without knowing them to be as magic methods. Magic methods can be identified with their names which start with __ and ends with __ like __init__, __call__, __str__ etc. These methods are also called Dunder Methods, because of their name starting and ending with Double Underscore (Dunder).

How to Install Pyenv on Ubuntu 18.04

How to Install Pyenv on Ubuntu 18.04 - What is Pyenv? Pyenv is a fantastic tool for installing and managing multiple Python versions. It enables a developer to quickly gain access to newer

How to Install Microsoft Teams on Ubuntu 20.04

In this tutorial, we will show you how to install Microsoft Teams on Ubuntu 20.04 machine. we can install teams using Debian installer file or by adding microsoft repository.

Python Programming: A Beginner’s Guide

Python is an interpreted, high-level, powerful general-purpose programming language. You may ask, Python’s a snake right? and Why is this programming language named after it?