forked from NVIDIA/semantic-segmentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
382 lines (318 loc) · 13 KB
/
utils.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
"""
Copyright 2020 Nvidia Corporation
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. 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.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
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.
"""
from collections import OrderedDict
import torch
import torch.nn.functional as F
from torch import nn
from network.mynn import Norm2d, Upsample
from network.xception import xception71
from network.wider_resnet import wrn38
from network.SEresnext import se_resnext50_32x4d, se_resnext101_32x4d
from network.Resnet import resnet50, resnet101
import network.hrnetv2 as hrnetv2
from runx.logx import logx
from config import cfg
class get_resnet(nn.Module):
def __init__(self, trunk_name, output_stride=8):
super(get_resnet, self).__init__()
if trunk_name == 'seresnext-50':
resnet = se_resnext50_32x4d()
elif trunk_name == 'seresnext-101':
resnet = se_resnext101_32x4d()
elif trunk_name == 'resnet-50':
resnet = resnet50()
resnet.layer0 = nn.Sequential(resnet.conv1, resnet.bn1,
resnet.relu, resnet.maxpool)
elif trunk_name == 'resnet-101':
resnet = resnet101()
resnet.layer0 = nn.Sequential(resnet.conv1, resnet.bn1,
resnet.relu, resnet.maxpool)
else:
raise ValueError("Not a valid network arch")
self.layer0 = resnet.layer0
self.layer1, self.layer2, self.layer3, self.layer4 = \
resnet.layer1, resnet.layer2, resnet.layer3, resnet.layer4
if output_stride == 8:
for n, m in self.layer3.named_modules():
if 'conv2' in n:
m.dilation, m.padding, m.stride = (2, 2), (2, 2), (1, 1)
elif 'downsample.0' in n:
m.stride = (1, 1)
for n, m in self.layer4.named_modules():
if 'conv2' in n:
m.dilation, m.padding, m.stride = (4, 4), (4, 4), (1, 1)
elif 'downsample.0' in n:
m.stride = (1, 1)
elif output_stride == 16:
for n, m in self.layer4.named_modules():
if 'conv2' in n:
m.dilation, m.padding, m.stride = (2, 2), (2, 2), (1, 1)
elif 'downsample.0' in n:
m.stride = (1, 1)
else:
raise 'unsupported output_stride {}'.format(output_stride)
def forward(self, x):
x = self.layer0(x)
x = self.layer1(x)
s2_features = x
x = self.layer2(x)
s4_features = x
x = self.layer3(x)
x = self.layer4(x)
return s2_features, s4_features, x
def get_trunk(trunk_name, output_stride=8):
"""
Retrieve the network trunk and channel counts.
"""
assert output_stride == 8, 'Only stride8 supported right now'
if trunk_name == 'wrn38':
#
# FIXME: pass in output_stride once we support stride 16
#
backbone = wrn38(pretrained=True)
s2_ch = 128
s4_ch = 256
high_level_ch = 4096
elif trunk_name == 'xception71':
backbone = xception71(output_stride=output_stride, BatchNorm=Norm2d,
pretrained=True)
s2_ch = 64
s4_ch = 128
high_level_ch = 2048
elif trunk_name == 'seresnext-50' or trunk_name == 'seresnext-101':
backbone = get_resnet(trunk_name, output_stride=output_stride)
s2_ch = 48
s4_ch = -1
high_level_ch = 2048
elif trunk_name == 'resnet-50' or trunk_name == 'resnet-101':
backbone = get_resnet(trunk_name, output_stride=output_stride)
s2_ch = 256
s4_ch = -1
high_level_ch = 2048
elif trunk_name == 'hrnetv2':
backbone = hrnetv2.get_seg_model()
high_level_ch = backbone.high_level_ch
s2_ch = -1
s4_ch = -1
else:
raise 'unknown backbone {}'.format(trunk_name)
logx.msg("Trunk: {}".format(trunk_name))
return backbone, s2_ch, s4_ch, high_level_ch
class ConvBnRelu(nn.Module):
# https://github.com/lingtengqiu/Deeperlab-pytorch/blob/master/seg_opr/seg_oprs.py
def __init__(self, in_planes, out_planes, kernel_size, stride=1, padding=0,
norm_layer=Norm2d):
super(ConvBnRelu, self).__init__()
self.conv = nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size,
stride=stride, padding=padding, bias=False)
self.bn = norm_layer(out_planes, eps=1e-5)
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 AtrousSpatialPyramidPoolingModule(nn.Module):
"""
operations performed:
1x1 x depth
3x3 x depth dilation 6
3x3 x depth dilation 12
3x3 x depth dilation 18
image pooling
concatenate all together
Final 1x1 conv
"""
def __init__(self, in_dim, reduction_dim=256, output_stride=16,
rates=(6, 12, 18)):
super(AtrousSpatialPyramidPoolingModule, self).__init__()
if output_stride == 8:
rates = [2 * r for r in rates]
elif output_stride == 16:
pass
else:
raise 'output stride of {} not supported'.format(output_stride)
self.features = []
# 1x1
self.features.append(
nn.Sequential(nn.Conv2d(in_dim, reduction_dim, kernel_size=1,
bias=False),
Norm2d(reduction_dim), nn.ReLU(inplace=True)))
# other rates
for r in rates:
self.features.append(nn.Sequential(
nn.Conv2d(in_dim, reduction_dim, kernel_size=3,
dilation=r, padding=r, bias=False),
Norm2d(reduction_dim),
nn.ReLU(inplace=True)
))
self.features = nn.ModuleList(self.features)
# img level features
self.img_pooling = nn.AdaptiveAvgPool2d(1)
self.img_conv = nn.Sequential(
nn.Conv2d(in_dim, reduction_dim, kernel_size=1, bias=False),
Norm2d(reduction_dim), nn.ReLU(inplace=True))
def forward(self, x):
x_size = x.size()
img_features = self.img_pooling(x)
img_features = self.img_conv(img_features)
img_features = Upsample(img_features, x_size[2:])
out = img_features
for f in self.features:
y = f(x)
out = torch.cat((out, y), 1)
return out
class ASPP_edge(AtrousSpatialPyramidPoolingModule):
def __init__(self, in_dim, reduction_dim=256, output_stride=16,
rates=(6, 12, 18)):
super(ASPP_edge, self).__init__(in_dim=in_dim,
reduction_dim=reduction_dim,
output_stride=output_stride,
rates=rates)
self.edge_conv = nn.Sequential(
nn.Conv2d(1, reduction_dim, kernel_size=1, bias=False),
Norm2d(reduction_dim), nn.ReLU(inplace=True))
def forward(self, x, edge):
x_size = x.size()
img_features = self.img_pooling(x)
img_features = self.img_conv(img_features)
img_features = Upsample(img_features, x_size[2:])
out = img_features
edge_features = Upsample(edge, x_size[2:])
edge_features = self.edge_conv(edge_features)
out = torch.cat((out, edge_features), 1)
for f in self.features:
y = f(x)
out = torch.cat((out, y), 1)
return out
def dpc_conv(in_dim, reduction_dim, dil, separable):
if separable:
groups = reduction_dim
else:
groups = 1
return nn.Sequential(
nn.Conv2d(in_dim, reduction_dim, kernel_size=3, dilation=dil,
padding=dil, bias=False, groups=groups),
nn.BatchNorm2d(reduction_dim),
nn.ReLU(inplace=True)
)
class DPC(nn.Module):
'''
From: Searching for Efficient Multi-scale architectures for dense
prediction
'''
def __init__(self, in_dim, reduction_dim=256, output_stride=16,
rates=[(1, 6), (18, 15), (6, 21), (1, 1), (6, 3)],
dropout=False, separable=False):
super(DPC, self).__init__()
self.dropout = dropout
if output_stride == 8:
rates = [(2 * r[0], 2 * r[1]) for r in rates]
elif output_stride == 16:
pass
else:
raise 'output stride of {} not supported'.format(output_stride)
self.a = dpc_conv(in_dim, reduction_dim, rates[0], separable)
self.b = dpc_conv(reduction_dim, reduction_dim, rates[1], separable)
self.c = dpc_conv(reduction_dim, reduction_dim, rates[2], separable)
self.d = dpc_conv(reduction_dim, reduction_dim, rates[3], separable)
self.e = dpc_conv(reduction_dim, reduction_dim, rates[4], separable)
self.drop = nn.Dropout(p=0.1)
def forward(self, x):
a = self.a(x)
b = self.b(a)
c = self.c(a)
d = self.d(a)
e = self.e(b)
out = torch.cat((a, b, c, d, e), 1)
if self.dropout:
out = self.drop(out)
return out
def get_aspp(high_level_ch, bottleneck_ch, output_stride, dpc=False):
"""
Create aspp block
"""
if dpc:
aspp = DPC(high_level_ch, bottleneck_ch, output_stride=output_stride)
else:
aspp = AtrousSpatialPyramidPoolingModule(high_level_ch, bottleneck_ch,
output_stride=output_stride)
aspp_out_ch = 5 * bottleneck_ch
return aspp, aspp_out_ch
def BNReLU(ch):
return nn.Sequential(
Norm2d(ch),
nn.ReLU())
def make_seg_head(in_ch, out_ch):
bot_ch = cfg.MODEL.SEGATTN_BOT_CH
return nn.Sequential(
nn.Conv2d(in_ch, bot_ch, kernel_size=3, padding=1, bias=False),
Norm2d(bot_ch),
nn.ReLU(inplace=True),
nn.Conv2d(bot_ch, bot_ch, kernel_size=3, padding=1, bias=False),
Norm2d(bot_ch),
nn.ReLU(inplace=True),
nn.Conv2d(bot_ch, out_ch, kernel_size=1, bias=False))
def init_attn(m):
for module in m.modules():
if isinstance(module, (nn.Conv2d, nn.Linear)):
nn.init.zeros_(module.weight)
if module.bias is not None:
nn.init.constant_(module.bias, 0.5)
elif isinstance(module, cfg.MODEL.BNFUNC):
module.weight.data.fill_(1)
module.bias.data.zero_()
def make_attn_head(in_ch, out_ch):
bot_ch = cfg.MODEL.SEGATTN_BOT_CH
if cfg.MODEL.MSCALE_OLDARCH:
return old_make_attn_head(in_ch, bot_ch, out_ch)
od = OrderedDict([('conv0', nn.Conv2d(in_ch, bot_ch, kernel_size=3,
padding=1, bias=False)),
('bn0', Norm2d(bot_ch)),
('re0', nn.ReLU(inplace=True))])
if cfg.MODEL.MSCALE_INNER_3x3:
od['conv1'] = nn.Conv2d(bot_ch, bot_ch, kernel_size=3, padding=1,
bias=False)
od['bn1'] = Norm2d(bot_ch)
od['re1'] = nn.ReLU(inplace=True)
if cfg.MODEL.MSCALE_DROPOUT:
od['drop'] = nn.Dropout(0.5)
od['conv2'] = nn.Conv2d(bot_ch, out_ch, kernel_size=1, bias=False)
od['sig'] = nn.Sigmoid()
attn_head = nn.Sequential(od)
# init_attn(attn_head)
return attn_head
def old_make_attn_head(in_ch, bot_ch, out_ch):
attn = nn.Sequential(
nn.Conv2d(in_ch, bot_ch, kernel_size=3, padding=1, bias=False),
Norm2d(bot_ch),
nn.ReLU(inplace=True),
nn.Conv2d(bot_ch, bot_ch, kernel_size=3, padding=1, bias=False),
Norm2d(bot_ch),
nn.ReLU(inplace=True),
nn.Conv2d(bot_ch, out_ch, kernel_size=out_ch, bias=False),
nn.Sigmoid())
init_attn(attn)
return attn