-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport_config.py
299 lines (255 loc) · 11.2 KB
/
export_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import argparse
import os
import traceback
import yaml
from typing import Optional, List, Any
from paddleseg.cvlibs.config import Config
def check_file(file_type: str, path: Optional[str]):
"""
Checks whether file exists and does not point to a directory.
Raises an exception otherwise.
:param file_type: the string describing the file, used in the exceptions
:type file_type: str
:param path: the path to check, checks ignored if None
:type path: str
"""
if path is not None:
if not os.path.exists(path):
raise IOError("%s not found: %s" % (file_type, path))
if os.path.isdir(path):
raise IOError("%s points to a directory: %s" % (file_type, path))
def is_bool(s: str) -> bool:
"""
Checks whether the string is a boolean value.
:param s: the string to check
:type s: str
:return: True if a boolean
:rtype: bool
"""
return (s.lower() == "true") or (s.lower() == "false")
def parse_bool(s: str) -> bool:
"""
Returns True if the lower case of the string is "true".
:param s: the string to evaluate
:type s: str
:return: True if "true"
:rtype: bool
"""
return s.lower() == "true"
def is_int(s: str) -> bool:
"""
Checks whether the string is an int value.
:param s: the string to check
:type s: str
:return: True if an int
:rtype: bool
"""
try:
int(s)
return True
except:
return False
def is_float(s: str) -> bool:
"""
Checks whether the string is a float value.
:param s: the string to check
:type s: str
:return: True if a float
:rtype: bool
"""
try:
float(s)
return True
except:
return False
def set_value(config: dict, path: List[str], value: Any):
"""
Sets the value in the YAML config according to its path.
:param config: the config dictionary to update
:type config: dict
:param path: the list of path elements to use for navigating the hierarchical dictionary
:type path: list
:param value: the value to use
"""
current = config
found = False
for i in range(len(path)):
if path[i] in current:
if i < len(path) - 1:
current = current[path[i]]
else:
found = True
if isinstance(current[path[i]], bool):
current[path[i]] = parse_bool(value)
elif isinstance(current[path[i]], int):
current[path[i]] = int(value)
elif isinstance(current[path[i]], float):
current[path[i]] = float(value)
elif isinstance(current[path[i]], list):
values = value.split(",")
# can we infer type?
if len(current[path[i]]) > 0:
if isinstance(current[path[i]][0], bool):
current[path[i]] = [parse_bool(x) for x in values]
elif isinstance(current[path[i]][0], int):
current[path[i]] = [int(x) for x in values]
elif isinstance(current[path[i]][0], float):
current[path[i]] = [float(x) for x in values]
else:
current[path[i]] = values
else:
current[path[i]] = value
elif path[i].startswith("[") and path[i].endswith("]") and isinstance(current, list):
index = int(path[i][1:len(path[i])-1])
if index < len(current):
current = current[index]
else:
# not present, we'll just add it
if i == len(path) - 1:
print("Adding option: %s" % (str(path)))
if is_bool(value):
current[path[i]] = parse_bool(value)
elif is_int(value):
current[path[i]] = int(value)
elif is_float(value):
current[path[i]] = float(value)
else:
current[path[i]] = value
found = True
break
if not found:
print("Failed to locate path in config: %s" % str(path))
def remove_value(config: dict, path: List[str]):
"""
Removes the value from the YAML config according to its path.
:param config: the config dictionary to update
:type config: dict
:param path: the list of path elements to use for navigating the hierarchical dictionary
:type path: list
"""
current = config
removed = False
for i in range(len(path)):
if path[i] in current:
if i < len(path) - 1:
current = current[path[i]]
else:
del current[path[i]]
removed = True
elif path[i].startswith("[") and path[i].endswith("]") and isinstance(current, list):
index = int(path[i][1:len(path[i])-1])
if index < len(current):
current = current[index]
else:
break
if not removed:
print("Failed to locate path in config: %s" % str(path))
def export(input_file: str, output_file: str, train_annotations: str = None, val_annotations: str = None,
num_classes: int = None, batch_size: int = None, num_iters: int = None,
additional: List[str] = None, remove: List[str] = None):
"""
Exports the config file while updating specified parameters.
:param input_file: the template YAML config file to load and modify
:type input_file: str
:param output_file: the YAML file to store the updated config data in
:type output_file: str
:param train_annotations: the text file with the training annotations/images relation, ignored if None
:type train_annotations: str
:param val_annotations: the text file with the validation annotations/images relation, ignored if None
:type val_annotations: str
:param num_classes: the number of classes in the dataset, ignored if None
:type num_classes: int
:param batch_size: the batch size to use, ignored if None
:type batch_size: int
:param num_iters: the number of epochs to train, ignored if None
:type num_iters: int
:param additional: the list of additional parameters to set, format: PATH:VALUE, with PATH being the dot-notation path through the YAML parameter hierarchy in the file; if VALUE is to update a list, then the elements must be separated by comma
:type additional: list
:param remove: the list of parameters to remove, format: PATH, with PATH being the dot-notation path through the YAML parameter hierarchy in the file
:type remove: list
"""
# some sanity checks
check_file("Config file", input_file)
check_file("Training annotations", train_annotations)
check_file("Validation annotations", val_annotations)
if (num_classes is not None) and (num_classes < 1):
num_classes = None
# load template
print("Loading config from: %s" % input_file)
config = Config(input_file)
config = config.dic
if train_annotations is not None:
set_value(config, ["train_dataset", "type"], "Dataset")
set_value(config, ["train_dataset", "dataset_root"], os.path.dirname(train_annotations))
set_value(config, ["train_dataset", "train_path"], train_annotations)
if num_classes is not None:
set_value(config, ["train_dataset", "num_classes"], num_classes)
if val_annotations is not None:
set_value(config, ["val_dataset", "type"], "Dataset")
set_value(config, ["val_dataset", "dataset_root"], os.path.dirname(val_annotations))
set_value(config, ["val_dataset", "val_path"], val_annotations)
if num_classes is not None:
set_value(config, ["val_dataset", "num_classes"], num_classes)
if batch_size is not None:
set_value(config, ["batch_size"], batch_size)
if num_iters is not None:
set_value(config, ["iters"], num_iters)
if num_classes is not None:
set_value(config, ["model", "num_classes"], num_classes)
if additional is not None:
for add in additional:
if ":" in add:
path_str, value = add.split(":")
path = path_str.split(".")
set_value(config, path, value)
else:
print("Invalid format for additional parameter, expected PATH:VALUE but found: %s" % add)
if remove is not None:
for rem in remove:
path = rem.split(".")
remove_value(config, path)
print("Saving config to: %s" % output_file)
with open(output_file, "w") as fp:
yaml.dump(config, fp)
def main(args=None):
"""
Performs the bash.bashrc generation.
Use -h to see all options.
:param args: the command-line arguments to use, uses sys.argv if None
:type args: list
"""
parser = argparse.ArgumentParser(
description='Exports a PaddleSeg config file and updates specific fields with user-supplied values.',
prog="paddleseg_export_config",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("-i", "--input", metavar="FILE", required=True, help="The PaddleClass YAML config file template to export.")
parser.add_argument("-o", "--output", metavar="FILE", required=True, help="The YAML file to store the exported config file in.")
parser.add_argument("-t", "--train_annotations", metavar="FILE", required=False, help="The text file with the labels for the training data (images are expected to be located below that directory).")
parser.add_argument("-v", "--val_annotations", metavar="FILE", required=False, help="The text file with the labels for the validation data (images are expected to be located below that directory).")
parser.add_argument("-c", "--num_classes", metavar="NUM", required=False, type=int, help="The number of classes in the dataset.")
parser.add_argument("-I", "--num_iters", metavar="NUM", required=False, type=int, help="The number of iterations to train.")
parser.add_argument("-a", "--additional", metavar="PATH:VALUE", required=False, help="Additional parameters to override; format: PATH:VALUE, with PATH representing the dot-notation path through the parameter hierarchy in the YAML file, if VALUE is to update a list, then the elements must be separated by comma.", nargs="*")
parser.add_argument("-r", "--remove", metavar="PATH", required=False, help="Parameters to remove; format: PATH, with PATH representing the dot-notation path through the parameter hierarchy in the YAML file", nargs="*")
parsed = parser.parse_args(args=args)
export(parsed.input, parsed.output,
train_annotations=parsed.train_annotations, val_annotations=parsed.val_annotations,
num_classes=parsed.num_classes, num_iters=parsed.num_iters,
additional=parsed.additional, remove=parsed.remove)
def sys_main():
"""
Runs the main function using the system cli arguments, and
returns a system error code.
:return: 0 for success, 1 for failure.
:rtype: int
"""
try:
main()
return 0
except Exception:
print(traceback.format_exc())
return 1
if __name__ == "__main__":
try:
main()
except Exception:
print(traceback.format_exc())