-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvPointUNet.py
552 lines (486 loc) · 21.1 KB
/
ConvPointUNet.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
import torch.nn as nn
import torch
from neural.models.convpoint.ConvPoint.convpoint.nn import PtConv
class ConvPointUNet(torch.nn.Module):
def __init__(
self,
sample_qty,
channel_in,
first_conv_channel,
n_inlet_layers,
encoder_blocks,
decoder_blocks,
encoder_channel,
decoder_channel,
n_outlet_layers,
output_layer_channel,
nout,
n_centers,
n_pts,
non_linearity="ReLU",
):
"""
- n_pts: number of points at each layer of the network
- n_centers: number of points to include in the convolution
"""
super(ConvPointUNet, self).__init__()
self.non_linearity = non_linearity
self.encoder_blocks = encoder_blocks
self.encoder_channel = encoder_channel
self.decoder_blocks = decoder_blocks
self.decoder_channel = decoder_channel
self.n_centers = n_centers
self.channel_in = channel_in
self.first_conv_channel = first_conv_channel
self.n_inlet_layers = n_inlet_layers
self.n_outlet_layers = n_outlet_layers
self.output_layer_channel = output_layer_channel
self.nout = nout
self.pts_in = n_pts[0] # inlet and outlet level of points
self.n_pts_per_level_enc = n_pts[:] # of points at the downsampling levels
self.n_pts_per_level_dec = list(
reversed(n_pts[:-1])
) # of points at the upsampling levels
self.n_pts_per_level_dec.append(sample_qty)
self.stages = len(self.encoder_blocks)
# inlet
self.create_inlet_layers()
# encoder
self.create_downsampling_layers()
self.create_encoder_layers()
# decoder
self.create_upsampling_layers()
self.create_decoder_layers()
# header
self.header = self.make_predict_module(self.decoder_channel[-1], nout)
# weight initialization # only initializes linear layer, convPt layers have initialization
self._init_weights() # TODO: fix
def create_downsampling_layers(self):
"""
The number of points are specified in the forward pass
"""
self.downsample = nn.ModuleList([])
for i in range(self.stages):
if i == 0:
self.downsample.extend(
[
ConvPointRelu(
in_features=self.first_conv_channel,
out_features=self.encoder_channel[i],
n_centers=self.n_centers,
dim=3,
nonlinearity=self.non_linearity,
)
]
)
else:
self.downsample.extend(
[
ConvPointRelu(
in_features=self.encoder_channel[i - 1],
out_features=self.encoder_channel[i],
n_centers=self.n_centers,
dim=3,
nonlinearity=self.non_linearity,
)
]
)
def create_encoder_layers(self):
self.encoder = nn.ModuleList([])
for i in range(self.stages):
self.encoder.extend(
[
ConvPointResidualBlocks(
in_features=self.encoder_channel[i],
out_features=self.encoder_channel[i],
n_centers=self.n_centers,
n_blocks=self.encoder_blocks[i],
nonlinearity=self.non_linearity,
)
]
)
def create_upsampling_layers(self):
self.upsample = nn.ModuleList([])
for i in range(self.stages):
if i == 0:
self.upsample.extend(
[
ConvPointRelu(
in_features=self.encoder_channel[-1],
n_centers=self.n_centers,
out_features=self.decoder_channel[0],
dim=3,
nonlinearity=self.non_linearity,
)
]
)
else:
self.upsample.extend(
[
ConvPointRelu(
in_features=self.decoder_channel[i - 1],
out_features=self.decoder_channel[i],
n_centers=self.n_centers,
dim=3,
nonlinearity=self.non_linearity,
)
]
)
def create_decoder_layers(self):
concat_in_channel = []
for i in range(self.stages):
if i < (self.stages - 1):
concat_in_channel.extend(
[self.decoder_channel[i] + self.encoder_channel[-i - 2]]
)
# print("concat channel in", i, " ", self.decoder_channel[i] + self.encoder_channel[-i-2])
else:
concat_in_channel.extend(
[self.decoder_channel[i] + self.first_conv_channel]
)
# print("concat channel in", i, " ", self.decoder_channel[i] + self.first_conv_channel)
self.decoder = nn.ModuleList([])
for i in range(self.stages):
self.decoder.extend(
[
ConvPointResidualBlocks(
in_features=concat_in_channel[i],
out_features=self.decoder_channel[i],
n_centers=self.n_centers,
n_blocks=self.decoder_blocks[i],
nonlinearity=self.non_linearity,
)
]
)
def create_inlet_layers(self):
module_list = nn.ModuleList()
for i in range(self.n_inlet_layers):
if i == 0:
module_list.append(
ConvPointRelu(
dim=3,
in_features=self.channel_in,
n_centers=self.n_centers,
out_features=self.first_conv_channel,
nonlinearity=self.non_linearity,
)
)
else:
module_list.append(
ConvPointRelu(
dim=3,
in_features=self.first_conv_channel,
n_centers=self.n_centers,
out_features=self.first_conv_channel,
nonlinearity=self.non_linearity,
)
)
self.inlet_conv = module_list
def make_predict_module(self, in_channels, out_channels=4):
"""
Linear Layer Input:
- (batch_size,*, H_in) where H_in is the in_features
Output:
- (batch_size,*, H_out) where H_out is the outfeatures
By mapping the shape of the input to (batch_size,n_pts,in_feats) it will process the inputs in a point-wise fashion
"""
module_list = []
if self.n_outlet_layers > 1: # multi layer
for i in range(self.n_outlet_layers):
if i == 0: # first layer
module_list.append(
nn.Linear(
in_features=in_channels,
out_features=self.output_layer_channel,
)
)
if self.non_linearity == "ELU":
module_list.append(nn.ELU(inplace=True))
else:
module_list.append(nn.ReLU(inplace=True))
else:
module_list.append(
nn.Linear(
in_features=self.output_layer_channel,
out_features=self.output_layer_channel,
)
)
if self.non_linearity == "ELU":
module_list.append(nn.ELU(inplace=True))
else:
module_list.append(nn.ReLU(inplace=True))
# output layer
module_list.append(
nn.Linear(
in_features=self.output_layer_channel, out_features=out_channels
)
)
return torch.nn.Sequential(*module_list)
else: # single layer
activation = (
nn.ELU(inplace=True)
if self.non_linearity == "ELU"
else nn.ReLU(inplace=True)
)
return torch.nn.Sequential(
nn.Linear(
in_features=in_channels, out_features=self.output_layer_channel
),
activation,
nn.Linear(
in_features=self.output_layer_channel, out_features=out_channels
),
)
def forward(self, feats, pts):
"""
pts: x,y,z coordinates
feats: input features
PtConv Forward params: (features, pts, kernel_n_points, n_points_out)
"""
pts_list = []
# pass through the inlet
if self.n_inlet_layers == 1:
# in_n_pts = sample_qty, out_n_pts=sample_qty # pts, feats, kernel size = neighborhood size
# print("inlet_0_pts shape: ",pts.shape)
# print("inlet_features_0 shape: ",feats.shape )
inlet_layer_feats, inlet_layer_pts = self.inlet_conv[0](
in_features=feats,
in_pts=pts,
n_kernel_pts=self.n_centers,
output_points=None,
)
else:
for i in range(self.n_inlet_layers):
if i == 0:
# print("inlet_0_pts shape: ",pts.shape)
# print("inlet_features_0 shape: ",feats.shape )
inlet_layer_feats, inlet_layer_pts = self.inlet_conv[i](
in_features=feats,
in_pts=pts,
n_kernel_pts=self.n_centers,
output_points=None,
)
else:
# print("inlet_{}_pts shape: ".format(i),inlet_layer_pts.shape)
# print("inlet_{}_features_0 shape: ".format(i),inlet_layer_feats.shape)
inlet_layer_feats, inlet_layer_pts = self.inlet_conv[i](
in_features=inlet_layer_feats,
in_pts=inlet_layer_pts,
n_kernel_pts=self.n_centers,
output_points=None,
)
# print("Inlet Layer Points", inlet_layer_pts.shape[1])
pts_list.append(inlet_layer_pts)
# encoder
stages = len(self.encoder_blocks)
encoder_layers = []
for i in range(stages):
if i == 0:
# print("down_sample_{} input shape pts: ".format(i),inlet_layer_pts.shape)
# print("down_sample_{} input shape feats: ".format(i),inlet_layer_feats.shape)
ds_i_feats, ds_i_pts = self.downsample[i](
in_features=inlet_layer_feats,
in_pts=inlet_layer_pts,
n_kernel_pts=self.n_centers,
output_points=self.n_pts_per_level_enc[
i
],
)
pts_list.append(ds_i_pts)
# print("down_sample_{} output shape pts: ".format(i),ds_i_pts.shape)
# print("down_sample_{} output shape feats: ".format(i),ds_i_feats.shape)
enc_i_feats, enc_i_pts = self.encoder[i](
in_features=ds_i_feats,
in_pts=ds_i_pts,
n_kernel_pts=self.n_centers,
)
# print("encoder_{} output shape pts: ".format(i),enc_i_pts.shape)
# print("encoder_{} output shape feats: ".format(i),enc_i_feats.shape)
encoder_layers.append((enc_i_feats, enc_i_pts))
# print("downsample",i,'inlet pts',inlet_layer_pts.shape[1], " outlet pts", ds_i_pts.shape[1])
else:
feats, pts = encoder_layers[i - 1]
n_centers = self.n_centers if feats.shape[1] > self.n_centers else feats.shape[1]
# print("down_sample_{} input shape pts: ".format(i),pts.shape)
# print("down_sample_{} input shape feats: ".format(i),feats.shape)
ds_i_feats, ds_i_pts = self.downsample[i](
in_features=feats,
in_pts=pts,
n_kernel_pts=n_centers,
output_points=self.n_pts_per_level_enc[i],
)
pts_list.append(ds_i_pts)
# print("down_sample_{} output shape pts: ".format(i),ds_i_pts.shape)
# print("down_sample_{} output shape feats: ".format(i),ds_i_feats.shape)
n_centers = self.n_centers if ds_i_feats.shape[1] > self.n_centers else ds_i_feats.shape[1]
# print(n_centers)
enc_i_feats, enc_i_pts = self.encoder[i](
in_features=ds_i_feats,
in_pts=ds_i_pts,
n_kernel_pts=n_centers,
)
# print("encoder_{} output shape pts: ".format(i),enc_i_pts.shape)
# print("encoder_{} output shape feats: ".format(i),enc_i_feats.shape)
# print("downsample",i,'inlet pts',pts.shape[1], " outlet pts", ds_i_pts.shape[1])
encoder_layers.append((enc_i_feats, enc_i_pts))
# decoder
pts_for_the_way_up = list(reversed(pts_list))
decoder_layers = []
for i in range(stages):
if i == 0:
enc_i_feats, enc_i_pts = encoder_layers[-1]
n_centers = self.n_centers if enc_i_feats.shape[1] > self.n_centers else enc_i_feats.shape[1]
# print("upsample_{} input shape pts: ".format(i),enc_i_pts.shape)
# print("upsample_{} input shape feats: ".format(i),enc_i_feats.shape)
upsample_i_feats, upsample_i_pts = self.upsample[i](
in_features=enc_i_feats,
in_pts=enc_i_pts,
n_kernel_pts=n_centers,
output_points=pts_for_the_way_up[i + 1],
)
# print("upsample_{} output shape pts: ".format(i),upsample_i_pts.shape)
# print("upsample_{} output shape feats: ".format(i),upsample_i_feats.shape)
# print("upsample",i,"inlet_pts", enc_i_pts.shape[1], " outlet pts", upsample_i_pts.shape[1])
else:
dec_feats, dec_pts = decoder_layers[i - 1]
n_centers = self.n_centers if dec_feats.shape[1] > self.n_centers else dec_feats.shape[1]
# print("upsample_{} input shape pts: ".format(i),dec_pts.shape)
# print("upsample_{} input shape feats: ".format(i),dec_feats.shape)
upsample_i_feats, upsample_i_pts = self.upsample[i](
in_features=dec_feats,
in_pts=dec_pts,
n_kernel_pts=n_centers,
output_points=pts_for_the_way_up[i + 1],
)
# print("upsample_{} output shape pts: ".format(i),upsample_i_pts.shape)
# print("upsample_{} output shape feats: ".format(i),upsample_i_feats.shape)
# print("upsample",i,"inlet_pts", dec_pts.shape[1], " outlet pts", upsample_i_pts.shape[1])
# skip connections
if i < (self.stages - 1):
cat_upsample_feats = torch.cat(
(upsample_i_feats, encoder_layers[-i - 2][0]), dim=2
)
# print("upsample_{} concat shape feats: ".format(i),cat_upsample_feats.shape)
# print("\tcat upsample: ", upsample.F.shape, encoder_layers[-i-2].F.shape, cat_upsample.F.shape)
else:
# print(upsample,encoder_layers[0])
cat_upsample_feats = torch.cat(
(upsample_i_feats, inlet_layer_feats), dim=2
)
# print("upsample_{} concat shape feats: ".format(i),cat_upsample_feats.shape)
# print("\tcat upsample: ", cat_upsample.F.shape)
# print("decoder_{} input shape pts: ".format(i),upsample_i_pts.shape)
# print("decoder_{} input shape feats: ".format(i),cat_upsample_feats.shape)
n_centers = self.n_centers if cat_upsample_feats.shape[1] > self.n_centers else cat_upsample_feats.shape[1]
decoder_feats, decoder_pts = self.decoder[i](
in_features=cat_upsample_feats,
in_pts=upsample_i_pts,
n_kernel_pts=n_centers)
decoder_layers.append((decoder_feats, decoder_pts))
# header
decoder_output = decoder_layers[-1][0]
# print("decoder_output_shape: ", decoder_output.shape)
y_t = self.header(decoder_output)
return y_t
def _init_weights(self):
for m in self.modules():
nl = "relu" if self.non_linearity == "ReLU" else "elu"
# if isinstance(m, PtConv):
# torch.nn.init.kaiming_normal_(m.kernel, mode="fan_out", nonlinearity=nl)
if isinstance(m, torch.nn.Linear):
torch.nn.init.kaiming_normal_(m.kernel, mode="fan_out", nonlinearity=nl)
class ConvPointRelu(nn.Module):
def __init__(
self, in_features, out_features, n_centers, dim=3, nonlinearity="ReLU"
):
super(ConvPointRelu, self).__init__()
self.conv = PtConv(in_features, out_features, n_centers, dim, use_bias=True)
self.activation = (
nn.ELU(inplace=True) if nonlinearity == "ELU" else nn.ReLU(inplace=True)
)
def forward(self, in_features, in_pts, n_kernel_pts, output_points=None):
# if output_n_pts === none then it is a stride 1 conv
conv_feats_out, pts_out = self.conv(
input=in_features,
points=in_pts,
K=n_kernel_pts,
next_pts=output_points,
normalize=False,
)
feats_out = self.activation(conv_feats_out)
return feats_out, pts_out
class ConvPointResBlock(nn.Module):
# Traditional ResNet Block
"""
-- ----------- Identity ----------- + ---- Act ->
\ /
--- Weight --- Act --- Weight ---
"""
def __init__(
self, in_features, out_features, n_centers, dim=3, nonlinearity="ReLU"
):
super(ConvPointResBlock, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.act_1 = (
nn.ELU(inplace=True) if nonlinearity == "ELU" else nn.ReLU(inplace=True)
)
self.conv_1 = PtConv(in_features, out_features, n_centers, dim, use_bias=True)
self.act_2 = (
nn.ELU(inplace=True) if nonlinearity == "ELU" else nn.ReLU(inplace=True)
)
self.conv_2 = PtConv(out_features, out_features, n_centers, dim, use_bias=True)
if in_features != out_features:
self.projection = nn.Linear(in_features, out_features, bias=True)
def forward(self, in_features, in_pts, n_kernel_pts):
residual = in_features
feats_1, pts_1 = self.conv_1(
input=in_features,
points=in_pts,
K=n_kernel_pts,
next_pts=None,
normalize=False,
)
act_feats_1 = self.act_1(feats_1)
conv_feats_2, pts_2 = self.conv_2(
input=act_feats_1,
points=pts_1,
K=n_kernel_pts,
next_pts=None,
normalize=False,
)
if self.in_features != self.out_features:
residual = self.projection(residual)
resid_feats = conv_feats_2 + residual
act_resid_feats = self.act_2(resid_feats)
return act_resid_feats, pts_2
class ConvPointResidualBlocks(nn.Module):
def __init__(
self, in_features, out_features, n_centers, n_blocks, dim=3, nonlinearity="ReLU"
):
super(ConvPointResidualBlocks, self).__init__()
self.n_blocks = n_blocks
channels = [in_features] + [out_features] * n_blocks
self.res_blocks = nn.ModuleList(
[
ConvPointResBlock(
in_features=channels[i],
out_features=channels[i + 1],
n_centers=n_centers,
dim=3,
nonlinearity=nonlinearity,
)
for i in range(n_blocks)
]
)
def forward(self, in_features, in_pts, n_kernel_pts):
for i in range(self.n_blocks):
if i == 0:
x_feats, x_pts = self.res_blocks[i](
in_features=in_features, in_pts=in_pts, n_kernel_pts=n_kernel_pts
)
else:
x_feats, x_pts = self.res_blocks[i](
in_features=x_feats, in_pts=x_pts, n_kernel_pts=n_kernel_pts
)
return x_feats, x_pts