-
Notifications
You must be signed in to change notification settings - Fork 1
/
synthesize_fuse.py
136 lines (106 loc) · 6.48 KB
/
synthesize_fuse.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
#
# Copyright (C) 2023, Inria
# GRAPHDECO research group, https://team.inria.fr/graphdeco
# All rights reserved.
#
# This software is free for non-commercial, research and evaluation use
# under the terms of the LICENSE.md file.
#
# For inquiries contact [email protected]
#
import imageio
import numpy as np
import torch
from scene import Scene
import os
from tqdm import tqdm
from os import makedirs
from gaussian_renderer import render_motion, render_motion_mouth
import torchvision
from utils.general_utils import safe_state
from argparse import ArgumentParser
from arguments import ModelParams, PipelineParams, get_combined_args
from gaussian_renderer import GaussianModel, MotionNetwork, MouthMotionNetwork
import torch.nn.functional as F
def dilate_fn(bin_img, ksize=13):
pad = (ksize - 1) // 2
bin_img = F.pad(bin_img, pad=[pad, pad, pad, pad], mode='reflect')
out = F.max_pool2d(bin_img, kernel_size=ksize, stride=1, padding=0)
return out
def render_set(model_path, name, iteration, views, gaussians, motion_net, gaussians_mouth, motion_net_mouth, pipeline, background, fast, dilate):
render_path = os.path.join(model_path, name, "ours_{}".format(iteration), "renders")
gts_path = os.path.join(model_path, name, "ours_{}".format(iteration), "gt")
makedirs(render_path, exist_ok=True)
makedirs(gts_path, exist_ok=True)
all_preds = []
all_gts = []
all_preds_face = []
all_preds_mouth = []
all_preds_mouth_bg = []
for idx, view in enumerate(tqdm(views, desc="Rendering progress", ascii=True)):
with torch.no_grad():
render_pkg = render_motion(view, gaussians, motion_net, pipeline, background, frame_idx=0)
render_pkg_mouth = render_motion_mouth(view, gaussians_mouth, motion_net_mouth, pipeline, background, frame_idx=0)
# gt = view.original_image[0:3, :, :]
# torchvision.utils.save_image(rendering, os.path.join(render_path, '{0:05d}'.format(idx) + ".png"))
# torchvision.utils.save_image(gt, os.path.join(gts_path, '{0:05d}'.format(idx) + ".png"))
if dilate:
alpha_mouth = dilate_fn(render_pkg_mouth["alpha"][None])[0]
else:
alpha_mouth = render_pkg_mouth["alpha"]
mouth_image = render_pkg_mouth["render"] + view.background.cuda() / 255.0 * (1.0 - alpha_mouth)
alpha = render_pkg["alpha"]
image = render_pkg["render"] + mouth_image * (1.0 - alpha)
mouth_image = render_pkg_mouth["render"] - background[:, None, None] * (1.0 - alpha_mouth) + view.background.cuda() / 255.0 * (1.0 - alpha_mouth)
image = render_pkg["render"] - background[:, None, None] * (1.0 - alpha) + mouth_image * (1.0 - alpha)
pred = (image[0:3, ...].clamp(0, 1).permute(1, 2, 0).detach().cpu().numpy()* 255).astype(np.uint8)
all_preds.append(pred)
if not fast:
all_preds_face.append((render_pkg["render"].clamp(0, 1).permute(1, 2, 0).detach().cpu().numpy()* 255).astype(np.uint8)) # 人脸渲染
all_preds_mouth.append((render_pkg_mouth["render"].clamp(0, 1).permute(1, 2, 0).detach().cpu().numpy()* 255).astype(np.uint8)) # 嘴巴渲染
all_preds_mouth_bg.append(((mouth_image * (1.0 - alpha)).clamp(0, 1).permute(1, 2, 0).detach().cpu().numpy()* 255).astype(np.uint8))
all_gts.append(view.original_image.permute(1, 2, 0).cpu().numpy().astype(np.uint8)) # GT渲染
imageio.mimwrite(os.path.join(render_path, 'out.mp4'), all_preds, fps=25, quality=8, macro_block_size=1)
video_path = os.path.join(render_path, 'out.mp4')
audio_path = os.path.join(model_path.replace("output",'data'), 'aud_novel.wav')
output_path = os.path.join(render_path, 'out_with_audio.mp4')
if not fast:
imageio.mimwrite(os.path.join(gts_path, 'out.mp4'), all_gts, fps=25, quality=8, macro_block_size=1)
imageio.mimwrite(os.path.join(render_path, 'out_face.mp4'), all_preds_face, fps=25, quality=8, macro_block_size=1)
imageio.mimwrite(os.path.join(render_path, 'out_mouth.mp4'), all_preds_mouth, fps=25, quality=8, macro_block_size=1)
imageio.mimwrite(os.path.join(render_path, 'out_mouth_bg.mp4'), all_preds_mouth_bg, fps=25, quality=8, macro_block_size=1)
# Command to merge video and audio using ffmpeg
ffmpeg_cmd = f"ffmpeg -y -loglevel quiet -i {video_path} -i {audio_path} -strict -2 -c:v copy {output_path}"
os.system(ffmpeg_cmd)
def render_sets(dataset : ModelParams, iteration : int, pipeline : PipelineParams, use_train : bool, fast, dilate):
with torch.no_grad():
gaussians = GaussianModel(dataset.sh_degree)
gaussians_mouth = GaussianModel(dataset.sh_degree)
scene = Scene(dataset, gaussians, shuffle=False)
motion_net = MotionNetwork(args=dataset).cuda()
motion_net_mouth = MouthMotionNetwork(args=dataset).cuda()
(model_params, motion_params, model_mouth_params, motion_mouth_params) = torch.load(os.path.join(dataset.model_path, "chkpnt_fuse_latest.pth"))
motion_net.load_state_dict(motion_params, strict=False)
gaussians.restore(model_params, None)
motion_net_mouth.load_state_dict(motion_mouth_params, strict=False)
gaussians_mouth.restore(model_mouth_params, None)
# motion_net.fix(gaussians.get_xyz.cuda())
# motion_net_mouth.fix(gaussians_mouth.get_xyz.cuda())
bg_color = [0,1,0] if dataset.white_background else [0, 0, 0]
background = torch.tensor(bg_color, dtype=torch.float32, device="cuda")
render_set(dataset.model_path, "test" if not use_train else "train", scene.loaded_iter, scene.getTestCameras() if not use_train else scene.getTrainCameras(), gaussians, motion_net, gaussians_mouth, motion_net_mouth, pipeline, background, fast, dilate)
if __name__ == "__main__":
# Set up command line argument parser
parser = ArgumentParser(description="Testing script parameters")
model = ModelParams(parser)
pipeline = PipelineParams(parser)
parser.add_argument("--iteration", default=-1, type=int)
parser.add_argument("--use_train", action="store_true")
parser.add_argument("--quiet", action="store_true")
parser.add_argument("--fast", action="store_true")
parser.add_argument("--dilate", action="store_true")
args = get_combined_args(parser)
print("Rendering " + args.model_path)
# Initialize system state (RNG)
safe_state(args.quiet)
render_sets(model.extract(args), args.iteration, pipeline.extract(args), args.use_train, args.fast, args.dilate)