-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMLRefineNet.py
241 lines (200 loc) · 9.68 KB
/
MLRefineNet.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
"""
Real-Time Joint Semantic Segmentation and Depth Estimation Using Asymmetric Annotations for non-commercial purposes
Copyright (c) 2019, Vladimir Nekrasov
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import torch
import torch.nn as nn
import math
def conv3x3(in_planes, out_planes, stride=1, bias=False, dilation=1, groups=1):
"3x3 convolution"
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
padding=dilation, dilation=dilation, bias=bias, groups=groups)
def conv1x1(in_planes, out_planes, stride=1, bias=False, groups=1):
"1x1 convolution"
return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride,
padding=0, bias=bias, groups=groups)
def batchnorm(in_planes):
"batch norm 2d"
return nn.BatchNorm2d(in_planes, affine=True, eps=1e-5, momentum=0.1)
def convbnrelu(in_planes, out_planes, kernel_size, stride=1, groups=1, act=True):
"conv-batchnorm-relu"
if act:
return nn.Sequential(nn.Conv2d(in_planes, out_planes, kernel_size, stride=stride, padding=int(kernel_size / 2.), groups=groups, bias=False),
batchnorm(out_planes),
nn.ReLU6(inplace=True))
else:
return nn.Sequential(nn.Conv2d(in_planes, out_planes, kernel_size, stride=stride, padding=int(kernel_size / 2.), groups=groups, bias=False),
batchnorm(out_planes))
class CRPBlock(nn.Module):
"""CRP definition"""
def __init__(self, in_planes, out_planes, n_stages, groups=False):
super(CRPBlock, self).__init__()
for i in range(n_stages):
setattr(self, '{}_{}'.format(i + 1, 'outvar_dimred'),
conv1x1(in_planes if (i == 0) else out_planes,
out_planes, stride=1,
bias=False, groups=in_planes if groups else 1))
self.stride = 1
self.n_stages = n_stages
self.maxpool = nn.MaxPool2d(kernel_size=5, stride=1, padding=2)
def forward(self, x):
top = x
for i in range(self.n_stages):
top = self.maxpool(top)
top = getattr(self, '{}_{}'.format(i + 1, 'outvar_dimred'))(top)
x = top + x
return x
class InvertedResidualBlock(nn.Module):
"""Inverted Residual Block from https://arxiv.org/abs/1801.04381"""
def __init__(self, in_planes, out_planes, expansion_factor, stride=1):
super(InvertedResidualBlock, self).__init__()
intermed_planes = in_planes * expansion_factor
self.residual = (in_planes == out_planes) and (stride == 1)
self.output = nn.Sequential(convbnrelu(in_planes, intermed_planes, 1),
convbnrelu(intermed_planes, intermed_planes, 3, stride=stride, groups=intermed_planes),
convbnrelu(intermed_planes, out_planes, 1, act=False))
def forward(self, x):
residual = x
out = self.output(x)
if self.residual:
return (out + residual)
else:
return out
class Net(nn.Module):
"""Net Definition"""
# Increased number of repeats for each block by 1
mobilenet_config = [[1, 16, 2, 1], # expansion rate, output channels, number of repeats, stride
[6, 24, 3, 2],
[6, 32, 4, 2],
[6, 64, 5, 2],
[6, 96, 4, 1],
[6, 160, 4, 2],
[6, 320, 2, 1],
]
in_planes = 32 # number of input channels
num_layers = len(mobilenet_config)
def __init__(self, num_classes):
super(Net, self).__init__()
self.layer1 = convbnrelu(3, self.in_planes, kernel_size=3, stride=2)
c_layer = 2
for t,c,n,s in (self.mobilenet_config):
layers = []
for idx in range(n):
layers.append(InvertedResidualBlock(self.in_planes, c, expansion_factor=t, stride=s if idx == 0 else 1))
self.in_planes = c
setattr(self, 'layer{}'.format(c_layer), nn.Sequential(*layers))
c_layer += 1
## Light-Weight RefineNet ##
self.conv8 = conv1x1(320, 256, bias=False)
self.conv7 = conv1x1(160, 256, bias=False)
self.conv6 = conv1x1(96, 256, bias=False)
self.conv5 = conv1x1(64, 256, bias=False)
self.conv4 = conv1x1(32, 256, bias=False)
self.conv3 = conv1x1(24, 256, bias=False)
self.crp4 = self._make_crp(256, 256, 4, groups=False)
self.crp3 = self._make_crp(256, 256, 4, groups=False)
self.crp2 = self._make_crp(256, 256, 4, groups=False)
self.crp1 = self._make_crp(256, 256, 4, groups=True)
# Additional convolutional layers to refine features
self.conv_adapt4 = conv1x1(256, 256, bias=False)
self.conv_adapt3 = conv1x1(256, 256, bias=False)
self.conv_adapt2 = conv1x1(256, 256, bias=False)
self.pre_depth = conv1x1(256, 256, groups=256, bias=False)
self.depth = conv3x3(256, 1, bias=True)
self.pre_segm = conv1x1(256, 256, groups=256, bias=False)
self.segm = conv3x3(256, num_classes, bias=True)
# Add layers to upsample to full resolution (128x416)
self.crp0 = self._make_crp(256, 256, 4, groups=True)
self.conv0 = conv1x1(256, 256, bias=False)
self.up1 = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=False)
self.up2 = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=False)
self.up3 = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=False)
self.final_conv_segm = conv3x3(256, num_classes, bias=True)
self.final_conv_depth = conv3x3(256, 1, bias=True)
self.relu = nn.ReLU6(inplace=True)
self._initialize_weights()
def forward(self, x):
x = self.layer1(x)
x = self.layer2(x) # x / 2
l3 = self.layer3(x) # 24, x / 4
l4 = self.layer4(l3) # 32, x / 8
l5 = self.layer5(l4) # 64, x / 16
l6 = self.layer6(l5) # 96, x / 16
l7 = self.layer7(l6) # 160, x / 32
l8 = self.layer8(l7) # 320, x / 32
l8 = self.conv8(l8)
l7 = self.conv7(l7)
l7 = self.relu(l8 + l7)
l7 = self.crp4(l7)
l7 = self.conv_adapt4(l7)
l7 = nn.Upsample(size=l6.size()[2:], mode='bilinear', align_corners=False)(l7)
l6 = self.conv6(l6)
l5 = self.conv5(l5)
l5 = self.relu(l5 + l6 + l7)
l5 = self.crp3(l5)
l5 = self.conv_adapt3(l5)
l5 = nn.Upsample(size=l4.size()[2:], mode='bilinear', align_corners=False)(l5)
disp4 = self.final_conv_depth(l5)
l4 = self.conv4(l4)
l4 = self.relu(l5 + l4)
l4 = self.crp2(l4)
l4 = self.conv_adapt2(l4)
l4 = nn.Upsample(size=l3.size()[2:], mode='bilinear', align_corners=False)(l4)
disp3 = self.final_conv_depth(l4)
l3 = self.conv3(l3)
l3 = self.relu(l3 + l4)
l3 = self.crp1(l3)
# Additional CRP block and convolution layers
l3 = self.crp0(l3)
l3 = self.conv0(l3)
l3 = self.up1(l3) # Upsample from (32, 104) to (64, 208)
disp2 = self.final_conv_depth(l3)
l3 = self.up2(l3) # Upsample from (64, 208) to (128, 416)
# l3 = self.up3(l3) # Upsample from (128, 416) to (128, 416) - Redundant but for more refinement
out_segm = self.final_conv_segm(l3)
disp1 = self.final_conv_depth(l3)
if self.training:
return disp1, disp2, disp3, disp4, out_segm
else:
return disp1, out_segm
def _initialize_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
m.weight.data.normal_(0, 0.01)
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def _make_crp(self, in_planes, out_planes, stages, groups=False):
layers = [CRPBlock(in_planes, out_planes,stages, groups=groups)]
return nn.Sequential(*layers)
def net(num_classes):
"""Constructs the network.
Args:
num_classes (int): the number of classes for the segmentation head to output.
"""
model = Net(num_classes)
return model
if __name__ == '__main__':
from torchinfo import summary
refine_model = net(19)
print(summary(refine_model, input_size=(1, 3, 128, 416), mode='train', col_names=("input_size", "output_size", "num_params")))