forked from FreyrS/dMaSIF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark_models.py
359 lines (290 loc) · 11.9 KB
/
benchmark_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
350
351
352
353
354
355
356
357
358
359
import torch
import torch.nn.functional as F
import torch.nn as nn
from torch.nn import (
Sequential as Seq,
Dropout,
Linear as Lin,
LeakyReLU,
ReLU,
BatchNorm1d as BN,
)
import torch_geometric.transforms as T
from torch_geometric.data import DataLoader
from torch_geometric.nn import (
DynamicEdgeConv,
PointConv,
XConv,
fps,
radius,
global_max_pool,
knn_interpolate,
)
from pykeops.torch import LazyTensor
from benchmark_layers import MyDynamicEdgeConv, MyXConv
from geometry_processing import dMaSIFConv, mesh_normals_areas, tangent_vectors
from helper import diagonal_ranges
DEConv = {"torch": DynamicEdgeConv, "keops": MyDynamicEdgeConv}
# Dynamic Graph CNNs ===========================================================
# Adapted from the PyTorch_geometric gallery to get a close fit to
# the original paper.
def MLP(channels, batch_norm=True):
"""Multi-layer perceptron, with ReLU non-linearities and batch normalization."""
return Seq(
*[
Seq(
Lin(channels[i - 1], channels[i]),
BN(channels[i]) if batch_norm else nn.Identity(),
LeakyReLU(negative_slope=0.2),
)
for i in range(1, len(channels))
]
)
class DGCNN_seg(torch.nn.Module):
def __init__(
self, in_channels, out_channels, n_layers, k=40, aggr="max", backend="keops"
):
super(DGCNN_seg, self).__init__()
self.name = "DGCNN_seg_" + backend
self.I, self.O = (
in_channels + 3,
out_channels,
) # Add coordinates to input channels
self.n_layers = n_layers
self.transform_1 = DEConv[backend](MLP([2 * 3, 64, 128]), k, aggr)
self.transform_2 = MLP([128, 1024])
self.transform_3 = MLP([1024, 512, 256], batch_norm=False)
self.transform_4 = Lin(256, 3 * 3)
self.conv_layers = nn.ModuleList(
[DEConv[backend](MLP([2 * self.I, self.O, self.O]), k, aggr)]
+ [
DEConv[backend](MLP([2 * self.O, self.O, self.O]), k, aggr)
for i in range(n_layers - 1)
]
)
self.linear_layers = nn.ModuleList(
[
nn.Sequential(
nn.Linear(self.O, self.O), nn.ReLU(), nn.Linear(self.O, self.O)
)
for i in range(n_layers)
]
)
self.linear_transform = nn.ModuleList(
[nn.Linear(self.I, self.O)]
+ [nn.Linear(self.O, self.O) for i in range(n_layers - 1)]
)
def forward(self, positions, features, batch_indices):
# Lab: (B,), Pos: (N, 3), Batch: (N,)
pos, feat, batch = positions, features, batch_indices
# TransformNet:
x = pos # Don't use the normals!
x = self.transform_1(x, batch) # (N, 3) -> (N, 128)
x = self.transform_2(x) # (N, 128) -> (N, 1024)
x = global_max_pool(x, batch) # (B, 1024)
x = self.transform_3(x) # (B, 256)
x = self.transform_4(x) # (B, 3*3)
x = x[batch] # (N, 3*3)
x = x.view(-1, 3, 3) # (N, 3, 3)
# Apply the transform:
x0 = torch.einsum("ni,nij->nj", pos, x) # (N, 3)
# Add features to coordinates
x = torch.cat([x0, feat], dim=-1).contiguous()
for i in range(self.n_layers):
x_i = self.conv_layers[i](x, batch)
x_i = self.linear_layers[i](x_i)
x = self.linear_transform[i](x)
x = x + x_i
return x
# Reference PointNet models, from the PyTorch_geometric gallery =========================
class SAModule(torch.nn.Module):
"""Set abstraction module."""
def __init__(self, ratio, r, nn, max_num_neighbors=64):
super(SAModule, self).__init__()
self.ratio = ratio
self.r = r
self.conv = PointConv(nn)
self.max_num_neighbors = max_num_neighbors
def forward(self, x, pos, batch):
# Subsample with Farthest Point Sampling:
# idx = fps(pos, batch, ratio=self.ratio) # Extract self.ratio indices TURN OFF FOR NOW
idx = torch.arange(0, len(pos), device=pos.device)
# For each "cluster", get the list of (up to 64) neighbors in a ball of radius r:
row, col = radius(
pos,
pos[idx],
self.r,
batch,
batch[idx],
max_num_neighbors=self.max_num_neighbors,
)
# Applies the PointNet++ Conv:
edge_index = torch.stack([col, row], dim=0)
x = self.conv(x, (pos, pos[idx]), edge_index)
# Return the features and sub-sampled point clouds:
pos, batch = pos[idx], batch[idx]
return x, pos, batch
class GlobalSAModule(torch.nn.Module):
def __init__(self, nn):
super(GlobalSAModule, self).__init__()
self.nn = nn
def forward(self, x, pos, batch):
x = self.nn(torch.cat([x, pos], dim=1))
x = global_max_pool(x, batch)
pos = pos.new_zeros((x.size(0), 3))
batch = torch.arange(x.size(0), device=batch.device)
return x, pos, batch
class FPModule(torch.nn.Module):
def __init__(self, k, nn):
super(FPModule, self).__init__()
self.k = k
self.nn = nn
def forward(self, x, pos, batch, x_skip, pos_skip, batch_skip):
x = knn_interpolate(x, pos, pos_skip, batch, batch_skip, k=self.k)
if x_skip is not None:
x = torch.cat([x, x_skip], dim=1)
x = self.nn(x)
return x, pos_skip, batch_skip
class PointNet2_seg(torch.nn.Module):
def __init__(self, args, in_channels, out_channels):
super(PointNet2_seg, self).__init__()
self.name = "PointNet2"
self.I, self.O = in_channels, out_channels
self.radius = args.radius
self.k = 10000 # We don't restrict the number of points in a patch
self.n_layers = args.n_layers
# self.sa1_module = SAModule(1.0, self.radius, MLP([self.I+3, self.O, self.O]),self.k)
self.layers = nn.ModuleList(
[SAModule(1.0, self.radius, MLP([self.I + 3, self.O, self.O]), self.k)]
+ [
SAModule(1.0, self.radius, MLP([self.O + 3, self.O, self.O]), self.k)
for i in range(self.n_layers - 1)
]
)
self.linear_layers = nn.ModuleList(
[
nn.Sequential(
nn.Linear(self.O, self.O), nn.ReLU(), nn.Linear(self.O, self.O)
)
for i in range(self.n_layers)
]
)
self.linear_transform = nn.ModuleList(
[nn.Linear(self.I, self.O)]
+ [nn.Linear(self.O, self.O) for i in range(self.n_layers - 1)]
)
def forward(self, positions, features, batch_indices):
x = (features, positions, batch_indices)
for i, layer in enumerate(self.layers):
x_i, pos, b_ind = layer(*x)
x_i = self.linear_layers[i](x_i)
x = self.linear_transform[i](x[0])
x = x + x_i
x = (x, pos, b_ind)
return x[0]
## TangentConv benchmark segmentation
class dMaSIFConv_seg(torch.nn.Module):
def __init__(self, args, in_channels, out_channels, n_layers, radius=9.0):
super(dMaSIFConv_seg, self).__init__()
self.name = "dMaSIFConv_seg_keops"
self.radius = radius
self.I, self.O = in_channels, out_channels
self.layers = nn.ModuleList(
[dMaSIFConv(self.I, self.O, radius, self.O)]
+ [dMaSIFConv(self.O, self.O, radius, self.O) for i in range(n_layers - 1)]
)
self.linear_layers = nn.ModuleList(
[
nn.Sequential(
nn.Linear(self.O, self.O), nn.ReLU(), nn.Linear(self.O, self.O)
)
for i in range(n_layers)
]
)
self.linear_transform = nn.ModuleList(
[nn.Linear(self.I, self.O)]
+ [nn.Linear(self.O, self.O) for i in range(n_layers - 1)]
)
def forward(self, features):
# Lab: (B,), Pos: (N, 3), Batch: (N,)
points, nuv, ranges = self.points, self.nuv, self.ranges
x = features
for i, layer in enumerate(self.layers):
x_i = layer(points, nuv, x, ranges)
x_i = self.linear_layers[i](x_i)
x = self.linear_transform[i](x)
x = x + x_i
return x
def load_mesh(self, xyz, triangles=None, normals=None, weights=None, batch=None):
"""Loads the geometry of a triangle mesh.
Input arguments:
- xyz, a point cloud encoded as an (N, 3) Tensor.
- triangles, a connectivity matrix encoded as an (N, 3) integer tensor.
- weights, importance weights for the orientation estimation, encoded as an (N, 1) Tensor.
- radius, the scale used to estimate the local normals.
- a batch vector, following PyTorch_Geometric's conventions.
The routine updates the model attributes:
- points, i.e. the point cloud itself,
- nuv, a local oriented basis in R^3 for every point,
- ranges, custom KeOps syntax to implement batch processing.
"""
# 1. Save the vertices for later use in the convolutions ---------------
self.points = xyz
self.batch = batch
self.ranges = diagonal_ranges(
batch
) # KeOps support for heterogeneous batch processing
self.triangles = triangles
self.normals = normals
self.weights = weights
# 2. Estimate the normals and tangent frame ----------------------------
# Normalize the scale:
points = xyz / self.radius
# Normals and local areas:
if normals is None:
normals, areas = mesh_normals_areas(points, triangles, 0.5, batch)
tangent_bases = tangent_vectors(normals) # Tangent basis (N, 2, 3)
# 3. Steer the tangent bases according to the gradient of "weights" ----
# 3.a) Encoding as KeOps LazyTensors:
# Orientation scores:
weights_j = LazyTensor(weights.view(1, -1, 1)) # (1, N, 1)
# Vertices:
x_i = LazyTensor(points[:, None, :]) # (N, 1, 3)
x_j = LazyTensor(points[None, :, :]) # (1, N, 3)
# Normals:
n_i = LazyTensor(normals[:, None, :]) # (N, 1, 3)
n_j = LazyTensor(normals[None, :, :]) # (1, N, 3)
# Tangent basis:
uv_i = LazyTensor(tangent_bases.view(-1, 1, 6)) # (N, 1, 6)
# 3.b) Pseudo-geodesic window:
# Pseudo-geodesic squared distance:
rho2_ij = ((x_j - x_i) ** 2).sum(-1) * ((2 - (n_i | n_j)) ** 2) # (N, N, 1)
# Gaussian window:
window_ij = (-rho2_ij).exp() # (N, N, 1)
# 3.c) Coordinates in the (u, v) basis - not oriented yet:
X_ij = uv_i.matvecmult(x_j - x_i) # (N, N, 2)
# 3.d) Local average in the tangent plane:
orientation_weight_ij = window_ij * weights_j # (N, N, 1)
orientation_vector_ij = orientation_weight_ij * X_ij # (N, N, 2)
# Support for heterogeneous batch processing:
orientation_vector_ij.ranges = self.ranges # Block-diagonal sparsity mask
orientation_vector_i = orientation_vector_ij.sum(dim=1) # (N, 2)
orientation_vector_i = (
orientation_vector_i + 1e-5
) # Just in case someone's alone...
# 3.e) Normalize stuff:
orientation_vector_i = F.normalize(orientation_vector_i, p=2, dim=-1) # (N, 2)
ex_i, ey_i = (
orientation_vector_i[:, 0][:, None],
orientation_vector_i[:, 1][:, None],
) # (N,1)
# 3.f) Re-orient the (u,v) basis:
uv_i = tangent_bases # (N, 2, 3)
u_i, v_i = uv_i[:, 0, :], uv_i[:, 1, :] # (N, 3)
tangent_bases = torch.cat(
(ex_i * u_i + ey_i * v_i, -ey_i * u_i + ex_i * v_i), dim=1
).contiguous() # (N, 6)
# 4. Store the local 3D frame as an attribute --------------------------
self.nuv = torch.cat(
(normals.view(-1, 1, 3), tangent_bases.view(-1, 2, 3)), dim=1
)