forked from chungyiweng/humannerf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
165 lines (120 loc) · 4.72 KB
/
run.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
import os
import torch
import numpy as np
from tqdm import tqdm
from core.data import create_dataloader
from core.nets import create_network
from core.utils.train_util import cpu_data_to_gpu
from core.utils.image_util import ImageWriter, to_8b_image, to_8b3ch_image
from configs import cfg, args
EXCLUDE_KEYS_TO_GPU = ['frame_name',
'img_width', 'img_height', 'ray_mask']
def load_network():
model = create_network()
ckpt_path = os.path.join(cfg.logdir, f'{cfg.load_net}.tar')
ckpt = torch.load(ckpt_path, map_location='cuda:0')
model.load_state_dict(ckpt['network'], strict=False)
print('load network from ', ckpt_path)
return model.cuda().deploy_mlps_to_secondary_gpus()
def unpack_alpha_map(alpha_vals, ray_mask, width, height):
alpha_map = np.zeros((height * width), dtype='float32')
alpha_map[ray_mask] = alpha_vals
return alpha_map.reshape((height, width))
def unpack_to_image(width, height, ray_mask, bgcolor,
rgb, alpha, truth=None):
rgb_image = np.full((height * width, 3), bgcolor, dtype='float32')
truth_image = np.full((height * width, 3), bgcolor, dtype='float32')
rgb_image[ray_mask] = rgb
rgb_image = to_8b_image(rgb_image.reshape((height, width, 3)))
if truth is not None:
truth_image[ray_mask] = truth
truth_image = to_8b_image(truth_image.reshape((height, width, 3)))
alpha_map = unpack_alpha_map(alpha, ray_mask, width, height)
alpha_image = to_8b3ch_image(alpha_map)
return rgb_image, alpha_image, truth_image
def _freeview(
data_type='freeview',
folder_name=None):
cfg.perturb = 0.
model = load_network()
test_loader = create_dataloader(data_type)
writer = ImageWriter(
output_dir=os.path.join(cfg.logdir, cfg.load_net),
exp_name=folder_name)
model.eval()
for batch in tqdm(test_loader):
for k, v in batch.items():
batch[k] = v[0]
data = cpu_data_to_gpu(
batch,
exclude_keys=EXCLUDE_KEYS_TO_GPU)
with torch.no_grad():
net_output = model(**data,
iter_val=cfg.eval_iter)
rgb = net_output['rgb']
alpha = net_output['alpha']
width = batch['img_width']
height = batch['img_height']
ray_mask = batch['ray_mask']
target_rgbs = batch.get('target_rgbs', None)
rgb_img, alpha_img, _ = unpack_to_image(
width, height, ray_mask, np.array(cfg.bgcolor) / 255.,
rgb.data.cpu().numpy(),
alpha.data.cpu().numpy())
imgs = [rgb_img]
if cfg.show_truth and target_rgbs is not None:
target_rgbs = to_8b_image(target_rgbs.numpy())
imgs.append(target_rgbs)
if cfg.show_alpha:
imgs.append(alpha_img)
img_out = np.concatenate(imgs, axis=1)
writer.append(img_out)
writer.finalize()
def run_freeview():
_freeview(
data_type='freeview',
folder_name=f"freeview_{cfg.freeview.frame_idx}" \
if not cfg.render_folder_name else cfg.render_folder_name)
def run_tpose():
cfg.ignore_non_rigid_motions = True
_freeview(
data_type='tpose',
folder_name='tpose' \
if not cfg.render_folder_name else cfg.render_folder_name)
def run_movement(render_folder_name='movement'):
cfg.perturb = 0.
model = load_network()
test_loader = create_dataloader('movement')
writer = ImageWriter(
output_dir=os.path.join(cfg.logdir, cfg.load_net),
exp_name=render_folder_name)
model.eval()
for idx, batch in enumerate(tqdm(test_loader)):
for k, v in batch.items():
batch[k] = v[0]
data = cpu_data_to_gpu(
batch,
exclude_keys=EXCLUDE_KEYS_TO_GPU + ['target_rgbs'])
with torch.no_grad():
net_output = model(**data, iter_val=cfg.eval_iter)
rgb = net_output['rgb']
alpha = net_output['alpha']
width = batch['img_width']
height = batch['img_height']
ray_mask = batch['ray_mask']
rgb_img, alpha_img, truth_img = \
unpack_to_image(
width, height, ray_mask, np.array(cfg.bgcolor)/255.,
rgb.data.cpu().numpy(),
alpha.data.cpu().numpy(),
batch['target_rgbs'])
imgs = [rgb_img]
if cfg.show_truth:
imgs.append(truth_img)
if cfg.show_alpha:
imgs.append(alpha_img)
img_out = np.concatenate(imgs, axis=1)
writer.append(img_out, img_name=f"{idx:06d}")
writer.finalize()
if __name__ == '__main__':
globals()[f'run_{args.type}']()