-
Notifications
You must be signed in to change notification settings - Fork 45
/
sequence_folders.py
207 lines (188 loc) · 7.13 KB
/
sequence_folders.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
import numpy
import os
import torch.utils.data as data
import numpy as np
from transforms3d.euler import mat2euler
from scipy.misc import imread
from path import Path
import random
def load_as_float(path):
return imread(path).astype(np.float32)
class SequenceFolder(data.Dataset):
"""A sequence data loader where the files are arranged in this way:
root/scene_1/0000000.jpg
root/scene_1/0000001.jpg
..
root/scene_1/cam.txt
root/scene_2/0000000.jpg
.
transform functions must take in a list a images and a numpy array (usually intrinsics matrix)
"""
def __init__(self, root, seed=None, ttype='train.txt', sequence_length=2, transform=None,
target_transform=None, add_geo=False, depth_source="p", dataset="", gt_source='g',
pose_source='', scale=False, req_angle=False, size=0, req_gt=False, get_path=False):
print(dataset + pose_source)
np.random.seed(seed)
random.seed(seed)
self.root = Path(root)
scene_list_path = self.root / ttype
scenes = [self.root / folder[:-1] for folder in open(scene_list_path) if folder.startswith(dataset)]
# if size > 0:
# scenes = random.sample(scenes, size * sequence_length)
self.size = size
self.pose_source = pose_source
self.ttype = ttype
self.scenes = sorted(scenes)
self.scale = scale
self.transform = transform
self.geo = add_geo
self.gt_source = gt_source
self.avg_scale = 0
self.max_scale = 0
self.counter = 0
self.req_angle = req_angle
self.depth_source = depth_source
self.req_gt = req_gt
self.get_path = get_path
self.crawl_folders(sequence_length)
def crawl_folders(self, sequence_length):
sequence_set = []
demi_length = sequence_length // 2
p_num = 0
g_num = 0
scale_sum = 0
l1counter = 0
for scene in self.scenes:
intrinsics = np.genfromtxt(scene / 'cam.txt').astype(np.float32).reshape((3, 3))
source = False
if self.pose_source and os.path.exists(scene / self.pose_source):
poses = np.genfromtxt(scene / self.pose_source).astype(np.float32)
source = True
imgs = sorted(scene.files('*.jpg'))
if len(imgs) >= 20:
print(scene)
else:
poses = np.genfromtxt(scene / 'poses.txt').astype(np.float32)
if self.req_gt:
poses_gt = np.genfromtxt(scene / 'poses.txt').astype(np.float32)
imgs = sorted(scene.files('*.jpg'))
# print(len(imgs))
if len(imgs) < sequence_length:
continue
for i in range(len(imgs)):
if i < demi_length:
shifts = list(range(0, sequence_length))
shifts.pop(i)
elif i >= len(imgs) - demi_length:
shifts = list(range(len(imgs) - sequence_length, len(imgs)))
shifts.pop(i - len(imgs))
else:
shifts = list(range(i - demi_length, i + (sequence_length + 1) // 2))
shifts.pop(demi_length)
img = imgs[i]
depth = img.dirname() / img.name[:-4] + '.npy'
if self.gt_source == 'p':
depth = img.dirname() / img.name[:-4] + '_p.npy'
pose_tgt = np.concatenate((poses[i, :].reshape((3, 4)), np.array([[0, 0, 0, 1]])), axis=0)
if self.req_gt:
pose_tgt_gt = np.concatenate((poses_gt[i, :].reshape((3, 4)), np.array([[0, 0, 0, 1]])), axis=0)
sample = {'intrinsics': intrinsics, 'tgt': img, 'tgt_depth': depth, 'ref_imgs': [],
'ref_poses': [], 'ref_poses_gt': [], 'ref_depths': [], 'scale': 1.0, 'gt_angle': [],
'source': source}
for j in shifts:
sample['ref_imgs'].append(imgs[j])
if self.geo:
if self.depth_source == 'g':
sample['ref_depths'].append(imgs[j].dirname() / imgs[j].name[:-4] + '.npy')
elif self.depth_source == 'p':
path = imgs[j].dirname() / imgs[j].name[:-4] + '_p.npy'
if (os.path.exists(path)):
sample['ref_depths'].append(path)
p_num += 1
else:
sample['ref_depths'].append(imgs[j].dirname() / imgs[j].name[:-4] + '.npy')
g_num += 1
else:
path = imgs[j].dirname() / imgs[j].name[:-4] + '_' + self.depth_source + '.npy'
if (os.path.exists(path)):
sample['ref_depths'].append(path)
p_num += 1
else:
sample['ref_depths'].append(imgs[j].dirname() / imgs[j].name[:-4] + '.npy')
g_num += 1
pose_src = np.concatenate((poses[j, :].reshape((3, 4)), np.array([[0, 0, 0, 1]])), axis=0)
pose_rel = pose_src @ np.linalg.inv(pose_tgt)
if self.req_gt:
pose_src_gt = np.concatenate((poses_gt[j, :].reshape((3, 4)), np.array([[0, 0, 0, 1]])), axis=0)
pose_rel_gt = pose_src_gt @ np.linalg.inv(pose_tgt_gt)
if self.req_angle:
angle = mat2euler(pose_rel[:3, :3])
sample['gt_angle'] = angle
pose = pose_rel[:3, :].reshape((1, 3, 4)).astype(np.float32)
if self.req_gt:
pose_gt = pose_rel_gt[:3, :].reshape((1, 3, 4)).astype(np.float32)
sample['ref_poses_gt'].append(pose_gt)
if self.scale:
self.counter = self.counter + 1
scale = (pose[0, 0, 3] ** 2 + pose[0, 1, 3] ** 2 + pose[0, 2, 3] ** 2) ** 0.5
scale_sum += scale
self.avg_scale = scale_sum / self.counter
self.max_scale = max(self.max_scale, scale)
sample['scale'] = scale
pose[0, 0, 3] /= scale
pose[0, 1, 3] /= scale
pose[0, 2, 3] /= scale
if scale < 0.5:
l1counter += 1
sample['ref_poses'].append(pose)
sequence_set.append(sample)
if self.size > 0:
sequence_set = random.sample(sequence_set, self.size)
if self.ttype == 'train.txt':
random.shuffle(sequence_set)
print("pn:", p_num, " gn:", g_num)
self.samples = [sq for sq in sequence_set if str(sq['tgt']).split('/')[3].startswith('')]
def __getitem__(self, index):
sample = self.samples[index]
tgt_img = load_as_float(sample['tgt'])
tgt_depth = np.load(sample['tgt_depth'])
if not sample["source"]:
print("warning")
nanmask = tgt_depth != tgt_depth
num = np.sum(nanmask)
if num != 0:
print('tgt depth nan')
tgt_depth[nanmask] = 1
tgt_depth = tgt_depth / sample['scale']
ref_depths = []
for path in sample['ref_depths']:
ref_depth = np.load(path)
nanmask = ref_depth != ref_depth
num = np.sum(nanmask)
if (num != 0):
print('ref depth nan')
ref_depth[nanmask] = 1
ref_depth = ref_depth / sample['scale']
ref_depths.append(ref_depth)
ref_imgs = [load_as_float(ref_img) for ref_img in sample['ref_imgs']]
ref_poses = sample['ref_poses']
if self.transform is not None:
imgs, depths, intrinsics = self.transform([tgt_img] + ref_imgs, [tgt_depth] + ref_depths,
np.copy(sample['intrinsics']))
tgt_img = imgs[0]
tgt_depth = depths[0]
ref_imgs = imgs[1:]
ref_depths = depths[1:]
else:
intrinsics = np.copy(sample['intrinsics'])
if self.get_path:
return tgt_img, ref_imgs, ref_poses, sample['ref_poses_gt'], intrinsics, np.linalg.inv(
intrinsics), tgt_depth, ref_depths, sample['tgt']
if self.req_angle:
return tgt_img, ref_imgs, ref_poses, np.array([a for a in sample['gt_angle']])
if self.req_gt:
return tgt_img, ref_imgs, ref_poses, sample['ref_poses_gt'], intrinsics, np.linalg.inv(
intrinsics), tgt_depth, ref_depths
return tgt_img, ref_imgs, ref_poses, intrinsics, np.linalg.inv(intrinsics), tgt_depth, ref_depths
def __len__(self):
return len(self.samples)