-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmfnet.py
471 lines (390 loc) · 17.4 KB
/
mfnet.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
import torch
from torch import nn
from torch.nn import functional as F
from torch.nn import BatchNorm2d as BatchNorm2d
class _SelfAttentionBlock(nn.Module):
'''
The basic implementation for self-attention block/non-local block
Input:
N X C X H X W
Parameters:
in_channels : the dimension of the input feature map
key_channels : the dimension after the key/query transform
value_channels : the dimension after the value transform
scale : choose the scale to downsample the input feature maps (save memory cost)
Return:
N X C X H X W
position-aware context features.(w/o concate or add with the input)
'''
def __init__(self, in_channels, key_channels, value_channels, out_channels=None, scale=1):
super(_SelfAttentionBlock, self).__init__()
self.scale = 2
# self.scale = scale
self.in_channels = in_channels
self.out_channels = out_channels
self.key_channels = key_channels
self.value_channels = value_channels
if out_channels == None:
self.out_channels = in_channels
self.pool = nn.MaxPool2d(kernel_size=(scale, scale))
# self.pool = nn.MaxPool2d(scale, scale)
self.f_key = nn.Sequential(
nn.Conv2d(in_channels=self.in_channels, out_channels=self.key_channels,
kernel_size=1, stride=1, padding=0),
BatchNorm2d(self.key_channels),
)
# self.f_query= nn.Sequential(
# nn.Conv2d(in_channels=self.in_channels, out_channels=self.key_channels,
# kernel_size=1, stride=1, padding=0),
# BatchNorm2d(self.key_channels),
# )
self.f_query = self.f_key
self.f_value = nn.Conv2d(in_channels=self.in_channels, out_channels=self.value_channels,
kernel_size=1, stride=1, padding=0)
self.W = nn.Conv2d(in_channels=self.value_channels, out_channels=self.out_channels,
kernel_size=1, stride=1, padding=0)
nn.init.constant_(self.W.weight, 0)
nn.init.constant_(self.W.bias, 0)
def forward(self, x):
batch_size, h, w = x.size(0), x.size(2), x.size(3)
if self.scale > 1:
x = self.pool(x)
value = self.f_value(x).view(batch_size, self.value_channels, -1)
value = value.permute(0, 2, 1)
query = self.f_query(x).view(batch_size, self.key_channels, -1)
query = query.permute(0, 2, 1)
key = self.f_key(x).view(batch_size, self.key_channels, -1)
sim_map = torch.matmul(query, key)
sim_map = (self.key_channels ** -.5) * sim_map
sim_map = F.softmax(sim_map, dim=-1)
context = torch.matmul(sim_map, value)
context = context.permute(0, 2, 1).contiguous()
context = context.view(batch_size, self.value_channels, *x.size()[2:])
context = self.W(context)
if self.scale > 1:
context = F.interpolate(input=context, size=(h, w), mode='bilinear', align_corners=True)
return context
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 SelfAttentionBlock2D(_SelfAttentionBlock):
def __init__(self, in_channels, key_channels, value_channels, out_channels=None, scale=1):
super(SelfAttentionBlock2D, self).__init__(in_channels,
key_channels,
value_channels,
out_channels,
scale)
class BaseOC_Module(nn.Module):
"""
Implementation of the BaseOC module
Parameters:
in_features / out_features: the channels of the input / output feature maps.
dropout: we choose 0.05 as the default value.
size: you can apply multiple sizes. Here we only use one size.
Return:
features fused with Object context information.
"""
def __init__(self, in_channels, out_channels, key_channels, value_channels, dropout, sizes=([1])):
super(BaseOC_Module, self).__init__()
self.stages = []
self.stages = nn.ModuleList(
[self._make_stage(in_channels, out_channels, key_channels, value_channels, size) for size in sizes])
self.conv_bn_dropout = nn.Sequential(
nn.Conv2d(2 * in_channels, out_channels, kernel_size=1, padding=0),
BatchNorm2d(out_channels),
nn.Dropout2d(dropout)
)
def _make_stage(self, in_channels, output_channels, key_channels, value_channels, size):
return SelfAttentionBlock2D(in_channels,
key_channels,
value_channels,
output_channels,
size)
def forward(self, feats):
priors = [stage(feats) for stage in self.stages]
context = priors[0]
for i in range(1, len(priors)):
context += priors[i]
output = self.conv_bn_dropout(torch.cat([context, feats], 1))
return output
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 BaseOC_Context_Module(nn.Module):
"""
Output only the context features.
Parameters:
in_features / out_features: the channels of the input / output feature maps.
dropout: specify the dropout ratio
fusion: We provide two different fusion method, "concat" or "add"
size: we find that directly learn the attention weights on even 1/8 feature maps is hard.
Return:
features after "concat" or "add"
"""
def __init__(self, in_channels, out_channels, key_channels, value_channels, dropout, sizes=([1])):
super(BaseOC_Context_Module, self).__init__()
self.stages = []
self.stages = nn.ModuleList(
[self._make_stage(in_channels, out_channels, key_channels, value_channels, size) for size in sizes])
self.conv_bn_dropout = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=1, padding=0),
BatchNorm2d(out_channels, activation='none'),
)
def _make_stage(self, in_channels, output_channels, key_channels, value_channels, size):
return SelfAttentionBlock2D(in_channels,
key_channels,
value_channels,
output_channels,
size)
def forward(self, feats):
priors = [stage(feats) for stage in self.stages]
context = priors[0]
for i in range(1, len(priors)):
context += priors[i]
output = self.conv_bn_dropout(context)
return output
import torch
import torch.nn as nn
import torch.nn.functional as F
import time
class Conv(nn.Module):
def __init__(self, nIn, nOut, kSize, stride, padding, dilation=(1, 1), groups=1, bn_acti=False, bias=False):
super().__init__()
self.bn_acti = bn_acti
self.conv = nn.Conv2d(nIn, nOut, kernel_size=kSize,
stride=stride, padding=padding,
dilation=dilation, groups=groups, bias=bias)
if self.bn_acti:
if groups != 1:
self.bn_prelu = BN(nOut)
else:
self.bn_prelu = BNPReLU(nOut)
def forward(self, input):
output = self.conv(input)
if self.bn_acti:
output = self.bn_prelu(output)
return output
class BNPReLU(nn.Module):
def __init__(self, nIn):
super().__init__()
self.bn = nn.BatchNorm2d(nIn, eps=1e-3)
self.acti = nn.PReLU(nIn)
def forward(self, input):
output = self.bn(input)
output = self.acti(output)
return output
class BN(nn.Module):
def __init__(self, nIn):
super().__init__()
self.bn = nn.BatchNorm2d(nIn, eps=1e-3)
def forward(self, input):
output = self.bn(input)
return output
class MFModule(nn.Module):
def __init__(self, nIn, d=1, kSize=3, dkSize=3):
super().__init__()
self.bn_relu_1 = BNPReLU(nIn)
self.conv3x3 = Conv(nIn, nIn // 2, kSize, 1, padding=1, bn_acti=True)
self.dconv3x1 = Conv(nIn // 2, nIn // 2, (dkSize, 1), 1,
padding=(1, 0), groups=nIn // 2, bn_acti=True)
# self.dconv1x3 = Conv(nIn // 2, nIn // 2, (1, dkSize), 1,
# padding=(0, 1), groups=nIn // 2, bn_acti=True)
self.ddconv3x1 = Conv(nIn // 2, nIn // 2, (dkSize, 1), 1,
padding=(1 * d, 0), dilation=(d, 1), groups=nIn // 2, bn_acti=True)
self.ddconv1x3 = Conv(nIn // 2, nIn // 2, (1, dkSize), 1,
padding=(0, 1 * d), dilation=(1, d), groups=nIn // 2, bn_acti=True)
self.bn_relu_2 = BNPReLU(nIn // 2)
self.conv1x1 = Conv(nIn // 2, nIn, 3, 1, padding=1, bn_acti=False)
def forward(self, input):
output = self.bn_relu_1(input)
output = self.conv3x3(output)
br1 = self.dconv3x1(output)
# br1 = self.dconv1x3(br1)
br2 = self.ddconv3x1(output)
br2 = self.ddconv1x3(br2)
output = br1 + br2
output = self.bn_relu_2(output)
output = self.conv1x1(output)
return output + input
class DownSamplingBlock(nn.Module):
def __init__(self, nIn, nOut):
super().__init__()
self.nIn = nIn
self.nOut = nOut
if self.nIn < self.nOut:
nConv = nOut - nIn
else:
nConv = nOut
self.conv3x3 = Conv(nIn, nConv, kSize=3, stride=2, padding=1)
self.max_pool = nn.MaxPool2d(2, stride=2)
self.bn_prelu = BNPReLU(nOut)
def forward(self, input):
output = self.conv3x3(input)
if self.nIn < self.nOut:
max_pool = self.max_pool(input)
output = torch.cat([output, max_pool], 1)
output = self.bn_prelu(output)
return output
class InputInjection(nn.Module):
def __init__(self, ratio):
super().__init__()
self.pool = nn.ModuleList()
for i in range(0, ratio):
self.pool.append(nn.AvgPool2d(3, stride=2, padding=1))
def forward(self, input):
for pool in self.pool:
input = pool(input)
return input
class FeatureFusionModule(nn.Module):
def __init__(self, in_chan, out_chan, *args, **kwargs):
super(FeatureFusionModule, self).__init__()
self.convblk = Conv(in_chan, out_chan, kSize=1, stride=1, padding=0, bn_acti=True)
self.conv1 = nn.Conv2d(out_chan,
out_chan//4,
kernel_size = 1,
stride = 1,
padding = 0,
bias = False)
self.conv2 = nn.Conv2d(out_chan//4,
out_chan,
kernel_size = 1,
stride = 1,
padding = 0,
bias = False)
self.relu = nn.ReLU(inplace=True)
self.sigmoid = nn.Sigmoid()
self.init_weight()
def forward(self, fsp, fcp):
fcat = torch.cat([fsp, fcp], dim=1)
feat = self.convblk(fcat)
atten = F.avg_pool2d(feat, feat.size()[2:])
atten = self.conv1(atten)
atten = self.relu(atten)
atten = self.conv2(atten)
atten = self.sigmoid(atten)
feat_atten = torch.mul(feat, atten)
feat_out = feat_atten + feat
return feat_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)
class MFNet(nn.Module):
def __init__(self, classes=19, block_1=2, block_2=5):
super().__init__()
self.init_conv = nn.Sequential(
Conv(3, 32, 3, 2, padding=1, bn_acti=True),
Conv(32, 32, 3, 1, padding=1, bn_acti=True),
Conv(32, 32, 3, 1, padding=1, bn_acti=True),
)
self.down_1 = InputInjection(1) # down-sample the image 1 times
self.down_2 = InputInjection(2) # down-sample the image 2 times
self.down_3 = InputInjection(4) # down-sample the image 3 times
self.bn_prelu_1 = BNPReLU(32 + 3)
# MF Block 1
self.downsample_1 = DownSamplingBlock(32 + 3, 64)
self.MF_Block_1 = nn.Sequential()
for i in range(0, block_1):
self.MF_Block_1.add_module("MF_Module_1_" + str(i), MFModule(64, d=2))
self.bn_prelu_2 = BNPReLU(128 + 3)
# MF Block 2
dilation_block_2 = [2, 4, 4, 16, 16]
self.downsample_2 = DownSamplingBlock(128 + 3, 128)
self.downsample_3 = DownSamplingBlock(128, 128)
self.MF_Block_2 = nn.Sequential()
for i in range(0, block_2):
self.MF_Block_2.add_module("MF_Module_2_" + str(i),
MFModule(128, d=dilation_block_2[i]))
self.bn_prelu_3 = BNPReLU(256 + 3)
self.classifier = nn.Sequential(Conv(259, 64, 1, 1, padding=0),
nn.BatchNorm2d(64),
nn.PReLU(),)
self.ocm = nn.Sequential(
nn.Conv2d(128, 128, kernel_size=1, stride=1, padding=0),
nn.BatchNorm2d(128),
nn.PReLU(),
BaseOC_Module(128, 128, 64, 64, 0.05)
)
self.convocm = nn.Sequential(Conv(128, 64, kSize=1, stride=1, padding=0),
nn.BatchNorm2d(64),
nn.PReLU())
self.convfuse = nn.Sequential(Conv(128, 64, kSize=3, stride=1, padding=1),
nn.BatchNorm2d(64),
nn.PReLU())
self.SP = nn.Sequential(
nn.Conv2d(64, 32, kernel_size=1, stride=1, padding=0),
nn.BatchNorm2d(32),
nn.PReLU())
self.ffm = FeatureFusionModule(96, 64)
self.classifier2 = nn.Sequential(Conv(64, classes, 1, 1, padding=0),
nn.BatchNorm2d(classes),
nn.PReLU(),
)
def forward(self, input):
output0 = self.init_conv(input)
down_1 = self.down_1(input)
down_2 = self.down_2(input)
down_3 = self.down_3(input)
output0_cat = self.bn_prelu_1(torch.cat([output0, down_1], 1))
# MF Block 1
output1_0 = self.downsample_1(output0_cat)
output1 = self.MF_Block_1(output1_0)
output1_cat = self.bn_prelu_2(torch.cat([output1, output1_0, down_2], 1))
# MF Block 2
output2_0 = self.downsample_2(output1_cat)
output2_0 = self.downsample_3(output2_0)
output2 = self.MF_Block_2(output2_0)
output2_cat = self.bn_prelu_3(torch.cat([output2, output2_0, down_3], 1))
out0 = self.classifier(output2_cat)
# out = torch.cat([out, output1], 1)
outocm = self.ocm(output2_0)
outocm = self.convocm(outocm)
out1 = self.convfuse(torch.cat([outocm, out0], 1))
out1 = F.interpolate(out1, scale_factor=4, mode='bilinear', align_corners=False)
out2 = self.SP(output1)
out = self.ffm(out2, out1)
out = self.classifier2(out)
out = F.interpolate(out, input.size()[2:], mode='bilinear', align_corners=False)
return out
# if __name__ == '__main__':
# t_start = time.time()
# iteration = 5
# for _ in range(iteration):
# img = torch.randn(2, 3, 256, 512)
# model = MFNet(11)
# outputs = model(img)
# # print(outputs.size())
# elapsed_time = time.time() - t_start
#
# speed_time = elapsed_time / iteration * 1000
# fps = iteration / elapsed_time
#
# print('Elapsed Time: [%.2f s / %d iter]' % (elapsed_time, iteration))
# print('Speed Time: %.2f ms / iter FPS: %.2f' % (speed_time, fps))
#
# def netParams(model):
# total_paramters = 0
# for parameter in model.parameters():
# i = len(parameter.size())
# p = 1
# for j in range(i):
# p *= parameter.size(j)
# total_paramters += p
# return total_paramters
#
# print(netParams(model))