-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_blocks.py
1193 lines (997 loc) · 43.2 KB
/
basic_blocks.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
# -*- coding: utf-8 -*-
from turtle import forward
import torch
import torch.nn as nn
import torch.nn.functional as F
from cc_attention import *
kx = ky = kz = 3
px = py = pz = 1
epsilon = 1e-7
norm_mode = 'bn'
num_groups = None
ch_per_group = 16
class Norm(nn.Module):
def __init__(self, channel):
super(Norm, self).__init__()
if norm_mode == 'bn':
self.norm = nn.BatchNorm3d(channel)
elif norm_mode == 'gn':
if num_groups is not None and ch_per_group is not None:
raise ValueError('Can only choose one, num_groups or ch_per_group')
if num_groups is not None:
assert channel%num_groups == 0, 'channel%%num_groups != 0'
self.norm = nn.GroupNorm(num_groups, channel)
elif ch_per_group is not None:
assert channel%ch_per_group == 0, 'channel%%ch_per_group != 0'
self.norm = nn.GroupNorm(channel//ch_per_group, channel)
else:
raise ValueError('Please choose one, num_groups or ch_per_group')
else:
raise ValueError('Unknown normalization mode')
def forward(self, x):
return self.norm(x)
class Upsample(nn.Module):
def __init__(self, scale=2, z=False):
super(Upsample, self).__init__()
z_scale = scale if z else 1
self.scale = (scale, scale, z_scale)
def forward(self, x):
x = F.interpolate(x, scale_factor=self.scale, mode='trilinear', align_corners=False)
return x
class DeConvBlock(nn.Module):
def __init__(self, in_channel, out_channel, scale=2):
super(DeConvBlock, self).__init__()
k = (scale, scale, 1)
s = (scale, scale, 1)
self.deconv = nn.ConvTranspose3d(in_channel, out_channel, kernel_size=k, stride=s, padding=0)
def forward(self, x):
x = self.deconv(x)
return x
class UpConvBlock(nn.Module):
def __init__(self, in_channel, out_channel, scale=2, deconv=False):
super(UpConvBlock, self).__init__()
if deconv:
self.up = nn.Sequential(
DeConvBlock(in_channel, out_channel, scale))
else:
layers = [Upsample(scale)]
if in_channel != out_channel:
layers.append(nn.Conv3d(in_channel, out_channel, kernel_size=1, stride=1, padding=0))
self.up = nn.Sequential(*layers)
def forward(self, x):
x = self.up(x)
return x
class ConvBlock(nn.Module):
def __init__(self, in_channel, out_channel, bias=True, n=2):
super(ConvBlock, self).__init__()
k = (kx, ky, kz)
p = (px, py, pz)
layers = []
for _ in range(n):
layers += [
nn.Conv3d(in_channel, out_channel, kernel_size=k, stride=1, padding=p, bias=bias),
Norm(out_channel),
nn.ReLU(inplace=True),
]
in_channel = out_channel
self.conv = nn.Sequential(*layers)
def forward(self, x):
x = self.conv(x)
return x
class RecurrentBlock(nn.Module):
def __init__(self, channel, bias=True, t=2):
super(RecurrentBlock, self).__init__()
self.t = t
k = (kx, ky, kz)
p = (px, py, pz)
self.conv = nn.Sequential(
nn.Conv3d(channel, channel, kernel_size=k, stride=1, padding=p, bias=bias),
Norm(channel),
nn.ReLU(inplace=True))
def forward(self, x):
for i in range(self.t):
if i == 0:
x1 = self.conv(x)
x1 = self.conv(x+x1)
return x1
# Recurrent CNN
class RCNNBlock(nn.Module):
def __init__(self, in_channel, out_channel, bias=True, num_rcnn=2, t=2):
super(RCNNBlock, self).__init__()
self.conv_1x1 = nn.Conv3d(in_channel, out_channel, kernel_size=1, stride=1, padding=0)
layers = []
for _ in range(num_rcnn):
layers.append(RecurrentBlock(out_channel, bias=bias, t=t))
self.nn = nn.Sequential(*layers)
def forward(self, x):
out = self.conv_1x1(x)
out = self.nn(out)
return out
# Recurrent Residual CNN
class RRCNNBlock(nn.Module):
def __init__(self, in_channel, out_channel, bias=True, num_rcnn=2, t=2):
super(RRCNNBlock, self).__init__()
self.conv_1x1 = nn.Conv3d(in_channel, out_channel, kernel_size=1, stride=1, padding=0)
layers = []
for _ in range(num_rcnn):
layers.append(RecurrentBlock(out_channel, bias=bias, t=t))
self.nn = nn.Sequential(*layers)
def forward(self, x):
x1 = self.conv_1x1(x)
x2 = self.nn(x1)
return x1 + x2
class AttentionBlock(nn.Module):
def __init__(self, f_g, f_l, f_int, bias=True):
super(AttentionBlock, self).__init__()
self.w_g = nn.Sequential(
nn.Conv3d(f_g, f_int, kernel_size=1, stride=1, padding=0, bias=bias),
nn.BatchNorm3d(f_int))
self.w_x = nn.Sequential(
nn.Conv3d(f_l, f_int, kernel_size=1, stride=1, padding=0, bias=bias),
nn.BatchNorm3d(f_int))
self.psi = nn.Sequential(
nn.Conv3d(f_int, 1, kernel_size=1, stride=1, padding=0, bias=bias),
nn.BatchNorm3d(1),
nn.Sigmoid())
self.relu = nn.ReLU(inplace=True)
def forward(self, g, x):
g1 = self.w_g(g)
x1 = self.w_x(x)
psi = self.relu(g1+x1)
psi = self.psi(psi)
return x * psi
class Flatten(nn.Module):
def forward(self, x):
return x.reshape(x.shape[0], -1)
class BAMChannelGate(nn.Module):
def __init__(self, gate_channel, reduction_ratio=16, num_layers=1):
super(BAMChannelGate, self).__init__()
gate_c = [Flatten()]
gate_channels = [gate_channel]
gate_channels += [gate_channel//reduction_ratio] * num_layers
gate_channels += [gate_channel]
for i in range(len(gate_channels)-2):
gate_c += [
nn.Linear(gate_channels[i], gate_channels[i+1]),
nn.BatchNorm1d(gate_channels[i+1]),
nn.ReLU(inplace=True)
]
gate_c.append(nn.Linear(gate_channels[-2], gate_channels[-1]))
self.gate_c = nn.Sequential(*gate_c)
def forward(self, x):
avg_pool = F.adaptive_avg_pool3d(x, 1)
att = self.gate_c(avg_pool)
att = att.reshape(att.shape[0], att.shape[1], 1, 1, 1).expand_as(x)
return att
class BAMSpatialGate(nn.Module):
def __init__(self, gate_channel, reduction_ratio=16, dilation_conv_num=2, dilation_val=4, dim='3d'):
k = (kx, ky, kz if dim == '3d' else 1)
p = (dilation_val, dilation_val, dilation_val if dim == '3d' else 0)
super(BAMSpatialGate, self).__init__()
gate_s = [
nn.Conv3d(gate_channel, gate_channel//reduction_ratio, kernel_size=1),
nn.BatchNorm3d(gate_channel//reduction_ratio),
nn.ReLU(inplace=True)
]
for _ in range(dilation_conv_num):
gate_s += [
nn.Conv3d(gate_channel//reduction_ratio, gate_channel//reduction_ratio, kernel_size=k, padding=p, dilation=dilation_val),
nn.BatchNorm3d(gate_channel//reduction_ratio),
nn.ReLU(inplace=True)
]
gate_s.append(nn.Conv3d(gate_channel//reduction_ratio, 1, kernel_size=1))
self.gate_s = nn.Sequential(*gate_s)
def forward(self, x):
att = self.gate_s(x).expand_as(x)
return att
class BAM(nn.Module):
def __init__(self, gate_channel, dim='3d'):
super(BAM, self).__init__()
self.channel_att = BAMChannelGate(gate_channel)
self.spatial_att = BAMSpatialGate(gate_channel, dim=dim)
def forward(self, x):
att_c = self.channel_att(x)
att_s = self.spatial_att(x)
scale = 1 + torch.sigmoid(att_c+att_s)
return x * scale
class BAMAPBlock(nn.Module):
def __init__(self, channel, dim='3d'):
super(BAMAPBlock, self).__init__()
k = (kx, ky, kz if dim == '3d' else 1)
p = (px, py, pz if dim == '3d' else 0)
self.conv = nn.Conv3d(channel*2, channel, kernel_size=k, stride=1, padding=p)
self.bam = BAM(channel, dim=dim)
def forward(self, x1, x2):
out = self.conv(torch.cat((x1, x2), dim=1))
out = self.bam(out)
return out
class CBAMChannelGate(nn.Module):
def __init__(self, gate_channel, reduction_ratio=10):
super(CBAMChannelGate, self).__init__()
self.gate_channel = gate_channel
self.mlp = nn.Sequential(
Flatten(),
nn.Linear(gate_channel, gate_channel//reduction_ratio),
nn.ReLU(inplace=True),
nn.Linear(gate_channel//reduction_ratio, gate_channel))
def forward(self, x):
avg_pool = F.adaptive_avg_pool3d(x, 1)
max_pool = F.adaptive_max_pool3d(x, 1)
channel_att = self.mlp(avg_pool) + self.mlp(max_pool)
scale = torch.sigmoid(channel_att).reshape(channel_att.shape[0], channel_att.shape[1], 1, 1, 1).expand_as(x)
return x * scale
class ChannelPool(nn.Module):
def forward(self, x):
channel_max = x.max(dim=1)[0].unsqueeze(1)
channel_mean = x.mean(dim=1).unsqueeze(1)
return torch.cat((channel_max, channel_mean), dim=1)
class CBAMSpatialGate(nn.Module):
def __init__(self):
super(CBAMSpatialGate, self).__init__()
self.compress = ChannelPool()
self.spatial = nn.Sequential(
nn.Conv3d(2, 1, kernel_size=7, stride=1, padding=(7-1)//2, bias=False),
nn.BatchNorm3d(1, eps=1e-5, momentum=0.01, affine=True),
nn.ReLU(inplace=True))
def forward(self, x):
x_compress = self.compress(x)
x_out = self.spatial(x_compress)
scale = torch.sigmoid(x_out)
return x * scale
class CBAM(nn.Module):
def __init__(self, gate_channel, reduction_ratio=16):
super(CBAM, self).__init__()
self.channel_gate = CBAMChannelGate(gate_channel, reduction_ratio)
self.spatial_gate = CBAMSpatialGate()
def forward(self, x):
x_out = self.channel_gate(x)
x_out = self.spatial_gate(x_out)
return x_out
class CBAMAPBlock(nn.Module):
def __init__(self, channel, level, reduction_ratio, fixed_kernel=True):
super(CBAMAPBlock, self).__init__()
kernel = 3 if fixed_kernel else 9-2*level
self.conv = nn.Conv3d(channel*2, channel, kernel_size=kernel, stride=1, padding=(kernel-1)//2)
self.cbam = CBAM(channel, reduction_ratio)
def forward(self, x1, x2):
out = self.conv(torch.cat((x1, x2), dim=1))
out = self.cbam(out)
return out
# Classification Guided Module
class CGM(nn.Module):
def __init__(self, in_channel):
super(CGM, self).__init__()
self.net = nn.Sequential(
nn.Conv3d(in_channel, 1, kernel_size=(1, 1, 1)),
nn.AdaptiveAvgPool3d((50, 50, 1)))
self.classifier = nn.Sequential(
Flatten(),
nn.Linear(2500, 2))
def forward(self, x):
out = self.net(x)
out = self.classifier(out)
return out
class ResConvBlock(nn.Module):
def __init__(self, in_channel, out_channel, bias=True, n=2, dim='3d'):
super(ResConvBlock, self).__init__()
k = (kx, ky, kz if dim == '3d' else 1)
p = (px, py, pz if dim == '3d' else 0)
self.conv_1x1 = nn.Conv3d(in_channel, out_channel, kernel_size=1, stride=1, padding=0)
layers = []
for _ in range(n):
layers += [
nn.Conv3d(out_channel, out_channel, kernel_size=k, stride=1, padding=p, bias=bias),
Norm(out_channel),
nn.ReLU(inplace=True),
]
self.conv = nn.Sequential(*layers)
def forward(self, x):
x1 = self.conv_1x1(x)
x2 = self.conv(x1)
return x1 + x2
class AFFModule(nn.Module):
def __init__(self, channel):
super(AFFModule, self).__init__()
self.se_block = SEBlock(channel*2)
self.conv_1x1 = nn.Conv3d(channel*2, channel, kernel_size=1)
self.avg_pool = nn.AdaptiveAvgPool3d(1)
self.activation = nn.Sigmoid()
def forward(self, x, x3d):
concat = torch.cat((x, x3d), dim=1)
att = self.conv_1x1(self.se_block(concat))
global_context = self.avg_pool(att)
weights = self.activation(global_context)
return x + x3d*weights
class DAF(nn.Module):
def __init__(self, channel):
super(DAF, self).__init__()
self.se_block = SEBlock(channel*2)
self.conv_1x1 = nn.Conv3d(channel*2, channel, kernel_size=1)
self.avg_pool = nn.AdaptiveAvgPool3d(1)
self.activation = nn.Sigmoid()
self.se_block_2d = SEBlock(channel)
def forward(self, x, x3d):
concat = torch.cat((x, x3d), dim=1)
att = self.conv_1x1(self.se_block(concat))
global_context = self.avg_pool(att)
weights = self.activation(global_context)
return self.se_block_2d(x) + x3d*weights
class DTBlock(nn.Module):
def __init__(self, channel):
super(DTBlock, self).__init__()
self.att2d = SEBlock(channel)
self.att3d = SEBlock(channel)
def forward(self, x, x3d):
return self.att2d(x) + self.att3d(x3d)
class ZattDTBlock(nn.Module):
def __init__(self, channel, alpha=1):
super(ZattDTBlock, self).__init__()
self.att2d = SEBlock(channel)
self.att3d = SEBlock(channel)
self.avg_pool = nn.AdaptiveAvgPool3d((1, 1, None))
self.query_conv = nn.Conv3d(channel, channel//8, kernel_size=1)
self.key_conv = nn.Conv3d(channel, channel//8, kernel_size=1)
self.value_conv = nn.Conv3d(channel, channel, kernel_size=1)
self.alpha = alpha
def forward(self, x, x3d):
b, c, h, w, d = x.shape
fusion = x + x3d
q = self.avg_pool(self.query_conv(fusion)).view(b, c//8, d).permute(0, 2, 1)
k = self.avg_pool(self.key_conv(fusion)).view(b, c//8, d)
score = torch.bmm(q, k)
attn = F.softmax(score.view(-1, score.shape[-1]), dim=1).view(score.shape[0], -1, score.shape[-1])
attn = torch.permute(attn, (0, 2, 1))
context = torch.bmm(self.value_conv(fusion).view(b, c*h*w, d), attn).view(fusion.shape)
return self.alpha*context + fusion
class RConvBlock(nn.Module):
def __init__(self, in_channel, out_channel, bias=True, n=2):
super(RConvBlock, self).__init__()
k = (kx, ky, kz)
p = (px, py, pz)
self.conv_1x1 = nn.Conv3d(in_channel, out_channel, kernel_size=1, stride=1, padding=0)
layers = []
layers += [
nn.Conv3d(out_channel, out_channel, kernel_size=(3, 1, 1), stride=1, padding=(1, 0, 0), bias=bias),
Norm(out_channel),
nn.ReLU(inplace=True),
nn.Conv3d(out_channel, out_channel, kernel_size=(1, 3, 1), stride=1, padding=(0, 1, 0), bias=bias),
Norm(out_channel),
nn.ReLU(inplace=True),
nn.Conv3d(out_channel, out_channel, kernel_size=(1, 1, 3), stride=1, padding=(0, 0, 1), bias=bias),
Norm(out_channel),
nn.ReLU(inplace=True),
]
self.conv = nn.Sequential(*layers)
def forward(self, x):
x1 = self.conv_1x1(x)
x2 = self.conv(x1)
return x1 + x2
class Bottleneck(nn.Module):
def __init__(self, in_channel, growth_rate, bias=False):
super(Bottleneck, self).__init__()
out_channel = 4 * growth_rate
self.bn1 = nn.BatchNorm3d(in_channel)
self.conv1 = nn.Conv3d(in_channel, out_channel, kernel_size=1, bias=bias)
self.bn2 = nn.BatchNorm3d(out_channel)
self.conv2 = nn.Conv3d(out_channel, growth_rate, kernel_size=3, padding=1, bias=bias)
def forward(self, x):
out = self.conv1(F.relu(self.bn1(x)))
out = self.conv2(F.relu(self.bn2(out)))
out = torch.cat((x, out), dim=1)
return out
class SingleLayer(nn.Module):
def __init__(self, in_channel, growth_rate):
super(SingleLayer, self).__init__()
self.bn1 = nn.BatchNorm3d(in_channel)
self.conv1 = nn.Conv3d(in_channel, growth_rate, kernel_size=3, padding=1, bias=False)
def forward(self, x):
out = self.conv1(F.relu(self.bn1(x)))
out = torch.cat((x, out), 1)
return out
class DCBlock(nn.Module):
def __init__(self, channel, growth_rate, num_blocks, bottleneck):
super(DCBlock, self).__init__()
layers = []
for _ in range(int(num_blocks)):
if bottleneck:
layers.append(Bottleneck(channel, growth_rate))
else:
layers.append(SingleLayer(channel, growth_rate))
channel += growth_rate
self.dense = nn.Sequential(*layers)
def forward(self, x):
return self.dense(x)
class MFSEBlock(nn.Module):
# Mixed Fusion Squeeze-and-Excitation
def __init__(self, channel):
super(MFSEBlock, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool3d(1)
self.maxout = nn.AdaptiveMaxPool3d(1)
self.fc_avg_pool = nn.Sequential(
nn.Linear(channel, channel//2, bias=False),
nn.ReLU(inplace=True),
nn.Linear(channel//2, channel, bias=False),
nn.Sigmoid())
self.fc_maxout = nn.Sequential(
nn.Linear(channel, channel//2, bias=False),
nn.ReLU(inplace=True),
nn.Linear(channel//2, channel, bias=False),
nn.Sigmoid())
self.conv_1x1 = nn.Conv3d(3*channel, channel, kernel_size=1, stride=1, padding=0)
def forward(self, x):
b, c = x.shape[:2]
y1 = self.avg_pool(x).reshape(b, c)
y1 = self.fc_avg_pool(y1).reshape(b, c, 1, 1, 1)
y2 = self.maxout(x).reshape(b, c)
y2 = self.fc_maxout(y2).reshape(b, c, 1, 1, 1)
y = torch.cat(((y1+y2), (y1*y2), torch.maximum(y1, y2)), dim=1)
y = self.conv_1x1(y)
return x * y
class MAGM(nn.Module):
# Multi-level Attention Gate Module
def __init__(self, in_channel, level):
super(MAGM, self).__init__()
self.level = level
self.resample = []
for i in range(1, level):
channel = in_channel * (2**i)
self.resample.append(nn.Sequential(
UpConvBlock(channel, in_channel, 2**i, deconv=True)))
self.resample = nn.ModuleList(self.resample)
self.conv_1x1 = nn.Sequential(
nn.ReLU(inplace=True),
nn.Conv3d(in_channel, in_channel, kernel_size=1, stride=1, padding=0),
nn.Sigmoid())
def forward(self, *x):
assert len(x) == self.level, f'The number of input tensors should be equal to {self.level}.'
tensors = [x[0]]
for i in range(1, len(x)):
tensors.append(self.resample[i-1](x[i]))
attention_coef = torch.stack(tensors, dim=0).sum(dim=0)
attention_coef = self.conv_1x1(attention_coef)
return x[0] * attention_coef
class CDB(nn.Module):
def __init__(self, channel, level, cur_level, pyramid=False):
super(CDB, self).__init__()
self.pyramid = pyramid
self.pyramid_level = level - cur_level - 1
total_channel = (level-cur_level)*channel if pyramid else channel
self.conv_sub_1x1 = nn.Conv3d(channel, channel, kernel_size=1, stride=1, padding=0)
self.mfse0 = MFSEBlock(channel)
if pyramid:
for i in range(1, level-cur_level):
ks = 2**i + 1
p = (ks-1) // 2
setattr(self, f'conv{i}', nn.Conv3d(channel, channel, kernel_size=(ks, ks, kz), stride=1, padding=(p, p, pz)))
setattr(self, f'mfse{i}', MFSEBlock(channel))
self.conv_apb_1x1 = nn.Conv3d(total_channel, channel, kernel_size=1, stride=1, padding=0)
def forward(self, x1, x2):
x = self.conv_sub_1x1(x1-x2)
if self.pyramid:
branches = [self.mfse0(x)]
for i in range(1, self.pyramid_level+1):
branches.append(getattr(self, f'mfse{i}')(getattr(self, f'conv{i}')(x)))
x = torch.cat(branches, dim=1)
x = self.conv_apb_1x1(x)
else:
x = self.conv_apb_1x1(self.mfse0(x))
return x
class SEBlock(nn.Module):
def __init__(self, in_channel, reduction=16):
super(SEBlock, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool3d(1)
self.fc_avg_pool = nn.Sequential(
nn.Linear(in_channel, in_channel//reduction, bias=False),
nn.ReLU(inplace=True),
nn.Linear(in_channel//reduction, in_channel, bias=False),
nn.Sigmoid())
def forward(self, x):
b, c = x.shape[:2]
avg_pool = self.avg_pool(x).reshape(b, c)
y = self.fc_avg_pool(avg_pool).reshape(b, c, 1, 1, 1)
return x * y
class SEPP(nn.Module):
def __init__(self, in_channel, dilation):
super(SEPP, self).__init__()
k = (kx, ky, 1)
p = (px, py, 1)
self.atrous0 = nn.Sequential(
nn.Conv3d(in_channel, in_channel, kernel_size=k, dilation=dilation, stride=1, padding=p),
ConvBlock(in_channel, in_channel, n=1))
self.atrous1 = nn.Sequential(
nn.Conv3d(in_channel, in_channel, kernel_size=k, dilation=dilation*2, stride=1, padding=p),
ConvBlock(in_channel, in_channel, n=1))
self.atrous2 = nn.Sequential(
nn.Conv3d(in_channel, in_channel, kernel_size=k, dilation=dilation*4, stride=1, padding=p),
ConvBlock(in_channel, in_channel, n=1))
self.se0 = SEBlock(in_channel)
self.se1 = SEBlock(in_channel)
self.se2 = SEBlock(in_channel)
self.conv1x1_0 = ConvBlock(in_channel, in_channel, n=1)
self.conv1x1_1 = ConvBlock(in_channel, in_channel, n=1)
self.conv1x1_2 = ConvBlock(in_channel, in_channel, n=1)
self.con1x1_final = nn.Conv3d(in_channel*3, in_channel, kernel_size=1)
def forward(self, x):
b, c = x.shape[:2]
atrous0 = self.atrous0(x)
atrous1 = self.atrous1(x)
atrous2 = self.atrous2(x)
dilation0 = x * self.se0(atrous0).reshape(b, c, 1, 1, 1)
dilation1 = x * self.se1(atrous1).reshape(b, c, 1, 1, 1)
dilation2 = x * self.se2(atrous2).reshape(b, c, 1, 1, 1)
branch0 = self.conv1x1_0(dilation0)
branch1 = self.conv1x1_1(dilation1)
branch2 = self.conv1x1_2(dilation2)
out = torch.cat((branch0, branch1, branch2), dim=1)
return self.con1x1_final(out)
class SEPPAPBlock(nn.Module):
def __init__(self, channel):
super(SEPPAPBlock, self).__init__()
self.sepp = SEPP(channel, dilation=1)
self.conv = nn.Conv3d(channel*2, channel, kernel_size=3, stride=1, padding=1)
def forward(self, x1, x2):
out = self.conv(torch.cat((x1, x2), dim=1))
out = self.sepp(out)
return out
# https://github.com/xvjiarui/GCNet/blob/a9fcc88c4bd3a0b89de3678b4629c9dfd190575f/mmdet/ops/gcb/context_block.py#L13
class GCBlock(nn.Module):
def __init__(self, inplanes, ratio, pooling_type='att', fusion_types=('channel_add', )):
super(GCBlock, self).__init__()
valid_fusion_types = ['channel_add', 'channel_mul']
assert pooling_type in ['avg', 'att']
assert isinstance(fusion_types, (list, tuple))
assert all([f in valid_fusion_types for f in fusion_types])
assert len(fusion_types) > 0, 'at least one fusion should be used'
self.inplanes = inplanes
self.ratio = ratio
self.planes = int(inplanes * ratio)
self.pooling_type = pooling_type
self.fusion_types = fusion_types
if pooling_type == 'att':
self.conv_mask = nn.Conv3d(inplanes, 1, kernel_size=1)
self.softmax = nn.Softmax(dim=2)
else:
self.avg_pool = nn.AdaptiveAvgPool3d(1)
if 'channel_add' in fusion_types:
self.channel_add_conv = nn.Sequential(
nn.Conv3d(self.inplanes, self.planes, kernel_size=1),
nn.LayerNorm([self.planes, 1, 1, 1]),
nn.ReLU(inplace=True), # yapf: disable
nn.Conv3d(self.planes, self.inplanes, kernel_size=1))
else:
self.channel_add_conv = None
if 'channel_mul' in fusion_types:
self.channel_mul_conv = nn.Sequential(
nn.Conv3d(self.inplanes, self.planes, kernel_size=1),
nn.LayerNorm([self.planes, 1, 1, 1]),
nn.ReLU(inplace=True), # yapf: disable
nn.Conv3d(self.planes, self.inplanes, kernel_size=1))
else:
self.channel_mul_conv = None
def spatial_pool(self, x):
b, c, h, w, d = x.size()
if self.pooling_type == 'att':
input_x = x
input_x = input_x.view(b, c, h*w*d) # [N, C, H*W*D]
input_x = input_x.unsqueeze(1) # [N, 1, C, H*W*D]
context_mask = self.conv_mask(x) # [N, 1, H, W, D]
context_mask = context_mask.view(b, 1, h*w*d) # [N, 1, H*W*D]
context_mask = self.softmax(context_mask) # [N, 1, H*W*D]
context_mask = context_mask.unsqueeze(-1) # [N, 1, H*W*D, 1]
context = torch.matmul(input_x, context_mask) # [N, 1, C, 1]
context = context.unsqueeze(-1).view(b, c, 1, 1, 1) # [N, C, 1, 1, 1]
else:
context = self.avg_pool(x) # [N, C, 1, 1, 1]
return context
def forward(self, x):
context = self.spatial_pool(x) # [N, C, 1, 1, 1]
out = x
if self.channel_mul_conv is not None:
channel_mul_term = torch.sigmoid(self.channel_mul_conv(context)) # [N, C, 1, 1, 1]
out = out * channel_mul_term
if self.channel_add_conv is not None:
channel_add_term = self.channel_add_conv(context) # [N, C, 1, 1, 1]
out = out + channel_add_term
return out
class ASPP(nn.Module):
def __init__(self, channel, rate=1):
super(ASPP, self).__init__()
k = (kx, ky, kz)
p = (px, py, pz)
self.atrous0 = nn.Conv3d(channel, channel, kernel_size=k, padding=p, dilation=rate)
self.atrous1 = nn.Conv3d(channel, channel, kernel_size=k, padding=p, dilation=rate*2)
self.atrous2 = nn.Conv3d(channel, channel, kernel_size=k, padding=p, dilation=rate*4)
self.conv1x1 = nn.Conv3d(channel*4, channel, kernel_size=1)
def forward(self, x):
_, _, h, w, d = x.shape
tmp = x
x0 = self.atrous0(x)
x0 = F.interpolate(x0, size=(h, w, d), mode='trilinear', align_corners=False)
x1 = self.atrous0(x)
x1 = F.interpolate(x1, size=(h, w, d), mode='trilinear', align_corners=False)
x2 = self.atrous0(x)
x2 = F.interpolate(x2, size=(h, w, d), mode='trilinear', align_corners=False)
concat = torch.cat((tmp, x0, x1, x2), dim=1)
out = self.conv1x1(concat)
return out
class SCSEBlock(nn.Module):
def __init__(self, in_channels, reduction=16):
super(SCSEBlock, self).__init__()
self.cSE = nn.Sequential(
nn.AdaptiveAvgPool3d(1),
nn.Conv3d(in_channels, in_channels//reduction, 1),
nn.ReLU(inplace=True),
nn.Conv3d(in_channels//reduction, in_channels, 1),
nn.Sigmoid())
self.sSE = nn.Sequential(nn.Conv3d(in_channels, 1, 1), nn.Sigmoid())
def forward(self, x):
return x * self.cSE(x) + x * self.sSE(x)
from pywt import dwt2
class DWTBlock(nn.Module):
def __init__(self, channels):
super(DWTBlock, self).__init__()
self.conv1x1 = nn.Conv3d(channels+4, channels, kernel_size=1)
self.attention = SCSEBlock(channels)
def to_tensor(self, LL, LH, HL, HH):
return torch.from_numpy(LL).cuda(), \
torch.from_numpy(LH).cuda(), \
torch.from_numpy(HL).cuda(), \
torch.from_numpy(HH).cuda()
def forward(self, x1, x2):
LL, (LH, HL, HH) = dwt2(x2.cpu(), wavelet='haar', mode='symmetric', axes=(-3, -2))
LL, LH, HL, HH = self.to_tensor(LL, LH, HL, HH)
features = torch.cat((LL, LH, HL, HH, x1), dim=1)
return self.attention(self.conv1x1(features)), LL
class DWTBlockV2(nn.Module):
def __init__(self, channels):
super(DWTBlockV2, self).__init__()
self.conv0 = ConvBlock(4, channels)
self.conv1 = nn.Conv3d(channels*2, channels, kernel_size=3, padding=1)
self.attention = SCSEBlock(channels)
def to_tensor(self, LL, LH, HL, HH):
return torch.from_numpy(LL).cuda(), \
torch.from_numpy(LH).cuda(), \
torch.from_numpy(HL).cuda(), \
torch.from_numpy(HH).cuda()
def forward(self, x1, x2):
LL, (LH, HL, HH) = dwt2(x2.cpu(), wavelet='haar', mode='symmetric', axes=(-3, -2))
LL, LH, HL, HH = self.to_tensor(LL, LH, HL, HH)
dwt_features = self.conv0(torch.cat((LL, LH, HL, HH), dim=1))
features = self.conv1(torch.cat((dwt_features, x1), dim=1))
return self.attention(features), LL
class DWTBlockv3(nn.Module):
def __init__(self, channels):
super(DWTBlockv3, self).__init__()
self.bn = Norm(4)
self.relu = nn.ReLU(inplace=True)
self.attention = BAM(channels)
def to_tensor(self, LL, LH, HL, HH):
return torch.from_numpy(LL).cuda(), \
torch.from_numpy(LH).cuda(), \
torch.from_numpy(HL).cuda(), \
torch.from_numpy(HH).cuda()
def forward(self, x1, x2):
LL, (LH, HL, HH) = dwt2(x2.cpu(), wavelet='haar', mode='symmetric', axes=(-3, -2))
LL, LH, HL, HH = self.to_tensor(LL, LH, HL, HH)
dwt_features = torch.cat((LL, LH, HL, HH), dim=1)
dwt_features = self.relu(self.bn(dwt_features))
features = torch.cat((dwt_features, x1), dim=1)
return self.attention(features), LL
class DWTInitBlock(nn.Module):
def __init__(self):
super(DWTInitBlock, self).__init__()
num_dwt_features = 4
self.conv = nn.Conv3d(num_dwt_features, num_dwt_features*3, kernel_size=3, padding=1)
def to_tensor(self, LL, LH, HL, HH):
return torch.from_numpy(LL).cuda(), \
torch.from_numpy(LH).cuda(), \
torch.from_numpy(HL).cuda(), \
torch.from_numpy(HH).cuda()
def forward(self, x):
LL, (LH, HL, HH) = dwt2(x.cpu(), wavelet='haar', mode='symmetric', axes=(-3, -2))
LL, LH, HL, HH = self.to_tensor(LL, LH, HL, HH)
dwt_features = torch.cat((LL, LH, HL, HH), dim=1)
init = self.conv(dwt_features)
return torch.cat((init, dwt_features), dim=1)
class APBlock(nn.Module):
def __init__(self, channel):
super(APBlock, self).__init__()
self.conv = nn.Conv3d(channel, channel, kernel_size=3, stride=1, padding=1)
self.bam = BAM(channel)
def forward(self, x):
out = self.conv(x)
out = self.bam(out)
return out
class APBlockv2(nn.Module):
def __init__(self, channel):
super(APBlockv2, self).__init__()
self.conv = nn.Conv3d(channel*5, channel, kernel_size=3, stride=1, padding=1)
self.bam = BAM(channel)
def forward(self, LL, LH, HL, HH, x):
out = self.conv(torch.cat((LL, LH, HL, HH, x), dim=1))
out = self.bam(out)
return out
class APBlockv3(nn.Module):
def __init__(self, channel):
super(APBlockv3, self).__init__()
self.bam = BAM(channel)
def forward(self, x):
out = self.bam(x)
return out
# https://github.com/osmr/imgclsmob/blob/master/pytorch/pytorchcv/models/xception.py
class DWSConv(nn.Module):
'''
Depthwise separable convolution layer.
Parameters:
----------
in_channels : int
Number of input channels.
out_channels : int
Number of output channels.
kernel_size : int or tuple/list of 3 int
Convolution window size.
stride : int or tuple/list of 3 int, default 1
Strides of the convolution.
padding : int or tuple/list of 3 int, default 0
Padding value for convolution layer.
'''
def __init__(self, in_channel, out_channel, kernel_size, stride=1, padding=0):
super(DWSConv, self).__init__()
self.dw_conv = nn.Conv3d(
in_channels=in_channel,
out_channels=in_channel,
kernel_size=kernel_size,
stride=stride,
padding=padding,
groups=in_channel,
bias=False)
self.pw_conv = nn.Conv3d(
in_channels=in_channel,
out_channels=out_channel,
kernel_size=1,
bias=False)
def forward(self, x):
x = self.dw_conv(x)
x = self.pw_conv(x)
return x
class DWSConvBlock(nn.Module):
'''
Depthwise separable convolution block with batchnorm and ReLU pre-activation.
Parameters:
----------
in_channels : int
Number of input channels.
out_channels : int
Number of output channels.
kernel_size : int or tuple/list of 3 int
Convolution window size.
stride : int or tuple/list of 3 int
Strides of the convolution.
padding : int or tuple/list of 3 int
Padding value for convolution layer.
activate : bool
Whether activate the convolution block.
'''
def __init__(self, in_channel, out_channel, stride=1, activate=True):
super(DWSConvBlock, self).__init__()
k = (kx, ky, kz)
p = (px, py, pz)
self.activate = activate
# if self.activate:
# self.activ = nn.ReLU(inplace=False)
self.conv1 = DWSConv(in_channel=in_channel, out_channel=out_channel, kernel_size=k, stride=stride, padding=p)
self.bn = Norm(out_channel)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
# if self.activate:
# x = self.activ(x)
x = self.conv1(x)
x = self.bn(x)
x = self.relu(x)
return x
class ResDWSConvBlock(nn.Module):
def __init__(self, in_channel, out_channel):
super(ResDWSConvBlock, self).__init__()
self.conv1x1 = nn.Conv3d(in_channel, out_channel, kernel_size=1, stride=1, padding=0)
self.dws = DWSConvBlock(out_channel, out_channel)
self.conv3x3 = nn.Conv3d(in_channel, out_channel, kernel_size=3, stride=(2, 2, 1), padding=1)
def forward(self, x, skip):
x = self.conv1x1(x)
x = self.dws(x)
skip = self.conv3x3(skip)
return x + skip
class PACBlock2d(nn.Module):
def __init__(self, channel, ratio=6, bias=True):
super(PACBlock2d, self).__init__()
k = (kx, ky, kz)
self.pac0 = nn.Sequential(
nn.Conv3d(
in_channels=channel,
out_channels=channel,
kernel_size=k,
dilation=(ratio, ratio, 1),
padding=(ratio, ratio, 1),
bias=bias))
self.pac1 = nn.Sequential(
nn.Conv3d(
in_channels=channel,
out_channels=channel,
kernel_size=k,
dilation=(ratio*2, ratio*2, 1),
padding=(ratio*2, ratio*2, 1),
bias=bias))
self.pac2 = nn.Sequential(
nn.Conv3d(
in_channels=channel,
out_channels=channel,
kernel_size=k,
dilation=(ratio*3, ratio*3, 1),
padding=(ratio*3, ratio*3, 1),
bias=bias))
self.gap = nn.Sequential(
nn.AdaptiveAvgPool3d(1),
nn.Conv3d(in_channels=channel, out_channels=channel, kernel_size=1))