doevent commited on
Commit
84b4706
·
1 Parent(s): f4de781

Upload models/network.py

Browse files
Files changed (1) hide show
  1. models/network.py +352 -0
models/network.py ADDED
@@ -0,0 +1,352 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import torch.nn.functional as F
4
+ from torch.nn import init
5
+ import torchvision
6
+ import torch.nn.utils.spectral_norm as spectral_norm
7
+ import math
8
+
9
+
10
+ class ConvBlock(nn.Module):
11
+ def __init__(self, inChannels, outChannels, convNum, normLayer=None):
12
+ super(ConvBlock, self).__init__()
13
+ self.inConv = nn.Sequential(
14
+ nn.Conv2d(inChannels, outChannels, kernel_size=3, padding=1),
15
+ nn.ReLU(inplace=True)
16
+ )
17
+ layers = []
18
+ for _ in range(convNum - 1):
19
+ layers.append(nn.Conv2d(outChannels, outChannels, kernel_size=3, padding=1))
20
+ layers.append(nn.ReLU(inplace=True))
21
+ if not (normLayer is None):
22
+ layers.append(normLayer(outChannels))
23
+ self.conv = nn.Sequential(*layers)
24
+
25
+ def forward(self, x):
26
+ x = self.inConv(x)
27
+ x = self.conv(x)
28
+ return x
29
+
30
+
31
+ class ResidualBlock(nn.Module):
32
+ def __init__(self, channels, normLayer=None):
33
+ super(ResidualBlock, self).__init__()
34
+ layers = []
35
+ layers.append(nn.Conv2d(channels, channels, kernel_size=3, padding=1))
36
+ layers.append(spectral_norm(nn.Conv2d(channels, channels, kernel_size=3, padding=1)))
37
+ if not (normLayer is None):
38
+ layers.append(normLayer(channels))
39
+ layers.append(nn.ReLU(inplace=True))
40
+ layers.append(nn.Conv2d(channels, channels, kernel_size=3, padding=1))
41
+ if not (normLayer is None):
42
+ layers.append(normLayer(channels))
43
+ self.conv = nn.Sequential(*layers)
44
+
45
+ def forward(self, x):
46
+ residual = self.conv(x)
47
+ return F.relu(x + residual, inplace=True)
48
+
49
+
50
+ class ResidualBlockSN(nn.Module):
51
+ def __init__(self, channels, normLayer=None):
52
+ super(ResidualBlockSN, self).__init__()
53
+ layers = []
54
+ layers.append(spectral_norm(nn.Conv2d(channels, channels, kernel_size=3, padding=1)))
55
+ layers.append(nn.LeakyReLU(0.2, True))
56
+ layers.append(spectral_norm(nn.Conv2d(channels, channels, kernel_size=3, padding=1)))
57
+ if not (normLayer is None):
58
+ layers.append(normLayer(channels))
59
+ self.conv = nn.Sequential(*layers)
60
+
61
+ def forward(self, x):
62
+ residual = self.conv(x)
63
+ return F.leaky_relu(x + residual, 2e-1, inplace=True)
64
+
65
+
66
+ class DownsampleBlock(nn.Module):
67
+ def __init__(self, inChannels, outChannels, convNum=2, normLayer=None):
68
+ super(DownsampleBlock, self).__init__()
69
+ layers = []
70
+ layers.append(nn.Conv2d(inChannels, outChannels, kernel_size=3, padding=1, stride=2))
71
+ layers.append(nn.ReLU(inplace=True))
72
+ for _ in range(convNum - 1):
73
+ layers.append(nn.Conv2d(outChannels, outChannels, kernel_size=3, padding=1))
74
+ layers.append(nn.ReLU(inplace=True))
75
+ if not (normLayer is None):
76
+ layers.append(normLayer(outChannels))
77
+ self.conv = nn.Sequential(*layers)
78
+
79
+ def forward(self, x):
80
+ return self.conv(x)
81
+
82
+
83
+ class UpsampleBlock(nn.Module):
84
+ def __init__(self, inChannels, outChannels, convNum=2, normLayer=None):
85
+ super(UpsampleBlock, self).__init__()
86
+ self.conv1 = nn.Conv2d(inChannels, outChannels, kernel_size=3, padding=1, stride=1)
87
+ self.combine = nn.Conv2d(2 * outChannels, outChannels, kernel_size=3, padding=1)
88
+ layers = []
89
+ for _ in range(convNum - 1):
90
+ layers.append(nn.Conv2d(outChannels, outChannels, kernel_size=3, padding=1))
91
+ layers.append(nn.ReLU(inplace=True))
92
+ if not (normLayer is None):
93
+ layers.append(normLayer(outChannels))
94
+ self.conv2 = nn.Sequential(*layers)
95
+
96
+ def forward(self, x, x0):
97
+ x = self.conv1(x)
98
+ x = F.interpolate(x, scale_factor=2, mode='nearest')
99
+ x = self.combine(torch.cat((x, x0), 1))
100
+ x = F.relu(x)
101
+ return self.conv2(x)
102
+
103
+
104
+ class UpsampleBlockSN(nn.Module):
105
+ def __init__(self, inChannels, outChannels, convNum=2, normLayer=None):
106
+ super(UpsampleBlockSN, self).__init__()
107
+ self.conv1 = spectral_norm(nn.Conv2d(inChannels, outChannels, kernel_size=3, stride=1, padding=1))
108
+ self.shortcut = spectral_norm(nn.Conv2d(outChannels, outChannels, kernel_size=3, stride=1, padding=1))
109
+ layers = []
110
+ for _ in range(convNum - 1):
111
+ layers.append(spectral_norm(nn.Conv2d(outChannels, outChannels, kernel_size=3, padding=1)))
112
+ layers.append(nn.LeakyReLU(0.2, True))
113
+ if not (normLayer is None):
114
+ layers.append(normLayer(outChannels))
115
+ self.conv2 = nn.Sequential(*layers)
116
+
117
+ def forward(self, x, x0):
118
+ x = self.conv1(x)
119
+ x = F.interpolate(x, scale_factor=2, mode='nearest')
120
+ x = x + self.shortcut(x0)
121
+ x = F.leaky_relu(x, 2e-1)
122
+ return self.conv2(x)
123
+
124
+
125
+ class HourGlass2(nn.Module):
126
+ def __init__(self, inChannel=3, outChannel=1, resNum=3, normLayer=None):
127
+ super(HourGlass2, self).__init__()
128
+ self.inConv = ConvBlock(inChannel, 64, convNum=2, normLayer=normLayer)
129
+ self.down1 = DownsampleBlock(64, 128, convNum=2, normLayer=normLayer)
130
+ self.down2 = DownsampleBlock(128, 256, convNum=2, normLayer=normLayer)
131
+ self.residual = nn.Sequential(*[ResidualBlock(256) for _ in range(resNum)])
132
+ self.up2 = UpsampleBlock(256, 128, convNum=3, normLayer=normLayer)
133
+ self.up1 = UpsampleBlock(128, 64, convNum=3, normLayer=normLayer)
134
+ self.outConv = nn.Conv2d(64, outChannel, kernel_size=3, padding=1)
135
+
136
+ def forward(self, x):
137
+ f1 = self.inConv(x)
138
+ f2 = self.down1(f1)
139
+ f3 = self.down2(f2)
140
+ r3 = self.residual(f3)
141
+ r2 = self.up2(r3, f2)
142
+ r1 = self.up1(r2, f1)
143
+ y = self.outConv(r1)
144
+ return y
145
+
146
+
147
+ class ColorProbNet(nn.Module):
148
+ def __init__(self, inChannel=1, outChannel=2, with_SA=False):
149
+ super(ColorProbNet, self).__init__()
150
+ BNFunc = nn.BatchNorm2d
151
+ # conv1: 256
152
+ conv1_2 = [spectral_norm(nn.Conv2d(inChannel, 64, 3, stride=1, padding=1)), nn.LeakyReLU(0.2, True),]
153
+ conv1_2 += [spectral_norm(nn.Conv2d(64, 64, 3, stride=1, padding=1)), nn.LeakyReLU(0.2, True),]
154
+ conv1_2 += [BNFunc(64, affine=True)]
155
+ # conv2: 128
156
+ conv2_3 = [spectral_norm(nn.Conv2d(64, 128, 3, stride=2, padding=1)), nn.LeakyReLU(0.2, True),]
157
+ conv2_3 += [spectral_norm(nn.Conv2d(128, 128, 3, stride=1, padding=1)), nn.LeakyReLU(0.2, True),]
158
+ conv2_3 += [spectral_norm(nn.Conv2d(128, 128, 3, stride=1, padding=1)), nn.LeakyReLU(0.2, True),]
159
+ conv2_3 += [BNFunc(128, affine=True)]
160
+ # conv3: 64
161
+ conv3_3 = [spectral_norm(nn.Conv2d(128, 256, 3, stride=2, padding=1)), nn.LeakyReLU(0.2, True),]
162
+ conv3_3 += [spectral_norm(nn.Conv2d(256, 256, 3, stride=1, padding=1)), nn.LeakyReLU(0.2, True),]
163
+ conv3_3 += [spectral_norm(nn.Conv2d(256, 256, 3, stride=1, padding=1)), nn.LeakyReLU(0.2, True),]
164
+ conv3_3 += [BNFunc(256, affine=True)]
165
+ # conv4: 32
166
+ conv4_3 = [spectral_norm(nn.Conv2d(256, 512, 3, stride=2, padding=1)), nn.LeakyReLU(0.2, True),]
167
+ conv4_3 += [spectral_norm(nn.Conv2d(512, 512, 3, stride=1, padding=1)), nn.LeakyReLU(0.2, True),]
168
+ conv4_3 += [spectral_norm(nn.Conv2d(512, 512, 3, stride=1, padding=1)), nn.LeakyReLU(0.2, True),]
169
+ conv4_3 += [BNFunc(512, affine=True)]
170
+ # conv5: 32
171
+ conv5_3 = [spectral_norm(nn.Conv2d(512, 512, 3, stride=1, padding=1)), nn.LeakyReLU(0.2, True),]
172
+ conv5_3 += [spectral_norm(nn.Conv2d(512, 512, 3, stride=1, padding=1)), nn.LeakyReLU(0.2, True),]
173
+ conv5_3 += [spectral_norm(nn.Conv2d(512, 512, 3, stride=1, padding=1)), nn.LeakyReLU(0.2, True),]
174
+ conv5_3 += [BNFunc(512, affine=True)]
175
+ # conv6: 32
176
+ conv6_3 = [spectral_norm(nn.Conv2d(512, 512, 3, stride=1, padding=1)), nn.LeakyReLU(0.2, True),]
177
+ conv6_3 += [spectral_norm(nn.Conv2d(512, 512, 3, stride=1, padding=1)), nn.LeakyReLU(0.2, True),]
178
+ conv6_3 += [spectral_norm(nn.Conv2d(512, 512, 3, stride=1, padding=1)), nn.LeakyReLU(0.2, True),]
179
+ conv6_3 += [BNFunc(512, affine=True),]
180
+ if with_SA:
181
+ conv6_3 += [Self_Attn(512)]
182
+ # conv7: 32
183
+ conv7_3 = [spectral_norm(nn.Conv2d(512, 512, 3, stride=1, padding=1)), nn.LeakyReLU(0.2, True),]
184
+ conv7_3 += [spectral_norm(nn.Conv2d(512, 512, 3, stride=1, padding=1)), nn.LeakyReLU(0.2, True),]
185
+ conv7_3 += [spectral_norm(nn.Conv2d(512, 512, 3, stride=1, padding=1)), nn.LeakyReLU(0.2, True),]
186
+ conv7_3 += [BNFunc(512, affine=True)]
187
+ # conv8: 64
188
+ conv8up = [nn.Upsample(scale_factor=2, mode='nearest'), nn.Conv2d(512, 256, 3, stride=1, padding=1),]
189
+ conv3short8 = [nn.Conv2d(256, 256, 3, stride=1, padding=1),]
190
+ conv8_3 = [nn.ReLU(True),]
191
+ conv8_3 += [nn.Conv2d(256, 256, 3, stride=1, padding=1), nn.ReLU(True),]
192
+ conv8_3 += [nn.Conv2d(256, 256, 3, stride=1, padding=1), nn.ReLU(True),]
193
+ conv8_3 += [BNFunc(256, affine=True),]
194
+ # conv9: 128
195
+ conv9up = [nn.Upsample(scale_factor=2, mode='nearest'), nn.Conv2d(256, 128, 3, stride=1, padding=1),]
196
+ conv9_2 = [nn.Conv2d(128, 128, 3, stride=1, padding=1), nn.ReLU(True),]
197
+ conv9_2 += [BNFunc(128, affine=True)]
198
+ # conv10: 64
199
+ conv10up = [nn.Upsample(scale_factor=2, mode='nearest'), nn.Conv2d(128, 64, 3, stride=1, padding=1),]
200
+ conv10_2 = [nn.ReLU(True),]
201
+ conv10_2 += [nn.Conv2d(64, outChannel, 3, stride=1, padding=1), nn.ReLU(True),]
202
+
203
+ self.conv1_2 = nn.Sequential(*conv1_2)
204
+ self.conv2_3 = nn.Sequential(*conv2_3)
205
+ self.conv3_3 = nn.Sequential(*conv3_3)
206
+ self.conv4_3 = nn.Sequential(*conv4_3)
207
+ self.conv5_3 = nn.Sequential(*conv5_3)
208
+ self.conv6_3 = nn.Sequential(*conv6_3)
209
+ self.conv7_3 = nn.Sequential(*conv7_3)
210
+ self.conv8up = nn.Sequential(*conv8up)
211
+ self.conv3short8 = nn.Sequential(*conv3short8)
212
+ self.conv8_3 = nn.Sequential(*conv8_3)
213
+ self.conv9up = nn.Sequential(*conv9up)
214
+ self.conv9_2 = nn.Sequential(*conv9_2)
215
+ self.conv10up = nn.Sequential(*conv10up)
216
+ self.conv10_2 = nn.Sequential(*conv10_2)
217
+ # claffificaton output
218
+ #self.model_class = nn.Sequential(*[nn.Conv2d(256, 313, kernel_size=1, padding=0, stride=1),])
219
+
220
+ def forward(self, input_grays):
221
+ f1_2 = self.conv1_2(input_grays)
222
+ f2_3 = self.conv2_3(f1_2)
223
+ f3_3 = self.conv3_3(f2_3)
224
+ f4_3 = self.conv4_3(f3_3)
225
+ f5_3 = self.conv5_3(f4_3)
226
+ f6_3 = self.conv6_3(f5_3)
227
+ f7_3 = self.conv7_3(f6_3)
228
+ f8_up = self.conv8up(f7_3) + self.conv3short8(f3_3)
229
+ f8_3 = self.conv8_3(f8_up)
230
+ f9_up = self.conv9up(f8_3)
231
+ f9_2 = self.conv9_2(f9_up)
232
+ f10_up = self.conv10up(f9_2)
233
+ f10_2 = self.conv10_2(f10_up)
234
+ out_feats = f10_2
235
+ #out_probs = self.model_class(f8_3)
236
+ return out_feats
237
+
238
+
239
+
240
+ def conv(batchNorm, in_planes, out_planes, kernel_size=3, stride=1):
241
+ if batchNorm:
242
+ return nn.Sequential(
243
+ nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size, stride=stride, padding=(kernel_size-1)//2, bias=False),
244
+ nn.BatchNorm2d(out_planes),
245
+ nn.LeakyReLU(0.1)
246
+ )
247
+ else:
248
+ return nn.Sequential(
249
+ nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size, stride=stride, padding=(kernel_size-1)//2, bias=True),
250
+ nn.LeakyReLU(0.1)
251
+ )
252
+
253
+
254
+ def deconv(in_planes, out_planes):
255
+ return nn.Sequential(
256
+ nn.ConvTranspose2d(in_planes, out_planes, kernel_size=4, stride=2, padding=1, bias=True),
257
+ nn.LeakyReLU(0.1)
258
+ )
259
+
260
+ class SpixelNet(nn.Module):
261
+ def __init__(self, inChannel=3, outChannel=9, batchNorm=True):
262
+ super(SpixelNet,self).__init__()
263
+ self.batchNorm = batchNorm
264
+ self.conv0a = conv(self.batchNorm, inChannel, 16, kernel_size=3)
265
+ self.conv0b = conv(self.batchNorm, 16, 16, kernel_size=3)
266
+ self.conv1a = conv(self.batchNorm, 16, 32, kernel_size=3, stride=2)
267
+ self.conv1b = conv(self.batchNorm, 32, 32, kernel_size=3)
268
+ self.conv2a = conv(self.batchNorm, 32, 64, kernel_size=3, stride=2)
269
+ self.conv2b = conv(self.batchNorm, 64, 64, kernel_size=3)
270
+ self.conv3a = conv(self.batchNorm, 64, 128, kernel_size=3, stride=2)
271
+ self.conv3b = conv(self.batchNorm, 128, 128, kernel_size=3)
272
+ self.conv4a = conv(self.batchNorm, 128, 256, kernel_size=3, stride=2)
273
+ self.conv4b = conv(self.batchNorm, 256, 256, kernel_size=3)
274
+ self.deconv3 = deconv(256, 128)
275
+ self.conv3_1 = conv(self.batchNorm, 256, 128)
276
+ self.deconv2 = deconv(128, 64)
277
+ self.conv2_1 = conv(self.batchNorm, 128, 64)
278
+ self.deconv1 = deconv(64, 32)
279
+ self.conv1_1 = conv(self.batchNorm, 64, 32)
280
+ self.deconv0 = deconv(32, 16)
281
+ self.conv0_1 = conv(self.batchNorm, 32, 16)
282
+ self.pred_mask0 = nn.Conv2d(16, outChannel, kernel_size=3, stride=1, padding=1, bias=True)
283
+ self.softmax = nn.Softmax(1)
284
+ for m in self.modules():
285
+ if isinstance(m, nn.Conv2d) or isinstance(m, nn.ConvTranspose2d):
286
+ init.kaiming_normal_(m.weight, 0.1)
287
+ if m.bias is not None:
288
+ init.constant_(m.bias, 0)
289
+ elif isinstance(m, nn.BatchNorm2d):
290
+ init.constant_(m.weight, 1)
291
+ init.constant_(m.bias, 0)
292
+
293
+ def forward(self, x):
294
+ out1 = self.conv0b(self.conv0a(x)) #5*5
295
+ out2 = self.conv1b(self.conv1a(out1)) #11*11
296
+ out3 = self.conv2b(self.conv2a(out2)) #23*23
297
+ out4 = self.conv3b(self.conv3a(out3)) #47*47
298
+ out5 = self.conv4b(self.conv4a(out4)) #95*95
299
+ out_deconv3 = self.deconv3(out5)
300
+ concat3 = torch.cat((out4, out_deconv3), 1)
301
+ out_conv3_1 = self.conv3_1(concat3)
302
+ out_deconv2 = self.deconv2(out_conv3_1)
303
+ concat2 = torch.cat((out3, out_deconv2), 1)
304
+ out_conv2_1 = self.conv2_1(concat2)
305
+ out_deconv1 = self.deconv1(out_conv2_1)
306
+ concat1 = torch.cat((out2, out_deconv1), 1)
307
+ out_conv1_1 = self.conv1_1(concat1)
308
+ out_deconv0 = self.deconv0(out_conv1_1)
309
+ concat0 = torch.cat((out1, out_deconv0), 1)
310
+ out_conv0_1 = self.conv0_1(concat0)
311
+ mask0 = self.pred_mask0(out_conv0_1)
312
+ prob0 = self.softmax(mask0)
313
+ return prob0
314
+
315
+
316
+
317
+ ## VGG architecter, used for the perceptual loss using a pretrained VGG network
318
+ class VGG19(torch.nn.Module):
319
+ def __init__(self, requires_grad=False, local_pretrained_path='checkpoints/vgg19.pth'):
320
+ super().__init__()
321
+ #vgg_pretrained_features = torchvision.models.vgg19(pretrained=True).features
322
+ model = torchvision.models.vgg19()
323
+ model.load_state_dict(torch.load(local_pretrained_path))
324
+ vgg_pretrained_features = model.features
325
+
326
+ self.slice1 = torch.nn.Sequential()
327
+ self.slice2 = torch.nn.Sequential()
328
+ self.slice3 = torch.nn.Sequential()
329
+ self.slice4 = torch.nn.Sequential()
330
+ self.slice5 = torch.nn.Sequential()
331
+ for x in range(2):
332
+ self.slice1.add_module(str(x), vgg_pretrained_features[x])
333
+ for x in range(2, 7):
334
+ self.slice2.add_module(str(x), vgg_pretrained_features[x])
335
+ for x in range(7, 12):
336
+ self.slice3.add_module(str(x), vgg_pretrained_features[x])
337
+ for x in range(12, 21):
338
+ self.slice4.add_module(str(x), vgg_pretrained_features[x])
339
+ for x in range(21, 30):
340
+ self.slice5.add_module(str(x), vgg_pretrained_features[x])
341
+ if not requires_grad:
342
+ for param in self.parameters():
343
+ param.requires_grad = False
344
+
345
+ def forward(self, X):
346
+ h_relu1 = self.slice1(X)
347
+ h_relu2 = self.slice2(h_relu1)
348
+ h_relu3 = self.slice3(h_relu2)
349
+ h_relu4 = self.slice4(h_relu3)
350
+ h_relu5 = self.slice5(h_relu4)
351
+ out = [h_relu1, h_relu2, h_relu3, h_relu4, h_relu5]
352
+ return out