-
Notifications
You must be signed in to change notification settings - Fork 11
/
render_model.py
263 lines (225 loc) · 8.35 KB
/
render_model.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
251
252
253
254
255
256
257
258
259
260
261
262
263
"""Load g model and render all outputs to disc"""
from dataclasses import dataclass
from pathlib import Path
import torch
import tyro
import os
import numpy as np
import shutil
from nerfstudio.cameras.cameras import Cameras
from nerfstudio.models.splatfacto import SplatfactoModel
from nerfstudio.utils.eval_utils import eval_setup
from nerfstudio.utils import colormaps
from nerfstudio.data.datasets.base_dataset import InputDataset
from PIL import Image
from torch import Tensor
from typing import List, Literal, Optional, Union
def save_img(image, image_path, verbose=True) -> None:
"""helper to save images
Args:
image: image to save (numpy, Tensor)
image_path: path to save
verbose: whether to print save path
Returns:
None
"""
if image.shape[-1] == 1 and torch.is_tensor(image):
image = image.repeat(1, 1, 3)
if torch.is_tensor(image):
image = image.detach().cpu().numpy() * 255
image = image.astype(np.uint8)
if not Path(os.path.dirname(image_path)).exists():
Path(os.path.dirname(image_path)).mkdir(parents=True)
im = Image.fromarray(image)
if verbose:
print("saving to: ", image_path)
im.save(image_path)
# Depth Scale Factor m to mm
SCALE_FACTOR = 0.001
SAVE_RAW_DEPTH = False
def save_depth(depth, depth_path, verbose=True, scale_factor=SCALE_FACTOR) -> None:
"""helper to save metric depths
Args:
depth: image to save (numpy, Tensor)
depth_path: path to save
verbose: whether to print save path
scale_factor: depth metric scaling factor
Returns:
None
"""
if torch.is_tensor(depth):
depth = depth.float() / scale_factor
depth = depth.detach().cpu().numpy()
else:
depth = depth / scale_factor
if not Path(os.path.dirname(depth_path)).exists():
Path(os.path.dirname(depth_path)).mkdir(parents=True)
if verbose:
print("saving to: ", depth_path)
np.save(depth_path, depth)
def save_outputs_helper(
rgb_out: Optional[Tensor],
gt_img: Optional[Tensor],
depth_color: Optional[Tensor],
depth_gt_color: Optional[Tensor],
depth_gt: Optional[Tensor],
depth: Optional[Tensor],
normal_gt: Optional[Tensor],
normal: Optional[Tensor],
render_output_path: Path,
image_name: Optional[str],
) -> None:
"""Helper to save model rgb/depth/gt outputs to disk
Args:
rgb_out: rgb image
gt_img: gt rgb image
depth_color: colored depth image
depth_gt_color: gt colored depth image
depth_gt: gt depth map
depth: depth map
render_output_path: save directory path
image_name: stem of save name
Returns:
None
"""
if image_name is None:
image_name = ""
if rgb_out is not None and gt_img is not None:
# easier consecutive compare
save_img(rgb_out, os.getcwd() + f"/{render_output_path}/{image_name}_pred.png", False)
save_img(gt_img, os.getcwd() + f"/{render_output_path}/{image_name}_gt.png", False)
if depth_color is not None:
save_img(
depth_color,
os.getcwd()
+ f"/{render_output_path}/pred/depth/colorised/{image_name}.png",
False,
)
if depth_gt_color is not None:
save_img(
depth_gt_color,
os.getcwd() + f"/{render_output_path}/gt/depth/colorised/{image_name}.png",
False,
)
if depth_gt is not None:
# save metric depths
save_depth(
depth_gt,
os.getcwd() + f"/{render_output_path}/gt/depth/raw/{image_name}.npy",
False,
)
if SAVE_RAW_DEPTH:
if depth is not None:
save_depth(
depth,
os.getcwd() + f"/{render_output_path}/pred/depth/raw/{image_name}.npy",
False,
)
if normal is not None:
save_normal(
normal,
os.getcwd() + f"/{render_output_path}/pred/normal/{image_name}.png",
verbose=False,
)
if normal_gt is not None:
save_normal(
normal_gt,
os.getcwd() + f"/{render_output_path}/gt/normal/{image_name}.png",
verbose=False,
)
@dataclass
class RenderModel:
"""Render outputs of a GS model."""
load_config: Path = Path("outputs/")
"""Path to the config YAML file."""
output_dir: Path = Path("./data/renders/")
"""Path to the output directory."""
set: Literal["train", "eval"] = "eval"
"""Dataset to test with (train or eval)"""
output_same_dir: bool = True
"""Output to the subdirectory of the load_config path"""
def main(self):
if self.output_same_dir:
self.output_dir = os.path.join(os.path.dirname(self.load_config), 'renders')
if os.path.exists(self.output_dir):
shutil.rmtree(self.output_dir)
os.makedirs(self.output_dir)
print('writing %s' % str(self.output_dir))
_, pipeline, _, _ = eval_setup(self.load_config)
assert isinstance(pipeline.model, SplatfactoModel)
model: SplatfactoModel = pipeline.model
dataset: InputDataset
with torch.no_grad():
if self.set == "train":
dataset = pipeline.datamanager.train_dataset
images = pipeline.datamanager.cached_train
elif self.set == "eval":
dataset = pipeline.datamanager.eval_dataset
images = pipeline.datamanager.cached_eval
else:
raise RuntimeError("Invalid set")
cameras: Cameras = dataset.cameras # type: ignore
for image_idx in range(len(dataset)): # type: ignore
data = images[image_idx]
# process batch gt data
mask = None
if "mask" in data:
mask = data["mask"]
gt_img = 256 - data["image"] # not sure why negative
if "sensor_depth" in data:
depth_gt = data["sensor_depth"]
depth_gt_color = colormaps.apply_depth_colormap(
data["sensor_depth"]
)
else:
depth_gt = None
depth_gt_color = None
if "normal" in data:
normal_gt = data["normal"]
else:
normal_gt = None
# process pred outputs
camera = cameras[image_idx : image_idx + 1].to("cpu")
#if self.set == "train":
# camera idx is used to fetch camera optimizer adjustments
# and should not be used for 'eval' data
camera.metadata['cam_idx'] = image_idx
outputs = model.get_outputs_for_camera(camera=camera)
rgb_out, depth_out = outputs["rgb"], outputs["depth"]
normal = None
if "normal" in outputs:
normal = outputs["normal"]
seq_name = Path(dataset.image_filenames[image_idx])
image_name = f"{seq_name.stem}"
depth_color = colormaps.apply_depth_colormap(depth_out)
depth = depth_out.detach().cpu().numpy()
if mask is not None:
rgb_out = rgb_out * mask
gt_img = gt_img * mask
if depth_color is not None:
depth_color = depth_color * mask
if depth_gt_color is not None:
depth_gt_color = depth_gt_color * mask
if depth_gt is not None:
depth_gt = depth_gt * mask
if depth is not None:
depth = depth * mask
if normal_gt is not None:
normal_gt = normal_gt * mask
if normal is not None:
normal = normal * mask
# save all outputs
save_outputs_helper(
rgb_out,
gt_img,
depth_color,
depth_gt_color,
depth_gt,
depth,
normal_gt,
normal,
self.output_dir,
image_name,
)
if __name__ == "__main__":
tyro.cli(RenderModel).main()