-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEFMOD.py
More file actions
1145 lines (929 loc) · 48.6 KB
/
EFMOD.py
File metadata and controls
1145 lines (929 loc) · 48.6 KB
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
import torch
import math
import torch.nn as nn
import torch.nn.functional as F
from typing import Optional, Union, Sequence
from mmseg.registry import MODELS
from mmcv.cnn.bricks import DropPath
from torchvision.transforms.functional import rotate
from torchvision.transforms import InterpolationMode
from functools import partial
from mmcv.cnn import ConvModule, build_norm_layer,build_activation_layer
from mmengine.model import BaseModule
from mmseg.models.utils import autopad, make_divisible, BHWC2BCHW, BCHW2BHWC
from mmengine.utils.dl_utils.parrots_wrapper import _BatchNorm
tensor_rotate = partial(rotate,interpolation=InterpolationMode.BILINEAR)
'''-----------------------------------------------------------------------------------------------------------------'''
class ResidualBlock(BaseModule):
# 实现子module:Residual Block
def __init__(self,
in_ch:int,
out_ch:int,
stride:int=1,
shortcut=None,
norm_cfg: Optional[dict] = dict(type='BN'),
act_cfg: Optional[dict] = dict(type='ReLU'),
init_cfg: Optional[dict] = None
):
super(ResidualBlock, self).__init__(init_cfg)
self.conv1 = ConvModule(in_ch,out_ch,3,stride,padding=1,bias=False,norm_cfg=norm_cfg,act_cfg=None)
self.conv2 = ConvModule(out_ch,out_ch,3,1,1,bias=False,norm_cfg=norm_cfg,act_cfg=None)
self.downsample = shortcut
def forward(self, x):
out = self.conv1(x)
# out = self.bn1(out)
out = F.relu(out)
out = self.conv2(out)
residual = x if self.downsample is None else self.downsample(x)
out += residual
return F.relu(out)
class connecter(BaseModule):
# 连接器
def __init__(self,
in_ch,
out_ch,
scale_factor=0.5,
norm_cfg: Optional[dict] = dict(type='BN'),
act_cfg: Optional[dict] = dict(type='ReLU'),
init_cfg: Optional[dict] = None
):
super(connecter, self).__init__(init_cfg)
self.downsample = partial(F.interpolate, scale_factor=scale_factor, mode='area', recompute_scale_factor=True)
# mode(str):用于采样的算法。'nearest'| 'linear'| 'bilinear'| 'bicubic'| 'trilinear'| 'area'。默认:'nearest'
# /home/wyc/software/anconda3/envs/pytroch_test/lib/python3.9/site-packages/torch/nn/functional.py:3502:
# UserWarning: The default behavior for interpolate/upsample with float scale_factor changed in 1.6.0 to align with other frameworks/libraries,
# and now uses scale_factor directly, instead of relying on the computed output size. If you wish to restore the old behavior,
# please set recompute_scale_factor=True. See the documentation of nn.Upsample for details.
if not in_ch == out_ch:
shortcut = ConvModule(in_ch, out_ch,1,1,norm_cfg=norm_cfg,act_cfg=None)
else:
shortcut = None
self.connect_conv = ResidualBlock(in_ch, out_ch, shortcut=shortcut,norm_cfg=norm_cfg,act_cfg=act_cfg)
def forward(self, x):
x = self.downsample(x)
x = self.connect_conv(x)
return x
"""-----------------------------------------------------------------------------------------------------------------"""
class MFE_Block(BaseModule):
def __init__(self,
in_ch,
out_ch,
stride=1,
dilation=1,
bias = False,
strip=9,
norm_cfg: Optional[dict] = dict(type='BN'),
act_cfg: Optional[dict] = dict(type='ReLU'),
init_cfg: Optional[dict] = None
):
super().__init__(init_cfg)
self.conv_a0 = nn.Sequential(ConvModule(in_ch,out_ch,3,1,1,norm_cfg=norm_cfg,act_cfg=None),
nn.ELU(inplace=True)
)
self.multi_conv1 = ConvModule(out_ch, 8, (1, strip), stride=stride, padding=(0, strip // 2), norm_cfg=None,
act_cfg=None)
self.multi_conv2 = ConvModule(out_ch, 8, (strip, 1), stride=stride, padding=(strip // 2, 0), norm_cfg=None,
act_cfg=None)
self.multi_conv3 = ConvModule(out_ch, 8, (1, strip), stride=stride, padding=(0, strip // 2), norm_cfg=None,
act_cfg=None)
self.multi_conv4 = ConvModule(out_ch, 8, (1, strip), stride=stride, padding=(0, strip // 2), norm_cfg=None,
act_cfg=None)
self.channel_concern = nn.Sequential(ConvModule(out_ch, out_ch, 1, 1, padding=0,dilation=dilation, bias=bias,
norm_cfg=norm_cfg,act_cfg=None),
nn.ELU(inplace=True)
)
self.angle = [0,45,90,135,180]
def forward(self, x):
x = self.conv_a0(x)
x1 = self.multi_conv1(x)
x2 = self.multi_conv2(x)
x4 = self.multi_conv3(tensor_rotate(x,self.angle[1]))
x5 = self.multi_conv4(tensor_rotate(x,self.angle[3]))
out = torch.cat((x1,
x2,
tensor_rotate(x4,-self.angle[1]),
tensor_rotate(x5,-self.angle[3]),
), 1)
out = self.channel_concern(out)+x
return out
'''----------------------------------------------------Encoder--------------------------------------------------------'''
class DoubleConv(BaseModule):
"""(convolution => [BN] => ReLU) * 2"""
def __init__(self,
in_channels,
out_channels,
mid_channels=None,
norm_cfg=dict(type='BN'),
act_cfg=dict(type='ReLU'),
init_cfg=None,
):
super().__init__(init_cfg)
if not mid_channels:
mid_channels = out_channels
self.double_conv = nn.Sequential(
ConvModule(in_channels, mid_channels,kernel_size=3, padding=1,bias=False,norm_cfg=norm_cfg,
act_cfg=act_cfg),
ConvModule(mid_channels, out_channels, kernel_size=3, padding=1, bias=False, norm_cfg=norm_cfg,
act_cfg=act_cfg),
)
def forward(self, x):
return self.double_conv(x)
class EFE(BaseModule):
def __init__(self,
in_channels:int,
out_channels:int,
norm_cfg=dict(type='BN'),
act_cfg=dict(type='GELU'),
):
super().__init__()
self.down_conv=nn.Sequential(nn.MaxPool2d(2),
DoubleConv(in_channels, out_channels,norm_cfg=norm_cfg,act_cfg=act_cfg))
self.hv_conv = nn.Sequential(ConvModule(out_channels,out_channels,(1,3),1,(0,1),norm_cfg=None,act_cfg=None),
ConvModule(out_channels,out_channels,(1,3),1,(0,1),norm_cfg=norm_cfg,act_cfg=None)
)
self.act = build_activation_layer(act_cfg)
self.conv_1x1 = ConvModule(out_channels*2,out_channels,1,norm_cfg=norm_cfg,act_cfg=act_cfg)
def forward(self,x):
x_1= self.down_conv(x)
x_2 = self.hv_conv(x_1)
x = self.act(torch.cat((x_1,x_2),1))
return self.conv_1x1(x)
"""----------------------------------------------------ASPP_aug--------------------------------------------------------"""
class ASPPConv(nn.Sequential):
def __init__(self,
in_channels:int,
out_channels:int,
dilation:int,
norm_cfg=dict(type='BN'),
act_cfg=dict(type='GELU'),
):
modules = [
ConvModule(in_channels, out_channels,3,padding=dilation,dilation=dilation,bias=False,norm_cfg=None,act_cfg=None),
ConvModule(out_channels,out_channels,(9,1),1,padding=(4,0),norm_cfg=None,act_cfg=None),
ConvModule(out_channels,out_channels,(1,9),1,(0,4),norm_cfg=norm_cfg,act_cfg=act_cfg),
]
super(ASPPConv, self).__init__(*modules)
# 池化 -> 1*1 卷积 -> 上采样
class ASPPPooling(nn.Sequential):
def __init__(self,
in_channels:int,
out_channels:int,
norm_cfg=dict(type='BN'),
act_cfg=dict(type='GELU'),
):
super(ASPPPooling, self).__init__(
nn.AdaptiveAvgPool2d(1), # 自适应均值池化
ConvModule(in_channels,out_channels,1,bias=False,norm_cfg=norm_cfg,act_cfg=act_cfg)
)
def forward(self, x):
size = x.shape[-2:]
for mod in self:
x = mod(x)
# 上采样
return F.interpolate(x, size=size, mode='bilinear', align_corners=False)
# 整个 ASPP 架构
class ASPP(BaseModule):
def __init__(self,
in_channels:int,
atrous_rates:list,
out_channels:int=256,
norm_cfg=dict(type='BN'),
act_cfg=dict(type='GELU'),
init_cfg=None
):
super(ASPP, self).__init__(init_cfg)
modules = []
# 1*1 卷积
modules.append(
ConvModule(in_channels,out_channels,1,bias=False,norm_cfg=norm_cfg,act_cfg=act_cfg)
)
# 多尺度空洞卷积
rates = tuple(atrous_rates)
for rate in rates:
modules.append(ASPPConv(in_channels, out_channels, rate,norm_cfg=norm_cfg,act_cfg=act_cfg))
# 池化
modules.append(ASPPPooling(in_channels, out_channels,norm_cfg=norm_cfg,act_cfg=act_cfg))
self.convs = nn.ModuleList(modules)
# 拼接后的卷积
self.project = nn.Sequential(
ConvModule(len(self.convs) * out_channels, out_channels, 1, bias=False,norm_cfg=norm_cfg,act_cfg=act_cfg),
nn.Dropout(0.5)
)
def forward(self, x):
res = []
for conv in self.convs:
res.append(conv(x))
res = torch.cat(res, dim=1)
return self.project(res)
'''------------------------------------------branch_conv-----------------------------------------------------'''
class SE(BaseModule):
def __init__(self,
in_channel,
ratio=16,
norm_cfg=dict(type='BN'),
act_cfg=dict(type='ReLU'),
init_cfg=None):
super().__init__(init_cfg)
self.gap = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Sequential(
nn.Linear(in_channel, ratio, bias=False), # ? c -> c/r
nn.ReLU(),
nn.Linear(ratio, in_channel, bias=False), # ? c/r -> c
nn.Sigmoid()
)
def forward(self, x):
b, c = x.shape[0:2]
y = self.gap(x).view(b, c)
y = self.fc(y).view(b, c, 1, 1)
return y*x
class BRANCH_CONV16(BaseModule):
def __init__(self,
oup_channels: int,
group_num: int = 16,
norm_cfg: Optional[dict] = dict(type='BN'),
act_cfg: Optional[dict] = dict(type='GELU'),
init_cfg: Optional[dict] = None
):
super().__init__(init_cfg)
# self.gn = GroupBatchnorm2d16(oup_channels, group_num=group_num,norm_cfg=norm_cfg,act_cfg=act_cfg)
self.gn = nn.GroupNorm(num_groups=group_num,num_channels=oup_channels)
self.cha_at = SE(oup_channels,norm_cfg=norm_cfg,act_cfg=act_cfg)
def forward(self, x):
gn_x= self.gn(x)
out = self.cha_at(gn_x)
return out
class GroupBatchnorm2d4(BaseModule):
def __init__(self,
c_num: int,
group_num: int = 4,
eps: float = 1e-10,
norm_cfg: Optional[dict] = dict(type='BN', momentum=0.03, eps=0.001),
act_cfg: Optional[dict] = dict(type='GELU'),
init_cfg: Optional[dict] = None
):
super(GroupBatchnorm2d4, self).__init__(init_cfg)
assert c_num >= group_num
self.group_num = group_num
self.gamma = nn.Parameter(torch.randn(c_num, 1, 1))
self.beta = nn.Parameter(torch.zeros(c_num, 1, 1))
self.eps = eps
def forward(self, x):
N, C, H, W = x.size()
x = x.view(N, self.group_num, -1)
mean = x.mean(dim=2, keepdim=True)
std = x.std(dim=2, keepdim=True)
x = (x - mean) / (std + self.eps)
x = x.view(N, C, H, W)
return x * self.gamma + self.beta
class BRANCH_CONV4(BaseModule):
def __init__(self,
oup_channels: int,
group_num: int = 4,
norm_cfg: Optional[dict] = dict(type='BN'),
act_cfg: Optional[dict] = dict(type='GELU'),
init_cfg: Optional[dict] = None
):
super().__init__(init_cfg)
self.gn = nn.GroupNorm(group_num,oup_channels)
self.cha_at = SE(oup_channels,norm_cfg=norm_cfg,act_cfg=act_cfg)
def forward(self, x):
gn_x = self.gn(x)
out = self.cha_at(gn_x)
return out
class DepthWiseConv2d(BaseModule):
def __init__(self,
dim_in:int,
dim_out:int,
kernel_size:int=3,
padding:int=1,
stride:int=1,
dilation:int=1,
norm_cfg: Optional[dict] = dict(type='BN'),
act_cfg: Optional[dict] = dict(type='GELU'),
init_cfg: Optional[dict] = None
):
super().__init__(init_cfg)
# self.conv1 = nn.Conv2d(dim_in, dim_in, kernel_size=kernel_size, padding=padding,
# stride=stride, dilation=dilation, groups=dim_in)
self.conv1 = ConvModule(dim_in, dim_in, kernel_size=kernel_size, padding=padding,
stride=stride, dilation=dilation, groups=dim_in,norm_cfg=None,act_cfg=None)
self.norm_layer = nn.GroupNorm(4, dim_in)
# self.conv2 = nn.Conv2d(dim_in, dim_out, kernel_size=1)
self.conv2 = ConvModule(dim_in, dim_out, kernel_size=1, stride=1, norm_cfg=None, act_cfg=None)
def forward(self, x):
return self.conv2(self.norm_layer(self.conv1(x)))
class LayerNorm(BaseModule):
def __init__(self,
normalized_shape,
eps=1e-6,
data_format="channels_last",
norm_cfg: Optional[dict] = dict(type='BN'),
act_cfg: Optional[dict] = dict(type='GELU'),
init_cfg: Optional[dict] = None
):
super().__init__(init_cfg)
self.weight = nn.Parameter(torch.ones(normalized_shape))
self.bias = nn.Parameter(torch.zeros(normalized_shape))
self.eps = eps
self.data_format = data_format
if self.data_format not in ["channels_last", "channels_first"]:
raise NotImplementedError
self.normalized_shape = (normalized_shape,)
def forward(self, x):
if self.data_format == "channels_last":
return F.layer_norm(x, self.normalized_shape, self.weight, self.bias, self.eps)
elif self.data_format == "channels_first":
u = x.mean(1, keepdim=True)
s = (x - u).pow(2).mean(1, keepdim=True)
x = (x - u) / torch.sqrt(s + self.eps)
x = self.weight[:, None, None] * x + self.bias[:, None, None]
return x
class MOD_mutil1(BaseModule):
def __init__(self,
dim_in:int,
dim_out:int,
x:int=8,
y:int=8,
norm_cfg: Optional[dict] = dict(type='BN'),
act_cfg: Optional[dict] = dict(type='GELU'),
conv_cfg: Optional[dict] = dict(type='Conv1d'),
init_cfg: Optional[dict] = None
):
super().__init__(init_cfg)
c_dim_in = dim_in//4
k_size =3
pad =(k_size -1) // 2
self.branch_conv = BRANCH_CONV4(c_dim_in,norm_cfg=norm_cfg,act_cfg=act_cfg)
self.params_c = nn.Parameter(torch.Tensor(1, c_dim_in, 1, 1), requires_grad=True)
# self.conv_c = nn.Sequential(nn.Conv2d(c_dim_in, c_dim_in, kernel_size=k_size, padding=pad, groups=c_dim_in), nn.GELU(), nn.Conv2d(c_dim_in, c_dim_in, 1))
self.conv_c = nn.Sequential(ConvModule(c_dim_in, c_dim_in, kernel_size=k_size, padding=pad, groups=c_dim_in,norm_cfg=None,act_cfg=act_cfg),
ConvModule(c_dim_in, c_dim_in, 1,norm_cfg=None, act_cfg=None)
)
self.params_x = nn.Parameter(torch.Tensor(1, 1, x, 1), requires_grad=True)
nn.init.ones_(self.params_x)
# self.conv_x = nn.Sequential(nn.Conv1d(c_dim_in, c_dim_in, kernel_size=k_size, padding=pad, groups=c_dim_in), nn.GELU(), nn.Conv1d(c_dim_in, c_dim_in, 1))
self.conv_x = nn.Sequential(ConvModule(c_dim_in, c_dim_in, kernel_size=k_size, padding=pad, groups=c_dim_in,norm_cfg=None,act_cfg=act_cfg,conv_cfg=conv_cfg),
ConvModule(c_dim_in, c_dim_in, 1,norm_cfg=None,act_cfg=None,conv_cfg=conv_cfg)
)
self.params_y = nn.Parameter(torch.Tensor(1, 1, 1, y), requires_grad=True)
nn.init.ones_(self.params_y)
self.conv_y = nn.Sequential(ConvModule(c_dim_in, c_dim_in, kernel_size=k_size, padding=pad, groups=c_dim_in,norm_cfg=None,act_cfg=act_cfg,conv_cfg=conv_cfg),
ConvModule(c_dim_in, c_dim_in, 1,norm_cfg=None,act_cfg=None,conv_cfg=conv_cfg)
)
self.dw = nn.Sequential(ConvModule(c_dim_in,c_dim_in,1,norm_cfg=None,act_cfg=act_cfg),
ConvModule(c_dim_in,c_dim_in,3,1,1,groups=c_dim_in,norm_cfg=None,act_cfg=None)
)
self.norm1 = LayerNorm(dim_in, eps=1e-6, data_format='channels_first',norm_cfg=norm_cfg,act_cfg=act_cfg)
self.norm2 = LayerNorm(dim_in, eps=1e-6, data_format='channels_first',norm_cfg=norm_cfg,act_cfg=act_cfg)
self.ldw = nn.Sequential(ConvModule(dim_in,dim_in,3,1,1,groups=dim_in,norm_cfg=None,act_cfg=act_cfg),
ConvModule(dim_in,dim_out,1,1,norm_cfg=None,act_cfg=None)
)
def forward(self, x):
x = self.norm1(x)
x1, x2, x3, x4 = torch.chunk(x, 4, dim=1)
params_c = self.params_c
x1 = x1 * self.conv_c(F.interpolate(params_c, size=x1.shape[2:4] ,mode='bilinear', align_corners=True))
x1 = self.branch_conv(x1)
x2 = x2.permute(0, 3, 1, 2)
params_x = self.params_x
x2 = x2 * self.conv_x(F.interpolate(params_x, size=x2.shape[2:4] ,mode='bilinear', align_corners=True).squeeze(0)).unsqueeze(0)
x2 = x2.permute(0, 2, 3, 1)
x2 = self.branch_conv(x2)
x3 = x3.permute(0, 2, 1, 3)
params_y = self.params_y
x3 = x3 * self.conv_y(F.interpolate(params_y, size=x3.shape[2:4] ,mode='bilinear', align_corners=True).squeeze(0)).unsqueeze(0)
x3 = x3.permute(0, 2, 1, 3)
x3 = self.branch_conv(x3)
x4 = self.dw(x4)
x4 = self.branch_conv(x4)
x = torch.cat([x1,x2,x3,x4],dim=1)
x = self.norm2(x)
x = self.ldw(x)
return x
'''---------------------------------------------------------------------------------------------------------------------------------------'''
class LayerNorm(BaseModule):
def __init__(self,
normalized_shape,
eps=1e-6,
data_format="channels_last",
norm_cfg: Optional[dict] = dict(type='BN', momentum=0.03, eps=0.001),
act_cfg: Optional[dict] = dict(type='GELU'),
init_cfg: Optional[dict] = None
):
super().__init__(init_cfg)
self.weight = nn.Parameter(torch.ones(normalized_shape))
self.bias = nn.Parameter(torch.zeros(normalized_shape))
self.eps = eps
self.data_format = data_format
if self.data_format not in ["channels_last", "channels_first"]:
raise NotImplementedError
self.normalized_shape = (normalized_shape,)
def forward(self, x):
if self.data_format == "channels_last":
return F.layer_norm(x, self.normalized_shape, self.weight, self.bias, self.eps)
elif self.data_format == "channels_first":
u = x.mean(1, keepdim=True)
s = (x - u).pow(2).mean(1, keepdim=True)
x = (x - u) / torch.sqrt(s + self.eps)
x = self.weight[:, None, None] * x + self.bias[:, None, None]
return x
class ASPPPoolingH(nn.Sequential):
def __init__(self, in_channels, out_channels):
super(ASPPPoolingH, self).__init__(
nn.AdaptiveAvgPool2d((32, 1)),
nn.Conv2d(in_channels, out_channels, 1, bias=False),
nn.BatchNorm2d(out_channels),
nn.GELU())
def forward(self, x):
size = x.shape[-2:]
for mod in self:
x = mod(x)
return F.interpolate(x, size=size, mode='bilinear', align_corners=False)
class ASPPPoolingW(nn.Sequential):
def __init__(self, in_channels, out_channels):
super(ASPPPoolingW, self).__init__(
nn.AdaptiveAvgPool2d((1, 32)),
nn.Conv2d(in_channels, out_channels, 1, bias=False),
nn.BatchNorm2d(out_channels),
nn.GELU())
def forward(self, x):
size = x.shape[-2:]
for mod in self:
x = mod(x)
return F.interpolate(x, size=size, mode='bilinear', align_corners=False)
class rot_attconv(BaseModule):
def __init__(self,
in_channels,
n_filters,
inp=False,
norm_cfg: Optional[dict] = dict(type='BN', momentum=0.03, eps=0.001),
act_cfg: Optional[dict] = dict(type='GELU'),
init_cfg: Optional[dict] = None
):
super().__init__(init_cfg)
self.inp = inp
self.deconv1 = ConvModule(
in_channels, n_filters, (1, 9), padding=(0, 4),norm_cfg=None,act_cfg=None)
self.deconv2 = ConvModule(
in_channels, n_filters, (9, 1), padding=(4, 0),norm_cfg=None,act_cfg=None)
self.deconv3 = ConvModule(
in_channels, n_filters, (9, 1), padding=(4, 0),norm_cfg=None,act_cfg=None)
self.deconv4 = ConvModule(
in_channels, n_filters, (1, 9), padding=(0, 4),norm_cfg=None,act_cfg=None)
self.ASPPH = ASPPPoolingH(in_channels=in_channels, out_channels=n_filters)
self.ASPPW = ASPPPoolingW(in_channels=in_channels, out_channels=n_filters)
for m in self.modules():
if isinstance(m, nn.Conv2d) or isinstance(m, nn.ConvTranspose2d):
if m.bias is not None:
m.bias.data.zero_()
self.conv = ConvModule(in_channels * 6,n_filters,1,norm_cfg=None,act_cfg=None)
self.LN = LayerNorm(n_filters, eps=1e-6, data_format='channels_first',norm_cfg=norm_cfg,act_cfg=act_cfg)
self.GELU = nn.GELU()
def forward(self, x):
x1 = self.deconv1(x)
x2 = self.deconv2(x)
x3 = self.inv_h_transform(self.deconv3(self.h_transform(x)))
x4 = self.inv_v_transform(self.deconv4(self.v_transform(x)))
x5 = self.ASPPH(x)
x6 = self.ASPPW(x)
x = torch.cat((x1, x2, x3, x4, x5, x6), 1)
if self.inp:
x = F.interpolate(x, scale_factor=2)
x = self.conv(x)
x = self.LN(x)
x = self.GELU(x)
return x
def h_transform(self, x):
shape = x.size()
x = torch.nn.functional.pad(x, (0, shape[-1]))
x = x.reshape(shape[0], shape[1], -1)[..., :-shape[-1]]
x = x.reshape(shape[0], shape[1], shape[2], 2*shape[3]-1)
return x
def inv_h_transform(self, x):
shape = x.size()
x = x.reshape(shape[0], shape[1], -1).contiguous()
x = torch.nn.functional.pad(x, (0, shape[-2]))
x = x.reshape(shape[0], shape[1], shape[-2], 2*shape[-2])
x = x[..., 0: shape[-2]]
return x
def v_transform(self, x):
x = x.permute(0, 1, 3, 2)
shape = x.size()
x = torch.nn.functional.pad(x, (0, shape[-1]))
x = x.reshape(shape[0], shape[1], -1)[..., :-shape[-1]]
x = x.reshape(shape[0], shape[1], shape[2], 2*shape[3]-1)
return x.permute(0, 1, 3, 2)
def inv_v_transform(self, x):
x = x.permute(0, 1, 3, 2)
shape = x.size()
x = x.reshape(shape[0], shape[1], -1)
x = torch.nn.functional.pad(x, (0, shape[-2]))
x = x.reshape(shape[0], shape[1], shape[-2], 2*shape[-2])
x = x[..., 0: shape[-2]]
return x.permute(0, 1, 3, 2)
'''------------------------------------------------------------FMCM2----------------------------------------------------------------------'''
class DepthWiseConv2d(BaseModule):
def __init__(self,
dim_in:int,
dim_out:int,
kernel_size:int=3,
padding:int=1,
stride:int=1,
dilation:int=1,
norm_cfg: Optional[dict] = dict(type='BN'),
act_cfg: Optional[dict] = dict(type='GELU'),
init_cfg: Optional[dict] = None
):
super().__init__(init_cfg)
# self.conv1 = nn.Conv2d(dim_in, dim_in, kernel_size=kernel_size, padding=padding,
# stride=stride, dilation=dilation, groups=dim_in)
self.conv1 = ConvModule(dim_in,dim_in, kernel_size=kernel_size,stride=stride,
padding=padding,dilation=dilation,groups=dim_in,norm_cfg=None,act_cfg=None)
self.norm_layer = nn.GroupNorm(4, dim_in)
# self.conv2 = nn.Conv2d(dim_in, dim_out, kernel_size=1)
self.conv2 = ConvModule(dim_in, dim_out, kernel_size=1,norm_cfg=None,act_cfg=None)
def forward(self, x):
return self.conv2(self.norm_layer(self.conv1(x)))
class LayerNorm(BaseModule):
def __init__(self,
normalized_shape,
eps=1e-6,
data_format="channels_last",
norm_cfg: Optional[dict] = dict(type='BN'),
act_cfg: Optional[dict] = dict(type='GELU'),
init_cfg: Optional[dict] = None
):
super().__init__(init_cfg)
self.weight = nn.Parameter(torch.ones(normalized_shape))
self.bias = nn.Parameter(torch.zeros(normalized_shape))
self.eps = eps
self.data_format = data_format
if self.data_format not in ["channels_last", "channels_first"]:
raise NotImplementedError
self.normalized_shape = (normalized_shape,)
def forward(self, x):
if self.data_format == "channels_last":
return F.layer_norm(x, self.normalized_shape, self.weight, self.bias, self.eps)
elif self.data_format == "channels_first":
u = x.mean(1, keepdim=True)
s = (x - u).pow(2).mean(1, keepdim=True)
x = (x - u) / torch.sqrt(s + self.eps)
x = self.weight[:, None, None] * x + self.bias[:, None, None]
return x
class MOD_mutil2(BaseModule):
def __init__(self,
dim_in:int,
dim_out:int,
x:int=8,
y:int=8,
norm_cfg: Optional[dict] = dict(type='BN'),
act_cfg: Optional[dict] = dict(type='GELU'),
conv_cfg: Optional[dict] = dict(type='Conv1d'),
init_cfg: Optional[dict] = None
):
super().__init__(init_cfg)
c_dim_in = dim_in//4
k_size =3
pad =(k_size -1) // 2
self.branch_conv = BRANCH_CONV16(c_dim_in,norm_cfg=norm_cfg,act_cfg=act_cfg)
# self.SRU16 = ConvModule(c_dim_in,c_dim_in,1,1,norm_cfg=norm_cfg,act_cfg=act_cfg)
self.params_c = nn.Parameter(torch.Tensor(1, c_dim_in, 1, 1), requires_grad=True)
nn.init.ones_(self.params_c)
# self.conv_c = nn.Sequential(nn.Conv2d(c_dim_in, c_dim_in, kernel_size=k_size, padding=pad, groups=c_dim_in), nn.GELU(), nn.Conv2d(c_dim_in, c_dim_in, 1))
self.conv_c = nn.Sequential(ConvModule(c_dim_in, c_dim_in, kernel_size=k_size, padding=pad, groups=c_dim_in,norm_cfg=None,act_cfg=act_cfg),
ConvModule(c_dim_in, c_dim_in, 1, norm_cfg=None, act_cfg=None)
)
self.params_x = nn.Parameter(torch.Tensor(1, 1, x, 1), requires_grad=True)
nn.init.ones_(self.params_x)
# self.conv_x = nn.Sequential(nn.Conv1d(c_dim_in, c_dim_in, kernel_size=k_size, padding=pad, groups=c_dim_in), nn.GELU(), nn.Conv1d(c_dim_in, c_dim_in, 1))
self.conv_x = nn.Sequential(ConvModule(c_dim_in, c_dim_in, kernel_size=k_size, padding=pad, groups=c_dim_in,norm_cfg=None,act_cfg=act_cfg,conv_cfg=conv_cfg),
ConvModule(c_dim_in, c_dim_in, 1,norm_cfg=None,act_cfg=None,conv_cfg=conv_cfg)
)
self.params_y = nn.Parameter(torch.Tensor(1, 1, 1, y), requires_grad=True)
nn.init.ones_(self.params_y)
# self.conv_y = nn.Sequential(nn.Conv1d(c_dim_in, c_dim_in, kernel_size=k_size, padding=pad, groups=c_dim_in), nn.GELU(), nn.Conv1d(c_dim_in, c_dim_in, 1))
self.conv_y = nn.Sequential(ConvModule(c_dim_in, c_dim_in, kernel_size=k_size, padding=pad, groups=c_dim_in,norm_cfg=None,act_cfg=act_cfg,conv_cfg=conv_cfg),
ConvModule(c_dim_in, c_dim_in, 1,norm_cfg=None,act_cfg=None,conv_cfg=conv_cfg)
)
self.dw = nn.Sequential(ConvModule(c_dim_in,c_dim_in,1,norm_cfg=None,act_cfg=act_cfg),
ConvModule(c_dim_in,c_dim_in,3,1,1,groups=c_dim_in,norm_cfg=None,act_cfg=None)
)
self.norm1 = LayerNorm(dim_in, eps=1e-6, data_format='channels_first', norm_cfg=norm_cfg, act_cfg=act_cfg)
self.norm2 = LayerNorm(dim_in, eps=1e-6, data_format='channels_first', norm_cfg=norm_cfg, act_cfg=act_cfg)
self.ldw = nn.Sequential(ConvModule(dim_in,dim_in,3,1,1,groups=dim_in,norm_cfg=None,act_cfg=act_cfg),
ConvModule(dim_in,dim_out,1,1,norm_cfg=None,act_cfg=None)
)
def forward(self, x):
x = self.norm1(x)
x1, x2, x3, x4 = torch.chunk(x, 4, dim=1)
params_c = self.params_c
x1 = x1 * self.conv_c(F.interpolate(params_c, size=x1.shape[2:4] ,mode='bilinear', align_corners=True))
x1 = self.branch_conv(x1)
x2 = x2.permute(0, 3, 1, 2)
params_x = self.params_x
x2 = x2 * self.conv_x(F.interpolate(params_x, size=x2.shape[2:4] ,mode='bilinear', align_corners=True).squeeze(0)).unsqueeze(0)
x2 = x2.permute(0, 2, 3, 1)
x2 = self.branch_conv(x2)
x3 = x3.permute(0, 2, 1, 3)
params_y = self.params_y
x3 = x3 * self.conv_y(F.interpolate(params_y, size=x3.shape[2:4] ,mode='bilinear', align_corners=True).squeeze(0)).unsqueeze(0)
x3 = x3.permute(0, 2, 1, 3)
x3 = self.branch_conv(x3)
x4 = self.dw(x4)
x4 = self.branch_conv(x4)
x = torch.cat([x1,x2,x3,x4],dim=1)
x = self.norm2(x)
x = self.ldw(x)
return x
'''----------------------------------------------------------------------------------------------------------------------'''
class LayerNorm(BaseModule):
def __init__(self,
normalized_shape,
eps=1e-6,
data_format="channels_last",
norm_cfg: Optional[dict] = dict(type='BN'),
act_cfg: Optional[dict] = dict(type='GELU'),
init_cfg: Optional[dict] = None
):
super().__init__(init_cfg)
self.weight = nn.Parameter(torch.ones(normalized_shape))
self.bias = nn.Parameter(torch.zeros(normalized_shape))
self.eps = eps
self.data_format = data_format
if self.data_format not in ["channels_last", "channels_first"]:
raise NotImplementedError
self.normalized_shape = (normalized_shape,)
def forward(self, x):
if self.data_format == "channels_last":
return F.layer_norm(x, self.normalized_shape, self.weight, self.bias, self.eps)
elif self.data_format == "channels_first":
u = x.mean(1, keepdim=True)
s = (x - u).pow(2).mean(1, keepdim=True)
x = (x - u) / torch.sqrt(s + self.eps)
x = self.weight[:, None, None] * x + self.bias[:, None, None]
return x
class MOD_coa(BaseModule):
def __init__(self, in_channels,
n_filters,
coa_kernel_size: int = 11,
inp:bool=False,
norm_cfg: Optional[dict] = dict(type='BN'),
act_cfg=dict(type='GELU'),
init_cfg=None):
super().__init__(init_cfg)
self.conv1 = ConvModule(in_channels, in_channels // 4, 1, 1, 0, groups=in_channels // 4,
bias=False, norm_cfg=norm_cfg, act_cfg=act_cfg)
self.inp = inp
self.deconv1 = ConvModule(
in_channels // 4, in_channels // 8, (1, coa_kernel_size), padding=(0, coa_kernel_size // 2), norm_cfg=None,
act_cfg=None)
self.deconv2 = ConvModule(
in_channels // 4, in_channels // 8, (coa_kernel_size, 1), padding=(coa_kernel_size // 2, 0), norm_cfg=None,
act_cfg=None
)
self.deconv3 = ConvModule(
in_channels // 4, in_channels // 8, (coa_kernel_size, 1), padding=(coa_kernel_size // 2, 0), norm_cfg=None,
act_cfg=None
)
self.deconv4 = ConvModule(
in_channels // 4, in_channels // 8, (1, coa_kernel_size), padding=(0, coa_kernel_size // 2), norm_cfg=None,
act_cfg=None
)
self.conv_ = ConvModule(in_channels // 2, in_channels // 2, 1, 1, norm_cfg=norm_cfg, act_cfg=act_cfg)
self.conv3 = ConvModule(
in_channels // 2, n_filters, 1, 1, norm_cfg=None, act_cfg=None)
for m in self.modules():
if isinstance(m, nn.Conv2d) or isinstance(m, nn.ConvTranspose2d):
if m.bias is not None:
m.bias.data.zero_()
self.LN = LayerNorm(n_filters, eps=1e-6, data_format='channels_first',norm_cfg=norm_cfg,act_cfg=act_cfg)
self.GELU = build_activation_layer(act_cfg)
def forward(self, x):
x = self.conv1(x)
x1 = self.deconv1(x)
x2 = self.deconv2(x)
x3 = self.inv_h_transform(self.deconv3(self.h_transform(x)))
x4 = self.inv_v_transform(self.deconv4(self.v_transform(x)))
x = torch.cat((x1, x2, x3, x4), 1)
if self.inp:
x = F.interpolate(x, scale_factor=2)
x = self.conv_(x)
x = self.conv3(x)
x = self.LN(x)
x = self.GELU(x)
return x
def h_transform(self, x):
shape = x.size()
x = torch.nn.functional.pad(x, (0, shape[-1]))
x = x.reshape(shape[0], shape[1], -1)[..., :-shape[-1]]
x = x.reshape(shape[0], shape[1], shape[2], 2*shape[3]-1)
return x
def inv_h_transform(self, x):
shape = x.size()
x = x.reshape(shape[0], shape[1], -1).contiguous()
x = torch.nn.functional.pad(x, (0, shape[-2]))
x = x.reshape(shape[0], shape[1], shape[-2], 2*shape[-2])
x = x[..., 0: shape[-2]]
return x
def v_transform(self, x):
x = x.permute(0, 1, 3, 2)
shape = x.size()
x = torch.nn.functional.pad(x, (0, shape[-1]))
x = x.reshape(shape[0], shape[1], -1)[..., :-shape[-1]]
x = x.reshape(shape[0], shape[1], shape[2], 2*shape[3]-1)
return x.permute(0, 1, 3, 2)
def inv_v_transform(self, x):
x = x.permute(0, 1, 3, 2)
shape = x.size()
x = x.reshape(shape[0], shape[1], -1)
x = torch.nn.functional.pad(x, (0, shape[-2]))
x = x.reshape(shape[0], shape[1], shape[-2], 2*shape[-2])
x = x[..., 0: shape[-2]]
return x.permute(0, 1, 3, 2)
'''--------------------------------------------------downsample----------------------------------------------------------'''
class DownSamplingLayer(BaseModule):
"""Down sampling layer"""
def __init__(
self,
in_channels: int,
out_channels: Optional[int] = None,
norm_cfg: Optional[dict] = dict(type='BN', momentum=0.03, eps=0.001),
act_cfg: Optional[dict] = dict(type='GELU'),
init_cfg: Optional[dict] = None,
):
super().__init__(init_cfg)
out_channels = out_channels or (in_channels * 2)
self.down_conv = ConvModule(in_channels, out_channels, kernel_size=3, stride=2, padding=1,
norm_cfg=norm_cfg, act_cfg=act_cfg)
def forward(self, x):
return self.down_conv(x)
"""-----------------------------------------------------final_rot_conv------------------------------------------------"""
class Final_DecoderBlock(BaseModule):
def __init__(self,
in_channels,
n_filters,
in_p=True,
strip=9,
norm_cfg: Optional[dict] = dict(type='BN'),
act_cfg: Optional[dict] = dict(type='GELU'),
init_cfg: Optional[dict] = None
):
super(Final_DecoderBlock, self).__init__(init_cfg)
out_pad = 1 if in_p else 0
stride = 2 if in_p else 1
self.cbr1 = ConvModule(in_channels,in_channels//4,1,1,norm_cfg=norm_cfg,act_cfg=act_cfg)
self.cbr2 = ConvModule(in_channels, in_channels // 2, 1,1,norm_cfg=norm_cfg,act_cfg=act_cfg)
self.deconv1 = ConvModule(in_channels // 4, in_channels // 4, (1, strip), padding=(0, strip//2),norm_cfg=None,act_cfg=None)
self.deconv2 = ConvModule( in_channels // 4, in_channels // 4, (strip, 1), padding=(strip//2, 0),norm_cfg=None,act_cfg=None)
self.deconv3 = ConvModule(in_channels // 4, in_channels // 4, (strip, 1), padding=(strip//2, 0),norm_cfg=None,act_cfg=None)
self.deconv4 = ConvModule(in_channels // 4, in_channels // 4, (strip, 1), padding=(strip//2, 0),norm_cfg=None,act_cfg=None)
self.cbr3_1 = ConvModule(in_channels // 4 + in_channels // 2, in_channels // 4, 1,norm_cfg=norm_cfg,act_cfg=act_cfg)
self.cbr3_2 = ConvModule(in_channels // 4 + in_channels // 2, in_channels // 4, 1,norm_cfg=norm_cfg,act_cfg=act_cfg)
self.cbr3_3 = ConvModule(in_channels // 4 + in_channels // 2, in_channels // 4, 1,norm_cfg=norm_cfg,act_cfg=act_cfg)
self.cbr3_4 = ConvModule(in_channels // 4 + in_channels // 2, in_channels // 4, 1,norm_cfg=norm_cfg,act_cfg=act_cfg)
self.deconvbr = nn.Sequential(nn.ConvTranspose2d(in_channels, in_channels // 4 + in_channels // 4,
3, stride=stride, padding=1, output_padding=out_pad),
nn.BatchNorm2d(in_channels // 4 + in_channels // 4),
nn.ReLU(inplace=True), )
self.conv3 = ConvModule(in_channels // 4 + in_channels // 4, n_filters, 1,norm_cfg=norm_cfg)
_,self.bn3 = build_norm_layer(norm_cfg,n_filters)
self.relu3 = build_activation_layer(act_cfg)
def forward(self, x, inp=False):
x01 = self.cbr1(x)
x02 = self.cbr2(x)
x1 = self.deconv1(x01)
x2 = self.deconv2(x01)
x3 = tensor_rotate(self.deconv3(tensor_rotate(x01, 45)), -45)
x4 = tensor_rotate(self.deconv4(tensor_rotate(x01,135)),-135)
x1 = self.cbr3_1(torch.cat((x1, x02), 1))
x2 = self.cbr3_2(torch.cat((x2, x02), 1))
x3 = self.cbr3_3(torch.cat((x3, x02), 1))
x4 = self.cbr3_4(torch.cat((x4, x02), 1))
x = torch.cat((x1, x2, x3, x4), 1)
x = self.deconvbr(x)
x = self.conv3(x)
x = self.bn3(x)
x = self.relu3(x)
return x
'''------------------------------------------------------Net-------------------------------------------------------------'''
@MODELS.register_module()
class EFMOD(BaseModule):
def __init__(self,
in_channels=3,
base_channels=32,
norm_eval: bool = False, # 是否在评估模式下使用归一化,默认为false
norm_cfg: Optional[dict] = dict(type='BN'),
act_cfg: Optional[dict] = dict(type='GELU'),
init_cfg: Optional[dict] = [dict(type='Kaiming',layer='Conv2d',
a=math.sqrt(5),
distribution='uniform',
mode='fan_in',
nonlinearity='leaky_relu'),
dict(type='Constant',val=1,layer=['_BatchNorm', 'GroupNorm'])] # 初始化配置字典
):
super().__init__(init_cfg)
self.input_channel = in_channels
self.class_num = base_channels
self.norm_eval = norm_eval
out_channels = [32, 64, 128, 256, 512]
self.conv1 = MFE_Block(self.input_channel,out_channels[0],norm_cfg=norm_cfg,act_cfg=act_cfg) #(bs,c,h,w)→(bs,32,h,w)