-
Notifications
You must be signed in to change notification settings - Fork 3
/
run.py
256 lines (222 loc) · 7.37 KB
/
run.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
# ------------------------------------------------------------------------------------
# NeRF-Factory
# Copyright (c) 2022 POSTECH, KAIST, Kakao Brain Corp. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 [see LICENSE for details]
# ------------------------------------------------------------------------------------
import argparse
import logging
import os
import shutil
from typing import *
import gin
import torch
from pytorch_lightning import Trainer
from pytorch_lightning import loggers as pl_loggers
from pytorch_lightning import seed_everything
from pytorch_lightning.callbacks import (
LearningRateMonitor,
ModelCheckpoint,
TQDMProgressBar,
)
#from pytorch_lightning.plugins import DDPStrategy
from pytorch_lightning.strategies import DDPStrategy
from utils.select_option import select_callback
from utils.select_option import select_dataset
from utils.select_option import select_model
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ("yes", "true", "t", "y", "1"):
return True
elif v.lower() in ("no", "false", "f", "n", "0"):
return False
else:
raise Exception("Boolean value expected.")
@gin.configurable()
def run(
ginc: str,
ginb: str,
resume_training: bool,
ckpt_path: Optional[str],
scene_name: Optional[str],
datadir: Optional[str] = None,
logbase: Optional[str] = None,
model_name: Optional[str] = None,
dataset_name: Optional[str] = None,
postfix: Optional[str] = None,
entity: Optional[str] = None,
# Optimization
max_steps: int = -1,
max_epochs: int = -1,
precision: int = 32,
# Logging
log_every_n_steps: int = 1000,
progressbar_refresh_rate: int = 5,
# Run Mode
run_train: bool = True,
run_eval: bool = True,
run_render: bool = False,
num_devices: Optional[int] = None,
num_sanity_val_steps: int = 0,
seed: int = 777,
debug: bool = False,
save_last: bool = True,
grad_max_norm=0.0,
grad_clip_algorithm="norm",
):
logging.getLogger("lightning").setLevel(logging.ERROR)
datadir = datadir.rstrip("/")
exp_name = (
model_name + "_" + dataset_name + "_" + scene_name + "_" + str(seed).zfill(3)
)
if postfix is not None:
exp_name += "_" + postfix
if debug:
exp_name += "_debug"
if num_devices is None:
num_devices = torch.cuda.device_count()
print("----num_devices-----",num_devices)
if model_name in ["plenoxel"]:
num_devices = 1
if logbase is None:
logbase = "logs"
os.makedirs(logbase, exist_ok=True)
logdir = os.path.join(logbase, exp_name)
os.makedirs(logdir, exist_ok=True)
os.makedirs(os.path.join(logdir, exp_name), exist_ok=True)
logger = pl_loggers.TensorBoardLogger(
save_dir=logdir,
name=exp_name,
)
# Logging all parameters
if run_train:
txt_path = os.path.join(logdir, "config.gin")
with open(txt_path, "w") as fp_txt:
for config_path in ginc:
fp_txt.write(f"Config from {config_path}\n\n")
with open(config_path, "r") as fp_config:
readlines = fp_config.readlines()
for line in readlines:
fp_txt.write(line)
fp_txt.write("\n")
fp_txt.write("\n### Binded options\n")
for line in ginb:
fp_txt.write(line + "\n")
seed_everything(seed, workers=True)
lr_monitor = LearningRateMonitor(logging_interval="step")
model_checkpoint = ModelCheckpoint(
monitor="val/psnr",
dirpath=logdir,
filename="best",
save_top_k=1,
mode="max",
save_last=save_last,
)
tqdm_progrss = TQDMProgressBar(refresh_rate=progressbar_refresh_rate)
callbacks = []
if not model_name in ["plenoxel"]:
callbacks.append(lr_monitor)
callbacks += [model_checkpoint, tqdm_progrss]
callbacks += select_callback(model_name)
ddp_plugin = DDPStrategy(find_unused_parameters=False) if num_devices > 1 else None
trainer = Trainer(
logger=logger if run_train else None,
log_every_n_steps=log_every_n_steps,
devices=num_devices,
max_epochs=max_epochs,
max_steps=max_steps,
accelerator="gpu",
replace_sampler_ddp=False,
strategy=ddp_plugin,
check_val_every_n_epoch=1,
precision=precision,
num_sanity_val_steps= 0,
callbacks=callbacks,
gradient_clip_algorithm=grad_clip_algorithm,
gradient_clip_val=grad_max_norm,
)
if resume_training:
if ckpt_path is None:
ckpt_path = f"{logdir}/last.ckpt"
data_module = select_dataset(
dataset_name=dataset_name,
scene_name=scene_name,
datadir=datadir,
)
model = select_model(model_name=model_name)
model.logdir = logdir
if run_train:
best_ckpt = os.path.join(logdir, "best.ckpt")
if os.path.exists(best_ckpt):
os.remove(best_ckpt)
version0 = os.path.join(logdir, exp_name, "version_0")
if os.path.exists(version0):
shutil.rmtree(version0, True)
trainer.fit(model, data_module, ckpt_path=ckpt_path)
if run_eval:
ckpt_path = (
f"{logdir}/best.ckpt"
if model_name != "mipnerf360"
else f"{logdir}/last.ckpt"
)
trainer.test(model, data_module, ckpt_path=ckpt_path)
if run_render:
ckpt_path = (
f"{logdir}/best.ckpt"
if model_name != "mipnerf360"
else f"{logdir}/last.ckpt"
)
trainer.predict(model, data_module, ckpt_path=ckpt_path)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--ginc",
action="append",
help="gin config file",
)
parser.add_argument(
"--ginb",
action="append",
help="gin bindings",
)
parser.add_argument(
"--resume_training",
type=str2bool,
nargs="?",
const=True,
default=False,
help="gin bindings",
)
parser.add_argument(
"--ckpt_path", type=str, default=None, help="path to checkpoints"
)
parser.add_argument(
"--scene_name", type=str, default=None, help="scene name to render"
)
parser.add_argument("--seed", type=int, default=220902, help="seed to use")
args = parser.parse_args()
ginbs = []
if args.ginb:
ginbs.extend(args.ginb)
logging.info(f"Gin configuration files: {args.ginc}")
logging.info(f"Gin bindings: {ginbs}")
# import shlex
# #argString = '-e scenes_yuval -sb ../results -d "/media/dense/My Book/neural_sparse_big_folder/neural-scene-graphs/data/kitti/training/image_02" -r 1'
# argString = '--ginc configs/infonerf/tnt.gin --scene_name toy --ginb run.run_train=False'
# args = parser.parse_args(shlex.split(argString))
gin.parse_config_files_and_bindings(args.ginc, ginbs)
if "CURRENT_RANK_DATALOADER" not in os.environ :
os.environ["CURRENT_RANK_DATALOADER"] = "1"
f = open('output1.txt','w')
f.write('{}'.format(1))
f.close()
else:
pass #print("AAAA")
run(
ginc=args.ginc,
ginb=ginbs,
scene_name=args.scene_name,
resume_training=args.resume_training,
ckpt_path=args.ckpt_path,
seed=args.seed,
)