-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest.py
238 lines (209 loc) · 6.94 KB
/
test.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
#
# 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 os
import os.path as osp
import sys
import torch
from tqdm import tqdm, trange
import torchvision
from time import time
import numpy as np
import concurrent.futures
import yaml
from argparse import ArgumentParser
from random import randint
import SimpleITK as sitk
sys.path.append("./")
from r2_gaussian.arguments import (
ModelParams,
PipelineParams,
get_combined_args,
)
from r2_gaussian.dataset import Scene
from r2_gaussian.gaussian import GaussianModel, render, query, initialize_gaussian
from r2_gaussian.utils.general_utils import safe_state, t2a
from r2_gaussian.utils.image_utils import metric_vol, metric_proj
def testing(
dataset: ModelParams,
pipeline: PipelineParams,
iteration: int,
skip_render_train: bool,
skip_render_test: bool,
skip_recon: bool,
):
# Set up dataset
scene = Scene(
dataset,
shuffle=False,
)
# Set up Gaussians
gaussians = GaussianModel(None) # scale_bound will be loaded later
loaded_iter = initialize_gaussian(gaussians, dataset, iteration)
scene.gaussians = gaussians
save_path = osp.join(
dataset.model_path,
"test",
"iter_{}".format(loaded_iter),
)
# Evaluate projection train
if not skip_render_train:
evaluate_render(
save_path,
"render_train",
scene.getTrainCameras(),
gaussians,
pipeline,
)
# Evaluate projection test
if not skip_render_test:
evaluate_render(
save_path,
"render_test",
scene.getTestCameras(),
gaussians,
pipeline,
)
# Evaluate volume reconstruction
if not skip_recon:
evaluate_volume(
save_path,
"reconstruction",
scene.scanner_cfg,
gaussians,
pipeline,
scene.vol_gt,
)
def evaluate_volume(
save_path,
name,
scanner_cfg,
gaussians: GaussianModel,
pipeline: PipelineParams,
vol_gt,
):
"""Evaluate volume reconstruction."""
slice_save_path = osp.join(save_path, name)
os.makedirs(slice_save_path, exist_ok=True)
query_pkg = query(
gaussians,
scanner_cfg["offOrigin"],
scanner_cfg["nVoxel"],
scanner_cfg["sVoxel"],
pipeline,
)
vol_pred = query_pkg["vol"]
psnr_3d, _ = metric_vol(vol_gt, vol_pred, "psnr")
ssim_3d, ssim_3d_axis = metric_vol(vol_gt, vol_pred, "ssim")
multithread_write(
[vol_gt[..., i][None] for i in range(vol_gt.shape[2])],
slice_save_path,
"_gt",
)
multithread_write(
[vol_pred[..., i][None] for i in range(vol_pred.shape[2])],
slice_save_path,
"_pred",
)
eval_dict = {
"psnr_3d": psnr_3d,
"ssim_3d": ssim_3d,
"ssim_3d_x": ssim_3d_axis[0],
"ssim_3d_y": ssim_3d_axis[1],
"ssim_3d_z": ssim_3d_axis[2],
}
with open(osp.join(save_path, "eval3d.yml"), "w") as f:
yaml.dump(eval_dict, f, default_flow_style=False, sort_keys=False)
np.save(osp.join(save_path, "vol_gt.npy"), t2a(vol_gt))
np.save(osp.join(save_path, "vol_pred.npy"), t2a(vol_pred))
# For visualization with 3D slicer
sitk.WriteImage(
sitk.GetImageFromArray(t2a(vol_gt).transpose(2, 0, 1)),
os.path.join(save_path, "vol_gt.nii.gz"),
)
sitk.WriteImage(
sitk.GetImageFromArray(t2a(vol_pred).transpose(2, 0, 1)),
os.path.join(save_path, "vol_pred.nii.gz"),
)
print(f"{name} complete. psnr_3d: {psnr_3d}, ssim_3d: {ssim_3d}")
def evaluate_render(save_path, name, views, gaussians, pipeline):
"""Evaluate projection rendering."""
proj_save_path = osp.join(save_path, name)
# If already rendered, skip.
if osp.exists(osp.join(save_path, "eval.yml")):
print("{} in {} already rendered. Skip.".format(name, save_path))
return
os.makedirs(proj_save_path, exist_ok=True)
gt_list = []
render_list = []
for view in tqdm(views, desc="render {}".format(name), leave=False):
rendering = render(view, gaussians, pipeline)["render"]
gt = view.original_image[0:3, :, :]
gt_list.append(gt)
render_list.append(rendering)
multithread_write(gt_list, proj_save_path, "_gt")
multithread_write(render_list, proj_save_path, "_pred")
images = torch.concat(render_list, 0).permute(1, 2, 0)
gt_images = torch.concat(gt_list, 0).permute(1, 2, 0)
psnr_2d, psnr_2d_projs = metric_proj(gt_images, images, "psnr")
ssim_2d, ssim_2d_projs = metric_proj(gt_images, images, "ssim")
eval_dict = {
"psnr_2d": psnr_2d,
"ssim_2d": ssim_2d,
"psnr_2d_projs": psnr_2d_projs,
"ssim_2d_projs": ssim_2d_projs,
}
with open(osp.join(save_path, f"eval2d_{name}.yml"), "w") as f:
yaml.dump(eval_dict, f, default_flow_style=False, sort_keys=False)
print(
f"{name} complete. psnr_2d: {eval_dict['psnr_2d']}, ssim_2d: {eval_dict['ssim_2d']}."
)
def multithread_write(image_list, path, suffix):
executor = concurrent.futures.ThreadPoolExecutor(max_workers=None)
def write_image(image, count, path):
try:
torchvision.utils.save_image(
image, osp.join(path, "{0:05d}".format(count) + "{}.png".format(suffix))
)
np.save(
osp.join(path, "{0:05d}".format(count) + "{}.npy".format(suffix)),
image.cpu().numpy()[0],
)
return count, True
except:
return count, False
tasks = []
for index, image in enumerate(image_list):
tasks.append(executor.submit(write_image, image, index, path))
executor.shutdown()
for index, status in enumerate(tasks):
if status == False:
write_image(image_list[index], index, path)
if __name__ == "__main__":
# Set up command line argument parser
parser = ArgumentParser(description="Testing script parameters")
model = ModelParams(parser, sentinel=True)
pipeline = PipelineParams(parser)
parser.add_argument("--quiet", action="store_true")
parser.add_argument("--iteration", default=-1, type=int)
parser.add_argument("--skip_render_train", action="store_true", default=False)
parser.add_argument("--skip_render_test", action="store_true", default=False)
parser.add_argument("--skip_recon", action="store_true", default=False)
args = get_combined_args(parser)
safe_state(args.quiet)
with torch.no_grad():
testing(
model.extract(args),
pipeline.extract(args),
args.iteration,
args.skip_render_train,
args.skip_render_test,
args.skip_recon,
)