-
Notifications
You must be signed in to change notification settings - Fork 31
/
train.py
110 lines (88 loc) · 3.86 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
import torch
import numpy as np
import gaussian_splatting.utils as utils
from gaussian_splatting.trainer import Trainer
import gaussian_splatting.utils.loss_utils as loss_utils
from gaussian_splatting.utils.data_utils import read_all
from gaussian_splatting.utils.camera_utils import to_viewpoint_camera
from gaussian_splatting.utils.point_utils import get_point_clouds
from gaussian_splatting.gauss_model import GaussModel
from gaussian_splatting.gauss_render import GaussRenderer
import contextlib
from torch.profiler import profile, ProfilerActivity
USE_GPU_PYTORCH = True
USE_PROFILE = False
class GSSTrainer(Trainer):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.data = kwargs.get('data')
self.gaussRender = GaussRenderer(**kwargs.get('render_kwargs', {}))
self.lambda_dssim = 0.2
self.lambda_depth = 0.0
def on_train_step(self):
ind = np.random.choice(len(self.data['camera']))
camera = self.data['camera'][ind]
rgb = self.data['rgb'][ind]
depth = self.data['depth'][ind]
mask = (self.data['alpha'][ind] > 0.5)
if USE_GPU_PYTORCH:
camera = to_viewpoint_camera(camera)
if USE_PROFILE:
prof = profile(activities=[ProfilerActivity.CUDA], with_stack=True)
else:
prof = contextlib.nullcontext()
with prof:
out = self.gaussRender(pc=self.model, camera=camera)
if USE_PROFILE:
print(prof.key_averages(group_by_stack_n=True).table(sort_by='self_cuda_time_total', row_limit=20))
l1_loss = loss_utils.l1_loss(out['render'], rgb)
depth_loss = loss_utils.l1_loss(out['depth'][..., 0][mask], depth[mask])
ssim_loss = 1.0-loss_utils.ssim(out['render'], rgb)
total_loss = (1-self.lambda_dssim) * l1_loss + self.lambda_dssim * ssim_loss + depth_loss * self.lambda_depth
psnr = utils.img2psnr(out['render'], rgb)
log_dict = {'total': total_loss,'l1':l1_loss, 'ssim': ssim_loss, 'depth': depth_loss, 'psnr': psnr}
return total_loss, log_dict
def on_evaluate_step(self, **kwargs):
import matplotlib.pyplot as plt
ind = np.random.choice(len(self.data['camera']))
camera = self.data['camera'][ind]
if USE_GPU_PYTORCH:
camera = to_viewpoint_camera(camera)
rgb = self.data['rgb'][ind].detach().cpu().numpy()
out = self.gaussRender(pc=self.model, camera=camera)
rgb_pd = out['render'].detach().cpu().numpy()
depth_pd = out['depth'].detach().cpu().numpy()[..., 0]
depth = self.data['depth'][ind].detach().cpu().numpy()
depth = np.concatenate([depth, depth_pd], axis=1)
depth = (1 - depth / depth.max())
depth = plt.get_cmap('jet')(depth)[..., :3]
image = np.concatenate([rgb, rgb_pd], axis=1)
image = np.concatenate([image, depth], axis=0)
utils.imwrite(str(self.results_folder / f'image-{self.step}.png'), image)
if __name__ == "__main__":
device = 'cuda'
folder = './B075X65R3X'
data = read_all(folder, resize_factor=0.5)
data = {k: v.to(device) for k, v in data.items()}
data['depth_range'] = torch.Tensor([[1,3]]*len(data['rgb'])).to(device)
points = get_point_clouds(data['camera'], data['depth'], data['alpha'], data['rgb'])
raw_points = points.random_sample(2**14)
# raw_points.write_ply(open('points.ply', 'wb'))
gaussModel = GaussModel(sh_degree=4, debug=False)
gaussModel.create_from_pcd(pcd=raw_points)
render_kwargs = {
'white_bkgd': True,
}
trainer = GSSTrainer(model=gaussModel,
data=data,
train_batch_size=1,
train_num_steps=25000,
i_image =100,
train_lr=1e-3,
amp=False,
fp16=False,
results_folder='result/test',
render_kwargs=render_kwargs,
)
trainer.on_evaluate_step()
trainer.train()