-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeepLabV3.py
362 lines (286 loc) · 13.2 KB
/
DeepLabV3.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
"""
This is the implementation of DeepLabv3+ without multi-scale inputs. This implementation uses ResNet-101 as backbone.
This deeplab is used with imagenet pretraining to match the current pytorch implementation that provides these weights.
This implementation follows the new implementation of Resnet bottleneck module where the stride is performed in the 3x3 conv.
Code taken from https://github.com/WilhelmT/ClassMix
Slightly modified
"""
import torch.nn as nn
import math
import torch.utils.model_zoo as model_zoo
import torch
import numpy as np
affine_par = True
import torch.nn.functional as F
BatchNorm = nn.BatchNorm2d
class Decoder(nn.Module):
def __init__(self, num_classes):
super(Decoder, self).__init__()
low_level_inplanes = 256
self.conv1 = nn.Conv2d(low_level_inplanes, 48, 1, bias=False)
self.bn1 = BatchNorm(48)
self.relu = nn.ReLU()
self.pre_last_conv = nn.Sequential(nn.Conv2d(304, 256, kernel_size=3, stride=1, padding=1, bias=False),
BatchNorm(256),
nn.ReLU(),
nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1, bias=False),
BatchNorm(256),
nn.ReLU())
self.last_dropout = nn.Dropout(0.1)
self.last_conv = nn.Conv2d(256, num_classes, kernel_size=1, stride=1)
self.interp = nn.Upsample(size=(512, 512), mode='bilinear', align_corners=True)
self._init_weight()
def forward(self, x, low_level_feat, return_features=True):
'''x: high level feature(aspp) (N, 256, 32, 32)
x_low_level_feat: (N, 256, 128, 128)'''
low_level_feat = self.conv1(low_level_feat)
low_level_feat = self.bn1(low_level_feat)
low_level_feat = self.relu(low_level_feat) # (N, 48, 128, 128)
x = F.interpolate(x, size=low_level_feat.size()[2:], mode='bilinear', align_corners=True) # (N, 256, 128, 128)
x = torch.cat((x, low_level_feat), dim=1) # (N, 256+48, 128, 128)
x_f = self.pre_last_conv(x) # (N, 256, 128, 128)
x = self.last_dropout(x_f) # (N, 256, 128, 128)
x = self.last_conv(x) # (N, num_classes, 128, 128)
x = self.interp(x)
if return_features:
return x, x_f
return x
def _init_weight(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
torch.nn.init.kaiming_normal_(m.weight)
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
class _ASPPModule(nn.Module):
def __init__(self, inplanes, planes, kernel_size, padding, dilation):
super(_ASPPModule, self).__init__()
self.atrous_conv = nn.Conv2d(inplanes, planes, kernel_size=kernel_size,
stride=1, padding=padding, dilation=dilation, bias=False)
self.bn = BatchNorm(planes)
self.relu = nn.ReLU()
self._init_weight()
def forward(self, x):
x = self.atrous_conv(x)
x = self.bn(x)
return self.relu(x)
def _init_weight(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
torch.nn.init.kaiming_normal_(m.weight)
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
class ASPP(nn.Module):
def __init__(self):
super(ASPP, self).__init__()
inplanes = 2048
dilations = [1, 6, 12, 18]
self.aspp1 = _ASPPModule(inplanes, 256, 1, padding=0, dilation=dilations[0])
self.aspp2 = _ASPPModule(inplanes, 256, 3, padding=dilations[1], dilation=dilations[1])
self.aspp3 = _ASPPModule(inplanes, 256, 3, padding=dilations[2], dilation=dilations[2])
self.aspp4 = _ASPPModule(inplanes, 256, 3, padding=dilations[3], dilation=dilations[3])
self.global_avg_pool = nn.Sequential(nn.AdaptiveAvgPool2d((1, 1)),
nn.Conv2d(inplanes, 256, 1, stride=1, bias=False),
BatchNorm(256),
nn.ReLU())
self.conv1 = nn.Conv2d(1280, 256, 1, bias=False)
self.bn1 = BatchNorm(256)
self.relu = nn.ReLU()
self.dropout = nn.Dropout(0.5)
self._init_weight()
def forward(self, x):
x1 = self.aspp1(x)
x2 = self.aspp2(x)
x3 = self.aspp3(x)
x4 = self.aspp4(x)
x5 = self.global_avg_pool(x)
x5 = F.interpolate(x5, size=x4.size()[2:], mode='bilinear', align_corners=True)
x = torch.cat((x1, x2, x3, x4, x5), dim=1)
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
return self.dropout(x)
def _init_weight(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
torch.nn.init.kaiming_normal_(m.weight)
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def outS(i):
i = int(i)
i = (i+1)/2
i = int(np.ceil((i+1)/2.0))
i = (i+1)/2
return i
def conv3x3(in_planes, out_planes, stride=1):
"3x3 convolution with padding"
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
padding=1, bias=False)
class Bottleneck(nn.Module):
expansion = 4
def __init__(self, inplanes, planes, stride=1, dilation=1, downsample=None):
super(Bottleneck, self).__init__()
self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, stride=1, bias=False) # change
self.bn1 = nn.BatchNorm2d(planes,affine = affine_par)
for i in self.bn1.parameters():
i.requires_grad = False
padding = dilation
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, # change
padding=padding, bias=False, dilation = dilation)
self.bn2 = nn.BatchNorm2d(planes,affine = affine_par)
for i in self.bn2.parameters():
i.requires_grad = False
self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False)
self.bn3 = nn.BatchNorm2d(planes * 4, affine = affine_par)
for i in self.bn3.parameters():
i.requires_grad = False
self.relu = nn.ReLU(inplace=True)
self.downsample = downsample
self.stride = stride
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.relu(out)
out = self.conv3(out)
out = self.bn3(out)
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
class ResNet(nn.Module):
def __init__(self, block=Bottleneck, num_classes=3, output_stride=16):
print('Creating Semantic DeepLabV3 with {} classes'.format(num_classes))
self.inplanes = 64
super(ResNet, self).__init__()
self.num_classes= num_classes
if output_stride == 16:
strides = [2, 2, 1]
dilations = [1, 1, 2]
elif output_stride == 8:
strides = [2, 1, 1]
dilations = [ 1, 2, 4]
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
bias=False)
self.bn1 = nn.BatchNorm2d(64, affine = affine_par)
for i in self.bn1.parameters():
i.requires_grad = False
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) # change
layers = [3, 4, 6, 3]
self.layer1 = self._make_layer(block, 64, layers[0])
self.layer2 = self._make_layer(block, 128, layers[1], stride=strides[0], dilation=dilations[0])
self.layer3 = self._make_layer(block, 256, layers[2], stride=strides[1], dilation=dilations[1])
self.layer4 = self._make_layer(block, 512, layers[3], stride=strides[2], dilation=dilations[2])
self.aspp = ASPP()
self.decoder = Decoder(num_classes)
dim_in = 256
feat_dim = 256
self.projection_head = nn.Sequential(
nn.Linear(dim_in, feat_dim),
nn.BatchNorm1d(feat_dim),
nn.ReLU(inplace=True),
nn.Linear(feat_dim, feat_dim)
)
self.prediction_head = nn.Sequential(
nn.Linear(feat_dim, feat_dim),
nn.BatchNorm1d(feat_dim),
nn.ReLU(inplace=True),
nn.Linear(feat_dim, feat_dim)
)
for class_c in range(num_classes):
selector = nn.Sequential(
nn.Linear(feat_dim, feat_dim),
nn.BatchNorm1d(feat_dim),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Linear(feat_dim, 1)
)
self.__setattr__('contrastive_class_selector_' + str(class_c), selector)
for class_c in range(num_classes):
selector = nn.Sequential(
nn.Linear(feat_dim, feat_dim),
nn.BatchNorm1d(feat_dim),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Linear(feat_dim, 1)
)
self.__setattr__('contrastive_class_selector_memory' + str(class_c), selector)
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, 0.01)
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def _make_layer(self, block, planes, blocks, stride=1, dilation=1):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion or dilation == 2 or dilation == 4:
downsample = nn.Sequential(
nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(planes * block.expansion,affine = affine_par))
for i in downsample._modules['1'].parameters():
i.requires_grad = False
layers = []
layers.append(block(self.inplanes, planes, stride,dilation=dilation, downsample=downsample))
self.inplanes = planes * block.expansion
for i in range(1, blocks):
layers.append(block(self.inplanes, planes, dilation=dilation))
return nn.Sequential(*layers)
def _make_pred_layer(self,block, dilation_series, padding_series,num_classes):
return block(dilation_series,padding_series,num_classes)
def forward_projection_head(self, features):
return self.projection_head(features)
def forward_prediction_head(self, features):
return self.prediction_head(features)
def forward(self, x, return_features=True): # assume x: (N, 3, 512, 512)
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x) # (N, 64, 256, 256)
x = self.maxpool(x) # (N, 64, 128, 128)
low_level_feat = self.layer1(x) # (N, 256, 128, 128)
x = self.layer2(low_level_feat) # (N, 512, 64, 64)
x = self.layer3(x) # (N, 1024, 32, 32)
x = self.layer4(x) # (N, 2048, 32, 32)
x = self.aspp(x) # (N, 256, 32, 32)
if return_features:
x, features = self.decoder(x, low_level_feat, True)
return x, features # (N, num_classes, 128, 128), (N, 256, 128, 128)
else:
x = self.decoder(x, low_level_feat, False)
return x
def get_1x_lr_params(self):
"""
This generator returns all the parameters of the net except for
the last classification layer. Note that for each batchnorm layer,
requires_grad is set to False in deeplab_resnet.py, therefore this function does not return
any batchnorm parameter
"""
b = []
b.append(self.conv1)
b.append(self.bn1)
b.append(self.layer1)
b.append(self.layer2)
b.append(self.layer3)
b.append(self.layer4)
b.append(self.aspp)
b.append(self.decoder)
b.append(self.projection_head)
b.append(self.prediction_head)
for class_c in range(self.num_classes):
b.append(self.__getattr__('contrastive_class_selector_' + str(class_c)))
b.append(self.__getattr__('contrastive_class_selector_memory' + str(class_c)))
for i in range(len(b)):
for k in b[i].parameters():
if k.requires_grad:
yield k
def optim_parameters(self, args):
return [{'params': self.get_1x_lr_params(), 'lr': args.learning_rate}]
def Res_Deeplab50(num_classes, os=16):
model = ResNet(Bottleneck,[3, 4, 6, 3], num_classes, output_stride=os)
return model