2

I am trying to train my own COCO dataset with detectron2, however when I start my own training I encounter a key error

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()

so I went back to check on my COCO json file, however the json file was output with standard COCO format, any idea what might cause the problem

p.s I am training the data with the detectron2 sample code, so I think there shouldn't be a problem

eric chen
  • 33
  • 1
  • 5

1 Answers1

0

I am guessing (without looking at your json construction code) that you are missing 'category_id' in annotations. you can follow as below for one class.

 # 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)

Hope you got the idea.

CognitiveRobot
  • 1,337
  • 1
  • 9
  • 26