-
Notifications
You must be signed in to change notification settings - Fork 0
/
render_samples.py
250 lines (219 loc) · 6.32 KB
/
render_samples.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
import os
from parser import config_parser
import numpy as np
import torch
from load_llff import get_data_variables
from render_utils import render_path, save_res
from run_nerf_helpers import create_nerf
from train_helpers import get_rotation
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def get_pose_time_after_rot(pose2render, time2render, rotation_axis, rotation_degrees):
pose2render = [pose2render]
for r_axis, r_deg in zip(rotation_axis, rotation_degrees):
rotation = torch.from_numpy(get_rotation(r_axis, np.deg2rad(r_deg))).type(
pose2render[0].type()
)
novel_pose2render = pose2render[0].clone()
novel_pose2render[:, :3, :3] = novel_pose2render[:, :3, :3] @ rotation.T
pose2render.append(novel_pose2render)
time2render = np.stack([time2render] * (1 + len(rotation_axis)), 1)
pose2render = torch.stack(pose2render, 1)
assert time2render.shape[0] == pose2render.shape[0]
assert time2render.shape[1] == pose2render.shape[1]
return pose2render, time2render
def save_render(
basedir,
expname,
result_type,
idx,
pose2render,
time2render,
hwf,
chunk,
render_kwargs_test,
):
testsavedir = os.path.join(basedir, expname, result_type + f"_{idx:06d}")
os.makedirs(testsavedir, exist_ok=True)
with torch.no_grad():
ret = render_path(
pose2render,
time2render,
hwf,
chunk,
render_kwargs_test,
savedir=testsavedir,
)
moviebase = os.path.join(testsavedir, f"{expname}_{result_type}_{idx:06d}_")
save_res(moviebase, ret)
def render_fix(
basedir,
expname,
idx,
chunk,
hwf,
render_kwargs_test,
poses,
view_idx=None,
time_idx=None,
key="",
rotation_axis=["x"],
rotation_degrees=[0],
):
"""
Fix view if view_idx is not None.
Fix time if time_idx is not None.
If both view_idx and time_idx are None, render test views.
"""
num_img = int(render_kwargs_test["num_img"])
i_train = np.arange(num_img)
if view_idx is not None:
result_type = f"{key}testset_view{view_idx:03d}"
time2render = i_train / float(num_img) * 2.0 - 1.0
pose2render = torch.Tensor(poses[view_idx : view_idx + 1, ...]).expand(
[num_img, 3, 4]
)
elif time_idx is not None:
result_type = f"{key}testset_time{time_idx:03d}"
time2render = np.tile(time_idx, [int(num_img)]) / float(num_img) * 2.0 - 1.0
pose2render = torch.Tensor(poses)
else:
result_type = f"{key}testset"
time2render = i_train / float(num_img) * 2.0 - 1.0
pose2render = torch.Tensor(poses)
pose2render, time2render = get_pose_time_after_rot(
pose2render, time2render, rotation_axis, rotation_degrees
)
save_render(
basedir,
expname,
result_type,
idx,
pose2render,
time2render,
hwf,
chunk,
render_kwargs_test,
)
def render_novel_view_and_time(
basedir,
expname,
idx,
chunk,
hwf,
render_kwargs_test,
render_poses,
key="",
rotation_axis=["x"],
rotation_degrees=[0],
):
"""
Change time and view at the same time.
"""
result_type = f"{key}novelviewtime"
num_img = int(render_kwargs_test["num_img"])
i_train = np.arange(num_img)
time2render = np.concatenate(
(
np.repeat((i_train / float(num_img) * 2.0 - 1.0), 4),
np.repeat((i_train / float(num_img) * 2.0 - 1.0)[::-1][1:-1], 4),
)
)
if len(time2render) > len(render_poses):
pose2render = np.tile(
render_poses, (int(np.ceil(len(time2render) / len(render_poses))), 1, 1)
)
pose2render = pose2render[: len(time2render)]
pose2render = torch.Tensor(pose2render)
else:
time2render = np.tile(
time2render, int(np.ceil(len(render_poses) / len(time2render)))
)
time2render = time2render[: len(render_poses)]
pose2render = torch.Tensor(render_poses)
pose2render, time2render = get_pose_time_after_rot(
pose2render, time2render, rotation_axis, rotation_degrees
)
save_render(
basedir,
expname,
result_type,
idx,
pose2render,
time2render,
hwf,
chunk,
render_kwargs_test,
)
def main():
parser = config_parser()
args = parser.parse_args()
if args.random_seed is not None:
print("Fixing random seed", args.random_seed)
np.random.seed(args.random_seed)
# Create log dir and copy the config file
basedir = args.basedir
expname = args.expname
os.makedirs(os.path.join(basedir, expname), exist_ok=True)
# Get data variables
(
images,
invdepths,
masks,
poses,
bds_dict,
render_poses,
grids,
hwf,
num_img,
N_rand,
) = get_data_variables(args)
# Create nerf model
num_objects = len(masks[0]) - 1 or 1
render_kwargs_train, render_kwargs_test, start, grad_vars, optimizer = create_nerf(
args, num_objects
)
render_kwargs_test.update(bds_dict)
axis = ["x"] * num_objects
angle = [0] * num_objects
# axis = ["x", "x", "y"]
# angle = [0, -10, 15]
# render_kwargs_test.update({"cam_order": [0, 1, 2, 2]})
render_kwargs_test.update({"hard_blending": True})
key = "testing_cam_"
for ax, ang in zip(axis, angle):
key += f"{ax}_{ang}_"
fix_values = [
(None, None),
(args.view_idx, None),
(None, args.time_idx),
]
for fix_value in fix_values:
render_fix(
basedir,
expname,
start + 1,
args.chunk,
hwf,
render_kwargs_test,
poses,
view_idx=fix_value[0],
time_idx=fix_value[1],
key=key,
rotation_axis=axis,
rotation_degrees=angle,
)
render_novel_view_and_time(
basedir,
expname,
start + 1,
args.chunk,
hwf,
render_kwargs_test,
render_poses,
key=key,
rotation_axis=axis,
rotation_degrees=angle,
)
if __name__ == "__main__":
torch.set_default_tensor_type("torch.cuda.FloatTensor")
main()