A simple guide to Mask R-CNN implementation on a custom dataset.

A step by step tutorial to train the multi-class object detection model on your own dataset.

Soumya Yadav
Analytics Vidhya
5 min readAug 2, 2020

--

Output result for Object detection by MaskRCNN. (source)

In
this article, we will understand concisely different methods of object detection followed by simple steps to train your own model.

Comparison between Object localization, Semantic segmentation and Instance segmentation. (source)

Object detection can be broadly classified into three broad categories:
(a) Object localization
(b) Semantic segmentation
(c) Instance segmentation

Object localization refers to the detection of an object in the image and further assigning those objects a single bounding box. Semantic segmentation is a recently developed approach for object detection where every pixel is assigned a class. As shown in the above image, the bottle is marked with one type of colour and cube with another. This method groups similar type of objects into one single category. There are various state-of-the-art algorithms being developed in this category.
The third type of detection is instance segmentation, which involves detecting and assigning every individual object in the image a separate identity.

Out of all the three methods described here, instance segmentation is the most challenging task in the field of computer vision. It has a vast use in the field of Autonomous driving and perception which makes it even more interesting.

The Code:

With this brief introduction to object detection, let’s start the simple implementation of MaskRCNN. Please follow the step by step procedure as mentioned below.

Step 1: Clone the repository.

Please clone the custom MaskRCNN repository given below:

Please make sure that all the dependencies are installed which are given in readme file.

Step 2: Prepare the data.

Prepare your data by using the following procedure:

1. Collect all the images into one single folder which are to be used for training.
2. Divide the dataset roughly into the 90:10 ratio for training and validation
3. Add the training images into the following folder named ‘train’

4. Add the training images into the following folder named ‘val’

5. Annotate the images using image annotation software. There are many tools available for image annotations. I usually prefer Makesense.ai as there is no need to download/install it. Also, it does not store any image in its own server.
6. After annotation save the file to ‘train’ folder as “annotations.json”.
7. After validation image annotations, save the file to ‘val’ folder as “annotations.json”

Step 3: Prepare the model.

After annotation, open the ‘custom.py’ file given in

Now follow the given steps:

  1. (Line 91) Add the names of the categories according to your requirements. I am here using two categories to train my model, bicycles and cars. Please feel free to make further changes according to your needs. Remember to input the class name exactly the same way as you have mentioned in annotations (even the spaces)
Custom.py file. (Image by Author)

2. (Line 140) Add the class labels in the same manner as you have added in the above step to map the label names with class number.

Custom.py file. (Image by Author)

3. (Line 71) Add the number of classes after the plus sign. For example, here the number of classes for me are 2 (Car and bicycle).

Custom.py file. (Image by Author)

With these three small steps, you are ready to train the model!

Step 4: Train the model.

If you are windows user go to the ‘custom’ folder in file explorer and type ‘cmd’ in the address bar above and press enter as shown in the figure.

Custom_MaskRCNN file folder. (Image by Author)

After opening command prompt paste the following command

Paste your path to the dataset between ‘dataset=’ and ‘ — weightsthen press enter. If you are training for the first time, then it will take some time to download the coco weights depending on your internet speed. After the model starts training your screen should look like this.

MaksRCNN training. (Image by Author)

Step 5: Results

After your model is trained, start testing it by opening the python notebook in the custom folder.

  1. Open ‘demo.ipynb’ by typing ‘jupyter notebook’ in the address bar of the custom folder.
  2. Go to logs folder in main CustomMask_RCNN folder and find the most recent folder. The folder name should start from ‘custom’ and followed by a unique number. Example- ‘custom0001293487’. Change the name of the most recent folder to ‘custom’.
  3. Insert the images you need to test your model in the ‘images’ folder at the root directory.
  4. Add the names of the classes you trained your model upon in the cell no. 4. Please add the class names in the sequence you have entered in the ‘custom.py’ file. Do not remove the class ‘BG’. Add your class names after it, as shown below.
Class names. (Image by Author)

5. Run all the cells to generate the final output.

Output result for Object detection by MaskRCNN. (source)

I hope this will prove to be a helpful guide for MaskRCNN implementation. The most exciting thing I found was the level of application it has, which ranges from autonomous driving to weed detection for farmers.

If you have any doubt, please leave a comment below or go to my repository to raise an issue.

Github link:

--

--

Soumya Yadav
Analytics Vidhya

I like to build stuff and experiment | Investment @Kalaari Capital