微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Detectron2培训KeyError

如何解决Detectron2培训KeyError

我正在尝试使用detectron2训练自己的COCO数据集,但是当我开始自己的训练时,遇到一个关键错误

KeyError:'category_id

error code : https://i.stack.imgur.com/yO5IO.png

//this is the code i am training with 
from detectron2.engine import DefaultTrainer
from detectron2.config import get_cfg
import os

cfg = get_cfg()
cfg.merge_from_file(model_zoo.get_config_file("COCO- 
InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
cfg.DATASETS.TRAIN = ("coco_train_new",)
cfg.DATASETS.TEST = ()
cfg.DataLoader.NUM_WORKERS = 2
cfg.MODEL.WEIGHTS = "detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl"  
cfg.soLVER.ims_PER_BATCH = 2
cfg.soLVER.BASE_LR = 0.00025  # pick a good LR
cfg.soLVER.MAX_ITER = 1000    # 300 iterations seems good enough for this toy dataset; you may need to train longer for a practical dataset
cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = 512   # faster,and good enough for this toy dataset (default: 512)
cfg.MODEL.ROI_HEADS.NUM_CLASSES = 1  # only has one class (ballon)

os.makedirs(cfg.OUTPUT_DIR,exist_ok=True)
trainer = DefaultTrainer(cfg) 
trainer.resume_or_load(resume=False)
trainer.train()

所以我返回检查我的COCO json文件,但是json文件以标准COCO格式输出, 任何想法可能导致问题的原因

p.s我正在用detectron2示例代码训练数据,所以我认为应该没问题

解决方法

我正在猜测(不查看您的json构造代码)您在注释中缺少“ category_id”。您可以按照以下步骤进行一堂课。

 # Main dict
    dataset_dicts = {"images": [],"type": "Balloon-detection","annotations": [],"categories": []
                     }
    # Adding categories. At the moment only for balloon.
    category = {'supercategory': 'object','id': 1,'name': 'balloon'}
    dataset_dicts['categories'].append(category)

    for <iterate for all images>:
    ....
    ....
        for <iterate for all objects>:
        ....
        ....
            # Save annotation
            annotation = {
                    'image_id': index,"bbox": [xmin,ymin,o_width,o_height],"area": o_width*o_height,"bbox_mode": BoxMode.XYWH_ABS,"category_id": 1,"iscrowd": 0,'id': annotation_id
                }
                dataset_dicts['annotations'].append(annotation)

希望您有主意。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。