-
Notifications
You must be signed in to change notification settings - Fork 11
/
pydnet.py
144 lines (133 loc) · 6.39 KB
/
pydnet.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
#
# MIT License
#
# Copyright (c) 2018 Matteo Poggi m.poggi@unibo.it
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from layers import *
class pydnet(object):
def __init__(self, placeholders=None):
self.model_collection = ['PyDnet']
self.placeholders = placeholders
self.build_model()
self.build_outputs()
def build_model(self):
with tf.variable_scope("pyramid") as scope:
pyramid = self.build_pyramid(self.placeholders['im0'])
# SCALE 6
with tf.variable_scope("L6") as scope:
with tf.variable_scope("estimator") as scope:
conv6 = self.build_estimator(pyramid[6])
self.disp7 = self.get_disp(conv6)
with tf.variable_scope("upsampler") as scope:
upconv6 = self.bilinear_upsampling_by_deconvolution(conv6)
# SCALE 5
with tf.variable_scope("L5") as scope:
with tf.variable_scope("estimator") as scope:
conv5 = self.build_estimator(pyramid[5], upconv6)
self.disp6 = self.get_disp(conv5)
with tf.variable_scope("upsampler") as scope:
upconv5 = self.bilinear_upsampling_by_deconvolution(conv5)
# SCALE 4
with tf.variable_scope("L4") as scope:
with tf.variable_scope("estimator") as scope:
conv4 = self.build_estimator(pyramid[4], upconv5)
self.disp5 = self.get_disp(conv4)
with tf.variable_scope("upsampler") as scope:
upconv4 = self.bilinear_upsampling_by_deconvolution(conv4)
# SCALE 3
with tf.variable_scope("L3") as scope:
with tf.variable_scope("estimator") as scope:
conv3 = self.build_estimator(pyramid[3], upconv4)
self.disp4 = self.get_disp(conv3)
with tf.variable_scope("upsampler") as scope:
upconv3 = self.bilinear_upsampling_by_deconvolution(conv3)
# SCALE 2
with tf.variable_scope("L2") as scope:
with tf.variable_scope("estimator") as scope:
conv2 = self.build_estimator(pyramid[2], upconv3)
self.disp3 = self.get_disp(conv2)
with tf.variable_scope("upsampler") as scope:
upconv2 = self.bilinear_upsampling_by_deconvolution(conv2)
# SCALE 1
with tf.variable_scope("L1") as scope:
with tf.variable_scope("estimator") as scope:
conv1 = self.build_estimator(pyramid[1], upconv2)
self.disp2 = self.get_disp(conv1)
# Pyramidal features extraction
def build_pyramid(self, input_batch):
features = []
features.append(input_batch)
with tf.variable_scope("conv1a"):
conv1a = conv2d_leaky(input_batch, [3, 3, 3, 16], [16], 2, True)
with tf.variable_scope("conv1b"):
conv1b = conv2d_leaky(conv1a, [3, 3, 16, 16], [16], 1, True)
features.append(conv1b)
with tf.variable_scope("conv2a"):
conv2a = conv2d_leaky(conv1b, [3, 3, 16, 32], [32], 2, True)
with tf.variable_scope("conv2b"):
conv2b = conv2d_leaky(conv2a, [3, 3, 32, 32], [32], 1, True)
features.append(conv2b)
with tf.variable_scope("conv3a"):
conv3a = conv2d_leaky(conv2b, [3, 3, 32, 64], [64], 2, True)
with tf.variable_scope("conv3b"):
conv3b = conv2d_leaky(conv3a, [3, 3, 64, 64], [64], 1, True)
features.append(conv3b)
with tf.variable_scope("conv4a"):
conv4a = conv2d_leaky(conv3b, [3, 3, 64, 96], [96], 2, True)
with tf.variable_scope("conv4b"):
conv4b = conv2d_leaky(conv4a, [3, 3, 96, 96], [96], 1, True)
features.append(conv4b)
with tf.variable_scope("conv5a"):
conv5a = conv2d_leaky(conv4b, [3, 3, 96, 128], [128], 2, True)
with tf.variable_scope("conv5b"):
conv5b = conv2d_leaky(conv5a, [3, 3, 128, 128], [128], 1, True)
features.append(conv5b)
with tf.variable_scope("conv6a"):
conv6a = conv2d_leaky(conv5b, [3, 3, 128, 192], [192], 2, True)
with tf.variable_scope("conv6b"):
conv6b = conv2d_leaky(conv6a, [3, 3, 192, 192], [192], 1, True)
features.append(conv6b)
return features
# Single scale estimator
def build_estimator(self, features, upsampled_disp=None):
if upsampled_disp is not None:
disp2 = tf.concat([features, upsampled_disp], -1)
else:
disp2 = features
with tf.variable_scope("disp-3") as scope:
disp3 = conv2d_leaky(disp2, [3, 3, disp2.shape[3], 96], [96], 1, True)
with tf.variable_scope("disp-4") as scope:
disp4 = conv2d_leaky(disp3, [3, 3, disp3.shape[3], 64], [64], 1, True)
with tf.variable_scope("disp-5") as scope:
disp5 = conv2d_leaky(disp4, [3, 3, disp4.shape[3], 32], [32], 1, True)
with tf.variable_scope("disp-6") as scope:
disp6 = conv2d_leaky(disp5, [3, 3, disp5.shape[3], 8], [8], 1, False) # 8 channels for compatibility with @other@ devices
return disp6
# Upsampling layer
def bilinear_upsampling_by_deconvolution(self,x):
f = x.get_shape().as_list()[-1]
return deconv2d_leaky(x, [2, 2, f, f], f, 2, True)
# Disparity prediction layer
def get_disp(self, x):
disp = 0.3 * tf.nn.sigmoid(tf.slice(x, [0,0,0,0], [-1,-1,-1,2]))
return disp
# Build multi-scale outputs
def build_outputs(self):
shape = tf.shape(self.placeholders['im0'])
size = [shape[1], shape[2]]
self.results = tf.image.resize_images(self.disp2, size), tf.image.resize_images(self.disp3, size), tf.image.resize_images(self.disp4, size)