-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdat_local_attention_base_backbone.py
383 lines (325 loc) · 13.3 KB
/
dat_local_attention_base_backbone.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
# +
# --------------------------------------------------------
# Swin Transformer
# Copyright (c) 2021 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Ze Liu
# --------------------------------------------------------
# Vision Transformer with Deformable Attention
# Modified by Zhuofan Xia
# --------------------------------------------------------
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from timm.models.layers import DropPath, to_2tuple
from dat_blocks import *
class TransformerStage(nn.Module):
def __init__(self, fmap_size, window_size, ns_per_pt,
dim_in, dim_embed, depths, stage_spec, n_groups,
use_pe, sr_ratio,
heads, stride, offset_range_factor, stage_idx,
dwc_pe, no_off, fixed_pe,
attn_drop, proj_drop, expansion, drop, drop_path_rate, use_dwc_mlp):
super().__init__()
fmap_size = to_2tuple(fmap_size)
self.depths = depths
hc = dim_embed // heads
assert dim_embed == heads * hc
self.proj = nn.Conv2d(dim_in, dim_embed, 1, 1, 0) if dim_in != dim_embed else nn.Identity()
self.layer_norms = nn.ModuleList(
[LayerNormProxy(dim_embed) for _ in range(2 * depths)]
)
self.mlps = nn.ModuleList(
[
TransformerMLPWithConv(dim_embed, expansion, drop)
if use_dwc_mlp else TransformerMLP(dim_embed, expansion, drop)
for _ in range(depths)
]
)
self.attns = nn.ModuleList()
self.drop_path = nn.ModuleList()
for i in range(depths):
if stage_spec[i] == 'L':
self.attns.append(
LocalAttention(dim_embed, heads, window_size, attn_drop, proj_drop)
)
elif stage_spec[i] == 'D':
self.attns.append(
DAttentionBaseline(fmap_size, fmap_size, heads,
hc, n_groups, attn_drop, proj_drop,
stride, offset_range_factor, use_pe, dwc_pe,
no_off, fixed_pe, stage_idx)
)
elif stage_spec[i] == 'S':
shift_size = math.ceil(window_size / 2)
self.attns.append(
ShiftWindowAttention(dim_embed, heads, window_size, attn_drop, proj_drop, shift_size, fmap_size)
)
else:
raise NotImplementedError(f'Spec: {stage_spec[i]} is not supported.')
self.drop_path.append(DropPath(drop_path_rate[i]) if drop_path_rate[i] > 0.0 else nn.Identity())
def forward(self, x):
x = self.proj(x)
positions = []
references = []
for d in range(self.depths):
x0 = x
x, pos, ref = self.attns[d](self.layer_norms[2 * d](x))
x = self.drop_path[d](x) + x0
x0 = x
x = self.mlps[d](self.layer_norms[2 * d + 1](x))
x = self.drop_path[d](x) + x0
positions.append(pos)
references.append(ref)
return x, positions, references
class DWConv2d_BN(nn.Module):
"""Depthwise Separable Convolution with BN module."""
def __init__(
self,
in_ch,
out_ch,
kernel_size=1,
stride=1,
norm_layer=nn.BatchNorm2d,
act_layer=nn.Hardswish,
bn_weight_init=1,
):
super().__init__()
# dw
self.dwconv = nn.Conv2d(
in_ch,
out_ch,
kernel_size,
stride,
(kernel_size - 1) // 2,
groups=out_ch,
bias=False,
)
# pw-linear
self.pwconv = nn.Conv2d(out_ch, out_ch, 1, 1, 0, bias=False)
self.bn = norm_layer(out_ch)
self.act = act_layer() if act_layer is not None else nn.Identity()
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2.0 / n))
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(bn_weight_init)
m.bias.data.zero_()
def forward(self, x):
"""
foward function
"""
x = self.dwconv(x)
x = self.pwconv(x)
x = self.bn(x)
x = self.act(x)
return x
class ChannelAttention(nn.Module):
def __init__(self, in_planes, ratio=16):
super(ChannelAttention, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.max_pool = nn.AdaptiveMaxPool2d(1)
self.fc = nn.Sequential(nn.Conv2d(in_planes, in_planes // 16, 1, bias=False),
nn.ReLU(),
nn.Conv2d(in_planes // 16, in_planes, 1, bias=False))
self.sigmoid = nn.Sigmoid()
def forward(self, x):
avg_out = self.fc(self.avg_pool(x))
max_out = self.fc(self.max_pool(x))
out = avg_out + max_out
return self.sigmoid(out)
class SpatialAttention(nn.Module):
def __init__(self, kernel_size=7):
super(SpatialAttention, self).__init__()
self.conv1 = nn.Conv2d(2, 1, kernel_size, padding=kernel_size//2, bias=False)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
avg_out = torch.mean(x, dim=1, keepdim=True)
max_out, _ = torch.max(x, dim=1, keepdim=True)
x = torch.cat([avg_out, max_out], dim=1)
x = self.conv1(x)
return self.sigmoid(x)
class DAT(nn.Module):
def __init__(self, img_size=224, patch_size=4, num_classes=7, expansion=4,
dim_stem=128, dims=[128, 256, 512, 1024], depths=[2, 2, 18, 2],
heads=[4, 8, 16, 32],
window_sizes=[7, 7, 7, 7],
drop_rate=0.0, attn_drop_rate=0.0, drop_path_rate=0.0,
strides=[-1,-1,-1,-1], offset_range_factor=[1, 2, 3, 4],
stage_spec=[['L', 'D'], ['L', 'D'], ['L', 'D', 'L', 'D', 'L', 'D'], ['L', 'D']],
groups=[-1, -1, 3, 6],
use_pes=[False, False, False, False],
dwc_pes=[False, False, False, False],
sr_ratios=[8, 4, 2, 1],
fixed_pes=[False, False, False, False],
no_offs=[False, False, False, False],
ns_per_pts=[4, 4, 4, 4],
use_dwc_mlps=[False, False, False, False],
use_conv_patches=False,
**kwargs):
super().__init__()
self.stem = nn.Sequential(
nn.Conv2d(3, 64, (3, 3), 2, 1),
nn.BatchNorm2d(64),
nn.Conv2d(64, dim_stem, (3, 3), 2, 1),
LayerNormProxy(dim_stem)
) if use_conv_patches else nn.Sequential(
nn.Conv2d(3, dim_stem, patch_size, patch_size, 0),
LayerNormProxy(dim_stem)
)
img_size = img_size // patch_size
dpr = [x.item() for x in torch.linspace(0, drop_path_rate, sum(depths))]
self.stages = nn.ModuleList()
for i in range(4):
dim1 = dim_stem if i == 0 else dims[i - 1] * 2
dim2 = dims[i]
self.stages.append(
TransformerStage(img_size, window_sizes[i], ns_per_pts[i],
dim1, dim2, depths[i], stage_spec[i], groups[i], use_pes[i],
sr_ratios[i], heads[i], strides[i],
offset_range_factor[i], i,
dwc_pes[i], no_offs[i], fixed_pes[i],
attn_drop_rate, drop_rate, expansion, drop_rate,
dpr[sum(depths[:i]):sum(depths[:i + 1])],
use_dwc_mlps[i])
)
img_size = img_size // 2
self.down_projs = nn.ModuleList()
for i in range(3):
self.down_projs.append(
nn.Sequential(
nn.Conv2d(dims[i], dims[i + 1], 3, 2, 1, bias=False),
LayerNormProxy(dims[i + 1])
) if use_conv_patches else nn.Sequential(
nn.Conv2d(dims[i], dims[i + 1], 2, 2, 0, bias=False),
LayerNormProxy(dims[i + 1])
)
)
self.cls_norm = LayerNormProxy(dims[-1])
self.cls_head = nn.Linear(dims[-1], num_classes)
self.reset_parameters()
self.down_sample_1 = nn.Conv2d(128, 256, (3, 3), stride=2, padding=1)
self.bn1 = nn.BatchNorm2d(256)
self.ca1 = ChannelAttention(256)
self.sa1 = SpatialAttention()
self.down_sample_2 = nn.Conv2d(256, 512, (3, 3), stride=2, padding=1)
self.bn2 = nn.BatchNorm2d(512)
self.ca2 = ChannelAttention(512)
self.sa2 = SpatialAttention()
self.down_sample_3 = nn.Conv2d(512, 1024, (3, 3), stride=2, padding=1)
self.bn3 = nn.BatchNorm2d(1024)
self.ca3 = ChannelAttention(1024)
self.sa3 = SpatialAttention()
self.ca4 = ChannelAttention(1024)
self.sa4 = SpatialAttention()
self.act = nn.ReLU()
def reset_parameters(self):
for m in self.parameters():
if isinstance(m, (nn.Linear, nn.Conv2d)):
nn.init.kaiming_normal_(m.weight)
nn.init.zeros_(m.bias)
@torch.no_grad()
def load_pretrained(self, state_dict):
new_state_dict = {}
for state_key, state_value in state_dict.items():
keys = state_key.split('.')
m = self
for key in keys:
if key.isdigit():
m = m[int(key)]
else:
m = getattr(m, key)
if m.shape == state_value.shape:
new_state_dict[state_key] = state_value
else:
# Ignore different shapes
if 'relative_position_index' in keys:
new_state_dict[state_key] = m.data
if 'q_grid' in keys:
new_state_dict[state_key] = m.data
if 'reference' in keys:
new_state_dict[state_key] = m.data
# Bicubic Interpolation
if 'relative_position_bias_table' in keys:
n, c = state_value.size()
l = int(math.sqrt(n))
assert n == l ** 2
L = int(math.sqrt(m.shape[0]))
pre_interp = state_value.reshape(1, l, l, c).permute(0, 3, 1, 2)
post_interp = F.interpolate(pre_interp, (L, L), mode='bicubic')
new_state_dict[state_key] = post_interp.reshape(c, L ** 2).permute(1, 0)
if 'rpe_table' in keys:
c, h, w = state_value.size()
C, H, W = m.data.size()
pre_interp = state_value.unsqueeze(0)
post_interp = F.interpolate(pre_interp, (H, W), mode='bicubic')
new_state_dict[state_key] = post_interp.squeeze(0)
self.load_state_dict(new_state_dict, strict=False)
@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', 'rpe_table'}
def forward_feature(self, x):
outs = []
# stem block
embed_feature = self.stem(x)
positions = []
references = []
# f1 skip CBAM skip connection
skip1 = self.down_sample_1(embed_feature)
skip1 = self.bn1(skip1)
skip1 = self.act(skip1)
f1, pos, ref = self.stages[0](embed_feature)
f1 = self.down_projs[0](f1)
residual_f1 = f1
f1 = self.ca1(skip1) * f1
f1 = self.sa1(f1) * f1 + residual_f1
positions.append(pos)
references.append(ref)
outs.append(f1)
# f2 skip CBAM connection
skip2 = self.down_sample_2(f1)
skip2 = self.bn2(skip2)
skip2 = self.act(skip2)
f2, pos, ref = self.stages[1](f1)
f2 = self.down_projs[1](f2)
residual_f2 = f2
f2 = self.ca2(skip2) * f2
f2 = self.sa2(f2) * f2 + residual_f2
positions.append(pos)
references.append(ref)
outs.append(f2)
# f3 skip CBAM connection
skip3 = self.down_sample_3(f2)
skip3 = self.bn3(skip3)
skip3 = self.act(skip3)
f3, pos, ref = self.stages[2](f2)
f3 = self.down_projs[2](f3)
residual_f3 = f3
f3 = self.ca3(skip3) * f3
f3 = self.sa3(f3) * f3 + residual_f3
positions.append(pos)
references.append(ref)
outs.append(f3)
# f4 skip CBAM connection
f4, pos, ref = self.stages[3](f3)
residual_f4 = f4
f4 = self.ca4(f4) * f4
f4 = self.sa4(f4) * f4 + residual_f4
positions.append(pos)
references.append(ref)
outs.append(f4)
out = self.cls_norm(f4)
out = F.adaptive_avg_pool2d(out, 1)
out = torch.flatten(out, 1)
out = self.cls_head(out)
return out, outs
def forward(self, x):
res, _ = self.forward_feature(x)
return res, _