-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_attentivecylinder3d.py
1425 lines (1118 loc) · 59.3 KB
/
train_attentivecylinder3d.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
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# coding: utf-8
#
# ## Dependencies
# ## Imports
# In[ ]:
import os
import csv
import time
import argparse
import os
import sys
sys.path.append('.')
import numpy as np
import numba as nb
from tqdm import tqdm
import multiprocessing
import torch
import torch.optim as optim
from torch.autograd import Function
import torch.nn as nn
import torch.nn.functional as F
import torch_scatter
import spconv.pytorch as spconv
import MinkowskiEngine as ME
from utils.metric_util import per_class_iu, fast_hist_crop
from utils.lovasz_losses import lovasz_softmax
from dataloader.pc_dataset import get_SemKITTI_label_name
from dataloader.dataset_semantickitti import get_model_class as get_model_class_dataset, collate_fn_BEV
from dataloader.pc_dataset import get_pc_model_class
from config.config import load_config_data
from utils.load_save_util import load_checkpoint
from models_.modules.common import ConvType, NormType, get_norm, conv, get_nonlinearity_fn
from models_.modules.resnet_block import *
import warnings
warnings.filterwarnings("ignore")
#print(torch.__version__)
pytorch_device = torch.device('cuda:0')
# ## Cylinder3D - Architecture -First Part -> Second Part
# ![image.png](attachment:1422ef7a-b0cb-48c4-be9d-d4c369cfe36d.png)
# In[ ]:
REGISTERED_MODELS_CLASSES = {}
def register_model(cls, name=None):
global REGISTERED_MODELS_CLASSES
if name is None:
name = cls.__name__
assert name not in REGISTERED_MODELS_CLASSES, f"exist class: {REGISTERED_MODELS_CLASSES}"
REGISTERED_MODELS_CLASSES[name] = cls
return cls
def get_model_class_c3d(name):
global REGISTERED_MODELS_CLASSES
assert name in REGISTERED_MODELS_CLASSES, f"available class: {REGISTERED_MODELS_CLASSES}"
return REGISTERED_MODELS_CLASSES[name]
@register_model
class cylinder_asym(nn.Module):
def __init__(self,
cylin_model,
segmentator_spconv,
sparse_shape,
):
super().__init__()
self.name = "cylinder_asym"
self.cylinder_3d_generator = cylin_model # Network Architecture - First Part
self.cylinder_3d_spconv_seg = segmentator_spconv # Network Architecture - Second Part
self.sparse_shape = sparse_shape
def forward(self, train_pt_fea_ten, train_vox_ten, batch_size, iter):
coords, features_3d = self.cylinder_3d_generator(train_pt_fea_ten, train_vox_ten) # Network Architecture - First Part
spatial_features = self.cylinder_3d_spconv_seg(features_3d, coords, batch_size, iter) # Network Architecture - Second Part
return spatial_features
# ## Segmentator 3D Asymmetric Sparse Convolution 3D
#
# ![image.png](attachment:498f5b6b-ea7f-4eaf-bd2b-9a6b90cc833a.png)
# In[ ]:
##########################################################################################
# ASYMMETRIC CONVOLUTIONS #
##########################################################################################
def conv3x3(in_planes, out_planes, stride=1, indice_key=None):
return spconv.SubMConv3d(in_planes, out_planes, kernel_size=3, stride=stride,
padding=1, bias=False, indice_key=indice_key)
def conv1x3(in_planes, out_planes, stride=1, indice_key=None):
return spconv.SubMConv3d(in_planes, out_planes, kernel_size=(1, 3, 3), stride=stride,
padding=(0, 1, 1), bias=False, indice_key=indice_key)
def conv1x1x3(in_planes, out_planes, stride=1, indice_key=None):
return spconv.SubMConv3d(in_planes, out_planes, kernel_size=(1, 1, 3), stride=stride,
padding=(0, 0, 1), bias=False, indice_key=indice_key)
def conv1x3x1(in_planes, out_planes, stride=1, indice_key=None):
return spconv.SubMConv3d(in_planes, out_planes, kernel_size=(1, 3, 1), stride=stride,
padding=(0, 1, 0), bias=False, indice_key=indice_key)
def conv3x1x1(in_planes, out_planes, stride=1, indice_key=None):
return spconv.SubMConv3d(in_planes, out_planes, kernel_size=(3, 1, 1), stride=stride,
padding=(1, 0, 0), bias=False, indice_key=indice_key)
def conv3x1(in_planes, out_planes, stride=1, indice_key=None):
return spconv.SubMConv3d(in_planes, out_planes, kernel_size=(3, 1, 3), stride=stride,
padding=(1, 0, 1), bias=False, indice_key=indice_key)
def conv1x1(in_planes, out_planes, stride=1, indice_key=None):
return spconv.SubMConv3d(in_planes, out_planes, kernel_size=1, stride=stride,
padding=1, bias=False, indice_key=indice_key)
##########################################################################################
# ResContextBlock - Used before going into the first DownSample Block. #
##########################################################################################
class ResContextBlock(nn.Module):
def __init__(self, in_filters, out_filters, kernel_size=(3, 3, 3), stride=1, indice_key=None):
super(ResContextBlock, self).__init__()
self.conv1 = conv1x3(in_filters, out_filters, indice_key=indice_key + "bef")
self.bn0 = nn.BatchNorm1d(out_filters)
self.act1 = nn.LeakyReLU()
#self.conv1_2 = conv3x1(out_filters, out_filters, indice_key=indice_key + "bef")
self.conv1_2 = conv1x3(out_filters, out_filters, indice_key=indice_key + "bef")
self.bn0_2 = nn.BatchNorm1d(out_filters)
self.act1_2 = nn.LeakyReLU()
self.conv2 = conv3x1(in_filters, out_filters, indice_key=indice_key + "bef")
self.act2 = nn.LeakyReLU()
self.bn1 = nn.BatchNorm1d(out_filters)
#self.conv3 = conv1x3(out_filters, out_filters, indice_key=indice_key + "bef")
self.conv3 = conv3x1(out_filters, out_filters, indice_key=indice_key + "bef")
self.act3 = nn.LeakyReLU()
self.bn2 = nn.BatchNorm1d(out_filters)
self.weight_initialization()
def weight_initialization(self):
for m in self.modules():
if isinstance(m, nn.BatchNorm1d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
def forward(self, x):
shortcut = self.conv1(x)
shortcut = shortcut.replace_feature(self.act1(shortcut.features))
shortcut = shortcut.replace_feature(self.bn0(shortcut.features))
shortcut = self.conv1_2(shortcut)
shortcut = shortcut.replace_feature(self.act1_2(shortcut.features))
shortcut = shortcut.replace_feature(self.bn0_2(shortcut.features))
resA = self.conv2(x)
resA = resA.replace_feature(self.act2(resA.features))
reaA = resA.replace_feature(self.bn1(resA.features))
resA = self.conv3(resA)
resA = resA.replace_feature(self.act3(resA.features))
resA = resA.replace_feature(self.bn2(resA.features))
resA = resA.replace_feature(resA.features + shortcut.features)
return resA
##########################################################################################
# ResBlock - Used to create DownSample Blocks. There are originally 4 DownSample Blocks. #
##########################################################################################
class ResBlock(nn.Module):
def __init__(self, in_filters, out_filters, dropout_rate, kernel_size=(3, 3, 3), stride=1,
pooling=True, drop_out=True, height_pooling=False, indice_key=None):
super(ResBlock, self).__init__()
self.pooling = pooling
self.drop_out = drop_out
self.conv1 = conv3x1(in_filters, out_filters, indice_key=indice_key + "bef")
self.act1 = nn.LeakyReLU()
self.bn0 = nn.BatchNorm1d(out_filters)
#self.conv1_2 = conv1x3(out_filters, out_filters, indice_key=indice_key + "bef")
self.conv1_2 = conv3x1(out_filters, out_filters, indice_key=indice_key + "bef")
self.act1_2 = nn.LeakyReLU()
self.bn0_2 = nn.BatchNorm1d(out_filters)
self.conv2 = conv1x3(in_filters, out_filters, indice_key=indice_key + "bef")
self.act2 = nn.LeakyReLU()
self.bn1 = nn.BatchNorm1d(out_filters)
#self.conv3 = conv3x1(out_filters, out_filters, indice_key=indice_key + "bef")
self.conv3 = conv1x3(out_filters, out_filters, indice_key=indice_key + "bef")
self.act3 = nn.LeakyReLU()
self.bn2 = nn.BatchNorm1d(out_filters)
if pooling:
if height_pooling:
self.pool = spconv.SparseConv3d(out_filters, out_filters, kernel_size=3, stride=2,
padding=1, indice_key=indice_key, bias=False)
else:
self.pool = spconv.SparseConv3d(out_filters, out_filters, kernel_size=3, stride=(2, 2, 1),
padding=1, indice_key=indice_key, bias=False)
self.weight_initialization()
def weight_initialization(self):
for m in self.modules():
if isinstance(m, nn.BatchNorm1d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
def forward(self, x):
shortcut = self.conv1(x)
shortcut = shortcut.replace_feature(self.act1(shortcut.features))
shortcut = shortcut.replace_feature(self.bn0(shortcut.features))
shortcut = self.conv1_2(shortcut)
shortcut = shortcut.replace_feature(self.act1_2(shortcut.features))
shortcut = shortcut.replace_feature(self.bn0_2(shortcut.features))
resA = self.conv2(x)
resA = resA.replace_feature(self.act2(resA.features))
resA = resA.replace_feature(self.bn1(resA.features))
resA = self.conv3(resA)
resA = resA.replace_feature(self.act3(resA.features))
resA = resA.replace_feature(self.bn2(resA.features))
resA = resA.replace_feature(resA.features + shortcut.features)
if self.pooling:
resB = self.pool(resA)
return resB, resA
else:
return resA
##########################################################################################
# UpSample Block. There are originally 4 UpSample Blocks #
##########################################################################################
class UpBlock(nn.Module):
def __init__(self, in_filters, out_filters, kernel_size=(3, 3, 3), indice_key=None, up_key=None):
super(UpBlock, self).__init__()
# self.drop_out = drop_out
self.trans_dilao = conv3x3(in_filters, out_filters, indice_key=indice_key + "new_up")
self.trans_act = nn.LeakyReLU()
self.trans_bn = nn.BatchNorm1d(out_filters)
self.conv1 = conv1x3(out_filters, out_filters, indice_key=indice_key)
self.act1 = nn.LeakyReLU()
self.bn1 = nn.BatchNorm1d(out_filters)
#self.conv3 = conv3x1(out_filters, out_filters, indice_key=indice_key + "bef")
self.conv2 = conv1x3(out_filters, out_filters, indice_key=indice_key)
self.act2 = nn.LeakyReLU()
self.bn2 = nn.BatchNorm1d(out_filters)
#self.conv3 = conv3x3(out_filters, out_filters, indice_key=indice_key)
self.conv3 = conv1x3(out_filters, out_filters, indice_key=indice_key)
self.act3 = nn.LeakyReLU()
self.bn3 = nn.BatchNorm1d(out_filters)
# self.dropout3 = nn.Dropout3d(p=dropout_rate)
self.up_subm = spconv.SparseInverseConv3d(out_filters, out_filters, kernel_size=3, indice_key=up_key,
bias=False)
self.weight_initialization()
def weight_initialization(self):
for m in self.modules():
if isinstance(m, nn.BatchNorm1d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
def forward(self, x, skip):
upA = self.trans_dilao(x)
upA = upA.replace_feature(self.trans_act(upA.features))
upA = upA.replace_feature(self.trans_bn(upA.features))
## upsample
upA = self.up_subm(upA)
upA = upA.replace_feature(upA.features + skip.features)
upE = self.conv1(upA)
upE = upE.replace_feature(self.act1(upE.features))
upE = upE.replace_feature(self.bn1(upE.features))
upE = self.conv2(upE)
upE = upE.replace_feature(self.act2(upE.features))
upE = upE.replace_feature(self.bn2(upE.features))
upE = self.conv3(upE)
upE = upE.replace_feature(self.act3(upE.features))
upE = upE.replace_feature(self.bn3(upE.features))
return upE
##########################################################################################
# DDCM - Figure 4 (right hand side) and architecture Figure: after last U block #
##########################################################################################
class ReconBlock(nn.Module):
def __init__(self, in_filters, out_filters, kernel_size=(3, 3, 3), stride=1, indice_key=None):
super(ReconBlock, self).__init__()
self.conv1 = conv3x1x1(in_filters, out_filters, indice_key=indice_key + "bef")
self.bn0 = nn.BatchNorm1d(out_filters)
self.act1 = nn.Sigmoid()
self.conv1_2 = conv1x3x1(in_filters, out_filters, indice_key=indice_key + "bef")
self.bn0_2 = nn.BatchNorm1d(out_filters)
self.act1_2 = nn.Sigmoid()
self.conv1_3 = conv1x1x3(in_filters, out_filters, indice_key=indice_key + "bef")
self.bn0_3 = nn.BatchNorm1d(out_filters)
self.act1_3 = nn.Sigmoid()
def forward(self, x):
shortcut = self.conv1(x)
shortcut = shortcut.replace_feature(self.bn0(shortcut.features))
shortcut = shortcut.replace_feature(self.act1(shortcut.features))
shortcut2 = self.conv1_2(x)
shortcut2 = shortcut2.replace_feature(self.bn0_2(shortcut2.features))
shortcut2 = shortcut2.replace_feature(self.act1_2(shortcut2.features))
shortcut3 = self.conv1_3(x)
shortcut3 = shortcut.replace_feature(self.bn0_3(shortcut3.features))
shortcut3 = shortcut3.replace_feature(self.act1_3(shortcut3.features))
shortcut = shortcut.replace_feature(shortcut.features + shortcut2.features + shortcut3.features)
shortcut = shortcut.replace_feature(shortcut.features * x.features)
return shortcut
# ## Transformer Blocks from CodedVTR
# ![image-2.png](attachment:image-2.png)
# In[ ]:
# In[ ]:
##########################################################################################
# Transformer Block from CodedVTR #
##########################################################################################
class CodedVTRBlock(nn.Module): # ddp could not contain unused parameter, so donnot inherit from TRBlock
expansion=1
def __init__(self,
inplanes,
planes,
stride=1,
dilation=1,
downsample=None,
conv_type=ConvType.HYPERCUBE,
nonlinearity_type='ReLU',
bn_momentum=0.1,
D=3):
super(CodedVTRBlock, self).__init__()
self.inplanes = inplanes
self.planes = planes
'''
The Codebook-based Attention: for original feature [1, dim], generate the attnmap [K,h];
then do dotproduct with codebook [D,M,K,dim], get choice [D,M], use it to aggregate the codebook;
apply on value [1, dim] to generate final feature
------------------
inplanes/outplanes: the feature dim
expansion: the width expansion
qk_type:
- conv
- dotproduct(pairwise)
conv_v: use conv or linear for gen value
vec_dim: the attn_map feature dim
H: head num
D,M - codebook size
K - neighbor-size
The Geometry-based Attention
------------------
custom-kernel: use CROSS-like / different dilations of neighbor
geo-shape: whether apply geo-shape for codebook elements
temp - the softmax temperature
------------------
'''
self.expansion = 2
self.qk_type = 'conv' # ['conv','pairwise']
self.conv_v = True
self.top_k_choice = False
self.temp_ = 2.e0 # the initial temp
# === some additonal tricks ===
self.skip_choice = False # only_used in debug mode, notice that this mode contains unused params, so could not support ddp for now
self.geo_shape = True # used in v5
self.sparse_pattern_reg = True # used in v5
if self.inplanes != self.planes:
self.linear_top = MinkoskiConvBNReLU(inplanes, planes, kernel_size=1)
self.downsample = ME.MinkowskiConvolution(inplanes, planes, kernel_size=1, dimension=3)
if self.conv_v == True:
self.v = nn.Sequential(
MinkoskiConvBNReLU(planes, planes, kernel_size=3),
MinkoskiConvBNReLU(planes, planes*self.expansion, kernel_size=1),
)
else:
self.v = MinkoskiConvBNReLU(planes, planes*self.expansion, kernel_size=1)
self.codebook = nn.ModuleList([])
self.D = 3
self.M = 8
self.CUSTOM_KERNEL = True
if self.CUSTOM_KERNEL: # distinct geometric shape for codebook elements
kgargs0 = {
"kernel_size": 3,
"stride": 1,
"dilation": 2,
# "region_type":ME.RegionType.HYPER_CROSS,
"region_type":ME.RegionType.HYPER_CUBE,
"dimension": 3,
}
kgargs1 = {
"kernel_size": 3,
"stride": 1,
"dilation": 1,
"region_type":ME.RegionType.HYPER_CUBE,
"dimension": 3,
}
kgargs2 = {
"kernel_size": 3,
"stride": 1,
"dilation": 3,
"region_type":ME.RegionType.HYPER_CUBE,
"dimension": 3,
}
self.kgargs = [kgargs0, kgargs1, kgargs2] # len should align with M
kgs = [ME.KernelGenerator(**kg) for kg in self.kgargs]
for i_ in range(self.D):
self.codebook.append(
nn.Sequential(
ME.MinkowskiChannelwiseConvolution(planes*self.expansion, kernel_size=3, dimension=3, kernel_generator=kgs[i_]),
)
)
if not self.skip_choice:
if self.qk_type == 'conv':
self.q = nn.ModuleList([])
for i_ in range(self.D):
self.q.append(
nn.Sequential(
ME.MinkowskiConvolution(planes,self.M, dimension=3, kernel_generator=kgs[i_]),
)
)
elif self.qk_type == 'pairwise':
self.q = MinkoskiConvBNReLU(planes, self.M, kernel_size=1)
# self.pos_enc = MinkoskiConvBNReLU(3, self.M, kernel_size=1)
else:
kgargs0 = {
"kernel_size": 3,
"stride": 1,
"dilation": 1,
"region_type":ME.RegionType.HYPER_CUBE,
"dimension": 3,
}
self.kgargs = [kgargs0]*self.D
for i_ in range(self.D):
self.codebook.append(
nn.Sequential(
ME.MinkowskiConvolution(planes,self.M, dimension=3, kernel_generator=kgs[i_]),
)
)
if not self.skip_choice:
if self.qk_type == 'conv': # since conv already contains the neighbor info, so no pos_enc
self.q = nn.Sequential(
ME.MinkowskiConvolution(planes, self.M, kernel_size=3,dimension=3),
)
elif self.qk_type == 'pairwise':
self.q = MinkoskiConvBNReLU(planes, self.M, kernel_size=1)
else:
raise NotImplementedError
if self.geo_shape:
# 3 masks
# each contains masks at differnt stride
# mask1 = torch.load('./plot/final/sparse_masks.pth')
mask0 = np.array([
[0,1,3,6,7,13],
[1,2,9,14,15,17],
[0,5,6,7,8,10],
[17,19,20,22,23],
])
mask1 = np.array([
[10,11,12,20,21,22],
[1,2,3,4,5,6,10,21,20],
[3,4,5,6,7,8,9,10,11],
[17,18,19,20,22,23,24],
])
mask2 = np.array([
[0,5,9,13,19,22],
[1,3,7,8,11,16,20],
[4,6,11,12,18,24,25],
[5,6,10,14,19,23],
])
self.codebook_masks = [mask0, mask1, mask2]
with torch.no_grad():
for _ in range(len(self.codebook)):
new_kernel = self.codebook[_][0].kernel
k_, dim_ = new_kernel.shape
if len(self.codebook_masks[_])>0:
assert self.M % len(self.codebook_masks[_]) == 0
if len(self.codebook_masks[_])>1:
dim_per_mask = dim_ // len(self.codebook_masks[_])
else:
dim_per_mask = dim_
for m_ in range(len(self.codebook_masks[_])):
new_kernel[self.codebook_masks[_][m_],dim_per_mask*m_:dim_per_mask*(m_+1)] = 0
self.codebook[_][0].kernel = nn.Parameter(self.codebook[_][0].kernel)
# codebook_weight = torch.stack([m[0].kernel for m in self.codebook])
self.out_bn_relu = nn.Sequential(
ME.MinkowskiConvolution(planes*self.expansion, planes, kernel_size=1, dimension=3),
ME.MinkowskiBatchNorm(planes),
ME.MinkowskiReLU(),
)
def expand_dim(self,x):
# x shold be like [N, vec_dim]; [N, vec_dim, M]
# expand em as [N, dim]; [N, dim, M]
assert x.shape[1] == self.M
if len(x.shape) == 2:
N, dim = x.shape
x = x.unsqueeze(2).expand(-1,-1,self.planes*self.expansion//self.M).reshape(-1,self.planes*self.expansion)
elif len(x.shape) == 3:
N, dim, M = x.shape
x = x.unsqueeze(2).expand(-1,-1,self.planes*self.expansion//self.M, -1).reshape(-1,self.planes*self.expansion,M)
return x
def get_sparse_pattern(self, x, choice, type_=1):
# FORMULA 1: get codebook kernel shapes and directly use the sparse-pattern matching
# as the guidance of choice
if type_ == 1:
sparse_patterns= [] # [M]
for m_ in range(self.D):
kgargs = self.kgargs[m_]
if 'dimension' in kgargs.keys():
del kgargs['dimension']
neis_d = x.coordinate_manager.get_kernel_map(x.coordinate_map_key,
x.coordinate_map_key,
**kgargs
)
N = x.C.shape[0]
# its easy to get how many matched elements of cur-point & kernel
# but the kernel shape is hard to be flexible, like i need to index the lower-right part
if self.geo_shape:
# only when codebook-prior is given, each point would have different pattern
sparse_pattern_ = torch.zeros([N, self.M], device=x.device)
else:
sparse_pattern_ = torch.zeros([N, 1], device=x.device)
if hasattr(self, "codebook_masks"):
# TODO: acquire the stride corresponding
cur_mask = self.codebook_masks[m_]
else:
cur_mask = []
cur_k = len(neis_d.keys())
for k_ in range(cur_k):
if not k_ in neis_d.keys():
continue
if len(cur_mask)>0:
for i_ in range(len(cur_mask)):
if k_ in cur_mask[i_]: # for masked k
continue
else:
sparse_pattern_[neis_d[k_][0].long(),i_] +=1
else:
sparse_pattern_[neis_d[k_][0].long(),:] +=1
if len(cur_mask)>0:
for i_ in range(len(cur_mask)):
sparse_pattern_[:,i_] = sparse_pattern_[:,i_] / (cur_k - len(cur_mask[i_]))
if cur_k == len(cur_mask[i_]): # assert zero division, empty kernel
import ipdb; ipdb.set_trace()
else:
sparse_pattern_ = sparse_pattern_ / cur_k
sparse_patterns.append(sparse_pattern_)
sparse_patterns = torch.stack(sparse_patterns, dim=-1) # [N,D,M]
# Reg Type1: encourage the kernel to lean to map with more matching neighbors
temp_ = 0.2
eps = 1.e-3
sparse_patterns = F.softmax((F.softmax((sparse_patterns+eps)/temp_, dim=1)+eps)/temp_, dim=-1) # [N. vec-dim. M]
self.register_buffer("sparse_patterns",sparse_patterns)
return choice*self.sparse_patterns
else:
# formula 2: MultiScale Estimation of how sparse a point is
# apply softmax in the normalized N points dimension
# calc the relative sparsity distance to many centers as regs
raise NotImplementedError
def schedule_update(self, iter_=None):
'''
some schedulable params
'''
# ======= the temp annealing for choice =============
# exponential temp annealing, best results in v3, now used in v5
self.temp = (self.temp_)**(1-iter_) # start from the temp, end with 0.00000....
# linear temp annealing - had worse results than upper annealing - this was v4 test
# as in: https://www.researchgate.net/publication/337856246_Dynamic_Convolution_Attention_over_Convolution_Kernels
#if (self.temp_ - 3 * iter_ >= 0):
# self.temp = self.temp_ - 3 * iter_ # start from the temp, end with 1
#else:
# self.temp = 1
if self.skip_choice == True and iter_> 0.1:
self.skip_choice = False
print('SkipChoice Warmup Done, Start training choice qk')
if self.skip_choice == False and not hasattr(self, "q"):
self.q = nn.Sequential(
ME.MinkowskiConvolution(self.planes, self.M, kernel_size=3,dimension=3),
ME.MinkowskiBatchNorm(self.M),
)
self.q.to(self.codebook[0][0].kernel.device)
pass
# ========= Temperature Annealing ==============
#if not hasattr(self, 'temp0'):
# self.temp0 = self.temp
#self.temp = self.temp0*(0.01)**(iter_)
def forward(self, x, iter_=None, aux=None):
'''
For each dilation(D=3), different d have different kernel shape and different Ks, e.g., cube-shape kernel has k=27, cross-shaped has k=7
1st do qk projection: [N, dim, K] ()
- conv: directly use conv neighbor aggregation(extra params), output: [N, H]
- pairwise: use linear mapping, then gather neighbor & dotproduct. output: [N, H, K] -> [N, H]
2nd: q_ dot product with Codebook(M set of conv weights): [N, H, M] -> [N, dim, M], the apply softmax to get choice of [D, M]
3rd: use choice: [D, M] to aggregate M codebook elements(channel-wise convs) for each point, then apply the coedbook(through channel-wise conv on value)
'''
self.register_buffer('coord_map', x.C)
self.schedule_update(iter_)
# align the channel for the decoder that concat the input
if self.planes != self.inplanes:
res = self.downsample(x)
x = self.linear_top(x)
else:
res = x
# generate the value
v_ = self.v(x)
# generate the qk
if self.skip_choice:
pass
else: # no skip choice
if self.qk_type == 'conv':
if not self.CUSTOM_KERNEL:
q_ = self.q(x)
q_f = self.expand_dim(q_.F)
q_= ME.SparseTensor(features=q_f, coordinate_map_key=q_.coordinate_map_key, coordinate_manager=q_.coordinate_manager) # [N, dim]
N, dim = q_.F.shape
qs = [q_]*self.D
else:
qs = []
for _ in range(self.D):
q_ = self.q[_](x)
q_f =self.expand_dim(q_.F)
qs.append(
ME.SparseTensor(features=q_f, coordinate_map_key=q_.coordinate_map_key, coordinate_manager=q_.coordinate_manager) # [N, dim]
)
N, dim = q_f.shape
# get dot-product of codebook-weight & q_
choice = []
out = []
for _ in range(self.D):
self.codebook[_][0].kernel.requires_grad = False # detach the grad from choice to codebook elements
choice_ = self.codebook[_](qs[_])
choice.append(choice_.F.reshape(
[choice_.shape[0], self.M, self.planes*self.expansion // self.M]
).sum(-1)
)
choice = torch.stack(choice, dim=-1)
eps = 1.e-3
if self.D > 1: # if M==1, skip softmax since there is only 1 value
choice = F.softmax((choice)/self.temp, dim=-1) # [N, vec_dim, M]
else:
pass
# attn_map = torch.stack([self.codebook[_][0].kernel for _ in range(self.D) ], dim=0) # [M. K], in some case(CUSTOM_KERNEL)
attn_map = torch.cat([self.codebook[_][0].kernel for _ in range(self.D)],dim=0) # [M. K]
self.register_buffer('attn_map', attn_map)
self.register_buffer('choice_map', choice)
elif self.qk_type == 'pairwise':
q_ = self.q(x)
q_f = q_.F
N, _ = q_.F.shape
choices = []
for i_m, kg in enumerate(self.kgargs): # iter over M
if 'dimension' in kg.keys():
del kg['dimension']
neis_d = q_.coordinate_manager.get_kernel_map(q_.coordinate_map_key,
q_.coordinate_map_key,
**kg
)
choice = []
for k_ in range(len(neis_d.keys())):
if not k_ in neis_d.keys():
continue
neis_ = torch.gather(q_.F, dim=0, index=neis_d[k_][0].reshape(-1,1).expand(-1,self.M).long())
neis = torch.zeros(N,self.M, device=q_.F.device) # DEBUG: not sure if needs decalre every time
neis.scatter_(dim=0, index=neis_d[k_][1].reshape(-1,1).expand(-1,self.M).long(), src=neis_)
sparse_mask_cur_k = (neis.abs().sum(-1) > 0).float()
neis = neis*(q_.F*sparse_mask_cur_k.unsqueeze(-1).expand(-1, self.M))
neis = neis*sparse_mask_cur_k.unsqueeze(-1).expand(-1, self.M)
out_cur_k = self.expand_dim(neis)*self.codebook[i_m][0].kernel[k_].unsqueeze(0)
out_cur_k = out_cur_k.sum(1) # [N]
choice.append(out_cur_k)
choice = torch.stack(choice, dim=-1) # [N,K]
choice = F.softmax(choice/self.temp, dim=-1).sum(-1)
choices.append(choice) # [N]
choices = torch.stack(choices, dim=-1)
choices = F.softmax(choices/self.temp, dim=-1) # [N,M]
choice = choices.unsqueeze(1).expand(-1, self.M, -1) # [N, dim, M]
self.register_buffer('choice_map', choices)
if self.sparse_pattern_reg:
choice = self.get_sparse_pattern(x, choice)
if self.skip_choice:
N, dim = v_.shape
out = []
for _ in range(self.D):
self.codebook[_][0].kernel.requires_grad = True
out_ = self.codebook[_](v_)
out.append(out_.F)
out = torch.stack(out, dim=-1)
out = out.sum(-1)
elif self.top_k_choice:
assert self.M == 1 # same point use the same choice
out = torch.zeros([N,dim,self.top_k_choice], device=x.device)
choice_topk = torch.topk(choice, self.top_k_choice, dim=-1)[0] # shape [N,dim]
choice_topk_idx = torch.topk(choice, self.top_k_choice, dim=-1)[1][:,0,:] # shape [N]
for _ in range(self.D):
self.codebook[_][0].kernel.requires_grad = True
# DEV: split points for different choice
# however, if choice has the channle freedom
# could not handle
cur_out_ = self.codebook[_](v_) # the conv
for top_ in range(self.top_k_choice):
choice_idx = torch.where(choice_topk_idx[:,top_] == _)[0]
# cur_v_ = v_.features_at_coordinates(v_.C[choice_idx,:].float())
if len(choice_idx) > 1:
# cur_v_ = ME.SparseTensor(
# features=v_.F[choice_idx,:],
# coordinates=v_.C[choice_idx,:],
# coordinate_map_key=v_.coordinate_map_key,
# coordinate_manager=v_.coordinate_manager
# )
out[:,:,top_].scatter_(
src=cur_out_.F[choice_idx,:]*choice_topk[choice_idx,:,top_],
index=choice_idx.unsqueeze(-1).repeat(1,dim),
dim=0)
else:
pass
out = out.sum(-1)
else:
# normal-case: apply the attn_weight aggregation with the channelwiseConvolution
out = torch.zeros([N, self.planes*self.expansion], device=v_.device)
for _ in range(self.D):
self.codebook[_][0].kernel.requires_grad = True
out_ = self.codebook[_](v_)
out += out_.F*self.expand_dim(choice[:,:,_])
out = out.reshape([N, self.planes*self.expansion])
out = ME.SparseTensor(features=out, coordinate_map_key=x.coordinate_map_key, coordinate_manager=x.coordinate_manager)
out = self.out_bn_relu(out)
out = out + res
return out
# argumentation
# 1. rechenaufwand -> wir haben encoder/decoder -> wichtigste attn blöcke sind in der mitte weil features most condensed
# warum im decoder zuerst streichen: einzelpunktinformationen im encoder noch unverzerrt(er)
# 2. warum zwischen jedem block -> attention is all you need paper - wir haben conv blöcke, machine translation hat feed forward blöcke
# ## Net Architecture - Second Part
#
# In[ ]:
##########################################################################################
# Net Architecture - Second Part #
##########################################################################################
class Asymm_3d_spconv(nn.Module):
def __init__(self,
output_shape,
use_norm=True,
num_input_features=128,
nclasses=20, n_height=32, strict=False, init_size=16):
super(Asymm_3d_spconv, self).__init__()
self.nclasses = nclasses
self.nheight = n_height
self.strict = False
self.head_size = 8
sparse_shape = np.array(output_shape)
# sparse_shape[0] = 11
print("sparse shape:" + str(sparse_shape))
# cylindrical partition splits these point clouds into 3D representation
# with the size = 480 × 360× 32, where three dimensions indicate the radius,
# angle and height, respectively.
self.sparse_shape = sparse_shape
self.downCntx = ResContextBlock(num_input_features, init_size, indice_key="pre")
self.downAttn2Block = CodedVTRBlock(init_size, init_size)
# DOWN BLOCKS
self.resBlock2 = ResBlock(init_size, 2 * init_size, 0.2, height_pooling=True, indice_key="down2")
self.downAttn3Block = CodedVTRBlock(2 * init_size, 2 * init_size)
self.resBlock3 = ResBlock(2 * init_size, 4 * init_size, 0.2, height_pooling=True, indice_key="down3")
self.downAttn4Block = CodedVTRBlock(4 * init_size, 4 * init_size)
self.resBlock4 = ResBlock(4 * init_size, 8 * init_size, 0.2, pooling=True, height_pooling=False, indice_key="down4")
self.downAttn5Block = CodedVTRBlock(8 * init_size, 8 * init_size)
self.resBlock5 = ResBlock(8 * init_size, 16 * init_size, 0.2, pooling=True, height_pooling=False, indice_key="down5")
# UP BLOCKS with CodedVTR Attention Block
self.upAttn0Block = CodedVTRBlock(16 * init_size, 16 * init_size)
self.upBlock0 = UpBlock(16 * init_size, 16 * init_size, indice_key="up0", up_key="down5")
self.upAttn1Block = CodedVTRBlock(16 * init_size, 16 * init_size)
self.upBlock1 = UpBlock(16 * init_size, 8 * init_size, indice_key="up1", up_key="down4")
self.upAttn2Block = CodedVTRBlock(8 * init_size, 8 * init_size)
self.upBlock2 = UpBlock(8 * init_size, 4 * init_size, indice_key="up2", up_key="down3")
self.upAttn3Block = CodedVTRBlock(4 * init_size, 4 * init_size)
self.upBlock3 = UpBlock(4 * init_size, 2 * init_size, indice_key="up3", up_key="down2")
self.ReconNet = ReconBlock(2 * init_size, 2 * init_size, indice_key="recon")
self.logits = spconv.SubMConv3d(4 * init_size, nclasses, indice_key="logit", kernel_size=3, stride=1, padding=1,
bias=True)
def forward(self, voxel_features, coors, batch_size, iter):
# x = x.contiguous()
coors = coors.int()
# import pdb
# pdb.set_trace()
sparseconv_features = spconv.SparseConvTensor(voxel_features, coors, self.sparse_shape,
batch_size)
#################################
# Down Context Block #
#################################
downcntx = self.downCntx(sparseconv_features)
#################################
# ENCODER BLOCKS #
#################################
downAttn1 = self.downAttn2Block(ME.SparseTensor(features=downcntx.features, coordinates=downcntx.indices), iter_=iter)
downcntx.replace_feature(downAttn1.F)
down1c, down1b = self.resBlock2(downcntx)
downAttn2 = self.downAttn3Block(ME.SparseTensor(features=down1c.features, coordinates=down1c.indices), iter_=iter)
down1c.replace_feature(downAttn2.F)
down2c, down2b = self.resBlock3(down1c)
downAttn3 = self.downAttn4Block(ME.SparseTensor(features=down2c.features, coordinates=down2c.indices), iter_=iter)
down2c.replace_feature(downAttn3.F)
down3c, down3b = self.resBlock4(down2c)
downAttn4 = self.downAttn5Block(ME.SparseTensor(features=down3c.features, coordinates=down3c.indices), iter_=iter)
down3c.replace_feature(downAttn4.F)
down4c, down4b = self.resBlock5(down3c)
#################################
# DECODER BLOCKS #
#################################
upAttn4 = self.upAttn0Block(ME.SparseTensor(features=down4c.features, coordinates=down4c.indices), iter_=iter)
down4c.replace_feature(upAttn4.F)
up4e = self.upBlock0(down4c, down4b)
upAttn3 = self.upAttn1Block(ME.SparseTensor(features=up4e.features, coordinates=up4e.indices), iter_=iter)
up4e.replace_feature(upAttn3.F)
up3e = self.upBlock1(up4e, down3b)
upAttn2 = self.upAttn2Block(ME.SparseTensor(features=up3e.features, coordinates=up3e.indices), iter_=iter)
up3e.replace_feature(upAttn2.F)
up2e = self.upBlock2(up3e, down2b)
upAttn1 = self.upAttn3Block(ME.SparseTensor(features=up2e.features, coordinates=up2e.indices), iter_=iter)
up2e.replace_feature(upAttn1.F)
up1e = self.upBlock3(up2e, down1b)
#################################
# DDCM BLOCK #
#################################
up0e = self.ReconNet(up1e)
up0e = up0e.replace_feature(torch.cat((up0e.features, up1e.features), 1))
logits = self.logits(up0e)
y = logits.dense()
return y
# ## Cylinder Feature Generator - First Part
# ![image.png](attachment:049edf76-a570-476b-98f2-2374bb95ac10.png)
# ![image.png](attachment:68044999-41a4-4af2-9157-00466d8bbec2.png)
# In[ ]:
##########################################################################################
# Net Architecture - First Part #
##########################################################################################
class cylinder_fea(nn.Module):
def __init__(self, grid_size, fea_dim=3,
out_pt_fea_dim=64, max_pt_per_encode=64, fea_compre=None):
super(cylinder_fea, self).__init__()
self.PPmodel = nn.Sequential(