-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval_nerf.py
346 lines (309 loc) · 11.7 KB
/
eval_nerf.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import argparse
import csv
import os
import time
import imageio
import IPython
import lpips
import numpy as np
import torch
import torchvision
import yaml
from pytorch_msssim import ssim
from tqdm import tqdm
import update_compression
from nerf import (
CfgNode,
get_embedding_function,
get_ray_bundle,
load_blender_data,
load_llff_data,
models,
run_one_iter_of_nerf,
)
from nerf.nerf_helpers import img2mse, mse2psnr
def cast_to_image(tensor, dataset_type):
# Input tensor is (H, W, 3). Convert to (3, H, W).
tensor = tensor.permute(2, 0, 1)
# Convert to PIL Image and then np.array (output shape: (H, W, 3))
img = np.array(torchvision.transforms.ToPILImage()(tensor.detach().cpu()))
return img
# # Map back to shape (3, H, W), as tensorboard needs channels first.
# return np.moveaxis(img, [-1], [0])
def cast_to_disparity_image(tensor):
img = (tensor - tensor.min()) / (tensor.max() - tensor.min())
img = img.clamp(0, 1) * 255
return img.detach().cpu().numpy().astype(np.uint8)
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--config", type=str, required=True, help="Path to (.yml) config file."
)
parser.add_argument(
"--checkpoint",
type=str,
required=True,
help="Checkpoint / pre-trained model to evaluate.",
)
parser.add_argument(
"--csvfile",
type=str,
required=True,
help="The path of the file to save the test statistics",
)
parser.add_argument(
"--checkpoint-type",
type=str,
required=True,
choices=["federated", "federated_cumiter", "single_node", "control", "initial"],
help="The type of checkpoint",
)
parser.add_argument(
"--savedir", type=str, help="Save images to this directory, if specified."
)
parser.add_argument(
"--save-disparity-image", action="store_true", help="Save disparity images too."
)
configargs = parser.parse_args()
# Read config file.
cfg = None
with open(configargs.config, "r") as f:
cfg_dict = yaml.load(f, Loader=yaml.FullLoader)
cfg = CfgNode(cfg_dict)
images, poses, render_poses, hwf = None, None, None, None
i_train, i_val, i_test = None, None, None
if cfg.dataset.type.lower() == "blender":
# Load blender dataset
images, poses, render_poses, hwf, i_split = load_blender_data(
cfg.dataset.basedir,
half_res=cfg.dataset.half_res,
testskip=cfg.dataset.testskip,
)
i_train, i_val, i_test = i_split
H, W, focal = hwf
H, W = int(H), int(W)
if cfg.nerf.train.white_background:
images = images[..., :3] * images[..., -1:] + (1.0 - images[..., -1:])
elif cfg.dataset.type.lower() == "llff":
# Load LLFF dataset
images, poses, bds, render_poses, i_test = load_llff_data(
cfg.dataset.basedir,
factor=cfg.dataset.downsample_factor,
)
hwf = poses[0, :3, -1]
H, W, focal = hwf
hwf = [int(H), int(W), focal]
render_poses = torch.from_numpy(render_poses)
# LLFF only uses a single holdout test image, but make it a list to align with
# the blender convention
i_test = [int(i_test)]
print(i_test)
# Device on which to run.
device = "cpu"
if torch.cuda.is_available():
device = "cuda"
encode_position_fn = get_embedding_function(
num_encoding_functions=cfg.models.coarse.num_encoding_fn_xyz,
include_input=cfg.models.coarse.include_input_xyz,
log_sampling=cfg.models.coarse.log_sampling_xyz,
)
encode_direction_fn = None
if cfg.models.coarse.use_viewdirs:
encode_direction_fn = get_embedding_function(
num_encoding_functions=cfg.models.coarse.num_encoding_fn_dir,
include_input=cfg.models.coarse.include_input_dir,
log_sampling=cfg.models.coarse.log_sampling_dir,
)
# Initialize a coarse resolution model.
model_coarse = getattr(models, cfg.models.coarse.type)(
num_encoding_fn_xyz=cfg.models.coarse.num_encoding_fn_xyz,
num_encoding_fn_dir=cfg.models.coarse.num_encoding_fn_dir,
include_input_xyz=cfg.models.coarse.include_input_xyz,
include_input_dir=cfg.models.coarse.include_input_dir,
use_viewdirs=cfg.models.coarse.use_viewdirs,
)
model_coarse.to(device)
# If a fine-resolution model is specified, initialize it.
model_fine = None
if hasattr(cfg.models, "fine"):
model_fine = getattr(models, cfg.models.fine.type)(
num_encoding_fn_xyz=cfg.models.fine.num_encoding_fn_xyz,
num_encoding_fn_dir=cfg.models.fine.num_encoding_fn_dir,
include_input_xyz=cfg.models.fine.include_input_xyz,
include_input_dir=cfg.models.fine.include_input_dir,
use_viewdirs=cfg.models.fine.use_viewdirs,
)
model_fine.to(device)
checkpoint = torch.load(configargs.checkpoint, map_location=device)
# If federated compression was used for the experiment, reparameterise models
if configargs.checkpoint_type in ["federated", "single_node", "federated_cumiter"]:
if cfg.federated.compress_method == "ML":
model_coarse = update_compression.reparameterise_model_ML_from_state_dict(
model_coarse, checkpoint["model_coarse_state_dict"]
)
if model_fine is not None:
model_fine = update_compression.reparameterise_model_ML_from_state_dict(
model_fine, checkpoint["model_fine_state_dict"]
)
model_coarse.load_state_dict(checkpoint["model_coarse_state_dict"])
if checkpoint["model_fine_state_dict"]:
try:
model_fine.load_state_dict(checkpoint["model_fine_state_dict"])
except:
print(
"The checkpoint has a fine-level model, but it could "
"not be loaded (possibly due to a mismatched config file."
)
# All the previous state dict and reparameterisation stuff is messy as, so just do
# this to 100% make sure
model_coarse.to(device)
model_fine.to(device)
if "height" in checkpoint.keys():
hwf[0] = checkpoint["height"]
if "width" in checkpoint.keys():
hwf[1] = checkpoint["width"]
if "focal_length" in checkpoint.keys():
hwf[2] = checkpoint["focal_length"]
model_coarse.eval()
if model_fine:
model_fine.eval()
render_poses = render_poses.float().to(device)
# Create directory to save images to.
if configargs.savedir is not None:
os.makedirs(configargs.savedir, exist_ok=True)
if configargs.save_disparity_image:
os.makedirs(os.path.join(configargs.savedir, "disparity"), exist_ok=True)
csv_f = open(configargs.csvfile, "w")
csv_writer = csv.writer(csv_f)
csv_header = [
"exp_id",
"config_file",
"ckpt",
"ckpt_type",
"dataset",
"param_count_coarse",
"param_count_fine",
"buffer_count_coarse",
"buffer_count_fine",
"weight_count_coarse",
"weight_count_fine",
"bias_count_coarse",
"bias_count_fine",
"train_iters",
"initialise_image_count",
"node_count",
"partition_method",
"merge_every",
"compress_rank",
"compress_rank_variance_proportion",
"compress_method",
"img_i",
"loss",
"psnr",
"ssim",
"lpips",
]
csv_writer.writerow(csv_header)
# Evaluation loop
times_per_image = []
# for i, pose in enumerate(tqdm(render_poses)):
loss_fn_vgg = lpips.LPIPS(net="vgg")
for i in tqdm(i_test):
pose = poses[i]
target = torch.Tensor(images[i]).to(device)[:, :, :3] # discard 4th channel
start = time.time()
rgb = None, None
disp = None, None
# Render at pose
with torch.no_grad():
pose = pose[:3, :4]
if isinstance(pose, np.ndarray):
pose = torch.from_numpy(pose).to(device)
ray_origins, ray_directions = get_ray_bundle(hwf[0], hwf[1], hwf[2], pose)
rgb_coarse, disp_coarse, _, rgb_fine, disp_fine, _ = run_one_iter_of_nerf(
hwf[0],
hwf[1],
hwf[2],
model_coarse,
model_fine,
ray_origins.to(device),
ray_directions.to(device),
cfg,
mode="validation",
encode_position_fn=encode_position_fn,
encode_direction_fn=encode_direction_fn,
)
rgb_save = rgb_fine if rgb_fine is not None else rgb_coarse
if configargs.save_disparity_image:
disp = disp_fine if disp_fine is not None else disp_coarse
times_per_image.append(time.time() - start)
# Calculate comparison metrics for image
# loss and PSNR
rgb = rgb_save.clone().to("cpu")
target = target.to("cpu")
img_loss = img2mse(rgb, target)
loss = img_loss
psnr = mse2psnr(img_loss)
# SSIM
# need to permute axes from H,W,D -> N,D,H,W
rgb = torch.permute(rgb, (2, 0, 1))
rgb = torch.unsqueeze(rgb, 0)
target = torch.permute(target, (2, 0, 1))
target = torch.unsqueeze(target, 0)
ssim_val = ssim(rgb, target, data_range=1)
# LPIPS
# uses same axis configuration as SSIM
# normalise to [-1,1] from [0,1]
target = 2 * target - 1
rgb = 2 * rgb - 1
lpips_loss = loss_fn_vgg(rgb, target)
print(
f"loss: {loss.item()}, psnr: {psnr}, ssim: {ssim_val.item()}, lpips: {lpips_loss.item()}"
)
# Write CSV statistics
csv_row = [
cfg.experiment.logdir + "/" + cfg.experiment.id,
configargs.config,
configargs.checkpoint,
configargs.checkpoint_type,
cfg.dataset.basedir,
update_compression.get_parameter_count(model_coarse),
update_compression.get_parameter_count(model_fine) if model_fine else None,
update_compression.get_buffer_count(model_coarse),
update_compression.get_buffer_count(model_fine),
update_compression.get_weight_count(model_coarse),
update_compression.get_weight_count(model_fine),
update_compression.get_bias_count(model_coarse),
update_compression.get_bias_count(model_fine),
cfg.experiment.train_iters,
cfg.federated.initialise_image_count,
cfg.federated.nodes,
cfg.federated.partition,
cfg.federated.merge_every,
cfg.federated.compress_rank
if cfg.federated.compress_rank != "none"
else None,
cfg.federated.compress_rank_variance_proportion
if cfg.federated.compress_rank_variance_proportion != "none"
else None,
cfg.federated.compress_method,
i,
loss.item(),
psnr,
ssim_val.item(),
lpips_loss.item(),
]
csv_writer.writerow(csv_row)
# Save image and disparity if required
if configargs.savedir:
savefile = os.path.join(configargs.savedir, f"{i:04d}.png")
imageio.imwrite(
savefile, cast_to_image(rgb_save[..., :3], cfg.dataset.type.lower())
)
if configargs.save_disparity_image:
savefile = os.path.join(configargs.savedir, "disparity", f"{i:04d}.png")
imageio.imwrite(savefile, cast_to_disparity_image(disp))
tqdm.write(f"Avg time per image: {sum(times_per_image) / (i + 1)}")
csv_f.close()
if __name__ == "__main__":
main()