-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
349 lines (271 loc) · 12.9 KB
/
models.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
from torch_cluster import knn_graph
from torch_geometric.nn.conv import XConv,PPFConv # PPF Conv is roto-translational equivariant
import torch.nn as nn
import torch
from torch.nn import Sequential, Linear, ReLU
from torch_geometric.nn import MessagePassing
from torch_cluster import knn_graph
from torch_geometric.nn.conv import XConv,PPFConv # PPF Conv is roto-translational equivariant
import torch.nn as nn
import torch
from torch.nn import Sequential, Linear, ReLU
from torch_geometric.nn import MessagePassing
class PointAutoEncoderV2(nn.Module):
def __init__(self, num_points, latent_dim, num_features = 0):
"""
Architecture from https://arxiv.org/pdf/1707.02392.pdf Learning Representations and Generative Models for 3D Point Clouds
Adapted from https://github.com/TropComplique/point-cloud-autoencoder
Arguments:
num_points: integer describing number of points in each padded point cloud
latent_dim: integer describing dimensionality of latent vector
"""
super(PointAutoEncoderV2, self).__init__()
pointwise_layers = []
num_units = [num_features + 3, 64, 128, 128, 256, latent_dim]
for n, m in zip(num_units[:-1], num_units[1:]):
pointwise_layers.extend([
nn.Conv1d(n, m, kernel_size=1, bias=False),
nn.BatchNorm1d(m),
nn.ReLU(inplace=True)
])
self.pointwise_layers = nn.Sequential(*pointwise_layers)
self.pooling = nn.AdaptiveMaxPool1d(1)
self.num_features= num_features
self.decoder = nn.Sequential(
nn.Conv1d(latent_dim, 256, kernel_size=1, bias=False),
nn.ReLU(inplace=True),
nn.Conv1d(256, 256, kernel_size=1, bias=False),
nn.ReLU(inplace=True),
nn.Conv1d(256, num_points * 3, kernel_size=1)
)
self.mlp1 = nn.Sequential(
nn.Linear(latent_dim, latent_dim//2),
nn.ReLU(inplace=True),
nn.Linear(latent_dim//2, num_points)
)
self.mlp2 = nn.Sequential(
nn.Linear(latent_dim, latent_dim//2),
nn.ReLU(inplace=True),
nn.Linear(latent_dim//2, num_points)
)
self.apply(self.weights_init_uniform)
def weights_init_uniform(self, m):
classname = m.__class__.__name__
# for every Linear layer in a model..
if classname.find('Linear') != -1:
# apply a uniform distribution to the weights and a bias=0
m.weight.data.normal_(mean=0.0, std=0.12)
m.bias.data.fill_(0)
if classname.find('Conv1d')!= -1:
m.weight.data.normal_(mean=0.0, std=0.12)
def forward(self, x):
"""
Arguments:
x: a float tensor with shape [b, 3, num_points].
Returns:
encoding: a float tensor with shape [b, k].
restoration: a float tensor with shape [b, 3, num_points].
"""
x = x.unsqueeze(0)
x = torch.permute(x, (0, 2, 1))
b, _, num_points = x.size()
x = self.pointwise_layers(x) # shape [b, k, num_points]
encoding = self.pooling(x) # shape [b, k, 1]
reconstructed_feature1 = self.mlp1(encoding.T).T
reconstructed_feature2 = self.mlp2(encoding.T).T
print(reconstructed_feature1.shape, reconstructed_feature2.shape)
reconstructed_points = self.decoder(encoding) # shape [b, num_points * 3, 1]
reconstructed_points = reconstructed_points.view(b, num_points, 3)
reconstructed_x = torch.cat((reconstructed_points,reconstructed_feature1,reconstructed_feature2 ), axis=1)
return encoding, reconstructed_x
class PointAutoEncoder(nn.Module):
def __init__(self, num_points, latent_dim, num_features = 0):
"""
Architecture from https://arxiv.org/pdf/1707.02392.pdf Learning Representations and Generative Models for 3D Point Clouds
Adapted from https://github.com/TropComplique/point-cloud-autoencoder
Arguments:
num_points: integer describing number of points in each padded point cloud
latent_dim: integer describing dimensionality of latent vector
"""
super(PointAutoEncoder, self).__init__()
pointwise_layers = []
num_units = [num_features + 3, 64, 128, 128, 256, latent_dim]
for n, m in zip(num_units[:-1], num_units[1:]):
pointwise_layers.extend([
nn.Conv1d(n, m, kernel_size=1, bias=False),
nn.BatchNorm1d(m),
nn.ReLU(inplace=True)
])
self.pointwise_layers = nn.Sequential(*pointwise_layers)
self.pooling = nn.AdaptiveMaxPool1d(1)
self.num_features= num_features
self.decoder = nn.Sequential(
nn.Conv1d(latent_dim, 256, kernel_size=1, bias=False),
nn.ReLU(inplace=True),
nn.Conv1d(256, 256, kernel_size=1, bias=False),
nn.ReLU(inplace=True),
nn.Conv1d(256, num_points * 3, kernel_size=1)
)
self.apply(self.weights_init_uniform)
def weights_init_uniform(self, m):
classname = m.__class__.__name__
# for every Linear layer in a model..
if classname.find('Linear') != -1:
# apply a uniform distribution to the weights and a bias=0
m.weight.data.normal_(mean=0.0, std=0.12)
m.bias.data.fill_(0)
if classname.find('Conv1d')!= -1:
m.weight.data.normal_(mean=0.0, std=0.12)
def forward(self, x):
"""
Arguments:
x: a float tensor with shape [b, 3, num_points].
Returns:
encoding: a float tensor with shape [b, k].
restoration: a float tensor with shape [b, 3, num_points].
"""
x = x.unsqueeze(0)
x = torch.permute(x, (0, 2, 1))
b, _, num_points = x.size()
x = self.pointwise_layers(x) # shape [b, k, num_points]
encoding = self.pooling(x) # shape [b, k, 1]
x = self.decoder(encoding) # shape [b, num_points * 3, 1]
restoration = x.view(b, num_points, 3 + self.num_features)
return encoding, restoration
class PointNetLayer(MessagePassing):
def __init__(self, in_channels, out_channels):
# Message passing with "max" aggregation.
super().__init__(aggr='max')
# Initialization of the MLP:
# Here, the number of input features correspond to the hidden node
# dimensionality plus point dimensionality (=3).
self.mlp = Sequential(Linear(in_channels + 3, out_channels),
ReLU(),
Linear(out_channels, out_channels))
def forward(self, h, pos, edge_index):
# Start propagating messages.
return self.propagate(edge_index, h=h, pos=pos)
def message(self, h_j, pos_j, pos_i):
# h_j defines the features of neighboring nodes as shape [num_edges, in_channels]
# pos_j defines the position of neighboring nodes as shape [num_edges, 3]
# pos_i defines the position of central nodes as shape [num_edges, 3]
input = pos_j - pos_i # Compute spatial relation.
if h_j is not None:
# In the first layer, we may not have any hidden node features,
# so we only combine them in case they are present.
input = torch.cat([h_j, input], dim=-1)
print(input.shape)
mess = self.mlp(input)
print(mess)
return mess
class PointNetEncoder(torch.nn.Module):
def __init__(self):
super().__init__()
torch.manual_seed(12345)
self.conv1 = PointNetLayer(3, 32)
self.conv2 = PointNetLayer(32, 32)
def forward(self, pos, batch):
# Compute the kNN graph:
# Here, we need to pass the batch vector to the function call in order
# to prevent creating edges between points of different examples.
# We also add `loop=True` which will add self-loops to the graph in
# order to preserve central point information.
edge_index = knn_graph(pos, k=16, batch=batch, loop=True)
# 3. Start bipartite message passing.
h = self.conv1(h=pos, pos=pos, edge_index=edge_index)
h = h.relu()
h = self.conv2(h=h, pos=pos, edge_index=edge_index)
return h
class XConvEncoder(torch.nn.Module):
def __init__(self, num_features, hidden_dim, kernel_size=10):
super().__init__()
torch.manual_seed(12345)
self.conv1 = XConv(num_features,hidden_dim, dim=num_features, kernel_size= kernel_size, hidden_channels= num_features)
self.conv2 = XConv(hidden_dim,hidden_dim, dim=num_features, kernel_size= kernel_size, hidden_channels= num_features)
# self.conv2 = XConv(hidden_dim,hidden_dim, dim=num_features, kernel_size= kernel_size, hidden_channels= num_features)
# self.conv2 = XConv(hidden_dim,hidden_dim, dim=num_features, kernel_size= kernel_size, hidden_channels= num_features)
# self.conv2 = XConv(hidden_dim,hidden_dim, dim=num_features, kernel_size= kernel_size, hidden_channels= num_features)
def forward(self, pos, batch):
x = self.conv1(x=pos, pos=pos, batch=batch)
x = x.relu()
x = self.conv2(x=x, pos=pos, batch=batch)
return x
class XConvDecoder(torch.nn.Module):
def __init__(self, input_dim, out_features):
super().__init__()
torch.manual_seed(12345)
self.conv1 = XConv(input_dim, input_dim, dim=input_dim, kernel_size= 5, hidden_channels= input_dim)
self.conv2 = XConv(input_dim,out_features, dim=input_dim, kernel_size= 5, hidden_channels= out_features)
def forward(self, pos, batch):
x = self.conv1(x=pos, pos=pos, batch=batch)
x = x.relu()
x = self.conv2(x=x, pos=pos, batch=batch)
return x
class LinearDecoder(torch.nn.Module):
def __init__(self, input_dim, out_features, hidden_dim=16):
super().__init__()
self.linear1 = nn.Linear(input_dim, hidden_dim)
self.linear2 = nn.Linear(hidden_dim, hidden_dim)
self.linear3 = nn.Linear(hidden_dim, hidden_dim)
self.linear4 = nn.Linear(hidden_dim, out_features)
def forward(self, z):
x_ = self.linear1(z).relu()
x_ = self.linear2(x_).relu()
x_ = self.linear3(x_).relu()
x_ = self.linear4(x_)
return x_
class XConvAutoEncoder(torch.nn.Module):
def __init__(self, num_features, num_points, hidden_dim=128, kernel_size=10):
super().__init__()
torch.manual_seed(12345)
# self.encoder = nn.Sequential(
# XConv(num_features, 64, dim=3, kernel_size=10, hidden_channels=3),
# nn.ReLU(inplace=True),
# XConv(64, 128, dim=3, kernel_size=10, hidden_channels=3),
# nn.ReLU(inplace=True),
# XConv(128, 128, dim=3, kernel_size=10, hidden_channels=3),
# nn.ReLU(inplace=True),
# XConv(128, 256, dim=3, kernel_size=10, hidden_channels=3),
# )
self.conv1 = XConv(num_features, 64, dim=3, kernel_size=10, hidden_channels=3)
self.conv2 = XConv(64, 128, dim=3, kernel_size=10, hidden_channels=3)
self.conv3 = XConv(128, 128, dim=3, kernel_size=10, hidden_channels=3)
self.conv4 = XConv(128, 256, dim=3, kernel_size=10, hidden_channels=3)
self.conv5 = XConv(256, hidden_dim, dim=3, kernel_size=10, hidden_channels=3)
self.pooling = nn.AdaptiveMaxPool1d(1)
self.decoder = nn.Sequential(
nn.Conv1d(hidden_dim, 256, kernel_size=1, bias=False),
# nn.BatchNorm1d(256),
nn.ReLU(inplace=True),
nn.Conv1d(256, 256, kernel_size=1, bias=False),
# nn.BatchNorm1d(256),
nn.Conv1d(256, num_points * 3, kernel_size=1)
)
def forward(self, pos, batch):
num_points = pos.shape[0]
x = self.conv1(x=pos, pos=pos, batch=batch).relu()
x = self.conv2(x=x, pos=pos, batch=batch).relu()
x = self.conv3(x=x, pos=pos, batch=batch).relu()
x = self.conv4(x=x, pos=pos, batch=batch).relu()
x = self.conv5(x=x, pos=pos, batch=batch)
encoding = self.pooling(x.T) # shape [b, k, 1]
decoded = self.decoder(encoding)
restoration = decoded.view(3, num_points)
# x = self.decoder(encoding) # shape [b, num_points * 3, 1]
# restoration = x.view(b, 3, num_points)
return restoration.T
class LinearPointAutoEncoder(nn.Module):
def __init__(self, encoder, decoder):
super().__init__()
self.encoder = encoder
self.decoder = decoder
def forward(self, pos, batch):
z = self.encode(pos, batch)
x_ = self.decode(z)
return x_
def encode(self, pos, batch):
encoded = self.encoder(pos, batch)
return encoded
def decode(self, encoded):
decoded = self.decoder(encoded)
return decoded