-
Notifications
You must be signed in to change notification settings - Fork 62
/
train.py
376 lines (330 loc) · 13.9 KB
/
train.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
from omegaconf import OmegaConf
import numpy as np
import os
import time
import wandb
import random
import imageio
import logging
import argparse
import torch
from tools.eval import do_evaluation
from utils.misc import import_str
from utils.backup import backup_project
from utils.logging import MetricLogger, setup_logging
from models.video_utils import render_images, save_videos
from datasets.driving_dataset import DrivingDataset
logger = logging.getLogger()
current_time = time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime())
def set_seeds(seed=31):
"""
Fix random seeds.
"""
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
def setup(args):
# get config
cfg = OmegaConf.load(args.config_file)
# parse datasets
args_from_cli = OmegaConf.from_cli(args.opts)
if "dataset" in args_from_cli:
cfg.dataset = args_from_cli.pop("dataset")
assert "dataset" in cfg or "data" in cfg, \
"Please specify dataset in config or data in config"
if "dataset" in cfg:
dataset_type = cfg.pop("dataset")
dataset_cfg = OmegaConf.load(
os.path.join("configs", "datasets", f"{dataset_type}.yaml")
)
# merge data
cfg = OmegaConf.merge(cfg, dataset_cfg)
# merge cli
cfg = OmegaConf.merge(cfg, args_from_cli)
log_dir = os.path.join(args.output_root, args.project, args.run_name)
# update config and create log dir
cfg.log_dir = log_dir
os.makedirs(log_dir, exist_ok=True)
for folder in ["images", "videos", "metrics", "configs_bk", "buffer_maps", "backup"]:
os.makedirs(os.path.join(log_dir, folder), exist_ok=True)
# setup wandb
if args.enable_wandb:
# sometimes wandb fails to init in cloud machines, so we give it several (many) tries
while (
wandb.init(
project=args.project,
entity=args.entity,
sync_tensorboard=True,
settings=wandb.Settings(start_method="fork"),
)
is not wandb.run
):
continue
wandb.run.name = args.run_name
wandb.run.save()
wandb.config.update(OmegaConf.to_container(cfg, resolve=True))
wandb.config.update(args)
# setup random seeds
set_seeds(cfg.seed)
global logger
setup_logging(output=log_dir, level=logging.INFO, time_string=current_time)
logger.info("\n".join("%s: %s" % (k, str(v)) for k, v in sorted(dict(vars(args)).items())))
# save config
logger.info(f"Config:\n{OmegaConf.to_yaml(cfg)}")
saved_cfg_path = os.path.join(log_dir, "config.yaml")
with open(saved_cfg_path, "w") as f:
OmegaConf.save(config=cfg, f=f)
# also save a backup copy
saved_cfg_path_bk = os.path.join(log_dir, "configs_bk", f"config_{current_time}.yaml")
with open(saved_cfg_path_bk, "w") as f:
OmegaConf.save(config=cfg, f=f)
logger.info(f"Full config saved to {saved_cfg_path}, and {saved_cfg_path_bk}")
# Backup codes
backup_project(
os.path.join(log_dir, 'backup'), "./",
["configs", "datasets", "models", "utils", "tools"],
[".py", ".h", ".cpp", ".cuh", ".cu", ".sh", ".yaml"]
)
return cfg
def main(args):
cfg = setup(args)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# build dataset
dataset = DrivingDataset(data_cfg=cfg.data)
# setup trainer
trainer = import_str(cfg.trainer.type)(
**cfg.trainer,
num_timesteps=dataset.num_img_timesteps,
model_config=cfg.model,
num_train_images=len(dataset.train_image_set),
num_full_images=len(dataset.full_image_set),
test_set_indices=dataset.test_timesteps,
scene_aabb=dataset.get_aabb().reshape(2, 3),
device=device
)
# NOTE: If resume, gaussians will be loaded from checkpoint
# If not, gaussians will be initialized from dataset
if args.resume_from is not None:
trainer.resume_from_checkpoint(
ckpt_path=args.resume_from,
load_only_model=True
)
logger.info(
f"Resuming training from {args.resume_from}, starting at step {trainer.step}"
)
else:
trainer.init_gaussians_from_dataset(dataset=dataset)
logger.info(
f"Training from scratch, initializing gaussians from dataset, starting at step {trainer.step}"
)
if args.enable_viewer:
# a simple viewer for background visualization
trainer.init_viewer(port=args.viewer_port)
# define render keys
render_keys = [
"gt_rgbs",
"rgbs",
"Background_rgbs",
"Dynamic_rgbs",
"RigidNodes_rgbs",
"DeformableNodes_rgbs",
"SMPLNodes_rgbs",
# "depths",
# "Background_depths",
# "Dynamic_depths",
# "RigidNodes_depths",
# "DeformableNodes_depths",
# "SMPLNodes_depths",
# "mask"
]
if cfg.render.vis_lidar:
render_keys.insert(0, "lidar_on_images")
if cfg.render.vis_sky:
render_keys += ["rgb_sky_blend", "rgb_sky"]
if cfg.render.vis_error:
render_keys.insert(render_keys.index("rgbs") + 1, "rgb_error_maps")
# setup optimizer
trainer.initialize_optimizer()
# setup metric logger
metrics_file = os.path.join(cfg.log_dir, "metrics.json")
metric_logger = MetricLogger(delimiter=" ", output_file=metrics_file)
all_iters = np.arange(trainer.step, trainer.num_iters + 1)
# DEBUG USE
# do_evaluation(
# step=0,
# cfg=cfg,
# trainer=trainer,
# dataset=dataset,
# render_keys=render_keys,
# args=args,
# )
for step in metric_logger.log_every(all_iters, cfg.logging.print_freq):
#----------------------------------------------------------------------------
#---------------------------- Validate ------------------------------
if step % cfg.logging.vis_freq == 0 and cfg.logging.vis_freq > 0:
logger.info("Visualizing...")
vis_timestep = np.linspace(
0,
dataset.num_img_timesteps,
trainer.num_iters // cfg.logging.vis_freq + 1,
endpoint=False,
dtype=int,
)[step // cfg.logging.vis_freq]
with torch.no_grad():
render_results = render_images(
trainer=trainer,
dataset=dataset.full_image_set,
compute_metrics=True,
compute_error_map=cfg.render.vis_error,
vis_indices=[
vis_timestep * dataset.pixel_source.num_cams + i
for i in range(dataset.pixel_source.num_cams)
],
)
if args.enable_wandb:
wandb.log(
{
"image_metrics/psnr": render_results["psnr"],
"image_metrics/ssim": render_results["ssim"],
"image_metrics/occupied_psnr": render_results["occupied_psnr"],
"image_metrics/occupied_ssim": render_results["occupied_ssim"],
}
)
vis_frame_dict = save_videos(
render_results,
save_pth=os.path.join(
cfg.log_dir, "images", f"step_{step}.png"
), # don't save the video
layout=dataset.layout,
num_timestamps=1,
keys=render_keys,
save_seperate_video=cfg.logging.save_seperate_video,
num_cams=dataset.pixel_source.num_cams,
fps=cfg.render.fps,
verbose=False,
)
if args.enable_wandb:
for k, v in vis_frame_dict.items():
wandb.log({"image_rendering/" + k: wandb.Image(v)})
del render_results
torch.cuda.empty_cache()
#----------------------------------------------------------------------------
#---------------------------- training step -------------------------------
# prepare for training
trainer.set_train()
trainer.preprocess_per_train_step(step=step)
trainer.optimizer_zero_grad() # zero grad
# get data
train_step_camera_downscale = trainer._get_downscale_factor()
image_infos, cam_infos = dataset.train_image_set.next(train_step_camera_downscale)
for k, v in image_infos.items():
if isinstance(v, torch.Tensor):
image_infos[k] = v.cuda(non_blocking=True)
for k, v in cam_infos.items():
if isinstance(v, torch.Tensor):
cam_infos[k] = v.cuda(non_blocking=True)
# forward & backward
outputs = trainer(image_infos, cam_infos)
trainer.update_visibility_filter()
loss_dict = trainer.compute_losses(
outputs=outputs,
image_infos=image_infos,
cam_infos=cam_infos,
)
# check nan or inf
for k, v in loss_dict.items():
if torch.isnan(v).any():
raise ValueError(f"NaN detected in loss {k} at step {step}")
if torch.isinf(v).any():
raise ValueError(f"Inf detected in loss {k} at step {step}")
trainer.backward(loss_dict)
# after training step
trainer.postprocess_per_train_step(step=step)
#----------------------------------------------------------------------------
#------------------------------- logging ----------------------------------
with torch.no_grad():
# cal stats
metric_dict = trainer.compute_metrics(
outputs=outputs,
image_infos=image_infos,
)
metric_logger.update(**{"train_metrics/"+k: v.item() for k, v in metric_dict.items()})
metric_logger.update(**{"train_stats/gaussian_num_" + k: v for k, v in trainer.get_gaussian_count().items()})
metric_logger.update(**{"losses/"+k: v.item() for k, v in loss_dict.items()})
metric_logger.update(**{"train_stats/lr_" + group['name']: group['lr'] for group in trainer.optimizer.param_groups})
if args.enable_wandb:
wandb.log({k: v.avg for k, v in metric_logger.meters.items()})
#----------------------------------------------------------------------------
#---------------------------- Saving --------------------------------
do_save = step > 0 and (
(step % cfg.logging.saveckpt_freq == 0) or (step == trainer.num_iters)
) and (args.resume_from is None)
if do_save:
trainer.save_checkpoint(
log_dir=cfg.log_dir,
save_only_model=True,
is_final=step == trainer.num_iters,
)
#----------------------------------------------------------------------------
#------------------------ Cache Image Error ---------------------------
if (
step > 0 and trainer.optim_general.cache_buffer_freq > 0
and step % trainer.optim_general.cache_buffer_freq == 0
):
logger.info("Caching image error...")
trainer.set_eval()
with torch.no_grad():
dataset.pixel_source.update_downscale_factor(
1 / dataset.pixel_source.buffer_downscale
)
render_results = render_images(
trainer=trainer,
dataset=dataset.full_image_set,
)
dataset.pixel_source.reset_downscale_factor()
dataset.pixel_source.update_image_error_maps(render_results)
# save error maps
merged_error_video = dataset.pixel_source.get_image_error_video(
dataset.layout
)
imageio.mimsave(
os.path.join(
cfg.log_dir, "buffer_maps", f"buffer_maps_{step}.mp4"
),
merged_error_video,
fps=cfg.render.fps,
)
logger.info("Done caching rgb error maps")
logger.info("Training done!")
do_evaluation(
step=step,
cfg=cfg,
trainer=trainer,
dataset=dataset,
render_keys=render_keys,
args=args,
)
if args.enable_viewer:
print("Viewer running... Ctrl+C to exit.")
time.sleep(1000000)
return step
if __name__ == "__main__":
parser = argparse.ArgumentParser("Train Gaussian Splatting for a single scene")
parser.add_argument("--config_file", help="path to config file", type=str)
parser.add_argument("--output_root", default="./work_dirs/", help="path to save checkpoints and logs", type=str)
# eval
parser.add_argument("--resume_from", default=None, help="path to checkpoint to resume from", type=str)
parser.add_argument("--render_video_postfix", type=str, default=None, help="an optional postfix for video")
# wandb logging part
parser.add_argument("--enable_wandb", action="store_true", help="enable wandb logging")
parser.add_argument("--entity", default="ziyc", type=str, help="wandb entity name")
parser.add_argument("--project", default="drivestudio", type=str, help="wandb project name, also used to enhance log_dir")
parser.add_argument("--run_name", default="omnire", type=str, help="wandb run name, also used to enhance log_dir")
# viewer
parser.add_argument("--enable_viewer", action="store_true", help="enable viewer")
parser.add_argument("--viewer_port", type=int, default=8080, help="viewer port")
# misc
parser.add_argument("opts", help="Modify config options using the command-line", default=None, nargs=argparse.REMAINDER)
args = parser.parse_args()
final_step = main(args)