-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphototourism.py
353 lines (307 loc) · 17.2 KB
/
phototourism.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import torch
from torch.utils.data import Dataset
import glob
import numpy as np
import os
import pandas as pd
import pickle
from PIL import Image
from torchvision import transforms as T
from .ray_utils import *
from .colmap_utils import \
read_cameras_binary, read_images_binary, read_points3d_binary
class PhototourismDataset(Dataset):
def __init__(self, root_dir, split='train', img_downscale=1, val_num=1, use_cache=False, uw_model=False,ndc = False):
"""
img_downscale: how much scale to downsample the training images.
The original image sizes are around 500~100, so value of 1 or 2
are recommended.
ATTENTION! Value of 1 will consume large CPU memory,
about 40G for brandenburg gate.
val_num: number of val images (used for multigpu, validate same image for all gpus)
use_cache: during data preparation, use precomputed rays (useful to accelerate
data loading, especially for multigpu!)
"""
self.root_dir = root_dir
self.split = split
assert img_downscale >= 1, 'image can only be downsampled, please set img_downscale>=1!'
self.img_downscale = img_downscale
if split == 'val': # image downscale=1 will cause OOM in val mode
self.img_downscale = max(2, self.img_downscale)
self.val_num = max(1, val_num) # at least 1
self.use_cache = use_cache
self.define_transforms()
self.ndc = ndc
self.read_meta()
self.white_back = False
self.uw_model = uw_model
def read_meta(self):
# read all files in the tsv first (split to train and test later)
tsv = glob.glob(os.path.join(self.root_dir, '*.tsv'))[0]
self.scene_name = os.path.basename(tsv)[:-4]
self.files = pd.read_csv(tsv, sep='\t')
self.files = self.files[~self.files['id'].isnull()] # remove data without id
self.files.reset_index(inplace=True, drop=True)
# Step 1. load image paths
# Attention! The 'id' column in the tsv is BROKEN, don't use it!!!!
# Instead, read the id from images.bin using image file name!
if self.use_cache:
with open(os.path.join(self.root_dir, f'cache/img_ids.pkl'), 'rb') as f:
self.img_ids = pickle.load(f)
with open(os.path.join(self.root_dir, f'cache/image_paths.pkl'), 'rb') as f:
self.image_paths = pickle.load(f)
else:
imdata = read_images_binary(os.path.join(self.root_dir, 'dense/sparse/images.bin'))
img_path_to_id = {}
for v in imdata.values():
img_path_to_id[v.name] = v.id
self.img_ids = []
self.image_paths = {} # {id: filename}
for filename in list(self.files['filename']):
id_ = img_path_to_id[filename]
self.image_paths[id_] = filename
self.img_ids += [id_]
# Step 2: read and rescale camera intrinsics
if self.use_cache:
with open(os.path.join(self.root_dir, f'cache/Ks{self.img_downscale}.pkl'), 'rb') as f:
self.Ks = pickle.load(f)
else:
self.Ks = {} # {id: K}
camdata = read_cameras_binary(os.path.join(self.root_dir, 'dense/sparse/cameras.bin'))
for id_ in self.img_ids:
K = np.zeros((3, 3), dtype=np.float32)
cam = camdata[1]
# img_w, img_h = int(cam.params[2]*2), int(cam.params[3]*2)
img_w, img_h = int(cam.width), int(cam.height)
img_w_, img_h_ = img_w//self.img_downscale, img_h//self.img_downscale
K[0, 0] = cam.params[0]*img_w_/img_w # fx
K[1, 1] = cam.params[1]*img_h_/img_h # fy
K[0, 2] = cam.params[2]*img_w_/img_w # cx
K[1, 2] = cam.params[3]*img_h_/img_h # cy
K[2, 2] = 1
self.Ks[id_] = K
# Step 3: read c2w poses (of the images in tsv file only) and correct the order
if self.use_cache:
self.poses = np.load(os.path.join(self.root_dir, 'cache/poses.npy'))
else:
w2c_mats = []
bottom = np.array([0, 0, 0, 1.]).reshape(1, 4)
for id_ in self.img_ids:
im = imdata[id_]
R = im.qvec2rotmat()
t = im.tvec.reshape(3, 1)
w2c_mats += [np.concatenate([np.concatenate([R, t], 1), bottom], 0)]
w2c_mats = np.stack(w2c_mats, 0) # (N_images, 4, 4)
self.poses = np.linalg.inv(w2c_mats)[:, :3] # (N_images, 3, 4)
# Original poses has rotation in form "right down front", change to "right up back"
self.poses[..., 1:3] *= -1
# Step 4: correct scale
if self.use_cache:
self.xyz_world = np.load(os.path.join(self.root_dir, 'cache/xyz_world.npy'))
with open(os.path.join(self.root_dir, f'cache/nears.pkl'), 'rb') as f:
self.nears = pickle.load(f)
with open(os.path.join(self.root_dir, f'cache/fars.pkl'), 'rb') as f:
self.fars = pickle.load(f)
else:
pts3d = read_points3d_binary(os.path.join(self.root_dir, 'dense/sparse/points3D.bin'))
self.xyz_world = np.array([pts3d[p_id].xyz for p_id in pts3d])
xyz_world_h = np.concatenate([self.xyz_world, np.ones((len(self.xyz_world), 1))], -1)
# Compute near and far bounds for each image individually
self.nears, self.fars = {}, {} # {id_: distance}
for i, id_ in enumerate(self.img_ids):
xyz_cam_i = (xyz_world_h @ w2c_mats[i].T)[:, :3] # xyz in the ith cam coordinate
xyz_cam_i = xyz_cam_i[xyz_cam_i[:, 2]>0] # filter out points that lie behind the cam
self.nears[id_] = np.percentile(xyz_cam_i[:, 2], 0.1)
self.fars[id_] = np.percentile(xyz_cam_i[:, 2], 99.9)
max_far = np.fromiter(self.fars.values(), np.float32).max()
scale_factor = max_far/5 # so that the max far is scaled to 5
self.poses[..., 3] /= scale_factor
for k in self.nears:
self.nears[k] /= scale_factor
for k in self.fars:
self.fars[k] /= scale_factor
self.xyz_world /= scale_factor
self.poses_dict = {id_: self.poses[i] for i, id_ in enumerate(self.img_ids)}
# Step 5. split the img_ids (the number of images is verfied to match that in the paper)
self.img_ids_train = [id_ for i, id_ in enumerate(self.img_ids)
if self.files.loc[i, 'split']=='train']
self.img_ids_test = [id_ for i, id_ in enumerate(self.img_ids)
if self.files.loc[i, 'split']=='test']
self.N_images_train = len(self.img_ids_train)
self.N_images_test = len(self.img_ids_test)
if self.split == 'train': # create buffer of all rays and rgb data
if self.use_cache:
all_rays = np.load(os.path.join(self.root_dir,
f'cache/rays{self.img_downscale}.npy'))
self.all_rays = torch.from_numpy(all_rays)
all_rgbs = np.load(os.path.join(self.root_dir,
f'cache/rgbs{self.img_downscale}.npy'))
self.all_rgbs = torch.from_numpy(all_rgbs)
else:
self.all_rays = []
self.all_rgbs = []
for id_ in self.img_ids_train:
c2w = torch.FloatTensor(self.poses_dict[id_])
img = Image.open(os.path.join(self.root_dir, 'dense/images',
self.image_paths[id_])).convert('RGB')
img_w, img_h = img.size
if self.img_downscale > 1:
img_w = img_w//self.img_downscale
img_h = img_h//self.img_downscale
img = img.resize((img_w, img_h), Image.LANCZOS)
img = self.transform(img) # (3, h, w)
img = img.view(3, -1).permute(1, 0) # (h*w, 3) RGB
self.all_rgbs += [img]
directions = get_ray_directions(img_h, img_w, self.Ks[id_])
rays_o, rays_d = get_rays(directions, c2w,self.ndc)
if self.ndc:
focal =(np.sqrt(self.Ks[id_][0,0]**2+self.Ks[id_][1,1]**2))
rays_o, rays_d = get_ndc_rays(img_h, img_w, focal,1, rays_o, rays_d)
# rays_d = rays_d / torch.norm(rays_d, dim=-1, keepdim=True)
rays_t = id_ * torch.ones(len(rays_o), 1)
self.all_rays += [torch.cat([rays_o, rays_d,
self.nears[id_]*torch.ones_like(rays_o[:, :1]),
self.fars[id_]*torch.ones_like(rays_o[:, :1]),
rays_t],
1)] # (h*w, 8)
self.all_rays = torch.cat(self.all_rays, 0) # ((N_images-1)*h*w, 8)
self.all_rgbs = torch.cat(self.all_rgbs, 0) # ((N_images-1)*h*w, 3)
elif self.split == 'test':
if self.use_cache:
all_rays = np.load(os.path.join(self.root_dir,
f'cache/rays{self.img_downscale}.npy'))
self.all_rays = torch.from_numpy(all_rays)
all_rgbs = np.load(os.path.join(self.root_dir,
f'cache/rgbs{self.img_downscale}.npy'))
self.all_rgbs = torch.from_numpy(all_rgbs)
else:
self.all_rays = []
self.all_rgbs = []
for id_ in self.img_ids_test:
c2w = torch.FloatTensor(self.poses_dict[id_])
img = Image.open(os.path.join(self.root_dir, 'dense/images',
self.image_paths[id_])).convert('RGB')
img_w, img_h = img.size
if self.img_downscale > 1:
img_w = img_w // self.img_downscale
img_h = img_h // self.img_downscale
img = img.resize((img_w, img_h), Image.LANCZOS)
img = self.transform(img) # (3, h, w)
img = img.view(3, -1).permute(1, 0) # (h*w, 3) RGB
self.all_rgbs += [img]
directions = get_ray_directions(img_h, img_w, self.Ks[id_])
rays_o, rays_d = get_rays(directions, c2w,self.ndc)
if self.ndc:
focal =(np.sqrt(self.Ks[id_][0,0]**2+self.Ks[id_][1,1]**2))
rays_o, rays_d = get_ndc_rays(img_h, img_w, focal,1, rays_o, rays_d)
# rays_d = rays_d / torch.norm(rays_d, dim=-1, keepdim=True)
rays_t = id_ * torch.ones(len(rays_o), 1)
self.all_rays += [torch.cat([rays_o, rays_d,
self.nears[id_] * torch.ones_like(rays_o[:, :1]),
self.fars[id_] * torch.ones_like(rays_o[:, :1]),
rays_t],
1)] # (h*w, 8)
self.all_rays = torch.cat(self.all_rays, 0) # ((N_images-1)*h*w, 8)
self.all_rgbs = torch.cat(self.all_rgbs, 0) # ((N_images-1)*h*w, 3)
elif self.split in ['val', 'test_train']: # use the first image as val image (also in train)
self.val_id = self.img_ids_train[0]
# else: # for testing, create a parametric rendering path
# # test poses and appearance index are defined in eval.py
# pass
def define_transforms(self):
self.transform = T.ToTensor()
def __len__(self):
if self.split == 'train':
return len(self.all_rays)
if self.split == 'test':
return self.N_images_test
if self.split == 'test_train':
return self.N_images_train
if self.split == 'val':
return self.val_num
return len(self.poses_test)
def __getitem__(self, idx):
if self.split == 'train': # use data in the buffers
sample = {'rays': self.all_rays[idx, :8],
'ts': self.all_rays[idx, 8].long(), # torch.zeros_like(self.all_rays[idx, 8].long()) if self.uw_model else self.all_rays[idx, 8].long() ,
'rgbs': self.all_rgbs[idx]}
elif self.split =='test':
sample = {}
id_ = self.img_ids_test[idx]
sample['c2w'] = c2w = torch.FloatTensor(self.poses_dict[id_])
img = Image.open(os.path.join(self.root_dir, 'dense/images',
self.image_paths[id_])).convert('RGB')
img_w, img_h = img.size
if self.img_downscale > 1:
img_w = img_w//self.img_downscale
img_h = img_h//self.img_downscale
img = img.resize((img_w, img_h), Image.LANCZOS)
img = self.transform(img) # (3, h, w)
img = img.view(3, -1).permute(1, 0) # (h*w, 3) RGB
sample['rgbs'] = img
directions = get_ray_directions(img_h, img_w, self.Ks[id_])
rays_o, rays_d = get_rays(directions, c2w,self.ndc)
if self.ndc:
focal = (np.sqrt(self.Ks[id_][0, 0] ** 2 + self.Ks[id_][1, 1] ** 2))
rays_o, rays_d = get_ndc_rays(img_h, img_w, focal, 1, rays_o, rays_d)
rays = torch.cat([rays_o, rays_d,
self.nears[id_]*torch.ones_like(rays_o[:, :1]),
self.fars[id_]*torch.ones_like(rays_o[:, :1])],
1) # (h*w, 8)
sample['rays'] = rays
if self.uw_model: #TODO: uncomment for global parametrs
sample['ts'] = id_ * torch.ones(len(rays), dtype=torch.long)#torch.zeros(len(rays), dtype=torch.long)# #torch.zeros(len(rays), dtype=torch.long)
else:
sample['ts'] = id_ * torch.ones(len(rays), dtype=torch.long)
sample['img_wh'] = torch.LongTensor([img_w, img_h])
elif self.split in ['val', 'test_train']:
sample = {}
if self.split == 'val':
id_ = self.val_id
else:
id_ = self.img_ids_train[idx]
sample['c2w'] = c2w = torch.FloatTensor(self.poses_dict[id_])
img = Image.open(os.path.join(self.root_dir, 'dense/images',
self.image_paths[id_])).convert('RGB')
img_w, img_h = img.size
if self.img_downscale > 1:
img_w = img_w//self.img_downscale
img_h = img_h//self.img_downscale
img = img.resize((img_w, img_h), Image.LANCZOS)
img = self.transform(img) # (3, h, w)
img = img.view(3, -1).permute(1, 0) # (h*w, 3) RGB
sample['rgbs'] = img
directions = get_ray_directions(img_h, img_w, self.Ks[id_])
rays_o, rays_d = get_rays(directions, c2w,self.ndc)
if self.ndc:
focal = (np.sqrt(self.Ks[id_][0, 0] ** 2 + self.Ks[id_][1, 1] ** 2))
rays_o, rays_d = get_ndc_rays(img_h, img_w, focal, 1, rays_o, rays_d)
rays = torch.cat([rays_o, rays_d,
self.nears[id_]*torch.ones_like(rays_o[:, :1]),
self.fars[id_]*torch.ones_like(rays_o[:, :1])],
1) # (h*w, 8)
sample['rays'] = rays
if self.uw_model: #TODO: uncomment for global parametrs
sample['ts'] = id_ * torch.ones(len(rays), dtype=torch.long)# torch.zeros(len(rays), dtype=torch.long) #id_ * torch.ones(len(rays), dtype=torch.long) #
else:
sample['ts'] = id_ * torch.ones(len(rays), dtype=torch.long)
sample['img_wh'] = torch.LongTensor([img_w, img_h])
else:
sample = {}
sample['c2w'] = c2w = torch.FloatTensor(self.poses_test[idx])
directions = get_ray_directions(self.test_img_h, self.test_img_w, self.test_K)
rays_o, rays_d = get_rays(directions, c2w,self.ndc)
if self.ndc:
focal = (np.sqrt(self.test_K[0, 0] ** 2 + self.test_K[1, 1] ** 2))
rays_o, rays_d = get_ndc_rays(self.test_img_h, self.test_img_w, focal, 1, rays_o, rays_d)
near, far = 0, 1
else:
near, far = 0, 5
rays = torch.cat([rays_o, rays_d,
near*torch.ones_like(rays_o[:, :1]),
far*torch.ones_like(rays_o[:, :1])],
1)
sample['rays'] = rays
sample['ts']= self.test_appearance_idx * torch.ones(len(rays), dtype=torch.long) #torch.zeros(len(rays), dtype=torch.long) if self.uw_model else self.test_appearance_idx * torch.ones(len(rays), dtype=torch.long) #TODO: change
sample['img_wh'] = torch.LongTensor([self.test_img_w, self.test_img_h])
return sample