将注释从 Mask-RCNN 数据集格式转换为 COCO 格式

Bur*_*maz 5 python machine-learning image-processing computer-vision deep-learning

我想训练一个模型来检测图像中的车辆和道路。为此,我将使用 Mask R-CNN 和 YOLACT++。我使用 vgg 图像注释器为 Mask R-CNN 标记了一些图像,分割点如下图所示。

在此处输入图片说明

如您所见,没有 area 参数或 bbox 参数。我可以使用 minx miny maxx maxy 找到我的实例的 bbox,但我找不到如何找到该分割区域的区域。您可以在下图中看到 Yolac 注释结构。

在此处输入图片说明

标记所有实例需要大量时间。我花了至少 10 分钟来标记图像中的所有汽车,而且我已经有 500 个标记的图像。您对我有什么建议或想法可以帮助我节省时间,同时将第一个注释格式转换为第二个注释格式(掩码 r-cnn 到 coco(yolact))?

小智 3

类似这样的东西,但这取决于你如何在 vgg 中注释

def vgg_to_coco(vgg_path: str, outfile: str=None, class_keyword: str = "Class"):
    with open(vgg_path) as f:
        vgg = json.load(f)

    images_ids_dict = {v["filename"]: i for i, v in enumerate(vgg.values())}
    # TDOD fix
    images_info = [{"file_name": k, "id": v, "width": 1024, "height": 1024} for k, v in images_ids_dict.items()]

    classes = {class_keyword} | {r["region_attributes"][class_keyword] for v in vgg.values() for r in v["regions"]
                                 if class_keyword in r["region_attributes"]}
    category_ids_dict = {c: i for i, c in enumerate(classes, 1)}
    categories = [{"supercategory": class_keyword, "id": v, "name": k} for k, v in category_ids_dict.items()]
    annotations = []
    suffix_zeros = math.ceil(math.log10(len(vgg)))
    for i, v in enumerate(vgg.values()):
        for j, r in enumerate(v["regions"]):
            if class_keyword in r["region_attributes"]:
                x, y = r["shape_attributes"]["all_points_x"], r["shape_attributes"]["all_points_y"]
                annotations.append({
                    "segmentation": [list(chain.from_iterable(zip(x, y)))],
                    "area": helper.polygon_area(x, y),
                    "bbox": helper.bbox(x, y, out_format="width_height"),
                    "image_id": images_ids_dict[v["filename"]],
                    "category_id": category_ids_dict[r["region_attributes"][class_keyword]],
                    "id": int(f"{i:0>{suffix_zeros}}{j:0>{suffix_zeros}}"),
                    "iscrowd": 0
                })

    coco = {
        "images": images_info,
        "categories": categories,
        "annotations": annotations
    }
    if outfile is None:
        outfile = vgg_path.replace(".json", "_coco.json")
    with open(outfile, "w") as f:
        json.dump(coco, f)
Run Code Online (Sandbox Code Playgroud)

您必须将 1024s 更改为您的图像尺寸,或者如果您有可变的图像尺寸,则必须为此创建一个地图。