-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_configs.py
278 lines (219 loc) · 9.84 KB
/
generate_configs.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
import copy
import os
import yaml
graph_models = ["gat", "gcn", "monet", "multigat"]
image_models = ["cnn", "vgg", "prevgg"]
datasets = ["mnist", "fmnist", "cifar10", "cifar100", "covid", "lfw", "socofing"]
datasets_classes = [
"mnist_img_slic",
"mnist_img_slic",
"cifar_img_slic",
"cifar_img_slic",
"covid_img_slic",
"lfw_img_slic",
"socofing_img_slic",
]
num_class = [10, 10, 10, 100, 4, 62, 600]
name_to_class = dict(zip(datasets, datasets_classes))
name_to_num_class = dict(zip(datasets, num_class))
graph_img_dir = "configs/custom_trainer/graph_image"
graph_dir = "configs/custom_trainer/graph"
image_dir = "configs/custom_trainer/image"
# Image and Graph
dataset_paths = "./configs/templates/dataset/paths"
def generate_dataset_yaml_indiv(typ, model, dataset):
temp = f"{dataset}.txt"
if typ == "image":
transform_path = img_transforms_path
else:
transform_path = grp_transforms_path
transform_path = os.path.join(transform_path, dataset)
list_models = os.listdir(transform_path)
list_models = [x.split(".")[0] for x in list_models]
if model in list_models:
model_transform = os.path.join(transform_path, f"{model}.txt")
else:
model_transform = os.path.join(transform_path, "regular.txt")
with open(model_transform) as f:
head = f.read()
with open(os.path.join(dataset_paths, temp)) as f:
tail = f.read()
return head + "\n" + tail
def generate_model_yaml_indiv(typ, model, dataset):
if typ == "image":
transform_path = img_transforms_path
else:
transform_path = grp_transforms_path
transform_path = os.path.join(transform_path, model)
list_datasets = os.listdir(transform_path)
list_datasets = [x.split(".")[0] for x in list_datasets]
if dataset in list_datasets:
model_template = os.path.join(transform_path, f"{dataset}.txt")
else:
model_template = os.path.join(transform_path, "regular.txt")
with open(model_template) as f:
head = f.read()
return head
def generate_train_yaml(typ, model, dataset):
if typ == "image":
template_path = "./configs/templates/train/image"
elif typ == "graph":
template_path = "./configs/templates/train/graph"
else:
template_path = f"./configs/templates/train/graph_image/{model}"
print(model, template_path)
list_datasets = os.listdir(template_path)
list_datasets = [x.split(".")[0] for x in list_datasets]
if dataset in list_datasets:
train_template = os.path.join(template_path, f"{dataset}.yaml")
else:
train_template = os.path.join(template_path, "regular.yaml")
with open(train_template) as f:
content = f.read()
return content
def generate_model_yaml(image_path, graph_path, dataset, combo_type):
with open(image_path) as f:
d1 = yaml.safe_load(f)
with open(graph_path) as f:
d2 = yaml.safe_load(f)
if "linear_layer_params" in d1:
d1["linear_layer_params"]["intermediate_layer_sizes"] = []
d1["num_classes"] = 32 if combo_type == "projection" else name_to_num_class[dataset]
if "linear_layer_params" in d2:
d2["linear_layer_params"]["intermediate_layer_sizes"] = []
d2["num_classes"] = 32 if combo_type == "projection" else name_to_num_class[dataset]
d = {"name": combo_type}
d["cnn_config"] = d1
d["gnn_config"] = d2
if combo_type == "projection":
d["num_classes"] = name_to_num_class[dataset]
return d
def generate_dataset_yaml(image_path, graph_path, dataset):
with open(image_path) as f:
d = yaml.safe_load(f)
with open(graph_path) as f:
d2 = yaml.safe_load(f)
d3 = copy.deepcopy(d)
d["main"]["name"] = name_to_class[dataset]
d["main"]["image_transform_args"] = d3["main"]["transform_args"]
del d["main"]["transform_args"]
d["main"]["graph_transform_args"] = d2["main"]["transform_args"]
d["train"]["name"] = name_to_class[dataset]
d["train"]["image_transform_args"] = d3["train"]["transform_args"]
del d["train"]["transform_args"]
d["train"]["graph_transform_args"] = d2["train"]["transform_args"]
d["val"]["name"] = name_to_class[dataset]
d["val"]["image_transform_args"] = d3["val"]["transform_args"]
del d["val"]["transform_args"]
d["val"]["graph_transform_args"] = d2["val"]["transform_args"]
d["train_val"]["train"]["name"] = name_to_class[dataset]
d["train_val"]["train"]["image_transform_args"] = d3["train"]["transform_args"]
del d["train_val"]["train"]["transform_args"]
d["train_val"]["train"]["graph_transform_args"] = d2["train"]["transform_args"]
d["train_val"]["val"]["name"] = name_to_class[dataset]
d["train_val"]["val"]["image_transform_args"] = d3["val"]["transform_args"]
del d["train_val"]["val"]["transform_args"]
d["train_val"]["val"]["graph_transform_args"] = d2["val"]["transform_args"]
return d
# Image and Graph Dataset
img_transforms_path = "./configs/templates/dataset/transforms/image"
grp_transforms_path = "./configs/templates/dataset/transforms/graph"
for dataset in datasets:
for image_model in image_models:
image_path = os.path.join(image_dir, image_model + "_" + dataset)
if not os.path.exists(image_path):
os.makedirs(image_path)
with open(os.path.join(image_path, "dataset.yaml"), "w") as f:
f.write(generate_dataset_yaml_indiv("image", image_model, dataset))
for graph_model in graph_models:
graph_path = os.path.join(graph_dir, graph_model + "_" + dataset)
if not os.path.exists(graph_path):
os.makedirs(graph_path)
with open(os.path.join(graph_path, "dataset.yaml"), "w") as f:
f.write(generate_dataset_yaml_indiv("graph", graph_model, dataset))
# Image and Graph Model
img_transforms_path = "./configs/templates/model/image"
grp_transforms_path = "./configs/templates/model/graph"
for dataset in datasets:
for image_model in image_models:
image_path = os.path.join(image_dir, image_model + "_" + dataset)
if not os.path.exists(image_path):
os.makedirs(image_path)
with open(os.path.join(image_path, "model.yaml"), "w") as f:
f.write(generate_model_yaml_indiv("image", image_model, dataset))
for graph_model in graph_models:
graph_path = os.path.join(graph_dir, graph_model + "_" + dataset)
if not os.path.exists(graph_path):
os.makedirs(graph_path)
with open(os.path.join(graph_path, "model.yaml"), "w") as f:
f.write(generate_model_yaml_indiv("graph", graph_model, dataset))
# Image and Graph Train
# with open("./configs/templates/train/image.yaml") as f:
# image_train_yaml = f.read()
# for root, dirs, fils in os.walk("configs/custom_trainer/image"):
# for fil in fils:
# if "train.yaml" in fil:
# with open(os.path.join(root, fil), "w") as f:
# f.write(image_train_yaml)
# with open("./configs/templates/train/graph.yaml") as f:
# graph_train_yaml = f.read()
# for root, dirs, fils in os.walk("configs/custom_trainer/graph"):
# for fil in fils:
# if "train.yaml" in fil:
# with open(os.path.join(root, fil), "w") as f:
# f.write(graph_train_yaml)
for dataset in datasets:
for image_model in image_models:
image_path = os.path.join(image_dir, image_model + "_" + dataset)
if not os.path.exists(image_path):
os.makedirs(image_path)
with open(os.path.join(image_path, "train.yaml"), "w") as f:
f.write(generate_train_yaml("image", image_model, dataset))
for graph_model in graph_models:
graph_path = os.path.join(graph_dir, graph_model + "_" + dataset)
if not os.path.exists(graph_path):
os.makedirs(graph_path)
with open(os.path.join(graph_path, "train.yaml"), "w") as f:
f.write(generate_train_yaml("graph", graph_model, dataset))
# Graph - Image Entire
# with open("./configs/templates/train/graph_image.yaml") as f:
# graph_image_train_yaml = f.read()
# Projection
for combo_type in ["projection", "hybrid"]:
for dataset in datasets:
for image_model in image_models:
image_path = os.path.join(image_dir, image_model + "_" + dataset)
for graph_model in graph_models:
graph_path = os.path.join(graph_dir, graph_model + "_" + dataset)
graph_image_path = os.path.join(
graph_img_dir,
combo_type,
image_model + "_" + graph_model + "_" + dataset,
)
if not os.path.exists(graph_image_path):
os.makedirs(graph_image_path)
with open(os.path.join(graph_image_path, "train.yaml"), "w") as f:
f.write(generate_train_yaml("graph_image", combo_type, dataset))
model_yaml = generate_model_yaml(
os.path.join(image_path, "model.yaml"),
os.path.join(graph_path, "model.yaml"),
dataset,
combo_type,
)
with open(os.path.join(graph_image_path, "model.yaml"), "w") as f:
yaml.dump(model_yaml, f)
dataset_yaml = generate_dataset_yaml(
os.path.join(image_path, "dataset.yaml"),
os.path.join(graph_path, "dataset.yaml"),
dataset,
)
with open(os.path.join(graph_image_path, "dataset.yaml"), "w") as f:
yaml.dump(dataset_yaml, f)
# Clean save all the train configs
# for root, dirs, fils in os.walk("configs/custom_trainer"):
# for fil in fils:
# if "train.yaml" in fil:
# with open(os.path.join(root, fil)) as f:
# d = yaml.safe_load(f)
# with open(os.path.join(root, fil), "w") as f:
# yaml.dump(d, f)