-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
142 lines (119 loc) · 4.35 KB
/
main.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
import os
import gc
import cv2
import torch
import time
import numpy as np
import hydra
from gs.renderer import GaussianRenderer
from utils.camera import get_c2ws_and_camera_info, get_c2ws_and_camera_info_v1
from kornia.losses import ssim_loss
from functools import partial
from utils.misc import print_info, save_img, tic, toc
from utils import misc
from utils.loss import get_loss_fn
import visdom
from collections import deque
from rich.console import Console
from tqdm import tqdm
import wandb
from torch.utils.tensorboard import SummaryWriter
import datetime
import logging
# import line_profiler
# profile = line_profiler.LineProfiler()
console = Console()
@hydra.main(config_path="conf", config_name="garden")
def main(cfg):
logger = logging.getLogger(__name__)
if cfg.viewer:
logger.info("Viewer enabled")
os.chdir(hydra.utils.get_original_cwd())
timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
writer = SummaryWriter(f"./logs/runs/{cfg.data_name}/{timestamp}")
print(cfg.debug)
if cfg.debug:
global debug
debug = True
console.print(f"[blue underline]cwd: {os.getcwd()}")
console.print(f"[red bold]debug mode")
if cfg.timing:
misc._timing_ = True
console.print(f"[red bold]timimg enabled")
tic()
toc("test")
c2ws, camera_info, images, pts, rgb = get_c2ws_and_camera_info_v1(cfg)
renderer = GaussianRenderer(cfg, pts, rgb).to(cfg.device)
if cfg.debug:
avg_radius = np.linalg.norm(c2ws.cpu().numpy()[:, :3, 3], axis=-1).mean()
console.print(f"[red bold]avg_radius: {avg_radius:.2f}")
N_images = images.shape[0]
if isinstance(images, np.ndarray):
images = torch.from_numpy(images).to(torch.float32)
# loss_fn = torch.nn.functional.mse_loss
loss_fn = get_loss_fn(cfg)
opt = torch.optim.Adam(renderer.parameters(), lr=cfg.lr)
if cfg.get("viewer", False):
from utils.viewer.viser_viewer import ViserViewer
viewer = ViserViewer(cfg)
viewer.set_renderer(renderer)
viewer_eval_fps = 1000.0
log_iteration = cfg.get("log_iteration", 50)
only_forward = cfg.get("only_forward", False)
warm_up = cfg.get("warm_up", 1000)
renderer.train()
with tqdm(total=cfg.max_iteration) as pbar:
for e in range(cfg.max_iteration):
i = e % N_images
tic()
out = renderer(c2ws[i], camera_info)
toc("whole renderer forward")
if e == 0:
print_info(out, "out")
print("num total gaussian", renderer.total_dub_gaussians)
if only_forward:
pbar.update(1)
continue
gt = images[i].to(cfg.device)
loss = loss_fn(out, gt)
opt.zero_grad()
tic()
with torch.autograd.profiler.profile(enabled=cfg.timing) as prof:
loss.backward()
toc("backward")
if e % log_iteration == 0:
save_img(
out.cpu().clamp(min=0.0, max=1.0),
f"./tmp/{cfg.data_name}",
f"train_{e}.png",
)
save_img(
gt.cpu().clamp(min=0.0, max=1.0),
f"./tmp/{cfg.data_name}",
f"gt_{e}.png",
)
if cfg.debug:
exit(0)
writer.add_scalar("loss", loss.item(), e)
writer.add_image(
"out", out.cpu().moveaxis(-1, 0).clamp(min=0, max=1.0), e
)
renderer.log(writer, e)
opt.step()
if e > warm_up:
renderer.adaptive_control(e)
opt = torch.optim.Adam(renderer.parameters(), lr=cfg.lr)
# logger.info(f"Iteration: {e}/{cfg.max_iteration} loss: {loss.item():.4f}")
pbar.set_description(f"Iteration: {e}/{cfg.max_iteration}")
pbar.set_postfix(loss=f"{loss.item():.4f}")
pbar.update(1)
if cfg.viewer:
while viewer.pause_training:
viewer.update()
time.sleep(1.0 / viewer_eval_fps)
if e % viewer.train_viewer_update_period_slider.value == 0:
viewer.update()
if cfg.timing:
print(prof.key_averages())
if __name__ == "__main__":
main()