forked from commaai/speedchallenge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
depth_and_motion_net.py
75 lines (55 loc) · 4.23 KB
/
depth_and_motion_net.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
import tensorflow as tf
def depth_and_motion_net(inputs):
conv1 = tf.keras.layers.Conv2D(16, (7, 7), strides=2, activation=tf.nn.relu, padding='same', name='conv1')(inputs)
conv2 = tf.keras.layers.Conv2D(32, (5, 5), strides=2, activation=tf.nn.relu, padding='same', name='conv2')(conv1)
conv3 = tf.keras.layers.Conv2D(64, (3, 3), strides=2, activation=tf.nn.relu, padding='same', name='conv3')(conv2)
conv4 = tf.keras.layers.Conv2D(128, (3, 3), strides=2, activation=tf.nn.relu, padding='same', name='conv4')(conv3)
conv5 = tf.keras.layers.Conv2D(256, (3, 3), strides=2, activation=tf.nn.relu, padding='same', name='conv5')(conv4)
conv6 = tf.keras.layers.Conv2D(256, (3, 3), strides=2, activation=tf.nn.relu, padding='same', name='conv6')(conv5)
conv7 = tf.keras.layers.Conv2D(256, (3, 3), strides=2, activation=tf.nn.relu, padding='same', name='conv7')(conv6)
bottleneck = tf.keras.layers.Lambda(lambda x: tf.reduce_mean(x, [1, 2], keepdims=True))(conv7)
rotation = tf.keras.layers.Conv2D(3, (1, 1), name='rot_conv')(bottleneck)
translation = tf.keras.layers.Conv2D(3, (1, 1), name='trans_conv')(bottleneck)
rotation = tf.keras.layers.Lambda(lambda x: tf.squeeze(x, axis=(1, 2)))(rotation)
translation = tf.keras.layers.Lambda(lambda x: tf.squeeze(x, axis=(1, 2)))(translation)
rotation = Scale(0.001)(rotation)
translation = Scale(0.001)(translation)
return rotation, translation
def depth_and_motion_net_fc(inputs):
conv1 = tf.keras.layers.Conv2D(16, (7, 7), strides=2, activation=tf.nn.relu, padding='same', name='conv1')(inputs)
conv2 = tf.keras.layers.Conv2D(32, (5, 5), strides=2, activation=tf.nn.relu, padding='same', name='conv2')(conv1)
conv3 = tf.keras.layers.Conv2D(64, (3, 3), strides=2, activation=tf.nn.relu, padding='same', name='conv3')(conv2)
conv4 = tf.keras.layers.Conv2D(128, (3, 3), strides=2, activation=tf.nn.relu, padding='same', name='conv4')(conv3)
conv5 = tf.keras.layers.Conv2D(256, (3, 3), strides=2, activation=tf.nn.relu, padding='same', name='conv5')(conv4)
conv6 = tf.keras.layers.Conv2D(256, (3, 3), strides=2, activation=tf.nn.relu, padding='same', name='conv6')(conv5)
conv7 = tf.keras.layers.Conv2D(256, (3, 3), strides=2, activation=tf.nn.relu, padding='same', name='conv7')(conv6)
bottleneck = tf.keras.layers.Lambda(lambda x: tf.reduce_mean(x, [1, 2]))(conv7)
rotation = tf.keras.layers.Dense(3, name='rot_fc')(bottleneck)
translation = tf.keras.layers.Dense(3, name='trans_fc')(bottleneck)
rotation = Scale(0.001)(rotation)
translation = Scale(0.001)(translation)
return rotation, translation
def depth_and_motion_net_fc_no_mean(inputs):
conv1 = tf.keras.layers.Conv2D(16, (7, 7), strides=2, activation=tf.nn.relu, padding='same', name='conv1')(inputs)
conv2 = tf.keras.layers.Conv2D(32, (5, 5), strides=2, activation=tf.nn.relu, padding='same', name='conv2')(conv1)
conv3 = tf.keras.layers.Conv2D(64, (3, 3), strides=2, activation=tf.nn.relu, padding='same', name='conv3')(conv2)
conv4 = tf.keras.layers.Conv2D(128, (3, 3), strides=2, activation=tf.nn.relu, padding='same', name='conv4')(conv3)
conv5 = tf.keras.layers.Conv2D(256, (3, 3), strides=2, activation=tf.nn.relu, padding='same', name='conv5')(conv4)
conv6 = tf.keras.layers.Conv2D(256, (3, 3), strides=2, activation=tf.nn.relu, padding='same', name='conv6')(conv5)
conv7 = tf.keras.layers.Conv2D(256, (3, 3), strides=2, activation=tf.nn.relu, padding='same', name='conv7')(conv6)
flat = tf.keras.layers.Flatten()(conv7)
rotation = tf.keras.layers.Dense(3, name='rot_fc')(flat)
translation = tf.keras.layers.Dense(3, name='trans_fc')(flat)
rotation = Scale(0.001)(rotation)
translation = Scale(0.001)(translation)
return rotation, translation
class Scale(tf.keras.layers.Layer):
def __init__(self, constraint_minimum):
super(Scale, self).__init__()
self.constraint_minimum = constraint_minimum
def build(self, input_shape):
def constraint(x):
return tf.nn.relu(x - self.constraint_minimum) + self.constraint_minimum
self.scale = self.add_weight("scale", initializer=tf.keras.initializers.Constant(0.01), constraint=constraint)
def call(self, inputs):
return inputs * self.scale