MCPcopy Create free account
hub / github.com/UX-Decoder/Semantic-SAM / load_opt_command

Function load_opt_command

utils/arguments.py:67–107  ·  view source on GitHub ↗
(args)

Source from the content-addressed store, hash-verified

65
66
67def load_opt_command(args):
68 parser = argparse.ArgumentParser(description='Pretrain or fine-tune models for NLP tasks.')
69 parser.add_argument('command', help='Command: train/evaluate/train-and-evaluate')
70 parser.add_argument('--conf_files', nargs='+', required=True, help='Path(s) to the config file(s).')
71 parser.add_argument('--user_dir', help='Path to the user defined module for tasks (models, criteria), optimizers, and lr schedulers.')
72 parser.add_argument('--config_overrides', nargs='*', help='Override parameters on config with a json style string, e.g. {"<PARAM_NAME_1>": <PARAM_VALUE_1>, "<PARAM_GROUP_2>.<PARAM_SUBGROUP_2>.<PARAM_2>": <PARAM_VALUE_2>}. A key with "." updates the object in the corresponding nested dict. Remember to escape " in command line.')
73 parser.add_argument('--overrides', help='arguments that used to override the config file in cmdline', nargs=argparse.REMAINDER)
74
75 cmdline_args = parser.parse_args() if not args else parser.parse_args(args)
76
77 opt = load_opt_from_config_files(cmdline_args.conf_files)
78
79 if cmdline_args.config_overrides:
80 config_overrides_string = ' '.join(cmdline_args.config_overrides)
81 logger.warning(f"Command line config overrides: {config_overrides_string}")
82 config_dict = json.loads(config_overrides_string)
83 load_config_dict_to_opt(opt, config_dict)
84
85 if cmdline_args.overrides:
86 assert len(cmdline_args.overrides) % 2 == 0, "overrides arguments is not paired, required: key value"
87 keys = [cmdline_args.overrides[idx*2] for idx in range(len(cmdline_args.overrides)//2)]
88 vals = [cmdline_args.overrides[idx*2+1] for idx in range(len(cmdline_args.overrides)//2)]
89 vals = [val.replace('false', '').replace('False','') if len(val.replace(' ', '')) == 5 else val for val in vals]
90
91 types = []
92 for key in keys:
93 key = key.split('.')
94 ele = opt.copy()
95 while len(key) > 0:
96 ele = ele[key.pop(0)]
97 types.append(type(ele))
98
99 config_dict = {x:z(y) for x,y,z in zip(keys, vals, types)}
100 load_config_dict_to_opt(opt, config_dict)
101
102 # combine cmdline_args into opt dictionary
103 for key, val in cmdline_args.__dict__.items():
104 if val is not None:
105 opt[key] = val
106
107 return opt, cmdline_args
108
109
110def save_opt_to_json(opt, conf_file):

Callers

nothing calls this directly

Calls 3

load_config_dict_to_optFunction · 0.85
itemsMethod · 0.80

Tested by

no test coverage detected