-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodels.py
More file actions
378 lines (322 loc) · 12 KB
/
Copy pathmodels.py
File metadata and controls
378 lines (322 loc) · 12 KB
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
'''
All neural networks and layers are defined here.
The main functions here are: PoseNet and DepthNet.
'''
from load_config import *
def get_pretrained_mobilenetv2(n_inputs=1, name=None, n_downsamples=4):
# Make model with desired amount of inputs
base_model = tf.keras.applications.MobileNetV2(
input_shape=[config.image_h, config.image_w, 3 * n_inputs],
include_top=False,
weights=None,
)
# base_model.summary()
# Load pretrained model
base_model_trained = tf.keras.applications.MobileNetV2(
input_shape=[config.image_h, config.image_w, 3],
include_top=False,
weights='imagenet',
)
# Copy weights
stop_pretrained = False
for v, v_trained in zip(
base_model.trainable_variables,
base_model_trained.trainable_variables,
):
if False: #re.match('block_13(.+)', v.name) or stop_pretrained:
print('Randomly initializing layer', v.name)
stop_pretrained = True
if re.match('Conv1(.+)kernel:0', v.name):
new_kernel = tf.concat([v_trained] * n_inputs, 2) / n_inputs
new_kernel = new_kernel.numpy()
noise = np.random.random(size=new_kernel.shape) * new_kernel.std() * config.init_binocular_noise
scale = 1 / np.sqrt(1 + config.init_binocular_noise**2)
v.assign((new_kernel + noise) * scale)
print('kernel found:', v.name)
else:
v.assign(v_trained)
# Use the activations of these layers
layer_names = [
'block_1_expand_relu', # /2
'block_3_expand_relu', # /4
'block_6_expand_relu', # /8
'block_13_expand_relu', # /16
#'block_16_project', # /32
'out_relu', # /32
][:n_downsamples]
layers = [base_model.get_layer(name).output for name in layer_names]
# Create the feature extraction model
down_stack = tf.keras.Model(inputs=base_model.input, outputs=layers, name=name)
# Scale image between -1 and 1
inputs = kl.Input(shape=[config.image_h, config.image_w, 3 * n_inputs])
x = inputs * 2 - 1
# x = tf.keras.applications.mobilenet_v2.preprocess_input(inputs * 255)
outputs = down_stack(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs, name=name)
return model
def get_pretrained_resnet50(n_inputs=1, name=None):
# Make model with desired amount of inputs
base_model = tf.keras.applications.ResNet50(
input_shape=[config.image_h, config.image_w, 3 * n_inputs],
include_top=False,
weights=None,
)
# base_model.summary()
# Load pretrained model
base_model_trained = tf.keras.applications.ResNet50(
input_shape=[config.image_h, config.image_w, 3],
include_top=False,
weights='imagenet',
)
# Copy weights
for v, v_trained in zip(
base_model.trainable_variables,
base_model_trained.trainable_variables,
):
if re.match('conv1_conv(.+)kernel:0', v.name):
new_kernel = tf.concat([v_trained] * n_inputs, 2) / n_inputs
new_kernel = new_kernel.numpy()
noise = np.random.random(size=new_kernel.shape) * new_kernel.std() * config.init_binocular_noise
scale = 1 / np.sqrt(1 + config.init_binocular_noise**2)
v.assign((new_kernel + noise) * scale)
print('kernel found:', v.name)
else:
v.assign(v_trained)
# Use the activations of these layers
layer_names = [
'conv1_relu', # /2
'conv2_block3_out', # /4
'conv3_block4_out', # /8
'conv4_block6_out', # /16
]
layers = [base_model.get_layer(name).output for name in layer_names]
# Create the feature extraction model
down_stack = tf.keras.Model(inputs=base_model.input, outputs=layers, name=name)
return down_stack
def get_pretrained_vgg16(name=None):
# Load pretrained model
base_model = tf.keras.applications.VGG16(
input_shape=[config.image_h, config.image_w, 3],
include_top=False,
weights='imagenet',
)
# base_model.summary()
# Use the activations of these layers
layer_name = 'block1_conv2'
layer = base_model.get_layer(layer_name).output
# Create the feature extraction model
down_stack = tf.keras.Model(inputs=base_model.input, outputs=layer, name=name)
down_stack.trainable = False
# Preprocess image
inputs = kl.Input(shape=[config.image_h, config.image_w, 3])
x = tf.keras.applications.vgg16.preprocess_input(inputs)
outputs = down_stack(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs, name=name)
return model
def get_pretrained(backbone, n_inputs=1, name=None, *args, **kwargs):
backbone_fn = {
'resnet50': get_pretrained_resnet50,
'mobilenetv2': get_pretrained_mobilenetv2,
}[backbone]
backbone = backbone_fn(n_inputs=n_inputs, name=name, *args, **kwargs)
return backbone
def reflection_pad(x):
# Reflection padding
x = tf.concat([x[:, 1:2], x, x[:, -2:-1]], 1)
x = tf.concat([x[:, :, 1:2], x, x[:, :, -2:-1]], 2)
return x
class BasicBlock(kl.Layer):
def __init__(self, n_units, stride=1, downsample=None, activation='relu'):
super().__init__()
self.n_units = n_units
self.stride = stride
self.downsample = downsample
self.activation = activation
self.conv1 = kl.Conv2D(
n_units, 3,
strides=(stride, stride),
padding='valid',
use_bias=False)
self.conv2 = kl.Conv2D(
n_units, 3,
padding='valid',
use_bias=False)
self.bn1 = kl.BatchNormalization(axis=-1)
self.bn2 = kl.BatchNormalization(axis=-1)
self.downsample = keras.Sequential([
kl.Conv2D(n_units, 1, strides=(stride, stride), padding='valid'),
kl.BatchNormalization(axis=-1),
])
if activation == 'relu':
self.activation = keras.activations.relu
elif activation is None:
self.activation = tf.identity
def call(self, x):
identity = x
x = reflection_pad(x)
x = self.conv1(x)
x = self.bn1(x)
x = keras.activations.relu(x)
x = reflection_pad(x)
x = self.conv2(x)
x = self.bn2(x)
x = (x + self.downsample(identity)) * np.sqrt(0.5)
x = self.activation(x)
return x
def get_config(self):
config = super().get_config().copy()
config.update({
'n_units': self.n_units,
'stride': self.stride,
'downsample': self.downsample,
'activation': self.activation,
})
return config
class UpSamplingBlock(kl.Layer):
def __init__(self, n_units, name=None):
super().__init__(name=name)
self.up = kl.UpSampling2D()
self.n_units = n_units
self.conv = BasicBlock(n_units)
def call(self, x, residual=None):
x = self.up(x)
if residual is not None:
x = tf.concat([x, residual], 3)
return self.conv(x)
def get_config(self):
config = super().get_config().copy()
config.update({
'n_units': self.n_units,
})
return config
def FlowNet():
#
# Define Layers
#
# Encoder
backbone = get_pretrained(config.backbone_flow, n_inputs=2, name='backbone')
# Flow decoder
upsample_16_8 = UpSamplingBlock(256, name='upsample_16_8')
upsample_8_4 = UpSamplingBlock(128, name='upsample_8_4')
upsample_4_2 = UpSamplingBlock(64, name='upsample_4_2')
upsample_2_1 = UpSamplingBlock(32, name='upsample_2_1')
# Final convolution layers
final_conv_1 = kl.Conv2D(2, 1, padding='same', activation=None, name='final_conv_1')
final_conv_mask = kl.Conv2D(1, 1, padding='same', activation='sigmoid', name='final_conv_mask')
final_conv_2 = keras.Sequential([
kl.Conv2D(2, 1, padding='same', activation=None),
kl.UpSampling2D(),
], name='final_conv_2')
final_conv_4 = keras.Sequential([
kl.Conv2D(2, 1, padding='same', activation=None),
kl.UpSampling2D(),
kl.UpSampling2D(),
], name='final_conv_4')
final_conv_8 = keras.Sequential([
kl.Conv2D(2, 1, padding='same', activation=None),
kl.UpSampling2D(),
kl.UpSampling2D(),
kl.UpSampling2D(),
], name='final_conv_8')
#
# Define graph
#
# Convolutions
inputs = kl.Input(shape=[config.image_h, config.image_w, 6])
x2, x4, x8, x16 = backbone(inputs)
# Upsamplings
x = x16
x = upsample_16_8(x, x8)
flow_8 = final_conv_8(x)
x = upsample_8_4(x, x4)
flow_4 = final_conv_4(x)
x = upsample_4_2(x, x2)
flow_2 = final_conv_2(x)
x = upsample_2_1(x)
flow_1 = final_conv_1(x)
flow_mask = final_conv_mask(x*0.01)
return Model(inputs=inputs, outputs=[flow_1, flow_2, flow_4, flow_8, flow_mask], name='flow_net')
def DepthNet():
#
# Define Layers
#
# Encoder
backbone = get_pretrained(config.backbone_depth, n_inputs=2, name='backbone')
# Depth decoder
upsample_16_8 = UpSamplingBlock(256, name='upsample_16_8')
upsample_8_4 = UpSamplingBlock(128, name='upsample_8_4')
upsample_4_2 = UpSamplingBlock(64, name='upsample_4_2')
upsample_2_1 = UpSamplingBlock(32, name='upsample_2_1')
# Final convolution layers
final_conv_1 = kl.Conv2D(1, 1, padding='same', activation=None, name='final_conv_1')
final_conv_2 = keras.Sequential([
kl.Conv2D(1, 1, padding='same', activation=None),
kl.UpSampling2D(),
], name='final_conv_2')
final_conv_4 = keras.Sequential([
kl.Conv2D(1, 1, padding='same', activation=None),
kl.UpSampling2D(),
kl.UpSampling2D(),
], name='final_conv_4')
final_conv_8 = keras.Sequential([
kl.Conv2D(1, 1, padding='same', activation=None),
kl.UpSampling2D(),
kl.UpSampling2D(),
kl.UpSampling2D(),
], name='final_conv_8')
#
# Define graph
#
# Convolutions
inputs = kl.Input(shape=[config.image_h, config.image_w, 6])
x2, x4, x8, x16 = backbone(inputs)
# Upsamplings
x = x16
x = upsample_16_8(x, x8)
depth_8 = final_conv_8(x)
x = upsample_8_4(x, x4)
depth_4 = final_conv_4(x)
x = upsample_4_2(x, x2)
depth_2 = final_conv_2(x)
x = upsample_2_1(x)
depth_1 = final_conv_1(x)
# Depth prediction
depths = []
for depth in [depth_1, depth_2, depth_4, depth_8]:
# formula from https://arxiv.org/pdf/1806.01260.pdf (monodepth2)
a = 1 / config.min_depth
b = 1 / config.max_depth
depth = 1 / (a * tf.math.sigmoid(depth - 2) + b)
depths.append(depth[:, :, :, 0])
return Model(inputs=inputs, outputs=depths, name='depth_net')
def PoseNet():
#
# Define Layers
#
# Encoder
backbone = get_pretrained(config.backbone_pose, n_inputs=4, name='backbone', n_downsamples=5)
flatten = kl.GlobalAveragePooling2D()
rotation_net = keras.Sequential([
kl.Dense(512, activation=config.posenet_activation),
kl.Dense(512, activation=config.posenet_activation),
kl.Dense(3, activation=None),
], name='rotation_net')
translation_net = keras.Sequential([
kl.Dense(512, activation=config.posenet_activation),
kl.Dense(512, activation=config.posenet_activation),
kl.Dense(3, activation=None),
], name='translation_net')
concat = kl.Concatenate(axis=1)
#
# Define graph
#
# Convolutions
inputs = kl.Input(shape=[config.image_h, config.image_w, 12])
x2, x4, x8, x16, x32 = backbone(inputs)
# Flatten
x = flatten(x32)
# Predict pose
rotation = rotation_net(x) * config.scale_rot # [batch, 3]
translation = translation_net(x) * config.scale_tr # [batch, 3]
output = concat([rotation, translation]) # [batch, 6]
return Model(inputs=inputs, outputs=output, name='pose_net')