Mask RCNN 1 class only

Viewed 3021

I am looking to use only one class, person (along with BG, background), for the Mask RCNN object detection. I am using this link: https://github.com/matterport/Mask_RCNN to run the mask rcnn. Is there a specific way to complete this (editing specific files, creating an extra python file, or just by filtering selections from the class_names array)? Any direction or solution will be highly appreciated. Thank you

3 Answers

I've trained the same repo for sheeps. You have to do 2 things:

  1. Change the train and inference class numbers as 1 + 1 ( bg and person ):

     class SheepsConfig(Config):
    
         NAME = "sheeps"
         NUM_CLASSES = 1 + 1 # background + sheep
    
     config = SheepsConfig()  # Don't forget to use this config while creating your model
     config.display()
    
  2. You need to create dataset to train on. You can use coco as follows:

     import coco
     from pycocotools.coco import COCO
    
     ct = COCO("/YourPathToCocoDataset/annotations/instances_train2014.json")
     ct.getCatIds(['sheep']) 
     # Sheep class' id is 20. You should run for person and use that id
    
     COCO_DIR = "/YourPathToCocoDataset/"
     # This path has train2014, annotations and val2014 files in it
    
     # Training dataset
     dataset_train = coco.CocoDataset()
     dataset_train.load_coco(COCO_DIR, "train", class_ids=[20])
     dataset_train.prepare()
    
     # Validation dataset
     dataset_val = coco.CocoDataset()
     dataset_val.load_coco(COCO_DIR, "val", class_ids=[20])
     dataset_val.prepare()
    

And then simply create your model as:

# Create model in training mode
model = modellib.MaskRCNN(mode="training", config=config, model_dir=MODEL_DIR)
model.load_weights(COCO_MODEL_PATH, by_name=True, exclude=["mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask"])
# This COCO_MODEL_PATH is the path to the mask_rcnn_coco.h5 file in this repo

Then you can train it with this code:

model.train(dataset_train, dataset_val,
        learning_rate=config.LEARNING_RATE, 
        epochs=100, 
        layers='heads')#You can also use 'all' to train all network.

Don't forget to use tensorflow 1.x and keras 2.1.0 :) I can train with these versions.

I tried to follow what @dnl_anoj suggested "showing results of only person classes". I removed all classes but the person class from the predicted results . You can use the following code in run_on_opencv_image() function in the predictor.py file in https://github.com/matterport/Mask_RCNN.

predictions = self.coco_demo.compute_prediction(image)
top_predictions = self.coco_demo.select_top_predictions(predictions)

masks = top_predictions.get_field("mask")
boxes = top_predictions.bbox
label_indexs = top_predictions.get_field("labels").numpy()

x = np.where(label_indexs != 1) # get indexes of labels which are not person

#remove items which are not person class
masks = np.delete(masks,x, axis=0)
boxes = np.delete(boxes,x, axis=0)
label_indexs = np.delete(label_indexs,x)
labels = self.convert_label_index_to_string(label_indexs)
Related