-
Notifications
You must be signed in to change notification settings - Fork 112
/
decoder.py
280 lines (219 loc) · 9.54 KB
/
decoder.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from src.layers import ResnetBlockFC
from src.common import normalize_coordinate, normalize_3d_coordinate, map2local
class LocalDecoder(nn.Module):
''' Decoder with conditional batch normalization (CBN) class.
Instead of conditioning on global features, on plane local features
Args:
dim (int): input dimension
c_dim (int): dimension of latent conditioned code c
hidden_size (int): hidden size of Decoder network
n_blocks (int): number of blocks ResNetBlockFC layers
leaky (bool): whether to use leaky ReLUs
sample_mode (str): sampling feature strategy, bilinear|nearest
padding (float): conventional padding paramter of ONet for unit cube, so [-0.5, 0.5] -> [-0.55, 0.55]
'''
def __init__(self, dim=3, c_dim=128,
hidden_size=256, n_blocks=5, leaky=False, sample_mode='bilinear', padding=0.1):
super().__init__()
self.c_dim = c_dim
self.n_blocks = n_blocks
if c_dim != 0:
self.fc_c = nn.ModuleList([
nn.Linear(c_dim, hidden_size) for i in range(n_blocks)
])
self.fc_p = nn.Linear(dim, hidden_size)
self.blocks = nn.ModuleList([
ResnetBlockFC(hidden_size) for i in range(n_blocks)
])
self.fc_out = nn.Linear(hidden_size, 1)
if not leaky:
self.actvn = F.relu
else:
self.actvn = lambda x: F.leaky_relu(x, 0.2)
self.sample_mode = sample_mode
self.padding = padding
def sample_plane_feature(self, p, c, plane='xz'):
xy = normalize_coordinate(p.clone(), plane=plane, padding=self.padding) # normalize to the range of (0, 1)
xy = xy[:, :, None].float()
vgrid = 2.0 * xy - 1.0 # normalize to (-1, 1)
c = F.grid_sample(c, vgrid, padding_mode='border', align_corners=True, mode=self.sample_mode).squeeze(-1)
return c
def sample_grid_feature(self, p, c):
p_nor = normalize_3d_coordinate(p.clone(), padding=self.padding) # normalize to the range of (0, 1)
p_nor = p_nor[:, :, None, None].float()
vgrid = 2.0 * p_nor - 1.0 # normalize to (-1, 1)
# acutally trilinear interpolation if mode = 'bilinear'
c = F.grid_sample(c, vgrid, padding_mode='border', align_corners=True, mode=self.sample_mode).squeeze(-1).squeeze(-1)
return c
def forward(self, p, c_plane, **kwargs):
if self.c_dim != 0:
plane_type = list(c_plane.keys())
c = 0
if 'grid' in plane_type:
c += self.sample_grid_feature(p, c_plane['grid'])
if 'xz' in plane_type:
c += self.sample_plane_feature(p, c_plane['xz'], plane='xz')
if 'xy' in plane_type:
c += self.sample_plane_feature(p, c_plane['xy'], plane='xy')
if 'yz' in plane_type:
c += self.sample_plane_feature(p, c_plane['yz'], plane='yz')
c = c.transpose(1, 2)
p = p.float()
net = self.fc_p(p)
for i in range(self.n_blocks):
if self.c_dim != 0:
net = net + self.fc_c[i](c)
net = self.blocks[i](net)
out = self.fc_out(self.actvn(net))
out = out.squeeze(-1)
return out
class PatchLocalDecoder(nn.Module):
''' Decoder with conditional batch normalization (CBN) class.
Instead of conditioning on global features, on plane local features
Args:
dim (int): input dimension
c_dim (int): dimension of latent conditioned code c
hidden_size (int): hidden size of Decoder network
n_blocks (int): number of blocks ResNetBlockFC layers
leaky (bool): whether to use leaky ReLUs
sample_mode (str): sampling feature strategy, bilinear|nearest
local_coord (bool): whether to use local coordinate
unit_size (float): defined voxel unit size for local system
pos_encoding (str): method for the positional encoding, linear|sin_cos
padding (float): conventional padding paramter of ONet for unit cube, so [-0.5, 0.5] -> [-0.55, 0.55]
'''
def __init__(self, dim=3, c_dim=128,
hidden_size=256, leaky=False, n_blocks=5, sample_mode='bilinear', local_coord=False, pos_encoding='linear', unit_size=0.1, padding=0.1):
super().__init__()
self.c_dim = c_dim
self.n_blocks = n_blocks
if c_dim != 0:
self.fc_c = nn.ModuleList([
nn.Linear(c_dim, hidden_size) for i in range(n_blocks)
])
#self.fc_p = nn.Linear(dim, hidden_size)
self.fc_out = nn.Linear(hidden_size, 1)
self.blocks = nn.ModuleList([
ResnetBlockFC(hidden_size) for i in range(n_blocks)
])
if not leaky:
self.actvn = F.relu
else:
self.actvn = lambda x: F.leaky_relu(x, 0.2)
self.sample_mode = sample_mode
if local_coord:
self.map2local = map2local(unit_size, pos_encoding=pos_encoding)
else:
self.map2local = None
if pos_encoding == 'sin_cos':
self.fc_p = nn.Linear(60, hidden_size)
else:
self.fc_p = nn.Linear(dim, hidden_size)
def sample_feature(self, xy, c, fea_type='2d'):
if fea_type == '2d':
xy = xy[:, :, None].float()
vgrid = 2.0 * xy - 1.0 # normalize to (-1, 1)
c = F.grid_sample(c, vgrid, padding_mode='border', align_corners=True, mode=self.sample_mode).squeeze(-1)
else:
xy = xy[:, :, None, None].float()
vgrid = 2.0 * xy - 1.0 # normalize to (-1, 1)
c = F.grid_sample(c, vgrid, padding_mode='border', align_corners=True, mode=self.sample_mode).squeeze(-1).squeeze(-1)
return c
def forward(self, p, c_plane, **kwargs):
p_n = p['p_n']
p = p['p']
if self.c_dim != 0:
plane_type = list(c_plane.keys())
c = 0
if 'grid' in plane_type:
c += self.sample_feature(p_n['grid'], c_plane['grid'], fea_type='3d')
if 'xz' in plane_type:
c += self.sample_feature(p_n['xz'], c_plane['xz'])
if 'xy' in plane_type:
c += self.sample_feature(p_n['xy'], c_plane['xy'])
if 'yz' in plane_type:
c += self.sample_feature(p_n['yz'], c_plane['yz'])
c = c.transpose(1, 2)
p = p.float()
if self.map2local:
p = self.map2local(p)
net = self.fc_p(p)
for i in range(self.n_blocks):
if self.c_dim != 0:
net = net + self.fc_c[i](c)
net = self.blocks[i](net)
out = self.fc_out(self.actvn(net))
out = out.squeeze(-1)
return out
class LocalPointDecoder(nn.Module):
''' Decoder with conditional batch normalization (CBN) class.
Instead of conditioning on global features, on plane local features
Args:
dim (int): input dimension
c_dim (int): dimension of latent conditioned code c
hidden_size (int): hidden size of Decoder network
leaky (bool): whether to use leaky ReLUs
n_blocks (int): number of blocks ResNetBlockFC layers
sample_mode (str): sampling mode for points
'''
def __init__(self, dim=3, c_dim=128,
hidden_size=256, leaky=False, n_blocks=5, sample_mode='gaussian', **kwargs):
super().__init__()
self.c_dim = c_dim
self.n_blocks = n_blocks
if c_dim != 0:
self.fc_c = nn.ModuleList([
nn.Linear(c_dim, hidden_size) for i in range(n_blocks)
])
self.fc_p = nn.Linear(dim, hidden_size)
self.blocks = nn.ModuleList([
ResnetBlockFC(hidden_size) for i in range(n_blocks)
])
self.fc_out = nn.Linear(hidden_size, 1)
if not leaky:
self.actvn = F.relu
else:
self.actvn = lambda x: F.leaky_relu(x, 0.2)
self.sample_mode = sample_mode
if sample_mode == 'gaussian':
self.var = kwargs['gaussian_val']**2
def sample_point_feature(self, q, p, fea):
# q: B x M x 3
# p: B x N x 3
# fea: B x N x c_dim
#p, fea = c
if self.sample_mode == 'gaussian':
# distance betweeen each query point to the point cloud
dist = -((p.unsqueeze(1).expand(-1, q.size(1), -1, -1) - q.unsqueeze(2)).norm(dim=3)+10e-6)**2
weight = (dist/self.var).exp() # Guassian kernel
else:
weight = 1/((p.unsqueeze(1).expand(-1, q.size(1), -1, -1) - q.unsqueeze(2)).norm(dim=3)+10e-6)
#weight normalization
weight = weight/weight.sum(dim=2).unsqueeze(-1)
c_out = weight @ fea # B x M x c_dim
return c_out
def forward(self, p, c, **kwargs):
n_points = p.shape[1]
if n_points >= 30000:
pp, fea = c
c_list = []
for p_split in torch.split(p, 10000, dim=1):
if self.c_dim != 0:
c_list.append(self.sample_point_feature(p_split, pp, fea))
c = torch.cat(c_list, dim=1)
else:
if self.c_dim != 0:
pp, fea = c
c = self.sample_point_feature(p, pp, fea)
p = p.float()
net = self.fc_p(p)
for i in range(self.n_blocks):
if self.c_dim != 0:
net = net + self.fc_c[i](c)
net = self.blocks[i](net)
out = self.fc_out(self.actvn(net))
out = out.squeeze(-1)
return out