-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
731 lines (645 loc) · 29.6 KB
/
model.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
import torch
import torch.nn as nn
import torch.utils.checkpoint as checkpoint
from timm.models.layers import DropPath, to_2tuple, trunc_normal_
import torch.nn.functional as F
from einops import rearrange, repeat
from einops.layers.torch import Rearrange
import math
import numpy as np
import time
from torch import einsum
import cv2
import scipy.misc
import utils
from natten import NeighborhoodAttention2D as NeighborhoodAttention
class SepConv2d(torch.nn.Module):
def __init__(self,
in_channels,
out_channels,
kernel_size,
stride=1,
padding=0,
dilation=1,act_layer=nn.ReLU):
super(SepConv2d, self).__init__()
self.depthwise = torch.nn.Conv2d(in_channels,
in_channels,
kernel_size=kernel_size,
stride=stride,
padding=padding,
dilation=dilation,
groups=in_channels)
self.pointwise = torch.nn.Conv2d(in_channels, out_channels, kernel_size=1)
self.act_layer = act_layer() if act_layer is not None else nn.Identity()
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.stride = stride
def forward(self, x):
x = self.depthwise(x)
x = self.act_layer(x)
x = self.pointwise(x)
return x
##########################################################################
## Channel Attention Layer
class CALayer(nn.Module):
def __init__(self, channel, reduction=16, bias=False):
super(CALayer, self).__init__()
# global average pooling: feature --> point
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.channel = channel
self.reduction = reduction
# feature channel downscale and upscale --> channel weight
self.conv_du = nn.Sequential(
nn.Conv2d(channel, channel // reduction, 1, padding=0, bias=bias),
nn.ReLU(inplace=True),
nn.Conv2d(channel // reduction, channel, 1, padding=0, bias=bias),
nn.Sigmoid()
)
def forward(self, x):
y = self.avg_pool(x)
y = self.conv_du(y)
return x * y
def conv(in_channels, out_channels, kernel_size, bias=False, stride = 1):
return nn.Conv2d(
in_channels, out_channels, kernel_size,
padding=(kernel_size//2), bias=bias, stride = stride, groups=out_channels)
##########################################################################
## Channel Attention Block (CAB)
class CAB(nn.Module):
def __init__(self, n_feat, kernel_size, reduction, bias, act):
super(CAB, self).__init__()
modules_body = []
self.n_feat = n_feat
self.kernel_size = kernel_size
modules_body.append(conv(n_feat, n_feat, kernel_size, bias=bias))
modules_body.append(act)
modules_body.append(conv(n_feat, n_feat, kernel_size, bias=bias))
self.CA = CALayer(n_feat, reduction, bias=bias)
self.body = nn.Sequential(*modules_body)
def forward(self, x):
res = self.body(x)
res = self.CA(res)
res += x
return res
#########################################
######## Embedding for q,k,v ########
class ConvProjection(nn.Module):
def __init__(self, dim, heads = 8, dim_head = 64, kernel_size=3, q_stride=1, k_stride=1, v_stride=1, dropout = 0.,
last_stage=False,bias=True):
super().__init__()
inner_dim = dim_head * heads
self.heads = heads
pad = (kernel_size - q_stride)//2
self.to_q = SepConv2d(dim, inner_dim, kernel_size, q_stride, pad, bias)
self.to_k = SepConv2d(dim, inner_dim, kernel_size, k_stride, pad, bias)
self.to_v = SepConv2d(dim, inner_dim, kernel_size, v_stride, pad, bias)
def forward(self, x, attn_kv=None):
b, n, c, h = *x.shape, self.heads
l = int(math.sqrt(n))
w = int(math.sqrt(n))
attn_kv = x if attn_kv is None else attn_kv
x = rearrange(x, 'b (l w) c -> b c l w', l=l, w=w)
attn_kv = rearrange(attn_kv, 'b (l w) c -> b c l w', l=l, w=w)
# print(attn_kv)
q = self.to_q(x)
q = rearrange(q, 'b (h d) l w -> b h (l w) d', h=h)
k = self.to_k(attn_kv)
v = self.to_v(attn_kv)
k = rearrange(k, 'b (h d) l w -> b h (l w) d', h=h)
v = rearrange(v, 'b (h d) l w -> b h (l w) d', h=h)
return q,k,v
class LinearProjection(nn.Module):
def __init__(self, dim, heads = 8, dim_head = 64, dropout = 0., bias=True):
super().__init__()
inner_dim = dim_head * heads
self.heads = heads
self.to_q = nn.Linear(dim, inner_dim, bias = bias)
self.to_kv = nn.Linear(dim, inner_dim * 2, bias = bias)
self.dim = dim
self.inner_dim = inner_dim
def forward(self, x, attn_kv=None):
B_, N, C = x.shape
attn_kv = x if attn_kv is None else attn_kv
q = self.to_q(x).reshape(B_, N, 1, self.heads, C // self.heads).permute(2, 0, 3, 1, 4)
kv = self.to_kv(attn_kv).reshape(B_, N, 2, self.heads, C // self.heads).permute(2, 0, 3, 1, 4)
q = q[0]
k, v = kv[0], kv[1]
return q,k,v
class LinearProjection_Concat_kv(nn.Module):
def __init__(self, dim, heads = 8, dim_head = 64, dropout = 0., bias=True):
super().__init__()
inner_dim = dim_head * heads
self.heads = heads
self.to_qkv = nn.Linear(dim, inner_dim * 3, bias = bias)
self.to_kv = nn.Linear(dim, inner_dim * 2, bias = bias)
self.dim = dim
self.inner_dim = inner_dim
def forward(self, x, attn_kv=None):
B_, N, C = x.shape
attn_kv = x if attn_kv is None else attn_kv
qkv_dec = self.to_qkv(x).reshape(B_, N, 3, self.heads, C // self.heads).permute(2, 0, 3, 1, 4)
kv_enc = self.to_kv(attn_kv).reshape(B_, N, 2, self.heads, C // self.heads).permute(2, 0, 3, 1, 4)
q, k_d, v_d = qkv_dec[0], qkv_dec[1], qkv_dec[2] # make torchscript happy (cannot use tensor as tuple)
k_e, v_e = kv_enc[0], kv_enc[1]
k = torch.cat((k_d,k_e),dim=2)
v = torch.cat((v_d,v_e),dim=2)
return q,k,v
#########################################
########### feed-forward network #############
class Mlp(nn.Module):
def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, 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 = act_layer()
self.fc2 = nn.Linear(hidden_features, out_features)
self.drop = nn.Dropout(drop)
self.in_features = in_features
self.hidden_features = hidden_features
self.out_features = out_features
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
class LeFF(nn.Module):
def __init__(self, dim=32, hidden_dim=128, act_layer=nn.GELU,drop = 0.):
super().__init__()
self.linear1 = nn.Sequential(nn.Linear(dim, hidden_dim),
act_layer())
self.dwconv = nn.Sequential(nn.Conv2d(hidden_dim,hidden_dim,groups=hidden_dim,kernel_size=3,stride=1,padding=1),
act_layer())
self.linear2 = nn.Sequential(nn.Linear(hidden_dim, dim))
self.dim = dim
self.hidden_dim = hidden_dim
def forward(self, x, img_size=(128,128)):
# bs x hw x c
bs, hw, c = x.size()
# hh = int(math.sqrt(hw))
hh = img_size[0]
ww = img_size[1]
x = self.linear1(x)
# spatial restore
x = rearrange(x, ' b (h w) (c) -> b c h w ', h = hh, w = ww)
# bs,hidden_dim,32x32
x = self.dwconv(x)
# flaten
x = rearrange(x, ' b c h w -> b (h w) c', h = hh, w = ww)
x = self.linear2(x)
return x
#########################################
# Downsample Block
class Downsample(nn.Module):
def __init__(self, in_channel, out_channel):
super(Downsample, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(in_channel, out_channel, kernel_size=4, stride=2, padding=1),
)
self.in_channel = in_channel
self.out_channel = out_channel
def forward(self, x, img_size=(128,128)):
B, L, C = x.shape
H = img_size[0]
W = img_size[1]
x = x.transpose(1, 2).contiguous().view(B, C, H, W)
out = self.conv(x).flatten(2).transpose(1,2).contiguous() # B H*W C
return out
# Upsample Block
class Upsample(nn.Module):
def __init__(self, in_channel, out_channel):
super(Upsample, self).__init__()
self.deconv = nn.Sequential(
nn.ConvTranspose2d(in_channel, out_channel, kernel_size=2, stride=2),
)
self.in_channel = in_channel
self.out_channel = out_channel
def forward(self, x, img_size=(128,128)):
B, L, C = x.shape
H = img_size[0]
W = img_size[1]
x = x.transpose(1, 2).contiguous().view(B, C, H, W)
out = self.deconv(x).flatten(2).transpose(1,2).contiguous() # B H*W C
return out
# Input Projection
class InputProj(nn.Module):
def __init__(self, in_channel=3, out_channel=64, kernel_size=3, stride=1, norm_layer=None,act_layer=nn.LeakyReLU):
super().__init__()
self.proj = nn.Sequential(
nn.Conv2d(in_channel, out_channel, kernel_size=3, stride=stride, padding=kernel_size//2),
act_layer(inplace=True)
)
if norm_layer is not None:
self.norm = norm_layer(out_channel)
else:
self.norm = None
self.in_channel = in_channel
self.out_channel = out_channel
def forward(self, x):
B, C, H, W = x.shape
x = self.proj(x).flatten(2).transpose(1, 2).contiguous() # B H*W C
if self.norm is not None:
x = self.norm(x)
return x
# Output Projection
class OutputProj(nn.Module):
def __init__(self, in_channel=64, out_channel=3, kernel_size=3, stride=1, norm_layer=None,act_layer=None):
super().__init__()
self.proj = nn.Sequential(
nn.Conv2d(in_channel, out_channel, kernel_size=3, stride=stride, padding=kernel_size//2),
)
if act_layer is not None:
self.proj.add_module(act_layer(inplace=True))
if norm_layer is not None:
self.norm = norm_layer(out_channel)
else:
self.norm = None
self.in_channel = in_channel
self.out_channel = out_channel
def forward(self, x, img_size=(128,128)):
B, L, C = x.shape
H = img_size[0]
W = img_size[1]
x = x.transpose(1, 2).view(B, C, H, W)
x = self.proj(x)
if self.norm is not None:
x = self.norm(x)
return x
#########################################
########### CA Transformer #############
class CATransformerBlock(nn.Module):
def __init__(self, dim, input_resolution, num_heads, win_size=10, shift_size=0,
mlp_ratio=4., qkv_bias=True, qk_scale=None, drop=0., attn_drop=0., drop_path=0.,
act_layer=nn.GELU, norm_layer=nn.LayerNorm, token_projection='linear', token_mlp='leff',
se_layer=False):
super().__init__()
self.dim = dim
self.input_resolution = input_resolution
self.num_heads = num_heads
self.win_size = win_size
self.shift_size = shift_size
self.mlp_ratio = mlp_ratio
self.token_mlp = token_mlp
if min(self.input_resolution) <= self.win_size:
self.shift_size = 0
self.win_size = min(self.input_resolution)
assert 0 <= self.shift_size < self.win_size, "shift_size must in 0-win_size"
self.norm1 = norm_layer(dim)
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,
drop=drop) if token_mlp == 'ffn' else LeFF(dim, mlp_hidden_dim, act_layer=act_layer, drop=drop)
self.CAB = CAB(dim, kernel_size=3, reduction=4, bias=False, act=nn.PReLU())
def extra_repr(self) -> str:
return f"dim={self.dim}, input_resolution={self.input_resolution}, num_heads={self.num_heads}, " \
f"win_size={self.win_size}, shift_size={self.shift_size}, mlp_ratio={self.mlp_ratio}"
def forward(self, x, xm, mask=None, img_size=(128, 128)):
B, L, C = x.shape
H = img_size[0]
W = img_size[1]
assert L == W * H, \
f"Input image size ({H}*{W} doesn't match model ({L})."
shortcut = x
x = self.norm1(x)
# spatial restore
x = rearrange(x, ' b (h w) (c) -> b c h w ', h=H, w=W)
# bs,hidden_dim,32x32
x = self.CAB(x)
# flaten
x = rearrange(x, ' b c h w -> b (h w) c', h=H, w=W)
x = x.view(B, H * W, C)
# FFN
x = shortcut + self.drop_path(x)
x = x + self.drop_path(self.mlp(self.norm2(x), img_size=img_size))
return x
#########################################
########### RA Module #############
class RALayer(nn.Module):
def __init__(
self,
dim,
input_resolution,
num_heads,
win_size=None,
shift_size=None,
kernel_size=11,
dilation=2,
mlp_ratio=4.0,
qkv_bias=True,
qk_scale=None,
drop=0.0,
attn_drop=0.0,
drop_path=0.0,
act_layer=nn.GELU,
norm_layer=nn.LayerNorm,
token_projection='linear',
token_mlp='leff',
se_layer='False',
layer_scale=None,
):
super().__init__()
self.dim = dim
self.input_resolution = input_resolution
self.num_heads = num_heads
self.mlp_ratio = mlp_ratio
self.token_mlp = token_mlp
self.kernel_size = kernel_size
self.norm1 = norm_layer(dim)
self.attn = NeighborhoodAttention(
dim,
kernel_size=kernel_size,
dilation=dilation,
num_heads=num_heads,
qkv_bias=qkv_bias,
qk_scale=qk_scale,
attn_drop=attn_drop,
proj_drop=drop,
)
self.drop_path = DropPath(drop_path) if drop_path > 0.0 else nn.Identity()
self.norm2 = norm_layer(dim)
self.mlp = Mlp(
in_features=dim,
hidden_features=int(dim * mlp_ratio),
act_layer=act_layer,
drop=drop,
) if token_mlp == 'ffn' else LeFF(
dim,
int(dim * mlp_ratio),
act_layer=act_layer,
drop=drop
)
self.CAB = CAB(dim, kernel_size=3, reduction=4, bias=False, act=nn.PReLU())
def extra_repr(self) -> str:
return f"dim={self.dim}, input_resolution={self.input_resolution}, num_heads={self.num_heads}, " \
f"kernel_size={self.kernel_size}, shift_size={0}, mlp_ratio={self.mlp_ratio}"
def forward(self, x, xm, mask=None, img_size = (128, 128)):
B, L, C = x.shape
H = img_size[0]
W = img_size[1]
assert L == W * H, \
f"Input image size ({H}*{W} doesn't match model ({L})."
shortcut = x
x = self.norm1(x)
x = x.view(B, H, W, C)
xm = xm.permute(0, 2, 3, 1)
with torch.autocast(device_type="cuda", enabled=False):
x = self.attn(x.float())
x = x.view(B, H * W, C)
x = rearrange(x, ' b (h w) (c) -> b c h w ', h=H, w=W)
x = self.CAB(x)
# flaten
x = rearrange(x, ' b c h w -> b (h w) c', h=H, w=W)
x = shortcut + self.drop_path(x)
x = x + self.drop_path(self.mlp(self.norm2(x), img_size=img_size))
return x
#########################################
########### Basic layer of RASM ################
class BasicRASM(nn.Module):
def __init__(self, dim, output_dim, input_resolution, depth, num_heads, win_size,
mlp_ratio=4., qkv_bias=True, qk_scale=None, drop=0., attn_drop=0.,
drop_path=0., norm_layer=nn.LayerNorm, use_checkpoint=False,
token_projection='linear',token_mlp='ffn',se_layer=False,cab=False):
super().__init__()
self.dim = dim
self.input_resolution = input_resolution
self.depth = depth
self.use_checkpoint = use_checkpoint
self.cab = cab
# build blocks
if cab:
self.blocks = nn.ModuleList([
CATransformerBlock(dim=dim, input_resolution=input_resolution,
num_heads=num_heads, win_size=win_size,
shift_size=0 if (i % 2 == 0) else win_size // 2,
mlp_ratio=mlp_ratio,
qkv_bias=qkv_bias, qk_scale=qk_scale,
drop=drop, attn_drop=attn_drop,
drop_path=drop_path[i] if isinstance(drop_path, list) else drop_path,
norm_layer=norm_layer, token_projection=token_projection, token_mlp=token_mlp,
se_layer=se_layer)
for i in range(depth)])
else:
self.blocks = nn.ModuleList([
RALayer(dim=dim, input_resolution=input_resolution,
num_heads=num_heads,mlp_ratio=mlp_ratio,
qkv_bias=qkv_bias, qk_scale=qk_scale,
drop=drop, attn_drop=attn_drop,
drop_path=drop_path[i] if isinstance(drop_path, list) else drop_path,
norm_layer=norm_layer,token_projection=token_projection,token_mlp=token_mlp,se_layer=se_layer)
for i in range(depth)])
def extra_repr(self) -> str:
return f"dim={self.dim}, input_resolution={self.input_resolution}, depth={self.depth}"
def forward(self, x, xm, mask=None, img_size=(128,128)):
for blk in self.blocks:
if self.use_checkpoint:
x = checkpoint.checkpoint(blk, x, xm, mask, img_size)
else:
x = blk(x, xm, mask, img_size)
return x
class RASM(nn.Module):
def __init__(self, img_size=256, in_chans=3,
embed_dim=32, depths=[2, 2, 2, 2, 2, 2, 2, 2, 2], num_heads=[1, 2, 4, 8, 16, 16, 8, 4, 2],
win_size=8, mlp_ratio=4., qkv_bias=True, qk_scale=None,
drop_rate=0., attn_drop_rate=0., drop_path_rate=0.1,
norm_layer=nn.LayerNorm, patch_norm=True,
use_checkpoint=False, token_projection='linear', token_mlp='leff', se_layer=False,
dowsample=Downsample, upsample=Upsample, **kwargs):
super().__init__()
self.num_enc_layers = len(depths)//2
self.num_dec_layers = len(depths)//2
self.embed_dim = embed_dim
self.patch_norm = patch_norm
self.mlp_ratio = mlp_ratio
self.token_projection = token_projection
self.mlp = token_mlp
self.win_size =win_size
self.reso = img_size
self.pos_drop = nn.Dropout(p=drop_rate)
# stochastic depth
enc_dpr = [x.item() for x in torch.linspace(0, drop_path_rate, sum(depths[:self.num_enc_layers]))]
conv_dpr = [drop_path_rate]*depths[4]
dec_dpr = enc_dpr[::-1]
# build layers
# Input/Output
self.input_proj = InputProj(in_channel=4, out_channel=embed_dim, kernel_size=3, stride=1, act_layer=nn.LeakyReLU)
self.output_proj = OutputProj(in_channel=2*embed_dim, out_channel=in_chans, kernel_size=3, stride=1)
# self.CAB = CAB(embed_dim, kernel_size=3, reduction=4, bias=False, act=nn.PReLU())
# Encoder
self.encoderlayer_0 = BasicRASM(dim=embed_dim,
output_dim=embed_dim,
input_resolution=(img_size,
img_size),
depth=depths[0],
num_heads=num_heads[0],
win_size=win_size,
mlp_ratio=self.mlp_ratio,
qkv_bias=qkv_bias, qk_scale=qk_scale,
drop=drop_rate, attn_drop=attn_drop_rate,
drop_path=enc_dpr[sum(depths[:0]):sum(depths[:1])],
norm_layer=norm_layer,
use_checkpoint=use_checkpoint,
token_projection=token_projection,token_mlp=token_mlp,se_layer=se_layer,cab=True)
self.dowsample_0 = dowsample(embed_dim, embed_dim*2)
self.encoderlayer_1 = BasicRASM(dim=embed_dim*2,
output_dim=embed_dim*2,
input_resolution=(img_size // 2,
img_size // 2),
depth=depths[1],
num_heads=num_heads[1],
win_size=win_size,
mlp_ratio=self.mlp_ratio,
qkv_bias=qkv_bias, qk_scale=qk_scale,
drop=drop_rate, attn_drop=attn_drop_rate,
drop_path=enc_dpr[sum(depths[:1]):sum(depths[:2])],
norm_layer=norm_layer,
use_checkpoint=use_checkpoint,
token_projection=token_projection,token_mlp=token_mlp,se_layer=se_layer, cab=True)
self.dowsample_1 = dowsample(embed_dim*2, embed_dim*4)
self.encoderlayer_2 = BasicRASM(dim=embed_dim*4,
output_dim=embed_dim*4,
input_resolution=(img_size // (2 ** 2),
img_size // (2 ** 2)),
depth=depths[2],
num_heads=num_heads[2],
win_size=win_size,
mlp_ratio=self.mlp_ratio,
qkv_bias=qkv_bias, qk_scale=qk_scale,
drop=drop_rate, attn_drop=attn_drop_rate,
drop_path=enc_dpr[sum(depths[:2]):sum(depths[:3])],
norm_layer=norm_layer,
use_checkpoint=use_checkpoint,
token_projection=token_projection,token_mlp=token_mlp,se_layer=se_layer)
self.dowsample_2 = dowsample(embed_dim*4, embed_dim*8)
# Bottleneck
self.conv = BasicRASM(dim=embed_dim*8,
output_dim=embed_dim*8,
input_resolution=(img_size // (2 ** 3),
img_size // (2 ** 3)),
depth=depths[4],
num_heads=num_heads[4],
win_size=win_size,
mlp_ratio=self.mlp_ratio,
qkv_bias=qkv_bias, qk_scale=qk_scale,
drop=drop_rate, attn_drop=attn_drop_rate,
drop_path=conv_dpr,
norm_layer=norm_layer,
use_checkpoint=use_checkpoint,
token_projection=token_projection,token_mlp=token_mlp,se_layer=se_layer)
# # Decoder
self.upsample_0 = upsample(embed_dim*8, embed_dim*4)
self.decoderlayer_0 = BasicRASM(dim=embed_dim*8,
output_dim=embed_dim*8,
input_resolution=(img_size // (2 ** 2),
img_size // (2 ** 2)),
depth=depths[6],
num_heads=num_heads[6],
win_size=win_size,
mlp_ratio=self.mlp_ratio,
qkv_bias=qkv_bias, qk_scale=qk_scale,
drop=drop_rate, attn_drop=attn_drop_rate,
drop_path=dec_dpr[sum(depths[5:6]):sum(depths[5:7])],
norm_layer=norm_layer,
use_checkpoint=use_checkpoint,
token_projection=token_projection,token_mlp=token_mlp,se_layer=se_layer)
self.upsample_1 = upsample(embed_dim*8, embed_dim*2)
self.decoderlayer_1 = BasicRASM(dim=embed_dim*4,
output_dim=embed_dim*4,
input_resolution=(img_size // 2,
img_size // 2),
depth=depths[7],
num_heads=num_heads[7],
win_size=win_size,
mlp_ratio=self.mlp_ratio,
qkv_bias=qkv_bias, qk_scale=qk_scale,
drop=drop_rate, attn_drop=attn_drop_rate,
drop_path=dec_dpr[sum(depths[5:7]):sum(depths[5:8])],
norm_layer=norm_layer,
use_checkpoint=use_checkpoint,
token_projection=token_projection,token_mlp=token_mlp,se_layer=se_layer, cab=True)
self.upsample_2 = upsample(embed_dim*4, embed_dim)
self.decoderlayer_2 = BasicRASM(dim=embed_dim*2,
output_dim=embed_dim*2,
input_resolution=(img_size,
img_size),
depth=depths[8],
num_heads=num_heads[8],
win_size=win_size,
mlp_ratio=self.mlp_ratio,
qkv_bias=qkv_bias, qk_scale=qk_scale,
drop=drop_rate, attn_drop=attn_drop_rate,
drop_path=dec_dpr[sum(depths[5:8]):sum(depths[5:9])],
norm_layer=norm_layer,
use_checkpoint=use_checkpoint,
token_projection=token_projection,token_mlp=token_mlp,se_layer=se_layer,cab=True)
self.apply(self._init_weights)
def _init_weights(self, m):
if isinstance(m, nn.Linear):
trunc_normal_(m.weight, std=.02)
if isinstance(m, nn.Linear) and m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.LayerNorm):
nn.init.constant_(m.bias, 0)
nn.init.constant_(m.weight, 1.0)
@torch.jit.ignore
def no_weight_decay(self):
return {'absolute_pos_embed'}
@torch.jit.ignore
def no_weight_decay_keywords(self):
return {'relative_position_bias_table'}
def extra_repr(self) -> str:
return f"embed_dim={self.embed_dim}, token_projection={self.token_projection}, token_mlp={self.mlp},win_size={self.win_size}"
def forward(self, x, xm, mask=None):
# Input Projection
xi = torch.cat((x, xm), dim=1)
# xi = x
self.img_size = (x.shape[2], x.shape[3])
y = self.input_proj(xi)
y = self.pos_drop(y)
#Encoder
conv0 = self.encoderlayer_0(y, xm, mask=mask, img_size = self.img_size)
pool0 = self.dowsample_0(conv0, img_size = self.img_size)
m = nn.MaxPool2d(2)
xm1 = m(xm)
self.img_size = (int(self.img_size[0]/2), int(self.img_size[1]/2))
conv1 = self.encoderlayer_1(pool0, xm1, mask=mask, img_size = self.img_size)
pool1 = self.dowsample_1(conv1, img_size = self.img_size)
m = nn.MaxPool2d(2)
xm2 = m(xm1)
self.img_size = (int(self.img_size[0] / 2), int(self.img_size[1] / 2))
conv2 = self.encoderlayer_2(pool1, xm2, mask=mask, img_size = self.img_size)
pool2 = self.dowsample_2(conv2, img_size = self.img_size)
self.img_size = (int(self.img_size[0] / 2), int(self.img_size[1] / 2))
m = nn.MaxPool2d(2)
xm3 = m(xm2)
# Bottleneck
conv3 = self.conv(pool2, xm3, mask=mask, img_size = self.img_size)
#Decoder
up0 = self.upsample_0(conv3, img_size = self.img_size)
self.img_size = (int(self.img_size[0] * 2), int(self.img_size[1] * 2))
deconv0 = torch.cat([up0,conv2],-1)
deconv0 = self.decoderlayer_0(deconv0, xm2, mask=mask, img_size = self.img_size)
up1 = self.upsample_1(deconv0, img_size = self.img_size)
self.img_size = (int(self.img_size[0] * 2), int(self.img_size[1] * 2))
deconv1 = torch.cat([up1,conv1],-1)
deconv1 = self.decoderlayer_1(deconv1, xm1, mask=mask, img_size = self.img_size)
up2 = self.upsample_2(deconv1, img_size = self.img_size)
self.img_size = (int(self.img_size[0] * 2), int(self.img_size[1] * 2))
deconv2 = torch.cat([up2,conv0],-1)
deconv2 = self.decoderlayer_2(deconv2, xm, mask=mask, img_size = self.img_size)
# Output Projection
y = self.output_proj(deconv2, img_size = self.img_size) + x
return y
if __name__ == "__main__":
import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
img_size = 256
model_restoration = RASM(img_size=256,embed_dim=32,win_size=10,token_projection='linear',token_mlp='leff').cuda().eval()
x = torch.randn(1, 3, 256, 256).cuda()
xm = torch.randn(1, 1, 256, 256).cuda()
xm = (xm > 0.5).float()
y = model_restoration(x, xm)