-
Notifications
You must be signed in to change notification settings - Fork 0
/
pspnet.py
executable file
·412 lines (319 loc) · 14.3 KB
/
pspnet.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
import torch
from torch import nn
from torch.nn import functional as F
from models.psp import extractors
from models.sync_batchnorm import SynchronizedBatchNorm2d
class PSPModule(nn.Module):
def __init__(self, features, out_features=1024, sizes=(1, 2, 3, 6)):
super().__init__()
self.stages = []
self.stages = nn.ModuleList([self._make_stage(features, size) for size in sizes])
self.bottleneck = nn.Conv2d(features * (len(sizes) + 1), out_features, kernel_size=1)
self.relu = nn.ReLU(inplace=True)
def _make_stage(self, features, size):
prior = nn.AdaptiveAvgPool2d(output_size=(size, size))
conv = nn.Conv2d(features, features, kernel_size=1, bias=False)
return nn.Sequential(prior, conv)
def forward(self, feats):
h, w = feats.size(2), feats.size(3)
set_priors = [F.interpolate(input=stage(feats), size=(h, w), mode='bilinear', align_corners=False) for stage in self.stages]
priors = set_priors + [feats]
bottle = self.bottleneck(torch.cat(priors, 1))
return self.relu(bottle)
class PSPUpsample(nn.Module):
def __init__(self, x_channels, in_channels, out_channels):
super().__init__()
self.conv = nn.Sequential(
SynchronizedBatchNorm2d(in_channels),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels, out_channels, 3, padding=1),
SynchronizedBatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, 3, padding=1),
)
self.conv2 = nn.Sequential(
SynchronizedBatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, 3, padding=1),
SynchronizedBatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, 3, padding=1),
)
self.shortcut = nn.Conv2d(x_channels, out_channels, kernel_size=1)
def forward(self, x, up):
x = F.interpolate(input=x, scale_factor=2, mode='bilinear', align_corners=False)
p = self.conv(torch.cat([x, up], 1))
sc = self.shortcut(x)
p = p + sc
p2 = self.conv2(p)
return p + p2
class PSPNet(nn.Module):
def __init__(self, sizes=(1, 2, 3, 6), psp_size=2048, deep_features_size=1024, backend='resnet34',
pretrained=True):
super().__init__()
self.feats = getattr(extractors, backend)(pretrained)
self.psp = PSPModule(psp_size, 1024, sizes)
self.up_1 = PSPUpsample(1024, 1024+256, 512)
self.up_2 = PSPUpsample(512, 512+64, 256)
self.up_3 = PSPUpsample(256, 256+3, 32)
self.final_28 = nn.Sequential(
nn.Conv2d(1024, 32, kernel_size=1),
nn.ReLU(inplace=True),
nn.Conv2d(32, 1, kernel_size=1),
)
self.final_56 = nn.Sequential(
nn.Conv2d(512, 32, kernel_size=1),
nn.ReLU(inplace=True),
nn.Conv2d(32, 1, kernel_size=1),
)
self.final_11 = nn.Conv2d(32+3, 32, kernel_size=1)
self.final_21 = nn.Conv2d(32, 1, kernel_size=1)
def forward(self, x, seg, inter_s8=None, inter_s4=None):
images = {}
"""
First iteration, s8 output
"""
if inter_s8 is None:
p = torch.cat((x, seg, seg, seg), 1)
f, f_1, f_2 = self.feats(p)
p = self.psp(f)
inter_s8 = self.final_28(p)
r_inter_s8 = F.interpolate(inter_s8, scale_factor=8, mode='bilinear', align_corners=False)
r_inter_tanh_s8 = torch.tanh(r_inter_s8)
images['pred_28'] = torch.sigmoid(r_inter_s8)
images['out_28'] = r_inter_s8
else:
r_inter_tanh_s8 = inter_s8
"""
Second iteration, s4 output
"""
if inter_s4 is None:
p = torch.cat((x, seg, r_inter_tanh_s8, r_inter_tanh_s8), 1)
f, f_1, f_2 = self.feats(p)
p = self.psp(f)
inter_s8_2 = self.final_28(p)
r_inter_s8_2 = F.interpolate(inter_s8_2, scale_factor=8, mode='bilinear', align_corners=False)
r_inter_tanh_s8_2 = torch.tanh(r_inter_s8_2)
p = self.up_1(p, f_2)
inter_s4 = self.final_56(p)
r_inter_s4 = F.interpolate(inter_s4, scale_factor=4, mode='bilinear', align_corners=False)
r_inter_tanh_s4 = torch.tanh(r_inter_s4)
images['pred_28_2'] = torch.sigmoid(r_inter_s8_2)
images['out_28_2'] = r_inter_s8_2
images['pred_56'] = torch.sigmoid(r_inter_s4)
images['out_56'] = r_inter_s4
else:
r_inter_tanh_s8_2 = inter_s8
r_inter_tanh_s4 = inter_s4
"""
Third iteration, s1 output
"""
p = torch.cat((x, seg, r_inter_tanh_s8_2, r_inter_tanh_s4), 1)
f, f_1, f_2 = self.feats(p)
p = self.psp(f)
inter_s8_3 = self.final_28(p)
r_inter_s8_3 = F.interpolate(inter_s8_3, scale_factor=8, mode='bilinear', align_corners=False)
p = self.up_1(p, f_2)
inter_s4_2 = self.final_56(p)
r_inter_s4_2 = F.interpolate(inter_s4_2, scale_factor=4, mode='bilinear', align_corners=False)
p = self.up_2(p, f_1)
p = self.up_3(p, x)
"""
Final output
"""
p = F.relu(self.final_11(torch.cat([p, x], 1)), inplace=True)
p = self.final_21(p)
pred_224 = torch.sigmoid(p)
images['pred_224'] = pred_224
images['out_224'] = p
images['pred_28_3'] = torch.sigmoid(r_inter_s8_3)
images['pred_56_2'] = torch.sigmoid(r_inter_s4_2)
images['out_28_3'] = r_inter_s8_3
images['out_56_2'] = r_inter_s4_2
return images
class PSPNet_UOAIS(nn.Module):
def __init__(self, sizes=(1, 2, 3, 6), psp_size=2048, deep_features_size=1024, backend='resnet34',
pretrained=True):
super().__init__()
print("[DEBUG] : backbone", backend)
self.feats = getattr(extractors, backend)(pretrained)
# print('model, feats : ',self.feats)
self.psp = PSPModule(psp_size, 1024, sizes)
# self.catconv = nn.Conv2d(4, 3, kernel_size=1, stride=1, padding=0) #Edited : Add a Conv2d layer to reduce dimention 4 to 3
self.up_1 = PSPUpsample(1024, 1024+256, 512)
self.up_2 = PSPUpsample(512, 512+64, 256)
self.up_3 = PSPUpsample(256, 256+3, 32)
self.final_28 = nn.Sequential(
nn.Conv2d(1024, 32, kernel_size=1),
nn.ReLU(inplace=True),
nn.Conv2d(32, 1, kernel_size=1),
)
self.final_56 = nn.Sequential(
nn.Conv2d(512, 32, kernel_size=1),
nn.ReLU(inplace=True),
nn.Conv2d(32, 1, kernel_size=1),
)
self.final_11 = nn.Conv2d(32+3, 32, kernel_size=1)
self.final_21 = nn.Conv2d(32, 1, kernel_size=1)
def forward(self, x, depth, seg, inter_s8=None, inter_s4=None):
images = {}
"""
First iteration, s8 output
"""
if inter_s8 is None:
# cat = torch.cat((x, depth), 1) #Edited : C/oncat RGB and depth -> 4channel
# p = self.catconv(cat) #Edited : Reduce d/imention 4 to 3
p = torch.cat((x, depth, seg, seg, seg), 1)
f, f_1, f_2 = self.feats(p)
p = self.psp(f)
inter_s8 = self.final_28(p)
r_inter_s8 = F.interpolate(inter_s8, scale_factor=8, mode='bilinear', align_corners=False)
r_inter_tanh_s8 = torch.tanh(r_inter_s8)
images['pred_28'] = torch.sigmoid(r_inter_s8)
images['out_28'] = r_inter_s8
else:
r_inter_tanh_s8 = inter_s8
"""
Second iteration, s4 output
"""
if inter_s4 is None:
# cat = torch.cat((x, depth), 1) #Edited : Concat RGB and depth -> 4channel
# p = self.catconv(cat) #Edited : Reduce dimention 4 to 3
p = torch.cat((x, depth, seg, r_inter_tanh_s8, r_inter_tanh_s8), 1)
f, f_1, f_2 = self.feats(p)
p = self.psp(f)
inter_s8_2 = self.final_28(p)
r_inter_s8_2 = F.interpolate(inter_s8_2, scale_factor=8, mode='bilinear', align_corners=False)
r_inter_tanh_s8_2 = torch.tanh(r_inter_s8_2)
p = self.up_1(p, f_2)
inter_s4 = self.final_56(p)
r_inter_s4 = F.interpolate(inter_s4, scale_factor=4, mode='bilinear', align_corners=False)
r_inter_tanh_s4 = torch.tanh(r_inter_s4)
images['pred_28_2'] = torch.sigmoid(r_inter_s8_2)
images['out_28_2'] = r_inter_s8_2
images['pred_56'] = torch.sigmoid(r_inter_s4)
images['out_56'] = r_inter_s4
else:
r_inter_tanh_s8_2 = inter_s8
r_inter_tanh_s4 = inter_s4
"""
Third iteration, s1 output
"""
# cat = torch.cat((x, depth), 1) #Edited : Concat RGB and depth -> 4channel
# p = self.catconv(cat) #Edited : Reduce dimention 4 to 3
p = torch.cat((x, depth, seg, r_inter_tanh_s8_2, r_inter_tanh_s4), 1)
f, f_1, f_2 = self.feats(p)
p = self.psp(f)
inter_s8_3 = self.final_28(p)
r_inter_s8_3 = F.interpolate(inter_s8_3, scale_factor=8, mode='bilinear', align_corners=False)
p = self.up_1(p, f_2)
inter_s4_2 = self.final_56(p)
r_inter_s4_2 = F.interpolate(inter_s4_2, scale_factor=4, mode='bilinear', align_corners=False)
p = self.up_2(p, f_1)
p = self.up_3(p, x)
"""
Final output
"""
p = F.relu(self.final_11(torch.cat([p, x], 1)), inplace=True)
p = self.final_21(p)
pred_224 = torch.sigmoid(p)
images['pred_224'] = pred_224
images['out_224'] = p
images['pred_28_3'] = torch.sigmoid(r_inter_s8_3)
images['pred_56_2'] = torch.sigmoid(r_inter_s4_2)
images['out_28_3'] = r_inter_s8_3
images['out_56_2'] = r_inter_s4_2
return images
# class PSPNet_UOAIS(nn.Module):
# def __init__(self, sizes=(1, 2, 3, 6), psp_size=2048, deep_features_size=1024, backend='resnet34',
# pretrained=True):
# super().__init__()
# self.feats = getattr(extractors, backend)(pretrained)
# # print('model, feats : ',self.feats)
# self.psp = PSPModule(psp_size, 1024, sizes)
# self.catconv = nn.Conv2d(4, 3, kernel_size=1, stride=1, padding=0) #Edited : Add a Conv2d layer to reduce dimention 4 to 3
# self.up_1 = PSPUpsample(1024, 1024+256, 512)
# self.up_2 = PSPUpsample(512, 512+64, 256)
# self.up_3 = PSPUpsample(256, 256+3, 32)
# self.final_28 = nn.Sequential(
# nn.Conv2d(1024, 32, kernel_size=1),
# nn.ReLU(inplace=True),
# nn.Conv2d(32, 1, kernel_size=1),
# )
# self.final_56 = nn.Sequential(
# nn.Conv2d(512, 32, kernel_size=1),
# nn.ReLU(inplace=True),
# nn.Conv2d(32, 1, kernel_size=1),
# )
# self.final_11 = nn.Conv2d(32+3, 32, kernel_size=1)
# self.final_21 = nn.Conv2d(32, 1, kernel_size=1)
# def forward(self, x, depth, seg, inter_s8=None, inter_s4=None):
# images = {}
# """
# First iteration, s8 output
# """
# if inter_s8 is None:
# cat = torch.cat((x, depth), 1) #Edited : Concat RGB and depth -> 4channel
# p = self.catconv(cat) #Edited : Reduce dimention 4 to 3
# p = torch.cat((p, seg, seg, seg), 1)
# # print('p shape', p.shape)
# f, f_1, f_2 = self.feats(p)
# p = self.psp(f)
# inter_s8 = self.final_28(p)
# r_inter_s8 = F.interpolate(inter_s8, scale_factor=8, mode='bilinear', align_corners=False)
# r_inter_tanh_s8 = torch.tanh(r_inter_s8)
# images['pred_28'] = torch.sigmoid(r_inter_s8)
# images['out_28'] = r_inter_s8
# else:
# r_inter_tanh_s8 = inter_s8
# """
# Second iteration, s4 output
# """
# if inter_s4 is None:
# cat = torch.cat((x, depth), 1) #Edited : Concat RGB and depth -> 4channel
# p = self.catconv(cat) #Edited : Reduce dimention 4 to 3
# p = torch.cat((p, seg, r_inter_tanh_s8, r_inter_tanh_s8), 1)
# f, f_1, f_2 = self.feats(p)
# p = self.psp(f)
# inter_s8_2 = self.final_28(p)
# r_inter_s8_2 = F.interpolate(inter_s8_2, scale_factor=8, mode='bilinear', align_corners=False)
# r_inter_tanh_s8_2 = torch.tanh(r_inter_s8_2)
# p = self.up_1(p, f_2)
# inter_s4 = self.final_56(p)
# r_inter_s4 = F.interpolate(inter_s4, scale_factor=4, mode='bilinear', align_corners=False)
# r_inter_tanh_s4 = torch.tanh(r_inter_s4)
# images['pred_28_2'] = torch.sigmoid(r_inter_s8_2)
# images['out_28_2'] = r_inter_s8_2
# images['pred_56'] = torch.sigmoid(r_inter_s4)
# images['out_56'] = r_inter_s4
# else:
# r_inter_tanh_s8_2 = inter_s8
# r_inter_tanh_s4 = inter_s4
# """
# Third iteration, s1 output
# """
# cat = torch.cat((x, depth), 1) #Edited : Concat RGB and depth -> 4channel
# p = self.catconv(cat) #Edited : Reduce dimention 4 to 3
# p = torch.cat((p, seg, r_inter_tanh_s8_2, r_inter_tanh_s4), 1)
# f, f_1, f_2 = self.feats(p)
# p = self.psp(f)
# inter_s8_3 = self.final_28(p)
# r_inter_s8_3 = F.interpolate(inter_s8_3, scale_factor=8, mode='bilinear', align_corners=False)
# p = self.up_1(p, f_2)
# inter_s4_2 = self.final_56(p)
# r_inter_s4_2 = F.interpolate(inter_s4_2, scale_factor=4, mode='bilinear', align_corners=False)
# p = self.up_2(p, f_1)
# p = self.up_3(p, x)
# """
# Final output
# """
# p = F.relu(self.final_11(torch.cat([p, x], 1)), inplace=True)
# p = self.final_21(p)
# pred_224 = torch.sigmoid(p)
# images['pred_224'] = pred_224
# images['out_224'] = p
# images['pred_28_3'] = torch.sigmoid(r_inter_s8_3)
# images['pred_56_2'] = torch.sigmoid(r_inter_s4_2)
# images['out_28_3'] = r_inter_s8_3
# images['out_56_2'] = r_inter_s4_2
# return images