-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDuAT.py
405 lines (323 loc) · 14.3 KB
/
DuAT.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from lib.pvtv2 import pvt_v2_b2
import os
import torch
import torch.nn as nn
import torch.nn.functional as F
from mmcv.cnn import ConvModule
from torch.nn import Conv2d, UpsamplingBilinear2d
import warnings
import torch
from mmcv.cnn import constant_init, kaiming_init
from torch import nn
from torchvision.transforms.functional import normalize
warnings.filterwarnings('ignore')
class BasicConv2d(nn.Module):
def __init__(self, in_planes, out_planes, kernel_size, stride=1, padding=0, dilation=1):
super(BasicConv2d, self).__init__()
self.conv = nn.Conv2d(in_planes, out_planes,
kernel_size=kernel_size, stride=stride,
padding=padding, dilation=dilation, bias=False)
self.bn = nn.BatchNorm2d(out_planes)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
x = self.conv(x)
x = self.bn(x)
x = self.relu(x)
return x
class Block(nn.Sequential):
def __init__(self, input_num, num1, num2, dilation_rate, drop_out, bn_start=True, norm_layer=nn.BatchNorm2d):
super(Block, self).__init__()
if bn_start:
self.add_module('norm1', norm_layer(input_num)),
self.add_module('relu1', nn.ReLU(inplace=True)),
self.add_module('conv1', nn.Conv2d(in_channels=input_num, out_channels=num1, kernel_size=1)),
self.add_module('norm2', norm_layer(num1)),
self.add_module('relu2', nn.ReLU(inplace=True)),
self.add_module('conv2', nn.Conv2d(in_channels=num1, out_channels=num2, kernel_size=3,
dilation=dilation_rate, padding=dilation_rate)),
self.drop_rate = drop_out
def forward(self, _input):
feature = super(Block, self).forward(_input)
if self.drop_rate > 0:
feature = F.dropout2d(feature, p=self.drop_rate, training=self.training)
return feature
def Upsample(x, size, align_corners = False):
"""
Wrapper Around the Upsample Call
"""
return nn.functional.interpolate(x, size=size, mode='bilinear', align_corners=align_corners)
def last_zero_init(m):
if isinstance(m, nn.Sequential):
constant_init(m[-1], val=0)
else:
constant_init(m, val=0)
class ContextBlock(nn.Module):
def __init__(self,
inplanes,
ratio,
pooling_type='att',
fusion_types=('channel_mul', )):
super(ContextBlock, self).__init__()
assert pooling_type in ['avg', 'att']
assert isinstance(fusion_types, (list, tuple))
valid_fusion_types = ['channel_add', 'channel_mul']
assert all([f in valid_fusion_types for f in fusion_types])
assert len(fusion_types) > 0, 'at least one fusion should be used'
self.inplanes = inplanes
self.ratio = ratio
self.planes = int(inplanes * ratio)
self.pooling_type = pooling_type
self.fusion_types = fusion_types
if pooling_type == 'att':
self.conv_mask = nn.Conv2d(inplanes, 1, kernel_size=1)
self.softmax = nn.Softmax(dim=2)
else:
self.avg_pool = nn.AdaptiveAvgPool2d(1)
if 'channel_add' in fusion_types:
self.channel_add_conv = nn.Sequential(
nn.Conv2d(self.inplanes, self.planes, kernel_size=1),
nn.LayerNorm([self.planes, 1, 1]),
nn.ReLU(inplace=True), # yapf: disable
nn.Conv2d(self.planes, self.inplanes, kernel_size=1))
else:
self.channel_add_conv = None
if 'channel_mul' in fusion_types:
self.channel_mul_conv = nn.Sequential(
nn.Conv2d(self.inplanes, self.planes, kernel_size=1),
nn.LayerNorm([self.planes, 1, 1]),
nn.ReLU(inplace=True), # yapf: disable
nn.Conv2d(self.planes, self.inplanes, kernel_size=1))
else:
self.channel_mul_conv = None
self.reset_parameters()
def reset_parameters(self):
if self.pooling_type == 'att':
kaiming_init(self.conv_mask, mode='fan_in')
self.conv_mask.inited = True
if self.channel_add_conv is not None:
last_zero_init(self.channel_add_conv)
if self.channel_mul_conv is not None:
last_zero_init(self.channel_mul_conv)
def spatial_pool(self, x):
batch, channel, height, width = x.size()
if self.pooling_type == 'att':
input_x = x
# [N, C, H * W]
input_x = input_x.view(batch, channel, height * width)
# [N, 1, C, H * W]
input_x = input_x.unsqueeze(1)
# [N, 1, H, W]
context_mask = self.conv_mask(x)
# [N, 1, H * W]
context_mask = context_mask.view(batch, 1, height * width)
# [N, 1, H * W]
context_mask = self.softmax(context_mask)
# [N, 1, H * W, 1]
context_mask = context_mask.unsqueeze(-1)
# [N, 1, C, 1]
context = torch.matmul(input_x, context_mask)
# [N, C, 1, 1]
context = context.view(batch, channel, 1, 1)
else:
# [N, C, 1, 1]
context = self.avg_pool(x)
return context
def forward(self, x):
# [N, C, 1, 1]
context = self.spatial_pool(x)
out = x
if self.channel_mul_conv is not None:
# [N, C, 1, 1]
channel_mul_term = torch.sigmoid(self.channel_mul_conv(context))
out = out + out * channel_mul_term
if self.channel_add_conv is not None:
# [N, C, 1, 1]
channel_add_term = self.channel_add_conv(context)
out = out + channel_add_term
return out
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.fc1 = nn.Conv2d(in_planes, in_planes // 16, 1, bias=False)
self.relu1 = nn.ReLU()
self.fc2 = nn.Conv2d(in_planes // 16, in_planes, 1, bias=False)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
avg_out = self.fc2(self.relu1(self.fc1(self.avg_pool(x))))
max_out = self.fc2(self.relu1(self.fc1(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__()
assert kernel_size in (3, 7), 'kernel size must be 3 or 7'
padding = 3 if kernel_size == 7 else 1
self.conv1 = nn.Conv2d(2, 1, kernel_size, padding=padding, 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 ConvBranch(nn.Module):
def __init__(self, in_features, hidden_features = None, out_features = None):
super().__init__()
hidden_features = hidden_features or in_features
out_features = out_features or in_features
self.conv1 = nn.Sequential(
nn.Conv2d(in_features, hidden_features, 1, bias=False),
nn.BatchNorm2d(hidden_features),
nn.ReLU(inplace=True)
)
self.conv2 = nn.Sequential(
nn.Conv2d(hidden_features, hidden_features, 3, padding=1, groups=hidden_features, bias=False),
nn.BatchNorm2d(hidden_features),
nn.ReLU(inplace=True)
)
self.conv3 = nn.Sequential(
nn.Conv2d(hidden_features, hidden_features, 1, bias=False),
nn.BatchNorm2d(hidden_features),
nn.ReLU(inplace=True)
)
self.conv4 = nn.Sequential(
nn.Conv2d(hidden_features, hidden_features, 3, padding=1, groups=hidden_features, bias=False),
nn.BatchNorm2d(hidden_features),
nn.ReLU(inplace=True)
)
self.conv5 = nn.Sequential(
nn.Conv2d(hidden_features, hidden_features, 1, bias=False),
nn.BatchNorm2d(hidden_features),
nn.SiLU(inplace=True)
)
self.conv6 = nn.Sequential(
nn.Conv2d(hidden_features, hidden_features, 3, padding=1, groups=hidden_features, bias=False),
nn.BatchNorm2d(hidden_features),
nn.ReLU(inplace=True)
)
self.conv7 = nn.Sequential(
nn.Conv2d(hidden_features, out_features, 1, bias=False),
nn.ReLU(inplace=True)
)
self.ca = ChannelAttention(64)
self.sa = SpatialAttention()
self.sigmoid_spatial = nn.Sigmoid()
def forward(self, x):
res1 = x
res2 = x
x = self.conv1(x)
x = x + self.conv2(x)
x = self.conv3(x)
x = x + self.conv4(x)
x = self.conv5(x)
x = x + self.conv6(x)
x = self.conv7(x)
x_mask = self.sigmoid_spatial(x)
res1 = res1 * x_mask
return res2 + res1
class GLSA(nn.Module):
def __init__(self, input_dim=512, embed_dim=32, k_s=3):
super().__init__()
self.conv1_1 = BasicConv2d(embed_dim*2,embed_dim, 1)
self.conv1_1_1 = BasicConv2d(input_dim//2,embed_dim,1)
self.local_11conv = nn.Conv2d(input_dim//2,embed_dim,1)
self.global_11conv = nn.Conv2d(input_dim//2,embed_dim,1)
self.GlobelBlock = ContextBlock(inplanes= embed_dim, ratio=2)
self.local = ConvBranch(in_features = embed_dim, hidden_features = embed_dim, out_features = embed_dim)
def forward(self, x):
b, c, h, w = x.size()
x_0, x_1 = x.chunk(2,dim = 1)
# local block
local = self.local(self.local_11conv(x_0))
# Globel block
Globel = self.GlobelBlock(self.global_11conv(x_1))
# concat Globel + local
x = torch.cat([local,Globel], dim=1)
x = self.conv1_1(x)
return x
class SBA(nn.Module):
def __init__(self,input_dim = 64):
super().__init__()
self.input_dim = input_dim
self.d_in1 = BasicConv2d(input_dim//2, input_dim//2, 1)
self.d_in2 = BasicConv2d(input_dim//2, input_dim//2, 1)
self.conv = nn.Sequential(BasicConv2d(input_dim, input_dim, 3,1,1), nn.Conv2d(input_dim, 1, kernel_size=1, bias=False))
self.fc1 = nn.Conv2d(input_dim, input_dim//2, kernel_size=1, bias=False)
self.fc2 = nn.Conv2d(input_dim, input_dim//2, kernel_size=1, bias=False)
self.Sigmoid = nn.Sigmoid()
def forward(self, H_feature, L_feature):
L_feature = self.fc1(L_feature)
H_feature = self.fc2(H_feature)
g_L_feature = self.Sigmoid(L_feature)
g_H_feature = self.Sigmoid(H_feature)
L_feature = self.d_in1(L_feature)
H_feature = self.d_in2(H_feature)
L_feature = L_feature + L_feature * g_L_feature + (1 - g_L_feature) * Upsample(g_H_feature * H_feature, size= L_feature.size()[2:], align_corners=False)
H_feature = H_feature + H_feature * g_H_feature + (1 - g_H_feature) * Upsample(g_L_feature * L_feature, size= H_feature.size()[2:], align_corners=False)
H_feature = Upsample(H_feature, size = L_feature.size()[2:])
out = self.conv(torch.cat([H_feature,L_feature], dim=1))
return out
class DuAT(nn.Module):
def __init__(self, dim=32, dims= [64, 128, 320, 512]):
super(DuAT, self).__init__()
self.backbone = pvt_v2_b2() # [64, 128, 320, 512]
path = './pretrained_pth/pvt_v2_b2.pth'
save_model = torch.load(path)
model_dict = self.backbone.state_dict()
state_dict = {k: v for k, v in save_model.items() if k in model_dict.keys()}
model_dict.update(state_dict)
self.backbone.load_state_dict(model_dict)
c1_in_channels, c2_in_channels, c3_in_channels, c4_in_channels = dims[0], dims[1], dims[2], dims[3]
self.GLSA_c4 = GLSA(input_dim=c4_in_channels, embed_dim=dim)
self.GLSA_c3 = GLSA(input_dim=c3_in_channels, embed_dim=dim)
self.GLSA_c2 = GLSA(input_dim=c2_in_channels, embed_dim=dim)
self.L_feature = BasicConv2d(c1_in_channels,dim, 3,1,1)
self.SBA = SBA(input_dim = dim)
self.fuse = BasicConv2d(dim * 2, dim, 1)
self.fuse2 = nn.Sequential(BasicConv2d(dim*3, dim, 1,1),nn.Conv2d(dim, 1, kernel_size=1, bias=False))
def forward(self, x):
# backbone
pvt = self.backbone(x)
c1, c2, c3, c4 = pvt
n, _, h, w = c4.shape
_c4 = self.GLSA_c4(c4) # [1, 64, 11, 11]
_c4 = Upsample(_c4, c3.size()[2:])
_c3 = self.GLSA_c3(c3) # [1, 64, 22, 22]
_c2 = self.GLSA_c2(c2) # [1, 64, 44, 44]
output = self.fuse2(torch.cat([Upsample(_c4, c2.size()[2:]), Upsample(_c3, c2.size()[2:]), _c2], dim=1))
L_feature = self.L_feature(c1) # [1, 64, 88, 88]
H_feature = self.fuse(torch.cat([_c4, _c3], dim=1))
H_feature = Upsample(H_feature,c2.size()[2:])
output2 = self.SBA(H_feature,L_feature)
output = F.interpolate(output, scale_factor=8, mode='bilinear')
output2 = F.interpolate(output2, scale_factor=4, mode='bilinear')
return output, output2
if __name__ == '__main__':
model = DuAT().to('cuda')
from torchinfo import summary
# summary(model, (1, 3, 352, 352))
from thop import profile
import torch
input = torch.randn(1, 3, 352, 352).to('cuda')
macs, params = profile(model, inputs=(input,))
print('macs:', macs / 1000000000)
print('params:', params / 1000000)
# import time
## net = model()
# model.eval()
# time_count = 0.0
# for i in range(1000):
# image = torch.randn(1, 3, 352, 352).cuda()
# torch.cuda.synchronize()
# start_time = time.time()
# pred_semantic = model(image)
# torch.cuda.synchronize()
# print(time.time() - start_time)
# if i >= 100 and i <= 900:
# time_count = time_count + time.time() - start_time
# print("FPS:", 800 / time_count)