forked from jiwei0921/DMRA
-
Notifications
You must be signed in to change notification settings - Fork 4
/
model.py
364 lines (305 loc) · 15 KB
/
model.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
import torch
import torch.nn as nn
import numpy as np
def get_upsampling_weight(in_channels, out_channels, kernel_size):
"""Make a 2D bilinear kernel suitable for upsampling"""
factor = (kernel_size + 1) // 2
if kernel_size % 2 == 1:
center = factor - 1
else:
center = factor - 0.5
og = np.ogrid[:kernel_size, :kernel_size]
filt = (1 - abs(og[0] - center) / factor) * \
(1 - abs(og[1] - center) / factor)
weight = np.zeros((in_channels, out_channels, kernel_size, kernel_size),
dtype=np.float64)
weight[range(in_channels), range(out_channels), :, :] = filt
return torch.from_numpy(weight).float()
#################################### RGB Network #####################################
class RGBNet(nn.Module):
def __init__(self,n_class=2):
super(RGBNet, self).__init__()
# original image's size = 256*256*3
# conv1
self.conv1_1 = nn.Conv2d(3, 64, 3, padding=1)
self.bn1_1 = nn.BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True)
self.relu1_1 = nn.ReLU(inplace=True)
self.conv1_2 = nn.Conv2d(64, 64, 3, padding=1)
self.bn1_2 = nn.BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True)
self.relu1_2 = nn.ReLU(inplace=True)
self.pool1 = nn.MaxPool2d(2, stride=2, ceil_mode=True) # 1/2 2 layers
# conv2
self.conv2_1 = nn.Conv2d(64, 128, 3, padding=1)
self.bn2_1 = nn.BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True)
self.relu2_1 = nn.ReLU(inplace=True)
self.conv2_2 = nn.Conv2d(128, 128, 3, padding=1)
self.bn2_2 = nn.BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True)
self.relu2_2 = nn.ReLU(inplace=True)
self.pool2 = nn.MaxPool2d(2, stride=2, ceil_mode=True) # 1/4 2 layers
# conv3
self.conv3_1 = nn.Conv2d(128, 256, 3, padding=1)
self.bn3_1 = nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True)
self.relu3_1 = nn.ReLU(inplace=True)
self.conv3_2 = nn.Conv2d(256, 256, 3, padding=1)
self.bn3_2 = nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True)
self.relu3_2 = nn.ReLU(inplace=True)
self.conv3_3 = nn.Conv2d(256, 256, 3, padding=1)
self.bn3_3 = nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True)
self.relu3_3 = nn.ReLU(inplace=True)
self.conv3_4 = nn.Conv2d(256, 256, 3, padding=1)
self.bn3_4 = nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True)
self.relu3_4 = nn.ReLU(inplace=True)
self.pool3 = nn.MaxPool2d(2, stride=2, ceil_mode=True) # 1/8 4 layers
# conv4
self.conv4_1 = nn.Conv2d(256, 512, 3, padding=1)
self.bn4_1 = nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True)
self.relu4_1 = nn.ReLU(inplace=True)
self.conv4_2 = nn.Conv2d(512, 512, 3, padding=1)
self.bn4_2 = nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True)
self.relu4_2 = nn.ReLU(inplace=True)
self.conv4_3 = nn.Conv2d(512, 512, 3, padding=1)
self.bn4_3 = nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True)
self.relu4_3 = nn.ReLU(inplace=True)
self.conv4_4 = nn.Conv2d(512, 512, 3, padding=1)
self.bn4_4 = nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True)
self.relu4_4 = nn.ReLU(inplace=True)
self.pool4 = nn.MaxPool2d(2, stride=2, ceil_mode=True) # 1/16 4 layers
# conv5
self.conv5_1 = nn.Conv2d(512, 512, 3, padding=1)
self.bn5_1 = nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True)
self.relu5_1 = nn.ReLU(inplace=True)
self.conv5_2 = nn.Conv2d(512, 512, 3, padding=1)
self.bn5_2 = nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True)
self.relu5_2 = nn.ReLU(inplace=True)
self.conv5_3 = nn.Conv2d(512, 512, 3, padding=1)
self.bn5_3 = nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True)
self.relu5_3 = nn.ReLU(inplace=True)
self.conv5_4 = nn.Conv2d(512, 512, 3, padding=1)
self.bn5_4 = nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True)
self.relu5_4 = nn.ReLU(inplace=True) # 1/32 4 layers
self._initialize_weights()
def _initialize_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
# m.weight.data.zero_()
nn.init.normal(m.weight.data, std=0.01)
if m.bias is not None:
m.bias.data.zero_()
if isinstance(m, nn.ConvTranspose2d):
assert m.kernel_size[0] == m.kernel_size[1]
initial_weight = get_upsampling_weight(m.in_channels, m.out_channels, m.kernel_size[0])
m.weight.data.copy_(initial_weight)
def forward(self, x):
h = x
h = self.relu1_1(self.bn1_1(self.conv1_1(h)))
h = self.relu1_2(self.bn1_2(self.conv1_2(h)))
h_nopool1 = h
h = self.pool1(h)
h1 = h_nopool1 # (256x256)*64
h = self.relu2_1(self.bn2_1(self.conv2_1(h)))
h = self.relu2_2(self.bn2_2(self.conv2_2(h)))
h_nopool2 = h
h = self.pool2(h)
h2 = h_nopool2 # (128x128)*128
h = self.relu3_1(self.bn3_1(self.conv3_1(h)))
h = self.relu3_2(self.bn3_2(self.conv3_2(h)))
h = self.relu3_3(self.bn3_3(self.conv3_3(h)))
h = self.relu3_4(self.bn3_4(self.conv3_4(h)))
h_nopool3 = h
h = self.pool3(h)
h3 = h_nopool3 # (64x64)*256
h = self.relu4_1(self.bn4_1(self.conv4_1(h)))
h = self.relu4_2(self.bn4_2(self.conv4_2(h)))
h = self.relu4_3(self.bn4_3(self.conv4_3(h)))
h = self.relu4_4(self.bn4_4(self.conv4_4(h)))
h_nopool4 = h
h = self.pool4(h)
h4 = h_nopool4 # (32x32)*512
h = self.relu5_1(self.bn5_1(self.conv5_1(h)))
h = self.relu5_2(self.bn5_2(self.conv5_2(h)))
h = self.relu5_3(self.bn5_3(self.conv5_3(h)))
h = self.relu5_4(self.bn5_4(self.conv5_4(h)))
h5 = h # (16x16)*512
return h1,h2,h3,h4,h5
def copy_params_from_vgg19_bn(self, vgg19_bn):
features = [
self.conv1_1, self.bn1_1, self.relu1_1,
self.conv1_2, self.bn1_2, self.relu1_2,
self.pool1,
self.conv2_1, self.bn2_1, self.relu2_1,
self.conv2_2, self.bn2_2, self.relu2_2,
self.pool2,
self.conv3_1, self.bn3_1, self.relu3_1,
self.conv3_2, self.bn3_2, self.relu3_2,
self.conv3_3, self.bn3_3, self.relu3_3,
self.conv3_4, self.bn3_4, self.relu3_4,
self.pool3,
self.conv4_1, self.bn4_1, self.relu4_1,
self.conv4_2, self.bn4_2, self.relu4_2,
self.conv4_3, self.bn4_3, self.relu4_3,
self.conv4_4, self.bn4_4, self.relu4_4,
self.pool4,
self.conv5_1, self.bn5_1, self.relu5_1,
self.conv5_2, self.bn5_2, self.relu5_2,
self.conv5_3, self.bn5_3, self.relu5_3,
self.conv5_4, self.bn5_4, self.relu5_4,
]
for l1, l2 in zip(vgg19_bn.features, features):
if isinstance(l1, nn.Conv2d) and isinstance(l2, nn.Conv2d):
assert l1.weight.size() == l2.weight.size()
assert l1.bias.size() == l2.bias.size()
l2.weight.data = l1.weight.data
l2.bias.data = l1.bias.data
if isinstance(l1, nn.BatchNorm2d) and isinstance(l2, nn.BatchNorm2d):
assert l1.weight.size() == l2.weight.size()
assert l1.bias.size() == l2.bias.size()
l2.weight.data = l1.weight.data
l2.bias.data = l1.bias.data
#################################### Depth Network #####################################
class DepthNet(nn.Module):
def __init__(self, n_class=2):
super(DepthNet, self).__init__()
# original image's size = 256*256*3
# conv1
self.conv1_1 = nn.Conv2d(3, 64, 3, padding=1)
self.bn1_1 = nn.BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True)
self.relu1_1 = nn.ReLU(inplace=True)
self.conv1_2 = nn.Conv2d(64, 64, 3, padding=1)
self.bn1_2 = nn.BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True)
self.relu1_2 = nn.ReLU(inplace=True)
self.pool1 = nn.MaxPool2d(2, stride=2, ceil_mode=True) # 1/2 2 layers
# conv2
self.conv2_1 = nn.Conv2d(64, 128, 3, padding=1)
self.bn2_1 = nn.BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True)
self.relu2_1 = nn.ReLU(inplace=True)
self.conv2_2 = nn.Conv2d(128, 128, 3, padding=1)
self.bn2_2 = nn.BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True)
self.relu2_2 = nn.ReLU(inplace=True)
self.pool2 = nn.MaxPool2d(2, stride=2, ceil_mode=True) # 1/4 2 layers
# conv3
self.conv3_1 = nn.Conv2d(128, 256, 3, padding=1)
self.bn3_1 = nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True)
self.relu3_1 = nn.ReLU(inplace=True)
self.conv3_2 = nn.Conv2d(256, 256, 3, padding=1)
self.bn3_2 = nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True)
self.relu3_2 = nn.ReLU(inplace=True)
self.conv3_3 = nn.Conv2d(256, 256, 3, padding=1)
self.bn3_3 = nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True)
self.relu3_3 = nn.ReLU(inplace=True)
self.conv3_4 = nn.Conv2d(256, 256, 3, padding=1)
self.bn3_4 = nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True)
self.relu3_4 = nn.ReLU(inplace=True)
self.pool3 = nn.MaxPool2d(2, stride=2, ceil_mode=True) # 1/8 4 layers
# conv4
self.conv4_1 = nn.Conv2d(256, 512, 3, padding=1)
self.bn4_1 = nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True)
self.relu4_1 = nn.ReLU(inplace=True)
self.conv4_2 = nn.Conv2d(512, 512, 3, padding=1)
self.bn4_2 = nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True)
self.relu4_2 = nn.ReLU(inplace=True)
self.conv4_3 = nn.Conv2d(512, 512, 3, padding=1)
self.bn4_3 = nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True)
self.relu4_3 = nn.ReLU(inplace=True)
self.conv4_4 = nn.Conv2d(512, 512, 3, padding=1)
self.bn4_4 = nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True)
self.relu4_4 = nn.ReLU(inplace=True)
self.pool4 = nn.MaxPool2d(2, stride=2, ceil_mode=True) # 1/16 4 layers
# conv5
self.conv5_1 = nn.Conv2d(512, 512, 3, padding=1)
self.bn5_1 = nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True)
self.relu5_1 = nn.ReLU(inplace=True)
self.conv5_2 = nn.Conv2d(512, 512, 3, padding=1)
self.bn5_2 = nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True)
self.relu5_2 = nn.ReLU(inplace=True)
self.conv5_3 = nn.Conv2d(512, 512, 3, padding=1)
self.bn5_3 = nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True)
self.relu5_3 = nn.ReLU(inplace=True)
self.conv5_4 = nn.Conv2d(512, 512, 3, padding=1)
self.bn5_4 = nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True)
self.relu5_4 = nn.ReLU(inplace=True) # 1/32 4 layers
# depth vector
self.conv_fcn2 = nn.Conv2d(512, 64, 3, padding=1)
self.pool_avg = nn.AvgPool2d(16, stride=2, ceil_mode=True)
self.conv_c = nn.Conv2d(64, 6, 1, padding=0)
self._initialize_weights()
def _initialize_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
# m.weight.data.zero_()
nn.init.normal(m.weight.data, std=0.01)
if m.bias is not None:
m.bias.data.zero_()
if isinstance(m, nn.ConvTranspose2d):
assert m.kernel_size[0] == m.kernel_size[1]
initial_weight = get_upsampling_weight(m.in_channels, m.out_channels, m.kernel_size[0])
m.weight.data.copy_(initial_weight)
def forward(self, x):
h = x
h = self.relu1_1(self.bn1_1(self.conv1_1(h)))
h = self.relu1_2(self.bn1_2(self.conv1_2(h)))
h_nopool1 = h
h = self.pool1(h)
d1 = h_nopool1 # (256x256)*64
h = self.relu2_1(self.bn2_1(self.conv2_1(h)))
h = self.relu2_2(self.bn2_2(self.conv2_2(h)))
h_nopool2 = h
h = self.pool2(h)
d2 = h_nopool2 # (128x128)*128
h = self.relu3_1(self.bn3_1(self.conv3_1(h)))
h = self.relu3_2(self.bn3_2(self.conv3_2(h)))
h = self.relu3_3(self.bn3_3(self.conv3_3(h)))
h = self.relu3_4(self.bn3_4(self.conv3_4(h)))
h_nopool3 = h
h = self.pool3(h)
d3 = h_nopool3 # (64x64)*256
h = self.relu4_1(self.bn4_1(self.conv4_1(h)))
h = self.relu4_2(self.bn4_2(self.conv4_2(h)))
h = self.relu4_3(self.bn4_3(self.conv4_3(h)))
h = self.relu4_4(self.bn4_4(self.conv4_4(h)))
h_nopool4 = h
h = self.pool4(h)
d4 = h_nopool4 # (32x32)*512
h = self.relu5_1(self.bn5_1(self.conv5_1(h)))
h = self.relu5_2(self.bn5_2(self.conv5_2(h)))
h = self.relu5_3(self.bn5_3(self.conv5_3(h)))
h = self.relu5_4(self.bn5_4(self.conv5_4(h)))
d5 = h # (16x16)*512
# depth vector
vector = self.conv_fcn2(d5)
vector = self.pool_avg(vector)
depth_vector = self.conv_c(vector)
return depth_vector, d1, d2, d3, d4, d5
def copy_params_from_vgg19_bn(self, vgg19_bn):
features = [
self.conv1_1, self.bn1_1, self.relu1_1,
self.conv1_2, self.bn1_2, self.relu1_2,
self.pool1,
self.conv2_1, self.bn2_1, self.relu2_1,
self.conv2_2, self.bn2_2, self.relu2_2,
self.pool2,
self.conv3_1, self.bn3_1, self.relu3_1,
self.conv3_2, self.bn3_2, self.relu3_2,
self.conv3_3, self.bn3_3, self.relu3_3,
self.conv3_4, self.bn3_4, self.relu3_4,
self.pool3,
self.conv4_1, self.bn4_1, self.relu4_1,
self.conv4_2, self.bn4_2, self.relu4_2,
self.conv4_3, self.bn4_3, self.relu4_3,
self.conv4_4, self.bn4_4, self.relu4_4,
self.pool4,
self.conv5_1, self.bn5_1, self.relu5_1,
self.conv5_2, self.bn5_2, self.relu5_2,
self.conv5_3, self.bn5_3, self.relu5_3,
self.conv5_4, self.bn5_4, self.relu5_4,
]
for l1, l2 in zip(vgg19_bn.features, features):
if isinstance(l1, nn.Conv2d) and isinstance(l2, nn.Conv2d):
assert l1.weight.size() == l2.weight.size()
assert l1.bias.size() == l2.bias.size()
l2.weight.data = l1.weight.data
l2.bias.data = l1.bias.data
if isinstance(l1, nn.BatchNorm2d) and isinstance(l2, nn.BatchNorm2d):
assert l1.weight.size() == l2.weight.size()
assert l1.bias.size() == l2.bias.size()
l2.weight.data = l1.weight.data
l2.bias.data = l1.bias.data