forked from MIT-SPARK/MiDiffusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
122 lines (101 loc) · 3.56 KB
/
utils.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
#
# modified from:
# https://github.com/nv-tlabs/ATISS.
#
import yaml
try:
from yaml import CLoader as Loader
except ImportError:
from yaml import Loader
import json
import time
import string
import os
import random
import subprocess
import torch
PROJ_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
PATH_TO_DATASET_FILES = os.path.join(PROJ_DIR, "../ThreedFront/dataset_files/")
PATH_TO_PROCESSED_DATA = os.path.join(PROJ_DIR, "../ThreedFront/output/3d_front_processed/")
def load_config(config_file):
with open(config_file, "r") as f:
config = yaml.load(f, Loader=Loader)
return config
def update_data_file_paths(config_data):
config_data["dataset_directory"] = \
os.path.join(PATH_TO_PROCESSED_DATA, config_data["dataset_directory"])
config_data["annotation_file"] = \
os.path.join(PATH_TO_DATASET_FILES, config_data["annotation_file"])
return config_data
def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
def save_experiment_params(args, experiment_tag, directory):
t = vars(args)
params = {k: str(v) for k, v in t.items()}
git_dir = os.path.dirname(os.path.realpath(__file__))
git_head_hash = "foo"
try:
git_head_hash = subprocess.check_output(
['git', 'rev-parse', 'HEAD']
).strip()
except subprocess.CalledProcessError:
# Keep the current working directory to move back in a bit
cwd = os.getcwd()
os.chdir(git_dir)
git_head_hash = subprocess.check_output(
['git', 'rev-parse', 'HEAD']
).strip()
os.chdir(cwd)
params["git-commit"] = str(git_head_hash)
params["experiment_tag"] = experiment_tag
for k, v in list(params.items()):
if v == "":
params[k] = None
if hasattr(args, "config_file"):
config = load_config(args.config_file)
params.update(config)
with open(os.path.join(directory, "params.json"), "w") as f:
json.dump(params, f, indent=4)
def get_time_str(seconds):
hms_str = time.strftime("%H:%M:%S", time.gmtime(seconds))
if seconds < 24 * 3600:
return hms_str
day_str = f"{int(seconds // (24 * 3600))} day "
return day_str + hms_str
def yield_forever(iterator):
while True:
for x in iterator:
yield x
def load_checkpoints(model, optimizer, experiment_directory, args, device):
model_files = [
f for f in os.listdir(experiment_directory)
if f.startswith("model_")
]
if len(model_files) == 0:
return
ids = [int(f[6:]) for f in model_files]
max_id = max(ids)
model_path = os.path.join(
experiment_directory, "model_{:05d}"
).format(max_id)
opt_path = os.path.join(
experiment_directory, "opt_{:05d}"
).format(max_id)
if not (os.path.exists(model_path) and os.path.exists(opt_path)):
return
print("Loading model checkpoint from {}".format(model_path))
model.load_state_dict(torch.load(model_path, map_location=device))
print("Loading optimizer checkpoint from {}".format(opt_path))
optimizer.load_state_dict(
torch.load(opt_path, map_location=device)
)
args.continue_from_epoch = max_id
def save_checkpoints(epoch, model, optimizer, experiment_directory):
torch.save(
model.state_dict(),
os.path.join(experiment_directory, "model_{:05d}").format(epoch)
)
torch.save(
optimizer.state_dict(),
os.path.join(experiment_directory, "opt_{:05d}").format(epoch)
)