facebookresearch / detectron2

Detectron2 is a platform for object detection, segmentation and other visual recognition tasks.
https://detectron2.readthedocs.io/en/latest/
Apache License 2.0
30.19k stars 7.44k forks source link

Load a previous dumped config fails with "KeyError: 'Non-existent config key: SOLVER.CLIP_GRADIENTS'" #1141

Closed fwarmuth closed 4 years ago

fwarmuth commented 4 years ago

Hey all, Lets say i create a cfg from a config of the zoo:

    MODEL_CONFIG_FILE="COCO-Detection/faster_rcnn_R_50_C4_1x.yaml"
    cfg = get_cfg()
    cfg.merge_from_file(model_zoo.get_config_file(MODEL_CONFIG_FILE))
    cfg.DATASETS.TRAIN = (DATASET_NAME + "_train",)
    cfg.DATASETS.TEST = (DATASET_NAME + "_eval",)
    cfg.DATALOADER.NUM_WORKERS = 2
    cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(MODEL_CONFIG_FILE) 
    cfg.SOLVER.IMS_PER_BATCH = 32
    cfg.SOLVER.CHECKPOINT_PERIOD = 500
    cfg.SOLVER.BASE_LR = 0.00025  # pick a good LR
    cfg.SOLVER.MAX_ITER = args.solver_max_iter   
    cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = 128  
    cfg.MODEL.ROI_HEADS.NUM_CLASSES = len(args.classes_list)  
    cfg.OUTPUT_DIR = args.output_dir

I want to save that config to a yaml file:

    yaml_str = cfg.dump()
    with open(os.path.join(cfg.OUTPUT_DIR, 'config.yaml'), 'w', encoding='utf-8') as output_file:
        logging.debug("Writing config to output dir: %s" % os.path.join(cfg.OUTPUT_DIR, 'config.yaml'))
        output_file.write(yaml_str)

Then i use the above config to train: trainer = DefaultTrainer(cfg). All works fine to here.

Later down the line i then want to load that model and the used config in my inference machine:

    self.cfg = get_cfg()
    self.cfg.merge_from_file(PATH_TO_THE_CONFIG_YAML_CREATED_ABOVE) <<<<--< exception
    self.cfg.MODEL.WEIGHTS = PATH_TO_FINAL_PTH

But i get:

  File "**NothingToSeeHere**/lib/python3.6/site-packages/detectron2/config/config.py", line 47, in merge_from_file
    self.merge_from_other_cfg(loaded_cfg)
  File "**NothingToSeeHere**/lib/python3.6/site-packages/fvcore/common/config.py", line 124, in merge_from_other_cfg
    return super().merge_from_other_cfg(cfg_other)
  File "**NothingToSeeHere**/lib/python3.6/site-packages/yacs/config.py", line 217, in merge_from_other_cfg
    _merge_a_into_b(cfg_other, self, self, [])
  File "**NothingToSeeHere**/lib/python3.6/site-packages/yacs/config.py", line 460, in _merge_a_into_b
    _merge_a_into_b(v, b[k], root, key_list + [k])
  File "**NothingToSeeHere**/lib/python3.6/site-packages/yacs/config.py", line 473, in _merge_a_into_b
    raise KeyError("Non-existent config key: {}".format(full_key))
KeyError: 'Non-existent config key: SOLVER.CLIP_GRADIENTS'

I expect to be able to dump and load the config using the cfg node. What am i doing wrong?

regards

fwarmuth commented 4 years ago

btw. it works if i delete the failed section from the dumped yaml file:

...
  CLIP_GRADIENTS:
    CLIP_TYPE: value
    CLIP_VALUE: 1.0
    ENABLED: false
    NORM_TYPE: 2.0
...

But expected would be that this works without manual editing.

ppwwyyxx commented 4 years ago

If you need help to solve an unexpected issue you observed, please include details following the issue template.