-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmt_parts.py
248 lines (205 loc) · 7.89 KB
/
cmt_parts.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
import torch
import torch.nn as nn
class Conv2x2(nn.Module):
"""
2x2 Convolution
"""
def __init__(self, in_channels, out_channels, stride = 1, padding = 0):
super(Conv2x2, self).__init__()
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size = 2,
stride = stride, padding = padding, bias = True)
def forward(self, x):
result = self.conv(x)
return result
class DWCONV(nn.Module):
"""
Depthwise Convolution
"""
def __init__(self, in_channels, out_channels, stride = 1):
super(DWCONV, self).__init__()
self.depthwise = nn.Conv2d(in_channels, out_channels, kernel_size = 3,
stride = stride, padding = 1, groups = in_channels, bias = True)
def forward(self, x):
result = self.depthwise(x)
return result
class LPU(nn.Module):
"""
Local Perception Unit to extract local infomation.
LPU(X) = DWConv(X) + X
"""
def __init__(self, in_channels, out_channels):
super(LPU, self).__init__()
self.DWConv = DWCONV(in_channels, out_channels)
def forward(self, x):
result = self.DWConv(x) + x
return result
class LMHSA(nn.Module):
"""
Lightweight Multi-head-self-attention module.
Inputs:
Q: [N, C, H, W]
K: [N, C, H / stride, W / stride]
V: [N, C, H / stride, W / stride]
Outputs:
X: [N, C, H, W]
"""
def __init__(self, input_size, channels, d_k, d_v, stride, heads, dropout):
super(LMHSA, self).__init__()
self.dwconv_k = DWCONV(channels, channels, stride = stride)
self.dwconv_v = DWCONV(channels, channels, stride = stride)
self.fc_q = nn.Linear(channels, heads * d_k)
self.fc_k = nn.Linear(channels, heads * d_k)
self.fc_v = nn.Linear(channels, heads * d_v)
self.fc_o = nn.Linear(heads * d_k, channels)
self.channels = channels
self.d_k = d_k
self.d_v = d_v
self.stride = stride
self.heads = heads
self.dropout = dropout
self.scaled_factor = self.d_k ** -0.5
self.num_patches = (self.d_k // self.stride) ** 2
# self.B = nn.Parameter(torch.Tensor(1, self.heads, input_size ** 2, (input_size // stride) ** 2), requires_grad = True)
def forward(self, x):
b, c, h, w = x.shape
# Reshape
x_reshape = x.view(b, c, h * w).permute(0, 2, 1)
x_reshape = torch.nn.functional.layer_norm(x_reshape, (b, h * w, c))
# Get q, k, v
q = self.fc_q(x_reshape)
q = q.view(b, h * w, self.heads, self.d_k).permute(0, 2, 1, 3).contiguous() # [b, heads, h * w, d_k]
k = self.dwconv_k(x)
k_b, k_c, k_h, k_w = k.shape
k = k.view(k_b, k_c, k_h * k_w).permute(0, 2, 1).contiguous()
k = self.fc_k(k)
k = k.view(k_b, k_h * k_w, self.heads, self.d_k).permute(0, 2, 1, 3).contiguous() # [b, heads, k_h * k_w, d_k]
v = self.dwconv_v(x)
v_b, v_c, v_h, v_w = v.shape
v = v.view(v_b, v_c, v_h * v_w).permute(0, 2, 1).contiguous()
v = self.fc_v(v)
v = v.view(v_b, v_h * v_w, self.heads, self.d_v).permute(0, 2, 1, 3).contiguous() # [b, heads, v_h * v_w, d_v]
# Attention
attn = torch.einsum('... i d, ... j d -> ... i j', q, k) * self.scaled_factor
# attn = attn + self.B
attn = torch.softmax(attn, dim = -1) # [b, heads, h * w, k_h * k_w]
result = torch.matmul(attn, v).permute(0, 2, 1, 3)
result = result.contiguous().view(b, h * w, self.heads * self.d_v)
result = self.fc_o(result).view(b, self.channels, h, w)
result = result + x
return result
class IRFFN(nn.Module):
"""
Inverted Residual Feed-forward Network
"""
def __init__(self, in_channels, R):
super(IRFFN, self).__init__()
exp_channels = int(in_channels * R)
self.conv1 = nn.Sequential(
nn.Conv2d(in_channels, exp_channels, kernel_size = 1),
nn.BatchNorm2d(exp_channels),
nn.GELU()
)
self.dwconv = nn.Sequential(
DWCONV(exp_channels, exp_channels),
nn.BatchNorm2d(exp_channels),
nn.GELU()
)
self.conv2 = nn.Sequential(
nn.Conv2d(exp_channels, in_channels, 1),
nn.BatchNorm2d(in_channels)
)
def forward(self, x):
result = x + self.conv2(self.dwconv(self.conv1(x)))
return result
class Patch_Aggregate(nn.Module):
"""
Aggregate the patches into a single image.
To produce the hierachical representation.
Applied before each stage to reduce the size of intermediate features
(2x downsampling of resolution), and project it to a larger dimension
(2x enlargement of dimension).
Input:
- x: (B, In_C, H, W)
Output:
- x: (B, Out_C=In_C*2, H / 2, W / 2)
"""
def __init__(self, in_channels, out_channels = None, pad_flag = False):
super(Patch_Aggregate, self).__init__()
pad = 0
if pad_flag:
pad = 1
if out_channels is None:
out_channels = in_channels
self.conv = Conv2x2(in_channels, out_channels, stride = 2, padding = pad)
self.init_weight()
def init_weight(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight)
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m, nn.LayerNorm):
m.weight.data.fill_(1)
m.bias.data.zero_()
def forward(self, x):
x = self.conv(x)
_, c, h, w = x.size()
result = torch.nn.functional.layer_norm(x, (c, h, w))
return result
class CMTStem(nn.Module):
"""
Use CMTStem module to process input image and overcome the limitation of the
non-overlapping patches.
First past through the image with a 2x2 convolution to reduce the image size.
Then past throught two 1x1 convolution for better local information.
Input:
- x: (B, 3, H, W)
Output:
- result: (B, 32, H / 2, W / 2)
"""
def __init__(self, in_channels, out_channels):
super().__init__()
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size = 3, stride = 2, padding = 1, bias = False)
self.gelu1 = nn.GELU()
self.bn1 = nn.BatchNorm2d(out_channels)
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size = 3, stride = 1, padding = 1, bias = False)
self.gelu2 = nn.GELU()
self.bn2 = nn.BatchNorm2d(out_channels)
self.conv3 = nn.Conv2d(out_channels, out_channels, kernel_size = 3, stride = 1, padding = 1, bias = False)
self.gelu3 = nn.GELU()
self.bn3 = nn.BatchNorm2d(out_channels)
self.init_weight()
def init_weight(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight)
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def forward(self, x):
x = self.conv1(x)
x = self.gelu1(x)
x = self.bn1(x)
x = self.conv2(x)
x = self.gelu2(x)
x = self.bn2(x)
x = self.conv3(x)
x = self.gelu3(x)
result = self.bn3(x)
return result
class CMTBlock(nn.Module):
def __init__(self, img_size, stride, d_k, d_v, num_heads, R = 3.6, in_channels = 46):
super(CMTBlock, self).__init__()
# Local Perception Unit
self.lpu = LPU(in_channels, in_channels)
# Lightweight MHSA
self.lmhsa = LMHSA(img_size, in_channels, d_k, d_v, stride, num_heads, 0.0)
# Inverted Residual FFN
self.irffn = IRFFN(in_channels, R)
def forward(self, x):
x = self.lpu(x)
x = self.lmhsa(x)
x = self.irffn(x)
return x