Train Custom Dataset Mask RCNN. A tutorial to easily train custom... | by Michael Chan | Towards Data Science

Step by step explanation of how to train your Mask RCNN model with custom dataset.
First of all simply clone the following repository, it is a demo of an individual class segmentation. (we will cover multiple class too).
git clone https://github.com/miki998/Custom_Train_MaskRCNN
As mentioned in the README.md it is needed to install a mrcnn lib, which is not canonically given. You will simply need to do the following:
git clone https://github.com/matterport/Mask_RCNN.gitcd Mask_RCNNpython setup.py installcd ../rm -rf Mask_RCNN
On top of all, a starting weight is needed, of course you can write your own random initializer, but in our case we decided to simply pick the default .h5 given by the original author. To get the weight, you can get it from my drive.
cd weightswget https://drive.google.com/open?id=1h62_fPkdrBufw1xCaeGwm2SgLWVtFHFQcd ../
After cloning the repository, you should get the following structure. All modifications to be made are within the train.py file, and therefore using your favorite text editor, you will simply have to edit and then add the datasets.
From line 65 till 74, simply modify the category variable and the class names for it to match your dataset (here is the what is originally written):
# define 81 classes that the coco model knowns aboutCategory = 'food'class_names = [‘bread-wholemeal’, ‘potatoes-steamed’, ‘broccoli’, ‘butter’, ‘hard-cheese’, ‘water’, ‘banana’, ‘wine-white’, ‘bread-white’, ‘apple’, ‘pizza-margherita-baked’, ‘salad-leaf-salad-green’, ‘zucchini’, ‘water-mineral’, ‘coffee-with-caffeine’, ‘avocado’, ‘tomato’, ‘dark-chocolate’, ‘white-coffee-with-caffeine’, ‘egg’, ‘mixed-salad-chopped-without-sauce’, ‘sweet-pepper’, ‘mixed-vegetables’, ‘mayonnaise’, ‘rice’, ‘chips-french-fries’, ‘carrot’, ‘tomato-sauce’, ‘cucumber’, ‘wine-red’, ‘cheese’, ‘strawberries’, ‘espresso-with-caffeine’, ‘tea’, ‘chicken’, ‘jam’, ‘leaf-spinach’, ‘pasta-spaghetti’, ‘french-beans’, ‘bread-whole-wheat’]
EXTRA NOTE: Obviously if you want to change hyper parameter such as learning step/ nb of GPU per image or batch size, here is how to do it.
class CustomConfig(Config):    """Configuration for training on the toy  dataset.    Derives from the base Config class and overrides some values.    """    # Give the configuration a recognizable name    NAME = category     # We use a GPU with 12GB memory, which can fit two images.    # Adjust down if you use a smaller GPU.   IMAGES_PER_GPU = 1     # Number of classes (including background)   NUM_CLASSES = 1 + len(class_names)  # Background + toy     # Number of training steps per epoch    STEPS_PER_EPOCH = 100     # Skip detections with < 90% confidence    DETECTION_MIN_CONFIDENCE = 0.9
You will simply need to change the parameters written on this part of train.py. For more parameters, you can check the matterport’s github: https://github.com/matterport/Mask_RCNN
Wrapping up, after putting your own dataset in the dataset folder (check inside the folders to know what to put in and the format of it), running the following command starts the training:
python3 train.py train --dataset=./dataset --weights=coco
You should get this illustration after entering the command:
Thanks a bunch for reading and stay tuned for further interesting articles ! You can contact me whenever for further information or if you wanna work together on the subject. Also, a nice click on this link (toward the affiliate program) would really help me out! You will simply have to achieve some quick tasks (simply wait and activate notifications) and all of that will really help me out for more future hardware related content!