Mask R-CNN with TensorFlow 2 on Windows 10

Glasses detected with Mask R-CNN. Learn more about how I made the custom COCO dataset in this video!

Glasses detected with Mask R-CNN. Learn more about how I made the custom COCO dataset in this video!

Start Here

Matterport’s Mask R-CNN is an amazing tool for instance segmentation. It works on Windows, but as of June 2020, it hasn’t been updated to work with Tensorflow 2. For that reason, installing it and getting it working can be a challenge.

Since the Complete Guide to Creating COCO Datasets course uses Mask R-CNN, I wanted to see if I could get a newer version to make setup easier.

Turns out GitHub user @leekunhee had a pull request waiting with most of the changes implemented. Rather than wait to see if/when the Matterport repo maintainers would take up the fix, I decided to copy his changes over to my own repo and add a few more of my own (yay MIT license!!).

Updated Mask R-CNN Repo

https://github.com/akTwelve/Mask_RCNN


Prerequisites

Anaconda

While there are other ways to install Python, I find that Anaconda is the easiest way to manage multiple Python environments. It’s completely free and works on Windows, Mac, and Linux

Once Anaconda is installed, you will need to set up a new environment for ML-Agents.

This will create a new Python 3.7 environment called “mask_rcnn”. Nothing special about the name mask_rcnn at this point, it’s just informative.

  • Type “y” and press Enter to proceed

  • Follow the instructions to activate the environment. In my case, I ran

    conda activate mask_rcnn
  • Confirm that the environment is active by looking for “(mask_rcnn)” on the left side of the command prompt

CUDA 10.1

Download and install CUDA Toolkit 10.1 Update 2 from the CUDA Toolkit Archive.

You can’t always assume that the latest version of CUDA will be compatible with the version of Tensorflow you’re using. For Tensorflow 2.2, the recommended configuration is CUDA 10.1.

tensorflow2.2-GPU-CUDA-recommendedConfig.png

Make sure your PATH is set up properly to set CUDA_PATH to the CUDA\v10.1 folder. You’ll notice in the image below that I also have 10.2 installed, but CUDA_PATH is pointing to v10.1.

cuda_path.png

⛔ Note: If you don’t install version 10.1 and you try to use Tensorflow 2.2, you will get an error like this:

Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found

This happens because Tensorflow is looking for a DLL and can’t find it. CUDA RunTime 64bit 10.1 - make sense? Took me a month to figure this one out. Turns out it was finding cudart64_102.dll because I had CUDA 10.2 installed and the CUDA_PATH variable was pointing there instead.


Setup Instructions

1. Clone the Updated Mask_RCNN Repository

The following command will create a new folder called “aktwelve_mask_rcnn”, which will differentiate it from the original Matterport version, in case you already have that cloned. You should run this command from Git Bash.

git clone https://github.com/akTwelve/Mask_RCNN.git aktwelve_mask_rcnn

2. Install Dependencies

In Anaconda, make sure you’re in the mask_rcnn environment, then change directory into aktwelve_mask_rcnn.

Run the following command:

pip install -r requirements.txt

3. Run setup from the repository root directory

Run this command to install Mask R-CNN into your Anaconda Python environment:

python setup.py clean --all install

This also cleans up any previous installs in case you modified the Mask R-CNN code at all (something I had to do a lot when I was testing updates).

Using Mask R-CNN

Now you can use Mask R-CNN with Tensorflow 2.2 on Windows 10, with CUDA 10.1. I’ve got another tutorial for how to use it, so check that out here.