1+ import sys
12import json
23import os
34import torch
@@ -21,13 +22,13 @@ def get_opt(main_opt, remaining_args):
2122
2223 with open (main_opt .config_json , "r" ) as jsonf :
2324 train_json = flatten_json (json .load (jsonf ))
24-
25- # Save the config file when --save_config is passed
26- is_config_saved = False
27- if "save_config" in override_options_names :
28- is_config_saved = True
29- override_options_names .remove ("save_config" )
30- remaining_args . remove ( "--save_config" )
25+ # # Save the config file when --save_config is passed
26+ # is_config_saved = False
27+ # if "save_config" in override_options_names:
28+ # is_config_saved = True
29+ # override_options_names.remove("save_config")
30+ # remaining_args .remove("-- save_config")
31+ #
3132
3233 if not "--dataroot" in remaining_args :
3334 remaining_args += ["--dataroot" , "unused" ]
@@ -37,6 +38,24 @@ def get_opt(main_opt, remaining_args):
3738 TrainOptions ().parse_to_json (remaining_args )
3839 )
3940
41+ # Save the config file when --save_config is passed
42+ is_config_saved = False
43+ if "save_config" in override_options_names :
44+ is_config_saved = True
45+ override_options_names .remove ("save_config" )
46+
47+ train_keys = set (train_json .keys ())
48+ override_keys = set (override_options_json .keys ())
49+ override_names = set (override_options_names )
50+ override_not_in_train = override_names - train_keys
51+ override_not_in_override_json = override_names - override_keys
52+ if override_not_in_override_json :
53+ unknown_list = ", " .join (sorted (override_not_in_override_json ))
54+ print (
55+ f"\033 [93mWARNING: The following command-line options are not recognized: { unknown_list } \033 [0m"
56+ )
57+ sys .exit (1 )
58+
4059 for name in override_options_names :
4160 train_json [name ] = override_options_json [name ]
4261
@@ -46,4 +65,14 @@ def get_opt(main_opt, remaining_args):
4665 else :
4766 opt = TrainOptions ().parse () # get training options
4867
68+ parsed_opt_keys = set (vars (opt ).keys ())
69+ commandline_arg_names = set (get_override_options_names (remaining_args ))
70+ always_allowed = {"save_config" }
71+ invalid_args = commandline_arg_names - parsed_opt_keys - always_allowed
72+ if invalid_args :
73+ print (
74+ f"\033 [93mWARNING: The following command-line options are not recognized by the parser: { sorted (list (invalid_args ))} \033 [0m"
75+ )
76+ sys .exit (1 ) # Stop the script if there are any invalid args
77+
4978 return opt
0 commit comments