-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathblender.py
185 lines (150 loc) · 7.72 KB
/
blender.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
import torch
from torch.utils.data import Dataset
import json
import numpy as np
import os
from PIL import Image
from torchvision import transforms as T
from .ray_utils import *
class BlenderDataset(Dataset):
def __init__(self, args, split='train', load_ref=False):
self.args = args
self.root_dir = args.datadir
self.split = split
downsample = args.imgScale_train if split=='train' else args.imgScale_test
assert int(800*downsample)%32 == 0, \
f'image width is {int(800*downsample)}, it should be divisible by 32, you may need to modify the imgScale'
self.img_wh = (int(800*downsample),int(800*downsample))
self.define_transforms()
self.blender2opencv = np.array([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]])
if not load_ref:
self.read_meta()
self.white_back = True
def read_meta(self):
with open(os.path.join(self.root_dir, f"transforms_train.json"), 'r') as f:
self.meta = json.load(f)
# sub select training views from pairing file
if os.path.exists('configs/pairs.th'):
name = os.path.basename(self.root_dir)
self.img_idx = torch.load('configs/pairs.th')[f'{name}_{self.split}']
self.meta['frames'] = [self.meta['frames'][idx] for idx in self.img_idx]
print(f'===> {self.split}ing index: {self.img_idx}')
w, h = self.img_wh
self.focal = 0.5 * 800 / np.tan(0.5 * self.meta['camera_angle_x']) # original focal length
self.focal *= self.img_wh[0] / 800 # modify focal length to match size self.img_wh
# bounds, common for all scenes
self.near = 2.0
self.far = 6.0
self.bounds = np.array([self.near, self.far])
# ray directions for all pixels, same for all images (same H, W, focal)
self.directions = get_ray_directions(h, w, [self.focal,self.focal]) # (h, w, 3)
self.image_paths = []
self.poses = []
self.all_rays = []
self.all_rgbs = []
self.all_masks = []
for frame in self.meta['frames']:
pose = np.array(frame['transform_matrix']) @ self.blender2opencv
self.poses += [pose]
c2w = torch.FloatTensor(pose)
image_path = os.path.join(self.root_dir, f"{frame['file_path']}.png")
self.image_paths += [image_path]
img = Image.open(image_path)
img = img.resize(self.img_wh, Image.LANCZOS)
img = self.transform(img) # (4, h, w)
img = img.view(4, -1).permute(1, 0) # (h*w, 4) RGBA
self.all_masks += [img[:,-1:]>0]
img = img[:, :3] * img[:, -1:] + (1 - img[:, -1:]) # blend A to RGB
self.all_rgbs += [img]
rays_o, rays_d = get_rays(self.directions, c2w) # both (h*w, 3)
self.all_rays += [torch.cat([rays_o, rays_d,
self.near * torch.ones_like(rays_o[:, :1]),
self.far * torch.ones_like(rays_o[:, :1])],
1)] # (h*w, 8)
self.all_masks += []
self.poses = np.stack(self.poses)
if 'train' == self.split:
self.all_rays = torch.cat(self.all_rays, 0) # (len(self.meta['frames])*h*w, 3)
self.all_rgbs = torch.cat(self.all_rgbs, 0) # (len(self.meta['frames])*h*w, 3)
else:
self.all_rays = torch.stack(self.all_rays, 0) # (len(self.meta['frames]),h*w, 3)
self.all_rgbs = torch.stack(self.all_rgbs, 0).reshape(-1,*self.img_wh[::-1], 3) # (len(self.meta['frames]),h,w,3)
self.all_masks = torch.stack(self.all_masks, 0).reshape(-1,*self.img_wh[::-1]) # (len(self.meta['frames]),h,w,3)
def read_source_views(self, file=f"transforms_train.json", pair_idx=None, device=torch.device("cpu")):
with open(os.path.join(self.root_dir, file), 'r') as f:
meta = json.load(f)
w, h = self.img_wh
focal = 0.5 * 800 / np.tan(0.5 * meta['camera_angle_x']) # original focal length
focal *= self.img_wh[0] / 800 # modify focal length to match size self.img_wh
src_transform = T.Compose([
T.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
])
# if do not specify source views, load index from pairing file
if pair_idx is None:
name = os.path.basename(self.root_dir)
pair_idx = torch.load('configs/pairs.th')[f'{name}_train'][:3]
print(f'====> ref idx: {pair_idx}')
imgs, proj_mats = [], []
intrinsics, c2ws, w2cs = [],[],[]
for i,idx in enumerate(pair_idx):
frame = meta['frames'][idx]
c2w = np.array(frame['transform_matrix']) @ self.blender2opencv
w2c = np.linalg.inv(c2w)
c2ws.append(c2w)
w2cs.append(w2c)
# build proj mat from source views to ref view
proj_mat_l = np.eye(4)
intrinsic = np.array([[focal, 0, w / 2], [0, focal, h / 2], [0, 0, 1]])
intrinsics.append(intrinsic.copy())
intrinsic[:2] = intrinsic[:2] / 4 # 4 times downscale in the feature space
proj_mat_l[:3, :4] = intrinsic @ w2c[:3, :4]
if i == 0: # reference view
ref_proj_inv = np.linalg.inv(proj_mat_l)
proj_mats += [np.eye(4)]
else:
proj_mats += [proj_mat_l @ ref_proj_inv]
image_path = os.path.join(self.root_dir, f"{frame['file_path']}.png")
img = Image.open(image_path)
img = img.resize(self.img_wh, Image.LANCZOS)
img = self.transform(img) # (4, h, w)
img = img[:3] * img[-1:] + (1 - img[-1:]) # blend A to RGB
imgs.append(src_transform(img))
pose_source = {}
pose_source['c2ws'] = torch.from_numpy(np.stack(c2ws)).float().to(device)
pose_source['w2cs'] = torch.from_numpy(np.stack(w2cs)).float().to(device)
pose_source['intrinsics'] = torch.from_numpy(np.stack(intrinsics)).float().to(device)
near_far_source = [2.0,6.0]
imgs = torch.stack(imgs).float().unsqueeze(0).to(device)
proj_mats = torch.from_numpy(np.stack(proj_mats)[:,:3]).float().unsqueeze(0).to(device)
return imgs, proj_mats, near_far_source, pose_source
def load_poses_all(self, file=f"transforms_train.json"):
with open(os.path.join(self.root_dir, file), 'r') as f:
meta = json.load(f)
c2ws = []
for i,frame in enumerate(meta['frames']):
c2ws.append(np.array(frame['transform_matrix']) @ self.blender2opencv)
return np.stack(c2ws)
def define_transforms(self):
self.transform = T.ToTensor()
def __len__(self):
if self.split == 'train':
return len(self.all_rays)
return len(self.all_rgbs)
def __getitem__(self, idx):
if self.split == 'train': # use data in the buffers
# view, ray_idx = torch.randint(0,len(self.all_rays),(1,)), torch.randperm(self.all_rays.shape[1])[:self.args.batch_size]
# sample = {'rays': self.all_rays[view,ray_idx],
# 'rgbs': self.all_rgbs[view,ray_idx]}
sample = {'rays': self.all_rays[idx],
'rgbs': self.all_rgbs[idx]}
else: # create data for each image separately
# frame = self.meta['frames'][idx]
# c2w = torch.FloatTensor(frame['transform_matrix']) @ self.blender2opencv
img = self.all_rgbs[idx]
rays = self.all_rays[idx]
mask = self.all_masks[idx] # for quantity evaluation
sample = {'rays': rays,
'rgbs': img,
'mask': mask}
return sample