-
Notifications
You must be signed in to change notification settings - Fork 5
/
blender.py
168 lines (141 loc) · 4.88 KB
/
blender.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
# ------------------------------------------------------------------------------------
# NeRF-Factory
# Copyright (c) 2022 POSTECH, KAIST, Kakao Brain Corp. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 [see LICENSE for details]
# ------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------
# Modified from NeRF (https://github.com/bmild/nerf)
# Copyright (c) 2020 Google LLC. All Rights Reserved.
# ------------------------------------------------------------------------------------
import json
import os
import gdown
import imageio
import numpy as np
import torch
trans_t = lambda t: torch.tensor(
[[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, t], [0, 0, 0, 1]]
).float()
rot_phi = lambda phi: torch.tensor(
[
[1, 0, 0, 0],
[0, np.cos(phi), -np.sin(phi), 0],
[0, np.sin(phi), np.cos(phi), 0],
[0, 0, 0, 1],
]
).float()
rot_theta = lambda th: torch.tensor(
[
[np.cos(th), 0, -np.sin(th), 0],
[0, 1, 0, 0],
[np.sin(th), 0, np.cos(th), 0],
[0, 0, 0, 1],
]
).float()
def pose_spherical(theta, phi, radius):
c2w = trans_t(radius)
c2w = rot_phi(phi / 180.0 * np.pi) @ c2w
c2w = rot_theta(theta / 180.0 * np.pi) @ c2w
c2w = (
torch.tensor([[-1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]).float()
@ c2w
)
return c2w
def load_blender_data(
datadir: str,
scene_name: str,
compare_method: str,
train_skip: int,
val_skip: int,
test_skip: int,
cam_scale_factor: float,
white_bkgd: bool,
):
basedir = os.path.join(datadir, scene_name)
cam_trans = np.diag(np.array([1, -1, -1, 1], dtype=np.float32))
splits = ["train", "val", "test"]
metas = {}
# for s in splits:
# with open(os.path.join(basedir, compare_method, "transforms_{}_gt.json".format(s)), "r") as fp:
# metas[s] = json.load(fp)
#print('compare_method', compare_method)
fp_train = open(os.path.join(basedir, compare_method, "transforms_train.json"), "r") # training file
fp_val = open(os.path.join(basedir, "transforms_val.json"), "r") # evaluation file
fp_test = open(os.path.join(basedir, "transforms_test.json"), "r") # testing file
metas["train"], metas["val"], metas["test"] = json.load(fp_train), json.load(fp_val), json.load(fp_test)
images = []
extrinsics = []
counts = [0]
for s in splits: # train/ val/ test
meta = metas[s]
imgs = []
poses = []
if s == "train":
skip = train_skip
elif s == "val":
skip = val_skip
elif s == "test":
skip = test_skip
for frame in meta["frames"][::skip]:
fname = os.path.join(basedir, frame["file_path"])
imgs.append(imageio.imread(fname))
poses.append(np.array(frame["transform_matrix"]))
imgs = (np.array(imgs) / 255.0).astype(np.float32) # keep all 4 channels (RGBA)
poses = np.array(poses).astype(np.float32)
counts.append(counts[-1] + imgs.shape[0])
images.append(imgs)
extrinsics.append(poses)
i_split = [np.arange(counts[i], counts[i + 1]) for i in range(3)]
images = np.concatenate(images, 0)
extrinsics = np.concatenate(extrinsics, 0)
extrinsics[:, :3, 3] *= cam_scale_factor
extrinsics = extrinsics @ cam_trans
h, w = imgs[0].shape[:2]
num_frame = len(extrinsics)
i_split += [np.arange(num_frame)]
camera_angle_x = float(meta["camera_angle_x"])
focal = 0.5 * w / np.tan(0.5 * camera_angle_x)
intrinsics = np.array(
[
[[focal, 0.0, 0.5 * w], [0.0, focal, 0.5 * h], [0.0, 0.0, 1.0]]
for _ in range(num_frame)
]
)
image_sizes = np.array([[h, w] for _ in range(num_frame)])
# rendering poses in LOM dataset
render_poses = torch.stack(
[
pose_spherical(angle, -10.0, 4.0) @ cam_trans
for angle in np.linspace(70, 110, 30 + 1)[:-1]
],
0,
)
render_poses2 = torch.stack([
pose_spherical(110, angle2, 4.0) @ cam_trans
for angle2 in np.linspace(-10, 10, 20 + 1)[:-1]
],
0,)
render_poses3 = torch.stack([
pose_spherical(110*i, 10, 4.0*i) @ cam_trans
for i in np.linspace(1, 0.8, 20 + 1)[:-1]
],
0,)
render_poses = torch.cat((render_poses,render_poses2,render_poses3), 0)
render_poses[:, :3, 3] *= cam_scale_factor
near = 2.0
far = 6.0
if white_bkgd:
images = images[..., :3] * images[..., -1:] + (1.0 - images[..., -1:])
else:
images = images[..., :3]
return (
images,
intrinsics,
extrinsics,
image_sizes,
near,
far,
(-1, -1),
i_split,
render_poses,
)