-
Notifications
You must be signed in to change notification settings - Fork 0
/
swinT.py
434 lines (372 loc) · 16.9 KB
/
swinT.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
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import (
Layer,
Dropout,
Softmax,
LayerNormalization,
Conv2D,
Activation,
Dense,
)
from tensorflow.keras.activations import sigmoid
import collections
from tensorflow.keras import Model, Sequential
def to_2tuple(x):
if isinstance(x, collections.abc.Iterable):
return x
return (x, x)
class DropPath(Layer):
def __init__(self, prob):
super().__init__()
self.drop_prob = prob
def call(self, x, training=None):
if self.drop_prob == 0. or not training:
return x
keep_prob = 1 - self.drop_prob
shape = (x.shape[0],) + (1,) * (x.ndim - 1)
random_tensor = tf.random.uniform(shape=shape)
random_tensor = tf.where(random_tensor < keep_prob, 1, 0)
output = x / keep_prob * random_tensor
return output
class TruncatedDense(Dense):
def __init__(self, units, use_bias=False):
super().__init__(units, use_bias=use_bias)
class Mlp(Layer):
def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=Activation(tf.nn.gelu), drop=0.):
super().__init__()
out_features = out_features or in_features
hidden_features = hidden_features or in_features
self.fc1 = TruncatedDense(hidden_features)
self.act = act_layer
self.fc2 = TruncatedDense(out_features)
self.drop = Dropout(drop)
def call(self, x):
x = self.fc1(x)
x = self.act(x)
x = self.drop(x)
x = self.fc2(x)
x = self.drop(x)
return x
def window_partition(x, window_size):
"""
Args:
x: (B, H, W, C)
window_size (int): window size
Returns:
windows: (num_windows*B, window_size, window_size, C)
"""
B, H, W, C = x.shape
B=-1
x = tf.reshape(x, [B, H // window_size, window_size, W // window_size, window_size, C])
# TODO contiguous memory access?
windows = tf.reshape(tf.transpose(x, perm=[0, 1, 3, 2, 4, 5]), [-1, window_size, window_size, C])
return windows
@tf.function
def window_reverse(windows, window_size, H, W,C):
"""
Args:
windows: (num_windows*B, window_size, window_size, C)
window_size (int): Window size
H (int): Height of image
W (int): Width of image
Returns:
x: (B, H, W, C)
"""
#B = int(windows.shape[0] / (H * W / window_size / window_size))
B=-1
x = tf.reshape(windows, [B, H // window_size, W // window_size, window_size, window_size, C])
x = tf.reshape(tf.transpose(x, perm=[0, 1, 3, 2, 4, 5]), [B, H, W, C])
return x
def SAD(y_true, y_pred):
A = -tf.keras.losses.cosine_similarity(y_true,y_pred)
sad = tf.math.acos(A)
return sad
def C(x,y):
val=1.0-SAD(x,y)/np.pi
return val
class WindowAttention(Layer):
r""" Window based multi-head self attention (W-MSA) module with relative position bias.
It supports both of shifted and non-shifted window.
Args:
dim (int): Number of input channels.
window_size (tuple[int]): The height and width of the window.
num_heads (int): Number of attention heads.
qkv_bias (bool, optional): If True, add a learnable bias to query, key, value. Default: True
qk_scale (float | None, optional): Override default qk scale of head_dim ** -0.5 if set
attn_drop (float, optional): Dropout ratio of attention weight. Default: 0.0
proj_drop (float, optional): Dropout ratio of output. Default: 0.0
"""
def __init__(self, dim, window_size, num_heads, qkv_bias=False, qk_scale=None, attn_drop=0., proj_drop=0.):
super().__init__()
self.dim = dim
self.window_size = window_size # Wh, Ww
self.num_heads = num_heads
head_dim = dim // num_heads
self.scale = qk_scale or head_dim ** -0.5
# define a parameter table of relative position bias
initializer = tf.keras.initializers.TruncatedNormal(mean=0., stddev=.02)
self.relative_position_bias_table = tf.Variable(
initializer(shape=((2 * self.window_size[0] - 1) * (2 * self.window_size[1] - 1), num_heads)),
name="relative_position_bias_table") # 2*Wh-1 * 2*Ww-1, nH
# get pair-wise relative position index for each token inside the window
coords_h = tf.range(self.window_size[0])
coords_w = tf.range(self.window_size[1])
coords = tf.stack(tf.meshgrid(coords_h, coords_w)) # 2, Wh, Ww
coords_flatten = tf.reshape(coords, [2, -1]) # 2, Wh*Ww
relative_coords = coords_flatten[:, :, None] - coords_flatten[:, None, :] # 2, Wh*Ww, Wh*Ww
relative_coords = tf.transpose(relative_coords, perm=[1, 2, 0]) # Wh*Ww, Wh*Ww, 2
relative_coords = relative_coords + [self.window_size[0] - 1, self.window_size[1] - 1] # shift to start from 0
relative_coords = relative_coords * [2 * self.window_size[1] - 1, 1]
self.relative_position_index = tf.math.reduce_sum(relative_coords, -1) # Wh*Ww, Wh*Ww
self.qkv = TruncatedDense(dim * 3, use_bias=qkv_bias)
self.attn_drop = Dropout(attn_drop)
self.proj = TruncatedDense(dim)
self.proj_drop = Dropout(proj_drop)
self.softmax = Softmax(axis=-1)
def call(self, x, mask=None):
"""
Args:
x: input features with shape of (num_windows*B, N, C)
mask: (0/-inf) mask with shape of (num_windows, Wh*Ww, Wh*Ww) or None
"""
B_, N, C = x.shape
B_=-1
qkv = tf.transpose(tf.reshape(self.qkv(x), [B_, N, 3, self.num_heads, C // self.num_heads]),
perm=[2, 0, 3, 1, 4]) # [3, B_, num_head, Ww*Wh, C//num_head]
q, k, v = tf.unstack(qkv) # make torchscript happy (cannot use tensor as tuple)
q = q * self.scale
attn = tf.einsum('...ij,...kj->...ik', q, k)
relative_position_bias = tf.reshape(
tf.gather(self.relative_position_bias_table, tf.reshape(self.relative_position_index, [-1])),
[self.window_size[0] * self.window_size[1], self.window_size[0] * self.window_size[1],
-1]) # Wh*Ww,Wh*Ww,nH
relative_position_bias = tf.transpose(relative_position_bias, perm=[2, 0, 1]) # nH, Wh*Ww, Wh*Ww
attn = attn + relative_position_bias
if mask is not None and mask.shape != ():
nW = mask.shape[0] # every window has different mask [nW, N, N]
attn = tf.reshape(attn, [B_ // nW, nW, self.num_heads, N, N]) + mask[:, None, :,
:] # add mask: make each component -inf or just leave it
attn = tf.reshape(attn, [-1, self.num_heads, N, N])
attn = self.softmax(attn)
else:
attn = self.softmax(attn)
attn = self.attn_drop(attn)
x = tf.reshape(tf.transpose(attn @ v, perm=[0, 2, 1, 3]), [B_, N, C])
x = self.proj(x)
x = self.proj_drop(x)
return x
def extra_repr(self) -> str:
return f'dim={self.dim}, window_size={self.window_size}, num_heads={self.num_heads}'
def flops(self, N):
# calculate flops for 1 window with token length of N
flops = 0
# qkv = self.qkv(x)
flops += N * self.dim * 3 * self.dim
# attn = (q @ k.transpose(-2, -1))
flops += self.num_heads * N * (self.dim // self.num_heads) * N
# x = (attn @ v)
flops += self.num_heads * N * N * (self.dim // self.num_heads)
# x = self.proj(x)
flops += N * self.dim * self.dim
return flops
class SwinTransformerBlock(Layer):
def __init__(self, dim, input_resolution, num_heads, window_size=7, shift_size=0,
mlp_ratio=4., qkv_bias=False, qk_scale=None, drop=0., attn_drop=0., drop_path=0.,
act_layer=Activation(tf.nn.gelu), norm_layer=LayerNormalization):
super().__init__()
self.dim = dim
self.input_resolution = input_resolution
self.num_heads = num_heads
self.window_size = window_size
self.shift_size = shift_size
self.mlp_ratio = mlp_ratio
if min(self.input_resolution) <= self.window_size:
# if window size is larger than input resolution, we don't partition windows
self.shift_size = 0
self.window_size = min(self.input_resolution)
assert 0 <= self.shift_size < self.window_size, "shift_size must in 0-window_size"
self.norm1 = norm_layer(epsilon=1e-5)
self.attn = WindowAttention(
dim, window_size=to_2tuple(self.window_size), 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. else tf.identity
self.norm2 = norm_layer(epsilon=1e-5)
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 self.shift_size > 0:
# calculate attention mask for SW-MSA
H, W = self.input_resolution
img_mask = np.zeros([1, H, W, 1]) # 1 H W 1
h_slices = (slice(0, -self.window_size),
slice(-self.window_size, -self.shift_size),
slice(-self.shift_size, None))
w_slices = (slice(0, -self.window_size),
slice(-self.window_size, -self.shift_size),
slice(-self.shift_size, None))
cnt = 0
for h in h_slices:
for w in w_slices:
img_mask[:, h, w, :] = cnt
cnt += 1
img_mask = tf.constant(img_mask)
mask_windows = window_partition(img_mask, self.window_size) # nW, window_size, window_size, 1
mask_windows = tf.reshape(mask_windows, [-1, self.window_size * self.window_size])
attn_mask = mask_windows[:, None, :] - mask_windows[:, :, None]
self.attn_mask = tf.where(attn_mask == 0, -100., 0.)
else:
self.attn_mask = None
def call(self, x):
H, W = self.input_resolution
B, L, C = x.shape
B=-1
assert L == H * W, "input feature has wrong size"
shortcut = x
x = self.norm1(x)
x = tf.reshape(x, [B, H, W, C])
# cyclic shift
if self.shift_size > 0:
shifted_x = tf.roll(x, shift=[-self.shift_size, -self.shift_size], axis=(1, 2))
else:
shifted_x = x
# partition windows
x_windows = window_partition(shifted_x, self.window_size) # nW*B, window_size, window_size, C
x_windows = tf.reshape(x_windows,
[-1, self.window_size * self.window_size, C]) # nW*B, window_size*window_size, C
# W-MSA/SW-MSA
attn_windows = self.attn(x_windows, mask=self.attn_mask) # nW*B, window_size*window_size, C
# merge windows
attn_windows = tf.reshape(attn_windows, [-1, self.window_size, self.window_size, C])
shifted_x = window_reverse(attn_windows, self.window_size, H, W,C) # B H' W' C
# reverse cyclic shift
if self.shift_size > 0:
x = tf.roll(shifted_x, shift=[self.shift_size, self.shift_size], axis=(1, 2))
else:
x = shifted_x
x = tf.reshape(x, [B, H * W, C])
# FFN
x = shortcut + self.drop_path(x)
x = x + self.drop_path(self.mlp(self.norm2(x)))
return x
def extra_repr(self) -> str:
return f"dim={self.dim}, input_resolution={self.input_resolution}, num_heads={self.num_heads}, " \
f"window_size={self.window_size}, shift_size={self.shift_size}, mlp_ratio={self.mlp_ratio}"
def flops(self):
flops = 0
H, W = self.input_resolution
# norm1
flops += self.dim * H * W
# W-MSA/SW-MSA
nW = H * W / self.window_size / self.window_size
flops += nW * self.attn.flops(self.window_size * self.window_size)
# mlp
flops += 2 * H * W * self.dim * self.dim * self.mlp_ratio
# norm2
flops += self.dim * H * W
return flops
class BasicLayer(Layer):
def __init__(self, dim, input_resolution, depth, num_heads, window_size,
mlp_ratio=4., qkv_bias=False, qk_scale=None, drop=0., attn_drop=0.,
drop_path=0., norm_layer=LayerNormalization):
super().__init__()
self.dim = dim
self.input_resolution = input_resolution
self.depth = depth
# build blocks
self.blocks = [
SwinTransformerBlock(dim=dim, input_resolution=input_resolution,
num_heads=num_heads, window_size=window_size,
shift_size=0 if (i % 2 == 0) else window_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)
for i in range(depth)]
def call(self, x):
for blk in self.blocks:
x = blk(x)
return x
def extra_repr(self) -> str:
return f"dim={self.dim}, input_resolution={self.input_resolution}, depth={self.depth}"
def flops(self):
flops = 0
for blk in self.blocks:
flops += blk.flops()
return flops
class SwinTransformer(Model):
def __init__(self, out_channels,in_channels, input_resolution,depths=[2], num_heads=[6],
window_size=7, mlp_ratio=4., qkv_bias=False, qk_scale=None,
drop_rate=0., attn_drop_rate=0., drop_path_rate=0.,
norm_layer=LayerNormalization,
**kwargs):
super().__init__()
self.dim = in_channels
self.window_size=window_size
self.num_layers=len(depths)
self.input_resolution = tuple([i // self.window_size * self.window_size for i in input_resolution])
self.mlp_ratio = mlp_ratio
# stochastic depth
dpr = [x for x in np.linspace(0, drop_path_rate, sum(depths))] # stochastic depth decay rule
# build layers
self.sequence = Sequential(name="basic_layers_seq")
for i_layer in range(self.num_layers):
self.sequence.add(BasicLayer(dim=in_channels,
input_resolution=self.input_resolution,
depth=depths[i_layer],
num_heads=num_heads[i_layer],
window_size=window_size,
mlp_ratio=self.mlp_ratio,
qkv_bias=qkv_bias, qk_scale=qk_scale,
drop=drop_rate, attn_drop=attn_drop_rate,
drop_path=dpr[sum(depths[:i_layer]):sum(depths[:i_layer + 1])],
norm_layer=norm_layer))
# TODO: Check impact of epsilon
self.norm = norm_layer(epsilon=1e-5)
self.cnn =Conv2D(
filters=out_channels,
kernel_size=1,
strides=1, use_bias=False
)
def forward_features(self, x):
B, H, W, C = x.shape
B=-1
x = tf.reshape(x, [B, H * W,C])
x = self.sequence(x)
x = self.norm(x) # B L C
x = tf.reshape(x, [B, H, W, C])
#x = self.cnn(x)
return x
def call(self, x):
x = self.forward_features(x)
return x
class SwinT(Model):
def __init__(self,out_channels=1,depths = [2], num_heads = [4],window_size = 4, mlp_ratio = 4., qkv_bias = False, qk_scale = None,
drop_rate = 0, attn_drop_rate = 0, drop_path_rate = 0.1,norm_layer = LayerNormalization,):
super().__init__()
self.depths = depths
self.num_heads = num_heads
self.window_size =window_size
self.mlp_ratio =mlp_ratio
self.qkv_bias=qkv_bias
self.qk_scale = qk_scale
self.drop_rate = drop_rate
self.attn_drop_rate = attn_drop_rate
self.drop_path_rate = drop_path_rate
self.norm_layer = norm_layer
self.out_channels = out_channels
def forward(self,x):
# x:[b,c,h,w]
in_channels=x.shape[3]
h=x.shape[1]
w = x.shape[2]
if self.out_channels == 1:
self.out_channels = in_channels
self.SwinT = SwinTransformer(out_channels=self.out_channels,in_channels=in_channels, input_resolution=(h,w),depths=self.depths, num_heads=self.num_heads,
window_size=self.window_size, mlp_ratio=self.mlp_ratio, qkv_bias=self.qkv_bias, qk_scale=self.qk_scale,
drop_rate=self.drop_rate, attn_drop_rate=self.attn_drop_rate, drop_path_rate=self.drop_path_rate,
norm_layer=self.norm_layer)
return self.SwinT(x)
def call(self, x):
x = self.forward(x)
return x