| 125 | |
| 126 | |
| 127 | def parse_options(root_path, is_train=True): |
| 128 | parser = argparse.ArgumentParser() |
| 129 | parser.add_argument('-opt', type=str, required=True, help='Path to option YAML file.') |
| 130 | parser.add_argument('--launcher', choices=['none', 'pytorch', 'slurm'], default='none', help='job launcher') |
| 131 | parser.add_argument('--auto_resume', action='store_true') |
| 132 | parser.add_argument('--debug', action='store_true') |
| 133 | parser.add_argument('--local_rank', type=int, default=0) |
| 134 | parser.add_argument( |
| 135 | '--force_yml', nargs='+', default=None, help='Force to update yml files. Examples: train:ema_decay=0.999') |
| 136 | args = parser.parse_args() |
| 137 | |
| 138 | # parse yml to dict |
| 139 | opt = yaml_load(args.opt) |
| 140 | |
| 141 | # distributed settings |
| 142 | |
| 143 | if args.launcher == 'none': |
| 144 | opt['dist'] = False |
| 145 | print('Disable distributed.', flush=True) |
| 146 | else: |
| 147 | opt['dist'] = True |
| 148 | if args.launcher == 'slurm' and 'dist_params' in opt: |
| 149 | init_dist(args.launcher, **opt['dist_params']) |
| 150 | else: |
| 151 | init_dist(args.launcher) |
| 152 | opt['rank'], opt['world_size'] = get_dist_info() |
| 153 | |
| 154 | # random seed |
| 155 | seed = opt.get('manual_seed') |
| 156 | if seed is None: |
| 157 | seed = random.randint(1, 10000) |
| 158 | opt['manual_seed'] = seed |
| 159 | set_random_seed(seed + opt['rank']) |
| 160 | |
| 161 | # force to update yml options |
| 162 | if args.force_yml is not None: |
| 163 | for entry in args.force_yml: |
| 164 | # now do not support creating new keys |
| 165 | keys, value = entry.split('=') |
| 166 | keys, value = keys.strip(), value.strip() |
| 167 | value = _postprocess_yml_value(value) |
| 168 | eval_str = 'opt' |
| 169 | for key in keys.split(':'): |
| 170 | eval_str += f'["{key}"]' |
| 171 | eval_str += '=value' |
| 172 | # using exec function |
| 173 | exec(eval_str) |
| 174 | |
| 175 | # DIY Setting |
| 176 | opt = _merge_from_base(opt, args.opt) |
| 177 | # print(opt.keys()) |
| 178 | if opt['network_g']['type'] == "SwinIR": |
| 179 | opt['network_g']['img_size'] = opt['gt_size'] |
| 180 | |
| 181 | |
| 182 | opt['auto_resume'] = args.auto_resume |
| 183 | opt['is_train'] = is_train |
| 184 | |