-
Notifications
You must be signed in to change notification settings - Fork 3
/
main_sh.py
392 lines (336 loc) · 12.5 KB
/
main_sh.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import os
import gc
import cv2
import torch
import time
import numpy as np
import hydra
from gs.renderer import GaussianRenderer
from gs.sh_renderer import SHRenderer
from utils.camera import (
get_c2ws_and_camera_info,
get_c2ws_and_camera_info_v1,
get_c2ws_and_camera_info_nerf_sythetic,
)
from functools import partial
from utils.misc import (
print_info,
save_img,
tic,
toc,
lineprofiler,
step_check,
average_dicts,
)
from utils.metrics import Metrics
from utils import misc
from utils.loss import get_loss_fn
import visdom
from omegaconf import OmegaConf
import matplotlib.pyplot as plt
from rich.console import Console
from tqdm import tqdm
import wandb
from torch.utils.tensorboard import SummaryWriter
import datetime
import logging
console = Console()
@lineprofiler
def train_and_eval(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")
if cfg.wandb:
wandb.init(
project="gs",
config=cfg,
name=f"{cfg.data_name}_{timestamp}",
sync_tensorboard=True,
)
writer = SummaryWriter(
f"./logs/runs/{cfg.data_name}/{timestamp}", comment=cfg.comment
)
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")
data_type = cfg.get("data_type", "real")
if data_type == "real":
(
c2ws,
camera_info,
images,
pts,
rgb,
eval_mask,
) = get_c2ws_and_camera_info_v1(cfg)
elif data_type == "blender":
(
c2ws,
camera_info,
images,
pts,
rgb,
eval_mask,
) = get_c2ws_and_camera_info_nerf_sythetic(cfg)
else:
raise NotImplementedError
c2ws = c2ws.to(cfg.device).contiguous()
# renderer = SHRenderer(cfg, pts, rgb).to(cfg.device)
train_images = images[~eval_mask].contiguous()
eval_images = images[eval_mask].contiguous()
eval_mask = eval_mask.to(cfg.device)
train_c2ws = c2ws[~eval_mask].contiguous()
eval_c2ws = c2ws[eval_mask].contiguous()
N_train = train_images.shape[0]
N_eval = eval_images.shape[0]
console.print(f"[green bold]#(train images): {N_train} #(eval images): {N_eval}")
if not cfg.from_ckpt:
start = 0
renderer = SHRenderer(cfg, pts, rgb).to(cfg.device)
else:
renderer = SHRenderer.load(cfg.ckpt_path, cfg).to(cfg.device)
start = cfg.start_epoch
loss_fn = get_loss_fn(cfg)
metric_meter = Metrics(cfg.device)
opt = renderer.get_optimizer(start)
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)
writer.add_text("cfg", OmegaConf.to_yaml(cfg))
writer.add_text("comment", cfg.comment)
use_train_sample = cfg.get("use_train_sample", False)
if use_train_sample:
ema = cfg.get("ema", 0.9)
image_weight = np.ones(N_train, dtype=np.float32)
image_sampled_freq = np.zeros(N_train, dtype=np.int32)
renderer.train()
with tqdm(total=cfg.max_iteration) as pbar:
for e in range(start, cfg.max_iteration):
if use_train_sample:
i = np.random.choice(
N_train,
p=image_weight / np.sum(image_weight),
)
image_sampled_freq[i] += 1
else:
i = e % N_train
tic()
out = renderer(train_c2ws[i], camera_info)
toc("whole renderer forward")
if e == 0:
print_info(out, "out")
print("num total gaussian", renderer.total_dub_gaussians)
if cfg.debug:
save_img(
out.cpu().clamp(min=0.0, max=1.0),
f"./tmp/debug",
f"train_{e}.png",
)
if only_forward:
pbar.update(1)
continue
gt = train_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 step_check(e, log_iteration, True):
save_img(
out.cpu().clamp(min=0.0, max=1.0),
f"./tmp/{cfg.data_name}_sh",
f"train_{e}.png",
)
save_img(
gt.cpu().clamp(min=0.0, max=1.0),
f"./tmp/{cfg.data_name}_sh",
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
)
if use_train_sample:
fig, ax = plt.subplots()
ax.stairs(image_sampled_freq)
writer.add_figure("train/image_sample_freq", fig, e)
# writer.add_histogram("train/image_sample_freq", image_sampled_freq, e)
renderer.log(writer, e)
if use_train_sample:
image_weight[i] = ema * loss.item() + (1 - ema) * image_weight[i]
opt.step()
if step_check(e, cfg.eval_iteration):
renderer.eval()
metric_dicts = []
eval_losses = []
# do eval
with torch.no_grad():
for j in range(N_eval):
gt_eval_image = eval_images[j].to(cfg.device)
eval_out = renderer(eval_c2ws[j], camera_info)
eval_losses.append(loss_fn(eval_out, gt_eval_image).item())
metric_dicts.append(metric_meter(eval_out, gt_eval_image))
eval_loss = np.mean(eval_losses)
eval_metrics = average_dicts(metric_dicts)
writer.add_scalar("eval/eval_loss", eval_loss, e)
for key, value in eval_metrics.items():
writer.add_scalar(f"eval/{key}", value, e)
writer.add_image(
f"eval/eval_img",
eval_out.cpu().moveaxis(-1, 0).clamp(min=0, max=1.0),
e,
)
info_line = f"[red bold]Iteration {e} Evaluation loss: {eval_loss:.4g}"
for key, value in eval_metrics.items():
info_line += f" {key}: {value:.4g}"
console.print(info_line)
renderer.train()
if step_check(e, cfg.save_iteration):
renderer.save(f"./saved/{timestamp}_{cfg.data_name}_sh/model_{e}.pt")
if cfg.adapt_ctrl_enabled:
renderer.adaptive_control(e)
# opt = torch.optim.Adam(renderer.parameters(), lr=cfg.lr)
opt = renderer.get_optimizer(e)
# 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()
@lineprofiler
def train_only(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")
if cfg.wandb:
wandb.config = OmegaConf.to_container(cfg, resolve=True, throw_on_missing=True)
wandb.init(
project="gs",
# config=cfg,
name=f"{cfg.data_name}_{timestamp}",
sync_tensorboard=True,
)
writer = SummaryWriter(
f"./logs/runs/{cfg.data_name}/{timestamp}", comment=cfg.comment
)
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)
c2ws = c2ws.to(cfg.device).contiguous()
renderer = SHRenderer(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 = renderer.get_optimizer()
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)
writer.add_text("cfg", str(cfg))
writer.add_text("comment", cfg.comment)
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 step_check(e, log_iteration, True):
save_img(
out.cpu().clamp(min=0.0, max=1.0),
f"./tmp/{cfg.data_name}_sh",
f"train_{e}.png",
)
save_img(
gt.cpu().clamp(min=0.0, max=1.0),
f"./tmp/{cfg.data_name}_sh",
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 cfg.adapt_ctrl_enabled:
renderer.adaptive_control(e)
# opt = torch.optim.Adam(renderer.parameters(), lr=cfg.lr)
del opt
opt = renderer.get_optimizer(e)
# 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 step_check(e, cfg.save_iteration):
renderer.save(f"./saved/{timestamp}_{cfg.data_name}_sh/model_{e}.pt")
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())
@hydra.main(config_path="conf", config_name="garden_sh")
def main(cfg):
mode = cfg.get("mode", "train_only")
if mode == "train_only":
train_only(cfg)
elif mode == "train_and_eval":
train_and_eval(cfg)
else:
raise NotImplementedError
if __name__ == "__main__":
main()