-
Notifications
You must be signed in to change notification settings - Fork 21
/
train.py
162 lines (134 loc) · 6.12 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
import os, sys
import random
import numpy as np
import torch
import torch.nn as nn
import argparse
import cv2
import lpips
from scene import GaussianModel, Scene_mica
from src.deform_model import Deform_Model
from gaussian_renderer import render
from arguments import ModelParams, PipelineParams, OptimizationParams
from utils.loss_utils import huber_loss
from utils.general_utils import normalize_for_percep
def set_random_seed(seed):
r"""Set random seeds for everything.
Args:
seed (int): Random seed.
by_rank (bool):
"""
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
if __name__ == "__main__":
# Set up command line argument parser
parser = argparse.ArgumentParser(description="Training script parameters")
lp = ModelParams(parser)
op = OptimizationParams(parser)
pp = PipelineParams(parser)
parser.add_argument('--seed', type=int, default=0, help='Random seed.')
parser.add_argument('--idname', type=str, default='id1_25', help='id name')
parser.add_argument('--image_res', type=int, default=512, help='image resolution')
parser.add_argument("--start_checkpoint", type=str, default = None)
args = parser.parse_args(sys.argv[1:])
args.device = "cuda"
lpt = lp.extract(args)
opt = op.extract(args)
ppt = pp.extract(args)
batch_size = 1
set_random_seed(args.seed)
percep_module = lpips.LPIPS(net='vgg').to(args.device)
## deform model
DeformModel = Deform_Model(args.device).to(args.device)
DeformModel.training_setup()
## dataloader
data_dir = os.path.join('dataset', args.idname)
mica_datadir = os.path.join('metrical-tracker/output', args.idname)
log_dir = os.path.join(data_dir, 'log')
train_dir = os.path.join(log_dir, 'train')
model_dir = os.path.join(log_dir, 'ckpt')
os.makedirs(log_dir, exist_ok=True)
os.makedirs(train_dir, exist_ok=True)
os.makedirs(model_dir, exist_ok=True)
scene = Scene_mica(data_dir, mica_datadir, train_type=0, white_background=lpt.white_background, device = args.device)
first_iter = 0
gaussians = GaussianModel(lpt.sh_degree)
gaussians.training_setup(opt)
if args.start_checkpoint:
(model_params, gauss_params, first_iter) = torch.load(args.start_checkpoint)
DeformModel.restore(model_params)
gaussians.restore(gauss_params, opt)
bg_color = [1, 1, 1] if lpt.white_background else [0, 1, 0]
background = torch.tensor(bg_color, dtype=torch.float32, device=args.device)
codedict = {}
codedict['shape'] = scene.shape_param.to(args.device)
DeformModel.example_init(codedict)
viewpoint_stack = None
first_iter += 1
mid_num = 15000
for iteration in range(first_iter, opt.iterations + 1):
# Every 500 its we increase the levels of SH up to a maximum degree
if iteration % 500 == 0:
gaussians.oneupSHdegree()
# random Camera
if not viewpoint_stack:
viewpoint_stack = scene.getCameras().copy()
random.shuffle(viewpoint_stack)
if len(viewpoint_stack)>2000:
viewpoint_stack = viewpoint_stack[:2000]
viewpoint_cam = viewpoint_stack.pop(random.randint(0, len(viewpoint_stack)-1))
frame_id = viewpoint_cam.uid
# deform gaussians
codedict['expr'] = viewpoint_cam.exp_param
codedict['eyes_pose'] = viewpoint_cam.eyes_pose
codedict['eyelids'] = viewpoint_cam.eyelids
codedict['jaw_pose'] = viewpoint_cam.jaw_pose
verts_final, rot_delta, scale_coef = DeformModel.decode(codedict)
if iteration == 1:
gaussians.create_from_verts(verts_final[0])
gaussians.training_setup(opt)
gaussians.update_xyz_rot_scale(verts_final[0], rot_delta[0], scale_coef[0])
# Render
render_pkg = render(viewpoint_cam, gaussians, ppt, background)
image = render_pkg["render"]
# Loss
gt_image = viewpoint_cam.original_image
mouth_mask = viewpoint_cam.mouth_mask
loss_huber = huber_loss(image, gt_image, 0.1) + 40*huber_loss(image*mouth_mask, gt_image*mouth_mask, 0.1)
loss_G = 0.
head_mask = viewpoint_cam.head_mask
image_percep = normalize_for_percep(image*head_mask)
gt_image_percep = normalize_for_percep(gt_image*head_mask)
if iteration>mid_num:
loss_G = torch.mean(percep_module.forward(image_percep, gt_image_percep))*0.05
loss = loss_huber*1 + loss_G*1
loss.backward()
with torch.no_grad():
# Optimizer step
if iteration < opt.iterations :
gaussians.optimizer.step()
DeformModel.optimizer.step()
gaussians.optimizer.zero_grad(set_to_none = True)
DeformModel.optimizer.zero_grad(set_to_none = True)
# print loss
if iteration % 500 == 0:
if iteration<=mid_num:
print("step: %d, huber: %.5f" %(iteration, loss_huber.item()))
else:
print("step: %d, huber: %.5f, percep: %.5f" %(iteration, loss_huber.item(), loss_G.item()))
# visualize results
if iteration % 500 == 0 or iteration==1:
save_image = np.zeros((args.image_res, args.image_res*2, 3))
gt_image_np = (gt_image*255.).permute(1,2,0).detach().cpu().numpy().astype(np.uint8)
image = image.clamp(0, 1)
image_np = (image*255.).permute(1,2,0).detach().cpu().numpy().astype(np.uint8)
save_image[:, :args.image_res, :] = gt_image_np
save_image[:, args.image_res:, :] = image_np
cv2.imwrite(os.path.join(train_dir, f"{iteration}.jpg"), save_image[:,:,[2,1,0]])
# save checkpoint
if iteration % 5000 == 0:
print("\n[ITER {}] Saving Checkpoint".format(iteration))
torch.save((DeformModel.capture(), gaussians.capture(), iteration), model_dir + "/chkpnt" + str(iteration) + ".pth")