-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathDABNet.py
189 lines (136 loc) · 5.94 KB
/
DABNet.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
######################################################################################
#DABNet: Depth-wise Asymmetric Bottleneck for Real-time Semantic Segmentation
#Paper-Link: https://arxiv.org/pdf/1907.11357.pdf
######################################################################################
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchsummary import summary
__all__ = ["DABNet"]
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:
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 DABModule(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, 1, 1, padding=0, 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 DABNet(nn.Module):
def __init__(self, classes=19, block_1=3, block_2=6):
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(3) # down-sample the image 3 times
self.bn_prelu_1 = BNPReLU(32 + 3)
# DAB Block 1
self.downsample_1 = DownSamplingBlock(32 + 3, 64)
self.DAB_Block_1 = nn.Sequential()
for i in range(0, block_1):
self.DAB_Block_1.add_module("DAB_Module_1_" + str(i), DABModule(64, d=2))
self.bn_prelu_2 = BNPReLU(128 + 3)
# DAB Block 2
dilation_block_2 = [4, 4, 8, 8, 16, 16]
self.downsample_2 = DownSamplingBlock(128 + 3, 128)
self.DAB_Block_2 = nn.Sequential()
for i in range(0, block_2):
self.DAB_Block_2.add_module("DAB_Module_2_" + str(i),
DABModule(128, d=dilation_block_2[i]))
self.bn_prelu_3 = BNPReLU(256 + 3)
self.classifier = nn.Sequential(Conv(259, classes, 1, 1, padding=0))
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))
# DAB Block 1
output1_0 = self.downsample_1(output0_cat)
output1 = self.DAB_Block_1(output1_0)
output1_cat = self.bn_prelu_2(torch.cat([output1, output1_0, down_2], 1))
# DAB Block 2
output2_0 = self.downsample_2(output1_cat)
output2 = self.DAB_Block_2(output2_0)
output2_cat = self.bn_prelu_3(torch.cat([output2, output2_0, down_3], 1))
out = self.classifier(output2_cat)
out = F.interpolate(out, input.size()[2:], mode='bilinear', align_corners=False)
return out
"""print layers and params of network"""
if __name__ == '__main__':
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = DABNet(classes=19).to(device)
summary(model,(3,512,1024))