解决bug : KeyError: ‘Non-existent config key: MODEL.×××××××ב

解决bug: KeyError: ‘Non-existent config key: MODEL.××××××××’

时间:2023.4.3

一、背景

用detectron2跑代码,在配置文件(*.yaml)中增加一个变量,再运行会报错:

File "/root/detectron2/config/config.py", line 69, in merge_from_file
    self.merge_from_other_cfg(loaded_cfg)
  File "/root/anaconda3/envs//lib/python3.7/site-packages/fvcore/common/config.py", line 132, in merge_from_other_cfg
    return super().merge_from_other_cfg(cfg_other)
  File "/root/anaconda3/envs//lib/python3.7/site-packages/yacs/config.py", line 217, in merge_from_other_cfg
    _merge_a_into_b(cfg_other, self, self, [])
  File "/root/anaconda3/envs//lib/python3.7/site-packages/yacs/config.py", line 478, in _merge_a_into_b
    _merge_a_into_b(v, b[k], root, key_list + [k])
  File "/root/anaconda3/envs//lib/python3.7/site-packages/yacs/config.py", line 491, in _merge_a_into_b
    raise KeyError("Non-existent config key: {}".format(full_key))
KeyError: 'Non-existent config key: MODEL.××××××××'

二、解决方法

在train_net.py中,setup()函数中,一定要有add_***_config(cfg)

def setup(args):
    """
    Create configs and perform basic setups.
    """
    cfg = get_cfg()
    add_******_config(cfg)
    cfg.merge_from_file(args.config_file)
    cfg.merge_from_list(args.opts)
    cfg.freeze()
    default_setup(cfg, args)
    return cfg

然后来到add_***_config()函数代码,添加刚才在ymal配置文件新增的变量。

def add_****_config(cfg):
	cfg.MODEL.temp = True  # 新增加的变量temp

你可能感兴趣的:(bug)