-
Notifications
You must be signed in to change notification settings - Fork 3
/
fuse_block.py
260 lines (192 loc) · 9.23 KB
/
fuse_block.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
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.autograd import Variable
import numpy as np
import numbers
from einops import rearrange
## Layer Norm
def to_3d(x):
return rearrange(x, 'b c h w -> b (h w) c')
def to_4d(x,h,w):
return rearrange(x, 'b (h w) c -> b c h w',h=h,w=w)
class BiasFree_LayerNorm(nn.Module):
def __init__(self, normalized_shape):
super(BiasFree_LayerNorm, self).__init__()
if isinstance(normalized_shape, numbers.Integral):
normalized_shape = (normalized_shape,)
normalized_shape = torch.Size(normalized_shape)
assert len(normalized_shape) == 1
self.weight = nn.Parameter(torch.ones(normalized_shape))
self.normalized_shape = normalized_shape
def forward(self, x):
sigma = x.var(-1, keepdim=True, unbiased=False)
return x / torch.sqrt(sigma+1e-5) * self.weight
class WithBias_LayerNorm(nn.Module):
def __init__(self, normalized_shape):
super(WithBias_LayerNorm, self).__init__()
if isinstance(normalized_shape, numbers.Integral):
normalized_shape = (normalized_shape,)
normalized_shape = torch.Size(normalized_shape)
assert len(normalized_shape) == 1
self.weight = nn.Parameter(torch.ones(normalized_shape))
self.bias = nn.Parameter(torch.zeros(normalized_shape))
self.normalized_shape = normalized_shape
def forward(self, x):
mu = x.mean(-1, keepdim=True)
sigma = x.var(-1, keepdim=True, unbiased=False)
return (x - mu) / torch.sqrt(sigma+1e-5) * self.weight + self.bias
class LayerNorm(nn.Module):
def __init__(self, dim, LayerNorm_type):
super(LayerNorm, self).__init__()
if LayerNorm_type =='BiasFree':
self.body = BiasFree_LayerNorm(dim)
else:
self.body = WithBias_LayerNorm(dim)
def forward(self, x):
h, w = x.shape[-2:]
return to_4d(self.body(to_3d(x)), h, w)
##########################################################################
## Gated-Dconv Feed-Forward Network (GDFN)
class FeedForward(nn.Module):
def __init__(self, dim, ffn_expansion_factor, bias):
super(FeedForward, self).__init__()
hidden_features = int(dim * ffn_expansion_factor)
self.project_in = nn.Conv2d(dim, hidden_features * 2, kernel_size=1, bias=bias)
self.dwconv = nn.Conv2d(hidden_features * 2, hidden_features * 2, kernel_size=3, stride=1, padding=1,
groups=hidden_features * 2, bias=bias)
self.project_out = nn.Conv2d(hidden_features, dim, kernel_size=1, bias=bias)
def forward(self, x):
x = self.project_in(x)
x1, x2 = self.dwconv(x).chunk(2, dim=1)
x = F.gelu(x1) * x2
x = self.project_out(x)
return x
##########################################################################
## Multi-DConv Head Transposed Self-Attention (MDTA)
class Attention(nn.Module):
def __init__(self, dim, num_heads, bias):
super(Attention, self).__init__()
self.num_heads = num_heads
self.temperature = nn.Parameter(torch.ones(num_heads, 1, 1))
self.kv = nn.Conv2d(dim, dim * 2, kernel_size=1, bias=bias)
self.kv_dwconv = nn.Conv2d(dim * 2, dim * 2, kernel_size=3, stride=1, padding=1, groups=dim * 2, bias=bias)
self.q = nn.Conv2d(dim, dim, kernel_size=1, bias=bias)
self.q_dwconv = nn.Conv2d(dim, dim, kernel_size=3, stride=1, padding=1, bias=bias)
self.project_out = nn.Conv2d(dim, dim, kernel_size=1, bias=bias)
def forward(self, x, y):
b, c, h, w = x.shape
kv = self.kv_dwconv(self.kv(x))
k, v = kv.chunk(2, dim=1)
q = self.q_dwconv(self.q(y))
q = rearrange(q, 'b (head c) h w -> b head c (h w)', head=self.num_heads)
k = rearrange(k, 'b (head c) h w -> b head c (h w)', head=self.num_heads)
v = rearrange(v, 'b (head c) h w -> b head c (h w)', head=self.num_heads)
q = torch.nn.functional.normalize(q, dim=-1)
k = torch.nn.functional.normalize(k, dim=-1)
attn = (q @ k.transpose(-2, -1)) * self.temperature
attn = attn.softmax(dim=-1)
out = (attn @ v)
out = rearrange(out, 'b head c (h w) -> b (head c) h w', head=self.num_heads, h=h, w=w)
out = self.project_out(out)
return out
##########################################################################
class TransformerBlock(nn.Module):
def __init__(self, dim_2, dim, num_heads=2, ffn_expansion_factor=2.66, bias=False, LayerNorm_type='WithBias'):
super(TransformerBlock, self).__init__()
self.conv1 = nn.Conv2d(dim_2, dim, (1, 1))
# self.conv2 = nn.Conv2d(dim, dim_2, (1, 1))
self.norm1 = LayerNorm(dim, LayerNorm_type)
self.attn = Attention(dim, num_heads, bias)
self.norm2 = LayerNorm(dim, LayerNorm_type)
self.ffn = FeedForward(dim, ffn_expansion_factor, bias)
def forward(self, input_R, input_S):
# input_ch = input_R.size()[1]
input_S = F.interpolate(input_S, [input_R.shape[2], input_R.shape[3]])
input_S = self.conv1(input_S)
# input_S = F.interpolate(input_S, size=input_size, mode='bilinear', align_corners=True)
input_R = self.norm1(input_R)
input_S = self.norm1(input_S)
input_R = input_R + self.attn(input_R, input_S)
input_R = input_R + self.ffn(self.norm2(input_R))
return input_R
##########################################################################
class TransformerBlock_1(nn.Module):
def __init__(self, dim_2, dim, dim_in, num_heads=2, ffn_expansion_factor=1, bias=False, LayerNorm_type='WithBias'):
super(TransformerBlock_1, self).__init__()
self.conv1 = nn.Conv2d(dim_2, dim_in, (1, 1))
self.conv2 = nn.Conv2d(dim, dim_in, (1, 1))
self.conv3 = nn.Conv2d(dim_in, dim, (1, 1))
self.norm1 = LayerNorm(dim_in, LayerNorm_type)
self.attn = Attention(dim_in, num_heads, bias)
self.norm2 = LayerNorm(dim_in, LayerNorm_type)
self.ffn = FeedForward(dim_in, ffn_expansion_factor, bias)
def forward(self, input_R, input_S):
# input_ch = input_R.size()[1]
input_S = F.interpolate(input_S, [input_R.shape[2], input_R.shape[3]])
input_S = self.conv1(input_S)
input_R = self.conv2(input_R)
# input_S = F.interpolate(input_S, size=input_size, mode='bilinear', align_corners=True)
input_R = self.norm1(input_R)
input_S = self.norm1(input_S)
input_R = input_R + self.attn(input_R, input_S)
input_R = input_R + self.ffn(self.norm2(input_R))
input_R = self.conv3(input_R)
return input_R
def W(in_channels, out_channels):
return nn.Sequential(
nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=1, bias=False),
nn.BatchNorm2d(out_channels)
)
class Conv3x3(nn.Module):
"""Layer to pad and convolve input
"""
def __init__(self, in_channels, out_channels, use_refl=True):
super(Conv3x3, self).__init__()
if use_refl:
self.pad = nn.ReflectionPad2d(1)
else:
self.pad = nn.ZeroPad2d(1)
self.conv = nn.Conv2d(int(in_channels), int(out_channels), 3)
def forward(self, x):
out = self.pad(x)
out = self.conv(out)
return out
class ConvBlock(nn.Module):
"""Layer to perform a convolution followed by ELU
"""
def __init__(self, in_channels, out_channels):
super(ConvBlock, self).__init__()
self.conv = Conv3x3(in_channels, out_channels)
self.nonlin = nn.ELU(inplace=True)
def forward(self, x):
out = self.conv(x)
out = self.nonlin(out)
return out
class MultiEmbedding(nn.Module):
def __init__(self, in_channels, num_head=2, ratio=1):
super(MultiEmbedding, self).__init__()
self.in_channels = in_channels
self.num_head = num_head
self.out_channel = int(num_head * in_channels * ratio)
self.query_conv = nn.Conv2d(in_channels, self.out_channel, kernel_size=1, bias=True)
self.key_conv = nn.Conv2d(in_channels, self.out_channel, kernel_size=1, bias=True)
self.value_conv = nn.Conv2d(in_channels, self.out_channel, kernel_size=1, bias=True)
self.W = W(int(in_channels * ratio), in_channels)
self.fuse = nn.Sequential(ConvBlock(in_channels * 2, in_channels),
nn.Conv2d(in_channels, in_channels, kernel_size=1))
def forward(self, key, query):
batch, channels, height, width = query.size()
q_out = self.query_conv(query).contiguous().view(batch, self.num_head, -1, height, width)
k_out = self.key_conv(key).contiguous().view(batch, self.num_head, -1, height, width)
v_out = self.value_conv(key).contiguous().view(batch, self.num_head, -1, height, width)
att = (q_out * k_out).sum(dim=2) / np.sqrt(self.out_channel)
if self.num_head == 1:
softmax = att.unsqueeze(dim=2)
else:
softmax = F.softmax(att, dim=1).unsqueeze(dim=2)
weighted_value = v_out * softmax
weighted_value = weighted_value.sum(dim=1)
out = self.W(weighted_value)
return self.fuse(torch.cat([key, out], dim=1))