-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransformer.py
More file actions
362 lines (273 loc) · 12.6 KB
/
Transformer.py
File metadata and controls
362 lines (273 loc) · 12.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
import numpy as np
from torch.nn.init import _calculate_fan_in_and_fan_out
from timm.models.layers import to_2tuple, trunc_normal_
class RLN(nn.Module):
r"""Revised LayerNorm"""
def __init__(self, dim, eps=1e-5, detach_grad=False):
super(RLN, self).__init__()
self.eps = eps
self.detach_grad = detach_grad
self.weight = nn.Parameter(torch.ones((1, dim, 1, 1)))
self.bias = nn.Parameter(torch.zeros((1, dim, 1, 1)))
self.meta1 = nn.Conv2d(1, dim, 1)
self.meta2 = nn.Conv2d(1, dim, 1)
trunc_normal_(self.meta1.weight, std=.02)
nn.init.constant_(self.meta1.bias, 1)
trunc_normal_(self.meta2.weight, std=.02)
nn.init.constant_(self.meta2.bias, 0)
def forward(self, input):
mean = torch.mean(input, dim=(1, 2, 3), keepdim=True)
std = torch.sqrt((input - mean).pow(2).mean(dim=(1, 2, 3), keepdim=True) + self.eps)
normalized_input = (input - mean) / std
if self.detach_grad:
rescale, rebias = self.meta1(std.detach()), self.meta2(mean.detach())
else:
rescale, rebias = self.meta1(std), self.meta2(mean)
out = normalized_input * self.weight + self.bias
return out, rescale, rebias
class Mlp(nn.Module):
def __init__(self, network_depth, in_features, hidden_features=None, out_features=None):
super().__init__()
out_features = out_features or in_features
hidden_features = hidden_features or in_features
self.network_depth = network_depth
self.mlp = nn.Sequential(
nn.Conv2d(in_features, hidden_features, 1),
nn.ReLU(True),
nn.InstanceNorm2d(hidden_features, affine=True),
nn.Conv2d(hidden_features, out_features, 1)
)#加了实例归一化
self.apply(self._init_weights)
def _init_weights(self, m):
if isinstance(m, nn.Conv2d):
gain = (8 * self.network_depth) ** (-1 / 4)
fan_in, fan_out = _calculate_fan_in_and_fan_out(m.weight)
std = gain * math.sqrt(2.0 / float(fan_in + fan_out))
trunc_normal_(m.weight, std=std)
if m.bias is not None:
nn.init.constant_(m.bias, 0)
def forward(self, x):
return self.mlp(x)
def window_partition(x, window_size):
B, H, W, C = x.shape
x = x.view(B, H // window_size, window_size, W // window_size, window_size, C)
windows = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(-1, window_size ** 2, C)
return windows
def window_reverse(windows, window_size, H, W):
B = int(windows.shape[0] / (H * W / window_size / window_size))
x = windows.view(B, H // window_size, W // window_size, window_size, window_size, -1)
x = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(B, H, W, -1)
return x
def get_relative_positions(window_size):
coords_h = torch.arange(window_size)
coords_w = torch.arange(window_size)
coords = torch.stack(torch.meshgrid([coords_h, coords_w])) # 2, Wh, Ww
coords_flatten = torch.flatten(coords, 1) # 2, Wh*Ww
relative_positions = coords_flatten[:, :, None] - coords_flatten[:, None, :] # 2, Wh*Ww, Wh*Ww
relative_positions = relative_positions.permute(1, 2, 0).contiguous() # Wh*Ww, Wh*Ww, 2
relative_positions_log = torch.sign(relative_positions) * torch.log(1. + relative_positions.abs())
return relative_positions_log
class WindowAttention(nn.Module):
def __init__(self, dim, window_size, num_heads):
super().__init__()
self.dim = dim
self.window_size = window_size # Wh, Ww
self.num_heads = num_heads
head_dim = dim // num_heads
self.scale = head_dim ** -0.5
relative_positions = get_relative_positions(self.window_size)
self.register_buffer("relative_positions", relative_positions)
self.meta = nn.Sequential(
nn.Linear(2, 256, bias=True),
nn.ReLU(True),
nn.Linear(256, num_heads, bias=True)
)
self.softmax = nn.Softmax(dim=-1)
def forward(self, qkv):
B_, N, _ = qkv.shape
qkv = qkv.reshape(B_, N, 3, self.num_heads, self.dim // self.num_heads).permute(2, 0, 3, 1, 4)
q, k, v = qkv[0], qkv[1], qkv[2] # make torchscript happy (cannot use tensor as tuple)
q = q * self.scale
attn = (q @ k.transpose(-2, -1))
relative_position_bias = self.meta(self.relative_positions)
relative_position_bias = relative_position_bias.permute(2, 0, 1).contiguous() # nH, Wh*Ww, Wh*Ww
attn = attn + relative_position_bias.unsqueeze(0)
attn = self.softmax(attn)
x = (attn @ v).transpose(1, 2).reshape(B_, N, self.dim)
return x
class Attention(nn.Module):
def __init__(self, network_depth, dim, num_heads, window_size, shift_size, use_attn=False, conv_type=None):
super().__init__()
self.dim = dim
self.head_dim = int(dim // num_heads)
self.num_heads = num_heads
self.window_size = window_size
self.shift_size = shift_size
self.network_depth = network_depth
self.use_attn = use_attn
self.conv_type = conv_type
self.ca = nn.Sequential(*[
nn.AdaptiveAvgPool2d(1),
nn.Conv2d(dim, dim // 16, 1, padding=0),
nn.ReLU(inplace=True),
nn.Conv2d(dim // 16, dim, 1, padding=0, bias=True),
nn.Sigmoid()
])
if self.conv_type == 'Conv':
self.conv = nn.Sequential(
nn.Conv2d(dim, dim, kernel_size=3, padding=1, padding_mode='reflect'),
nn.ReLU(True),
nn.Conv2d(dim, dim, kernel_size=3, padding=1, padding_mode='reflect')
)
if self.conv_type == 'DWConv':
self.conv = nn.Sequential(*[
nn.Conv2d(dim, dim, kernel_size=5, padding=2, groups=dim, padding_mode='reflect'),
])
if self.conv_type == 'DWConv' or self.use_attn:
self.V = nn.Conv2d(dim, dim, 1)
self.proj = nn.Conv2d(dim, dim, 1)
if self.use_attn:
self.QK = nn.Conv2d(dim, dim * 2, 1)
self.attn = WindowAttention(dim, window_size, num_heads)
self.apply(self._init_weights)
def _init_weights(self, m):
if isinstance(m, nn.Conv2d):
w_shape = m.weight.shape
if w_shape[0] == self.dim * 2: # QK
fan_in, fan_out = _calculate_fan_in_and_fan_out(m.weight)
std = math.sqrt(2.0 / float(fan_in + fan_out))
trunc_normal_(m.weight, std=std)
else:
gain = (8 * self.network_depth) ** (-1 / 4)
fan_in, fan_out = _calculate_fan_in_and_fan_out(m.weight)
std = gain * math.sqrt(2.0 / float(fan_in + fan_out))
trunc_normal_(m.weight, std=std)
if m.bias is not None:
nn.init.constant_(m.bias, 0)
def check_size(self, x, shift=False):
_, _, h, w = x.size()
mod_pad_h = (self.window_size - h % self.window_size) % self.window_size
mod_pad_w = (self.window_size - w % self.window_size) % self.window_size
if shift:
x = F.pad(x, (self.shift_size, (self.window_size - self.shift_size + mod_pad_w) % self.window_size,
self.shift_size, (self.window_size - self.shift_size + mod_pad_h) % self.window_size),
mode='reflect')
else:
x = F.pad(x, (0, mod_pad_w, 0, mod_pad_h), 'reflect')
return x
def forward(self, X):
B, C, H, W = X.shape
if self.conv_type == 'DWConv' or self.use_attn:
V = self.V(X)
if self.use_attn:
QK = self.QK(X)
QKV = torch.cat([QK, V], dim=1)
# shift
shifted_QKV = self.check_size(QKV, self.shift_size > 0)
Ht, Wt = shifted_QKV.shape[2:]
# partition windows
shifted_QKV = shifted_QKV.permute(0, 2, 3, 1)
qkv = window_partition(shifted_QKV, self.window_size) # nW*B, window_size**2, C
attn_windows = self.attn(qkv)
# merge windows
shifted_out = window_reverse(attn_windows, self.window_size, Ht, Wt) # B H' W' C
# reverse cyclic shift
out = shifted_out[:, self.shift_size:(self.shift_size + H), self.shift_size:(self.shift_size + W), :]
attn_out = out.permute(0, 3, 1, 2)
if self.conv_type in ['Conv', 'DWConv']:
conv_out = self.conv(V)
out = self.proj(conv_out + attn_out)
else:
out = self.proj(attn_out)
else:
if self.conv_type == 'Conv':
out = self.conv(X) # no attention and use conv, no projection
elif self.conv_type == 'DWConv':
out = self.proj(self.conv(V))
return out
class TransformerBlock(nn.Module):
def __init__(self, network_depth, dim, num_heads, mlp_ratio=4.,
norm_layer=nn.LayerNorm, mlp_norm=False,
window_size=8, shift_size=0, use_attn=True, conv_type=None):
super().__init__()
self.use_attn = use_attn
self.mlp_norm = mlp_norm
self.norm1 = norm_layer(dim) if use_attn else nn.Identity()
self.attn = Attention(network_depth, dim, num_heads=num_heads, window_size=window_size,
shift_size=shift_size, use_attn=use_attn, conv_type=conv_type)
self.norm2 = norm_layer(dim) if use_attn and mlp_norm else nn.Identity()
self.mlp = Mlp(network_depth, dim, hidden_features=int(dim * mlp_ratio))
self.norm = nn.InstanceNorm2d(dim, affine=True)
def forward(self, x):
identity = x # 16x48x128x128
if self.use_attn: x, rescale, rebias = self.norm1(x)
x = self.attn(x)
if self.use_attn: x = x * rescale + rebias
x = identity + x
identity = x
if self.use_attn and self.mlp_norm: x, rescale, rebias = self.norm2(x)
x = self.norm(x)##实力归一化
x = self.mlp(x)
if self.use_attn and self.mlp_norm: x = x * rescale + rebias
x = identity + x
return x
class BasicLayer(nn.Module):
def __init__(self, network_depth, dim, depth, num_heads, mlp_ratio=4.,
norm_layer=nn.LayerNorm, window_size=8,
attn_ratio=0., attn_loc='last', conv_type=None):
super().__init__()
self.dim = dim
self.depth = depth
attn_depth = attn_ratio * depth
if attn_loc == 'last':
use_attns = [i >= depth - attn_depth for i in range(depth)]
elif attn_loc == 'first':
use_attns = [i < attn_depth for i in range(depth)]
elif attn_loc == 'middle':
use_attns = [i >= (depth - attn_depth) // 2 and i < (depth + attn_depth) // 2 for i in range(depth)]
# build blocks
self.blocks = nn.ModuleList([
TransformerBlock(network_depth=network_depth,
dim=dim,
num_heads=num_heads,
mlp_ratio=mlp_ratio,
norm_layer=norm_layer,
window_size=window_size,
shift_size=0 if (i % 2 == 0) else window_size // 2,
use_attn=use_attns[i], conv_type=conv_type)
for i in range(depth)])
def forward(self, x):
for blk in self.blocks:
x = blk(x)
return x
class PatchEmbed(nn.Module):
def __init__(self, patch_size=4, in_chans=3, embed_dim=96, kernel_size=None):
super().__init__()
self.in_chans = in_chans
self.embed_dim = embed_dim
if kernel_size is None:
kernel_size = patch_size
self.proj = nn.Conv2d(in_chans, embed_dim, kernel_size=kernel_size, stride=patch_size,
padding=(kernel_size - patch_size + 1) // 2, padding_mode='reflect')
def forward(self, x):
x = self.proj(x) # 4X24X256X256
return x
class PatchUnEmbed(nn.Module):
def __init__(self, patch_size=4, out_chans=3, embed_dim=96, kernel_size=None):
super().__init__()
self.out_chans = out_chans
self.embed_dim = embed_dim
if kernel_size is None:
kernel_size = 1
self.proj = nn.Sequential(
nn.Conv2d(embed_dim, out_chans * patch_size ** 2, kernel_size=kernel_size,
padding=kernel_size // 2, padding_mode='reflect'),
nn.PixelShuffle(patch_size)
)
def forward(self, x):
x = self.proj(x)
return x