-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUMLP_plus.py
1343 lines (1006 loc) · 47.6 KB
/
UMLP_plus.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 -*-
"""
Created on Mon Jul 12 14:29:40 2021
@author: 5106
"""
# -*- coding: utf-8 -*-
"""
Created on Sat Jul 10 17:27:27 2021
@author: 5106
"""
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from einops.layers.torch import Rearrange
from einops import rearrange
from timm.data import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD
from timm.models.layers import DropPath, trunc_normal_
from timm.models.registry import register_model
from timm.models.layers.helpers import to_2tuple
import math
from torch import Tensor
from torch.nn import init
from torch.nn.modules.utils import _pair
from torchvision.ops.deform_conv import deform_conv2d as deform_conv2d_tv
# MLP module
class Mlp(nn.Module):
def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=False , drop=0.):
super().__init__()
out_features = out_features or in_features
hidden_features = hidden_features or in_features
self.fc1 = nn.Linear(in_features, hidden_features)
self.act = nn.LeakyReLU(negative_slope=0.3, inplace=False)
self.fc2 = nn.Linear(hidden_features, out_features)
self.drop = nn.Dropout(drop)
def forward(self, x):
x = self.fc1(x)
x = self.act(x)
x = self.drop(x)
x = self.fc2(x)
x = self.drop(x)
return x
#https://zhuanlan.zhihu.com/p/569674523
class CycleFC(nn.Module):
def __init__(
self,
in_channels: int,
out_channels: int,
kernel_size, # re-defined kernel_size, represent the spatial area of staircase FC
stride: int = 1,
padding: int = 0,
dilation: int = 1,
groups: int = 1,
bias: bool = True,
):
"""
这里的kernel_size实际使用的时候时3x1或者1x3
"""
super(CycleFC, self).__init__()
if in_channels % groups != 0:
raise ValueError('in_channels must be divisible by groups')
if out_channels % groups != 0:
raise ValueError('out_channels must be divisible by groups')
if stride != 1:
raise ValueError('stride must be 1')
if padding != 0:
raise ValueError('padding must be 0')
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.stride = _pair(stride)
self.padding = _pair(padding)
self.dilation = _pair(dilation)
self.groups = groups
# 被偏移调整的1x1卷积的权重,由于后面使用torchvision提供的可变形卷积的函数,所以权重需要自己构造
self.weight = nn.Parameter(torch.empty(out_channels, in_channels // groups, 1, 1))
# kernel size == 1
if bias:
self.bias = nn.Parameter(torch.empty(out_channels))
else:
self.register_parameter('bias', None)
# 要注意,这里是在注册一个buffer,是一个常量,不可学习,但是可以保存到模型权重中。
self.register_buffer('offset', self.gen_offset())
def gen_offset(self):
"""
生成卷积核偏移量的核心操作。
要想理解这一函数的操作,需要首先理解后面使用的deform_conv2d_tv的具体用法。
具体可见:https://pytorch.org/vision/0.10/ops.html#torchvision.ops.deform_conv2d
这里对于offset参数的要求是:
offset (Tensor[batch_size,
2 * offset_groups * kernel_height * kernel_width,
out_height,
out_width])
– offsets to be applied for each position in the convolution kernel.
也就是说,对于样本s的输出特征图的通道c中的位置(x,y),这个函数会从offset中取出,形状为
kernel_height*kernel_width的卷积核所对应的偏移参数为
offset[s, 0:2*offset_groups*kernel_height*kernel_width, x, y]
也就是这一系列参数都是对应样本s的单个位置(x,y)的。
针对不同的位置可以有不同的offset,也可以有相同的(下面的实现就是后者)。
对于这2*offset_groups*kernel_height*kernel_width个数,涉及到对于输入特征通道的分组。
将其分成offset_groups组,每份单独拥有一组对应于卷积核中心位置的相对偏移量,
共2*kernel_height*kernel_width个数。
对于每个核参数,使用两个量来描述偏移,即h方向和w方向相对中心位置的偏移,
即下面代码中的减去kernel_height//2或者kernel_width//2。
需要注意的是,当偏移位置位于padding后的tensor边界外,则是将网格使用0补齐。
如果网格上有边界值,则使用边界值和用0补齐的网格顶点来计算双线性插值的结果。
"""
offset = torch.empty(1, self.in_channels*2, 1, 1)
start_idx = (self.kernel_size[0] * self.kernel_size[1]) // 2
assert self.kernel_size[0] == 1 or self.kernel_size[1] == 1, self.kernel_size
for i in range(self.in_channels):
if self.kernel_size[0] == 1:
offset[0, 2 * i + 0, 0, 0] = 0
# 这里计算了一个相对偏移位置。
# deform_conv2d使用的以对应输出位置为中心的偏移坐标索引方式
offset[0, 2 * i + 1, 0, 0] = (
(i + start_idx) % self.kernel_size[1] - (self.kernel_size[1] // 2)
)
else:
offset[0, 2 * i + 0, 0, 0] = (
(i + start_idx) % self.kernel_size[0] - (self.kernel_size[0] // 2)
)
offset[0, 2 * i + 1, 0, 0] = 0
return offset
def forward(self, input: Tensor) -> Tensor:
"""
Args:
input (Tensor[batch_size, in_channels, in_height, in_width]): input tensor
"""
B, C, H, W = input.size()
return deform_conv2d_tv(input,
self.offset.expand(B, -1, H, W),
self.weight,
self.bias,
stride=self.stride,
padding=self.padding,
dilation=self.dilation)
class CycleMLP(nn.Module):
def __init__(self, dim, qkv_bias=False, qk_scale=None, attn_drop=0., proj_drop=0.):
super().__init__()
self.mlp_c = nn.Linear(dim, dim, bias=qkv_bias)
self.sfc_h = CycleFC(dim, dim, (1, 3), 1, 0)
self.sfc_w = CycleFC(dim, dim, (3, 1), 1, 0)
self.reweight = Mlp(dim, dim // 4, dim * 3)
self.proj = nn.Linear(dim, dim)
self.proj_drop = nn.Dropout(proj_drop)
def forward(self, x):
B, H, W, C = x.shape
h = self.sfc_h(x.permute(0, 3, 1, 2)).permute(0, 2, 3, 1)
w = self.sfc_w(x.permute(0, 3, 1, 2)).permute(0, 2, 3, 1)
c = self.mlp_c(x)
a = (h + w + c).permute(0, 3, 1, 2).flatten(2).mean(2)
a = self.reweight(a).reshape(B, C, 3).permute(2, 0, 1).softmax(dim=0).unsqueeze(2).unsqueeze(2)
x = h * a[0] + w * a[1] + c * a[2]
x = self.proj(x)
x = self.proj_drop(x)
return x
class CycleBlock(nn.Module):
def __init__(self, dim, mlp_ratio=4., qkv_bias=False, qk_scale=None, drop=0., attn_drop=0.,
drop_path=0., act_layer=nn.GELU, norm_layer=nn.LayerNorm, skip_lam=1.0, mlp_fn=CycleMLP):
super().__init__()
self.norm1 = norm_layer(dim)
self.attn = mlp_fn(dim, qkv_bias=qkv_bias, qk_scale=None, attn_drop=attn_drop)
# NOTE: drop path for stochastic depth, we shall see if this is better than dropout here
self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity()
self.norm2 = norm_layer(dim)
mlp_hidden_dim = int(dim * mlp_ratio)
self.mlp = Mlp(in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer)
self.skip_lam = skip_lam
def forward(self, x):
x = x + self.drop_path(self.attn(self.norm1(x))) / self.skip_lam
x = x + self.drop_path(self.mlp(self.norm2(x))) / self.skip_lam
return x
def spatial_shift1(x):
b,w,h,c = x.size()
x[:,1:,:,:c//4] = x[:,:w-1,:,:c//4]
x[:,:w-1,:,c//4:c//2] = x[:,1:,:,c//4:c//2]
x[:,:,1:,c//2:c*3//4] = x[:,:,:h-1,c//2:c*3//4]
x[:,:,:h-1,3*c//4:] = x[:,:,1:,3*c//4:]
return x
def spatial_shift2(x):
b,w,h,c = x.size()
x[:,:,1:,:c//4] = x[:,:,:h-1,:c//4]
x[:,:,:h-1,c//4:c//2] = x[:,:,1:,c//4:c//2]
x[:,1:,:,c//2:c*3//4] = x[:,:w-1,:,c//2:c*3//4]
x[:,:w-1,:,3*c//4:] = x[:,1:,:,3*c//4:]
return x
class SplitAttention(nn.Module):
def __init__(self,channel=512,k=3):
super().__init__()
self.channel=channel
self.k=k
self.mlp1=nn.Linear(channel,channel,bias=False)
self.gelu=nn.GELU()
self.mlp2=nn.Linear(channel,channel*k,bias=False)
self.softmax=nn.Softmax(1)
def forward(self,x_all):
b,k,h,w,c=x_all.shape
x_all=x_all.reshape(b,k,-1,c) #bs,k,n,c
a=torch.sum(torch.sum(x_all,1),1) #bs,c
hat_a=self.mlp2(self.gelu(self.mlp1(a))) #bs,kc
hat_a=hat_a.reshape(b,self.k,c) #bs,k,c
bar_a=self.softmax(hat_a) #bs,k,c
attention=bar_a.unsqueeze(-2) # #bs,k,1,c
out=attention*x_all # #bs,k,n,c
out=torch.sum(out,1).reshape(b,h,w,c)
return out
class S2Attention(nn.Module):
def __init__(self, channels=512 ):
super().__init__()
self.mlp1 = nn.Linear(channels,channels*3)
self.mlp2 = nn.Linear(channels,channels)
self.split_attention = SplitAttention(channel=channels)
def forward(self, x):
b,h,w,c = x.size()
x = self.mlp1(x)
x1 = spatial_shift1(x[:,:,:,:c])
x2 = spatial_shift2(x[:,:,:,c:c*2])
x3 = x[:,:,:,c*2:]
x_all=torch.stack([x1,x2,x3],1)
a = self.split_attention(x_all)
x = self.mlp2(a)
# x=x.permute(0,3,1,2)
return x
# MLP-based Permutator module
# class WeightedPermuteMLP(nn.Module):
# def __init__(self, dim1, dim2, dim3, segment_dim=8, qkv_bias=False, qk_scale=None, attn_drop=0., proj_drop=0.1):
# super().__init__()
# self.segment_dim = segment_dim
# self.mlp_c = nn.Linear(dim1, dim1, bias=qkv_bias)
# self.mlp_h = nn.Linear(dim2, dim2, bias=qkv_bias)
# self.mlp_w = nn.Linear(dim3, dim3, bias=qkv_bias)
# self.reweight = Mlp(dim1, dim1 // 2, dim1 *3)
# self.proj = nn.Linear(dim1, dim1)
# self.proj_drop = nn.Dropout(proj_drop)
# seg = dim1//segment_dim
# self.complex_weight = nn.Parameter(torch.randn( dim2//seg, dim3//seg//2+1, dim1, 2, dtype=torch.float32) * 0.02)
# self.fuse=nn.Linear(4*dim1,dim1)
# # self.s2att = S2Attention(channels=dim1)
# def forward(self, x):
# B, H, W, C = x.shape
# S = C // self.segment_dim
# h = x.reshape(B, H, W, self.segment_dim, S).permute(0, 3, 2, 1, 4).reshape(B, self.segment_dim, W, H*S)
# xf = torch.fft.rfft2(x, dim=(1, 2), norm='ortho')
# weight = torch.view_as_complex(self.complex_weight)
# xf = xf * weight
# xf = torch.fft.irfft2(xf, s=(H, W), dim=(1, 2), norm='ortho')
# # xf = self.s2att(x)
# h = self.mlp_h(h).reshape(B, self.segment_dim, W, H, S).permute(0, 3, 2, 1, 4).reshape(B, H, W, C)
# w = x.reshape(B, H, W, self.segment_dim, S).permute(0, 1, 3, 2, 4).reshape(B, H, self.segment_dim, W*S)
# w = self.mlp_w(w).reshape(B, H, self.segment_dim, W, S).permute(0, 1, 3, 2, 4).reshape(B, H, W, C)
# c = self.mlp_c(x)
# # a = (h + w + c).permute(0, 3, 1, 2).flatten(2).mean(2)
# # a = self.reweight(a).reshape(B, C, 3).permute(2, 0, 1).softmax(dim=0).unsqueeze(2).unsqueeze(2)
# # x = h * a[0] + w * a[1] + c * a[2]
# x_fuse=torch.cat([h,w,c,xf],dim=3)
# x=self.fuse(x_fuse)
# # x = self.proj(x)
# x = self.proj_drop(x)
# return x
class WeightedPermuteMLP(nn.Module):
def __init__(self, dim1, dim2, dim3, segment_dim=8, qkv_bias=False, qk_scale=None, attn_drop=0., proj_drop=0.1):
super().__init__()
self.segment_dim = segment_dim
self.mlp_c = nn.Linear(dim1, dim1, bias=qkv_bias)
self.mlp_h = nn.Linear(dim2, dim2, bias=qkv_bias)
self.mlp_w = nn.Linear(dim3, dim3, bias=qkv_bias)
self.mlp_theta_c = nn.Linear(dim1, dim1, bias=qkv_bias)
self.mlp_theta_h = nn.Linear(dim2, dim2, bias=qkv_bias)
self.mlp_theta_w = nn.Linear(dim3, dim3, bias=qkv_bias)
self.reweight = Mlp(dim1, dim1 // 2, dim1 *3)
self.proj = nn.Linear(dim1, dim1)
self.proj_drop = nn.Dropout(proj_drop)
seg = dim1//segment_dim
self.complex_weight = nn.Parameter(torch.randn( dim2//seg, dim3//seg//2+1, dim1, 2, dtype=torch.float32) * 0.02)
self.fuse=nn.Linear(2*dim1,dim1)
self.s2att = S2Attention(channels=dim1)
# self.tfc_h = nn.Conv2d(2*dim1, dim1, (1,5), stride=1, padding=(0,5//2), groups=dim1, bias=False)
# self.tfc_w = nn.Conv2d(2*dim1, dim1, (5,1), stride=1, padding=(5//2,0), groups=dim1, bias=False)
self.tfc_h = nn.Linear(2*dim1,dim1)
self.tfc_w = nn.Linear(2*dim1,dim1)
def forward(self, x):
B, H, W, C = x.shape
S = C // self.segment_dim
h = x.reshape(B, H, W, self.segment_dim, S).permute(0, 3, 2, 1, 4).reshape(B, self.segment_dim, W, H*S)
xf = torch.fft.rfft2(x, dim=(1, 2), norm='ortho')
weight = torch.view_as_complex(self.complex_weight)
xf = xf * weight
xf = torch.fft.irfft2(xf, s=(H, W), dim=(1, 2), norm='ortho')
# xf = self.s2att(x)
h1 = self.mlp_h(h).reshape(B, self.segment_dim, W, H, S).permute(0, 3, 2, 1, 4).reshape(B, H, W, C)
theta_h = self.mlp_theta_h(h).reshape(B, self.segment_dim, W, H, S).permute(0, 3, 2, 1, 4).reshape(B, H, W, C)
# h = h1*torch.cos(theta_h) + h1*torch.sin(theta_h)
h = self.tfc_h(torch.cat([h1*torch.cos(theta_h), h1*torch.sin(theta_h)],dim=3) )
w = x.reshape(B, H, W, self.segment_dim, S).permute(0, 1, 3, 2, 4).reshape(B, H, self.segment_dim, W*S)
w1 = self.mlp_w(w).reshape(B, H, self.segment_dim, W, S).permute(0, 1, 3, 2, 4).reshape(B, H, W, C)
theta_w = self.mlp_theta_w(w).reshape(B, H, self.segment_dim, W, S).permute(0, 1, 3, 2, 4).reshape(B, H, W, C)
# w = w1*torch.cos(theta_w) + w1*torch.sin(theta_w)
w = self.tfc_w(torch.cat([w1*torch.cos(theta_w), w1*torch.sin(theta_w)],dim=3) )
c = self.mlp_c(x)
# c = x
a = (h + w + c).permute(0, 3, 1, 2).flatten(2).mean(2)
a = self.reweight(a).reshape(B, C, 3).permute(2, 0, 1).softmax(dim=0).unsqueeze(2).unsqueeze(2)
x = h * a[0] + w * a[1] + c * a[2]
x_fuse=torch.cat([x,xf],dim=3)
x=self.fuse(x_fuse)
# x = self.proj(x)
x = self.proj_drop(x)
return x
# MLP-based Permutator module
class WeightedPermuteMLP1(nn.Module):
def __init__(self, dim1, dim2, dim3, segment_dim=8, qkv_bias=False, qk_scale=None, attn_drop=0., proj_drop=0.1):
super().__init__()
self.segment_dim = segment_dim
self.mlp_c1 = nn.Linear(dim1//4, dim1, bias=qkv_bias)
self.mlp_c2 = nn.Linear(dim1//4, dim1, bias=qkv_bias)
self.mlp_c3 = nn.Linear(dim1, dim1, bias=qkv_bias)
self.mlp_h = nn.Linear(dim2, dim2, bias=qkv_bias)
self.mlp_w = nn.Linear(dim3, dim3, bias=qkv_bias)
self.reweight = Mlp(dim1, dim1 // 2, dim1 *3)
self.proj = nn.Linear(dim1, dim1)
self.proj_drop = nn.Dropout(proj_drop)
seg = dim1//segment_dim
self.complex_weight = nn.Parameter(torch.randn( dim2//seg, dim3//seg//2+1, dim1, 2, dtype=torch.float32) * 0.02)
self.fuse = nn.Linear(3*dim1, dim1)
def forward(self, x):
B, H, W, C = x.shape
S = C // self.segment_dim
h = x.reshape(B, H, W, self.segment_dim, S).permute(0, 3, 2, 1, 4).reshape(B, self.segment_dim, W, H*S)
h = self.mlp_h(h).reshape(B, self.segment_dim, W, H, S).permute(0, 3, 2, 1, 4).reshape(B, H, W, C)
w = x.reshape(B, H, W, self.segment_dim, S).permute(0, 1, 3, 2, 4).reshape(B, H, self.segment_dim, W*S)
w = self.mlp_w(w).reshape(B, H, self.segment_dim, W, S).permute(0, 1, 3, 2, 4).reshape(B, H, W, C)
# c1 = self.mlp_c1(x[:,:,:,0:C//4])
# c2 = self.mlp_c2(x[:,:,:,::4])
c3 = self.mlp_c3(x)
# a = (c1 + c2 + c3).permute(0, 3, 1, 2).flatten(2).mean(2)
# a = self.reweight(a).reshape(B, C, 3).permute(2, 0, 1).softmax(dim=0).unsqueeze(2).unsqueeze(2)
# c = c1 * a[0] + c2 * a[1] + c3 * a[2]
# x_fuse=torch.cat([c1,c2,c3],dim=3)
# c=self.fuse(x_fuse)
# a = (h + w + c).permute(0, 3, 1, 2).flatten(2).mean(2)
# a = self.reweight(a).reshape(B, C, 3).permute(2, 0, 1).softmax(dim=0).unsqueeze(2).unsqueeze(2)
# x = h * a[0] + w * a[1] + c * a[2]
x_fuse=torch.cat([h,w,c3],dim=3)
x=self.fuse(x_fuse)
x = self.proj(x)
x = self.proj_drop(x)
return x
# MLP-based Permutator module
class FirstMLP(nn.Module):
def __init__(self, in_ch, dim1):
super().__init__()
self.mlp_c1 = nn.Linear(in_ch//2, dim1)
self.mlp_c2 = nn.Linear(in_ch//2, dim1)
self.mlp_c3 = nn.Linear(in_ch, dim1)
self.reweight = Mlp(dim1, dim1 // 2, dim1 *3)
self.fuse = nn.Linear(4*dim1, dim1)
def forward(self, x):
x1 = x
x= x.permute(0, 2, 3, 1)
B, H, W, C = x.shape
c1 = self.mlp_c1(x[:,:,:,0:C//2])
c2 = self.mlp_c2(x[:,:,:,::2])
c3 = self.mlp_c3(x)
B, H, W, C = c3.shape
a = (c1 + c2 + c3).flatten(2).mean(2)
a = self.reweight(a).reshape(B, C, 3).permute(2, 0, 1).softmax(dim=0).unsqueeze(2).unsqueeze(2)
c = c1 * a[0] + c2 * a[1] + c3 * a[2]
x_fuse=torch.cat([c,c1,c2,c3],dim=3)
x=self.fuse(x_fuse)
x= x.permute(0, 3, 1, 2)
# x = x1 + x
return x
# Complete Permutator block
class PermutatorBlock1(nn.Module):
def __init__(self, dim1, dim2, dim3, segment_dim, mlp_ratio=4., qkv_bias=False, qk_scale=None, drop=0., attn_drop=0.,
drop_path=0., act_layer=False, norm_layer=nn.LayerNorm, skip_lam=1.0, mlp_fn = WeightedPermuteMLP1):
super().__init__()
self.norm1 = norm_layer(dim1)
self.attn = mlp_fn(dim1, dim2, dim3, segment_dim=segment_dim, qkv_bias=qkv_bias, qk_scale=None, attn_drop=attn_drop)
# NOTE: drop path for stochastic depth, we shall see if this is better than dropout here
# self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity()
self.norm2 = norm_layer(dim1)
mlp_hidden_dim = int(dim1 * mlp_ratio)
# self.mlp = Mlp(in_features=dim1, hidden_features=mlp_hidden_dim, act_layer=act_layer)
self.skip_lam = skip_lam
seg = dim1//segment_dim
# self.complex_weight = nn.Parameter(torch.randn( dim2//seg, dim3//seg//2+1, dim1, 2, dtype=torch.float32) * 0.02)
# self.mlp_c = nn.Linear(dim1, dim1, bias=qkv_bias)
self.MLP_temporal = MLP_temporal(dim1//2+1)
def forward(self, x):
B, H, W, C = x.shape
x = x + self.attn(self.norm1(x)) / self.skip_lam
bias = x
x = self.MLP_temporal(x, B, H, W) + bias
# x = x + self.mlp(self.norm2(x)) / self.skip_lam
# xf = torch.fft.rfft2(x, dim=(1, 2), norm='ortho')
# weight = torch.view_as_complex(self.complex_weight)
# x = xf * weight
# x = self.mlp(self.norm2(x)) / self.skip_lam
# x = torch.fft.irfft2(x, s=(H, W), dim=(1, 2), norm='ortho') + x
# x = self.mlp_c(x)
return x
# frequency-domain MLPs
# dimension: FFT along the dimension, r: the real part of weights, i: the imaginary part of weights
# rb: the real part of bias, ib: the imaginary part of bias
class FreMLP(nn.Module):
def __init__(self, embed_size):
super(FreMLP, self).__init__()
self.embed_size = embed_size
self.sparsity_threshold = 0.01
def forward(self, B, nd, dimension, x, r, i, rb, ib):
o1_real = torch.zeros([B, nd, dimension // 2 + 1, self.embed_size],
device=x.device)
o1_imag = torch.zeros([B, nd, dimension // 2 + 1, self.embed_size],
device=x.device)
o1_real = F.relu(
torch.einsum('bijd,dd->bijd', x.real, r) - \
torch.einsum('bijd,dd->bijd', x.imag, i) + \
rb
)
o1_imag = F.relu(
torch.einsum('bijd,dd->bijd', x.imag, r) + \
torch.einsum('bijd,dd->bijd', x.real, i) + \
ib
)
y = torch.stack([o1_real, o1_imag], dim=-1)
y = F.softshrink(y, lambd=self.sparsity_threshold)
y = torch.view_as_complex(y)
return y
# frequency temporal learner
class MLP_temporal(nn.Module):
def __init__(self, embed_size):
super(MLP_temporal, self).__init__()
self.scale = 0.02
self.embed_size = embed_size
self.r2 = nn.Parameter(self.scale * torch.randn(self.embed_size, self.embed_size))
self.i2 = nn.Parameter(self.scale * torch.randn(self.embed_size, self.embed_size))
self.rb2 = nn.Parameter(self.scale * torch.randn(self.embed_size))
self.ib2 = nn.Parameter(self.scale * torch.randn(self.embed_size))
self.FreMLP = FreMLP(self.embed_size)
def forward(self, x, B, N, L):
# [B, N, T, D]
B, H, W, C = x.shape
x = torch.fft.rfft(x, dim=3, norm='ortho') # FFT on L dimension
y = self.FreMLP(B, N, L, x, self.r2, self.i2, self.rb2, self.ib2)
x = torch.fft.irfft(y, n=C, dim=3, norm="ortho")
return x
# Complete Permutator block
class PermutatorBlock(nn.Module):
def __init__(self, dim1, dim2, dim3, segment_dim, mlp_ratio=4., qkv_bias=False, qk_scale=None, drop=0., attn_drop=0.,
drop_path=0., act_layer=False, norm_layer=nn.LayerNorm, skip_lam=1.0, mlp_fn = WeightedPermuteMLP):
super().__init__()
self.norm1 = norm_layer(dim1)
self.attn = mlp_fn(dim1, dim2, dim3, segment_dim=segment_dim, qkv_bias=qkv_bias, qk_scale=None, attn_drop=attn_drop)
# NOTE: drop path for stochastic depth, we shall see if this is better than dropout here
# self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity()
self.norm2 = norm_layer(dim1)
mlp_hidden_dim = int(dim1 * mlp_ratio)
self.mlp = Mlp(in_features=dim1, hidden_features=mlp_hidden_dim, act_layer=act_layer)
self.skip_lam = skip_lam
seg = dim1//segment_dim
self.complex_weight = nn.Parameter(torch.randn( dim2//seg, dim3//seg//2+1, dim1, 2, dtype=torch.float32) * 0.02)
self.mlp_c = nn.Linear(dim1, dim1, bias=qkv_bias)
self.MLP_temporal = MLP_temporal(dim1//2+1)
def forward(self, x):
B, H, W, C = x.shape
x = x + self.attn(self.norm1(x)) / self.skip_lam
bias = x
x = self.MLP_temporal(x, B, H, W) + bias
x = x + self.mlp(self.norm2(x)) / self.skip_lam
# xf = torch.fft.rfft2(x, dim=(1, 2), norm='ortho')
# weight = torch.view_as_complex(self.complex_weight)
# x = xf * weight
# x = self.mlp(self.norm2(x)) / self.skip_lam
# x = torch.fft.irfft2(x, s=(H, W), dim=(1, 2), norm='ortho') + x
# x = self.mlp_c(x)
return x
# Convolutional module
class conv_block1(nn.Module):
"""
Convolution Block
"""
def __init__(self, in_ch, out_ch, kernel_sizes, strides,pads, dilas):
super(conv_block1, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(in_ch, out_ch, kernel_size=kernel_sizes, stride=strides, padding=pads, dilation=dilas,bias=True),
nn.InstanceNorm2d(out_ch),
nn.LeakyReLU(negative_slope=0.3)
)
def forward(self, x):
x = self.conv(x)
return x
# Upsampling module
class up_conv(nn.Module):
"""
Up Convolution Block
"""
def __init__(self, in_ch, out_ch, kernel_sizes, strides, pads):
super(up_conv, self).__init__()
self.up = nn.Sequential(
nn.Upsample(scale_factor=2,mode='nearest'),
nn.Conv2d(in_ch, out_ch, kernel_size=kernel_sizes, stride=strides, padding=pads, bias=True),
nn.InstanceNorm2d(out_ch),
nn.LeakyReLU(negative_slope=0.3)
)
def forward(self, x):
x = self.up(x)
return x
# Residual module
class _Res_Block(nn.Module):
def __init__(self, in_ch, out_ch):
super(_Res_Block, self).__init__()
self.res_conv = nn.Conv2d(in_ch, out_ch, kernel_size=3, padding=1)
self.res_conb = nn.Conv2d(out_ch, out_ch, kernel_size=3, padding=1)
self.relu = nn.PReLU()
self.instan = nn.InstanceNorm2d(out_ch)
def forward(self, x,al=1):
y = self.relu(self.instan(self.res_conv(x)))
y = self.res_conb(y)
y *= al
y = torch.add(y, x)
return y
# frequency channel learner
# def MLP_channel(self, x, B, N, L):
# # [B, N, T, D]
# self.scale = 0.02
# self.embed_size = x.shape[3]
# self.r1 = nn.Parameter(self.scale * torch.randn(self.embed_size, self.embed_size))
# self.i1 = nn.Parameter(self.scale * torch.randn(self.embed_size, self.embed_size))
# self.rb1 = nn.Parameter(self.scale * torch.randn(self.embed_size))
# self.ib1 = nn.Parameter(self.scale * torch.randn(self.embed_size))
# x = x.permute(0, 2, 1, 3)
# # [B, T, N, D]
# x = torch.fft.rfft(x, dim=2, norm='ortho') # FFT on N dimension
# y = self.FreMLP(B, L, N, x, self.r1, self.i1, self.rb1, self.ib1)
# x = torch.fft.irfft(y, n=self.feature_size, dim=2, norm="ortho")
# x = x.permute(0, 2, 1, 3)
# # [B, N, T, D]
# return x
# def forward(self, x):
# # x: [Batch, Input length, Channel]
# B, T, N = x.shape
# # embedding x: [B, N, T, D]
# x = self.tokenEmb(x)
# bias = x
# # [B, N, T, D]
# if self.channel_independence == '1':
# x = self.MLP_channel(x, B, N, T)
# # [B, N, T, D]
# x = self.MLP_temporal(x, B, N, T)
# x = x + bias
# x = self.fc(x.reshape(B, N, -1)).permute(0, 2, 1)
# return x
# Channel prediction network
class channel_pre(nn.Module):
def __init__(self):
super(channel_pre, self).__init__()
n1 = 32
filters = [n1, n1 * 2, n1 * 4, n1 * 8, n1 * 16]
in_ch=5
out_ch=5
self.inter = nn.Upsample(size=None, scale_factor=(4,1), mode='bicubic', align_corners=None)
self.Conv11 = conv_block1(in_ch, filters[0],1,1,1)
self.Conv22 = conv_block1(filters[0], filters[1],1,1,1)
self.Conv33 = conv_block1(filters[1], filters[2],1,1,1)
self.Conv = nn.Conv2d(filters[2], out_ch, kernel_size=1, stride=1, padding=0)
seg_dim1 = 16
seg1 = filters[1]//seg_dim1
seg_dim33 = 16
seg33 = filters[2]//seg_dim33
seg_dim2 = 16
seg2 = filters[0]//seg_dim2
self.mlp_mixer11 = PermutatorBlock(dim1 = filters[0], dim2 = 64*seg2, dim3 = 72*seg2, segment_dim=seg_dim2)
self.mlp_mixer22 = PermutatorBlock(dim1 = filters[1], dim2 = 64*seg1, dim3 = 72*seg1, segment_dim=seg_dim1)
self.mlp_mixer33 = PermutatorBlock(dim1 = filters[2], dim2 = 64*seg33, dim3 = 72*seg33, segment_dim=seg_dim33)
def forward(self, x):
# x = self.inter(x)
e1 = self.Conv11(x)
e1 = rearrange(e1, 'b c h w -> b h w c')
e1 = self.mlp_mixer11(e1)
e1 = rearrange(e1, 'b h w c-> b c h w')
e2 = self.Conv22(e1)
e2 = rearrange(e2, 'b c h w -> b h w c')
e2 = self.mlp_mixer22(e2)
e2 = rearrange(e2, 'b h w c-> b c h w')
e3 = self.Conv33(e2)
e3 = rearrange(e3, 'b c h w -> b h w c')
e3 = self.mlp_mixer33(e3)
e3 = rearrange(e3, 'b h w c-> b c h w')
out = self.Conv(e3)
return out
class Scale(nn.Module):
def __init__(self, init_value=1e-3):
super().__init__()
self.scale = nn.Parameter(torch.FloatTensor([init_value]))
def forward(self, input):
return input * self.scale
# Channel estimation network
class channel_est(nn.Module):
def __init__(self, in_ch, out_ch, Hn,Hw, deep_supervision=False):
super(channel_est, self).__init__()
n1 = 48
filters = [n1, n1 * 2, n1 * 4, n1 * 8, n1 * 16]
self.inter = nn.Upsample(size=None, scale_factor=(2,2), mode='bilinear', align_corners=True)
self.deep_supervision = deep_supervision
if self.deep_supervision:
self.final1 = nn.Conv2d(filters[0], out_ch, kernel_size=1, stride=1, padding=0)
self.final2 = nn.Conv2d(filters[0], out_ch, kernel_size=1, stride=1, padding=0)
# self.final3 = nn.Conv2d(filters[0], out_ch, kernel_size=1, stride=1, padding=0)
# self.final4 = nn.Conv2d(filters[0], out_ch, kernel_size=1, stride=1, padding=0)
else:
self.final = nn.Conv2d(filters[0], out_ch, kernel_size=1, stride=1, padding=0)
n2 = n1//2
filters1 = [n2, n2 * 2, n2 * 4, n2 * 8, n2 * 16]
seg_dim1 = filters1[1]
seg1 = filters[1]//seg_dim1
seg_dim33 = filters1[2]
seg33 = filters[2]//seg_dim33
seg_dim30 = filters1[3]
seg30 = filters[3]//seg_dim30
seg_dim2 = filters1[0]
seg2 = filters[0]//seg_dim2
self.conv0_0 = nn.Sequential(
conv_block1(in_ch, filters[0],1,1,1),
Rearrange('b c h w -> b h w c'),
PermutatorBlock(dim1 = filters[0], dim2 = Hn*seg2, dim3 = Hw*seg2, segment_dim=seg_dim2),
Rearrange('b h w c-> b c h w')
)
self.conv1_0 = nn.Sequential(
conv_block1(filters[0], filters[1],2,1,1),
Rearrange('b c h w -> b h w c'),
PermutatorBlock(dim1 = filters[1], dim2 = Hn//2*seg1, dim3 = Hw//2*seg1, segment_dim=seg_dim1),
Rearrange('b h w c-> b c h w')
)
self.conv2_0 = nn.Sequential(
conv_block1(filters[1], filters[2],2,1,1),
Rearrange('b c h w -> b h w c'),
PermutatorBlock(dim1 = filters[2], dim2 = Hn//4*seg33, dim3 = Hw//4*seg33, segment_dim=seg_dim33),
Rearrange('b h w c-> b c h w')
)
# self.conv3_0 = nn.Sequential(
# conv_block1(filters[2], filters[3],2,1,1),
# Rearrange('b c h w -> b h w c'),
# PermutatorBlock(dim1 = filters[3], dim2 = Hn//8*seg30, dim3 = Hw//8*seg30, segment_dim=seg_dim30),
# Rearrange('b h w c-> b c h w')
# )
self.conv0_1 = nn.Sequential(
conv_block1(filters[1], filters[0],1,1,1),
Rearrange('b c h w -> b h w c'),
PermutatorBlock(dim1 = filters[0], dim2 = Hn*seg2, dim3 = Hw*seg2, segment_dim=seg_dim2),
Rearrange('b h w c-> b c h w')
)
self.conv1_1 = nn.Sequential(
conv_block1(filters[2], filters[1],1,1,1),
Rearrange('b c h w -> b h w c'),
PermutatorBlock(dim1 = filters[1], dim2 = Hn//2*seg1, dim3 = Hw//2*seg1, segment_dim=seg_dim1),
Rearrange('b h w c-> b c h w')
)
# self.conv2_1 = conv_block1(filters[3], filters[2],1,1,1)
self.conv0_2 = nn.Sequential(
conv_block1(filters[0]*3+in_ch, filters[0],1,1,1),
Rearrange('b c h w -> b h w c'),
PermutatorBlock(dim1 = filters[0], dim2 = Hn*seg2, dim3 = Hw*seg2, segment_dim=seg_dim2),
Rearrange('b h w c-> b c h w')
)
# self.conv1_2 = conv_block1(filters[1]*3, filters[1],1,1,1)
# self.conv0_3 = conv_block1(filters[0]*4, filters[0],1,1,1)
self.Up1_0 = nn.Sequential(
up_conv(filters[1], filters[0]),
# Rearrange('b c h w -> b h w c'),
# PermutatorBlock(dim1 = filters[0], dim2 = Hn*seg2, dim3 = Hw*seg2, segment_dim=seg_dim2),
# Rearrange('b h w c-> b c h w')
)
self.Up2_0 = nn.Sequential(
up_conv(filters[2], filters[1]),
# Rearrange('b c h w -> b h w c'),
# PermutatorBlock(dim1 = filters[1], dim2 = Hn//2*seg1, dim3 = Hw//2*seg1, segment_dim=seg_dim1),
# Rearrange('b h w c-> b c h w')
)
# self.Up3_0 = nn.Sequential(
# up_conv(filters[3], filters[2]),
# Rearrange('b c h w -> b h w c'),
# PermutatorBlock(dim1 = filters[2], dim2 = Hn//4*seg33, dim3 = Hw//4*seg33, segment_dim=seg_dim33),
# Rearrange('b h w c-> b c h w')
# )
self.Up0_2 = nn.Sequential(
up_conv(filters[1], filters[0]),
# Rearrange('b c h w -> b h w c'),
# PermutatorBlock(dim1 = filters[0], dim2 = Hn*seg2, dim3 = Hw*seg2, segment_dim=seg_dim2),
# Rearrange('b h w c-> b c h w')
)
# self.Up1_2 = nn.Sequential(
# up_conv(filters[2], filters[1]),
# )
# self.Up0_3 = nn.Sequential(
# up_conv(filters[1], filters[0]),
# )
self.Up2_2 = nn.Sequential(
up_conv(filters[2], filters[1]),
up_conv(filters[1], filters[0])
)
self.weight1 = Scale(1)
self.MLP_temporal = MLP_temporal(in_ch//2+1)
def forward(self, x):
# x = self.inter(x)
x1 = rearrange(x, 'b c h w -> b h w c')
B, H, W, C = x1.shape
x1 = self.MLP_temporal(x1,B, H, W)
x1 = rearrange(x1, 'b h w c-> b c h w')
x = x+x1
x0_0 = self.conv0_0(x)
x1_0 = self.conv1_0(x0_0)
x0_1 = self.conv0_1(torch.cat((x0_0, self.Up1_0(x1_0)), dim=1))
x2_0 = self.conv2_0(x1_0)
x1_1 = self.conv1_1(torch.cat([x1_0, self.Up2_0(x2_0)], 1))
x0_2 = self.conv0_2(torch.cat([x,x0_0, self.Up2_2(x2_0), self.Up0_2(x1_1)], 1))
# x3_0 = self.conv3_0(x2_0)
# x2_1 = self.conv2_1(torch.cat([x2_0, self.Up3_0(x3_0)], 1))
# x1_2 = self.conv1_2(torch.cat([x1_0, x1_1, self.Up1_2(x2_1)], 1))
# x0_3 = self.conv0_3(torch.cat([x0_0, x0_1, x0_2, self.Up0_3(x1_2)], 1))
if self.deep_supervision:
output1 = self.final1(x0_1)
output2 = self.final2(x0_2)
# output3 = self.final3(x0_3)
return [output1, output2]
else:
output = self.final(x0_2)
# output = self.final(x0_2)
return output
class channel_est_pruning(nn.Module):
def __init__(self, in_ch, out_ch, Hn,Hw, deep_supervision=False):
super(channel_est_pruning, self).__init__()
n1 = 48
filters = [n1, n1 * 2, n1 * 4, n1 * 8, n1 * 16]
# self.inter = nn.Upsample(size=None, scale_factor=(2,2), mode='bilinear', align_corners=True)
self.deep_supervision = deep_supervision
if self.deep_supervision:
self.final1 = nn.Conv2d(filters[0], out_ch, kernel_size=1, stride=1, padding=0)
self.final2 = nn.Conv2d(filters[0], out_ch, kernel_size=1, stride=1, padding=0)