-
Notifications
You must be signed in to change notification settings - Fork 25
/
render.py
193 lines (164 loc) · 6 KB
/
render.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
import os
import numpy as np
import shutil
import torch
import torch.utils.data.distributed
from torch.utils.data import DataLoader
from gnt.data_loaders import dataset_dict
from gnt.render_image import render_single_image
from gnt.model import GNTModel
from gnt.sample_ray import RaySamplerSingleImage
from utils import img_HWC2CHW, colorize, img2psnr, lpips, ssim
import config
import torch.distributed as dist
from gnt.projection import Projector
from gnt.data_loaders.create_training_dataset import create_training_dataset
import imageio
def worker_init_fn(worker_id):
np.random.seed(np.random.get_state()[1][0] + worker_id)
def synchronize():
"""
Helper function to synchronize (barrier) among all processes when
using distributed training
"""
if not dist.is_available():
return
if not dist.is_initialized():
return
world_size = dist.get_world_size()
if world_size == 1:
return
dist.barrier()
@torch.no_grad()
def render(args):
device = "cuda:{}".format(args.local_rank)
out_folder = os.path.join(args.rootdir, "out", args.expname)
print("outputs will be saved to {}".format(out_folder))
os.makedirs(out_folder, exist_ok=True)
# save the args and config files
f = os.path.join(out_folder, "args.txt")
with open(f, "w") as file:
for arg in sorted(vars(args)):
attr = getattr(args, arg)
file.write("{} = {}\n".format(arg, attr))
if args.config is not None:
f = os.path.join(out_folder, "config.txt")
if not os.path.isfile(f):
shutil.copy(args.config, f)
assert args.eval_dataset == "llff_render", ValueError(
"rendering mode available only for llff dataset"
)
dataset = dataset_dict[args.eval_dataset](args, scenes=args.eval_scenes)
loader = DataLoader(dataset, batch_size=1)
iterator = iter(loader)
# Create GNT model
model = GNTModel(
args, load_opt=not args.no_load_opt, load_scheduler=not args.no_load_scheduler
)
# create projector
projector = Projector(device=device)
indx = 0
while True:
try:
data = next(iterator)
except:
break
if args.local_rank == 0:
tmp_ray_sampler = RaySamplerSingleImage(data, device, render_stride=args.render_stride)
H, W = tmp_ray_sampler.H, tmp_ray_sampler.W
log_view(
indx,
args,
model,
tmp_ray_sampler,
projector,
render_stride=args.render_stride,
prefix="val/" if args.run_val else "train/",
out_folder=out_folder,
ret_alpha=args.N_importance > 0,
single_net=args.single_net,
)
torch.cuda.empty_cache()
indx += 1
@torch.no_grad()
def log_view(
global_step,
args,
model,
ray_sampler,
projector,
render_stride=1,
prefix="",
out_folder="",
ret_alpha=False,
single_net=True,
):
model.switch_to_eval()
with torch.no_grad():
ray_batch = ray_sampler.get_all()
if model.feature_net is not None:
featmaps = model.feature_net(ray_batch["src_rgbs"].squeeze(0).permute(0, 3, 1, 2))
else:
featmaps = [None, None]
ret = render_single_image(
ray_sampler=ray_sampler,
ray_batch=ray_batch,
model=model,
projector=projector,
chunk_size=args.chunk_size,
N_samples=args.N_samples,
inv_uniform=args.inv_uniform,
det=True,
N_importance=args.N_importance,
white_bkgd=args.white_bkgd,
render_stride=render_stride,
featmaps=featmaps,
ret_alpha=ret_alpha,
single_net=single_net,
)
average_im = ray_sampler.src_rgbs.cpu().mean(dim=(0, 1))
if args.render_stride != 1:
average_im = average_im[::render_stride, ::render_stride]
average_im = img_HWC2CHW(average_im)
rgb_coarse = img_HWC2CHW(ret["outputs_coarse"]["rgb"].detach().cpu())
if "depth" in ret["outputs_coarse"].keys():
depth_pred = ret["outputs_coarse"]["depth"].detach().cpu()
depth_coarse = img_HWC2CHW(colorize(depth_pred, cmap_name="jet"))
else:
depth_coarse = None
if ret["outputs_fine"] is not None:
rgb_fine = img_HWC2CHW(ret["outputs_fine"]["rgb"].detach().cpu())
if "depth" in ret["outputs_fine"].keys():
depth_pred = ret["outputs_fine"]["depth"].detach().cpu()
depth_fine = img_HWC2CHW(colorize(depth_pred, cmap_name="jet"))
else:
rgb_fine = None
depth_fine = None
rgb_coarse = rgb_coarse.permute(1, 2, 0).detach().cpu().numpy()
filename = os.path.join(out_folder, prefix[:-1] + "_{:03d}_coarse.png".format(global_step))
imageio.imwrite(filename, rgb_coarse)
if depth_coarse is not None:
depth_coarse = depth_coarse.permute(1, 2, 0).detach().cpu().numpy()
filename = os.path.join(
out_folder, prefix[:-1] + "_{:03d}_coarse_depth.png".format(global_step)
)
imageio.imwrite(filename, depth_coarse)
if rgb_fine is not None:
rgb_fine = rgb_fine.permute(1, 2, 0).detach().cpu().numpy()
filename = os.path.join(out_folder, prefix[:-1] + "_{:03d}_fine.png".format(global_step))
imageio.imwrite(filename, rgb_fine)
if depth_fine is not None:
depth_fine = depth_fine.permute(1, 2, 0).detach().cpu().numpy()
filename = os.path.join(
out_folder, prefix[:-1] + "_{:03d}_fine_depth.png".format(global_step)
)
imageio.imwrite(filename, depth_fine)
if __name__ == "__main__":
parser = config.config_parser()
parser.add_argument("--run_val", action="store_true", help="run on val set")
args = parser.parse_args()
if args.distributed:
torch.cuda.set_device(args.local_rank)
torch.distributed.init_process_group(backend="nccl", init_method="env://")
synchronize()
render(args)