-
Notifications
You must be signed in to change notification settings - Fork 244
/
Copy pathGLSA.py
198 lines (171 loc) · 7.13 KB
/
GLSA.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
import torch
import torch.nn as nn
#论文:DuAT: Dual-Aggregation Transformer Network for Medical Image Segmentation(PRCV)
#论文地址:https://arxiv.org/pdf/2212.11677
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 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
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 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.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
#Global-to-Local Spatial Aggregation (GLSA)
class GLSA(nn.Module):
def __init__(self, input_dim, embed_dim):
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):
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
if __name__ == '__main__':
input = torch.randn(1, 32, 64, 64) #B C H W
block = GLSA(input_dim=32, embed_dim=32)
output = block(input)
print(input.size())
print(output.size())