Mask RCNN 只检测一类物体。

3
我希望在Mask RCNN目标检测中仅使用一个类别"person"(连同BG,即background)。我正在使用此链接:https://github.com/matterport/Mask_RCNN 运行mask rcnn。是否有特定的方法来完成这个任务(编辑特定文件、创建额外的Python文件或仅通过过滤class_names数组中的选择)?任何方向或解决方案将不胜感激。谢谢。
3个回答

1

我已经训练了同样的代码库用于羊。你需要做两件事:

  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()
    

然后,只需按如下方式创建您的模型:

# 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

然后,您可以使用以下代码对其进行训练:
model.train(dataset_train, dataset_val,
        learning_rate=config.LEARNING_RATE, 
        epochs=100, 
        layers='heads')#You can also use 'all' to train all network.

不要忘记使用tensorflow 1.x和keras 2.1.0 :) 我可以使用这些版本进行训练。


1
我尝试按照@dnl_anoj的建议“仅显示人物类别的结果”。我从预测结果中删除了除人物类别之外的所有类别。您可以在https://github.com/matterport/Mask_RCNNpredictor.py文件中的run_on_opencv_image()函数中使用以下代码。请保留HTML标签。
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)

该文件在链接的仓库中不存在。你编辑了哪个文件? - Dan Taninecz Miller

0

所以程序已经对“person”类进行了预训练。是否没有办法过滤掉其他类别的检测结果,只检测到“person”类别?我们是否需要创建另一个数据集进行预训练? - sv176
1
哦,抱歉我误解了你的问题。Mask RCNN存储库中提供了一些笔记本电脑,例如查看此笔记本电脑:https://github.com/matterport/Mask_RCNN/blob/master/samples/coco/inspect_data.ipynb 它有助于在随机图像上可视化模型的结果。检测到的类别用其ID表示,您可以在第三个单元格的结果中看到。'Person'的ID = 1,因此只选择具有ID = 1的掩码或框不应该是问题! - dnl_anoj
1
我想在我的评论中补充一点,如果你只是想检测人,你可以使用像YOLO这样的物体检测模型,它们更快且相当可靠。特别是OpenCV有一个实现,比Mask RCNN更容易使用,我想:https://www.data-stats.com/person-detection-using-yolo-and-opencv/ - dnl_anoj
1
所以我基本上想要创建黑色背景下的人物白色剪影。关于yolo我不太确定,因为它会为检测创建边界框。这将使剪影创建过程稍微复杂一些。 - sv176
Mask RCNN非常适合这个任务。 - dnl_anoj
dnl_anoj,你能否提供第二条评论的示例代码(通过指定ID选择掩码)?我无法仅使用图像中“人”上的掩码获得输出图像。 - sv176

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接