-
Notifications
You must be signed in to change notification settings - Fork 62
/
loader.py
94 lines (74 loc) · 2.29 KB
/
loader.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
import numpy as np
import skimage
from scipy.io import loadmat
from skimage.io import imread
from skimage.transform import resize
def read_im(f):
im = skimage.img_as_float(imread(f))
if im.ndim == 2:
im = np.stack([im, im, im], axis=2)
if im.shape[-1] == 4:
alpha = np.expand_dims(im[..., 3], 2)
im = im[..., :3] * alpha + (1 - alpha)
return im[..., :3]
def read_depth(f):
im = skimage.img_as_float(imread(f)) * 10
if im.ndim == 2:
im = np.expand_dims(im, 2)
return im
def read_camera(f):
cam = loadmat(f)
Rt = cam['extrinsic'][:3]
K = cam['K']
return K, Rt
def read_quat(f):
cam = loadmat(f)
q = cam['quat'].ravel()
return q
def read_vol(f, tsdf=False):
def get_vox(f):
try:
data = loadmat(f, squeeze_me=True)
except:
print('Error reading {:s}'.format(f))
return None
vol = np.transpose(data['Volume'].astype(np.bool), [0, 2, 1])
vol = vol[:, ::-1, :]
return vol
def get_tsdf(f, trunc=0.2):
try:
data = loadmat(f, squeeze_me=True)
except:
print('Error reading {:s}'.format(f))
return None
tsdf = data['tsdf']
tsdf[tsdf < -trunc] = -trunc
tsdf[tsdf > trunc] = trunc
tsdf = np.transpose(tsdf, [0, 2, 1])
tsdf = tsdf[:, ::-1, :]
return tsdf
load_func = get_tsdf if tsdf else get_vox
vol = load_func(f).astype(np.float32)
vol = vol[..., np.newaxis]
return vol
def pad_batch(batch_data, bs):
for k, v in batch_data.iteritems():
n = v.shape[0]
to_pad = bs - n
if to_pad == 0:
continue
pad = np.stack([v[0, ...]] * to_pad, axis=0)
batch_data[k] = np.concatenate([v, pad], axis=0)
return batch_data
def subsample_grid(grids, sub_ratio):
def subsample(g):
ss = np.array(g.shape) / sub_ratio
sub_grid = np.zeros(ss, dtype=np.bool)
for ix in range(sub_ratio):
for jx in range(sub_ratio):
for kx in range(sub_ratio):
sub_grid = np.logical_or(
sub_grid,
g[ix::sub_ratio, jx::sub_ratio, kx::sub_ratio])
return sub_grid
return [subsample(g) for g in grids]