-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_generator.py
280 lines (253 loc) · 14.2 KB
/
config_generator.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
import os, yaml, pandas, argparse, ast, fileinput, tempfile, shutil
from datetime import date
from pathlib import Path
## detect "_per_label" metrics
def detect_per_label_metrics(filename):
"""
This function detects if the file contains triaged (i.e., metrics split per label) per_label metrics or not
Args:
filename (str): The log file to check.
Returns:
bool: True if the file contains triaged per_label metrics or does not contain per_label stats, False otherwise.
"""
with open(filename, "r") as fp:
header = fp.readline()
if "_per_label" in header:
if "_per_label_" in header:
# in this case, the per label triage has already happened
return False
else:
return True
else:
return False
if __name__ == "__main__":
copyrightMessage = (
"Contact: [email protected]\n\n"
+ "This program is NOT FDA/CE approved and NOT intended for clinical use.\nCopyright (c) "
+ str(date.today().year)
+ " University of Pennsylvania. All rights reserved."
)
cwd = Path(__file__).resolve().parent
parser = argparse.ArgumentParser(
prog="GANDLF_Experiment_Submitter",
formatter_class=argparse.RawTextHelpFormatter,
description="Submit GaNDLF experiments on CUBIC Cluster.\n\n"
+ copyrightMessage,
)
parser.add_argument(
"-c",
"--config",
metavar="",
default=True,
type=ast.literal_eval,
help="Generate config or not. If false, tries to generate succinct information about training.",
)
args = parser.parse_args()
if args.config:
## make sure you have a baseline configuration somewhere
base_config = os.path.join(cwd, "config.yaml")
#### update configurations to be trained
### this example is to generate multiple configs based on schedulers and learning rates
# learning_rates = [0.1, 0.01, 0.001, 0.0001]
# schedulers = ["exponential", "step", "reduce_on_plateau", "cosineannealing"]
# for sched in schedulers:
# base_output_dir = os.path.join(cwd, sched)
# pathlib.Path(base_output_dir).mkdir(parents=True, exist_ok=True)
# for lr in learning_rates:
# with open(base_config, "r") as f:
# config = yaml.safe_load(f)
# config["learning_rate"] = lr
# config["scheduler"] = sched
# config["opt"] = "sgd"
# with open(os.path.join(base_output_dir, str(lr) + ".yaml"), "w") as f:
# yaml.dump(config, f)
### this example is to generate multiple configs based on a single scheduler (exponential), learning rate (0.01) and different gammas
# gamma_vals = [1, 0.01, 0.001, 0.0001]
# current_config_dir = os.path.join(cwd, "exponential")
# pathlib.Path(current_config_dir).mkdir(parents=True, exist_ok=True)
# for gamma in gamma_vals:
# config_to_write = os.path.join(current_config_dir, "gamma_" + str(gamma) + ".yaml")
# with open(base_config, "r") as f:
# config = yaml.safe_load(f)
# config["learning_rate"] = 0.01
# config["scheduler"] = {}
# config["scheduler"]["gamma"] = gamma
# config["scheduler"]["type"] = "exponential"
# with open(config_to_write, "w") as f:
# yaml.dump(config, f)
## this example is to generate multiple configs based on different batch sizes
# batch_sizes = [48, 52, 58]
# output_dir = os.path.join(cwd, "B")
# os.makedirs(output_dir, exist_ok=True)
# for batch in batch_sizes:
# config = os.path.join(output_dir, str(batch) + ".yaml")
# with open(base_config, "r") as f:
# config_dict = yaml.safe_load(f)
# config_dict["batch_size"] = batch
# with open(config, "w") as f:
# yaml.dump(config_dict, f)
else:
# get information about best config
dirs_in_cwd = os.listdir(cwd)
dirs_in_cwd.sort()
best_info = {"config": [], "train_epoch": [], "valid_epoch": []}
## populate the metrics to be shown - example shown for classification
metrics_to_populate = ["loss", "balanced_accuracy", "accuracy"]
metrics_calculated_per_label = ["accuracy"] # not always present
for metric in metrics_to_populate:
for type in ["train", "valid"]:
best_info[type + "_" + metric] = []
for dir in dirs_in_cwd:
current_dir = os.path.join(cwd, dir)
if os.path.isdir(current_dir):
print("Current directory: ", current_dir)
config_outputs_in_dir = os.listdir(current_dir)
config_outputs_in_dir.sort()
files_and_folders_inside = os.listdir(current_dir)
files_and_folders_inside.sort()
for internal_file_or_folder in files_and_folders_inside:
if internal_file_or_folder.endswith(
".yaml"
) or internal_file_or_folder.endswith(".yml"):
current_config = os.path.join(
current_dir, internal_file_or_folder
)
config = yaml.safe_load(open(current_config))
assert (
"model" in config
), "The 'model' attribute was not found in config"
if "num_classes" in config["model"]:
number_of_classes = config["model"]["num_classes"]
elif "class_list" in config["model"]:
number_of_classes = len(config["model"]["class_list"])
else:
number_of_classes = 0
print(
"The number of classes could not be determined from the config file:",
current_config,
)
config_output_dir = os.path.join(
current_dir, internal_file_or_folder.split(".")[0]
)
if os.path.isdir(config_output_dir):
print("Current config output: ", config_output_dir)
file_logs_training = os.path.join(
config_output_dir, "logs_training.csv"
)
file_logs_validation = os.path.join(
config_output_dir, "logs_validation.csv"
)
if os.path.isfile(file_logs_training) and os.path.isfile(
file_logs_validation
):
with open(file_logs_training, "r") as fp:
len_logs_training = len(fp.readlines())
with open(file_logs_validation, "r") as fp:
len_logs_validation = len(fp.readlines())
# ensure something other than the log headers have been written
if len_logs_training > 2 and len_logs_validation > 2:
temp_dir = tempfile.gettempdir()
Path(temp_dir).mkdir(parents=True, exist_ok=True)
new_train_file = os.path.join(
temp_dir, "logs_training.csv"
)
shutil.copyfile(file_logs_training, new_train_file)
new_valid_file = os.path.join(
temp_dir, "logs_validation.csv"
)
shutil.copyfile(
file_logs_validation, new_valid_file
)
assert not detect_per_label_metrics(
new_train_file
), "Per label metrics detected in training logs - update metrics_calculated_per_label with correct information, and comment these lines to ensure correct parsing"
assert not detect_per_label_metrics(
new_valid_file
), "Per label metrics detected in validation logs - update metrics_calculated_per_label with correct information, and comment these lines to ensure correct parsing"
### replace the per_label metric header information to ensure correct parsing - change as needed
def get_new_header(cohort):
return_string = "epoch_no," + cohort + "_loss,"
for metric in metrics_calculated_per_label:
if metric != "loss":
return_string += (
cohort
+ "_"
+ metric
+ ","
+ ",".join(
[
cohort
+ "_"
+ metric
+ "_per_label_"
+ str(i)
for i in range(
number_of_classes
)
]
)
+ ","
)
return return_string
def replace_per_label_metrics(filename, new_header):
for line in fileinput.input(
filename, inplace=True
):
if fileinput.isfirstline():
if "_dice_per_label" in line:
if "_dice_per_label_" in line:
# this means the per label metrics have already been replaced
print(line)
else:
print(new_header)
else:
print(line)
replace_per_label_metrics(
new_train_file, get_new_header("train")
)
replace_per_label_metrics(
new_valid_file, get_new_header("valid")
)
### replace the per_label metric header information to ensure correct parsing - change as needed
## sort by loss
best_train_loss_row = (
pandas.read_csv(new_train_file)
.sort_values(by="train_loss", ascending=True)
.iloc[0]
)
best_valid_loss_row = (
pandas.read_csv(new_valid_file)
.sort_values(by="valid_loss", ascending=True)
.iloc[0]
)
best_info["config"].append(
dir + "_" + internal_file_or_folder
)
best_info["train_epoch"].append(
best_train_loss_row["epoch_no"]
)
best_info["valid_epoch"].append(
best_valid_loss_row["epoch_no"]
)
shutil.rmtree(temp_dir)
for type in ["train", "valid"]:
for metric in metrics_to_populate:
if type == "train":
best_info[
"{}_{}".format(type, metric)
].append(
best_train_loss_row[
"{}_{}".format(type, metric)
]
)
else:
best_info[
"{}_{}".format(type, metric)
].append(
best_valid_loss_row[
"{}_{}".format(type, metric)
]
)
pandas.DataFrame.from_dict(best_info).to_csv(
os.path.join(cwd, "best_info.csv"), index=False
)