修改labelme源码,解决粘连mask分离问题

问题:

    我们需要训练一个粘连物体分割的模型,使用labelme进行数据标注。我在标注完以后使用labelme_json_to_dataset命令进行生成数据集的时候出现以下情况。(其中粘连部位会被其中一个类别覆盖)

                                                 

解决方案:

    首先找到json_to_dataset.py脚本(我的是mac系统位置是/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/labelme/cli/json_to_dataset.py)修改其中的以下部分:

    for shape in sorted(data["shapes"], key=lambda x: x["label"]):
        label_name = shape["label"]
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    lbl_0, lbl_1, _ = utils.shapes_to_label(
        img.shape, data["shapes"], label_name_to_value
    )

    然后修改shape.py脚本中的shapes_to_label方法。(位置在/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/labelme/utils/shape.py)

def shapes_to_label(img_shape, shapes, label_name_to_value):
    res = []
    for shape in shapes:
        cls = np.zeros(img_shape[:2], dtype=np.int32)
        ins = np.zeros_like(cls)
        instances = []
        points = shape["points"]
        label = shape["label"]
        group_id = shape.get("group_id")
        if group_id is None:
            group_id = uuid.uuid1()
        shape_type = shape.get("shape_type", None)

        cls_name = label
        instance = (cls_name, group_id)

        if instance not in instances:
            instances.append(instance)
        ins_id = instances.index(instance) + 1
        cls_id = label_name_to_value[cls_name]

        mask = shape_to_mask(img_shape[:2], points, shape_type)
        cls[mask] = cls_id
        ins[mask] = ins_id
        res.append(cls)

    return res[0], res[1], ins

    

然后使用labelme_json_to_dataset命令再生成标签的时候会出现以下两张图:

                                                          

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值