-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_tasks.py
307 lines (249 loc) · 13.7 KB
/
gen_tasks.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
300
301
302
303
304
305
306
307
import argparse
import json
import os
import shutil
import glob
from h36m_to_coco import TEACHERS, SAMPLINGS, PERCENTAGES
REF_HUMAN_POSE = "human_pose_parco.json"
REF_TASK = "nohead_densenet121_baseline_att_256x256_B.json"
INITIAL_MODEL = "densenet121_baseline_att_256x256_B_epoch_160_parco.pth"
SHUFFLE = False
MAX_NUM_WORKERS = 4
MEMORY_BATCH_SIZE = 32
REAL_BATCH_SIZE = 64
EPOCHS = 10
CONTINUAL_LEARNING_SUBJECT = "S1"
CONTINUAL_NUM_WORKERS = 1
CONTINUAL_WINDOW = int(50 * 30) # 1500 [0.01: 15]
CONTINUAL_BATCH_SIZE = 32
CONTINUAL_EPOCHS = 10
def save_task_info(task_json, initial_state_dict, images_dir, annotation_dir, tasks_dir, train_file, test_file, name):
task_json["model"]["initial_state_dict"] = initial_state_dict
task_json["train_dataset"]["images_dir"] = images_dir
task_json["train_dataset"]["annotations_file"] = os.path.join(annotation_dir, train_file)
task_json["train_dataset"]["image_extension"] = "png"
task_json["train_loader"]["batch_size"] = MEMORY_BATCH_SIZE
task_json["train_loader"]["shuffle"] = SHUFFLE
task_json["train_loader"]["num_workers"] = MAX_NUM_WORKERS
task_json["test_dataset"]["images_dir"] = images_dir
task_json["test_dataset"]["annotations_file"] = os.path.join(annotation_dir, test_file)
task_json["test_dataset"]["image_extension"] = "png"
task_json["test_loader"]["batch_size"] = MEMORY_BATCH_SIZE
task_json["test_loader"]["shuffle"] = SHUFFLE
task_json["test_loader"]["num_workers"] = MAX_NUM_WORKERS
task_json["batch_size"] = REAL_BATCH_SIZE
task_json["epochs"] = EPOCHS
json_data = json.dumps(task_json, indent=4)
# Write the JSON data to a file
with open(os.path.join(tasks_dir, name), "w") as f:
f.write(json_data)
def save_continual_task_info(task_json, initial_state_dict, images_dir, annotation_dir, tasks_dir, train_file, ref_train_file, test_file, name, perc=None):
task_json["model"]["initial_state_dict"] = initial_state_dict
task_json["train_dataset"]["images_dir"] = images_dir
task_json["train_dataset"]["annotations_file"] = os.path.join(annotation_dir, train_file)
task_json["train_dataset"]["image_extension"] = "png"
task_json["train_loader"]["batch_size"] = CONTINUAL_WINDOW if perc is None else int(CONTINUAL_WINDOW * perc)
task_json["train_loader"]["shuffle"] = SHUFFLE
task_json["train_loader"]["num_workers"] = CONTINUAL_NUM_WORKERS
task_json["test_dataset"]["images_dir"] = images_dir
task_json["test_dataset"]["annotations_file"] = os.path.join(annotation_dir, test_file)
task_json["test_dataset"]["image_extension"] = "png"
task_json["test_loader"]["batch_size"] = CONTINUAL_WINDOW if perc is None else int(CONTINUAL_WINDOW * perc)
task_json["test_loader"]["shuffle"] = SHUFFLE
task_json["test_loader"]["num_workers"] = CONTINUAL_NUM_WORKERS
task_json["ref_annotations_file"] = os.path.join(annotation_dir, ref_train_file)
task_json["batch_size"] = CONTINUAL_BATCH_SIZE
task_json["window"] = CONTINUAL_WINDOW
task_json["epochs"] = CONTINUAL_EPOCHS
task_json["ground_truth"] = {
"folder": os.path.join(os.path.dirname(images_dir), "h36m"),
"model": "vicon",
"subjects": ["S9", "S11"],
"cameras": ["55011271"],
}
json_data = json.dumps(task_json, indent=4)
# Write the JSON data to a file
with open(os.path.join(tasks_dir, name), "w") as f:
f.write(json_data)
def gen_bash_script(list_of_tasks, name, is_continual):
script = "#!/bin/bash\n"
script += "\n"
script += "# module load Python/3.9.16\n"
script += "# module load cuda11.1\n"
script += "module restore pytorch\n"
script += "\n"
script += "source ~/.venv_39/bin/activate\n"
script += "\n"
if is_continual:
command = "bash continual.bash"
else:
command = "python3 trt_pose/train.py"
for e in list_of_tasks:
script += "{} {}\n".format(command, e)
script += "\n"
with open(name, "w") as file:
file.write(script)
print("Bash script {} generated successfully".format(name))
def create_human_pose(human_pose_json, human_pose_folder, name):
# Write the JSON data to a file
prefix = os.path.splitext(REF_HUMAN_POSE)[0]
json_data = json.dumps(human_pose_json, indent=4)
with open(os.path.join(human_pose_folder, "{}_{}.json".format(prefix, name)), "w") as f:
f.write(json_data)
def gen_inference(repo_folder, data_folder, teachers, samplings, percentages, name):
script_folder = os.path.join(repo_folder, "scripts")
script = "#!/bin/bash\n"
script += "\n"
script += "SUBJECTS=(\n"
script += " \"S9\"\n"
script += " \"S11\"\n"
script += " # \"S1\"\n"
script += " # \"S5\"\n"
script += " # \"S6\"\n"
script += " # \"S7\"\n"
script += " # \"S8\"\n"
script += ")\n"
script += "\n"
script += "CAMERAS=(\n"
script += " \"55011271\"\n"
script += ")\n"
script += "\n"
template_run = ""
template_run += "{0}() {{\n"
template_run += " for cam in ${{CAMERAS[*]}}; do\n"
template_run += " for sub in ${{SUBJECTS[*]}}; do\n"
template_run += " echo \"CAMERA ${{cam}} - SUBJECT ${{sub}}\"\n"
template_run += " i=0\n"
template_run += " mkdir -p {2}/h36m/${{sub}}/{0}/\n"
template_run += " for action in $(ls -d {2}/h36m/${{sub}}/${{cam}}/*/); do\n"
template_run += " echo ${{action}}\n"
template_run += " TESTS[${{i}}]=${{action}}\n"
template_run += " i=$(($i + 1))\n"
template_run += " done\n"
template_run += " {1}python3 parcopose_from_folder.py -f ${{TESTS[*]}} -n {0} -o {2}/h36m/${{sub}}/{0}/\n"
template_run += " unset TESTS\n"
template_run += " done\n"
template_run += " done\n"
template_run += "}}\n"
template_run += "\n"
if type(teachers) is str:
if teachers == "openpose" or teachers == "openpose1":
script += template_run.format(teachers, "DNN=openpose ", data_folder)
else:
script += template_run.format(teachers, "", data_folder)
script += "{}\n".format(teachers)
elif samplings is None or percentages is None:
for teacher in teachers:
script += template_run.format("parco_h36m_{}".format(teacher), "", data_folder)
for teacher in teachers:
script += "parco_h36m_{}\n".format(teacher)
else:
for teacher in teachers:
for sampling in samplings:
for perc in percentages:
script += template_run.format("parco_h36m_{}sampling{}_{}".format(sampling, perc, teacher), "", data_folder)
for teacher in teachers:
for sampling in samplings:
for perc in percentages:
script += "parco_h36m_{}sampling{}_{}\n".format(sampling, perc, teacher)
script += "\necho \"Done\"\n"
with open(os.path.join(script_folder, name), "w") as file:
file.write(script)
def copy_checkpoints(teachers, samplings, percentages, tasks_dir, data_folder, human_pose_folder):
model_dir = os.path.join(data_folder, "models")
model_prefix = os.path.splitext(INITIAL_MODEL)[0]
for teacher in teachers:
task = "h36m_{}_{}".format(teacher, REF_TASK)
task_dir = os.path.join(tasks_dir, "{}.checkpoints".format(task))
model_file = os.path.join(task_dir, "epoch_0.pth")
target_model_name = "{}_h36m_{}.pth".format(model_prefix, teacher)
target_model_path = os.path.join(model_dir, target_model_name)
if os.path.exists(model_file):
shutil.copy(model_file, target_model_path)
for sampling in samplings:
for perc in percentages:
perc_str = "{}".format(int(perc * 100))
task = "{}{}_h36m_{}_{}".format(sampling, perc_str, teacher, REF_TASK)
task_dir = os.path.join(tasks_dir, "{}.checkpoints".format(task))
model_file = os.path.join(task_dir, "epoch_0.pth")
target_model_name = "{}_h36m_{}sampling{}_{}.pth".format(model_prefix, sampling, perc_str, teacher)
target_model_path = os.path.join(model_dir, target_model_name)
if os.path.exists(model_file):
shutil.copy(model_file, target_model_path)
# Copy all files from model dir
model_files = glob.glob(os.path.join(model_dir, "*.pth"))
for model_file in model_files:
model_basename = os.path.basename(model_file)
target_model_path = os.path.join(human_pose_folder, model_basename)
shutil.copy(model_file, target_model_path)
def main(repo_folder, data_folder):
initial_state_dict = os.path.join(
repo_folder, "submodule", "lib_maeve_py", "maeve", "nn", "trtpose", "models", INITIAL_MODEL)
human_pose_folder = os.path.join(
repo_folder, "submodule", "lib_maeve_py", "maeve", "nn", "trtpose", "models")
images_dir = os.path.join(data_folder, "h36m")
annotations_dir = os.path.join(data_folder, "annotations")
tasks_dir = os.path.join(repo_folder, "trt_pose", "tasks", "human_pose", "experiments")
with open(os.path.join(tasks_dir, REF_TASK)) as f:
ref_task_json = json.load(f)
with open(os.path.join(human_pose_folder, REF_HUMAN_POSE)) as f:
ref_human_pose_json = json.load(f)
tasks = []
continual_tasks = []
for teacher in TEACHERS:
save_task_info(
ref_task_json, initial_state_dict, images_dir, annotations_dir, tasks_dir,
"person_keypoints_trainh36m_{}.json".format(teacher),
"person_keypoints_valh36m_uniformsampling10_{}.json".format(teacher),
"h36m_{}_{}".format(teacher, REF_TASK))
save_continual_task_info(
ref_task_json, initial_state_dict, images_dir, annotations_dir, tasks_dir,
"continualtrain_person_keypoints_{}_{}.json".format(CONTINUAL_LEARNING_SUBJECT.lower(), teacher),
"continualtrain_person_keypoints_{}_{}.json".format(CONTINUAL_LEARNING_SUBJECT.lower(), teacher),
"continualval_person_keypoints_{}_uniformsampling10_{}.json".format(CONTINUAL_LEARNING_SUBJECT.lower(), teacher),
"continual_h36m_{}_{}".format(teacher, REF_TASK))
tasks.append(os.path.join(tasks_dir, "h36m_{}_{}".format(teacher, REF_TASK)))
continual_tasks.append(os.path.join(tasks_dir, "continual_h36m_{}_{}".format(teacher, REF_TASK)))
create_human_pose(ref_human_pose_json, human_pose_folder, "h36m_{}".format(teacher))
for teacher in TEACHERS:
for sampling in SAMPLINGS:
for perc in PERCENTAGES:
perc_str = "{}".format(int(perc * 100))
save_task_info(
ref_task_json, initial_state_dict, images_dir, annotations_dir, tasks_dir,
"person_keypoints_trainh36m_{}sampling{}_{}.json".format(sampling, perc_str, teacher),
"person_keypoints_valh36m_uniformsampling10_{}.json".format(teacher),
"{}{}_h36m_{}_{}".format(sampling, perc_str, teacher, REF_TASK))
save_continual_task_info(
ref_task_json, initial_state_dict, images_dir, annotations_dir, tasks_dir,
"continualtrain_person_keypoints_{}_{}sampling{}_{}.json".format(CONTINUAL_LEARNING_SUBJECT.lower(), sampling, perc_str, teacher),
"continualtrain_person_keypoints_{}_{}.json".format(CONTINUAL_LEARNING_SUBJECT.lower(), teacher),
"continualval_person_keypoints_{}_uniformsampling10_{}.json".format(CONTINUAL_LEARNING_SUBJECT.lower(), teacher),
"continual_{}{}_h36m_{}_{}".format(sampling, perc_str, teacher, REF_TASK), perc)
tasks.append(os.path.join(tasks_dir, "{}{}_h36m_{}_{}".format(sampling, perc_str, teacher, REF_TASK)))
continual_tasks.append(os.path.join(tasks_dir, "continual_{}{}_h36m_{}_{}".format(sampling, perc_str, teacher, REF_TASK)))
create_human_pose(ref_human_pose_json, human_pose_folder, "h36m_{}sampling{}_{}".format(sampling, perc_str, teacher))
gen_bash_script(tasks, os.path.join(repo_folder, "trt_pose", "run_train.bash"), is_continual=False)
gen_bash_script(continual_tasks, os.path.join(repo_folder, "trt_pose", "run_train_continual.bash"), is_continual=True)
percentages_string = ["{}".format(int(perc * 100)) for perc in PERCENTAGES]
for sampling in SAMPLINGS:
gen_inference(repo_folder, data_folder, TEACHERS, [sampling], percentages_string, "run_parcopose_h36m_{}sampling.bash".format(sampling))
gen_inference(repo_folder, data_folder, TEACHERS, None, None, "run_parcopose_h36m.bash")
gen_inference(repo_folder, data_folder, "trtpose", None, None, "run_trtpose.bash")
gen_inference(repo_folder, data_folder, "openpose1", None, None, "run_openpose.bash")
gen_inference(repo_folder, data_folder, "parco", None, None, "run_parcopose.bash")
copy_checkpoints(TEACHERS, SAMPLINGS, PERCENTAGES, tasks_dir, data_folder, human_pose_folder)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Automatic generation of tasks", epilog="PARCO")
parser.add_argument("--repo-folder",
"-rf",
dest="repo_folder",
required=True,
help="Folder with EdgeKnowledgeDistillation repository")
parser.add_argument("--data-folder",
"-df",
dest="data_folder",
required=True,
help="Folder with data (annotations and images)")
args = parser.parse_args()
main(args.repo_folder, args.data_folder)