-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpointconv_util.py
808 lines (660 loc) · 31.2 KB
/
pointconv_util.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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
import torch
import torch.nn as nn
import torch.nn.functional as F
from time import time
import numpy as np
from sklearn.neighbors.kde import KernelDensity
from pointnet2 import pointnet2_utils
LEAKY_RATE = 0.1
use_bn = False
class Conv1d(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=1, stride=1, padding=0, use_leaky=True, bn=use_bn):
super(Conv1d, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
relu = nn.ReLU(inplace=True) if not use_leaky else nn.LeakyReLU(LEAKY_RATE, inplace=True)
self.composed_module = nn.Sequential(
nn.Conv1d(in_channels, out_channels, kernel_size=kernel_size, stride=stride, padding=padding, bias=True),
nn.BatchNorm1d(out_channels) if bn else nn.Identity(),
relu
)
def forward(self, x):
x = self.composed_module(x)
return x
class Conv2d(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=1, stride=1, padding=0, use_leaky=True, bn=use_bn, bias=True):
super(Conv2d, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
relu = nn.ReLU(inplace=True) if not use_leaky else nn.LeakyReLU(LEAKY_RATE, inplace=True)
self.composed_module = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=kernel_size, stride=stride, padding=padding, bias=bias),
nn.BatchNorm2d(out_channels) if bn else nn.Identity(),
relu
)
def forward(self, x):
x = self.composed_module(x)
return x
def square_distance(src, dst):
"""
Calculate Euclid distance between each two points.
src^T * dst = xn * xm + yn * ym + zn * zm;
sum(src^2, dim=-1) = xn*xn + yn*yn + zn*zn;
sum(dst^2, dim=-1) = xm*xm + ym*ym + zm*zm;
dist = (xn - xm)^2 + (yn - ym)^2 + (zn - zm)^2
= sum(src**2,dim=-1)+sum(dst**2,dim=-1)-2*src^T*dst
Input:
src: source points, [B, N, C]
dst: target points, [B, M, C]
Output:
dist: per-point square distance, [B, N, M]
"""
B, N, _ = src.shape
_, M, _ = dst.shape
dist = -2 * torch.matmul(src, dst.permute(0, 2, 1))
dist += torch.sum(src ** 2, -1).view(B, N, 1)
dist += torch.sum(dst ** 2, -1).view(B, 1, M)
return dist
def knn_point(nsample, xyz, new_xyz):
"""
Input:
nsample: max sample number in local region
xyz: all points, [B, N, C]
new_xyz: query points, [B, S, C]
Return:
group_idx: grouped points index, [B, S, nsample]
"""
sqrdists = square_distance(new_xyz, xyz)
_, group_idx = torch.topk(sqrdists, nsample, dim = -1, largest=False, sorted=False)
return group_idx
def index_points_gather(points, fps_idx):
"""
Input:
points: input points data, [B, N, C]
idx: sample index data, [B, S]
Return:
new_points:, indexed points data, [B, S, C]
"""
points_flipped = points.permute(0, 2, 1).contiguous()
new_points = pointnet2_utils.gather_operation(points_flipped, fps_idx)
return new_points.permute(0, 2, 1).contiguous()
def index_points_group(points, knn_idx):
"""
Input:
points: input points data, [B, N, C]
knn_idx: sample index data, [B, N, K]
Return:
new_points:, indexed points data, [B, N, K, C]
"""
points_flipped = points.permute(0, 2, 1).contiguous()
new_points = pointnet2_utils.grouping_operation(points_flipped, knn_idx.int()).permute(0, 2, 3, 1)
return new_points
def group(nsample, xyz, points):
"""
Input:
nsample: scalar
xyz: input points position data, [B, N, C]
points: input points data, [B, N, D]
Return:
# new_xyz: sampled points position data, [B, N, C]
new_points: sampled points data, [B, npoint, nsample, C+D]
"""
B, N, C = xyz.shape
S = N
new_xyz = xyz
idx = knn_point(nsample, xyz, new_xyz)
grouped_xyz = index_points_group(xyz, idx) # [B, npoint, nsample, C]
grouped_xyz_norm = grouped_xyz - new_xyz.view(B, S, 1, C)
if points is not None:
grouped_points = index_points_group(points, idx)
new_points = torch.cat([grouped_xyz_norm, grouped_points], dim=-1) # [B, npoint, nsample, C+D]
else:
new_points = grouped_xyz_norm
return new_points, grouped_xyz_norm
def group_query(nsample, s_xyz, xyz, s_points):
"""
Input:
nsample: scalar
s_xyz: input points position data, [B, N, C]
s_points: input points data, [B, N, D]
xyz: input points position data, [B, S, C]
Return:
new_xyz: sampled points position data, [B, 1, C]
new_points: sampled points data, [B, 1, N, C+D]
"""
B, N, C = s_xyz.shape
S = xyz.shape[1]
new_xyz = xyz
idx = knn_point(nsample, s_xyz, new_xyz)
grouped_xyz = index_points_group(s_xyz, idx) # [B, npoint, nsample, C]
grouped_xyz_norm = grouped_xyz - new_xyz.view(B, S, 1, C)
if s_points is not None:
grouped_points = index_points_group(s_points, idx)
new_points = torch.cat([grouped_xyz_norm, grouped_points], dim=-1) # [B, npoint, nsample, C+D]
else:
new_points = grouped_xyz_norm
return new_points, grouped_xyz_norm
class WeightNet(nn.Module):
def __init__(self, in_channel, out_channel, hidden_unit = [8, 8], bn = use_bn):
super(WeightNet, self).__init__()
self.bn = bn
self.mlp_convs = nn.ModuleList()
self.mlp_bns = nn.ModuleList()
if hidden_unit is None or len(hidden_unit) == 0:
self.mlp_convs.append(nn.Conv2d(in_channel, out_channel, 1))
self.mlp_bns.append(nn.BatchNorm2d(out_channel))
else:
self.mlp_convs.append(nn.Conv2d(in_channel, hidden_unit[0], 1))
self.mlp_bns.append(nn.BatchNorm2d(hidden_unit[0]))
for i in range(1, len(hidden_unit)):
self.mlp_convs.append(nn.Conv2d(hidden_unit[i - 1], hidden_unit[i], 1))
self.mlp_bns.append(nn.BatchNorm2d(hidden_unit[i]))
self.mlp_convs.append(nn.Conv2d(hidden_unit[-1], out_channel, 1))
self.mlp_bns.append(nn.BatchNorm2d(out_channel))
def forward(self, localized_xyz):
#xyz : BxCxKxN
weights = localized_xyz
for i, conv in enumerate(self.mlp_convs):
if self.bn:
bn = self.mlp_bns[i]
weights = F.relu(bn(conv(weights)))
else:
weights = F.relu(conv(weights))
return weights
class PointConv(nn.Module):
def __init__(self, nsample, in_channel, out_channel, weightnet = 16, bn = use_bn, use_leaky = True):
super(PointConv, self).__init__()
self.bn = bn
self.nsample = nsample
self.weightnet = WeightNet(3, weightnet)
self.linear = nn.Linear(weightnet * in_channel, out_channel)
if bn:
self.bn_linear = nn.BatchNorm1d(out_channel)
self.relu = nn.ReLU(inplace=True) if not use_leaky else nn.LeakyReLU(LEAKY_RATE, inplace=True)
def forward(self, xyz, points):
"""
PointConv without strides size, i.e., the input and output have the same number of points.
Input:
xyz: input points position data, [B, C, N]
points: input points data, [B, D, N]
Return:
new_xyz: sampled points position data, [B, C, S]
new_points_concat: sample points feature data, [B, D', S]
"""
B = xyz.shape[0]
N = xyz.shape[2]
xyz = xyz.permute(0, 2, 1)
points = points.permute(0, 2, 1)
new_points, grouped_xyz_norm = group(self.nsample, xyz, points) # [B, npoint, nsample, C+D]
grouped_xyz = grouped_xyz_norm.permute(0, 3, 2, 1)
weights = self.weightnet(grouped_xyz) #BxWxKxN
new_points = torch.matmul(input=new_points.permute(0, 1, 3, 2), other = weights.permute(0, 3, 2, 1)).view(B, N, -1) #BxNxWxK * BxNxKxC => BxNxWxC -> BxNx(W*C)
new_points = self.linear(new_points)
if self.bn:
new_points = self.bn_linear(new_points.permute(0, 2, 1))
else:
new_points = new_points.permute(0, 2, 1)
new_points = self.relu(new_points)
return new_points
class PointConvD(nn.Module):
def __init__(self, npoint, nsample, in_channel, out_channel, weightnet = 16, bn = use_bn, use_leaky = True):
super(PointConvD, self).__init__()
self.npoint = npoint
self.bn = bn
self.nsample = nsample
self.weightnet = WeightNet(3, weightnet)
self.linear = nn.Linear(weightnet * in_channel, out_channel)
if bn:
self.bn_linear = nn.BatchNorm1d(out_channel)
self.relu = nn.ReLU(inplace=True) if not use_leaky else nn.LeakyReLU(LEAKY_RATE, inplace=True)
def forward(self, xyz, points):
"""
PointConv with downsampling.
Input:
xyz: input points position data, [B, C, N]
points: input points data, [B, D, N]
Return:
new_xyz: sampled points position data, [B, C, S]
new_points_concat: sample points feature data, [B, D', S]
"""
#import ipdb; ipdb.set_trace()
B = xyz.shape[0]
N = xyz.shape[2]
xyz = xyz.permute(0, 2, 1)
points = points.permute(0, 2, 1)
fps_idx = pointnet2_utils.furthest_point_sample(xyz, self.npoint)
new_xyz = index_points_gather(xyz, fps_idx)
new_points, grouped_xyz_norm = group_query(self.nsample, xyz, new_xyz, points)
grouped_xyz = grouped_xyz_norm.permute(0, 3, 2, 1)
weights = self.weightnet(grouped_xyz)
# B, N, S, C
new_points = torch.matmul(input=new_points.permute(0, 1, 3, 2), other = weights.permute(0, 3, 2, 1)).view(B, self.npoint, -1)
new_points = self.linear(new_points)
if self.bn:
new_points = self.bn_linear(new_points.permute(0, 2, 1))
else:
new_points = new_points.permute(0, 2, 1)
new_points = self.relu(new_points)
return new_xyz.permute(0, 2, 1), new_points, fps_idx
class PointConvK(nn.Module):
def __init__(self, npoint, nsample, in_channel, out_channel, weightnet = 16, bn = use_bn, use_leaky = True):
super(PointConvK, self).__init__()
self.npoint = npoint
self.bn = bn
self.nsample = nsample
self.weightnet = WeightNet(3, weightnet)
self.kernel = nn.Sequential(
nn.Conv2d(in_channel, out_channel, 1, bias=False),
nn.BatchNorm2d(out_channel),
)
self.agg = nn.Sequential(
nn.Conv2d(in_channel, 1, 1, bias=False),
nn.BatchNorm2d(1),
)
self.linear = nn.Linear(out_channel, out_channel)
if bn:
self.bn_linear = nn.BatchNorm1d(out_channel)
self.relu = nn.ReLU(inplace=True) if not use_leaky else nn.LeakyReLU(LEAKY_RATE, inplace=True)
def forward(self, xyz, points):
"""
PointConv with downsampling.
Input:
xyz: input points position data, [B, C, N]
points: input points data, [B, D, N]
Return:
new_xyz: sampled points position data, [B, C, S]
new_points_concat: sample points feature data, [B, D', S]
"""
#import ipdb; ipdb.set_trace()
B = xyz.shape[0]
N = xyz.shape[2]
C = points.shape[1]
xyz = xyz.permute(0, 2, 1)
points = points.permute(0, 2, 1)
fps_idx = pointnet2_utils.furthest_point_sample(xyz, self.npoint)
new_xyz = index_points_gather(xyz, fps_idx)
new_points, grouped_xyz_norm = group_query(self.nsample, xyz, new_xyz, points) # [B, npoint, nsample, C+D]
kernel = self.kernel(new_points.permute(0, 3, 1, 2))
kernel = self.relu(kernel) # B, Out*In, N, S
aggregation = torch.matmul(input = kernel.permute(0, 2, 1, 3), other = new_points.permute(0, 1, 2, 3))
aggregation = self.relu(self.agg(aggregation.permute(0, 3, 1, 2))).squeeze(1)
# B, N, S, C
new_points = self.linear(aggregation)
if self.bn:
new_points = self.bn_linear(new_points.permute(0, 2, 1))
else:
new_points = new_points.permute(0, 2, 1)
new_points = self.relu(new_points)
return new_xyz.permute(0, 2, 1), new_points, fps_idx
class SetAbstract(nn.Module):
def __init__(self, npoint, nsample, in_channel, mlp, mlp2=None, use_leaky = True):
super(SetAbstract, self).__init__()
self.npoint = npoint
self.nsample = nsample
self.mlp_convs = nn.ModuleList()
self.mlp_bns = nn.ModuleList()
self.mlp2_convs = nn.ModuleList()
last_channel = in_channel+3
for out_channel in mlp:
self.mlp_convs.append(nn.Conv2d(last_channel, out_channel, 1, bias = False))
self.mlp_bns.append(nn.BatchNorm2d(out_channel))
last_channel = out_channel
if mlp2:
for out_channel in mlp2:
self.mlp2_convs.append(nn.Sequential(nn.Conv1d(last_channel, out_channel, 1, bias=False),
nn.BatchNorm1d(out_channel)))
last_channel = out_channel
self.relu = nn.ReLU(inplace=True) if not use_leaky else nn.LeakyReLU(LEAKY_RATE, inplace=True)
def forward(self, xyz, points):
"""
PointConv with downsampling.
Input:
xyz: input points position data, [B, C, N]
points: input points data, [B, D, N]
Return:
new_xyz: sampled points position data, [B, C, S]
new_points_concat: sample points feature data, [B, D', S]
"""
#import ipdb; ipdb.set_trace()
B = xyz.shape[0]
N = xyz.shape[2]
xyz = xyz.permute(0, 2, 1)
points = points.permute(0, 2, 1)
fps_idx = pointnet2_utils.furthest_point_sample(xyz, self.npoint)
new_xyz = index_points_gather(xyz, fps_idx)
new_points, grouped_xyz_norm = group_query(self.nsample, xyz, new_xyz, points)
new_points = new_points.permute(0, 3, 1, 2)
for i, conv in enumerate(self.mlp_convs):
bn = self.mlp_bns[i]
new_points = self.relu(bn(conv(new_points)))
new_points = torch.max(new_points, -1)[0]
for i, conv in enumerate(self.mlp2_convs):
new_points = self.relu(conv(new_points))
return new_xyz.permute(0, 2, 1), new_points, fps_idx
class PointAtten(nn.Module):
def __init__(self, npoint, nsample, in_channel, out_channel, weightnet = 16, bn = use_bn, use_leaky = True):
super(PointConvD, self).__init__()
self.npoint = npoint
self.bn = bn
self.nsample = nsample
self.weightnet = WeightNet(3, weightnet)
self.linear = nn.Linear(weightnet * in_channel, out_channel)
if bn:
self.bn_linear = nn.BatchNorm1d(out_channel)
self.relu = nn.ReLU(inplace=True) if not use_leaky else nn.LeakyReLU(LEAKY_RATE, inplace=True)
def forward(self, xyz, points):
"""
PointConv with downsampling.
Input:
xyz: input points position data, [B, C, N]
points: input points data, [B, D, N]
Return:
new_xyz: sampled points position data, [B, C, S]
new_points_concat: sample points feature data, [B, D', S]
"""
#import ipdb; ipdb.set_trace()
B = xyz.shape[0]
N = xyz.shape[2]
xyz = xyz.permute(0, 2, 1)
points = points.permute(0, 2, 1)
fps_idx = pointnet2_utils.furthest_point_sample(xyz, self.npoint)
new_xyz = index_points_gather(xyz, fps_idx)
new_points, grouped_xyz_norm = group_query(self.nsample, xyz, new_xyz, points) # B, N, S, C
grouped_xyz = grouped_xyz_norm.permute(0, 3, 2, 1)
weights = self.weightnet(grouped_xyz) # B, 16, S, N
new_points = torch.matmul(input=new_points.permute(0, 1, 3, 2), other = weights.permute(0, 3, 2, 1)).view(B, self.npoint, -1) # B, N, C, S x B, N, S, 16
new_points = self.linear(new_points)
if self.bn:
new_points = self.bn_linear(new_points.permute(0, 2, 1))
else:
new_points = new_points.permute(0, 2, 1)
new_points = self.relu(new_points)
return new_xyz.permute(0, 2, 1), new_points, fps_idx
class PointConvFlow(nn.Module):
def __init__(self, nsample, in_channel, mlp, bn = use_bn, use_leaky = True):
super(PointConvFlow, self).__init__()
self.nsample = nsample
self.bn = bn
self.mlp_convs = nn.ModuleList()
if bn:
self.mlp_bns = nn.ModuleList()
last_channel = in_channel
for out_channel in mlp:
self.mlp_convs.append(nn.Conv2d(last_channel, out_channel, 1))
if bn:
self.mlp_bns.append(nn.BatchNorm2d(out_channel))
last_channel = out_channel
self.weightnet1 = WeightNet(3, last_channel)
self.weightnet2 = WeightNet(3, last_channel)
self.relu = nn.ReLU(inplace=True) if not use_leaky else nn.LeakyReLU(LEAKY_RATE, inplace=True)
def forward(self, xyz1, xyz2, points1, points2):
"""
Cost Volume layer for Flow Estimation
Input:
xyz1: input points position data, [B, C, N1]
xyz2: input points position data, [B, C, N2]
points1: input points data, [B, D, N1]
points2: input points data, [B, D, N2]
Return:
new_points: upsample points feature data, [B, D', N1]
"""
# import ipdb; ipdb.set_trace()
B, C, N1 = xyz1.shape
_, _, N2 = xyz2.shape
_, D1, _ = points1.shape
_, D2, _ = points2.shape
xyz1 = xyz1.permute(0, 2, 1)
xyz2 = xyz2.permute(0, 2, 1)
points1 = points1.permute(0, 2, 1)
points2 = points2.permute(0, 2, 1)
# point-to-patch Volume
knn_idx = knn_point(self.nsample, xyz2, xyz1) # B, N1, nsample
neighbor_xyz = index_points_group(xyz2, knn_idx)
direction_xyz = neighbor_xyz - xyz1.view(B, N1, 1, C)
grouped_points2 = index_points_group(points2, knn_idx) # B, N1, nsample, D2
grouped_points1 = points1.view(B, N1, 1, D1).repeat(1, 1, self.nsample, 1)
new_points = torch.cat([grouped_points1, grouped_points2, direction_xyz], dim = -1) # B, N1, nsample, D1+D2+3
new_points = new_points.permute(0, 3, 2, 1) # [B, D1+D2+3, nsample, N1]
for i, conv in enumerate(self.mlp_convs):
if self.bn:
bn = self.mlp_bns[i]
new_points = self.relu(bn(conv(new_points)))
else:
new_points = self.relu(conv(new_points))
# weighted sum
weights = self.weightnet1(direction_xyz.permute(0, 3, 2, 1)) # B C nsample N1
point_to_patch_cost = torch.sum(weights * new_points, dim = 2) # B C N
# Patch to Patch Cost
knn_idx = knn_point(self.nsample, xyz1, xyz1) # B, N1, nsample
neighbor_xyz = index_points_group(xyz1, knn_idx)
direction_xyz = neighbor_xyz - xyz1.view(B, N1, 1, C)
# weights for group cost
weights = self.weightnet2(direction_xyz.permute(0, 3, 2, 1)) # B C nsample N1
grouped_point_to_patch_cost = index_points_group(point_to_patch_cost.permute(0, 2, 1), knn_idx) # B, N1, nsample, C
patch_to_patch_cost = torch.sum(weights * grouped_point_to_patch_cost.permute(0, 3, 2, 1), dim = 2) # B C N
return patch_to_patch_cost
class CrossLayer(nn.Module):
def __init__(self, nsample, in_channel, mlp1, mlp2, bn = use_bn, use_leaky = True):
super(CrossLayer,self).__init__()
# self.fe1_layer = FlowEmbedding(radius=radius, nsample=nsample, in_channel = in_channel, mlp=[in_channel,in_channel], pooling=pooling, corr_func=corr_func)
# self.fe2_layer = FlowEmbedding(radius=radius, nsample=nsample, in_channel = in_channel, mlp=[in_channel, out_channel], pooling=pooling, corr_func=corr_func)
# self.flow = nn.Conv1d(out_channel, 3, 1)
self.nsample = nsample
self.bn = bn
self.mlp1_convs = nn.ModuleList()
if bn:
self.mlp1_bns = nn.ModuleList()
last_channel = in_channel * 2 + 3
for out_channel in mlp1:
self.mlp1_convs.append(nn.Conv2d(last_channel, out_channel, 1))
if bn:
self.mlp1_bns.append(nn.BatchNorm2d(out_channel))
last_channel = out_channel
if mlp2 is not None:
self.mlp2_convs = nn.ModuleList()
if bn:
self.mlp2_bns = nn.ModuleList()
last_channel = mlp1[-1] * 2 + 3
for out_channel in mlp2:
self.mlp2_convs.append(nn.Conv2d(last_channel, out_channel, 1))
if bn:
self.mlp2_bns.append(nn.BatchNorm2d(out_channel))
last_channel = out_channel
self.relu = nn.ReLU(inplace=True) if not use_leaky else nn.LeakyReLU(LEAKY_RATE, inplace=True)
def cross(self, xyz1, xyz2, points1, points2, mlp_convs, mlp_bns):
B, C, N1 = xyz1.shape
_, _, N2 = xyz2.shape
_, D1, _ = points1.shape
_, D2, _ = points2.shape
xyz1 = xyz1.permute(0, 2, 1)
xyz2 = xyz2.permute(0, 2, 1)
points1 = points1.permute(0, 2, 1)
points2 = points2.permute(0, 2, 1)
knn_idx = knn_point(self.nsample, xyz2, xyz1) # B, N1, nsample
neighbor_xyz = index_points_group(xyz2, knn_idx)
direction_xyz = neighbor_xyz - xyz1.view(B, N1, 1, C)
grouped_points2 = index_points_group(points2, knn_idx) # B, N1, nsample, D2
grouped_points1 = points1.view(B, N1, 1, D1).repeat(1, 1, self.nsample, 1)
new_points = torch.cat([grouped_points1, grouped_points2, direction_xyz], dim = -1) # B, N1, nsample, D1+D2+3
new_points = new_points.permute(0, 3, 2, 1) # [B, D1+D2+3, nsample, N1]
for i, conv in enumerate(mlp_convs):
if self.bn:
bn = mlp_bns[i]
new_points = self.relu(bn(conv(new_points)))
else:
new_points = self.relu(conv(new_points))
new_points = F.max_pool2d(new_points, (new_points.size(2), 1)).squeeze(2)
return new_points
def forward(self, pc1, pc2, feat1, feat2):
# _, feat1_new = self.fe1_layer(pc1, pc2, feat1, feat2)
# _, feat2_new = self.fe1_layer(pc2, pc1, feat2, feat1)
# _, feat1_final = self.fe2_layer(pc1, pc2, feat1_new, feat2_new)
# flow1 = self.flow(feat1_final)
feat1_new = self.cross(pc1, pc2, feat1, feat2, self.mlp1_convs, self.mlp1_bns if self.bn else None)
feat2_new = self.cross(pc2, pc1, feat2, feat1, self.mlp1_convs, self.mlp1_bns if self.bn else None)
feat1_final = self.cross(pc1, pc2, feat1_new, feat2_new, self.mlp2_convs, self.mlp2_bns if self.bn else None)
return feat1_new, feat2_new, feat1_final
class CrossLayerLight(nn.Module):
def __init__(self, nsample, in_channel, mlp1, mlp2, bn = use_bn, use_leaky = True):
super(CrossLayerLight,self).__init__()
self.nsample = nsample
self.bn = bn
self.pos1 = nn.Conv2d(3, mlp1[0], 1)
self.mlp1 = nn.ModuleList()
last_channel = in_channel
self.cross_t11 = nn.Conv1d(in_channel, mlp1[0], 1)
# self.cross_t12 = Conv1d(in_channel, mlp1[0], bn=bn, use_leaky=use_leaky)
# self.cross_t21 = Conv1d(in_channel, mlp1[0], bn=bn, use_leaky=use_leaky)
self.cross_t22 = nn.Conv1d(in_channel, mlp1[0], 1)
self.bias1 = nn.Parameter(torch.randn((1, mlp1[0], 1, 1)),requires_grad=True)
self.bn1 = nn.BatchNorm2d(mlp1[0]) if bn else nn.Identity()
for i in range(1, len(mlp1)):
self.mlp1.append(Conv2d(mlp1[i-1], mlp1[i], bn=bn, use_leaky=use_leaky))
last_channel = mlp1[i]
self.cross_t1 = nn.Conv1d(mlp1[-1], mlp2[0], 1)
self.cross_t2 = nn.Conv1d(mlp1[-1], mlp2[0], 1)
self.pos2 = nn.Conv2d(3, mlp2[0], 1)
self.bias2 = nn.Parameter(torch.randn((1, mlp2[0], 1, 1)),requires_grad=True)
self.bn2 = nn.BatchNorm2d(mlp2[0]) if bn else nn.Identity()
self.mlp2 = nn.ModuleList()
for i in range(1, len(mlp2)):
self.mlp2.append(Conv2d(mlp2[i-1], mlp2[i], bn=bn, use_leaky=use_leaky))
self.relu = nn.ReLU(inplace=True) if not use_leaky else nn.LeakyReLU(LEAKY_RATE, inplace=True)
def cross(self, xyz1, xyz2, points1, points2, pos, mlp, bn):
B, C, N1 = xyz1.shape
_, _, N2 = xyz2.shape
_, D1, _ = points1.shape
_, D2, _ = points2.shape
xyz1 = xyz1.permute(0, 2, 1)
xyz2 = xyz2.permute(0, 2, 1)
points1 = points1.permute(0, 2, 1)
points2 = points2.permute(0, 2, 1)
knn_idx = knn_point(self.nsample, xyz2, xyz1) # B, N1, nsample
neighbor_xyz = index_points_group(xyz2, knn_idx)
direction_xyz = neighbor_xyz - xyz1.view(B, N1, 1, C)
grouped_points2 = index_points_group(points2, knn_idx).permute(0, 3, 2, 1) # B, N1, nsample, D2
grouped_points1 = points1.view(B, N1, 1, D1).repeat(1, 1, self.nsample, 1).permute(0, 3, 2, 1)
direction_xyz = pos(direction_xyz.permute(0, 3, 2, 1))
new_points = self.relu(bn(grouped_points2 + grouped_points1 + direction_xyz))# B, N1, nsample, D1+D2+3
for i, conv in enumerate(mlp):
new_points = conv(new_points)
new_points = F.max_pool2d(new_points, (new_points.size(2), 1)).squeeze(2)
return new_points
def forward(self, pc1, pc2, feat1, feat2):
# _, feat1_new = self.fe1_layer(pc1, pc2, feat1, feat2)
# _, feat2_new = self.fe1_layer(pc2, pc1, feat2, feat1)
# _, feat1_final = self.fe2_layer(pc1, pc2, feat1_new, feat2_new)
# flow1 = self.flow(feat1_final)
feat1_new = self.cross(pc1, pc2, self.cross_t11(feat1), self.cross_t22(feat2), self.pos1, self.mlp1, self.bn1)
feat1_new = self.cross_t1(feat1_new)
feat2_new = self.cross(pc2, pc1, self.cross_t11(feat2), self.cross_t22(feat1), self.pos1, self.mlp1, self.bn1)
feat2_new = self.cross_t2(feat2_new)
feat1_final = self.cross(pc1, pc2, feat1_new, feat2_new, self.pos2, self.mlp2, self.bn2)
return feat1_new, feat2_new, feat1_final
class PointWarping(nn.Module):
def forward(self, xyz1, xyz2, flow1 = None):
if flow1 is None:
return xyz2
# move xyz1 to xyz2'
xyz1_to_2 = xyz1 + flow1
# interpolate flow
B, C, N1 = xyz1.shape
_, _, N2 = xyz2.shape
xyz1_to_2 = xyz1_to_2.permute(0, 2, 1) # B 3 N1
xyz2 = xyz2.permute(0, 2, 1) # B 3 N2
flow1 = flow1.permute(0, 2, 1)
# 3 nearest neightbor & use 1/dist as the weights
knn_idx = knn_point(3, xyz1_to_2, xyz2) # group flow 1 around points 2
grouped_xyz_norm = index_points_group(xyz1_to_2, knn_idx) - xyz2.view(B, N2, 1, C) # B N2 3 C
dist = torch.norm(grouped_xyz_norm, dim = 3).clamp(min = 1e-10)
norm = torch.sum(1.0 / dist, dim = 2, keepdim = True)
weight = (1.0 / dist) / norm
# from points 2 to group flow 1 and got weight, and use these weights and grouped flow to wrap a inverse flow and flow back
grouped_flow1 = index_points_group(flow1, knn_idx)
flow2 = torch.sum(weight.view(B, N2, 3, 1) * grouped_flow1, dim = 2)
warped_xyz2 = (xyz2 - flow2).permute(0, 2, 1) # B 3 N2
return warped_xyz2
class UpsampleFlow(nn.Module):
def forward(self, xyz, sparse_xyz, sparse_flow):
#import ipdb; ipdb.set_trace()
B, C, N = xyz.shape
_, _, S = sparse_xyz.shape
xyz = xyz.permute(0, 2, 1) # B N 3
sparse_xyz = sparse_xyz.permute(0, 2, 1) # B S 3
sparse_flow = sparse_flow.permute(0, 2, 1) # B S 3
# 3 nearest neightbor from dense around sparse & use 1/dist as the weights the same
knn_idx = knn_point(3, sparse_xyz, xyz)
grouped_xyz_norm = index_points_group(sparse_xyz, knn_idx) - xyz.view(B, N, 1, C)
dist = torch.norm(grouped_xyz_norm, dim = 3).clamp(min = 1e-10)
norm = torch.sum(1.0 / dist, dim = 2, keepdim = True)
weight = (1.0 / dist) / norm
grouped_flow = index_points_group(sparse_flow, knn_idx)
dense_flow = torch.sum(weight.view(B, N, 3, 1) * grouped_flow, dim = 2).permute(0, 2, 1)
return dense_flow
class SceneFlowEstimatorPointConv(nn.Module):
def __init__(self, feat_ch, cost_ch, flow_ch = 3, channels = [128, 128], mlp = [128, 64], neighbors = 9, clamp = [-200, 200], use_leaky = True):
super(SceneFlowEstimatorPointConv, self).__init__()
self.clamp = clamp
self.use_leaky = use_leaky
self.pointconv_list = nn.ModuleList()
last_channel = feat_ch + cost_ch + flow_ch
for _, ch_out in enumerate(channels):
pointconv = PointConv(neighbors, last_channel + 3, ch_out, bn = True, use_leaky = True)
self.pointconv_list.append(pointconv)
last_channel = ch_out
self.mlp_convs = nn.ModuleList()
for _, ch_out in enumerate(mlp):
self.mlp_convs.append(Conv1d(last_channel, ch_out))
last_channel = ch_out
self.fc = nn.Conv1d(last_channel, 3, 1)
def forward(self, xyz, feats, cost_volume, flow = None):
'''
feats: B C1 N
cost_volume: B C2 N
flow: B 3 N
'''
if flow is None:
new_points = torch.cat([feats, cost_volume], dim = 1)
else:
new_points = torch.cat([feats, cost_volume, flow], dim = 1)
for _, pointconv in enumerate(self.pointconv_list):
new_points = pointconv(xyz, new_points)
for conv in self.mlp_convs:
new_points = conv(new_points)
flow = self.fc(new_points)
return new_points, flow.clamp(self.clamp[0], self.clamp[1])
class SceneFlowEstimatorResidual(nn.Module):
def __init__(self, feat_ch, cost_ch, flow_ch = 3, channels = [128, 128], mlp = [128, 64], neighbors = 9, clamp = [-200, 200], use_leaky = True):
super(SceneFlowEstimatorResidual, self).__init__()
self.clamp = clamp
self.use_leaky = use_leaky
self.pointconv_list = nn.ModuleList()
last_channel = feat_ch + cost_ch
for _, ch_out in enumerate(channels):
pointconv = PointConv(neighbors, last_channel + 3, ch_out, bn = True, use_leaky = True)
self.pointconv_list.append(pointconv)
last_channel = ch_out
self.mlp_convs = nn.ModuleList()
for _, ch_out in enumerate(mlp):
self.mlp_convs.append(Conv1d(last_channel, ch_out))
last_channel = ch_out
self.fc = nn.Conv1d(last_channel, 3, 1)
def forward(self, xyz, feats, cost_volume, flow = None):
'''
feats: B C1 N
cost_volume: B C2 N
flow: B 3 N
'''
new_points = torch.cat([feats, cost_volume], dim = 1)
for _, pointconv in enumerate(self.pointconv_list):
new_points = pointconv(xyz, new_points)
for conv in self.mlp_convs:
new_points = conv(new_points)
flow_local = self.fc(new_points).clamp(self.clamp[0], self.clamp[1])
if flow is None:
flow = flow_local
else:
flow = flow_local + flow
return new_points, flow