-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathslf_bake.py
More file actions
145 lines (123 loc) · 5.12 KB
/
Copy pathslf_bake.py
File metadata and controls
145 lines (123 loc) · 5.12 KB
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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
import torch
import mitsuba
mitsuba.set_variant('cuda_ad_rgb')
import os
os.environ["OPENCV_IO_ENABLE_OPENEXR"]="1"
from utils.dataset import SyntheticDatasetLDR,RealDatasetLDR
from utils.dataset.scannetpp.dataset import Scannetpp
from utils.path_tracing import ray_intersect
from model.slf import VoxelSLF
from crf.model_crf import EmorCRF
from pathlib import Path
from tqdm import tqdm
from argparse import ArgumentParser
import time
from const import set_random_seed
set_random_seed()
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('--dataset_root', type=str, help='dataset root')
parser.add_argument('--scene', type=str, required=True, help='dataset folder')
parser.add_argument('--output', type=str, required=True, help='output path')
parser.add_argument('--dataset', type=str,required=True, help='dataset type')
parser.add_argument('--voxel_num', type=int,default=256, help='resolution for voxel radiance cache')
parser.add_argument('--ldr_img_dir', type=str, default=None)
parser.add_argument('--res_scale', type=float, default=1.0)
args = parser.parse_args()
device = torch.device(0) # use gpu device 0
DATASET_PATH = args.scene
OUTPUT_PATH = args.output
os.makedirs(OUTPUT_PATH,exist_ok=True)
# load mesh
if args.dataset in ['synthetic', 'real']:
mesh_path = os.path.join(DATASET_PATH,'scene.obj')
mesh_type = 'obj'
elif args.dataset == 'scannetpp':
mesh_path = os.path.join(args.dataset_root, 'data', args.scene, 'scans', 'scene.ply')
mesh_type = 'ply'
assert Path(mesh_path).exists(), 'mesh not found: '+mesh_path
scene = mitsuba.load_dict({
'type': 'scene',
'shape_id':{
'type': mesh_type,
'filename': mesh_path,
}
})
# load dataset
if args.dataset == 'synthetic':
dataset = SyntheticDatasetLDR(DATASET_PATH, img_dir=args.ldr_img_dir, split='train', pixel=False)
elif args.dataset == 'real':
dataset = RealDatasetLDR(DATASET_PATH, img_dir=args.ldr_img_dir, split='train', pixel=False)
elif args.dataset == 'scannetpp':
dataset = Scannetpp(args.dataset_root, args.scene, split='train', pixel=False, res_scale=args.res_scale)
img_hw = dataset.img_hw
model_crf = EmorCRF()
start_time = time.time()
# extract scene bounding box
print('find scene bound')
voxel_min = 1000.
voxel_max = 0.0
for idx in tqdm(range(len(dataset))):
batch = dataset[idx]
rays = batch['rays']
xs = rays[...,:3]
ds = rays[...,3:6]
positions,_,_,_,valid = ray_intersect(scene,xs.to(device),ds.to(device))
if not valid.any():
continue
position = positions[valid]
voxel_min = min(voxel_min,position.min())
voxel_max = max(voxel_max,position.max())
if args.dataset in ['synthetic', 'real']:
voxel_min = 1.1*voxel_min
voxel_max = 1.1*voxel_max
else:
voxel_c = (voxel_min + voxel_max)
voxel_min = voxel_c + (voxel_min - voxel_c) * 1.1
voxel_max = voxel_c + (voxel_max - voxel_c) * 1.1
# find voxels that are not occupied
print('find visible voxels')
res_spatial = args.voxel_num
SpatialHist = torch.zeros(res_spatial**3,device=device)
for idx in tqdm(range(len(dataset))):
batch = dataset[idx]
rays = batch['rays']
xs = rays[...,:3]
ds = rays[...,3:6]
positions,_,_,_,valid = ray_intersect(scene,xs.to(device),ds.to(device))
if not valid.any():
continue
position = (positions[valid]-voxel_min)/(voxel_max-voxel_min)
position = (position*res_spatial).long().clamp(0,res_spatial-1)
inds = position[...,0] + position[...,1]*res_spatial\
+ position[...,2]*res_spatial*res_spatial
SpatialHist.scatter_add_(0,inds,torch.ones_like(inds).float())
SpatialHist = SpatialHist.reshape(res_spatial,res_spatial,res_spatial)
mask = (SpatialHist>0)
# create voxle surface light field
print('bake voxel surface light field')
vslf = VoxelSLF(mask.cpu(),voxel_min.item(),voxel_max.item())
for idx in tqdm(range(len(dataset))):
batch = dataset[idx]
rays = batch['rays']
radiance = batch['rgbs']
xs = rays[...,:3]
ds = rays[...,3:6]
exposure = batch['exposure']
radiance = model_crf.inverse(radiance, exposure)
positions,_,_,_,valid = ray_intersect(scene,xs.to(device),ds.to(device))
if not valid.any():
continue
vslf.scatter_add(positions[valid].cpu(),radiance.to(device)[valid].cpu())
# average pooling the radiance
vslf.radiance = vslf.radiance/vslf.count[...,None].float().clamp_min(1)
torch.save({
'mask': (SpatialHist>0),
'voxel_min': voxel_min.item(),
'voxel_max': voxel_max.item(),
'weight':vslf.state_dict()
},os.path.join(OUTPUT_PATH,'vslf.npz'))