-
Notifications
You must be signed in to change notification settings - Fork 3
/
run_rasterize.py
125 lines (107 loc) · 4.37 KB
/
run_rasterize.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
import torch
from rasterizer.rasterizer import rasterize
from utils import config_parser
from dataset.dataset import nerfDataset, ScanDataset, DTUDataset, TTDataset, toyDeskDataset
import os
import numpy as np
import time
from utils import config_parser
from tqdm import tqdm
def run_rasterize(pc, test_set, train_set, args, buf_num=1):
train_loader = torch.utils.data.DataLoader(train_set, batch_size=1)
test_loader = torch.utils.data.DataLoader(test_set, batch_size=1)
z_list = []
id_list = []
for batch in tqdm(test_loader):
pose = batch['w2c'][0]
xyz_ndc = pc.get_ndc(pose)
id, zbuf = rasterize(xyz_ndc, (args.H, args.W), args.radius, buf_num)
z_list.append(zbuf.float().cpu())
id_list.append(id.long().cpu())
# if i % 20 == 0:
# print('test', i)
z_test = torch.cat(z_list, dim=0).numpy()
id_test = torch.cat(id_list, dim=0).numpy()
# print('z_list.shape', z_list.shape)
# np.save(test_z_path, z_list)
# np.save(test_id_path, id_list)
# train set
z_list = []
id_list = []
for batch in tqdm(train_loader):
pose = batch['w2c'][0]
xyz_ndc = pc.get_ndc(pose)
id, zbuf = rasterize(xyz_ndc, (args.H, args.W), args.radius, buf_num)
z_list.append(zbuf.float().cpu())
id_list.append(id.long().cpu())
# if i % 20 == 0:
# print('train', i)
z_train = torch.cat(z_list, dim=0).numpy()
id_train = torch.cat(id_list, dim=0).numpy()
del train_loader, test_loader, z_list, id_list, id, zbuf
return z_test, id_test, z_train, id_train
if __name__ == '__main__':
parser = config_parser()
args = parser.parse_args()
if args.dataset == 'scan':
train_set = ScanDataset(args, 'train', 'rasterize')
test_set = ScanDataset(args, 'test', 'rasterize')
elif args.dataset == 'nerf':
train_set = nerfDataset(args, 'train', 'rasterize')
test_set = nerfDataset(args, 'test', 'rasterize')
elif args.dataset == 'dtu':
train_set = DTUDataset(args, 'train', 'rasterize')
test_set = DTUDataset(args, 'test', 'rasterize')
elif args.dataset == 'tt':
train_set = TTDataset(args, 'train', 'render')
test_set = TTDataset(args, 'test', 'render')
elif args.dataset == 'toy':
train_set = toyDeskDataset(args, 'train', 'render')
test_set = toyDeskDataset(args, 'test', 'render')
else:
assert False
if not os.path.exists(args.frag_path):
os.makedirs(args.frag_path)
test_id_path = str(args.radius) + '-idx-' + str(args.H) + '-test.npy'
test_z_path = str(args.radius) + '-z-' + str(args.H) + '-test.npy'
test_id_path = os.path.join(args.frag_path, test_id_path)
test_z_path = os.path.join(args.frag_path, test_z_path)
train_id_path = str(args.radius) + '-idx-' + str(args.H) + '-train.npy'
train_z_path = str(args.radius) + '-z-' + str(args.H) + '-train.npy'
train_id_path = os.path.join(args.frag_path, train_id_path)
train_z_path = os.path.join(args.frag_path, train_z_path)
# begin = time.time()
pc = train_set.get_pc()
z_test, id_test, z_train, id_train = run_rasterize(pc, test_set, train_set, args)
# test set
# z_list = []
# id_list = []
# for i, batch in enumerate(test_loader):
# pose = batch['w2c'][0]
# xyz_ndc = pc.get_ndc(pose)
# id, zbuf = rasterize(xyz_ndc, (args.H, args.W), args.radius)
# z_list.append(zbuf.float().cpu())
# id_list.append(id.long().cpu())
# if i % 20 == 0:
# print('test', i)
# z_list = torch.cat(z_list, dim=0).numpy()
# id_list = torch.cat(id_list, dim=0).numpy()
# print('z_list.shape', z_list.shape)
np.save(test_z_path, z_test)
np.save(test_id_path, id_test)
# # train set
# z_list = []
# id_list = []
# for i, batch in enumerate(train_loader):
# pose = batch['w2c'][0]
# xyz_ndc = pc.get_ndc(pose)
# id, zbuf = rasterize(xyz_ndc, (args.H, args.W), args.radius)
# z_list.append(zbuf.float().cpu())
# id_list.append(id.long().cpu())
# if i % 20 == 0:
# print('train', i)
# z_list = torch.cat(z_list, dim=0).numpy()
# id_list = torch.cat(id_list, dim=0).numpy()
# print('z_list.shape', z_list.shape)
np.save(train_z_path, z_train)
np.save(train_id_path, id_train)