【问题标题】:Inference on image dataset without annotations in detectron2在detectron2中没有注释的图像数据集的推断
【发布时间】:2022-01-08 04:20:24
【问题描述】:

动机

  • 我有一个detectron2 Mask R-CNN 基线模型,它足以准确预测某些对象边界。
  • 我想将这些预测边界转换为 COCO 多边形,以注释下一个数据集(监督标注)。
  • 为此,我需要在没有注释的图像数据集上运行推理。
  • detectron2 方法 register_coco_instancesload_coco_json 需要带有注释的图像才能正确标记预测对象。

问题

  • 我可以在没有注释文件的情况下注册测试数据集吗?
  • 如果没有,最简单的方法是生成COCOLabelme JSON 文件,其中包含基本图像信息而没有注释?

代码

dataset_name = "test_data"
image_dir = "data/test"
coco_file = "data/test_annotations.json"

# Register dataset
# A COCO file is needed with image info, which I don't have
register_coco_instances(dataset_name , {}, coco_file, image_dir)
test_dict = load_coco_json(coco_file, image_dir, dataset_name=dataset_name )
metadata = MetadataCatalog.get(dataset_name)

# config details omitted for brevity
cfg = get_cfg()
predictor = DefaultPredictor(cfg)

# Make predictions for all images
for sample in test_dict:
    image_filename = sample["file_name"]
    img = cv2.imread(image_filename)
    outputs = predictor(img)
    # Display or save image with predictions to file

【问题讨论】:

    标签: python image-segmentation detectron labelme


    【解决方案1】:

    这是一种从图像目录生成图像详细信息并将其写入现有 COCO JSON 文件的方法:

    from PIL import Image
    
    def add_images_to_coco(image_dir, coco_filename):
        image_filenames = list(Path(image_dir).glob('*.jpg'))
        images = []
        for i, image_filename in enumerate(image_filenames):
            im = Image.open(image_filename)
            width, height = im.size
            image_details = {
                "id": id + 1,
                "height": height,
                "width": width,
                "file_name": str(image_filename.resolve()),
            }
            images.append(image_details)
    
        # This will overwrite the image tags in the COCO JSON file
        with open(coco_filename) as f:
            data = json.load(f)
    
        coco['images'] = images
    
        with open(coco_filename, 'w') as coco_file:
            json.dump(data, coco_file, indent = 4)
    

    如果您还没有类别,则需要使用您的类别创建一个基线 COCO JSON 文件。它应该看起来像这样:

    {
        "images": [ ],
        "annotations": [ ],
        "categories": [
            {
                "id": 1,
                "name": "Car",
                "supercategory": "Car"
            },
            {
                "id": 2,
                "name": "Person",
                "supercategory": "Person"
            }
        ]
    }
    

    【讨论】:

      猜你喜欢
      • 2021-02-24
      • 1970-01-01
      • 2021-10-22
      • 2021-05-18
      • 1970-01-01
      • 1970-01-01
      • 2019-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多