-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdata_prep.py
150 lines (129 loc) · 5.23 KB
/
data_prep.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
import os
import sys
import numpy as np
import copy
import math
import scipy.io as sio
import scipy.misc as smc
import glob
import scipy.ndimage
class FlowDataset():
def __init__(self, cachepath, npoint=512, nmask=10):
self.cachepath = cachepath
self.npoint = npoint
self.nmask = nmask
self.data = sio.loadmat(cachepath)
def generate_3d(self):
"""Generate a 3D random rotation matrix.
Returns:
np.matrix: A 3D rotation matrix.
"""
x1, x2, x3 = np.random.rand(3)
R = np.matrix([[np.cos(2 * np.pi * x1), np.sin(2 * np.pi * x1), 0],
[-np.sin(2 * np.pi * x1), np.cos(2 * np.pi * x1), 0],
[0, 0, 1]])
v = np.matrix([[np.cos(2 * np.pi * x2) * np.sqrt(x3)],
[np.sin(2 * np.pi * x2) * np.sqrt(x3)],
[np.sqrt(1 - x3)]])
H = np.eye(3) - 2 * v * v.T
M = -H * R
return M
def __getitem__(self, index):
pc1 = copy.deepcopy(self.data['pc1'][index])
pc2 = copy.deepcopy(self.data['pc2_partial'][index])
flow12 = copy.deepcopy(self.data['pc2'][index]-self.data['pc1'][index])
vismask = copy.deepcopy(self.data['vismask'][index])
permidx = np.random.permutation(pc1.shape[0])[:self.npoint]
pc1 = pc1[permidx,:]
flow12 = flow12[permidx,:]
vismask = vismask[permidx]
permidx2 = np.random.permutation(pc2.shape[0])[:self.npoint]
pc2 = pc2[permidx2,:]
# apply global motion
R1 = self.generate_3d()
R2 = np.eye(3)
flow12 = np.matmul(np.matmul(pc1,R1),R2-np.eye(3))+np.matmul(np.matmul(flow12,R1),R2)
pc1 = np.matmul(pc1,R1)
pc2 = np.matmul(np.matmul(pc2,R1),R2)
momasks = np.zeros((self.npoint, self.nmask))
return pc1, pc2, flow12, vismask, momasks
def __len__(self):
return self.data['pc1'].shape[0]
class SegDataset():
def __init__(self, cachepath, npoint=512, nmask=10, relrot=True):
self.cachepath = cachepath
self.npoint = npoint
self.nmask = nmask
self.relrot = relrot
self.data = sio.loadmat(cachepath)
def generate_3d(self):
"""Generate a 3D random rotation matrix.
Returns:
np.matrix: A 3D rotation matrix.
"""
x1, x2, x3 = np.random.rand(3)
R = np.matrix([[np.cos(2 * np.pi * x1), np.sin(2 * np.pi * x1), 0],
[-np.sin(2 * np.pi * x1), np.cos(2 * np.pi * x1), 0],
[0, 0, 1]])
v = np.matrix([[np.cos(2 * np.pi * x2) * np.sqrt(x3)],
[np.sin(2 * np.pi * x2) * np.sqrt(x3)],
[np.sqrt(1 - x3)]])
H = np.eye(3) - 2 * v * v.T
M = -H * R
return M
def __getitem__(self, index):
pc1 = copy.deepcopy(self.data['pc1'][index])
pc2 = copy.deepcopy(self.data['pc2'][index])
flow12 = copy.deepcopy(self.data['flow12'][index])
momasks = copy.deepcopy(self.data['momasks'][index])
permidx = np.random.permutation(pc1.shape[0])[:self.npoint]
pc1 = pc1[permidx,:]
flow12 = flow12[permidx,:]
momasks = np.eye(self.nmask)[np.minimum(momasks,self.nmask-1)[permidx].astype('int32')]
permidx2 = np.random.permutation(pc2.shape[0])[:self.npoint]
pc2 = pc2[permidx2,:]
# global transform
R0 = self.generate_3d()
pc1 = np.matmul(pc1,R0)
pc2 = np.matmul(pc2,R0)
flow12 = np.matmul(flow12,R0)
if self.relrot:
# relative transform
R1 = 0.5*self.generate_3d()+np.eye(3)
u1, _, vh1 = np.linalg.svd(R1, full_matrices=True)
R1 = np.matmul(u1,vh1)
flow12 = pc1+flow12-np.matmul(pc1,R1)
pc1 = np.matmul(pc1,R1)
vismask = np.zeros(self.npoint)
return pc1, pc2, flow12, vismask, momasks
def __len__(self):
return self.data['pc1'].shape[0]
class SynTestDataset():
def __init__(self, cachepath, npoint=512):
self.cachepath = cachepath
self.npoint = npoint
self.data = sio.loadmat(cachepath)
def __getitem__(self, index):
pc1 = copy.deepcopy(self.data['pc1'][index])
pc2 = copy.deepcopy(self.data['pc2'][index])
flow12 = copy.deepcopy(self.data['flow12'][index])
seg1 = copy.deepcopy(self.data['seg1'][index]).astype('int32')-1
if self.npoint<pc1.shape[0]:
permidx = np.random.permutation(pc1.shape[0])[:self.npoint]
pc1 = pc1[permidx,:]
flow12 = flow12[permidx,:]
seg1 = seg1[permidx]
permidx2 = np.random.permutation(pc2.shape[0])[:self.npoint]
pc2 = pc2[permidx2,:]
return pc1, pc2, flow12, seg1
def __len__(self):
return self.data['pc1'].shape[0]
if __name__=='__main__':
D = FlowDataset(cachepath='data/flow_validation.mat', npoint=512)
print(len(D))
pc1, pc2, flow12, vismask, momasks = D[0]
print(pc1.shape, pc2.shape, flow12.shape, vismask.shape, momasks.shape)
D = SegDataset(cachepath='data/seg_validation.mat')
print(len(D))
pc1, pc2, flow12, vismask, momasks = D[0]
print(pc1.shape, pc2.shape, flow12.shape, vismask.shape, momasks.shape)