【问题标题】:Mask RCNN 1 class only仅掩码 RCNN 1 类
【发布时间】:2021-04-24 21:17:29
【问题描述】:

我希望仅使用一类人(以及 BG、背景)来进行 Mask RCNN 对象检测。我正在使用这个链接:https://github.com/matterport/Mask_RCNN 来运行掩码 rcnn。有没有特定的方法来完成这个(编辑特定文件,创建一个额外的 python 文件,或者只是通过从 class_names 数组中过滤选择)?任何方向或解决方案都将受到高度赞赏。谢谢

【问题讨论】:

    标签: tensorflow image-processing keras computer-vision object-detection


    【解决方案1】:

    您链接的 github 的作者制作了一个气球示例,它写得非常好,只包含一个类(气球),您应该遵循本教程:https://engineering.matterport.com/splash-of-color-instance-segmentation-with-mask-r-cnn-and-tensorflow-7c761e238b46

    【讨论】:

    • 所以程序已经预训练了 'person' 类。有没有办法过滤检测,以便只检测到“人”而不检测其他类?我们是否需要专门创建另一个数据集来进行预训练?
    • 抱歉,我误解了你的问题。 Mask RCNN 存储库中提供了一些笔记本,例如:github.com/matterport/Mask_RCNN/blob/master/samples/coco/… 它有助于在随机图像上可视化模型的结果。检测到的类用它们的 ID 表示,您可以在第三个单元格的结果中看到。 'Person' 的 ID = 1,因此只选择 ID=1 的掩码或框应该不是问题!
    • 我想在我的评论中补充一点,如果您只想检测人,您可以使用更快且非常可靠的对象检测模型,例如 YOLO。特别是 OpenCV 有一个实现,它比我认为的 Mask RCNN 更容易使用:data-stats.com/person-detection-using-yolo-and-opencv
    • 所以我基本上想创建人的白色剪影(在黑色背景中)。对 yolo 不太确定,因为它会为检测创建边界框。它会使轮廓创建过程稍微复杂
    • Mask RCNN 非常适合这种情况。
    【解决方案2】:

    我已经为羊训练了相同的 repo。你必须做两件事:

    1. 将训练和推理类编号更改为 1 + 1( bg 和 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. 您需要创建数据集以进行训练。你可以按如下方式使用 coco:

       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 :) 我可以使用这些版本进行训练。

    【讨论】:

      【解决方案3】:

      我尝试遵循@dnl_anoj 建议的“仅显示人员类的结果”。我从预测结果中删除了除人员类之外的所有类。您可以在https://github.com/matterport/Mask_RCNN 中的predictor.py 文件中的run_on_opencv_image() 函数中使用以下代码。

      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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-21
        • 2021-05-01
        • 2018-11-21
        • 1970-01-01
        • 1970-01-01
        • 2020-02-28
        • 1970-01-01
        • 2020-04-19
        相关资源
        最近更新 更多