-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathAttaNet_light.py
251 lines (206 loc) · 9.2 KB
/
AttaNet_light.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
#!/usr/bin/python
# -*- encoding: utf-8 -*-
import torch
import torch.nn as nn
import torch.nn.functional as F
from light import Backbone
from torch.nn import BatchNorm2d
class ConvBNReLU(nn.Module):
def __init__(self, in_chan, out_chan, ks=3, stride=1, padding=1, *args, **kwargs):
super(ConvBNReLU, self).__init__()
self.conv = nn.Conv2d(in_chan,
out_chan,
kernel_size=ks,
stride=stride,
padding=padding,
bias=False)
self.bn = BatchNorm2d(out_chan)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
x = self.conv(x)
x = self.bn(x)
x = self.relu(x)
return x
def init_weight(self):
for ly in self.children():
if isinstance(ly, nn.Conv2d):
nn.init.kaiming_normal_(ly.weight, a=1)
if not ly.bias is None: nn.init.constant_(ly.bias, 0)
class AttaNetOutput(nn.Module):
def __init__(self, in_chan, mid_chan, n_classes, *args, **kwargs):
super(AttaNetOutput, self).__init__()
self.conv = ConvBNReLU(in_chan, mid_chan, ks=3, stride=1, padding=1)
self.conv_out = nn.Conv2d(mid_chan, n_classes, kernel_size=1, bias=False)
self.init_weight()
def forward(self, x):
x = self.conv(x)
x = self.conv_out(x)
return x
def init_weight(self):
for ly in self.children():
if isinstance(ly, nn.Conv2d):
nn.init.kaiming_normal_(ly.weight, a=1)
if not ly.bias is None: nn.init.constant_(ly.bias, 0)
def get_params(self):
wd_params, nowd_params = [], []
for name, module in self.named_modules():
if isinstance(module, (nn.Linear, nn.Conv2d)):
wd_params.append(module.weight)
if not module.bias is None:
nowd_params.append(module.bias)
elif isinstance(module, BatchNorm2d):
nowd_params += list(module.parameters())
return wd_params, nowd_params
class StripAttentionModule(nn.Module):
def __init__(self, in_chan, out_chan, *args, **kwargs):
super(StripAttentionModule, self).__init__()
self.conv1 = ConvBNReLU(in_chan, out_chan, ks=1, stride=1, padding=0)
self.conv2 = ConvBNReLU(in_chan, out_chan, ks=1, stride=1, padding=0)
self.conv3 = ConvBNReLU(in_chan, out_chan, ks=1, stride=1, padding=0)
self.softmax = nn.Softmax(dim=1)
self.init_weight()
def forward(self, x):
q = self.conv1(x)
batchsize, c_middle, h, w = q.size()
q = F.avg_pool2d(q, [h, 1])
q = q.view(batchsize, c_middle, -1).permute(0, 2, 1)
k = self.conv2(x)
k = k.view(batchsize, c_middle, -1)
attention_map = torch.bmm(q, k)
attention_map = self.softmax(attention_map)
v = self.conv3(x)
c_out = v.size()[1]
v = F.avg_pool2d(v, [h, 1])
v = v.view(batchsize, c_out, -1)
augmented_feature_map = torch.bmm(v, attention_map)
augmented_feature_map = augmented_feature_map.view(batchsize, c_out, h, w)
out = x + augmented_feature_map
return out
def init_weight(self):
for ly in self.children():
if isinstance(ly, nn.Conv2d):
nn.init.kaiming_normal_(ly.weight, a=1)
if not ly.bias is None: nn.init.constant_(ly.bias, 0)
def get_params(self):
wd_params, nowd_params = [], []
for name, module in self.named_modules():
if isinstance(module, (nn.Linear, nn.Conv2d)):
wd_params.append(module.weight)
if not module.bias is None:
nowd_params.append(module.bias)
elif isinstance(module, BatchNorm2d):
nowd_params += list(module.parameters())
return wd_params, nowd_params
class AttentionFusionModule(nn.Module):
def __init__(self, in_chan, out_chan, *args, **kwargs):
super(AttentionFusionModule, self).__init__()
self.conv = ConvBNReLU(in_chan, out_chan, ks=3, stride=1, padding=1)
self.conv_atten = nn.Conv2d(out_chan, out_chan, kernel_size= 1, bias=False)
self.bn_atten = BatchNorm2d(out_chan)
self.sigmoid_atten = nn.Sigmoid()
self.init_weight()
def forward(self, feat16, feat32):
feat32_up = F.interpolate(feat32, feat16.size()[2:], mode='nearest')
fcat = torch.cat([feat16, feat32_up], dim=1)
feat = self.conv(fcat)
atten = F.avg_pool2d(feat, feat.size()[2:])
atten = self.conv_atten(atten)
atten = self.bn_atten(atten)
atten = self.sigmoid_atten(atten)
return atten
def init_weight(self):
for ly in self.children():
if isinstance(ly, nn.Conv2d):
nn.init.kaiming_normal_(ly.weight, a=1)
if not ly.bias is None: nn.init.constant_(ly.bias, 0)
def get_params(self):
wd_params, nowd_params = [], []
for name, module in self.named_modules():
if isinstance(module, (nn.Linear, nn.Conv2d)):
wd_params.append(module.weight)
if not module.bias is None:
nowd_params.append(module.bias)
elif isinstance(module, BatchNorm2d):
nowd_params += list(module.parameters())
return wd_params, nowd_params
class AttaNetHead(nn.Module):
def __init__(self, *args, **kwargs):
super(AttaNetHead, self).__init__()
self.backbone = Backbone()
self.afm = AttentionFusionModule(128, 64)
self.conv_head8 = ConvBNReLU(64, 64, ks=3, stride=1, padding=1)
self.conv_head16 = ConvBNReLU(128, 64, ks=3, stride=1, padding=1)
self.conv_head81 = ConvBNReLU(64, 64, ks=3, stride=1, padding=1)
self.conv_head161 = ConvBNReLU(64, 64, ks=3, stride=1, padding=1)
self.conv_head1 = ConvBNReLU(64, 64, ks=3, stride=1, padding=1)
self.sam = StripAttentionModule(64, 64)
self.conv_head2 = ConvBNReLU(64, 64, ks=3, stride=1, padding=1)
self.init_weight()
def forward(self, x):
feat8, feat16, feat = self.backbone(x)
h8, w8 = feat8.size()[2:]
# Attention Fusion Module
feat16 = self.conv_head16(feat16)
feat8 = self.conv_head8(feat8)
atten = self.afm(feat8, feat16)
feat16 = self.conv_head161(feat16)
feat8 = self.conv_head81(feat8)
feat16 = torch.mul(feat16, atten)
feat16_up = F.interpolate(feat16, (h8, w8), mode='nearest')
feat8 = torch.mul(feat8, (1 - atten))
feat8_sum = feat16_up + feat8
# feature smoothness
feat8_sum = self.conv_head1(feat8_sum)
# Strip Attention Module
feat8_sum = self.sam(feat8_sum)
feat8_sum = self.conv_head2(feat8_sum)
return feat8_sum, feat16_up, feat
def init_weight(self):
for ly in self.children():
if isinstance(ly, nn.Conv2d):
nn.init.kaiming_normal_(ly.weight, a=1)
if not ly.bias is None: nn.init.constant_(ly.bias, 0)
def get_params(self):
wd_params, nowd_params = [], []
for name, module in self.named_modules():
if isinstance(module, (nn.Linear, nn.Conv2d)):
wd_params.append(module.weight)
if not module.bias is None:
nowd_params.append(module.bias)
elif isinstance(module, BatchNorm2d):
nowd_params += list(module.parameters())
return wd_params, nowd_params
class AttaNet(nn.Module):
def __init__(self, n_classes, *args, **kwargs):
super(AttaNet, self).__init__()
self.head = AttaNetHead()
self.conv_out = AttaNetOutput(64, 64, n_classes)
self.conv_out1 = AttaNetOutput(64, 64, n_classes)
self.conv_out2 = AttaNetOutput(128, 64, n_classes)
self.init_weight()
def forward(self, x):
h, w = x.size()[2:]
out, auxout1, auxout2 = self.head(x)
feat_out = self.conv_out(out)
feat_aux1 = self.conv_out1(auxout1)
feat_aux2 = self.conv_out2(auxout2)
feat_out = F.interpolate(feat_out, (h, w), mode='bilinear', align_corners=True)
feat_aux1 = F.interpolate(feat_aux1, (h, w), mode='bilinear', align_corners=True)
feat_aux2 = F.interpolate(feat_aux2, (h, w), mode='bilinear', align_corners=True)
return feat_out, feat_aux1, feat_aux2
def init_weight(self):
for ly in self.children():
if isinstance(ly, nn.Conv2d):
nn.init.kaiming_normal_(ly.weight, a=1)
if not ly.bias is None: nn.init.constant_(ly.bias, 0)
def get_params(self):
wd_params, nowd_params, lr_mul_wd_params, lr_mul_nowd_params = [], [], [], []
for name, child in self.named_children():
child_wd_params, child_nowd_params = child.get_params()
if isinstance(child, AttaNetOutput):
lr_mul_wd_params += child_wd_params
lr_mul_nowd_params += child_nowd_params
else:
wd_params += child_wd_params
nowd_params += child_nowd_params
return wd_params, nowd_params, lr_mul_wd_params, lr_mul_nowd_params